22
1 © 2015 The MathWorks, Inc. Accelerating Stateflow With LLVM By Dale Martin [email protected]

Accelerating Stateflow With LLVM

  • Upload
    others

  • View
    3

  • Download
    0

Embed Size (px)

Citation preview

Page 1: Accelerating Stateflow With LLVM

1© 2015 The MathWorks, Inc.

Accelerating Stateflow With LLVM

By Dale Martin

[email protected]

Page 2: Accelerating Stateflow With LLVM

2

What is Stateflow?

• A block in Simulink, which is a graphical language for modeling algorithms

Page 3: Accelerating Stateflow With LLVM

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

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

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

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

7

How can we target LLVM instead of C?

Page 8: Accelerating Stateflow With LLVM

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

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

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

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

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

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

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

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

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

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

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

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

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

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

Page 22: Accelerating Stateflow With LLVM

22

Questions?

Come find me or email [email protected]