Gollvm produces bad LLVM IR

332 views
Skip to first unread message

Khanh TN

unread,
Apr 8, 2021, 2:32:59 AM4/8/21
to golang-nuts
Hi,
I'm following this page https://go.googlesource.com/gollvm to compile Go code the LLVM way.
I'm at the step "How do I see the LLVM IR generated by gollvm?" of the page. I'm doing exactly as the instruction. Gollvm produces a .ll file but the file has bad syntax. Running llvm-as (or llc, lli) on the generated .ll file gives error (I think its syntax error)
Can anyone check this out?

Thankss,

Khanh

Than McIntosh

unread,
Apr 8, 2021, 7:07:13 AM4/8/21
to Khanh TN, golang-nuts
Hi,

Can you be a bit more specific, e.g. what Go code you compiler and what error you saw? It would also be helpful to know what version of lli/llc you were using.

Thanks, 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/e21d46ff-d151-46fc-ac1d-5da2a153a9fdn%40googlegroups.com.

Khanh TN

unread,
Apr 8, 2021, 10:00:33 AM4/8/21
to golang-nuts
Hi, 
Thanks for your reply.
It's a bit weird.
I tried LLVM10.0.1 and the LLVM13 built from github along with gollvm, both did not work.
However, I just tried LLVM11 and it worked. I haven't tried LLVM12. It looks like different LLVM version has different syntax?

The Go code I tried is simplest:
I have 1 file hello.go:

package main
import(
   "fmt"
)

func main() {
    fmt.Println("Hello, playground")
}

As instructed in "How do I see the LLVM IR generated by gollvm?" on https://go.googlesource.com/gollvm , I ran
> go build -work -x hello.go 1> transcript.txt 2>&1
> egrep '(WORK=|llvm-goc -c)' transcript.txt
WORK=/tmp/go-build903363323
[...]
> WORK=/tmp/go-build903363323
>[..change output to -o hello.ll; add -S -emit-llvm]

It outputs a hello.ll file. LLVM11 can process it but 10.0.1 and 13git cannot
Would love to hear your feedback. Maybe I need the exact LLVM version that gollvm is targeting?

Khanh

Than McIntosh

unread,
Apr 8, 2021, 10:40:19 AM4/8/21
to Khanh TN, golang-nuts
>It outputs a hello.ll file. LLVM11 can process it but 10.0.1 and 13git cannot
>Would love to hear your feedback. Maybe I need the exact LLVM version that gollvm is targeting?

Yes, that is correct. The LLVM IR dump format (*.ll files) tends to change over time and from release to release, it isn't intended or designed to be stable.

One way to insure that you have a conformant version of llc/lli (or equivalent) is just to build then in the same build directory where you built llvm. 

E.g. after "ninja gollvm" you can then try "ninja llc", then use the freshly built llc binary (found in ./bin) for your work.

Thanks, Than



Khanh TN

unread,
Apr 8, 2021, 10:55:16 AM4/8/21
to golang-nuts
Thanks. Looks like I need everything, including llc at LLVM11. I will stick with LLVM11.
I assume gollvm will match with whatever LLVM version is current, like, update to match LLVM12 when LLVM12 is out?

Khanh

Khanh TN

unread,
Apr 12, 2021, 11:51:34 AM4/12/21
to golang-nuts
Hi Than,
I'm trying to build gollvm with different versions of llvm: LLVM8, 10, 11 and 13git. It looks like only 13git works.
-emit-llvm has some issues. The produced *.ll and *.bc LLVM bitcode can only be opened with some of LLVM versions, and which version is quite random to me.
Is there a manual on which version of LLVM should I build gollvm with and which version is the bitcode that -emit-llvm produces?
Thanks,
Khanh

Than McIntosh

unread,
Apr 12, 2021, 12:06:15 PM4/12/21
to Khanh TN, golang-nuts
Hi,

At the moment gollvm does not put out releases per se, meaning that really the only thing that is advertised as working is the current LLVM tip plus the current gollvm tip at a given moment in time.

If you wanted to create a version of gollvm for an older LLVM release (ex: LLVM 10), you would need to look at the date that LLVM was released, then for each of the sub-repos (gollvm itself, <gollvm>/libgo/libbacktrace, <gollvm>/libgo/libffi, <gollvm>/gofrontend) you'd need to locate a git commit close to that date. Or something to this effect.

Can you tell me a bit more about your use case? Why not just use the most recent version of gollvm (e.g. tip)?

Thanks, Than


Khanh TN

unread,
Apr 12, 2021, 1:10:16 PM4/12/21
to golang-nuts
Hi,
Thanks for your reply.
I'm trying to extract the LLVM bitcode from Go code for analysis.
I did build from the tip of both LLVM and gollvm. I believe it is LLVM 13 on github. It produced good working binaries. However, the *.ll files produced by -emit-llvm by gollvm gives syntax error when read by the 13git version, so I tried LLVM11 and *.ll files work well with LLVM11. -emit-llvm can also output *.bc bitcode files. The *.bc files are, however, readable by 13git but not 11 version.
But I'm not sure this is exactly the problem, maybe I tweaked some parameters and got all the build messed up and got confused myself.

Khanh

Khanh TN

unread,
Apr 12, 2021, 10:38:40 PM4/12/21
to golang-nuts
Hi,
I somewhat figured out how to solve this.
Commands from LLVM like lli, llc for *.ll and *.bc files might work (or might not work) with arbitrary versions of LLVM (sometimes the tip doesnt work).
However, the command llvm-goc works well so I will stick with llvm-goc. I will ask later if I still need help.

Khanh
Reply all
Reply to author
Forward
0 new messages