I am trying to pass a global variable to a function as a parameter and modify this variable inside of a function
i have IR code as follows
#n = common global i32 0
declare i32 #readln(i32)
declare i32 #writeln(i32)
define i32 #main() {
entry:
%n = load i32, i32* #n, align 4
%calltmp = call i32 #readln(i32 %n)
%n1 = load i32, i32* #n, align 4
%calltmp2 = call i32 #writeln(i32 %n1)
ret i32 0
}
readln defined in separate module as a C function
int readln(int * x) {
return scanf("%d", x);
}
upon running i get segmentation fault 11
What am I doing wrong ?
comment is correct. inspire by this
Builder.CreateLoad(Builder.CreateIntToPtr(Value,Type::getInt32PtrTy(TheContext)), "ptr");
Builder.CreateIntToPtr(Value,Type::getInt32PtrTy(TheContext));
Related
I want to insert instructions into function without basic block, for example:
define void #_Z2f2v() nounwind {
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
%1 = load i32* %a, align 4
%2 = icmp sgt i32 %1, 0
ret void
}
But I read LLVM document, all C++ API I have are:
BasicBlock *bb = BasicBlock::Create(...);
irBuilder.setInsertPoint(bb);
irBuilder.CreateXXXInst(...);
or
Instruction *inst = new XXXInst(..., Instruction *insertBefore);
Instruction *inst = new XXXInst(..., BasicBlock *insertAtEnd);
It seems that I must create a BasicBlock at the beginning of a function.
How could I create instruction into function without BasicBlock by C++ API ?
I want to insert instructions into function without basic block, for example:
define void #_Z2f2v() nounwind {
%a = alloca i32, align 4
%b = alloca i32, align 4
store i32 2, i32* %a, align 4
%1 = load i32* %a, align 4
%2 = icmp sgt i32 %1, 0
ret void
}
That function contains exactly one basic block, not zero. To create a function like that, you add all of your instructions to the function's entry block.
How could I create instruction into function without BasicBlock by C++ API ?
You can't - neither using the C++ API nor any other way. Every instruction has to be part of a basic block by definition.
Basic blocks are the nodes in the CFG, so if you had an instruction without a basic block, it would not be part of the CFG and could therefore never be executed, which would be pointless.
I am trying to understand the LLVM IR generated from a C++ program
int add(int *x);
int func()
{
int T;
T=25;
return add(&T);
}
The generated IR is:
define i32 #_Z4funcv() local_unnamed_addr #0 {
entry:
%T = alloca i32, align 4
%0 = bitcast i32* %T to i8*
call void #llvm.lifetime.start.p0i8(i64 4, i8* nonnull %0) #3
store i32 25, i32* %T, align 4, !tbaa !2
%call = call i32 #_Z3addPi(i32* nonnull %T)
call void #llvm.lifetime.end.p0i8(i64 4, i8* nonnull %0) #3
ret i32 %call
}
I do not understand this line %0 = bitcast i32* %T to i8*. What is the purpose of converting %T from i32 to i8?
Assuming you know about intrinsics
llvm.lifetime.start / llvm.lifetime.end
and its uses as memory uses marker for MemoryDependenceAnalysis.
About the choice of pointer(address of variable) as i8 was made to make it more generic as byte addressable memory region with first arguments as number of bytes same as we use in malloc.
so to generate the intrinsic call we need a memory byte address and the number of bytes that is sizeof(T). that is why we need to convert i32* to i8*.
by the way the signature if lifetime intrinsics used in your examples are
declare void #llvm.lifetime.start(i64 , i8* nocapture )
declare void #llvm.lifetime.end(i64 , i8* nocapture )
go through Lang ref for more info.
LLVM is typed and monomorphical. Thus, you need to cast values to the right type before you can use them. As the other answer explained, the llvm intrinsics operate on i8* which is kind of the translation of a void* in C.
I want to create, from scratch, a new function in LLVM IR. The LLVM code should correspond to a C++ function with a reference argument, say
void foo(double &x){
x=0;
}
The tutorial such as http://llvm.org/releases/2.6/docs/tutorial/JITTutorial1.html is too old (llvm 2.6) and does not consider pass-by-reference function.
Any hint on how to do this? Thanks.
In LLVM, Reference types are typically implemented with pointer types. For the following C++ source code,
int foo(int & i) {
return i;
}
int bar(int *i) {
return *i;
}
void baz(int i) {
foo(i);
bar(&i);
}
The corresponding IR is:
; Function Attrs: nounwind
define i32 #_Z3fooRi(i32* dereferenceable(4) %i) #0 {
entry:
%i.addr = alloca i32*, align 8
store i32* %i, i32** %i.addr, align 8
%0 = load i32*, i32** %i.addr, align 8
%1 = load i32, i32* %0, align 4
ret i32 %1
}
; Function Attrs: nounwind
define i32 #_Z3barPi(i32* %i) #0 {
entry:
%i.addr = alloca i32*, align 8
store i32* %i, i32** %i.addr, align 8
%0 = load i32*, i32** %i.addr, align 8
%1 = load i32, i32* %0, align 4
ret i32 %1
}
; Function Attrs: nounwind
define void #_Z3bazi(i32 %i) #0 {
entry:
%i.addr = alloca i32, align 4
store i32 %i, i32* %i.addr, align 4
%call = call i32 #_Z3fooRi(i32* dereferenceable(4) %i.addr)
%call1 = call i32 #_Z3barPi(i32* %i.addr)
ret void
}
You can find that there is no essential difference for i between functions foo and bar: dereferenceable is just a parameter attribute that you can add yourself during the code generation from the frontend.
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i want to know if increment/decrement operator results vary from compiler to compiler or it is independent of the compiler. as I am getting different results in different compilers of c/c++ (tested in G++, TDM, gcc) for the same expression.
Let's look at the internal llvm assembly. Which isn't hard to do.
C++ code:
void preincrement() __attribute__((noinline));
void preincrement() {
volatile int x = 10;
volatile int y = ++x;
}
void postincrement() __attribute__((noinline));
void postincrement() {
volatile int x = 10;
volatile int y = x++;
}
int main()
{
preincrement();
postincrement();
}
Resultant LLVM Assembly (minus some leading and trailing text that isn't interesting):
; Function Attrs: noinline nounwind uwtable
define void #_Z12preincrementv() #0 {
%x = alloca i32, align 4
%y = alloca i32, align 4
store volatile i32 10, i32* %x, align 4
%1 = load volatile i32* %x, align 4
%2 = add nsw i32 %1, 1
store volatile i32 %2, i32* %x, align 4
store volatile i32 %2, i32* %y, align 4
ret void
}
; Function Attrs: noinline nounwind uwtable
define void #_Z13postincrementv() #0 {
%x = alloca i32, align 4
%y = alloca i32, align 4
store volatile i32 10, i32* %x, align 4
%1 = load volatile i32* %x, align 4
%2 = add nsw i32 %1, 1
store volatile i32 %2, i32* %x, align 4
store volatile i32 %1, i32* %y, align 4
ret void
}
; Function Attrs: nounwind uwtable
define i32 #main() #1 {
tail call void #_Z12preincrementv()
tail call void #_Z13postincrementv()
ret i32 0
}
We can see here pretty clearly the only difference: In the post increment version, we use the original value, instead of the upgraded value.
In both cases, we just use a traditional add opcode to do the real work.
I'm trying to figure out how to use the trampoline intrinsics in LLVM. The documentation makes mention of some amount of storage that's needed to store the trampoline in, which is platform dependent. My question is, how do I figure out how much is needed?
I found this example, that picks 32 bytes for apparently no reason. How does one choose a good value?
declare void #llvm.init.trampoline(i8*, i8*, i8*);
declare i8* #llvm.adjust.trampoline(i8*);
define i32 #foo(i32* nest %ptr, i32 %val)
{
%x = load i32* %ptr
%sum = add i32 %x, %val
ret i32 %sum
}
define i32 #main(i32, i8**)
{
%closure = alloca i32
store i32 13, i32* %closure
%closure_ptr = bitcast i32* %closure to i8*
%tramp_buf = alloca [32 x i8], align 4
%tramp_ptr = getelementptr [32 x i8]* %tramp_buf, i32 0, i32 0
call void #llvm.init.trampoline(
i8* %tramp_ptr,
i8* bitcast (i32 (i32*, i32)* #foo to i8*),
i8* %closure_ptr)
%ptr = call i8* #llvm.adjust.trampoline(i8* %tramp_ptr)
%fp = bitcast i8* %ptr to i32(i32)*
%val2 = call i32 %fp (i32 13)
; %val = call i32 #foo(i32* %closure, i32 42);
ret i32 %val2
}
Yes, trampolines are used to generate some code "on fly". It's unclear why do you need these intrinsics at all, because they are used to implement GCC's nested functions extension (in particular, when the address of the nested function is captured and the function access the stuff inside the enclosing function).
The best way to figure out the necessary size and alignment of trampoline buffer is to grep gcc sources for "TRAMPOLINE_SIZE" and "TRAMPOLINE_ALIGNMENT".
As far as I can see, at the time of this writing, the buffer of 72 bytes and alignment of 16 bytes will be enough for all the platforms gcc / LLVM supports.