Last active
January 19, 2024 16:26
-
-
Save cmpark0126/459514e8e73cac0c76284b8ec47334a7 to your computer and use it in GitHub Desktop.
How to Generate LLVM IR from C Source Code?
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
name=${1%.*c} # detatch .c from c file name | |
clang-9 -O0 -Xclang -disable-O0-optnone -fno-discard-value-names -emit-llvm -c $name.c # convert source code(.c) to bit code(.bc) | |
llvm-dis-9 $name.bc -o $name.ll # generate llvm ir(.ll) from bit code(.bc) obtained above without any optimization | |
opt-9 -mem2reg $name.bc -o $name.mem2reg.bc # optimize bit code using `mem2reg` optimization pass before generating llvm ir | |
llvm-dis-9 $name.mem2reg.bc -o $name.mem2reg.ll # generate llvm ir(.ll) from optimized bit code(.bc) |
$ vi temp.c # create & write c source code file
$ sh llvm-ir-gen.sh temp.c # generate LLVM IR from `temp.c`
$ vi temp.ll # check LLVM IR
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The shell script above helps convert arbitrary C source code to LLVM IR.
Each command and flag have the following meanings:
clang-9
: the compiler front end for the C. It uses the LLVM compiler infrastructure as its back end. (-9
represents version of clang compiler)-O<arg>
: Flags controlling how much optimization should be performed.0
here means not to do optimization work.-Xclang <arg>
: Pass to the clang compiler.-disable-O0-optnone
: Flags preventing output function from being tagged asoptnone
under-O0
option.-O0
is applied, all output functions are tagged withoptnone
. These functions are excluded from LLVM optimization. Although we use-O0
, we should avoid theoptnone
as we would like to apply for the optimization pass likemem2reg
manually afterward.-fno-discard-value-names
: Flags keeping the variable name of the input source code.-emit-llvm
: Flags helping to produce a generic bit code file (not executable binary file)llvm-dis-9
: Thellvm-dis
command is the LLVM disassembler. It takes an LLVM bitcode file and converts it into human-readable LLVM assembly language.opt-9
: Theopt
command is the modular LLVM optimizer and analyzer. It takes LLVM source files as input, runs the specified optimizations or analyses on it, and then outputs the optimized file or the analysis results.-{passname}
:opt
provides the ability to run any of LLVM’s optimization or analysis passes in any order. The-help
option lists all the passes available.mem2reg
means to use mem2reg optimization pass here.