[gollvm] Can't exec LLVM IR from go code

224 views
Skip to first unread message

lufeiha...@gmail.com

unread,
Nov 29, 2019, 11:25:07 AM11/29/19
to golang-nuts
Hello! I recently want to use gollvm to analyse code. However I met a problem after I get the llvm ir whith the instructions shown in gollvm official doc ---- I can not exec the .ll file by lli command

------------------------------------------------------------
zty@zty-server:~/go-projects/src/testfile$ cat hello.go 
package main

import "fmt"

func main() {
fmt.Println("hello world!")
return
}

zty@zty-server:~/go-projects/src/testfile$ go build -work -x hello.go 1> transcript_hello.txt 2>&1

zty@zty-server:~/go-projects/src/testfile$ egrep '(WORK=|llvm-goc -c)' transcript_hello.txt 
WORK=/tmp/go-build177380678
/usr/local/llvmgo/bin/llvm-goc -c -O2 -g -m64 -fdebug-prefix-map=$WORK=/tmp/go-build -gno-record-gcc-switches -fgo-relative-import-path=_/home/zty/go-projects/src/testfile -o $WORK/b001/_go_.o -I $WORK/b001/_importcfgroot_ ./hello.go $WORK/b001/_gomod_.go

zty@zty-server:~/go-projects/src/testfile$ /usr/local/llvmgo/bin/llvm-goc -c -O2 -g -m64 -fdebug-prefix-map=/tmp/go-build177380678=/tmp/go-build -gno-record-gcc-switches -fgo-relative-import-path=_/home/zty/go-projects/src/testfile -o hello.ll -S -emit-llvm -I /tmp/go-build177380678/b001/_importcfgroot_ ./hello.go /tmp/go-build177380678/b001/_gomod_.go

zty@zty-server:~/go-projects/src/testfile$ ls hello.ll
hello.ll

zty@zty-server:~/go-projects/src/testfile$ lli hello.ll
lli: error: 'main' function not found in module.
---------------------------------------------------------------

After I get the hello.ll, I can not exec it by lli command, while for other .ll from c code I can exec it.
And I find that llvm pass can work on this "hello.ll", so what's the reason of it?

---------------------------------  Hello World pass work as normal

zty@zty-server:~/go-projects/src/testfile$ opt -load /home/zty/workarea/llvm-project/build/lib/LLVMHello.so -hello -time-passes < hello.ll > /dev/null
Hello: __go_init_main
Hello: main.main
Hello: main.main..init0
Hello: main.struct.4runtime.gList.2.runtime.n.0int32.5..hash
Hello: main.struct.4runtime.gList.2.runtime.n.0int32.5..eq
Hello: main.struct.4Size.0uint32.2Mallocs.0uint64.2Frees.0uint64.5..hash
Hello: main.struct.4Size.0uint32.2Mallocs.0uint64.2Frees.0uint64.5..eq
===-------------------------------------------------------------------------===
                      ... Pass execution timing report ...
===-------------------------------------------------------------------------===
  Total Execution Time: 0.0022 seconds (0.0022 wall clock)

   ---User Time---   --User+System--   ---Wall Time---  --- Name ---
   0.0017 ( 77.3%)   0.0017 ( 77.3%)   0.0017 ( 77.4%)  Bitcode Writer
   0.0004 ( 18.2%)   0.0004 ( 18.2%)   0.0004 ( 18.1%)  Hello World Pass
   0.0001 (  4.4%)   0.0001 (  4.4%)   0.0001 (  4.5%)  Module Verifier
   0.0022 (100.0%)   0.0022 (100.0%)   0.0022 (100.0%)  Total

===-------------------------------------------------------------------------===
                                LLVM IR Parsing
===-------------------------------------------------------------------------===
  Total Execution Time: 0.0023 seconds (0.0023 wall clock)

   ---User Time---   --User+System--   ---Wall Time---  --- Name ---
   0.0023 (100.0%)   0.0023 (100.0%)   0.0023 (100.0%)  Parse IR
   0.0023 (100.0%)   0.0023 (100.0%)   0.0023 (100.0%)  Total

--------------------------------

Than McIntosh

unread,
Dec 2, 2019, 9:53:19 AM12/2/19
to lufeiha...@gmail.com, golang-nuts
Greetings,

What you are seeing is expected behavior. In your "hello.go" file, you've defined a "main.main" function, however in order to run that function you need a copy of the Go runtime and standard libraries. When a Go program runs, the "main" function is in fact part of the runtime -- the runtime does a lot of initialization work to set up the scheduler, garbage collector, etc before eventually getting around to invoking the user-written main.main function.

The interpreter has a number of other quirks and restrictions that make it tricky to use for running Go programs -- folks have experimented with using it in the past (see https://go-review.googlesource.com/c/gofrontend/+/140917) but it is not something that is supported directly out of the box.

Hope this helps.

Than

--
You received this message because you are subscribed to the Google Groups "golang-nuts" group.
To unsubscribe from this group and stop receiving emails from it, send an email to golang-nuts...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/golang-nuts/bf03fa0d-d119-4faa-93c8-441b46d5634d%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages