[llvm-dev] Execution

Tim Northover via llvm-dev llvm-dev at lists.llvm.org
Fri Jun 16 14:13:35 PDT 2017


(Adding llvm-dev back to the thread in case anyone else comes along
later with the same problem).

Your problem now is this prototype:

> define i64 @"main"(i64 %".1")

That's almost certainly not how main is called on your system
(typically it has to be "i32 @main(i32, i8**)" or "i32 @main()" though
odder platforms will differ.

What value do you expect %.1 to have on entry? What do you expect to
happen to the value returned?

I suspect what you really want is to call your function "@fib" and
write a separate wrapper-function @main that calls @fib with sensible
arguments and maybe prints out the result afterwards. You could write
this in C and let Clang give you the IR you need. For example to take
the input as a command-line argument:

#include <stdio.h>
#include <stdlib.h>
long long fib(long long);
int main(int argc, char *argv[]) {
  printf("%lld\n", fib(atoi(argv[1])));
}

Clang (when I run "clang -Os -S -o- -emit-llvm file.c") tells me the
IR for this is:

@.str = private unnamed_addr constant [6 x i8] c"%lld\0A\00", align 1
define i32 @main(i32, i8** nocapture readonly)  {
  %3 = getelementptr inbounds i8*, i8** %1, i64 1
  %4 = load i8*, i8** %3, align 8
  %5 = tail call i32 @atoi(i8* %4)
  %6 = sext i32 %5 to i64
  %7 = tail call i64 @fib(i64 %6)
  %8 = tail call i32 (i8*, ...) @printf(i8* getelementptr inbounds ([6
x i8], [6 x i8]* @.str, i64 0, i64 0), i64 %7)
  ret i32 0
}
declare i32 @printf(i8* nocapture readonly, ...)
declare i32 @atoi(i8* nocapture)

Put that into a module with your @fib definition and you should be
able to run lli on it:

$ lli combined_module.ll 12
233
$

Cheers.

Tim.

On 16 June 2017 at 13:54, Samaneh Berenjian <sama.scientist at aut.ac.ir> wrote:
> Thank you for your kind reply. my IR looks like the following and I have
> gotten it from the code in
> https://ian-bertolacci.github.io/llvm/llvmlite/python/compilers/programming/2016/03/06/LLVMLite_fibonacci.html.
> How can I make it looks like what you said me?
> ; ModuleID = "m_fibonacci_example"
> target triple = "unknown-unknown-unknown"
> target datalayout = ""
>
> define i64 @"main"(i64 %".1")
> {
> fn_fib_entry:
>   %".3" = icmp sle i64 %".1", 1
>   br i1 %".3", label %"fn_fib_entry.if", label %"fn_fib_entry.endif"
> fn_fib_entry.if:
>   ret i64 1
> fn_fib_entry.endif:
>   %".6" = sub i64 %".1", 1
>   %".7" = sub i64 %".1", 2
>   %".8" = call i64 @"main"(i64 %".6")
>   %".9" = call i64 @"main"(i64 %".7")
>   %".10" = add i64 %".8", %".9"
>   ret i64 %".10"
> }
>
> Thank you in advance
>
>
>
>
> On Fri, 06/16/2017 03:50 PM, Tim Northover <t.p.northover at gmail.com>
> wrote:
>
> On 16 June 2017 at 12:19, Samaneh Berenjian via llvm-dev
> <llvm-dev at lists.llvm.org> wrote:
>> I have written a code in llvmlite. Using command numba --dump-llvm
>> example.py > example.ll I can have .ll file. However, using lli
>> example.ll,
>> I am stopped with error: 'main' function not found in module. Is there
>> anyway at which it can be executed using lli?
>
> By default lli looks for a function named @main since that's the
> traditional C and C++ entry point (amongst others these days). You can
> override that and tell it to execute an arbitrary function with the
> "-entry-function" argument. Probably wise to make sure the prototype
> matches main though (i.e. return and int and take either northing or
> an argc, argv pair).
>
> Cheers.
>
> Tim.
>
> --
> This email was Anti Virus checked by Security Gateway.


More information about the llvm-dev mailing list