LLVM C++ Program Compilation - c++

Hi I have written a simple C++ code with the LLVM Api
#include <stdio.h>
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/IRBuilder.h"
int main()
{
llvm::LLVMContext& context = llvm::getGlobalContext();
llvm::Module* module = new llvm::Module("top", context);
llvm::IRBuilder<> builder(context);
module->dump( );
}
How do i compile this to get the LLVM IR file?
Thanks for any help

Compilation of the program itself to a native machine code binary is straightforward. On a Unix platform:
$ clang++ <c++ file> `llvm-config --cppflags --ldflags --libs core`
llvm-config is a tool providing the necessary compilation flags to include certain components of the llvm framework. In your case, it seems you only need the core component.
Adding the -v parameter to the command line requires clang to output all the used commands. Adding -S -emit-llvm (both parameters must be used together) will make it output an llvm IR translation of the source
$ clang++ -v file.cpp `llvm-config --cppflags --ldflags --libs core` -S -emit-llvm
The resulting file.s may be then directly interpreted with lli or further compiled to bitcode using llvm-as, which renders a .bc file.
$ llvm-as file.s
The same lli will gladly run that file as well:
$ lli file.s.bc
Running or interpreting any of these resulting files (llvm IR, bitcode or native program), will of course yield the same result, the dumping of the llvm IR code of the module top defined in your program (which is empty).
Documentation:
llvm-config
llvm-as
lli

The easiest thing to do is to forward the output into a file and then process that so something like this(assuming the compiled code above is compiled into an executable name 'parser'):
./parser >> test.ll
llvm-as test.ll
llc test.bc
clang -o test test.s
Hopefully that helps!
As a side note I have my parser to this internally via system() calls and command line options, so that may be something you want to think about as well^^

Related

How to use LLVM library in c++ Xcode project?

I'm loosely following the tutorial to implement Kaleidoscope on the LLVM website, and I'm at the point where I need to actually use the LLVM library for code generation. I've installed LLVM 13 using homebrew (brew install llvm), but I can't figure out how to use it in a c++ project in Xcode. I just get the error 'llvm/whatever/whatever' file not found for every file I try to include:
#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/BasicBlock.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DerivedTypes.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Type.h"
#include "llvm/IR/Verifier.h"
I've tried adding the include folder (/opt/homebrew/Cellar/llvm/13.0.1_1/include for me) to the header search paths in Xcode, which seems to do something but I end up with the error Undefined symbol: llvm::DisableABIBreakingChecks, and hundreds of different warnings.
All I want is to be able to use LLVM code generation in a small c++ project. Is there any simple way to do this?
I'm also doing that tutorial. I'll tell you how I got it to work, but since I'm not an LLVM expert, I don't know if this is the best way.
I checked out and built LLVM from source, as described here. Installing with brew like you did should also work fine.
Next, I looked at the build command they give in the tutorial:
clang++ -g -O3 toy.cpp `llvm-config --cxxflags --ldflags --system-libs --libs core`
-o toy
I can run that llvm-config command from my build directory and it gives me the flags that I need to somehow give to Xcode.
> bin/llvm-config --cxxflags --ldflags --system-libs --libs core
-I/Users/rob/Foo/llvm-project/llvm/include
-I/Users/rob/Foo/llvm-project/build/include
-std=c++14 -fno-exceptions -fno-rtti
-D__STDC_CONSTANT_MACROS -D__STDC_FORMAT_MACROS -D__STDC_LIMIT_MACROS
-L/Users/rob/Foo/llvm-project/build/lib
-Wl,-search_paths_first -Wl,-headerpad_max_install_names
-lLLVMCore -lLLVMRemarks -lLLVMBitstreamReader
-lLLVMBinaryFormat -lLLVMSupport -lLLVMDemangle
-lm -lz -lcurses -lxml2
I put those -I paths in the HEADER_SEARCH_PATHS build setting, and the -L path in LIBRARY_SEARCH_PATHS. In Xcode those values are a list of paths, so you add the paths without the -I or -L part. I then put the -l library arguments in OTHER_LDFLAGS, and the -D arguments in OTHER_CPLUSPLUSFLAGS.
After that, it compiled and ran from Xcode. I did not yet translate all of the output from llvm-config into Xcode build settings. For example, I did nothing about these:
-std=c++14 -fno-exceptions -fno-rtti
-Wl,-search_paths_first -Wl,-headerpad_max_install_names
Remains to be seen if that will cause problems later.

Trouble compiling code from LLVM Kaleidoscope tutorial

When I used g++ -std=c++17 toy.cpp -o toy to compile the toy.cpp file without the header #include "llvm/ADT/STLExtras.h" in the toy.cpp file, everything was fine and I produced a "working" binary file.
However, when I added the #include "llvm/ADT/STLExtras.h" line to the toy.cpp file and compile it with the command g++ -std=c++17 toy.cpp -o toy, the terminal crushed me with the following output:
-bash: llvm-config: command not found
toy.cpp:1:10: fatal error: 'llvm/ADT/STLExtras.h' file not found
#include "llvm/ADT/STLExtras.h"
The same thing still happens even if I used
"clang++ -g -O3 toy.cpp llvm-config --cxxflags --ldflags --system-libs --libs core -o toy"
instead of "clang++ -g -O3 toy.cpp llvm-config --cxxflag" in the tutorial. My guess is that there's something wrong with the llvm-config thing. It's been bothering me for a while, please teach me how to solve it.
Link to the toy.cpp code
The answer to this is "llvm-config" is not working as expected.
Just check with which llvm-config to figure out whether you got this program. (mine is /usr/local/opt/llvm#8/bin/llvm-config). Or you can install with brew install llvm.
By the way, I came up with the same issue lately on kaleidoscope tutorial (ch2), but I want to know why the #include "llvm/ADT/STLExtras.h fails after several attempts on $(llvm-config --cxxflags --ldflags), etc or we aren't able to compile following chapter's code simply by removing the header declaration when llvm include files are necessary.

Unable to read the LLVM IR corresponding to HelloWorld.bc using llvm-dis

I have the program HelloWorld.c
#include <stdio.h>
int main() {
printf("hello world\n");
return 0;
}
which is compiled to the bitcode helloWorld.bc using the command :
clang -emit-llvm HelloWorld.c -c -o HelloWorld.bc
In order to print the LLVM IR corresponding to HelloWorld.bc, I try to disassemble the .bc file.
llvm-dis HelloWorld.bc
Strangely, the last command outputs nothing to my Terminal. Where am I wrong?
llvm-dis writes the output to HelloWord.ll not to the Terminal, by the way you can generate human-readable bitcode directly with -S instead of -c while invoking clang.

How to replace llvm-ld with clang?

Summary: llvm-ld has been removed from the LLVM 3.2 release. I am trying to figure out how to use clang in its place in my build system.
Note that I figured out the answer to my own question while writing it but I am still posting it in case it is useful to anyone else. Alternative answers are also welcome.
Details:
I have a build process which first generates bitcode using clang++ -emit-llvm. Then I take the bitcode files and link them together with llvm-link. Then I apply some standard optimization passes with opt. Then I apply another custom compiler pass with opt. Then I apply the standard optimization passes again using opt a third time. Finally I take the output from the last run of opt and use llvm-link to link with appropriate libraries to generate my executable. When I tried to replace llvm-link with clang++ in this process I get the error message: file not recognized: File format not recognized
To make this question more concrete I created a simplified example of what I am trying to do. First there are two files that I want to compile and link together
test1.cpp:
#include <stdio.h>
int getNum();
int main()
{
int value = getNum();
printf("value is %d\n", value);
return 0;
}
test2.cpp
int getNum()
{
return 5;
}
I executed the following sequence of commands:
clang++ -emit-llvm -c test1.cpp test2.cpp
llvm-link -o test.bc1 test1.o test2.o
opt test.bc1 -o test.bc2 -std-compile-opts
(Note that I am currently running llvm 3.1, but I'm trying to figure out the steps that will work for llvm 3.2. I assume that I should be able to make the LLVM 3.1 version work correctly using clang instead of llvm-ld)
Then if I run:
llvm-ld test.bc2 -o a.out -native
everything is fine and a.out prints out 5.
However, if I run:
clang++ test.bc2 -o a.out
Then I get the error message:
test.bc2: file not recognized: File format not recognized clang-3:
error: linker command failed with exit code 1 (use -v to see invocation)
Obviously I know that I can produce an executable file by running clang directly on the .cpp files. But I'm wondering what the best way to integrate clang with opt is.
The test case described in the question can be compiled using the following steps:
clang++ -emit-llvm -c test1.cpp test2.cpp
llvm-link -o test.bc1 test1.o test2.o
opt test.bc1 -o test.bc2 -std-compile-opts
llc -filetype=obj test.bc2 -o test.o
clang++ test.o
This produces a working a.out file.
It seems that llc is needed to convert from bitcode to machine code which can then be processed by clang as it normally would.
In general I've found that
llvm-ld x.bc y.bc
can be replaced with
llc x.bc
llc y.bc
clang x.s y.s

Specifying multiple files when compiling from C++ to LLVM IR using clang

I compile my C++ program to LLVM IR using the following command.
clang++ -O4 -emit-llvm program.cpp -c -o program.ll -S -pthread
However, now I want to do the same for multiple files. How can I do that? I want to produce one IR file after the compilation (not separate IR files for each file). In other words I want to have the linked code (code from all files linked together) in my IR file.
You're probably looking for the llvm-link command, which links bitcode files together.