Is there a way to compile llvm ir to .so - llvm

we can use llc -filetype=obj xxx.bc -o xxx.o to compile llvm module to static object. I'd like to compile .ll or .bc to .so for dynamic link. Is there a llvm tool can do this?

Related

Use compiled LLVM/clang without gcc

I have been trying to compile LLVM/clang from source, both by myself and using this script. I am compiling clang using gcc 11.2, which itself was also built from source, and hence resides in a non-default location on my system (/scratch/opt/gcc-ml-11.2). Note that it only works with a gcc that was compiled with multilib enabled.
The problem is that I can use the compiled clang and clang++ binaries only if I supply the gcc location as follows: --gcc-toolchain=/scratch/opt/gcc-ml-11.2.0. Without that flag, I run into linker errors:
clang test.c -o test
/usr/bin/ld: cannot find crtbeginS.o: No such file or directory
/usr/bin/ld: cannot find -lgcc
/usr/bin/ld: cannot find -lgcc_s
I do not understand why a clang compiled binary needs to link against some kind of gcc library and why clang does not supply the crtbeginS.O file.
I have also tried using the LLVM linker as well as the LLVM standard library for C++ test applications, i.e., "-fuse-ld=lld -stdlib=libc++. This did not help either.
While I could just export CXXFLAGS=--gcc-toolchain=/scratch/opt/gcc-ml-11.2.0, I am not sure whether this is the right approach. Shouldn't clang/LLVM be able to function without a gcc installation?
I have also tried using my clang installation with the --gcc-toolchain=/scratch/opt/gcc-ml-11.2.0 flag to compile clang again, but the new binary also requires the gcc-toolchain flag.
Should I just globally export the path to my gcc toolchain? Why is this necessary? Isn't clang a standalone compiler that - if supplied with the option to use its own standard library - does not require gcc?
Thank you very much for your help!
Best,
Maxbit

How do I compile files linked with llvm-link and pass my customized libraries and compiler pass to clang?

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.

Object code generation for new RISCV instruction emitted by LLVM backend

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.

How to force clang use llvm assembler instead of system?

I'm working on LLVM/Clang fork (for AVR). How can i force Clang Driver to use LLVM assembler instead of system one?
MBA-Anton:bin asmirnov$ ./clang++
-I/Applications/avr.app/Contents/Resources/Java/hardware/avr/cores/avr -I/Applications/avr.app/Contents/Resources/Java/hardware/avr/variants/standard
/var/folders/64/fwfkm1k51zbd4_c5lwpsbljh0000gn/T/build5450310618632673119.tmp/Blink.cpp
-o /tmp/Blink.avr.hex -I /Applications/avr.app//Contents/Resources/Java/hardware/tools/avr/avr/include/
--target=avr -O1 -v
And it uses LLVM clang compiler (not system, correct):
clang version 3.6.0 (https://github.com/4ntoine/clang.git 0d08deedd548d964f63cf896ae9acb8d878a5fd8) (https://github.com/dylanmckay/avr-llvm.git 447b58bced825fa7bea31f3882f277535cc9fca6)
Target: avr
Thread model: posix
"/Users/asmirnov/Documents/dev/src/llvm_dylan/installed/bin/clang" -cc1 ...
But system assembler (incorrect):
"/usr/bin/as" -o ...
It should use LLVM's llvm-as as it "knows" AVR target (opcodes, etc). How can i force it use LLVM's assembler?
You can use clang to emit LLVM IR code by using the option -emit-llvm.
Let's say you want to compile a C file into LLVM IR:
clang -emit-llvm -c file.c
It will create a LLVM bytecode file file.bc. You will be able to compile the file with llc.

cc1.exe fail to initialize modified dragonegg.dll

I want to generate LLVM bitcode rather than LLVM IR, from C and C++ source.
Original version of dragonegg works correctly.
Since it doesn't have a function to emit LLVM bitcode, I tried to add that, but it doesn't work.
What's wrong with my modification?
gcc -fplugin=dragonegg.dll -fplugin-arg-dragonegg-emit-llvm -I ..\include -c test.c
cc1.exe: error: fail to initialize plugin dragonegg.dll
I posted my patch to gist.
https://gist.github.com/vroad/1f2dcd2c37e03005a349
I compiled latest LLVM and dragonegg with plugin-enabled gcc for Windows.
SourceForge.net: MinGW - Minimalist GNU for Windows: http://sourceforge.net/mailarchive/message.php?msg_id=28248366