4

I have successfully run llvm opt with my toy transformation pass but do not see how to use 'opt' with built-in transformation passes http://llvm.org/docs/Passes.html#introduction

I have an empty hi.c file

int main(){
}

For example, if I want to use -instcount pass,

opt -instcount hi.c

gives me strange error.

opt: hi.c:1:1: error: expected top-level entity
int main(){
^

Use opt -instcount hi.bc does not work neither, with

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.

If I use opt -inst-count -f hi.bc, the output is a messy bitcode.

Question: how should we use 'opt' with built-in transformation passes (those from the link above)? Thanks for your ideas. 'opt -help' says

opt [options] <input bitcode file>

but my example above 'opt -instcount hi.bc' does not work as expected (see above).

zell
  • 9,830
  • 10
  • 62
  • 115

1 Answers1

13

At first: opt only works on bitcode / readable LLVM IR files. So passing a .c file will never work. You have to compile the .c file first with clang:

clang -emit-llvm in.c -o input.bc

The Warning you encounter says basicly everything:

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.

opt has as output the probably modified bitcode file and since you do not support an output file it will print it to stdout. That is why you get "messy" bitcode.

To use opt the way it should be you can use /dev/null to get rid of the output:

opt -inst-count input.bc -o /dev/null

or support an output file

opt -inst-count input.bc -o output.bc

or print the output as readable LLVM IR to stdout

opt -inst-count input.bc -S

or print the ouptut as readable LLVM IR file to disk

opt -inst-count input.bc -S -o output.ll
Michael Haidl
  • 5,384
  • 25
  • 43
  • It's worth noting, since the names of the passes appear to change with new versions of clang, that you can ask for the complete list of supported passes with `opt -print-passes`. – Dan Barowy Feb 03 '23 at 00:22