Skip to content

Commit

Permalink
Remove functions/methods that work on the global context
Browse files Browse the repository at this point in the history
The global context should almost never be used, but it's very easy to
use accidentally. Therefore, I'd like to remove all convenience
functions that use the global context.

All functionality that got removed is still accessible as methods on
llvm.Context, and the global context can be obtained using
llvm.GlobalContext(). So no actual functionality is lost, just the very
easy to misuse "convenience" functions.
  • Loading branch information
aykevl committed Sep 19, 2023
1 parent 9edb640 commit ffd0534
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 68 deletions.
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
30 changes: 1 addition & 29 deletions ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,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 @@ -563,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 @@ -585,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 @@ -706,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 @@ -1363,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 @@ -1402,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

0 comments on commit ffd0534

Please sign in to comment.