Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cpl:labs:llvm-pass [2015/11/23 11:03]
laura.vasilescu
cpl:labs:llvm-pass [2015/12/08 00:32] (current)
irina.presa [Integrarea unui pas în LLVM]
Line 1: Line 1:
-====== ​Cum se construiește un LLVM pass? ======+====== LLVM Passes ​======
  
 [[http://​llvm.org/​docs/​WritingAnLLVMPass.html|LLVM Passes]] este un subsistem al framework-ului LLVM ce permite crearea de module simple pentru transformarea,​ prelucrarea și analiza codului intermediar ''​LLVM IR''​. [[http://​llvm.org/​docs/​WritingAnLLVMPass.html|LLVM Passes]] este un subsistem al framework-ului LLVM ce permite crearea de module simple pentru transformarea,​ prelucrarea și analiza codului intermediar ''​LLVM IR''​.
 +
 +Există numeroase pass-uri deja implementate în versiunea oficială LLVM. Acestea primesc ca input cod ''​LLVM IR''​ și oferă ca output fie ''​LLVM IR''​ (în cazul în care este un pass de transformare),​ fie un text cu diverse statistici (în cazul în care este un pass de analiză).
 +
 +===== Crearea unui Pass =====
 +
 +''​LLVM Passes''​ pot fi rulate cu ajutorul tool-ului ''​opt''​.
 +
 +==== Pass-uri existente ====
 +<code bash>
 +student@cpl-vm ~ $ opt --help
 +OVERVIEW: llvm .bc -> .bc modular optimizer and analysis printer
 +
 +USAGE: opt [options] <input bitcode file>
 +
 +OPTIONS:
 +  -O1                                             - Optimization level 1. Similar to clang -O1
 +  -O2                                             - Optimization level 2. Similar to clang -O2
 +  -O3                                             - Optimization level 3. Similar to clang -O3
 +  -Os                                             - Like -O2 with extra optimizations for size. Similar to clang -Os
 +  -Oz                                             - Like -Os but reduces code size further. Similar to clang -Oz
 +  -S                                              - Write output as LLVM assembly
 +  -analyze ​                                       - Only perform analysis, no optimization
 +  -asm-instrumentation ​                           - Instrumentation of inline assembly and assembly source files
 +    =none                                         ​- ​  no instrumentation at all
 +    =address ​                                     -   ​instrument instructions with memory arguments
 +  -asm-show-inst ​                                 - Emit internal instruction representation to assembly file
 +  Optimizations available:
 +[...]
 +    -bb-vectorize ​                                - Basic-Block Vectorization
 +    -constprop ​                                   - Simple constant propagation
 +    -da                                           - Dependence Analysis
 +    -dce                                          - Dead Code Elimination
 +    -domtree ​                                     - Dominator Tree Construction
 +    -dot-callgraph ​                               - Print call graph to '​dot'​ file
 +    -dot-cfg ​                                     - Print CFG of function to '​dot'​ file
 +    -dot-cfg-only ​                                - Print CFG of function to '​dot'​ file (with no function bodies)
 +    -dot-dom ​                                     - Print dominance tree of function to '​dot'​ file
 +    -dot-dom-only ​                                - Print dominance tree of function to '​dot'​ file (with no function bodies)
 +    -dot-postdom ​                                 - Print postdominance tree of function to '​dot'​ file
 +    -dot-postdom-only ​                            - Print postdominance tree of function to '​dot'​ file (with no function bodies)
 +    -instcount ​                                   - Counts the various types of Instructions
 +    -loop-deletion ​                               - Delete dead loops
 +    -loop-reroll ​                                 - Reroll loops
 +    -loop-rotate ​                                 - Rotate Loops
 +    -loop-unroll ​                                 - Unroll loops
 +    -loop-vectorize ​                              - Loop Vectorization
 +    -loops ​                                       - Natural Loop Information
 +    -mem2reg ​                                     - Promote Memory to Register
 +    -memdep ​                                      - Memory Dependence Analysis
 +    -print-callgraph ​                             - Print a call graph
 +    -reg2mem ​                                     - Demote all values to stack slots
 +    -sample-profile ​                              - Sample Profile loader
 +    -simplifycfg ​                                 - Simplify the CFG
 +    -verify ​                                      - Module Verifier
 +    -view-callgraph ​                              - View call graph
 +    -view-cfg ​                                    - View CFG of function
 +    -view-cfg-only ​                               - View CFG of function (with no function bodies)
 +    -view-dom ​                                    - View dominance tree of function
 +    -view-dom-only ​                               - View dominance tree of function (with no function bodies)
 +    -view-postdom ​                                - View postdominance tree of function
 +    -view-postdom-only ​                           - View postdominance tree of function (with no function bodies)
 +  -p                                              - Print module after each transformation
 +  -stats ​                                         - Enable statistics output from program (available with Asserts)
 +  -time-passes ​                                   - Time each pass, printing elapsed time for each on exit
 +[...]
 +</​code>​
 +
 +==== Hello World ====
 +
 +Vom rula pass-ul default hello pe un cod simplu.
 +
 +Codul sursă al pass-ului Hello din LLVM se găsește în directorul ''​$LLVM_SRC/​lib/​Transforms/​Hello''​. Vizualizați codul sursă și makefile-ul. Pentru detalii suplimentare consultați [[http://​llvm.org/​docs/​WritingAnLLVMPass.html|pagina de manual]].
 + 
 +După cum ați observat anterior, acesta nu este găsit by default de către tool-ul ''​opt''​. Va trebui să îi specificăm calea către biblioteca dinamică compilată.
 +
 +<code bash>
 +student@cpl-vm ~/​llvm-3.6.2 $ opt -load llvm-3.6.2/​install/​lib/​LLVMHello.so --help
 +OVERVIEW: llvm .bc -> .bc modular optimizer and analysis printer
 +
 +USAGE: opt [options] <input bitcode file>
 +
 +[...]
 +    -hello ​                                       - Hello World Pass
 +    -hello2 ​                                      - Hello World Pass (with getAnalysisUsage implemented)
 +[...]
 +</​code>​
 +
 +Fie următorul cod sursă C:
 +<code C ex.c>
 +#include <​stdio.h>​
 +
 +int compute(int x)
 +{
 +    return 10 + 4 * x;
 +}
 +
 +int main(void)
 +{
 +    int x = 10 * 12;
 +    ​
 +    printf("​Printing something: %d\n", compute(x));​
 +    ​
 +    return 0;
 +}
 +</​code>​
 +
 +Vom genera fișierul ''​.bc''​ corespunzător:​
 +<code bash>
 +student@cpl-vm ~ $ clang -emit-llvm ex.c -c -o ex.bc
 +</​code>​
 +
 +Vom rula pass-ul ''​Hello''​ peste bitcode-ul obținut:
 +<code bash>
 +student@cpl-vm ~ $ opt -load llvm-3.6.2/​install/​lib/​LLVMHello.so --hello < ex.bc 
 +WARNING: You're attempting to print out a bitcode file.
 +This is inadvisable as it may cause display problems. If
 +you REALLY want to taste LLVM bitcode first-hand, you
 +can force output with the `-f' option.
 +
 +Hello: compute
 +Hello: main
 +</​code>​
 +
 +==== Integrarea unui pas în LLVM ====
 +
 +Pentru a nu mai fi nevoiți să specificăm biblioteca dinamică la runtime, aceasta trebuie să fie integrată în codul utilitarelor ''​opt''​ și ''​bugpoint''​. Pentru a realiza acest lucru, trebuie să modificăm [[http://​llvm.org/​docs/​WritingAnLLVMPass.html#​quickstart|exemplul]] dat în **sursele** de LLVM astfel:
 +  * ''​lib/​Transforms/​Hello/​Makefile''​
 +    * se şterge: **LOADABLE_MODULE = 1**
 +    * și se adaugă: **BUILD_ARCHIVE = 1**
 +  * ''​tools/​opt/​CMakeLists.txt''​
 +    * se adaugă noul pass în lista
 +      * **set(LLVM_LINK_COMPONENTS ${LLVM_TARGETS_TO_BUILD} Analysis CodeGen Hello)**
 +    * la fel și pentru ''​tools/​bugpoint/​CMakeLists.txt''​
 +  * se crează fişierul ''​include/​llvm/​Transforms/​Hello.h''​ cu conţinutul ​
 +    * <code c>
 +#ifndef LLVM_TRANSFORMS_HELLO_H
 +#define LLVM_TRANSFORMS_HELLO_H
 +namespace llvm {
 +    class FunctionPass;​
 +        FunctionPass *createHelloPass();​
 +    }
 +#endif
 +</​code>​
 +  * se creeaza fișierul ''​lib/​Transforms/​Hello/​LLVMBuild.txt''​ după formatul din [[http://​llvm.org/​docs/​CommandGuide/​llvm-build.html|sistemul de build]] llvm.
 +    * ex: <code bash>
 +[component_0]
 +type = Library
 +name = Hello
 +parent = Transforms
 +library_name = hello
 +required_libraries = Analysis Support
 +</​code>​
 +    * se updatează și fișierul LLVMBuild.txt din directorul părinte pentru a include sub-directorul ''​Hello''​. ​
 +  * ''​lib/​Transforms/​Hello/​CMakeLists.txt''​
 +    * se şterge **add_llvm_loadable_module( LLVMHello Hello.cpp )**
 +    * și se adaugă
 +      * **add_llvm_library( LLVMHello Hello.cpp )**
 +  * ''​lib/​Transforms/​Hello/​Hello.cpp''​
 +    * se include
 +      * **llvm/​Transforms/​Hello.h**
 +    * se adaugă
 +      *<code c>
 +FunctionPass* llvm::​createHelloPass()
 +{
 +   ​return new Hello();
 +}
 +</​code>​
 +  * ''​include/​llvm/​LinkAllPasses.h''​
 +    * se include **llvm/​Transforms/​Hello.h**
 +    * și se adaugă
 +      *<code c>
 +(void) llvm::​createHelloPass();​
 +</​code>​
 +  * se creeaza un director nou în care vor fi compilate sursele. De exemplu dacă sursele se află în directorul **llvm-3.3.src** și am creat pe același nivel directorul **llvm**, executăm din **interiorul directorului nou creat** comanda
 +    *<code bash>
 +cmake -G "Unix Makefiles"​ ../​llvm-3.3.src
 +</​code>​
 +    * urmată de comanda <code bash> make </​code>​
cpl/labs/llvm-pass.1448269402.txt.gz · Last modified: 2015/11/23 11:03 by laura.vasilescu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0