Questions tagged [llvm-ir]

The LLVM Intermediate Representation

The idea behind the LLVM IR is to be "light-weight and low-level while being expressive, typed, and extensible at the same time." This is achieved by being as low-level as possible while it still being possible to map higher level concepts to the IR. The LLVM IR also provides type information, which can be useful for some optimizations.

Source: Aalto University Wiki (LLVM IR Advanced Course on Compilers)

1263 questions
7
votes
1 answer

How to declare a function in LLVM and define it later

How can I declare a function in LLVM (with a specific signature) and create a call to it, e.g. llvm::Value* return = m_builder.CreateCall( function, arguments ); but then define the body of the function later (which must be an InlineAsm function)…
Marco A.
  • 43,032
  • 26
  • 132
  • 246
7
votes
3 answers

How do I get debug information from a function?

I've used Clang to compile a function with debug information enabled. For Instructions there's the handy getDebugLoc(), but there's no such thing for Functions. Given a Function instance, how can I get the debug information (I'm guessing in…
Oak
  • 26,231
  • 8
  • 93
  • 152
7
votes
1 answer

Using LLVM as virtual machine - multiplatform and multiarchitecture coding

I'm currently working in a pet programming language (for learning purposes), and have gone through a lot of research over the past year, and I think its time to finally start modelling the concepts of such a languague. First of all I want it to…
7
votes
1 answer

LLVM IR: efficiently summing a vector

I'm writing a compiler that's generating LLVM IR instructions. I'm working extensively with vectors. I would like to be able to sum all the elements in a vector. Right now I'm just extracting each element individually and adding them up manually,…
David Given
  • 13,277
  • 9
  • 76
  • 123
6
votes
2 answers

How to get bitcode llvm after linking?

I am trying to get LLVM IR for a file which is linked with some static libararies. I tried to link using llvm-link . It just copy the .bc files in one file ( not like native linking). clang -L$(T_LIB_PATH) -lpthread -emit-llvm gives an error:…
Hazem Abaza
  • 331
  • 4
  • 21
6
votes
1 answer

SIMD vector memory load in LLVM

What is the "correct" (i.e., portable) way in LLVM to load data from memory into a SIMD vector? Looking at the typical IR generated by LLVM's auto-vectorizer for an x86 target, it seems like the pattern is: bitcast a pointer to the scalar type…
bluescarni
  • 3,937
  • 1
  • 22
  • 33
6
votes
1 answer

What is the difference between `select` and `phi` in LLVM IR?

For example, I have a C code: void foo(int x) { int y; if (x > 0) { y = 1; } else { y = 2; } // do something with y } To simplify this code in LLVM IR level (where y can be put in the register rather than stack),…
Evian
  • 1,035
  • 1
  • 9
  • 22
6
votes
1 answer

How to implement function pointer by using LLVM C++ API?

Let's say I want manually turn code below to IR code: #include int main() { int (*p)(const char *__s); // how to implement this? p = puts; // and this? p("Hello World!\n"); } I found that the IR representation of…
user2269707
6
votes
1 answer

how to implement virtual table by using llvm

I'm writing a toy compiler and want my language support virtual methods, but I have no idea how to do it, it seems not as straight forward as other statements which can be easily turn into the IR code without a second thought, the v-table concept in…
user2269707
6
votes
1 answer

Assertion failure in LLVM with LLVMlite

(This happened while doing the fix I found here: LLVM IR: expose variables to GDB?) I am trying to compile some code written in a custom programming language. It is compiled to LLVM IR, and then compiled to an ELF by clang. However, I was trying to…
NoOneIsHere
  • 1,054
  • 16
  • 28
6
votes
1 answer

LLVM IR nested phi instruction

im working on a my own programming language. Im currently generating the code in LLVM IR. I got a question on nested If statement with phi. So lets say I have this in my language : if n < 0 then print("n < 0") else if 100 < n…
MaxThom
  • 1,165
  • 1
  • 13
  • 20
6
votes
0 answers

How to properly link pre-generated LLVM IR with runtime generated IR?

What is the proper way to load a set of pre-generated LLVM IR and make it available to runtime JIT modules such that the same types aren't given new names and inlining and const propagation can still take place? My attempt so far: I compile those C…
Unknown1987
  • 1,671
  • 13
  • 30
6
votes
2 answers

Equivalent of #include for 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…
Jon Chesterfield
  • 2,251
  • 1
  • 20
  • 30
6
votes
2 answers

How to get the textual representation of an LLVM IR instruction?

If I is of type llvm::Instruction, we can print out the same in human-readable form (textual representation) by errs() << I; I want the assign the exact same representation to a std::string to C type string. How can I do that?
sherlock
  • 2,397
  • 3
  • 27
  • 44
6
votes
2 answers

In LLVM IR, I want to copy a set of Instructions and paste those instructions to another place in IR through LLVM pass. How to do this?

I want to copy these set of instructions from one part and paste to that in another part in IR %0 = load i32, i32* @x, align 4 %1 = load i32, i32* @y, align 4 %add = add nsw i32 %0, %1 %2 = load i32, i32* @n, align 4 %cmp = icmp slt i32…