Live coding a basic Go compiler with LLVM in 20 minutes

Overview

go2ll-talk

The code presented at Sheffield Go, 7th March.

Slides link

To run, just say make.

To take a look at the output of the program, run go run ., or build it and run it as you would any go program.

What's going on?

The goal of this talk was to show you how you could use some existing tooling to make a binary from Go code.

Of course, this isn't possible in 20 minutes, so we take some liberties:

  1. We use Go's x/tools/ssa (static single assignment) package, which enables us to turn Go's langauge semantics into a fairly simple data structure we can work with. (This is called the Intermediate Representation, or "IR").

  2. We use LLVM, which is a compiler framework which also uses static single assignment for its IR.

  3. The goal of this program then is to translate from one IR to the other.

To show some output, we use printf, which we steal from libc, C's runtime. We compile the resulting intermediate clang, the C frontend, which happens to be able to compile .ll (LLVM's "assembly" format).

This compiler is not at all general. It supports only the + operator and calling the println function (which is actually libc's printf function). So it can't do very much. But hopefully that simplicity is also what allows some newcomers to understand it.

go2ll

In the near future I plan to publish go2ll, which is a slightly more sophisticated frontend. It will only ever be a toy, because, for example, it is unlikely to implement garbage collection and goroutines. This means it won't be good for abitrary Go programs. On the other hand, I can still think of a few interesting uses, such as for speeding up CPU intensive compute kernels. I have already been able to demonstrate 30-40% speedups in already fairly well tuned code such as that for computing SHA1 and strconv.ParseFloat.

Issues
  • main.ll:7:2: error: instruction expected to be numbered '%1'

    main.ll:7:2: error: instruction expected to be numbered '%1'

    Hi. Can't build it using LLVM 11.0.0 release.

    $ make go run . > main.ll go: downloading github.com/llir/llvm v0.3.0-pre7 go: downloading golang.org/x/tools v0.0.0-20190226205152-f727befe758c go: downloading github.com/rickypai/natsort v0.0.0-20180124032556-f194e6bd5b0c go: downloading github.com/pkg/errors v0.8.0 clang -Wno-override-module main.ll main.ll:7:2: error: instruction expected to be numbered '%1' %0 = add i64 30, 12 ^ 1 error generated. make: *** [Makefile:3: all] Error 1

    Nevertheless

    ~/go2ll-talk/hello-world$ go run main.go Hello world %lld 42

    $ go version && clang --version go version go1.15.2 gollvm LLVM 12.0.0git linux/amd64 clang version 11.0.0 (https://github.com/llvm/llvm-project.git 0160ad802e899c2922bc9b29564080c22eb0908c) Target: x86_64-unknown-linux-gnu Thread model: posix InstalledDir: /usr/bin

    opened by advancedwebdeveloper 0
Owner
Peter Waller
Peter Waller
💯 Materials to help you rock your next coding interview

Tech Interview Handbook Credits: Illustration by @leftaligned Read on the website Black Lives Matter. Support the Equal Justice Initiative What is thi

Yangshun Tay 76.5k Aug 18, 2022
high performance coding with golang(Go 语言高性能编程,Go 语言陷阱,Gotchas,Traps)

Go 语言高性能编程 订阅 最新动态可以关注:知乎 Go语言 或微博 极客兔兔 订阅方式:watch geektutu/blog ,每篇文章都能收到邮件通知,或通过 RSS 订阅。

Dai Jie 3.1k Aug 9, 2022
Coding along the book

Learn Go with Tests Art by Denise Formats Gitbook EPUB or PDF Translations 中文 Português 日本語 한국어 Türkçe Support me I am proud to offer this resource fo

null 0 Oct 30, 2021
An open source, online coding platform that offers code practice and tutoring in 50 different programming languages

Exercism Golang En este repositorio voy a subir los ejercicios de la plataforma

Lucas Frontalini 0 Jan 7, 2022
Collection of coding examples from "Go In Practice"

Go In Practice Go In Practice 1. Noteworthy aspects of Go 2. A solid foundation 1. Noteworthy aspects of Go Multiple returns Named return values Read

Ignacio Herrera 0 Jan 3, 2022
Cracking the Coding Interview, 6th Ed

Cracking the Coding Interview, 6th Ed. In order to stay sharp, I try to solve a few of these every week. The repository is organized by chapter. Each

S. Elliott Johnson 0 Jan 26, 2022
This project is a collection of many of the basic tools used on Unix-like operating systems implemented in Go as a learning exercize.

GoUnix This project is a collection of many of the basic tools used on Unix-like operating systems implemented in Go as a learning exercize. The idea

Tom Hanson 65 Jul 21, 2022
I will be uploading some basic programming in Golang so if you want to contribute please Fork this repo and contriute.

Go-language I will be uploading some basic programming in Golang so if you want to contribute please Fork this repo and contriute. This repo is for pr

Sahitya Roy 3 Jan 21, 2022
A basic module for understanding functions, error handling, arrays, maps, unit testing, and compiling

Learning Go Basics Concepts A basic module for understanding functions, error handling, arrays, maps, unit testing, and compiling go mod init download

Gaurav Kaushik 0 Dec 15, 2021
Repo Tugas Basic Programming Part 2(Input/Output, Branching, Looping) ALTA Immersive BE5

Cara mengerjakan tugas hari ke 2 clone project ini dengan cara git clone https://github.com/jackthepanda96/Intro-to-Algoritm-Part2.git setelah clone

null 0 Dec 15, 2021
Repo Tugas Basic Programming Part 4(Complexity Analysis, Array, Slice, Function) ALTA Immersive BE5

Cara mengerjakan tugas hari ke 4 clone project ini dengan cara git clone https://github.com/ALTA-Immersive-BE5/Basic-Programming-Part4.git setelah cl

null 0 Dec 15, 2021
Repo Tugas Basic Programming Part 3(Array, Slice, Function) ALTA Immersive BE5

Cara mengerjakan tugas hari ke 3 clone project ini dengan cara git clone https://github.com/jackthepanda96/Basic-Programming-Part3.git setelah clone

null 0 Dec 15, 2021
Repo Tugas Basic Programming - ALTA Immersive Backend

Cara mengerjakan tugas hari ke 3 clone project ini dengan cara git clone https://github.com/jackthepanda96/Basic-Programming-Part3.git setelah clone

Jerry Young 0 Dec 15, 2021
A basic helloworld golang web program , just for personal use

Standard Go Project Layout Overview This is a basic helloworld golang web progra

Kay Yan 6 Jun 29, 2022
Tugas Alta Immersive Backend Golang Basic Programming Part 2(Input/Output, Branching, Looping)

Tatacara Melakukan Setup Tugas clone project ini dengan cara git clone https://github.com/Immersive-Backend-Resource/Basic-Programming-Part2.git sete

null 0 Jan 9, 2022
Tugas Alta Immersive Backend Golang Basic Programming Part 4(Complexity Analysis, Array, Slice, Function)

Tatacara Melakukan Setup Tugas clone project ini dengan cara git clone https://github.com/Immersive-Backend-Resource/Basic-Programming-Part4.git sete

null 0 Jan 9, 2022
Tugas Alta Immersive Backend Golang Basic Programming Part 3(Array, Slice, Function)

Tatacara Melakukan Setup Tugas clone project ini dengan cara git clone https://github.com/Immersive-Backend-Resource/Basic-Programming-Part3.git sete

null 0 Jan 9, 2022
Turtorial - A Hard Fork Of Icexin's Great Gocraft Project Aimed At Learning Basic Programming Tasks Through Turtles

TURTORIAL A voxel sandbox-survival game aimed at teaching the basics of programm

Joshua Steven Grant 1 Jan 25, 2022
Terminal interface to search the llvm C wrapper (llvm-c) API

Introduction This tool scrapes the LLVM-C API and (currently) prints all available API functions to stdout. It came to be from the lack of search opti

romes 11 Jan 4, 2022
Go compiler for small places. Microcontrollers, WebAssembly, and command-line tools. Based on LLVM.

TinyGo - Go compiler for small places TinyGo is a Go compiler intended for use in small places such as microcontrollers, WebAssembly (Wasm), and comma

TinyGo 11.3k Aug 11, 2022
Go compiler for small places. Microcontrollers, WebAssembly, and command-line tools. Based on LLVM.

TinyGo - Go compiler for small places TinyGo is a Go compiler intended for use in small places such as microcontrollers, WebAssembly (Wasm), and comma

TinyGo 11.3k Aug 10, 2022
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.

GraphJin - Build APIs in 5 minutes GraphJin gives you a high performance GraphQL API without you having to write any code. GraphQL is automagically co

Vikram Rangnekar 2.3k Aug 8, 2022
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.

GraphJin - Build APIs in 5 minutes GraphJin gives you a high performance GraphQL API without you having to write any code. GraphQL is automagically co

Vikram Rangnekar 2.3k Aug 10, 2022
GraphJin - Build APIs in 5 minutes with GraphQL. An instant GraphQL to SQL compiler.

GraphJin gives you a high performance GraphQL API without you having to write any code. GraphQL is automagically compiled into an efficient SQL query. Use it either as a library or a standalone service.

Vikram Rangnekar 2.3k Aug 18, 2022
Go compiler made from scratch, which can compile itself. It's going to be the smallest and simplest go compiler in the world.

Babygo, a go compiler made from scratch Babygo is a small and simple go compiler. (Smallest and simplest in the world, I believe.) It is made from scr

DQNEO 221 Aug 16, 2022
Compiler as a Service is a compiler that is available over http/https and gRPC

BlakBoks(CaaS) Elasticsearch but for compiling untrusted code Compiler as a Service is a compiler that is available over http/2 and gRPC. Setup First

Nathan Kimutai 0 Nov 24, 2021
ReCT-Go-Compiler - A compiler for the ReCT programming language written in Golang

ReCT-Go-Compiler A compiler for the ReCT programming language written in Golang

RedCubeDev 6 Apr 7, 2022
ReCT-Go-Compiler - A compiler for the ReCT programming language written in Golang

ReCT-Go-Compiler A compiler for the ReCT programming language written in Golang

null 6 Apr 7, 2022
Library for interacting with LLVM IR in pure Go.

llvm Library for interacting with LLVM IR in pure Go. Introduction Introductory blog post "LLVM IR and Go" Our Document Installation go get -u github.

null 931 Aug 9, 2022