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).
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
Related
Right now I am using the following line:
clang -Xclang -load -Xclang ../pass/pass.so -O2 -I../library/src/include/ -L../library/src/debug/ -DTAG_BITS=15 -lib1 -lib2 example.ll -o example
where I'm using my pass and example.ll is an example file that I linked with a runtime file (needed by the pass) using llvm-link. It does compile, but clang is skipping the libraries and using the built-ins instead:
clang: argument unused during compilation: '-I ../library/src/include/' [-Wunused-command-line-argument]
Why is the library being skipped? If I'm doing this wrong is there some other way I could compile the linked modules?
You've asked clang to compile example.ll which is an LLVM IR text file, and no other files. LLVM IR text doesn't have C-style #include statements, so the -I flag telling clang to look in your ../library/src/include/ directory to resolve files in a #include directive can't affect the compilation.
From https://github.com/riscv/riscv-llvm,
Using the llvm-riscv is fairly simple to build a full executable
however you need riscv64-unknown-*-gcc to do the assembling and
linking. An example of compiling hello world:
$ clang -target riscv64 -mriscv=RV64IAMFD -S hello.c -o hello.S
$ riscv64-unknown-elf-gcc -o hello.riscv hello.S
My question is: if I change the LLVM backend and get it to emit a new instruction in the hello.S file, how will riscv64-unknown-elf-gcc know how to convert it into object code? Do I also need to make changes in riscv64-unknown-elf-gcc so that it knows the format of the new instruction?
riscv64-unknown-elf-gcc calls as, i.e. usually GNU as from the binutils to assemble assembly code (i.e. hello.S in your snippet) into executable machine code. Thus you would have to modify the binutils if you want to assemble a new instruction.
Similar to GCC, clang supports stopping at different stages when processing C/C++. For example, passing a -E flag causes it to stop after the pre-processor and -c stops before linking.
So far, I am aware of,
-E : pre-processing
-fsyntax-only : syntax checking
-S : assembly
-c : object code
Am I missing any stopping points between those, or is that it?
You can also use -S -emit-llvm to generate LLVM IR assembly files and just -emit-llvm for LLVM bitcode object files. These are the language-independent code representations that clang and other LLVM front-ends generate and pass to LLVM to compile into an executable.
I have a source file which I preprocess using the options -E and -P (using GCC 4.1.2 for a vxWorks-based embedded platform). All other options are the same as when I compile the file. These options are:
-Wall
-march=pentium
-nostdinc
-O0
-fno-builtin
-fno-defer-pop
-g
-c
-o
as well as all include-paths. Now when I compile this preprocessed file, the resulting object-file is much smaller (about 30%) than when I compile the original directly. And when I then link the program, the linker complains about missing symbols (all in user-code), which again does not happen when using the original source-file. Why is there a difference? Is there any way to make this work?
You're sure you're not missing any -D defines from your command line? Your result would be consistent with parts not being compiled due to conditionals.
Another possibility (since you don't name the compiler specifically) is that you're using a generic gcc -E rather than the arch-specific cross compiler for your vxWorks environment. The cross-gcc will predefine some variables that you'll need for gcc -E.
When compiling the preprocessed output, try passing the -fpreprocessed option to tell GCC not to preprocess again.
The only difference I can think of is macros that result in expanding to an identifier that's a macro name that has already been expanded - the preprocessor stops expansion at that point, but if you ran the preprocessor again, the identifier would be expanded again. I would have expected any instances of this to probably cause a compiler error, but who knows?
I have set up an environment for autocompletion in Emacs, using clang 2.8 as the parser. It works well, but relies on saving the currently edited buffer to file before completion. This is slow, so I am trying to get clang to parse a file given to it via stdin instead, without luck so far.
The command line I feed clang when parsing a file is as follows:
clang -cc1 -fsyntax-only -Iinclude/ -code-completion-at foo.cpp:10:20 foo.cpp
This works well. But attempts to read from stdin fail. I've tried this:
cat foo.cpp | clang -xc++ -cc1 -fsyntax-only -Iinclude/ -code-completion-at -:10:20 -
But that makes clang terminate without making any completions and prints the warnings:
clang: warning: argument unused during compilation: '-cc1'
clang: warning: argument unused during compilation: '-code-completion-at'
clang: warning: argument unused during compilation: '-:10:20'
Any ideas?
Does it work if you specify -cc1 before -x c++?