Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

proof-of-concept compiler engine using llvmlite #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

cdetrio
Copy link

@cdetrio cdetrio commented Jan 31, 2019

Supports just enough wasm instructions to run fibonacci.wasm.

(don't merge, this PR is only for reference).

$ python fib.py
calling visit on body: [['i32.const', 1], ['set_local', 1], ['block', [], [['get_local', 0], ['i32.const', 1], ['i32.lt_s'], ['br_if', 0], ['i32.const', 1], ['set_local', 3], ['i32.const', 0], ['set_local', 2], ['loop', [], [['get_local', 2], ['get_local', 3], ['i32.add'], ['set_local', 1], ['get_local', 3], ['set_local', 2], ['get_local', 1], ['set_local', 3], ['get_local', 0], ['i32.const', 4294967295], ['i32.add'], ['tee_local', 0], ['br_if', 0], ['end']]], ['end']]], ['get_local', 1], ['end']]
calling append_basic_block...
iterating over block_instrs...
got loop init block: %"loop.init" = loop.init:
iterating over loop block_instrs...
executing llvm code: ; ModuleID = "m_fibonacci_example"
target triple = "unknown-unknown-unknown"
target datalayout = ""

define i32 @"fn_fib"(i32 %".1")
{
entry:
  %".3" = alloca i32
  store i32 1, i32* %".3"
  br label %"block1"
block1:
  %".6" = alloca i32
  store i32 %".1", i32* %".6"
  %".8" = load i32, i32* %".6"
  %".9" = icmp slt i32 1, %".8"
  %".10" = alloca i32
  store i32 1, i32* %".10"
  %".12" = alloca i32
  store i32 0, i32* %".12"
  br label %"loop.init"
loop.init:
  %".15" = load i32, i32* %".12"
  %".16" = load i32, i32* %".10"
  %".17" = add i32 %".16", %".15"
  store i32 %".17", i32* %".3"
  %".19" = load i32, i32* %".10"
  store i32 %".19", i32* %".12"
  %".21" = load i32, i32* %".3"
  store i32 %".21", i32* %".10"
  %".23" = load i32, i32* %".6"
  %".24" = add i32 4294967295, %".23"
  store i32 %".24", i32* %".6"
  %".26" = icmp sgt i32 %".24", 0
  br i1 %".26", label %"loop.init.if", label %"loop.init.endif"
loop.init.if:
  br label %"loop.init"
loop.init.endif:
  br label %"contin-2"
contin-2:
  br label %"contin-1"
contin-1:
  %".31" = load i32, i32* %".3"
  ret i32 %".31"
}

verifying llvm module...
module verified!
c_fn_fib(9) = 55
0.029740958999999983
55

@poemm
Copy link
Owner

poemm commented Jan 31, 2019

Wow! So impressed!

I ran it successfully.

It is impressive that you can write this barely touching spec_execution.py, and that compiler.py is almost standalone. I am curious whether you had to reference the spec when you wrote it, or if the code was enough.

It is good that you are starting with basic cases. I saw that they use the visitor pattern, and you visit by iterating over instructions in each block. I am curious if things will get complicated for some control flow cases.

I wish to help in this effort. I will read about llvmlite this weekend, and will think about whether there are any complicated cases.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants