Skip to content

Add CallModifier.never_intrinsify #21900

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
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
6 changes: 3 additions & 3 deletions lib/compiler_rt/arm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,15 @@ extern fn memmove(dest: ?[*]u8, src: ?[*]const u8, n: usize) ?[*]u8;

pub fn __aeabi_memcpy(dest: [*]u8, src: [*]u8, n: usize) callconv(.AAPCS) void {
@setRuntimeSafety(false);
_ = memcpy(dest, src, n);
_ = @call(.never_intrinsify, memcpy, .{ dest, src, n });
}
pub fn __aeabi_memcpy4(dest: [*]u8, src: [*]u8, n: usize) callconv(.AAPCS) void {
@setRuntimeSafety(false);
_ = memcpy(dest, src, n);
_ = @call(.never_intrinsify, memcpy, .{ dest, src, n });
}
pub fn __aeabi_memcpy8(dest: [*]u8, src: [*]u8, n: usize) callconv(.AAPCS) void {
@setRuntimeSafety(false);
_ = memcpy(dest, src, n);
_ = @call(.never_intrinsify, memcpy, .{ dest, src, n });
}

pub fn __aeabi_memmove(dest: [*]u8, src: [*]u8, n: usize) callconv(.AAPCS) void {
Expand Down
4 changes: 4 additions & 0 deletions lib/std/builtin.zig
Original file line number Diff line number Diff line change
Expand Up @@ -843,6 +843,10 @@ pub const CallModifier = enum {
/// Evaluates the call at compile-time. If the call cannot be completed at
/// compile-time, a compile error is emitted instead.
compile_time,

/// Prevents intrinsifying to corresponding implementation of builtin
/// function.
never_intrinsify,
};

/// This data structure is used by the Zig language code generation and
Expand Down
4 changes: 2 additions & 2 deletions lib/std/zig/Zir.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2718,8 +2718,8 @@ pub const Inst = struct {

pub const Flags = packed struct {
/// std.builtin.CallModifier in packed form
pub const PackedModifier = u3;
pub const PackedArgsLen = u27;
pub const PackedModifier = u4;
pub const PackedArgsLen = u26;

packed_modifier: PackedModifier,
ensure_result_used: bool = false,
Expand Down
5 changes: 4 additions & 1 deletion src/Air.zig
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,8 @@ pub const Inst = struct {
call_never_tail,
/// Same as `call` except with the `never_inline` attribute.
call_never_inline,
/// Same as `call` except with the `never_intrinsify` attribute.
call_never_intrinsify,
/// Count leading zeroes of an integer according to its representation in twos complement.
/// Result type will always be an unsigned integer big enough to fit the answer.
/// Uses the `ty_op` field.
Expand Down Expand Up @@ -1501,7 +1503,7 @@ pub fn typeOfIndex(air: *const Air, inst: Air.Inst.Index, ip: *const InternPool)

.tag_name, .error_name => return Type.slice_const_u8_sentinel_0,

.call, .call_always_tail, .call_never_tail, .call_never_inline => {
.call, .call_always_tail, .call_never_tail, .call_never_inline, .call_never_intrinsify => {
const callee_ty = air.typeOf(datas[@intFromEnum(inst)].pl_op.operand, ip);
return Type.fromInterned(ip.funcTypeReturnType(callee_ty.toIntern()));
},
Expand Down Expand Up @@ -1620,6 +1622,7 @@ pub fn mustLower(air: Air, inst: Air.Inst.Index, ip: *const InternPool) bool {
.call_always_tail,
.call_never_tail,
.call_never_inline,
.call_never_intrinsify,
.cond_br,
.switch_br,
.loop_switch_br,
Expand Down
1 change: 1 addition & 0 deletions src/Air/types_resolved.zig
Original file line number Diff line number Diff line change
Expand Up @@ -330,6 +330,7 @@ fn checkBody(air: Air, body: []const Air.Inst.Index, zcu: *Zcu) bool {
.call_always_tail,
.call_never_tail,
.call_never_inline,
.call_never_intrinsify,
=> {
const extra = air.extraData(Air.Call, data.pl_op.payload);
const args: []const Air.Inst.Ref = @ptrCast(air.extra[extra.end..][0..extra.data.args_len]);
Expand Down
4 changes: 2 additions & 2 deletions src/Liveness.zig
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ pub fn categorizeOperand(
return .none;
},

.call, .call_always_tail, .call_never_tail, .call_never_inline => {
.call, .call_always_tail, .call_never_tail, .call_never_inline, .call_never_intrinsify => {
const inst_data = air_datas[@intFromEnum(inst)].pl_op;
const callee = inst_data.operand;
const extra = air.extraData(Air.Call, inst_data.payload);
Expand Down Expand Up @@ -1109,7 +1109,7 @@ fn analyzeInst(
return analyzeOperands(a, pass, data, inst, .{ prefetch.ptr, .none, .none });
},

.call, .call_always_tail, .call_never_tail, .call_never_inline => {
.call, .call_always_tail, .call_never_tail, .call_never_inline, .call_never_intrinsify => {
const inst_data = inst_datas[@intFromEnum(inst)].pl_op;
const callee = inst_data.operand;
const extra = a.air.extraData(Air.Call, inst_data.payload);
Expand Down
2 changes: 1 addition & 1 deletion src/Liveness/Verify.zig
Original file line number Diff line number Diff line change
Expand Up @@ -340,7 +340,7 @@ fn verifyBody(self: *Verify, body: []const Air.Inst.Index) Error!void {
}
try self.verifyInst(inst);
},
.call, .call_always_tail, .call_never_tail, .call_never_inline => {
.call, .call_always_tail, .call_never_tail, .call_never_inline, .call_never_intrinsify => {
const pl_op = data[@intFromEnum(inst)].pl_op;
const extra = self.air.extraData(Air.Call, pl_op.payload);
const args = @as(
Expand Down
3 changes: 2 additions & 1 deletion src/Sema.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7602,6 +7602,7 @@ fn analyzeCall(

.never_tail => Air.Inst.Tag.call_never_tail,
.never_inline => Air.Inst.Tag.call_never_inline,
.never_intrinsify => Air.Inst.Tag.call_never_intrinsify,
.always_tail => Air.Inst.Tag.call_always_tail,

.async_kw => return sema.failWithUseOfAsync(block, call_src),
Expand Down Expand Up @@ -25719,7 +25720,7 @@ fn zirBuiltinCall(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError
var modifier = zcu.toEnum(std.builtin.CallModifier, modifier_val);
switch (modifier) {
// These can be upgraded to comptime or nosuspend calls.
.auto, .never_tail, .no_async => {
.auto, .never_tail, .never_intrinsify, .no_async => {
if (block.is_comptime) {
if (modifier == .never_tail) {
return sema.fail(block, modifier_src, "unable to perform 'never_tail' call at compile-time", .{});
Expand Down
9 changes: 5 additions & 4 deletions src/arch/aarch64/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -806,10 +806,11 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
.dbg_arg_inline,
=> try self.airDbgVar(inst),

.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call_never_intrinsify => try self.airCall(inst, .never_intrinsify),

.atomic_store_unordered => try self.airAtomicStore(inst, .unordered),
.atomic_store_monotonic => try self.airAtomicStore(inst, .monotonic),
Expand Down
9 changes: 5 additions & 4 deletions src/arch/arm/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -793,10 +793,11 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
.dbg_arg_inline,
=> try self.airDbgVar(inst),

.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call_never_intrinsify => try self.airCall(inst, .never_intrinsify),

.atomic_store_unordered => try self.airAtomicStore(inst, .unordered),
.atomic_store_monotonic => try self.airAtomicStore(inst, .monotonic),
Expand Down
9 changes: 5 additions & 4 deletions src/arch/riscv64/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1658,10 +1658,11 @@ fn genBody(func: *Func, body: []const Air.Inst.Index) InnerError!void {

.dbg_inline_block => try func.airDbgInlineBlock(inst),

.call => try func.airCall(inst, .auto),
.call_always_tail => try func.airCall(inst, .always_tail),
.call_never_tail => try func.airCall(inst, .never_tail),
.call_never_inline => try func.airCall(inst, .never_inline),
.call => try func.airCall(inst, .auto),
.call_always_tail => try func.airCall(inst, .always_tail),
.call_never_tail => try func.airCall(inst, .never_tail),
.call_never_inline => try func.airCall(inst, .never_inline),
.call_never_intrinsify => try func.airCall(inst, .never_intrinsify),

.atomic_store_unordered => try func.airAtomicStore(inst, .unordered),
.atomic_store_monotonic => try func.airAtomicStore(inst, .monotonic),
Expand Down
9 changes: 5 additions & 4 deletions src/arch/sparc64/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,11 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
.dbg_arg_inline,
=> try self.airDbgVar(inst),

.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call_never_intrinsify => try self.airCall(inst, .never_intrinsify),

.atomic_store_unordered => @panic("TODO try self.airAtomicStore(inst, .unordered)"),
.atomic_store_monotonic => @panic("TODO try self.airAtomicStore(inst, .monotonic)"),
Expand Down
1 change: 1 addition & 0 deletions src/arch/wasm/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1933,6 +1933,7 @@ fn genInst(func: *CodeGen, inst: Air.Inst.Index) InnerError!void {
.call_always_tail => func.airCall(inst, .always_tail),
.call_never_tail => func.airCall(inst, .never_tail),
.call_never_inline => func.airCall(inst, .never_inline),
.call_never_intrinsify => func.airCall(inst, .never_intrinsify),

.is_err => func.airIsErr(inst, .i32_ne),
.is_non_err => func.airIsErr(inst, .i32_eq),
Expand Down
9 changes: 5 additions & 4 deletions src/arch/x86_64/CodeGen.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2432,10 +2432,11 @@ fn genBody(self: *Self, body: []const Air.Inst.Index) InnerError!void {
.dbg_arg_inline,
=> try self.airDbgVar(inst),

.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call_never_intrinsify => try self.airCall(inst, .never_intrinsify),

.atomic_store_unordered => try self.airAtomicStore(inst, .unordered),
.atomic_store_monotonic => try self.airAtomicStore(inst, .monotonic),
Expand Down
12 changes: 7 additions & 5 deletions src/codegen/c.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3407,10 +3407,11 @@ fn genBodyInner(f: *Function, body: []const Air.Inst.Index) error{ AnalysisFail,
// The instruction has type `noreturn`, but there are instructions (and maybe a safety
// check) following nonetheless. The `unreachable` or safety check should be emitted by
// backends instead.
.call => try airCall(f, inst, .auto),
.call_always_tail => .none,
.call_never_tail => try airCall(f, inst, .never_tail),
.call_never_inline => try airCall(f, inst, .never_inline),
.call => try airCall(f, inst, .auto),
.call_always_tail => .none,
.call_never_tail => try airCall(f, inst, .never_tail),
.call_never_inline => try airCall(f, inst, .never_inline),
.call_never_intrinsify => try airCall(f, inst, .never_intrinsify),

// zig fmt: on
};
Expand Down Expand Up @@ -4540,7 +4541,7 @@ fn airCall(
else => break :known,
};
switch (modifier) {
.auto, .always_tail => try f.object.dg.renderNavName(writer, fn_nav),
.auto, .always_tail, .never_intrinsify => try f.object.dg.renderNavName(writer, fn_nav),
inline .never_tail, .never_inline => |m| try writer.writeAll(try f.getLazyFnName(@unionInit(LazyFnKey, @tagName(m), fn_nav))),
else => unreachable,
}
Expand All @@ -4550,6 +4551,7 @@ fn airCall(
.auto, .always_tail => {},
.never_tail => return f.fail("CBE: runtime callee with never_tail attribute unsupported", .{}),
.never_inline => return f.fail("CBE: runtime callee with never_inline attribute unsupported", .{}),
.never_intrinsify => return f.fail("CBE: runtime callee with never_intrinsify attribute unsupported", .{}),
else => unreachable,
}
// Fall back to function pointer call.
Expand Down
16 changes: 9 additions & 7 deletions src/codegen/llvm.zig
Original file line number Diff line number Diff line change
Expand Up @@ -5435,13 +5435,14 @@ pub const FuncGen = struct {
if (self.typeOfIndex(inst).isNoReturn(zcu)) return;
break :res res;
},
.call, .call_always_tail, .call_never_tail, .call_never_inline => |tag| res: {
.call, .call_always_tail, .call_never_tail, .call_never_inline, .call_never_intrinsify => |tag| res: {
const res = try self.airCall(inst, switch (tag) {
.call => .auto,
.call_always_tail => .always_tail,
.call_never_tail => .never_tail,
.call_never_inline => .never_inline,
else => unreachable,
.call => .auto,
.call_always_tail => .always_tail,
.call_never_tail => .never_tail,
.call_never_inline => .never_inline,
.call_never_intrinsify => .never_intrinsify,
else => unreachable,
});
// TODO: the AIR we emit for calls is a bit weird - the instruction has
// type `noreturn`, but there are instructions (and maybe a safety check) following
Expand Down Expand Up @@ -5585,6 +5586,7 @@ pub const FuncGen = struct {
switch (modifier) {
.auto, .never_tail, .always_tail => {},
.never_inline => try attributes.addFnAttr(.@"noinline", &o.builder),
.never_intrinsify => try attributes.addFnAttr(.nobuiltin, &o.builder),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that the handling of never_intrinsify will need to be combined with the code I added here: https://github.com/ziglang/zig/pull/21920/files#diff-4b7d5a9d78d51e4f41a41d80317e1ca360c16103d76b09c3dcb73ab3e4cc2d71R5579-R5581

.async_kw, .no_async, .always_inline, .compile_time => unreachable,
}

Expand Down Expand Up @@ -5794,7 +5796,7 @@ pub const FuncGen = struct {

const call = try self.wip.call(
switch (modifier) {
.auto, .never_inline => .normal,
.auto, .never_inline, .never_intrinsify => .normal,
.never_tail => .notail,
.always_tail => .musttail,
.async_kw, .no_async, .always_inline, .compile_time => unreachable,
Expand Down
9 changes: 5 additions & 4 deletions src/codegen/spirv.zig
Original file line number Diff line number Diff line change
Expand Up @@ -3518,10 +3518,11 @@ const NavGen = struct {

.assembly => try self.airAssembly(inst),

.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call => try self.airCall(inst, .auto),
.call_always_tail => try self.airCall(inst, .always_tail),
.call_never_tail => try self.airCall(inst, .never_tail),
.call_never_inline => try self.airCall(inst, .never_inline),
.call_never_intrinsify => try self.airCall(inst, .never_intrinsify),

.work_item_id => try self.airWorkItemId(inst),
.work_group_size => try self.airWorkGroupSize(inst),
Expand Down
1 change: 1 addition & 0 deletions src/print_air.zig
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ const Writer = struct {
.call_always_tail,
.call_never_tail,
.call_never_inline,
.call_never_intrinsify,
=> try w.writeCall(s, inst),

.dbg_var_ptr,
Expand Down
Loading