Question

I am trying to write a 5-element vector addition program in LLVM and can't figure out how to return the entire resultant vector. My code is below, and the error is

LLVM ERROR: Invalid return type of main() supplied

@veca = global [5 x i32] [i32 1, i32 2, i32 3, i32 4, i32 5]
@vecb = global [5 x i32] [i32 4, i32 6, i32 9, i32 2, i32 4]

define [5 x i32]* @vector_add([5 x i32]* %vec1, [5 x i32]* %vec2)
{
    entry:
    %vecc = alloca [5 x i32]
    br label %loop

    loop:
    %i  = phi i32 [0, %entry], [%nexti, %loop]
    %nexti = add i32 %i, 1
    %ptra = getelementptr [5 x i32]* %vec1, i64 0, i32 %i
        %loada = load i32* %ptra
    %ptrb = getelementptr [5 x i32]* %vec2, i64 0, i32 %i
    %loadb = load i32* %ptrb
    %added = add i32 %loada, %loadb
    %ptr = getelementptr [5 x i32]* %vecc, i32 0, i32 %i
    store i32 %added, i32* %ptr
    %eq = icmp eq i32 %i, 4
    br i1 %eq, label %loop, label %exit

    exit:
    ret [5 x i32]* %vecc
}

define [5 x i32] @main()
{
    %answer = call [5 x i32]* @vector_add([5 x i32]* @veca, [5 x i32]* @vecb)
    %ans = load [5 x i32]* %answer
    ret [5 x i32] %ans
}
Était-ce utile?

La solution

There is nothing wrong with this IR per se - it compiles just fine with llc. Your problem is probably with trying to JIT or interpret it, because an LLVM ExecutionEngine expects main with the standard signature:

int main()
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top