Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Equivalent of #include for LLVM IR

Tags:

llvm

llvm-ir

I have found myself with a reasonably large number of useful functions and constants written in LLVM's IR. I can use this pseudo-library by combining it with hand written IR, provided said hand written IR starts with a potentially lengthy list of declarations. I'm aware that IR isn't necessarily designed as a general purpose programming language to write stuff in.

This is much like writing a lot of C functions in one file then redeclaring them wherever they are used. In C this is worked around using #include and header files. That's not perfect, but it beats writing out the prototypes repeatedly.

What's the least nasty way to achieve something similar in IR? It only has to beat typing the stuff out over and over again (which I currently do in copy & paste fashion) and using cat as a custom build step.

Thanks!

like image 682
Jon Chesterfield Avatar asked Sep 12 '17 11:09

Jon Chesterfield


2 Answers

Sadly there is no such thing in LLVM IR.

LLVM IR isn't designed to have large amounts of it written by hand. Therefore it doesn't have a #include mechanism. The job of handling that kind of stuff falls onto the compiler using the LLVM API.

One thing you could do however if you want to achieve the same effect is either to try to see if someone else's preprocessor will work for what you're trying to do or write a custom preprocessor yourself.

like image 53
caa515 Avatar answered Sep 20 '22 18:09

caa515


You can use llvm-link for combining different IRs together.

For example, you have the following sequence.

// file : f1.ll
; Function Attrs: nounwind readnone
define i32 @f1(i32 %a) #0 {
entry:
  ret i32 %a
}

// file : f2.ll
; Function Attrs: nounwind
define i32 @f2(i32 %a) #0 {
entry:
%call = tail call i32 @f1(i32 %a) #2
 ret i32 %call
}

Then you can call

llvm-link f1.ll f2.ll -S -o ffinal.ll

ffinal.ll would contain both IR codes.

like image 31
ConsistentProgrammer Avatar answered Sep 21 '22 18:09

ConsistentProgrammer