How to use the -pgo-instr-gen arguments with llvm opt - llvm

I want to test the PGO with llvm and I find most of the tutorial in the internet uses clang with argument -fprofile-instr-use to enable PGO instrumentation. However, what I have is an llvm bitcode file instead of the source code and I want to apply PGO to these bitcode files. I noticed the opt tool has the argument -pgo-instr-gen.
However, after I applied it to my bitcode like:
opt -pgo-instr-gen input.ll -o output.ll
and then tried to run the output.ll with lli, I got a segmentation fault. So, what is the correct way to enable PGO in such cases?

Related

Is there any LLVM pass to check whether LLVM IR is in SSA form?

Is there any built in LLVM pass to check whether the generated IR is in SSA form or not?
For example: If the IR was compiled using mem2reg option , we will get IR in SSA form. Can we check in our pass , whether mem2reg was used or not?
See llvm::verifyModule, llvm::verifyFunction, or llvm::createVerifierPass. cf http://llvm.org/doxygen/Verifier_8cpp_source.html .
See also opt's -verify-each option and the Verify* fields of PassManagerBuilder.
I guess you want to know which LLVM pass is being used.
In that case, you can run opt with -debug-pass=Arguments, or -mllvm -debug-pass=Arguemnt if you are using clang.
You can convert the code to human readable .ll and then try to find if there are phi nodes in the IR code. Assuming you have the bitcode
llvm-dis <file.bc> -o file.ll
cat file.ll | grep phi

llc: unsupported relocation on symbol

Problem
llc is giving me the following error:
LLVM ERROR: unsupported relocation on symbol
Detailed compilation flow
I am implementing an LLVM frontend for a middle-level IR (MIR) of a compiler, and after I convert various methods to many bitcode files, I link them (llvm-link), optimize them (opt), convert them to machine code (llc), make them a shared library (clang for it's linker wrapper), and dynamically load them.
llc step fails for some of the methods that I am compiling!
Step 1: llvm-link: Merge many bitcode files
I may have many functions calling each other, so I llvm-link the different bitcode files that might interact with each other. This step has no issues. Example:
llvm-link function1.bc function2.bc -o lnk.bc
Step 2: opt: Run optimization passes
For now I am using the following:
opt -O3 lnk.bc -o opt.bc
This step proceeds with no issues, but that's the one that CAUSES the problem!
Also, it's necessary because in the future I will need this step to pass extra passes, e.g. loop-unroll
Step 3: llc: Generate machine code (PIC)
I am using the following command:
llc -march=thumb -arm-reserve-r9 -mcpu=cortex-a9 -filetype=obj -relocation-model pic opt.bc -o obj.o
I have kept the arch specific flags I've set just in case they contribute to the issue. I am using Position Independent Code because on next step I will be building a shared object.
This command fails with the error I've written on top of this answer.
Step 4: clang: Generate Shared Object
For the cases that Step 3 fails, this step isn't reached.
If llc succeeds, this step will succeed too!
Additional information
Configuration
The following run on an llvm3.6, which runs on an arm device.
Things I've noticed
If I omit -O3 (or any other level) with the opt step, then llc would work.
If I don't, and instead I omit them from llc, llc would still fail. Which makes me think that opt -O<level> is causing the issue.
If I use llc directly it will work, but I won't be able to run specific passes that opt allows me, so this is not a option for me.
I've faced this issue ONLY with 2 functions that I've compiled so far (from their original MIR), which use loops. The others produce working code!
If I don't use pic model at llc, it can generate obj.o, but then I'll have problems creating an .so from it!
Questions
Why is this happening??!!
Why opt has -relocation-model option? Isn't that supposed to be just an llc thing? I've tried setting this both at opt and llc to pic, but still fails.
I am using clang because it has a wrapper to a linker to get the the .so. Is there a way to do this step with an LLVM tool instead?
First of all, do not use neither llc nor opt. These are developer-side tools that should be never used in any production environment. Instead of this, implement your own proper optimization and codegeneration runtime via LLVM libraries.
As for this particular bug - the thumb codegenerator might contain some bugs. Please reduce the problem and report it. Or don't use Thumb mode at all :)

How do I make LLVM opt output an IR file when given an IR file?

I use LLVM opt to run a pass using, e.g, opt -load libMyPass.so my-pass foo.ll > foo1.ll.
foo.ll is an IR file, and I want foo1.ll to contain the result, of running the pass, in IR format. But foo1.ll becomes a bitcode file, so I need to issue llvm-dis foo1.ll to transform it into IR format.
How do I avoid having to run llvm-dis, and make opt transform from IR format to IR format?
opt has a nice option for doing that:
-S - Write output as LLVM assembly
I guess what confuses you is that LLVM assembly is a synonym for LLVM IR.

How to add a new line of code in an LLVM pass?

I am writing an LLVM pass, in which i need to add a a line of code :
list ObserverBoardInterface* ObserverList;.
I need to add this at a particular point in the program. So how would I write a pass that directly adds this line of code (what approach should i take) and how do i enter this code at a particular point using the LLVM pass (signal at which point this change needs to be made)?
To obtain a set of instructions, you can write your C/C++ code and compile it to llvm bitcode with command:
clang test.cpp -emit-llvm -S -o test.ll
then open test.ll with your favorite editor and read the set of instructions.
Once, you can write your own pass, which:
will create a function with a set of instructions obtained above and
will find a point for call of this function

LLVM - What optimizations frontend has done

I know that frontend (such as llvm-clang or llvm-gcc ) has also done some optimizations from native code to IR level.
But what's optimizations that frontend has done ? Is there a list or a document I can check.
Thanks.
You can print all the passes which the code goes through by using:
clang -O2 -Rpass=.* code.cc -o code
This will also print the information from each of the optimization passes that were used to process the code when O2 level is used with clang, for example.
See this link for more details: http://clang.llvm.org/docs/UsersManual.html#options-to-emit-optimization-reports