Download - Accelerating Stateflow With LLVM
![Page 2: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/2.jpg)
2
What is Stateflow?
• A block in Simulink, which is a graphical language for modeling algorithms
![Page 3: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/3.jpg)
3
What is Stateflow?
• A block in Simulink, which is a graphical language for modeling algorithms
• The Stateflow block models control flow by graphically modeling state
transition diagrams, flow charts, and truth tables
![Page 4: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/4.jpg)
4
What is Stateflow?
• A block in Simulink, which is a graphical
language for modeling algorithms
• The Stateflow block models control flow
by graphically modeling state transition
diagrams, flow charts, and truth tables
![Page 5: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/5.jpg)
5
Traditional Simulation Approach
Simulink Simulation Engine
Simulation target DLL
chart1.cchart2.c fcn.c sum gain scope
Simulink built-in block algorithms
![Page 6: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/6.jpg)
6
Simulation through Code Generation
Pros
– Simulation and production code generation based on same
technology => less code, fewer bugs
– Much faster runtime than “interpreted” implementation
– Easy to call customer-supplied C/C++ code
Cons
– First time overhead due to generating code and invoking a
compiler
– Needs an external C-compiler
(Different levels of difficulty to customers on Mac, Linux, and Windows)
![Page 7: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/7.jpg)
7
How can we target LLVM instead of C?
![Page 8: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/8.jpg)
8
How can we target LLVM instead of C?
A new compiler backend – from our internal IR, to LLVM
![Page 9: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/9.jpg)
9
About our internal IR
Represents a high level of abstraction – matrix operations, fixed-point and
complex math, structures, complex control flow
Gets progressively lowered into multiple backend languages – C, VHDL,
Verilog, PLC Structured Text
![Page 10: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/10.jpg)
10
About our internal IR
Represents a high level of abstraction – matrix operations, fixed-point and
complex math, structures, complex control flow
Gets progressively lowered into multiple backend languages – C, VHDL,
Verilog, PLC Structured Text
And now LLVM!
![Page 11: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/11.jpg)
11
An observation: we’re really good at working with our own IR
Many good debugging tools
Many experts in the building
![Page 12: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/12.jpg)
12
An observation: we’re really good at working with our own IR
Many good debugging tools
Many experts in the building
Let’s map our semantics onto LLVM in our own IR
![Page 13: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/13.jpg)
13
What does that mean exactly?
Like our “normal” compiler flows, we do “lowerings” to go from high-level
abstractions to lower level abstractions
– Lower matrix operations into loops
– Fixed-point math into integer math, etc
![Page 14: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/14.jpg)
14
What does that mean exactly?
Like our “normal” compiler flows, we do “lowerings” to go from high-level
abstractions to lower level abstractions
– Lower matrix operations into loops
– Fixed-point math into integer math, etc.
In addition, we go further
– Booleans become int1 or int8 depending on context (control flow vs. data)
– Unions get mapped into Structures with one field; accesses get turned into cast
operations
– Many more examples
![Page 15: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/15.jpg)
15
Where do we end up?
A syntactically legal version of our own IR
That maps one-to-one onto LLVM IR
![Page 16: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/16.jpg)
16
Where do we end up?
A syntactically legal version of our own IR
That maps one-to-one onto LLVM IR
This makes the translation really simple
![Page 17: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/17.jpg)
17
R2015a: Just-in-Time (JIT) Compilation
Simulink Simulation Engine
chart1()chart2() fcn()sum gain scope
Simulink builtin block algorithms
Optimized block functions
in memory without C-code
![Page 18: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/18.jpg)
18
JIT-based Simulation in R2015a
– No need for a C compiler
– Fast startup
Transparent to the user
– No knobs or buttons or options
– Model compilation speeds up through JIT when it can
Automatically fall back to codegen modes as needed
e.g., Custom code, and step-by-step debugging use code generation
![Page 19: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/19.jpg)
19
JIT Model Compile Time Improvement
Data from >5000 internal test models• 99% of the models are 20-50% faster
![Page 20: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/20.jpg)
20
Challenges
Supporting Linux, Mac, and Win64
Our runtime can throw exceptions
– On win64, LLVM can’t handle exceptions passing through
– Wrote a pass (in our IR) to wrap every runtime call with error checks/early returns
![Page 21: Accelerating Stateflow With LLVM](https://reader031.vdocument.in/reader031/viewer/2022013015/61d02803eb72811d7701ade3/html5/thumbnails/21.jpg)
21
Challenges
Discovered the hard way that MC-JIT does not really work on Windows with
LLVM 3.5 or 3.6
Due to release schedule, stuck at 3.5 and legacy JIT for now