I’m trying to run some of the default passes in LLVM 12.0.1
My test code is as shown below (ripe for loop unrolling)
I compiled to generate the LLVM IR as follows:
clang++ -O0 -emit-llvm -c test.cpp -o test_o0.bc
I then tried to run the instcount pass as follows:
opt -analyze -instcount test_o0.bc
But I’m not getting any output as shown below:
(base) ~llvm-project-llvmorg-12.0.1/myfiles$ opt -analyze -instcount test_o0.bc
Printing analysis ‘Counts the various types of Instructions’ for function ‘__cxx_global_var_init’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘main’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_GLOBAL__sub_I_test.cpp’:
Please advise if I’m missing something. I also tried to loop unroll it but it doesn’t seem to give a different IR.
The -O0 option will add the optnone attribute to every function, and so nearly all optimization passes will not do anything.
You could replace -O0 with -O1 -disable-llvm-passes to avoid adding the optnone attribute, but still produce a .bc file with the IR just as clang produced it.
(base) ~/work/663/oldllvm/llvm-project-llvmorg-12.0.1/myfiles$ clang++ -O1 -disable-llvm-passes -emit-llvm -c test.cpp -o test_o1.bc
clang-12: warning: argument unused during compilation: ‘-disable-llvm-passes’ [-Wunused-command-line-argument]
(base) ~/work/663/oldllvm/llvm-project-llvmorg-12.0.1/myfiles$ opt -analyze -instcount test_o1.bc
Printing analysis ‘Counts the various types of Instructions’ for function ‘__cxx_global_var_init’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘main’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_ZNSt9basic_iosIcSt11char_traitsIcEE8setstateESt12_Ios_Iostate’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_ZNSt11char_traitsIcE6lengthEPKc’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘ZStorSt12_Ios_IostateS’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_ZNKSt9basic_iosIcSt11char_traitsIcEE7rdstateEv’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_GLOBAL__sub_I_test.cpp’:
Ah sorry, that should be -Xclang -disable-llvm-passes.
If you still don’t get the output you expect, you need help from someone who understands passes.
(base) ~/work/663/oldllvm/llvm-project-llvmorg-12.0.1/myfiles$ clang++ -O1 -Xclang -disable-llvm-passes -emit-llvm -c test.cpp -o test_o1.bc
(base) :~/work/663/oldllvm/llvm-project-llvmorg-12.0.1/myfiles$ opt -analyze -instcount test_o1.bc
Printing analysis ‘Counts the various types of Instructions’ for function ‘__cxx_global_var_init’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘main’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_ZStlsISt11char_traitsIcEERSt13basic_ostreamIcT_ES5_PKc’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_ZNSt9basic_iosIcSt11char_traitsIcEE8setstateESt12_Ios_Iostate’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_ZNSt11char_traitsIcE6lengthEPKc’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘ZStorSt12_Ios_IostateS’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_ZNKSt9basic_iosIcSt11char_traitsIcEE7rdstateEv’:
Printing analysis ‘Counts the various types of Instructions’ for function ‘_GLOBAL__sub_I_test.cpp’:
Unfortunately, that still didn’t fix it. Any help from folks would be really appreciated!
Well, those printouts are saying that the instcount analysis is executed.
Notice however that the pass doen’t automatically print anything (afaict). But it collects the counts as statistics. So your need the -stats option to also print the statistics.
> opt test/Transforms/InstNamer/basic.ll -instcount -o /dev/null -stats
===-------------------------------------------------------------------------===
... Statistics Collected ...
===-------------------------------------------------------------------------===
1 instcount - Number of Add insts
1 instcount - Number of Br insts
1 instcount - Number of Ret insts
2 instcount - Number of basic blocks
1 instcount - Number of non-external functions
3 instcount - Number of instructions (of all types)
(with more recent LLVM releases the syntax for specifying the pass would be -passes=instcount as that is how it is done nowadays with the new pass manager)
(base) ~/work/663/oldllvm/llvm-project-llvmorg-12.0.1/myfiles$ opt …/llvm/test/Transforms/InstNamer/basic.ll -instcount -analyze -stats
Printing analysis ‘Counts the various types of Instructions’ for function ‘f_0’:
It’s strange that it still doesn’t work. Does the order of flags matter?