Skip to content

Commit e095ebf

Browse files
committed
stage2: make use of proper LLVM intrinsic APIs in LLVM backend
1 parent da545d6 commit e095ebf

File tree

2 files changed

+19
-7
lines changed

2 files changed

+19
-7
lines changed

src/llvm_backend.zig

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
const std = @import("std");
2+
const assert = std.debug.assert;
23
const Allocator = std.mem.Allocator;
34
const Compilation = @import("Compilation.zig");
45
const llvm = @import("llvm_bindings.zig");
@@ -433,15 +434,20 @@ pub const LLVMIRModule = struct {
433434
}
434435

435436
fn genBreakpoint(self: *LLVMIRModule, inst: *Inst.NoOp) !?*const llvm.ValueRef {
436-
// TODO: Store this function somewhere such that we dont have to add it again
437-
const fn_type = llvm.TypeRef.functionType(llvm.voidType(), null, 0, false);
438-
const func = self.llvm_module.addFunction("llvm.debugtrap", fn_type);
439-
440-
// TODO: add assertion: LLVMGetIntrinsicID
441-
_ = self.builder.buildCall(func, null, 0, "");
437+
const llvn_fn = self.getIntrinsic("llvm.debugtrap");
438+
_ = self.builder.buildCall(llvn_fn, null, 0, "");
442439
return null;
443440
}
444441

442+
fn getIntrinsic(self: *LLVMIRModule, name: []const u8) *const llvm.ValueRef {
443+
const id = llvm.lookupIntrinsicID(name.ptr, name.len);
444+
assert(id != 0);
445+
// TODO: add support for overload intrinsics by passing the prefix of the intrinsic
446+
// to `lookupIntrinsicID` and then passing the correct types to
447+
// `getIntrinsicDeclaration`
448+
return self.llvm_module.getIntrinsicDeclaration(id, null, 0);
449+
}
450+
445451
fn resolveInst(self: *LLVMIRModule, inst: *ir.Inst) !*const llvm.ValueRef {
446452
if (inst.castTag(.constant)) |const_inst| {
447453
return self.genTypedValue(inst.src, .{ .ty = inst.ty, .val = const_inst.val });
@@ -514,7 +520,7 @@ pub const LLVMIRModule = struct {
514520

515521
pub fn fail(self: *LLVMIRModule, src: usize, comptime format: []const u8, args: anytype) error{ OutOfMemory, CodegenFail } {
516522
@setCold(true);
517-
std.debug.assert(self.err_msg == null);
523+
assert(self.err_msg == null);
518524
self.err_msg = try Compilation.ErrorMsg.create(self.gpa, src, format, args);
519525
return error.CodegenFail;
520526
}

src/llvm_bindings.zig

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,16 @@ pub const ModuleRef = opaque {
6363
pub const getNamedFunction = LLVMGetNamedFunction;
6464
extern fn LLVMGetNamedFunction(*const ModuleRef, Name: [*:0]const u8) ?*const ValueRef;
6565

66+
pub const getIntrinsicDeclaration = LLVMGetIntrinsicDeclaration;
67+
extern fn LLVMGetIntrinsicDeclaration(Mod: *const ModuleRef, ID: c_uint, ParamTypes: ?[*]*const TypeRef, ParamCount: usize) *const ValueRef;
68+
6669
pub const printToString = LLVMPrintModuleToString;
6770
extern fn LLVMPrintModuleToString(*const ModuleRef) [*:0]const u8;
6871
};
6972

73+
pub const lookupIntrinsicID = LLVMLookupIntrinsicID;
74+
extern fn LLVMLookupIntrinsicID(Name: [*]const u8, NameLen: usize) c_uint;
75+
7076
pub const disposeMessage = LLVMDisposeMessage;
7177
extern fn LLVMDisposeMessage(Message: [*:0]const u8) void;
7278

0 commit comments

Comments
 (0)