Compiler - newlife-js/Wiki GitHub Wiki

by ์—ฐ์„ธ๋Œ€ํ•™๊ต ๊น€ํ•œ์ค€ ๊ต์ˆ˜๋‹˜

Compiler

Source Program์„ Target Program์œผ๋กœ ๋ฒˆ์—ญ + Optimizer

Accelerator Optimization

  • Increase Parallelism
    '# of cores(FLOPs) โ†‘
  • High communication bandwidth(CPU-GPU, GPU-GPU)
  • Domain specific customization CNN์— ๋น„ํ•ด RNN ๊ณ„์—ด์˜ compute-to-data ratio๊ฐ€ ๋‚ฎ์Œ -> Customizeํ•œ HW ๊ฐœ๋ฐœ(TPU ๋“ฑ)
  • Approximate computing
    Quantization, lower precision

Microsoft's BrainWave

DL Model์„ compileํ•ด์„œ FPGA์— ์˜ฌ๋ฆผ
ํ•˜๋‚˜์˜ FPGA์— model ์ „์ฒด๊ฐ€ ์˜ฌ๋ผ๊ฐ€์ง€ ๋ชปํ•ด์„œ partitioning์„ ํ†ตํ•ด ์—ฌ๋Ÿฌ ๊ฐœ์˜ FPGA์— ์˜ฌ๋ฆด ์ˆ˜ ์žˆ๋„๋ก ํ•จ

TVM

End-to-end optimizing compiler for DNN
image

  • Nvidia Diesel, Google XLA, MLIR

Front/Back end

  • Front end: ์ฝ”๋“œ๋ฅผ ์ง์—ญ
  • Back end: ์„ฑ๋Šฅ ํ–ฅ์ƒ์„ ์œ„ํ•œ ์ตœ์ ํ™”
    image

Front End

Lexical Analysis

A process of breaking a sequence of ASCII characters (source) into a sequence of tokens

  • recognize words by finite automata(Finite State Machine)
    image
  • Lexer(Lexical Analyzer): program that performs lexical analysis
    image

Class of Finite Automata

  • Deterministic Finite Automata (DFA)
    Edges leaving a node are uniquely labeled
  • Non-deterministic Finite Automata (NFA)
    Two or more edges leaving a node can be identically labeled
    An edge can be labeled with ฯต
  • Computers can understand only DFA, but directly transforming RE to DFA is difficult
    Use NFA as an intermediate step
    RE -> NFA -> DFA
RE to NFA

image

NFA to DFA

image

Syntax Analysis(Parsing)

parse phase structure
recursive structures๋Š” lexer๊ฐ€ ์ž˜ ์ฒ˜๋ฆฌํ•˜์ง€ ๋ชปํ•ด parser ํ•„์š”
image
โ€ป Context-free grammars
image
image
โ€ป Parsing tree
image -> Abstract Syntax Tree(๋ฉ”๋ชจ๋ฆฌ๋ฅผ ํšจ์œจ์ ์œผ๋กœ ์“ฐ๊ธฐ ์œ„ํ•ด)
image

Parser Generator(yacc, bison)

input: a set of context-free grammars specifying a parser
output: parser in target language, description of state machine
rules: pattern and action
image

Semantic Analysis

checks if each expression is correct

  • All identifiers (variable, class, functions, methods, โ€ฆ) are declared only once
  • Inheritance relationship
  • Types are well defined and related
  • Reserved identifiers are not misused

Type system

specifies which operations are valid for which types

  • Type checking: ensures that operations are used with the correct types
  • Type Inference: fills in missing type information

Intermediate Representation(IR)

abstract machine language
image

Instruction Selection

Process finding set of machine instructions that implement operations specified in IR tree
image

  • instruction tree patterns
    image
    image
    image
    instruction tree patterns์„ ํ†ตํ•ด instruction์„ optimizationํ•  ์ˆ˜ ์žˆ์Œ
    9 reg, 10 inst -> 5 reg, 6 inst image
    image

Back End

Control Flow Analysis

Determine how instructions are fetched during execution
image

Dominator Analysis

Control flow graph(CFG)์˜ ๊ฐ node๋“ค์˜ ์„ ํ›„ ๊ด€๊ณ„์— ๋Œ€ํ•œ ๋ถ„์„
Node ๐‘‘ ๐‘‘๐‘œ๐‘š๐‘–๐‘›๐‘Ž๐‘ก๐‘’๐‘  node ๐‘› if every path of directed edges from ๐‘ 0 to ๐‘› must go through ๐‘‘(dominator)
image

  • Dominator Tree: efficient representation of dominator information
  • Immediate Dominator: last dominator of ๐‘› on any path from ๐‘ 0 to ๐‘›
    image image

Data Flow Analysis

Live Variable Analysis

register allocation์„ ์œ„ํ•ด ๊ฐ node๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ์–ด์•ผ ํ•˜๋Š” register๋ฅผ iterativeํ•˜๊ฒŒ ์ฐพ๋Š” ๊ฒƒ
โ€ข ๐‘ข๐‘ ๐‘’s of node ๐‘› or register ๐‘ก: Source (RHS) registers of node ๐‘›, Nodes where ๐‘ก is used as source registers
โ€ข ๐‘‘๐‘’๐‘“๐‘–๐‘›๐‘–๐‘ก๐‘–๐‘œ๐‘› of node ๐‘› or register ๐‘ก: Destination (LHS) register of node ๐‘›, Node where t is defined
โ€ข A register ๐‘ก is ๐‘™๐‘–๐‘ฃ๐‘’ on edge ๐‘’: If there exists a path from ๐‘’ to a use of ๐‘ก that does not go through a definition of ๐‘ก
โ€ข Register ๐‘ก is ๐‘™๐‘–๐‘ฃ๐‘’โˆ’๐‘–๐‘› at CFG node ๐‘›: If ๐‘ก is live on any in-edge of ๐‘›
โ€ข Register ๐‘ก is ๐‘™๐‘–๐‘ฃ๐‘’โˆ’๐‘œ๐‘ข๐‘ก at CFG node ๐‘›: If ๐‘ก is live on any out-edge of ๐‘›
โ–  Interference Graph
Edge <๐‘ก๐‘–,๐‘ก๐‘—> exists if register ๐‘ก๐‘–, ๐‘ก๐‘— have overlapping live range
For some node ๐‘›, if ๐ท๐ธ๐น ๐‘› = {๐‘Ž} and ๐‘‚๐‘ˆ๐‘‡ ๐‘› = ๐‘1, ๐‘2, โ€ฆ , ๐‘๐‘˜ , then add interference edges: ๐‘Ž, ๐‘1 , ๐‘Ž, ๐‘2 , โ€ฆ , ๐‘Ž, ๐‘๐‘˜
image
โ–  Dead Code Elimination
side-effect์ด ์—†๋Š” ์ฝ”๋“œ๋ฅผ ์ง€์›Œ์„œ ์„ฑ๋Šฅ์„ ์˜ฌ๋ฆผ

Reaching Definition Analysis

A process to determine whether definition of register ๐‘ก can affect use of t
โ€ข ๐บ๐ธ๐‘[๐‘›]: the set of ๐‘‘๐‘’๐‘“๐‘–๐‘›๐‘–๐‘ก๐‘–๐‘œ๐‘› ๐‘–๐‘‘โ€™๐‘  that ๐‘› creates
โ€ข ๐พ๐ผ๐ฟ๐ฟ[๐‘›]: the set of ๐‘‘๐‘’๐‘“๐‘–๐‘›๐‘–๐‘ก๐‘–๐‘œ๐‘› ๐‘–๐‘‘โ€™๐‘  that ๐‘› kills
-> constant propagation, constant folding, copy propagation ์ ์šฉ ๊ฐ€๋Šฅ

Loop Optimization

Loop์ด๋ž€?
There exists a header node โ„Ž in ๐‘† that dominates all nodes in ๐‘†
From any node in ๐‘†, there exists a path of directed edges to โ„Ž
header h๋กœ ๋Œ์•„๊ฐ€๋Š” edge๋ฅผ back-edge๋ผ ํ•˜๊ณ , ์ด back-edge ํ•˜๋‚˜ ๋‹น natural loop ํ•˜๋‚˜๋กœ ํŒ๋ณ„
image

โ€ป Loop Invariant Code Motion(LICM): ๊ฒฐ๊ณผ์— ์˜ํ–ฅ์„ ๋ฏธ์น˜์ง€ ์•Š์œผ๋ฉด์„œ loop ๋ฐ–์œผ๋กœ code๋ฅผ ๋นผ๋‚ด๋Š” ๊ฒƒ
โ€ป Induction Variable: loop๋งˆ๋‹ค loop-invariant value๋งŒํผ ์ฆ๊ฐ€/๊ฐ์†Œํ•˜๋Š” ๋ณ€์ˆ˜

โš ๏ธ **GitHub.com Fallback** โš ๏ธ