Skip to content

Commit

Permalink
Replace JIT bindings with MCJIT bindings
Browse files Browse the repository at this point in the history
The former is deprecated and is on track to be removed before LLVM 3.6.
  • Loading branch information
pcc committed Aug 7, 2014
1 parent d3a95f5 commit bec5a8b
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 8 deletions.
5 changes: 3 additions & 2 deletions examples/factorial/factorial.go
Expand Up @@ -6,8 +6,9 @@ import "fmt"
import "github.com/go-llvm/llvm"

func test() {
llvm.LinkInJIT()
llvm.LinkInMCJIT()
llvm.InitializeNativeTarget()
llvm.InitializeNativeAsmPrinter()

mod := llvm.NewModule("fac_module")

Expand Down Expand Up @@ -56,7 +57,7 @@ func test() {
return
}

engine, err := llvm.NewJITCompiler(mod, 2)
engine, err := llvm.NewMCJITCompiler(mod, llvm.MCJITCompilerOptions{OptLevel: 2})
if err != nil {
fmt.Println(err)
return
Expand Down
19 changes: 16 additions & 3 deletions executionengine.go
Expand Up @@ -8,7 +8,7 @@ import "C"
import "unsafe"
import "errors"

func LinkInJIT() { C.LLVMLinkInJIT() }
func LinkInMCJIT() { C.LLVMLinkInMCJIT() }
func LinkInInterpreter() { C.LLVMLinkInInterpreter() }

type (
Expand All @@ -18,6 +18,12 @@ type (
ExecutionEngine struct {
C C.LLVMExecutionEngineRef
}
MCJITCompilerOptions struct {
OptLevel uint
CodeModel CodeModel
NoFramePointerElim bool
EnableFastISel bool
}
)

// helpers
Expand Down Expand Up @@ -82,9 +88,16 @@ func NewInterpreter(m Module) (ee ExecutionEngine, err error) {
}
return
}
func NewJITCompiler(m Module, optLevel int) (ee ExecutionEngine, err error) {

func NewMCJITCompiler(m Module, options MCJITCompilerOptions) (ee ExecutionEngine, err error) {
var cmsg *C.char
fail := C.LLVMCreateJITCompilerForModule(&ee.C, m.C, C.unsigned(optLevel), &cmsg)
copts := C.struct_LLVMMCJITCompilerOptions{
OptLevel: C.unsigned(options.OptLevel),
CodeModel: C.LLVMCodeModel(options.CodeModel),
NoFramePointerElim: boolToLLVMBool(options.NoFramePointerElim),
EnableFastISel: boolToLLVMBool(options.EnableFastISel),
}
fail := C.LLVMCreateMCJITCompilerForModule(&ee.C, m.C, &copts, C.size_t(unsafe.Sizeof(copts)), &cmsg)
if fail != 0 {
ee.C = nil
err = errors.New(C.GoString(cmsg))
Expand Down
5 changes: 3 additions & 2 deletions executionengine_test.go
Expand Up @@ -5,8 +5,9 @@ import (
)

func TestFactorial(t *testing.T) {
LinkInJIT()
LinkInMCJIT()
InitializeNativeTarget()
InitializeNativeAsmPrinter()

mod := NewModule("fac_module")

Expand Down Expand Up @@ -52,7 +53,7 @@ func TestFactorial(t *testing.T) {
return
}

engine, err := NewJITCompiler(mod, 2)
engine, err := NewMCJITCompiler(mod, MCJITCompilerOptions{OptLevel: 2})
if err != nil {
t.Errorf("Error creating JIT: %s", err)
return
Expand Down
8 changes: 8 additions & 0 deletions target.go
Expand Up @@ -86,6 +86,14 @@ func InitializeNativeTarget() error {
return nil
}

func InitializeNativeAsmPrinter() error {
fail := C.LLVMInitializeNativeAsmPrinter()
if fail != 0 {
return initializeNativeTargetError
}
return nil
}

//-------------------------------------------------------------------------
// llvm.TargetData
//-------------------------------------------------------------------------
Expand Down
2 changes: 1 addition & 1 deletion update_llvm.sh
Expand Up @@ -15,9 +15,9 @@ instrumentation \
interpreter \
ipo \
irreader \
jit \
linker \
mc \
mcjit \
objcarcopts \
option \
profiledata \
Expand Down

0 comments on commit bec5a8b

Please sign in to comment.