error: expected value token when calling the GEP instruction - llvm

I'm playing with LLVM and have started with simple Hello World. Here's the code that I'm trying to run:
test.s:
; Declare the string constant as a global constant.
#.str = private unnamed_addr constant [13 x i8] c"Hello world!\00"
; External declaration of the puts function
declare i32 #puts(i8* nocapture) nounwind
; Definition of main function
define i32 #main() { ; i32()*
; Convert [13 x i8]* to i8 *...
%cast210 = getelementptr [13 x i8], [13 x i8]* #.str, i64 0, i64 0
; Call putr function to write out the string to stdout.
call i32 #puts(i8* %cast210)
ret i32 0
}
I took it from here : http://llvm.org/docs/LangRef.html#id610. When I run it I get the following error:
$lli test.s
lli: test.s:10:37: error: expected value token
%cast210 = getelementptr [13 x i8], [13 x i8]* #.str, i64 0, i64 0
^
It's a bit confusing when code from official LLVM website fails. However, it can be fixed by modifying the problematic line as follows:
test_fixed.s:
; Declare the string constant as a global constant.
#.str = private unnamed_addr constant [13 x i8] c"Hello world!\00"
; External declaration of the puts function
declare i32 #puts(i8* nocapture) nounwind
; Definition of main function
define i32 #main() { ; i32()*
; Convert [13 x i8]* to i8 *...
%cast210 = getelementptr [13 x i8]* #.str, i64 0, i64 0
; Call putr function to write out the string to stdout.
call i32 #puts(i8* %cast210)
ret i32 0
}
My question is: what is going on here? When I check the documentation for getelementptr: http://llvm.org/docs/LangRef.html#id937, I get the impression that test.s is indeed correct. Yet it doesn't work. Please help.
Some context info:
$ lli -version
LLVM (http://llvm.org/):
LLVM version 3.3
Optimized build.
Built Jun 18 2013 (05:58:10).
Default target: x86_64-pld-linux-gnu
Host CPU: bdver1

This should be a problem about the version mismatch between your lli and the official LLVM docs. The official LLVM docs is for the latest developing version of LLVM, 3.7.
The LLVM IR code in your question was update on Mar 4 2015. according to this link, after getelementptr instruction format was updated.
However, your version of lli is 3.3, which is released on Jun 18 2013.
Please update your llvm toolchain to the latest version, and try it again.

Related

Program used external function 'llvm.returnaddress.i8' which could not be resolved

I want to use llvm.returnaddress.i8 in a pass for getting return value of a function.
After I execute ../llvm/bin/lli test_re.bc, I get
LLVM ERROR: Program used external function 'llvm.returnaddress.i8' which could not be resolved!
I don't know how to solve it. Is there any suggestion? Thanks!
The intrinsic is called llvm.returnaddress, not llvm.returnaddress.i8.
declare i8* #llvm.returnaddress(i32)
declare i32 #printf(i8*, ...)
#fmt = constant [20 x i8] c"Return address: %p\0a\00"
define i32 #main() {
%fmt = getelementptr [20 x i8], [20 x i8]* #fmt, i32 0, i32 0
%ra = call i8* #llvm.returnaddress(i32 0)
call i32 (i8*, ...) #printf(i8* %fmt, i8* %ra)
ret i32 0
}

expected comma after getelementptr's type. LLVM

(1) #str = private constant [13 x i8] c"Hello World\0A\00"
(2) define i32 #main(){
(3) %r2 = getelementptr [13 x i8]* #str, i32 0, i32 0
(4) ret i32 0
(5) }
I've got an error error in the line 3: expected comma after getelementptr's type. How to deal with it?
getelemtptr expects the type that you are indexing (without the pointer) as it's first argument. In your case that would be [13 x i8], so you probably want to do something like this:
%r2 = getelementptr [13 x i8], [13 x i8]* #str, i32 0, i32 0

LLVM: ERROR: Constant unimplemented for type

Why does the LLVM pure interpreter performs differently from the JIT-enabled interpreter with respect of array constants? (LLVM 3.8.1)
I have the following code:
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128 :128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-apple-macosx10.9.0"
%MyType = type { i8* }
define i32 #main(i32 %argc, i8** %argv) nounwind uwtable {
%const.arr = alloca [8 x i8], align 8
store [8 x i8] c"\D0\CFT\15\01\00\00\00", [8 x i8]* %const.arr
%1 = bitcast [8 x i8]* %const.arr to %MyType*
ret i32 0
}
If I compile this with llvm-as test.llvm and lli test.llvm.bc, I get no error.
On the other hand, If I hit lli -force-interpreter test.llvm.bc I get the following error:
LLVM ERROR: ERROR: Constant unimplemented for type: [8 x i8]
Inspecting the code, I don't see anything wrong with it. Why does it perform differently when I force the interpreter?

Llvm_executionengine fails to execute pointer operations correctly

I'm trying to generate code to box and unbox values in my untyped language. For evaluating a simple integer literal 3, I generate:
define i64 #0() {
entry:
%value = alloca { i64, [10 x i8], <10 x i64> }
%boxptr = getelementptr inbounds { i64, [10 x i8], <10 x i64> }* %value, i32 0, i32 0
store i64 3, i64* %boxptr
%boxptr1 = getelementptr inbounds { i64, [10 x i8], <10 x i64> }* %value, i32 0, i32 0
%load = load i64* %boxptr1
ret i64 %load
}
Seems right, and lli evaluates it to 3, but Llvm_executionengine evaluates the function to 216172782113783808 (junk value). The code from my toplevel.ml looks like:
open Llvm_executionengine
let the_execution_engine = ExecutionEngine.create_interpreter the_module
let print_and_jit se =
let f = sexpr_matcher se in
let result = ExecutionEngine.run_function f [||] the_execution_engine in
print_string "Evaluated to ";
print_int (GenericValue.as_int result);
What's wrong with my interpreter?

Trouble understanding llvm.global.annotations

I am trying to understand the global annotations which I get with a kernel from llvm but I am kinda confused on a couple of things.
#sgv = internal constant [4 x i8] c"222\00"
#fgv = internal constant [0 x i8] zeroinitializer
#lvgv = internal constant [0 x i8*] zeroinitializer
#llvm.global.annotations = appending global [1 x { i8*, i8*, i8*, i8*, i32 }] [{ i8*, i8*, i8*, i8*, i32 } { i8* bitcast (void (float addrspace(1)*, float addrspace(1)*, float addrspace(1)*)* #add_kernel to i8*), i8* getelementptr inbounds ([4 x i8]* #sgv, i32 0, i32 0), i8* getelementptr inbounds ([0 x i8]* #fgv, i32 0, i32 0), i8* bitcast ([0 x i8*]* #lvgv to i8*), i32 0 }], section "llvm.metadata"
define void #add_kernel(float addrspace(1)* %out, float addrspace(1)* %in1, float addrspace(1)* %in2) #0 {
So I can understand the sgv, fgv & lvgv part. However, when I am looking at the global annotations the parts I am confused about it is this.
[1 x { i8*, i8*, i8*, i8*, i32 }] -- what does the first list mean
[{ i8*, i8*, i8*, i8*, i32 } --> This part before the kernel. What does this signify.
i8* getelementptr inbounds ([4 x i8]* #sgv, i32 0, i32 0), --> In this part am
assuming the i32 0, i32 0 refer to the GEP indexes ?
I need a little more explanation on how this annotation is structured and what is the use of this global annotation.
I think that the part which confuses you is the literal struct - which starts with the struct's type and follows with constants for all the struct's field. 4 of these constants are constant expressions.
Here's a basic breakdown of the AST (not including [, {, }, ], and ,) - click for larger version:
I was too lazy to drill down into the constant expressions as well - but they are basically all in the format <type> <instruction> ( <instruction args>... ) - see more in the language reference.