llvm, clang and embedded linux systems

39
LLVM, Clang and Embedded Linux Systems Bruno Cardoso Lopes University of Campinas

Upload: others

Post on 03-Feb-2022

15 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: LLVM, Clang and Embedded Linux Systems

LLVM, Clang and Embedded Linux Systems

Bruno Cardoso LopesUniversity of Campinas

Page 2: LLVM, Clang and Embedded Linux Systems

What’s LLVM?

Page 3: LLVM, Clang and Embedded Linux Systems

What’s LLVM

• Compiler infrastructure

Frontend(clang)

IRTools

OptimizerBackends

JIT

AssemblerDisassembler

Page 4: LLVM, Clang and Embedded Linux Systems

What’s LLVM

• Virtual Instruction set (IR)

• SSA

• Bitcode

int add(int x, int y) { return x+y;}

Clang

define i32 @add(i32 %x, i32 %y) { %1 = add i32 %y, %x ret i32 %1}

IRC source code

Frontend

Page 5: LLVM, Clang and Embedded Linux Systems

What’s LLVM

• Optimization oriented compiler: compile time, link-time and run-time

• More than 30 analysis passes and 60 transformation passes.

Page 6: LLVM, Clang and Embedded Linux Systems

Why LLVM?

Page 7: LLVM, Clang and Embedded Linux Systems

Why LLVM?

• Open Source

• Active community

• Easy integration and quick patch review

Page 8: LLVM, Clang and Embedded Linux Systems

Why LLVM?

• Code easy to read and understand

• Transformations and optimizations are applied in several parts during compilation

Page 9: LLVM, Clang and Embedded Linux Systems

Design

Page 10: LLVM, Clang and Embedded Linux Systems

Design

• Written in C++

• Modular and composed of several libraries

• Pass mechanism with pluggable interface for transformations and analysis

• Several tools for each part of compilation

Page 11: LLVM, Clang and Embedded Linux Systems

Tools

Page 12: LLVM, Clang and Embedded Linux Systems

Tools

• Dragonegg

• Gcc 4.5 plugin

• llvm-gcc

• GIMPLE to LLVM IR

Front-end

Page 13: LLVM, Clang and Embedded Linux Systems

Tools

• Clang

• Library approach

• No cross-compiler generation needed

• Good diagnostics

• Static Analyzer

Front-end

Page 14: LLVM, Clang and Embedded Linux Systems

Tools

• Optimization are applied to the IR

• opt tool

Optimizer

add.bc

$ opt -O3 add.bc -o add2.bc

Aggressive Dead Code ElminationTail Call Elimination

Combine Redudant InstructionsDead Argument EliminationType-Based Alias Analysis

...

add2.bc

optimizations

Page 15: LLVM, Clang and Embedded Linux Systems

Tools

• llc tool: invoke the static backends

• Generates assembly or object code

Low Level Compiler

$ llc -march=arm add.bc -o add.s

define i32 @add(i32 %x, i32 %y) { %1 = add i32 %y, %x ret i32 %1}

.globl add

.align 2add: add r0, r1, r0 bx lr

add.bc

llc

add.s

Page 16: LLVM, Clang and Embedded Linux Systems

Tools

• llvm-mc tool

• Assembler and Disassembler

LLVM Machine Code

.globl add

.align 2add: add r0, r1, r0 bx lr

add.s

$ llvm-mc -show-encodings -triple armv7-linux add.s

llvm-mcadd r0, r1, r0

@ encoding: [0x00,0x00,0x81,0xe0]bx lr

@ encoding: [0x1e,0xff,0x2f,0xe1]

Page 17: LLVM, Clang and Embedded Linux Systems

Tools

• lli tool

• Execution Engine library

JIT

add.bclli

Execution Engine

x86

arm

Page 18: LLVM, Clang and Embedded Linux Systems

Tools

• libLTO library

• Bitcode files treated as native objects

• Enables mixing and matching bitcode w/ native objects

libLTO

Page 19: LLVM, Clang and Embedded Linux Systems

Codegen

Page 20: LLVM, Clang and Embedded Linux Systems

Codegen

• LLVM has a target independent code generator.

• Inheritance and overloading are used to specify target specific details.

• TableGen language, created to describe information and generate C++ code.

Page 21: LLVM, Clang and Embedded Linux Systems

Codegen

• TableGen

def ADDPS : PI<0x58, (outs VR256:$dst), (ins VR256:$src1, VR256:$src2), "addps $src2, $src1, $dst", [(set VR256:$dst, (fadd VR256:$src1, VR256:$src2))]>;

Encoding

Page 22: LLVM, Clang and Embedded Linux Systems

Codegen

• TableGen

def ADDPS : PI<0x58, (outs VR256:$dst), (ins VR256:$src1, VR256:$src2), "addps $src2, $src1, $dst", [(set VR256:$dst, (fadd VR256:$src1, VR256:$src2))]>;

Assembly

Page 23: LLVM, Clang and Embedded Linux Systems

Codegen

• TableGen

def ADDPS : PI<0x58, (outs VR256:$dst), (ins VR256:$src1, VR256:$src2), "addps $src2, $src1, $dst", [(set VR256:$dst, (fadd VR256:$src1, VR256:$src2))]>;

Instruction Selection Pattern

Page 24: LLVM, Clang and Embedded Linux Systems

Codegen

• Support several targets

ARM, Alpha, Blackfin, CellSPU, MBlazeMSP430, Mips, PTX, PowerPC, Sparc,

SystemZ, x86, XCore

Page 25: LLVM, Clang and Embedded Linux Systems

Codegen

• Support the target ABI

• Translate IR to real instruction and registers

Steps

Page 26: LLVM, Clang and Embedded Linux Systems

Codegen

• Target Calling Convention using TableGen and custom C++ code

• In the front-end

ABI

def CC_MipsEABI : CallingConv<[ // Promote i8/i16 arguments to i32. CCIfType<[i8, i16], CCPromoteToType<i32>>,

// Integer arguments are passed in integer registers. CCIfType<[i32], CCAssignToReg<[A0, A1, A2, A3, T0, T1, T2, T3]>>,]>;

Page 27: LLVM, Clang and Embedded Linux Systems

Codegen

• Back-end

• Legalization phase: nodes could be legal, expanded or customized

• DAGCombine (post and pre legalization)

• Instruction Selection

Translate IR to real instructions

Page 28: LLVM, Clang and Embedded Linux Systems

Codegen

• Legalization

Translate IR to real instructions

ISD::SUB ISD::SUB

ISD::SINT_TO_FP Several other nodes

ISD::CTTZ ARMISD::RBIT+ISD::CTLZ

Legal

Expand

Custom

Page 29: LLVM, Clang and Embedded Linux Systems

Codegen

• DAGCombine

Translate IR to real instructions

(bfi A, (and B, C1), C2) (bfi A, B, C2)

if C1 & C2 == C1

if A is constant

(mul x, 2^N + 1) (add (shl x, N), x)

Page 30: LLVM, Clang and Embedded Linux Systems

Codegen

• Instruction selection

• Tablegen pattern matching

• Custom C++ handling

Translate IR to real instructions

def ADDiu { list<dag> Pattern = [(set CPURegs:$dst,

(add CPURegs:$src, imm:$c))];}

Page 31: LLVM, Clang and Embedded Linux Systems

CodegenTarget Specific Optimizations

• Registered as passes

bool SparcTargetMachine::addPreEmitPass(PassManagerBase &PM, bool Fast) { PM.add(createSparcFPMoverPass(*this)); PM.add(createSparcDelaySlotFillerPass(*this)); return true;}

Page 32: LLVM, Clang and Embedded Linux Systems

CodegenARM

V4TV5TEV6

V6MV6T2V7AV7M

Archs

...Cortex M0, A8, A9, M3, M4

Processors

Features

Neon, VFP2, VFP3, Thumb2

Page 33: LLVM, Clang and Embedded Linux Systems

CodegenARM

• Target specific optimizations

PM.add(createARMLoadStoreOptimizationPass());PM.add(createThumb2SizeReductionPass());PM.add(createARMConstantIslandPass());PM.add(createThumb2ITBlockPass());

Page 34: LLVM, Clang and Embedded Linux Systems

CodegenARM

• Constant Islands pass:

• Limited PC-relative displacements

• Constants are scattered among instructions in a function

Page 35: LLVM, Clang and Embedded Linux Systems

CodegenARM

• Load Store optimizer

• Create load/store multiple instructions

• Recognize LDRD and STRD

Page 36: LLVM, Clang and Embedded Linux Systems

CodegenARM

• Code size reduction

• 32bit instructions to 16bit ones

Page 37: LLVM, Clang and Embedded Linux Systems

CodegenARM

• IT Block pass

• Recognize instruction suitable to become part of a IT block.

Page 38: LLVM, Clang and Embedded Linux Systems

CodegenMIPS

• Supports O32 and EABI.

• Mips I, 4ke and allegrex core (PSP)

• No target specific optimizations.

Page 39: LLVM, Clang and Embedded Linux Systems

Questions?