Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions IRBindings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
//===----------------------------------------------------------------------===//

#include "IRBindings.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/IRBuilder.h"
#include "llvm/IR/Intrinsics.h"
#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/Module.h"

Expand Down Expand Up @@ -85,3 +87,27 @@ LLVMValueRef LLVMGoGetInlineAsm(LLVMTypeRef Ty, char *AsmString,
IsAlignStack,
Dialect, CanThrow);
}

LLVMValueRef LLVMGoBuildIntrinsicCall(LLVMBuilderRef B, LLVMTypeRef RetTy,
unsigned ID, LLVMValueRef *Args,
unsigned Count, const char *Name) {
Intrinsic::ID IntrinsicID = static_cast<Intrinsic::ID>(ID);
ArrayRef<Value *> UnwrappedArgs(unwrap(Args), Count);
SmallVector<Type *, 8> ParamTys;
SmallVector<Type *, 8> OverloadTys;
SmallVector<Intrinsic::IITDescriptor, 8> Infos;

ParamTys.reserve(Count);
for (Value *Arg : UnwrappedArgs)
ParamTys.push_back(Arg->getType());

FunctionType *FTy = FunctionType::get(unwrap(RetTy), ParamTys, false);
Intrinsic::getIntrinsicInfoTableEntries(IntrinsicID, Infos);
ArrayRef<Intrinsic::IITDescriptor> InfoRef(Infos);
if (Intrinsic::matchIntrinsicSignature(FTy, InfoRef, OverloadTys) !=
Intrinsic::MatchIntrinsicTypes_Match)
return nullptr;

return wrap(unwrap(B)->CreateIntrinsic(IntrinsicID, OverloadTys,
UnwrappedArgs, nullptr, Name));
}
4 changes: 4 additions & 0 deletions IRBindings.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ LLVMValueRef LLVMGoGetInlineAsm(LLVMTypeRef Ty, char *AsmString,
LLVMBool IsAlignStack,
LLVMInlineAsmDialect Dialect, LLVMBool CanThrow);

LLVMValueRef LLVMGoBuildIntrinsicCall(LLVMBuilderRef B, LLVMTypeRef RetTy,
unsigned ID, LLVMValueRef *Args,
unsigned Count, const char *Name);

#ifdef __cplusplus
}
#endif
Expand Down
15 changes: 15 additions & 0 deletions ir.go
Original file line number Diff line number Diff line change
Expand Up @@ -385,6 +385,13 @@ func MDKindID(name string) (id int) {
return
}

func LookupIntrinsicID(name string) (id int) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
id = int(C.LLVMLookupIntrinsicID(cname, C.size_t(len(name))))
return
}

//-------------------------------------------------------------------------
// llvm.Attribute
//-------------------------------------------------------------------------
Expand Down Expand Up @@ -1955,6 +1962,14 @@ func (b Builder) CreateCall(t Type, fn Value, args []Value, name string) (v Valu
return
}

func (b Builder) CreateIntrinsic(ret Type, id int, args []Value, name string) (v Value) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
ptr, nvals := llvmValueRefs(args)
v.C = C.LLVMGoBuildIntrinsicCall(b.C, ret.C, C.unsigned(id), ptr, nvals, cname)
return
}

func (b Builder) CreateSelect(ifv, thenv, elsev Value, name string) (v Value) {
cname := C.CString(name)
defer C.free(unsafe.Pointer(cname))
Expand Down
30 changes: 30 additions & 0 deletions ir_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,36 @@ func TestDebugLoc(t *testing.T) {
}
}

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

memsetID := LookupIntrinsicID("llvm.memset")
if memsetID == 0 {
t.Fatal("could not look up llvm.memset intrinsic")
}
ptrTy := PointerType(ctx.Int8Type(), 0)
fnTy := FunctionType(ctx.VoidType(), []Type{ptrTy}, false)
fn := AddFunction(mod, "use_memset", fnTy)
builder := ctx.NewBuilder()
defer builder.Dispose()
builder.SetInsertPointAtEnd(ctx.AddBasicBlock(fn, "entry"))
call := builder.CreateIntrinsic(ctx.VoidType(), memsetID, []Value{
fn.Param(0),
ConstInt(ctx.Int8Type(), 0, false),
ConstInt(ctx.Int64Type(), 8, false),
ConstInt(ctx.Int1Type(), 0, false),
}, "")
builder.CreateRetVoid()
if got := call.CalledValue().IntrinsicID(); got != memsetID {
t.Fatalf("got intrinsic ID %d, want %d", got, memsetID)
}
if err := VerifyModule(mod, ReturnStatusAction); err != nil {
t.Fatalf("module with intrinsic call should verify: %v", err)
}
}

func TestSubtypes(t *testing.T) {
cont := NewContext()
defer cont.Dispose()
Expand Down
Loading