Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How should be used LLVM modules?

Tags:

llvm

I'm using LLVM to convert a user-defined language into bytecode, and I'm not sure to understand how should be used a module.

At the beginning, I thought it was something like the C/C++ object files (to avoid bytecode recompilation of every files when a single file is edited). However, I have found this line into LLVMpy documentation, which seems to say that it is not the case :

Inter-module reference is not possible. That is module A cannot call a function in module B, directly.

Can someone explain why are modules separated from the contexts if we can't have multiple modules for a single context ?

like image 465
Maël Nison Avatar asked May 15 '26 02:05

Maël Nison


1 Answers

It is possible, but like the .o files you mention, they must first be linked together into a single binary.

Given a pair of bitcode files:

$ llvm-dis a.bc -o -
; ModuleID = 'a.bc'

@0 = global [13 x i8] c"Hello world!\0A"

declare i32 @printf(i8*)                                                                                               

define void @f() {                                                                                                                                                                              
  %1 = call i32 @printf(i8* getelementptr inbounds ([13 x i8]* @0, i64 0, i64 0))                                                                                                               
  ret void
}

$ llvm-dis b.bc -o -
; ModuleID = 'b.bc'

declare void @f()

define i32 @main() {
  call void @f()
  ret i32 0
}

This won't work:

$ lli b.bc
LLVM ERROR: Program used external function 'f' which could not be resolved!

But if you link them together, it will:

$ llvm-ld a.bc b.bc -disable-opt -o c

$ llvm-dis c.bc -o -
; ModuleID = 'c.bc'

@0 = global [13 x i8] c"Hello world!\0A"

declare i32 @printf(i8*)

define void @f() {
  %1 = call i32 @printf(i8* getelementptr inbounds ([13 x i8]* @0, i64 0, i64 0))
  ret void
}

define i32 @main() {
  call void @f()
  ret i32 0
}

$ lli c.bc 
Hello world!
like image 188
null Avatar answered May 19 '26 04:05

null