Post

Understanding GoLLVM

Introduction

Hey everyone I hope all are doing great recently at undevs My friend published an intresting article on LLVM-IR which attracted me to reserch more on it and in that process I found this GOLLVM which is currently under developing stage but really an intresting thing to research on it and then I tought why not write an article to help you understand better and there are not much articles on it.

So, In this Article I would love to keep as simple as possible to be understood by everyone, But before that I would request all of you to read https://un-devs.github.io/low-level-exploration/journey-to-understanding-llvm-ir/ So that understanding this Article would be easy.

What is Gollvm???

Gollvm

well, I would say gollvm is a less mature go compiler(among the 3 compilers gc,gccgo,gollvm) which uses the LLVM infrastructure. It incorporates “gofrontend” (a Go language front end written in C++ and shared with GCCGO), a bridge component (which translates from gofrontend IR to LLVM IR), and a driver that sends the resulting IR through the LLVM back end. this is a sub-project of llvm.now lets set up an environment to work on.

Setting up environment

For setting the work area at first i have reffered to the official repo of gollvm https://github.com/thanm/dragongo which was not helpfull for some reson so i cloned a diffrent repo and made slight changes to the repo. let me take you through step by step process.

1
2
3
4
5
6
7
8
mkdir work-space
git clone https://github.com/llvm/llvm-project.git
cd llvm-project/llvm/tools
git clone https://github.com/thanm/dragongo
cd dragongo/llvm-gofrontend
git clone https://go.googlesource.com/gofrontend
cd ../../../../../

with the help of the above steps we would be able to set up the environment now lets build the gollvm. As the gollvm is under development we dont have any pre build gollvm so we need to build it with the help of cmake and ninja.

Building gollvm

before building this make sure you have updated version of cmake and ninja.as well as a C/C++ compiler (V10.0 or later for Clang, or V6.0 or later of GCC), and a working copy of ‘m4’. lets first create a build directory and keep it seperate from the llvm-tree.

1
2
3
4
5
cd work-space
mkdir build-debug
cd build-debug
cmake -DCMAKE_BUILD_TYPE=Debug -DLLVM_USE_LINKER=gold -G Ninja ../llvm-project/llvm
ninja gollvm

with this you would be able to build the gollvm now lets Installing gollvm

Installing gollvm

A gollvm installation will contain ‘llvm-goc’ (the compiler driver), the libgo standard Go libraries, and the standard Go tools (“go”, “vet”, “cgo”, etc).

The installation directory for gollvm needs to be specified when invoking cmake prior to the build so lets create a directory and move on with installation

1
2
3
4
5
6
7
8
9
10
mkdir build.rel
cd build.rel
cmake -DCMAKE_INSTALL_PREFIX=/my/install/dir -DCMAKE_BUILD_TYPE=Release -DLLVM_USE_LINKER=gold -G Ninja ../llvm-project/llvm

// Build all of gollvm
ninja gollvm

// Install gollvm to "/my/install/dir"
ninja install-gollvm

now lets use the installed copy of gollvm to run a go program but before that we Programs build with the Gollvm Go compiler default to shared linkage, meaning that they need to pick up the Go runtime library via LD_LIBRARY_PATH: so lets first set the path and run it

export LD_LIBRARY_PATH=/tmp/gollvm-install/lib64 export PATH=/tmp/gollvm-install/bin:$PATH

lets first write a simple go program

1
2
3
4
5
6
7
8
9
package main
  
import "fmt"

func main() {
  
    fmt.Println("welcome to undev!")
}

now lets try running this.

1
2
3
4
$ go run hello.go

welcome to undev!

Vola we did this.

This post is licensed under CC BY 4.0 by the author.