All Projects → zslwyuan → Llvm 9.0 Learner Tutorial

zslwyuan / Llvm 9.0 Learner Tutorial

Licence: gpl-3.0
A blog for LLVM(v9.0.0 or v11.0.0) beginner, step by step, with detailed documents and comments. Record the way I learn LLVM and accomplish a complete project for FPGA High-Level Synthesis with it.

Projects that are alternatives of or similar to Llvm 9.0 Learner Tutorial

Rxjava2 Android Samples
RxJava 2 Android Examples - Migration From RxJava 1 to RxJava 2 - How to use RxJava 2 in Android
Stars: ✭ 4,950 (+8434.48%)
Mutual labels:  samples, tutorial, sample
Phoenix Liveview Counter Tutorial
🤯 beginners tutorial building a real time counter in Phoenix 1.5.5 + LiveView 0.14.7 ⚡️
Stars: ✭ 115 (+98.28%)
Mutual labels:  tutorial, learn, beginner
Phoenix Todo List Tutorial
✅ Complete beginners tutorial building a todo list from scratch in Phoenix 1.5.3 (latest)
Stars: ✭ 65 (+12.07%)
Mutual labels:  tutorial, learn, beginner
Javascript Todo List Tutorial
✅ A step-by-step complete beginner example/tutorial for building a Todo List App (TodoMVC) from scratch in JavaScript following Test Driven Development (TDD) best practice. 🌱
Stars: ✭ 212 (+265.52%)
Mutual labels:  tutorial, learn, beginner
Learn Heroku
🏁 Learn how to deploy your web application to Heroku from scratch step-by-step in 7 minutes!
Stars: ✭ 110 (+89.66%)
Mutual labels:  tutorial, learn, beginner
Phoenix Chat Example
💬 A Step-by-Step Beginners Tutorial for Building, Testing & Deploying a Chat app in Phoenix 1.5.5 🚀
Stars: ✭ 452 (+679.31%)
Mutual labels:  tutorial, learn, beginner
C2goasm
C to Go Assembly
Stars: ✭ 1,072 (+1748.28%)
Mutual labels:  llvm, clang
Fcd
An optimizing decompiler
Stars: ✭ 622 (+972.41%)
Mutual labels:  llvm, clang
Python Tutorial
A Python 3 programming tutorial for beginners.
Stars: ✭ 647 (+1015.52%)
Mutual labels:  tutorial, beginner
Try Django
Learn Django bit by bit in this series
Stars: ✭ 767 (+1222.41%)
Mutual labels:  tutorial, learn
Tutoshowcase
A simple and Elegant Showcase view for Android
Stars: ✭ 499 (+760.34%)
Mutual labels:  tutorial, sample
Nodejs Getting Started
A tutorial for creating a complete application using Node.js on Google Cloud Platform
Stars: ✭ 705 (+1115.52%)
Mutual labels:  samples, tutorial
Lofocats ui
LofoCats UI is a simple web application consuming the LofoCats API, built with Ruby on Rails 🐱
Stars: ✭ 54 (-6.9%)
Mutual labels:  tutorial, sample
Graphql Editor
📺 Visual Editor & GraphQL IDE. Draw GraphQL schemas using visual 🔷 nodes and explore GraphQL API with beautiful UI. Even 🐒 can do that!
Stars: ✭ 5,485 (+9356.9%)
Mutual labels:  tutorial, learn
Minic Hosting
A simple stack-based virtual machine that runs C in the browser.
Stars: ✭ 628 (+982.76%)
Mutual labels:  tutorial, learn
Cmake Examples
Useful CMake Examples
Stars: ✭ 7,220 (+12348.28%)
Mutual labels:  clang, tutorial
Learn Vim
Learning Vim and Vimscript doesn't have to be hard. This is the guide that you're looking for.
Stars: ✭ 7,221 (+12350%)
Mutual labels:  tutorial, beginner
Vst24 Hello World
This project contains a "Hello World" style application for building a VST 2.4 plugin
Stars: ✭ 18 (-68.97%)
Mutual labels:  tutorial, beginner
Demo Multi Lang
Simple libp2p demos implemented in Go, JS and Rust
Stars: ✭ 17 (-70.69%)
Mutual labels:  tutorial, sample
Elm Cheat Sheet
An overview of Elm syntax and features
Stars: ✭ 928 (+1500%)
Mutual labels:  tutorial, learn

LLVM-9.0-Learner-Tutorial

A blog for LLVM(v9.0.0) beginner, step by step, with detailed documents and comments. (Actually I also implemented a similar tutorial project for LLVM-11). This blog records the way I learn LLVM and finnaly accomplish a complete project with it. Therefore, I will keep updating this blog according to the progress of my project. Currently, my target is to implement a source code optimizer. I am happy to discuss and any suggestion will be welcome ^_^ ([email protected]). If this blog is useful for you, a STAR will be encouragement to me. LOL

The learner note leads to our project for FPGA HLS, Light-HLS, presented in ICCAD 2019. If you are interested in analyzing LLVM from the perspective of project, you can have a look at Light-HLS. Please note that Light-HLS targets at FPGAs.If Light-HLS helps for your works, please cite our paper in ICCAD 2019 ^_^:

T. Liang, J. Zhao, L. Feng, S. Sinha and W. Zhang, "Hi-ClockFlow: Multi-Clock Dataflow Automation and Throughput Optimization in High-Level Synthesis," 2019 IEEE/ACM International Conference on Computer-Aided Design (ICCAD), Westminster, CO, USA, 2019, pp. 1-6. doi: 10.1109/ICCAD45719.2019.8942136

LLVM is very interesting tool but it is also upsetting for beginners (e.g. me). In this blog, I begin with the simplest pass construction and invocation, trying to explain the usage of some functions, types and variables. A complete project will be built gradually. I just hope this github can help some people like me to understand and handle LLVM better. PS: Clang provides user with Python-APIs, hence some people may use python to handle/analyze/transform the source code. An example project can be found here.

Please note that all the experiments are built with LLVM-9.0.0. In this blog, I tend to make the passes standalone, which means that they can be called in a main source code, but do not implement in the way which requires users compile the pass into dynamic library, re-make the LLVM source code and invoke the pass by "opt" command in the terminal. I think it might not be flexible, convenient and portable for development and testing. Visual Studio Code is the suggested editor since you can trace the functions and variables in LLVM very easily with VScode.

Category:

How to use this blog

List of Experiments I conducted

Further development


How to use this blog

  1. download the blog (entire project) and install LLVM-9.0.0. If you get problems during install LLVM, I suggest you to use the LLVM package with my install shell script.
  2. basic functiones and passes are implemented in the directory "Implementations". Nearly all the directories have their own README file to explain the directory.
  3. experiments are tested in the directory "Test".
  4. by making a "build" directory and using CMake in each experiment directory (e.g. this one), executable can be generated and tried. (hint: cmake .. & make)
  5. for user's convenience, I prepare some scripts for example, BuildAllFiles.sh, which will build all the projects, CleanBuiltFiles.sh, which will clean all the built files to shrink the size of the directories, and Build.sh in test directory, which will just build one test project. All these scripts can be run directly.
  6. looking into the source code with detailed comments, reader can trace the headers and functions to understand how the experiment work.

usage example

  When built, most test executables can be used like below but please check the source code for confirmation.
  (1) ./LLVM_expXXXXX  <C/C++ FILE> <top_function_name>   
  or
  (2) ./LLVM_expXXXXX  <C/C++ FILE>

  for comprehensive evaluation test:
  (1) ./LLVM_expXXXXX  <C/C++ FILE> <top_function_name> <configuration_file> [DEBUG]

List of Experiments I conducted:

Experiment 0: Find all the functions' names in the program. (Analysis Pass) PASS CODE TEST CODE

  -- keywords: installation of LLVM, basic pass template

Experiment 1: Get the dependence graph in a function and plot it. (Analysis Pass) PASS CODE TEST CODE

  -- keywords: dependence types, graph, iterator, passmanager, iterator, types in LLVM 

Experiment 2: Utilize existing LLVM passes in your project (Analysis Pass) TEST CODE

  -- keywords: invoke different passes in your application, createXXXXPass (or new xxxxPass), 

Experiment 3: Basic Loop information analysis (Analysis Pass) PASS CODE TEST CODE

  -- keywords: co-operation/dependence between Pass, Basic function for loop analysis

Experiment 4: Invoke polyhedral Information Pass (Analysis Pass) PASS CODE TEST CODE

  -- keywords: compilation with polly libraries, pass dependence

Experiment 5: Build a ASAP scheduler to schedule the instructions for parallel machine (e.g. FPGA) and evaluate latency (Analysis Pass) PASS CODE TEST CODE

  -- keywords: information processing, DFS traverse, comprehensively used the Function/Loop/Block information, basic scheduler implementation
  -- PS: Experiment 5a is just used to collect involved instructions in a IR file.

Experiment 6: Analyze the GEP lowering transformation pass (Passed implemented in previous experimented are analysis passes.) TEST CODE TEST CODE

  -- SeparateConstOffsetFromGEP, GEP Lowering, Transformation Pass, DataLayout

Experiment 7: Implementation a transformation pass to safely remove duplicated instrcutions (Transformation Pass) PASS CODE TEST CODE

  -- keywords: erase an instruction, remove duplicatied instruction, eraseFromParent, replaceAllUsesWith

Experiment 8: By using SCEV, Successfully obtain the range of the targe value and then implement bitwidth optimization (IR insertion/operand replacement/IR removal/reduncdant instruction check T_T....) (Transformation Pass) PASS CODE TEST CODE

  -- keywords: SCEV, value range, bitwidth optimization, Usage of IR Builder, change type/width of value

Experiment 9: Build a GEP operation transformation pass for HLS (Transformation Pass) PASS CODE TEST CODE

  --- HLS, ByteAlignment->ElementAlignment, SeparateConstOffsetFromGEP, GEP Lowering, Transformation Pass, DataLayout
  --- WARNING: Currently, array of struct is not supported. In the future, we need to transform array of struct to seperate array.

Experiment 10: Hack on Clang to detect arbitary precision integer ap_int in source code for VivadoHLS and mark them in the source code (Front-end Operation: AST Consumer) ACTION CODE

  --- Front-End Operation, AST Consumer, Visit Nodes in AST, Clang, Template Detection

Experiment 11: (Patch-Applied) Hack on Clang to properly map arbitary precision integer ap_int in source code for VivadoHLS into iX in IR generation (Front-end Operation: CodeGen) TEST CODE. Since a patch for LLVM-9 necessary for this tutorial is still under the review of LLVM, complete source code of LLVM for this tutorial can be downloaded via github in case that you cannot make the patch work properly. Example of declaring arbitrary precision integer variables is shown here.

  --- CodeGen Operation, AST Consumer, Visit Nodes in AST, Clang
  --- The original way I implement is too ugly and I applied the patch which Erich Keane shared via maillist clang-cfe.
  --- The patch, under review: https://reviews.llvm.org/D59105 , is developed by Erich Keane, Compiler Engineer, Intel Corporation. (Thanks Erich!^_^)

Experiment 12: Based on the library collected from VivadoHLS synthesis result, a Pass for more accurate evaluation of timing and resource is implemented. (Analysis Pass) PASS CODE TEST CODE

  --- HLS Timing/Resource Library for most instructions, Timing/Resource Evaluation, Operation Chaining for DSP Utilization

Experiment 12(a): Based on DominatorTree push instructions back to dominant ancestor to improve parallelism and reduce redundant instructions. (Transformation Pass) PASS CODE TEST CODE

  --- DominatorTree algorithm, parallelism, redudant instruction removal

Experiment 12(c): Since LLVM-provided loop strength reduction (LSR) pass limits itself according to the Target ISA addressing mode, an aggressive LSR pass is implemented for HLS to reduce the cost of DSPs on FPGA. (Transformation Pass) PASS CODE TEST CODE

  --- Loop Strength Reduction, Analysis based on Scalar Evolution, AddRecExpr, (Aggressive: may not stable for all the situations 0-0, but it works fine temporarily)
  --- Thank Momchil Velikov for his detailed reply about my inquiry about LSR pass

Experiment 13: Before involving HLS directives (array partitioning, loop pipelining/unroll), we need to check the memory access pattern of arrays in the source code (Analysis Pass) PASS CODE

  --- SCEV, Scalar Evolution Interpretation (AddRec, Add, Unknown, Constant), Array Access Pattern

Experiment 13c: map IR code back to source code based on DWARF debug information in IR metadata (Instruction=>line in source code; basic block/loop/function=>range in source code) PASS CODE TEST CODE

  --- DWARF Debug Information for the mapping from IR code to Source Code

Experiment 13d: unroll the specifc loops according to the label set in the source code and the setting in the configuration file (Transformation Pass) PASS CODE TEST CODE

  --- Loop Unrolling, Debug Information from Source Code

Experiment 14: (WIP) Based on the IR file, evaluate the resource and timing of the Window/Buffer/Filter in HLS

  --- IR transformation, Timing/Resource Evaluation for special structures on FPGA

Experiment 15: A flow of high-level synthesis including source code processing, implementation of HLS directives, IR optimization for FPGA implementation, timing scheduling and resource binding. It consists of a series of front-end IR processing and back-end (HI_WithDirectiveTimingResourceEvaluation pass is used to evaluate the timing and resource of the application, considering the effect of HLS directives.) TEST CODE (for more information for this flow, please refer to Light-HLS)

  --- Clang Source Code Transformation, IR optimization for FPGA HLS, implementation of HLS directives, timing scheduling and resource binding.

Further development

If you want to do your own works based on this project, following hints might be useful.

  1. user can add their passes according to the examples in the directory "Implementations".
  2. Note for development: (a) remember to define unique marco for header files (like #ifndef _HI_HI_POLLY_INFO); (b) Modify the CMakelists.txt file in the 4 directories: the pass directory(example), Implementation directory(example), LLVM_Learner_Libs directory(example) and the test directory(example). The modification should add subdirectory and consider the including path/library name.

Good Good Study Day Day Up (^o^)/~

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].