Skip to content

Commit

Permalink
Merge branch 'tinygo-org:master' into new-passes
Browse files Browse the repository at this point in the history
  • Loading branch information
rj45 authored Sep 20, 2023
2 parents 363a97e + ffd0534 commit b612c72
Show file tree
Hide file tree
Showing 12 changed files with 42 additions and 87 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,6 @@ jobs:
- name: Test LLVM 15
run:
go test -v -tags=llvm15
- name: Test default LLVM
run:
go test -v
test-macos-llvm-16:
runs-on: macos-latest
steps:
Expand All @@ -45,6 +42,9 @@ jobs:
- name: Test LLVM 16
run:
go test -v -tags=llvm16
- name: Test default LLVM
run:
go test -v
test-linux-llvm-14:
runs-on: ubuntu-20.04
steps:
Expand Down Expand Up @@ -73,9 +73,6 @@ jobs:
- name: Test LLVM 15
run:
go test -v -tags=llvm15
- name: Test default LLVM
run:
go test -v
test-linux-llvm-16:
runs-on: ubuntu-20.04
steps:
Expand All @@ -90,3 +87,6 @@ jobs:
- name: Test LLVM 16
run:
go test -v -tags=llvm16
- name: Test default LLVM
run:
go test -v
25 changes: 0 additions & 25 deletions bitreader.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,31 +24,6 @@ import (
"unsafe"
)

// ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the
// specified name, and returns a new LLVM module.
func ParseBitcodeFile(name string) (Module, error) {
var buf C.LLVMMemoryBufferRef
var errmsg *C.char
var cfilename *C.char = C.CString(name)
defer C.free(unsafe.Pointer(cfilename))
result := C.LLVMCreateMemoryBufferWithContentsOfFile(cfilename, &buf, &errmsg)
if result != 0 {
err := errors.New(C.GoString(errmsg))
C.free(unsafe.Pointer(errmsg))
return Module{}, err
}
defer C.LLVMDisposeMemoryBuffer(buf)

var m Module
if C.LLVMParseBitcode2(buf, &m.C) == 0 {
return m, nil
}

err := errors.New(C.GoString(errmsg))
C.free(unsafe.Pointer(errmsg))
return Module{}, err
}

// ParseBitcodeFile parses the LLVM IR (bitcode) in the file with the specified
// name, and returns a new LLVM module.
func (c Context) ParseBitcodeFile(name string) (Module, error) {
Expand Down
19 changes: 10 additions & 9 deletions executionengine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,11 @@ func TestFactorial(t *testing.T) {
InitializeNativeTarget()
InitializeNativeAsmPrinter()

mod := NewModule("fac_module")
ctx := NewContext()
mod := ctx.NewModule("fac_module")

fac_args := []Type{Int32Type()}
fac_type := FunctionType(Int32Type(), fac_args, false)
fac_args := []Type{ctx.Int32Type()}
fac_type := FunctionType(ctx.Int32Type(), fac_args, false)
fac := AddFunction(mod, "fac", fac_type)
fac.SetFunctionCallConv(CCallConv)
n := fac.Param(0)
Expand All @@ -34,26 +35,26 @@ func TestFactorial(t *testing.T) {
iffalse := AddBasicBlock(fac, "iffalse")
end := AddBasicBlock(fac, "end")

builder := NewBuilder()
builder := ctx.NewBuilder()
defer builder.Dispose()

builder.SetInsertPointAtEnd(entry)
If := builder.CreateICmp(IntEQ, n, ConstInt(Int32Type(), 0, false), "cmptmp")
If := builder.CreateICmp(IntEQ, n, ConstInt(ctx.Int32Type(), 0, false), "cmptmp")
builder.CreateCondBr(If, iftrue, iffalse)

builder.SetInsertPointAtEnd(iftrue)
res_iftrue := ConstInt(Int32Type(), 1, false)
res_iftrue := ConstInt(ctx.Int32Type(), 1, false)
builder.CreateBr(end)

builder.SetInsertPointAtEnd(iffalse)
n_minus := builder.CreateSub(n, ConstInt(Int32Type(), 1, false), "subtmp")
n_minus := builder.CreateSub(n, ConstInt(ctx.Int32Type(), 1, false), "subtmp")
call_fac_args := []Value{n_minus}
call_fac := builder.CreateCall(fac_type, fac, call_fac_args, "calltmp")
res_iffalse := builder.CreateMul(n, call_fac, "multmp")
builder.CreateBr(end)

builder.SetInsertPointAtEnd(end)
res := builder.CreatePHI(Int32Type(), "result")
res := builder.CreatePHI(ctx.Int32Type(), "result")
phi_vals := []Value{res_iftrue, res_iffalse}
phi_blocks := []BasicBlock{iftrue, iffalse}
res.AddIncoming(phi_vals, phi_blocks)
Expand Down Expand Up @@ -87,7 +88,7 @@ func TestFactorial(t *testing.T) {
pass.AddCFGSimplificationPass()
pass.Run(mod)

exec_args := []GenericValue{NewGenericValueFromInt(Int32Type(), 10, false)}
exec_args := []GenericValue{NewGenericValueFromInt(ctx.Int32Type(), 10, false)}
exec_res := engine.RunFunction(fac, exec_args)
var fac10 uint64 = 10 * 9 * 8 * 7 * 6 * 5 * 4 * 3 * 2 * 1
if exec_res.Int(false) != fac10 {
Expand Down
48 changes: 17 additions & 31 deletions ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,10 @@ package llvm
#include <stdlib.h>
*/
import "C"
import "unsafe"
import "errors"
import (
"errors"
"unsafe"
)

type (
// We use these weird structs here because *Ref types are pointers and
Expand Down Expand Up @@ -446,13 +448,6 @@ func (a Attribute) IsString() bool {

// Create and destroy modules.
// See llvm::Module::Module.
func NewModule(name string) (m Module) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
m.C = C.LLVMModuleCreateWithName(cname)
return
}

func (c Context) NewModule(name string) (m Module) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
Expand Down Expand Up @@ -561,17 +556,6 @@ func (c Context) IntType(numbits int) (t Type) {
return
}

func Int1Type() (t Type) { t.C = C.LLVMInt1Type(); return }
func Int8Type() (t Type) { t.C = C.LLVMInt8Type(); return }
func Int16Type() (t Type) { t.C = C.LLVMInt16Type(); return }
func Int32Type() (t Type) { t.C = C.LLVMInt32Type(); return }
func Int64Type() (t Type) { t.C = C.LLVMInt64Type(); return }

func IntType(numbits int) (t Type) {
t.C = C.LLVMIntType(C.unsigned(numbits))
return
}

func (t Type) IntTypeWidth() int {
return int(C.LLVMGetIntTypeWidth(t.C))
}
Expand All @@ -583,12 +567,6 @@ func (c Context) X86FP80Type() (t Type) { t.C = C.LLVMX86FP80TypeInContext(c.C)
func (c Context) FP128Type() (t Type) { t.C = C.LLVMFP128TypeInContext(c.C); return }
func (c Context) PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128TypeInContext(c.C); return }

func FloatType() (t Type) { t.C = C.LLVMFloatType(); return }
func DoubleType() (t Type) { t.C = C.LLVMDoubleType(); return }
func X86FP80Type() (t Type) { t.C = C.LLVMX86FP80Type(); return }
func FP128Type() (t Type) { t.C = C.LLVMFP128Type(); return }
func PPCFP128Type() (t Type) { t.C = C.LLVMPPCFP128Type(); return }

// Operations on function types
func FunctionType(returnType Type, paramTypes []Type, isVarArg bool) (t Type) {
var pt *C.LLVMTypeRef
Expand Down Expand Up @@ -704,9 +682,6 @@ func (c Context) VoidType() (t Type) { t.C = C.LLVMVoidTypeInContext(c.C); retu
func (c Context) LabelType() (t Type) { t.C = C.LLVMLabelTypeInContext(c.C); return }
func (c Context) TokenType() (t Type) { t.C = C.LLVMTokenTypeInContext(c.C); return }

func VoidType() (t Type) { t.C = C.LLVMVoidType(); return }
func LabelType() (t Type) { t.C = C.LLVMLabelType(); return }

//-------------------------------------------------------------------------
// llvm.Value
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -910,6 +885,18 @@ func ConstVector(scalarConstVals []Value, packed bool) (v Value) {
return
}

// IsConstantString checks if the constant is an array of i8.
func (v Value) IsConstantString() bool {
return C.LLVMIsConstantString(v.C) != 0
}

// ConstGetAsString will return the string contained in a constant.
func (v Value) ConstGetAsString() string {
length := C.size_t(0)
cstr := C.LLVMGetAsString(v.C, &length)
return C.GoStringN(cstr, C.int(length))
}

// Constant expressions
func (v Value) Opcode() Opcode { return Opcode(C.LLVMGetConstOpcode(v.C)) }
func (v Value) InstructionOpcode() Opcode { return Opcode(C.LLVMGetInstructionOpcode(v.C)) }
Expand Down Expand Up @@ -1349,7 +1336,6 @@ func (v Value) AllocatedType() (t Type) { t.C = C.LLVMGetAllocatedType(v.C); ret
// exclusive means of building instructions using the C interface.

func (c Context) NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilderInContext(c.C); return }
func NewBuilder() (b Builder) { b.C = C.LLVMCreateBuilder(); return }
func (b Builder) SetInsertPoint(block BasicBlock, instr Value) {
C.LLVMPositionBuilder(b.C, block.C, instr.C)
}
Expand Down Expand Up @@ -1388,7 +1374,7 @@ func (b Builder) GetCurrentDebugLocation() (loc DebugLoc) {
func (b Builder) SetInstDebugLocation(v Value) { C.LLVMSetInstDebugLocation(b.C, v.C) }
func (b Builder) InsertDeclare(module Module, storage Value, md Value) Value {
f := module.NamedFunction("llvm.dbg.declare")
ftyp := FunctionType(VoidType(), []Type{storage.Type(), md.Type()}, false)
ftyp := FunctionType(module.Context().VoidType(), []Type{storage.Type(), md.Type()}, false)
if f.IsNil() {
f = AddFunction(module, "llvm.dbg.declare", ftyp)
}
Expand Down
10 changes: 5 additions & 5 deletions ir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,11 @@ import (
)

func testAttribute(t *testing.T, name string) {
mod := NewModule("")
ctx := NewContext()
mod := ctx.NewModule("")
defer mod.Dispose()

ftyp := FunctionType(VoidType(), nil, false)
ftyp := FunctionType(ctx.VoidType(), nil, false)
fn := AddFunction(mod, "foo", ftyp)

kind := AttributeKindID(name)
Expand Down Expand Up @@ -104,11 +105,10 @@ func TestAttributes(t *testing.T) {
}

func TestDebugLoc(t *testing.T) {
mod := NewModule("")
ctx := NewContext()
mod := ctx.NewModule("")
defer mod.Dispose()

ctx := mod.Context()

b := ctx.NewBuilder()
defer b.Dispose()

Expand Down
1 change: 0 additions & 1 deletion llvm_config_darwin_llvm14.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !byollvm && darwin && llvm14
// +build !byollvm,darwin,llvm14

package llvm

Expand Down
3 changes: 1 addition & 2 deletions llvm_config_darwin_llvm15.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !byollvm && darwin && !llvm14 && !llvm16
// +build !byollvm,darwin,!llvm14,!llvm16
//go:build !byollvm && darwin && llvm15

package llvm

Expand Down
3 changes: 1 addition & 2 deletions llvm_config_darwin_llvm16.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !byollvm && darwin && llvm16
// +build !byollvm,darwin,llvm16
//go:build !byollvm && darwin && !llvm14 && !llvm15

package llvm

Expand Down
1 change: 0 additions & 1 deletion llvm_config_linux_llvm14.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !byollvm && linux && llvm14
// +build !byollvm,linux,llvm14

package llvm

Expand Down
3 changes: 1 addition & 2 deletions llvm_config_linux_llvm15.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !byollvm && linux && !llvm14 && !llvm16
// +build !byollvm,linux,!llvm14,!llvm16
//go:build !byollvm && linux && llvm15

package llvm

Expand Down
3 changes: 1 addition & 2 deletions llvm_config_linux_llvm16.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build !byollvm && linux && llvm16
// +build !byollvm,linux,llvm16
//go:build !byollvm && linux && !llvm14 && !llvm15

package llvm

Expand Down
1 change: 0 additions & 1 deletion llvm_dep.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
//===----------------------------------------------------------------------===//

//go:build !byollvm
// +build !byollvm

package llvm

Expand Down

0 comments on commit b612c72

Please sign in to comment.