SlideShare a Scribd company logo
LLVM Instruction Selection
Instruction Selection
• What is instruction selection ?
– To match the Compiler IR (intermediate
representation) to Target instruction.
Instruction Selection
• Why need Compiler IR ?
– In the beginning, compiler developer use “macro
expansion”
• Directly mapping high level language to assembly code
Disadvantage: Tightly coupling the code generator to a particular programming
language.
Create Machine independent IR to isolate backend and programming language
detail.
LLVM retargetablity design
LLVM IR come from frontend (clang/llvm-gcc/GHC)
LLVM Optimization Pass
LLVM IR Pass
LLVM DAG Pass
Emit target assembly code
Instruction Selection
Flags to dump pass sequence:
clang -mllvm -debug-pass=Structure {input}.c
Target Independent Passes
Target Dependent Passes
Flags to dump optimization detail for each pass:
clang -mllvm -debug -mllvm -print-after-all {input}.c
What is SelectionDAG ?
• LLVM IR will transfer to SelectionDag before
Instruction Selection
• a Directed-Acyclic-Graph to present
operations
– Node present operation (E.g. add/sub/or)
– Edge present define/use relationship
t15 hi20(sym)
t16
(lo12(sym))
t17 (or)
t3 (add)
198 (const)
Instruction selection in LLVM would be:
Mapping SDnode-> Target ISA
SDnode : selection DAG node
Define Target Instruction
• Define ADDI
– By ADDIForm (inherent by Instruction class)
• User could inherent and then specialize for different
kind ISA
– (outs GPR:$Rt)
• Output: 1 GPR
– (ins GPR:$Ra, imm15s:$imm)
• Inputs: 1 GPR, 1 imm15s constant
– Imm15s is a user defined operand constraint.
– “addit$Rt, $Rt, $imm”
• Assembly emit pattern
Selection DAG with CSE
• Have chance to deal with CSE.
+
+ +
y x8
Assume we have several instruction
With different cost.
Move reg, constant : cost 5
Add reg, reg, reg: cost 1
Addi reg, reg, constant: cost 5
There are two way to mapping instructions
R1 = y + 8; cost 5
R2 = x + 8 cost 5
R3 = R1 + R2 cost 1
Total cost 11
R1 = 8; cost 5
R2 = x + R1 cost 1
R3 = y + R1 cost 1
R4 = R2+R3 cost 1
Total cost 8
+
+ +
y x8
+
+ +
y x8
Selection DAG with CSE
• Dag->Target Dag selection only the one of the
factor influence optimization result.
– Even if Selection DAG not deal with CSE, we could
leave it to later pass to do it!
– In fact, LLVM move CSE consideration out of DAG
selection now.
– Currently, LLVM DAG Selection is purely greedy
selection and matching
Greedy DAG Selection
• LLVM would give each Target ISA “Complexity”
– Complexity big means it could cover more nodes
in the DAG
• Which means it may have chance to produce fewer
instruction if we choose Complexity big instruction
– In most of the case, few instructions could get better code
quality.
Greedy DAG Selection
• LLVM would give each Target ISA “Complexity”
– By default, complexity calculate by operation
number and operand kind in Target ISA
• E.g.
– Complexity (add R, R, Const) > Complexity (add R, R, R)
– Complexity (store R, [R + R << 2]) > Complexity (store R, [R])
– Complexity (add_shift) > Complexity (add)
• User also could modify their Target instruction
Complexity by AddedComplexity.
Greedy DAG Selection
• LLVM DAG selection is a bottom up method on
each basic block
– When try to match a node from the DAG
• LLVM will use a match table to select Target ISA
– E.g.
» Switch (Node kind)
• Case Add:
• If (op0 is reg && op1 is reg)
• If (op2 is reg) choose ADD
• If (op2is const) chose ADDI
– If Target have same semantic instructions
» Choose Complexity bigger first.
Instruction Matching Table
• LLVM will read Instruction pattern define in
*.td and generate Instruction matching table
– In build folder {Target}GenDAGISel.inc
• Generate by tablegen tools
• Contain the match table
• Implement as a interpreter to execute pattern match
Instruction Selection in dump file
Legalize SelectionDag
• Legalize Selection
DAG
– Legalize
• Support for the
target
– Types
• Check each Dag
Node type support
by the target
– Operations
• Check each Dag
Node operation
support by the target
Legalize SelectionDag
• How to specify target support types and
operations ?
– Defined in target
inherent TargetLowering constructor.
Load to 1 bit is not legal, promote to wider integer type
Instruction selection in LLVM
• Each SelectionDAG operation could specify as
– Legal
• The target natively supports this operation.
– Promote
• This operation should be executed in a larger type.
– Expand
• Try to expand(by llvm general code) this to other
operations, otherwise use a libcall.
– Custom
• Target should defined custom lowering method for the
operation
• Call the costom lowering method by
XXXTargetLowering::LowerOperation() target hook
Instruction selection in LLVM
CTPOP (counts the number of bits) for i32/i64 type is legal
Expand CTTZ (counts trailing zero) to other SDNodes for i32/i64 type
Customize BR_JT (branch to jump table)
Reference
• A deeper look into the LLVM code generator,
Part 1
– http://eli.thegreenplace.net/2013/02/25/a-
deeper-look-into-the-llvm-code-generator-part-1
• LLVM Legalization and Lowering
– https://wiki.aalto.fi/display/t1065450/LLVM+Legal
ization+and+Lowering
• Near-Optimal Instruction Selection on DAGs
– https://llvm.org/svn/llvm-project/www-
pubs/trunk/2008-CGO-DagISel.pdf
• Instruction Selection - Principles, Methods,

More Related Content

What's hot

Gcc porting
Gcc portingGcc porting
Gcc porting
Shiva Chen
 
LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)
Wang Hsiangkai
 
How to write a TableGen backend
How to write a TableGen backendHow to write a TableGen backend
How to write a TableGen backend
Min-Yih Hsu
 
Design and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationDesign and Implementation of GCC Register Allocation
Design and Implementation of GCC Register Allocation
Kito Cheng
 
BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2 BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2
Linaro
 
from Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Worksfrom Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Works
Zhen Wei
 
COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺
hydai
 
Build Programming Language Runtime with LLVM
Build Programming Language Runtime with LLVMBuild Programming Language Runtime with LLVM
Build Programming Language Runtime with LLVM
National Cheng Kung University
 
Ninja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersNinja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for Beginners
Chang W. Doh
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介
Akira Maruoka
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
Sumant Tambe
 
LLVM
LLVMLLVM
Q4.11: NEON Intrinsics
Q4.11: NEON IntrinsicsQ4.11: NEON Intrinsics
Q4.11: NEON Intrinsics
Linaro
 
Integrated Register Allocation introduction
Integrated Register Allocation introductionIntegrated Register Allocation introduction
Integrated Register Allocation introduction
Shiva Chen
 
BPF - in-kernel virtual machine
BPF - in-kernel virtual machineBPF - in-kernel virtual machine
BPF - in-kernel virtual machine
Alexei Starovoitov
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013
Vladimir Ivanov
 
LLVM最適化のこつ
LLVM最適化のこつLLVM最適化のこつ
LLVM最適化のこつ
MITSUNARI Shigeo
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDA
Raymond Tay
 
Better Embedded 2013 - Detecting Memory Leaks with Valgrind
Better Embedded 2013 - Detecting Memory Leaks with ValgrindBetter Embedded 2013 - Detecting Memory Leaks with Valgrind
Better Embedded 2013 - Detecting Memory Leaks with Valgrind
Rigels Gordani
 
How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)
Sławomir Zborowski
 

What's hot (20)

Gcc porting
Gcc portingGcc porting
Gcc porting
 
LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)LLVM Register Allocation (2nd Version)
LLVM Register Allocation (2nd Version)
 
How to write a TableGen backend
How to write a TableGen backendHow to write a TableGen backend
How to write a TableGen backend
 
Design and Implementation of GCC Register Allocation
Design and Implementation of GCC Register AllocationDesign and Implementation of GCC Register Allocation
Design and Implementation of GCC Register Allocation
 
BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2 BUD17-302: LLVM Internals #2
BUD17-302: LLVM Internals #2
 
from Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Worksfrom Binary to Binary: How Qemu Works
from Binary to Binary: How Qemu Works
 
COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺COSCUP2016 - LLVM框架、由淺入淺
COSCUP2016 - LLVM框架、由淺入淺
 
Build Programming Language Runtime with LLVM
Build Programming Language Runtime with LLVMBuild Programming Language Runtime with LLVM
Build Programming Language Runtime with LLVM
 
Ninja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for BeginnersNinja Build: Simple Guide for Beginners
Ninja Build: Simple Guide for Beginners
 
LLVM Backend の紹介
LLVM Backend の紹介LLVM Backend の紹介
LLVM Backend の紹介
 
Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)Fun with Lambdas: C++14 Style (part 1)
Fun with Lambdas: C++14 Style (part 1)
 
LLVM
LLVMLLVM
LLVM
 
Q4.11: NEON Intrinsics
Q4.11: NEON IntrinsicsQ4.11: NEON Intrinsics
Q4.11: NEON Intrinsics
 
Integrated Register Allocation introduction
Integrated Register Allocation introductionIntegrated Register Allocation introduction
Integrated Register Allocation introduction
 
BPF - in-kernel virtual machine
BPF - in-kernel virtual machineBPF - in-kernel virtual machine
BPF - in-kernel virtual machine
 
JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013JVM JIT-compiler overview @ JavaOne Moscow 2013
JVM JIT-compiler overview @ JavaOne Moscow 2013
 
LLVM最適化のこつ
LLVM最適化のこつLLVM最適化のこつ
LLVM最適化のこつ
 
Introduction to CUDA
Introduction to CUDAIntroduction to CUDA
Introduction to CUDA
 
Better Embedded 2013 - Detecting Memory Leaks with Valgrind
Better Embedded 2013 - Detecting Memory Leaks with ValgrindBetter Embedded 2013 - Detecting Memory Leaks with Valgrind
Better Embedded 2013 - Detecting Memory Leaks with Valgrind
 
How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)How it's made: C++ compilers (GCC)
How it's made: C++ compilers (GCC)
 

Similar to LLVM Instruction Selection

White and Black Magic on the JVM
White and Black Magic on the JVMWhite and Black Magic on the JVM
White and Black Magic on the JVM
Ivaylo Pashov
 
Groovy In the Cloud
Groovy In the CloudGroovy In the Cloud
Groovy In the Cloud
Jim Driscoll
 
JVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, WixJVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, Wix
Codemotion Tel Aviv
 
Understanding the Dalvik bytecode with the Dedexer tool
Understanding the Dalvik bytecode with the Dedexer toolUnderstanding the Dalvik bytecode with the Dedexer tool
Understanding the Dalvik bytecode with the Dedexer tool
Gabor Paller
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performance
ESUG
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
Dr. Syed Hassan Amin
 
Dalvik Vm &amp; Jit
Dalvik Vm &amp; JitDalvik Vm &amp; Jit
Dalvik Vm &amp; Jit
Ankit Somani
 
Dalvik Vm &amp; Jit
Dalvik Vm &amp; JitDalvik Vm &amp; Jit
Dalvik Vm &amp; Jit
Ankit Somani
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution Toolkit
Dimitry Snezhkov
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
Inductive Automation
 
AWS Lambda from the Trenches
AWS Lambda from the TrenchesAWS Lambda from the Trenches
AWS Lambda from the Trenches
Yan Cui
 
Fedor Polyakov - Optimizing computer vision problems on mobile platforms
Fedor Polyakov - Optimizing computer vision problems on mobile platforms Fedor Polyakov - Optimizing computer vision problems on mobile platforms
Fedor Polyakov - Optimizing computer vision problems on mobile platforms
Eastern European Computer Vision Conference
 
Os Lattner
Os LattnerOs Lattner
Os Lattner
oscon2007
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
Dmitri Nesteruk
 
running stable diffusion on android
running stable diffusion on androidrunning stable diffusion on android
running stable diffusion on android
Koan-Sin Tan
 
Modularity problems
Modularity  problemsModularity  problems
Modularity problems
Юлия Коваленко
 
Migration To Multi Core - Parallel Programming Models
Migration To Multi Core - Parallel Programming ModelsMigration To Multi Core - Parallel Programming Models
Migration To Multi Core - Parallel Programming Models
Zvi Avraham
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll build
Mark Stoodley
 
Specialized Compiler for Hash Cracking
Specialized Compiler for Hash CrackingSpecialized Compiler for Hash Cracking
Specialized Compiler for Hash Cracking
Positive Hack Days
 

Similar to LLVM Instruction Selection (20)

White and Black Magic on the JVM
White and Black Magic on the JVMWhite and Black Magic on the JVM
White and Black Magic on the JVM
 
Groovy In the Cloud
Groovy In the CloudGroovy In the Cloud
Groovy In the Cloud
 
JVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, WixJVM Memory Model - Yoav Abrahami, Wix
JVM Memory Model - Yoav Abrahami, Wix
 
Understanding the Dalvik bytecode with the Dedexer tool
Understanding the Dalvik bytecode with the Dedexer toolUnderstanding the Dalvik bytecode with the Dedexer tool
Understanding the Dalvik bytecode with the Dedexer tool
 
Sista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performanceSista: Improving Cog’s JIT performance
Sista: Improving Cog’s JIT performance
 
Improving Code Quality Through Effective Review Process
Improving Code Quality Through Effective  Review ProcessImproving Code Quality Through Effective  Review Process
Improving Code Quality Through Effective Review Process
 
Dalvik Vm &amp; Jit
Dalvik Vm &amp; JitDalvik Vm &amp; Jit
Dalvik Vm &amp; Jit
 
Dalvik Vm &amp; Jit
Dalvik Vm &amp; JitDalvik Vm &amp; Jit
Dalvik Vm &amp; Jit
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
Typhoon Managed Execution Toolkit
Typhoon Managed Execution ToolkitTyphoon Managed Execution Toolkit
Typhoon Managed Execution Toolkit
 
Design Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best PracticesDesign Like a Pro: Scripting Best Practices
Design Like a Pro: Scripting Best Practices
 
AWS Lambda from the Trenches
AWS Lambda from the TrenchesAWS Lambda from the Trenches
AWS Lambda from the Trenches
 
Fedor Polyakov - Optimizing computer vision problems on mobile platforms
Fedor Polyakov - Optimizing computer vision problems on mobile platforms Fedor Polyakov - Optimizing computer vision problems on mobile platforms
Fedor Polyakov - Optimizing computer vision problems on mobile platforms
 
Os Lattner
Os LattnerOs Lattner
Os Lattner
 
Unmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/InvokeUnmanaged Parallelization via P/Invoke
Unmanaged Parallelization via P/Invoke
 
running stable diffusion on android
running stable diffusion on androidrunning stable diffusion on android
running stable diffusion on android
 
Modularity problems
Modularity  problemsModularity  problems
Modularity problems
 
Migration To Multi Core - Parallel Programming Models
Migration To Multi Core - Parallel Programming ModelsMigration To Multi Core - Parallel Programming Models
Migration To Multi Core - Parallel Programming Models
 
Oh the compilers you'll build
Oh the compilers you'll buildOh the compilers you'll build
Oh the compilers you'll build
 
Specialized Compiler for Hash Cracking
Specialized Compiler for Hash CrackingSpecialized Compiler for Hash Cracking
Specialized Compiler for Hash Cracking
 

Recently uploaded

Anti-Universe And Emergent Gravity and the Dark Universe
Anti-Universe And Emergent Gravity and the Dark UniverseAnti-Universe And Emergent Gravity and the Dark Universe
Anti-Universe And Emergent Gravity and the Dark Universe
Sérgio Sacani
 
Signatures of wave erosion in Titan’s coasts
Signatures of wave erosion in Titan’s coastsSignatures of wave erosion in Titan’s coasts
Signatures of wave erosion in Titan’s coasts
Sérgio Sacani
 
Alternate Wetting and Drying - Climate Smart Agriculture
Alternate Wetting and Drying - Climate Smart AgricultureAlternate Wetting and Drying - Climate Smart Agriculture
Alternate Wetting and Drying - Climate Smart Agriculture
International Food Policy Research Institute- South Asia Office
 
GBSN - Biochemistry (Unit 6) Chemistry of Proteins
GBSN - Biochemistry (Unit 6) Chemistry of ProteinsGBSN - Biochemistry (Unit 6) Chemistry of Proteins
GBSN - Biochemistry (Unit 6) Chemistry of Proteins
Areesha Ahmad
 
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
Scintica Instrumentation
 
Compexometric titration/Chelatorphy titration/chelating titration
Compexometric titration/Chelatorphy titration/chelating titrationCompexometric titration/Chelatorphy titration/chelating titration
Compexometric titration/Chelatorphy titration/chelating titration
Vandana Devesh Sharma
 
JAMES WEBB STUDY THE MASSIVE BLACK HOLE SEEDS
JAMES WEBB STUDY THE MASSIVE BLACK HOLE SEEDSJAMES WEBB STUDY THE MASSIVE BLACK HOLE SEEDS
JAMES WEBB STUDY THE MASSIVE BLACK HOLE SEEDS
Sérgio Sacani
 
TOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptx
TOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptxTOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptx
TOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptx
shubhijain836
 
gastroretentive drug delivery system-PPT.pptx
gastroretentive drug delivery system-PPT.pptxgastroretentive drug delivery system-PPT.pptx
gastroretentive drug delivery system-PPT.pptx
Shekar Boddu
 
Summary Of transcription and Translation.pdf
Summary Of transcription and Translation.pdfSummary Of transcription and Translation.pdf
Summary Of transcription and Translation.pdf
vadgavevedant86
 
Evidence of Jet Activity from the Secondary Black Hole in the OJ 287 Binary S...
Evidence of Jet Activity from the Secondary Black Hole in the OJ 287 Binary S...Evidence of Jet Activity from the Secondary Black Hole in the OJ 287 Binary S...
Evidence of Jet Activity from the Secondary Black Hole in the OJ 287 Binary S...
Sérgio Sacani
 
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
vluwdy49
 
Gadgets for management of stored product pests_Dr.UPR.pdf
Gadgets for management of stored product pests_Dr.UPR.pdfGadgets for management of stored product pests_Dr.UPR.pdf
Gadgets for management of stored product pests_Dr.UPR.pdf
PirithiRaju
 
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Leonel Morgado
 
HUMAN EYE By-R.M Class 10 phy best digital notes.pdf
HUMAN EYE By-R.M Class 10 phy best digital notes.pdfHUMAN EYE By-R.M Class 10 phy best digital notes.pdf
HUMAN EYE By-R.M Class 10 phy best digital notes.pdf
Ritik83251
 
Microbiology of Central Nervous System INFECTIONS.pdf
Microbiology of Central Nervous System INFECTIONS.pdfMicrobiology of Central Nervous System INFECTIONS.pdf
Microbiology of Central Nervous System INFECTIONS.pdf
sammy700571
 
BIOTRANSFORMATION MECHANISM FOR OF STEROID
BIOTRANSFORMATION MECHANISM FOR OF STEROIDBIOTRANSFORMATION MECHANISM FOR OF STEROID
BIOTRANSFORMATION MECHANISM FOR OF STEROID
ShibsekharRoy1
 
Discovery of An Apparent Red, High-Velocity Type Ia Supernova at 𝐳 = 2.9 wi...
Discovery of An Apparent Red, High-Velocity Type Ia Supernova at  𝐳 = 2.9  wi...Discovery of An Apparent Red, High-Velocity Type Ia Supernova at  𝐳 = 2.9  wi...
Discovery of An Apparent Red, High-Velocity Type Ia Supernova at 𝐳 = 2.9 wi...
Sérgio Sacani
 
MICROBIAL INTERACTION PPT/ MICROBIAL INTERACTION AND THEIR TYPES // PLANT MIC...
MICROBIAL INTERACTION PPT/ MICROBIAL INTERACTION AND THEIR TYPES // PLANT MIC...MICROBIAL INTERACTION PPT/ MICROBIAL INTERACTION AND THEIR TYPES // PLANT MIC...
MICROBIAL INTERACTION PPT/ MICROBIAL INTERACTION AND THEIR TYPES // PLANT MIC...
ABHISHEK SONI NIMT INSTITUTE OF MEDICAL AND PARAMEDCIAL SCIENCES , GOVT PG COLLEGE NOIDA
 
11.1 Role of physical biological in deterioration of grains.pdf
11.1 Role of physical biological in deterioration of grains.pdf11.1 Role of physical biological in deterioration of grains.pdf
11.1 Role of physical biological in deterioration of grains.pdf
PirithiRaju
 

Recently uploaded (20)

Anti-Universe And Emergent Gravity and the Dark Universe
Anti-Universe And Emergent Gravity and the Dark UniverseAnti-Universe And Emergent Gravity and the Dark Universe
Anti-Universe And Emergent Gravity and the Dark Universe
 
Signatures of wave erosion in Titan’s coasts
Signatures of wave erosion in Titan’s coastsSignatures of wave erosion in Titan’s coasts
Signatures of wave erosion in Titan’s coasts
 
Alternate Wetting and Drying - Climate Smart Agriculture
Alternate Wetting and Drying - Climate Smart AgricultureAlternate Wetting and Drying - Climate Smart Agriculture
Alternate Wetting and Drying - Climate Smart Agriculture
 
GBSN - Biochemistry (Unit 6) Chemistry of Proteins
GBSN - Biochemistry (Unit 6) Chemistry of ProteinsGBSN - Biochemistry (Unit 6) Chemistry of Proteins
GBSN - Biochemistry (Unit 6) Chemistry of Proteins
 
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
(June 12, 2024) Webinar: Development of PET theranostics targeting the molecu...
 
Compexometric titration/Chelatorphy titration/chelating titration
Compexometric titration/Chelatorphy titration/chelating titrationCompexometric titration/Chelatorphy titration/chelating titration
Compexometric titration/Chelatorphy titration/chelating titration
 
JAMES WEBB STUDY THE MASSIVE BLACK HOLE SEEDS
JAMES WEBB STUDY THE MASSIVE BLACK HOLE SEEDSJAMES WEBB STUDY THE MASSIVE BLACK HOLE SEEDS
JAMES WEBB STUDY THE MASSIVE BLACK HOLE SEEDS
 
TOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptx
TOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptxTOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptx
TOPIC OF DISCUSSION: CENTRIFUGATION SLIDESHARE.pptx
 
gastroretentive drug delivery system-PPT.pptx
gastroretentive drug delivery system-PPT.pptxgastroretentive drug delivery system-PPT.pptx
gastroretentive drug delivery system-PPT.pptx
 
Summary Of transcription and Translation.pdf
Summary Of transcription and Translation.pdfSummary Of transcription and Translation.pdf
Summary Of transcription and Translation.pdf
 
Evidence of Jet Activity from the Secondary Black Hole in the OJ 287 Binary S...
Evidence of Jet Activity from the Secondary Black Hole in the OJ 287 Binary S...Evidence of Jet Activity from the Secondary Black Hole in the OJ 287 Binary S...
Evidence of Jet Activity from the Secondary Black Hole in the OJ 287 Binary S...
 
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
在线办理(salfor毕业证书)索尔福德大学毕业证毕业完成信一模一样
 
Gadgets for management of stored product pests_Dr.UPR.pdf
Gadgets for management of stored product pests_Dr.UPR.pdfGadgets for management of stored product pests_Dr.UPR.pdf
Gadgets for management of stored product pests_Dr.UPR.pdf
 
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
Describing and Interpreting an Immersive Learning Case with the Immersion Cub...
 
HUMAN EYE By-R.M Class 10 phy best digital notes.pdf
HUMAN EYE By-R.M Class 10 phy best digital notes.pdfHUMAN EYE By-R.M Class 10 phy best digital notes.pdf
HUMAN EYE By-R.M Class 10 phy best digital notes.pdf
 
Microbiology of Central Nervous System INFECTIONS.pdf
Microbiology of Central Nervous System INFECTIONS.pdfMicrobiology of Central Nervous System INFECTIONS.pdf
Microbiology of Central Nervous System INFECTIONS.pdf
 
BIOTRANSFORMATION MECHANISM FOR OF STEROID
BIOTRANSFORMATION MECHANISM FOR OF STEROIDBIOTRANSFORMATION MECHANISM FOR OF STEROID
BIOTRANSFORMATION MECHANISM FOR OF STEROID
 
Discovery of An Apparent Red, High-Velocity Type Ia Supernova at 𝐳 = 2.9 wi...
Discovery of An Apparent Red, High-Velocity Type Ia Supernova at  𝐳 = 2.9  wi...Discovery of An Apparent Red, High-Velocity Type Ia Supernova at  𝐳 = 2.9  wi...
Discovery of An Apparent Red, High-Velocity Type Ia Supernova at 𝐳 = 2.9 wi...
 
MICROBIAL INTERACTION PPT/ MICROBIAL INTERACTION AND THEIR TYPES // PLANT MIC...
MICROBIAL INTERACTION PPT/ MICROBIAL INTERACTION AND THEIR TYPES // PLANT MIC...MICROBIAL INTERACTION PPT/ MICROBIAL INTERACTION AND THEIR TYPES // PLANT MIC...
MICROBIAL INTERACTION PPT/ MICROBIAL INTERACTION AND THEIR TYPES // PLANT MIC...
 
11.1 Role of physical biological in deterioration of grains.pdf
11.1 Role of physical biological in deterioration of grains.pdf11.1 Role of physical biological in deterioration of grains.pdf
11.1 Role of physical biological in deterioration of grains.pdf
 

LLVM Instruction Selection

  • 2. Instruction Selection • What is instruction selection ? – To match the Compiler IR (intermediate representation) to Target instruction.
  • 3. Instruction Selection • Why need Compiler IR ? – In the beginning, compiler developer use “macro expansion” • Directly mapping high level language to assembly code Disadvantage: Tightly coupling the code generator to a particular programming language. Create Machine independent IR to isolate backend and programming language detail.
  • 4. LLVM retargetablity design LLVM IR come from frontend (clang/llvm-gcc/GHC)
  • 5. LLVM Optimization Pass LLVM IR Pass LLVM DAG Pass Emit target assembly code Instruction Selection Flags to dump pass sequence: clang -mllvm -debug-pass=Structure {input}.c Target Independent Passes Target Dependent Passes Flags to dump optimization detail for each pass: clang -mllvm -debug -mllvm -print-after-all {input}.c
  • 6. What is SelectionDAG ? • LLVM IR will transfer to SelectionDag before Instruction Selection • a Directed-Acyclic-Graph to present operations – Node present operation (E.g. add/sub/or) – Edge present define/use relationship t15 hi20(sym) t16 (lo12(sym)) t17 (or) t3 (add) 198 (const) Instruction selection in LLVM would be: Mapping SDnode-> Target ISA SDnode : selection DAG node
  • 7. Define Target Instruction • Define ADDI – By ADDIForm (inherent by Instruction class) • User could inherent and then specialize for different kind ISA – (outs GPR:$Rt) • Output: 1 GPR – (ins GPR:$Ra, imm15s:$imm) • Inputs: 1 GPR, 1 imm15s constant – Imm15s is a user defined operand constraint. – “addit$Rt, $Rt, $imm” • Assembly emit pattern
  • 8. Selection DAG with CSE • Have chance to deal with CSE. + + + y x8 Assume we have several instruction With different cost. Move reg, constant : cost 5 Add reg, reg, reg: cost 1 Addi reg, reg, constant: cost 5 There are two way to mapping instructions R1 = y + 8; cost 5 R2 = x + 8 cost 5 R3 = R1 + R2 cost 1 Total cost 11 R1 = 8; cost 5 R2 = x + R1 cost 1 R3 = y + R1 cost 1 R4 = R2+R3 cost 1 Total cost 8 + + + y x8 + + + y x8
  • 9. Selection DAG with CSE • Dag->Target Dag selection only the one of the factor influence optimization result. – Even if Selection DAG not deal with CSE, we could leave it to later pass to do it! – In fact, LLVM move CSE consideration out of DAG selection now. – Currently, LLVM DAG Selection is purely greedy selection and matching
  • 10. Greedy DAG Selection • LLVM would give each Target ISA “Complexity” – Complexity big means it could cover more nodes in the DAG • Which means it may have chance to produce fewer instruction if we choose Complexity big instruction – In most of the case, few instructions could get better code quality.
  • 11. Greedy DAG Selection • LLVM would give each Target ISA “Complexity” – By default, complexity calculate by operation number and operand kind in Target ISA • E.g. – Complexity (add R, R, Const) > Complexity (add R, R, R) – Complexity (store R, [R + R << 2]) > Complexity (store R, [R]) – Complexity (add_shift) > Complexity (add) • User also could modify their Target instruction Complexity by AddedComplexity.
  • 12. Greedy DAG Selection • LLVM DAG selection is a bottom up method on each basic block – When try to match a node from the DAG • LLVM will use a match table to select Target ISA – E.g. » Switch (Node kind) • Case Add: • If (op0 is reg && op1 is reg) • If (op2 is reg) choose ADD • If (op2is const) chose ADDI – If Target have same semantic instructions » Choose Complexity bigger first.
  • 13. Instruction Matching Table • LLVM will read Instruction pattern define in *.td and generate Instruction matching table – In build folder {Target}GenDAGISel.inc • Generate by tablegen tools • Contain the match table • Implement as a interpreter to execute pattern match
  • 15. Legalize SelectionDag • Legalize Selection DAG – Legalize • Support for the target – Types • Check each Dag Node type support by the target – Operations • Check each Dag Node operation support by the target
  • 16. Legalize SelectionDag • How to specify target support types and operations ? – Defined in target inherent TargetLowering constructor. Load to 1 bit is not legal, promote to wider integer type
  • 17. Instruction selection in LLVM • Each SelectionDAG operation could specify as – Legal • The target natively supports this operation. – Promote • This operation should be executed in a larger type. – Expand • Try to expand(by llvm general code) this to other operations, otherwise use a libcall. – Custom • Target should defined custom lowering method for the operation • Call the costom lowering method by XXXTargetLowering::LowerOperation() target hook
  • 18. Instruction selection in LLVM CTPOP (counts the number of bits) for i32/i64 type is legal Expand CTTZ (counts trailing zero) to other SDNodes for i32/i64 type Customize BR_JT (branch to jump table)
  • 19. Reference • A deeper look into the LLVM code generator, Part 1 – http://eli.thegreenplace.net/2013/02/25/a- deeper-look-into-the-llvm-code-generator-part-1 • LLVM Legalization and Lowering – https://wiki.aalto.fi/display/t1065450/LLVM+Legal ization+and+Lowering • Near-Optimal Instruction Selection on DAGs – https://llvm.org/svn/llvm-project/www- pubs/trunk/2008-CGO-DagISel.pdf • Instruction Selection - Principles, Methods,