Skip to content

Instantly share code, notes, and snippets.

@cmpark0126
Last active Nov 10, 2021
Embed
What would you like to do?
How to Generate LLVM IR from C Source Code?
#!/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)
@cmpark0126

This comment has been minimized.

Copy link
Owner Author

@cmpark0126 cmpark0126 commented Mar 2, 2020

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 as optnone under -O0 option.
      • When -O0 is applied, all output functions are tagged with optnone. These functions are excluded from LLVM optimization. Although we use -O0, we should avoid the optnone as we would like to apply for the optimization pass like mem2reg 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: The llvm-dis command is the LLVM disassembler. It takes an LLVM bitcode file and converts it into human-readable LLVM assembly language.
  • opt-9: The opt 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.
@cmpark0126

This comment has been minimized.

Copy link
Owner Author

@cmpark0126 cmpark0126 commented Mar 2, 2020

$ 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