Skip to content

Commit b3aa283

Browse files
committed
compiler: remove @setAlignStack
This commit finishes implementing ziglang#21209 by removing the `@setAlignStack` builtin in favour of `CallingConvention` payloads. The x86_64 backend is updated to use the stack alignment given in the calling convention (the LLVM backend was already updated in a previous commit). Resolves: ziglang#21209
1 parent 57725d0 commit b3aa283

File tree

11 files changed

+21
-82
lines changed

11 files changed

+21
-82
lines changed

lib/std/zig/AstGen.zig

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2902,7 +2902,6 @@ fn addEnsureResult(gz: *GenZir, maybe_unused_result: Zir.Inst.Ref, statement: As
29022902
.breakpoint,
29032903
.disable_instrumentation,
29042904
.set_float_mode,
2905-
.set_align_stack,
29062905
.branch_hint,
29072906
=> break :b true,
29082907
else => break :b false,
@@ -9315,14 +9314,6 @@ fn builtinCall(
93159314
});
93169315
return rvalue(gz, ri, .void_value, node);
93179316
},
9318-
.set_align_stack => {
9319-
const order = try expr(gz, scope, coerced_align_ri, params[0]);
9320-
_ = try gz.addExtendedPayload(.set_align_stack, Zir.Inst.UnNode{
9321-
.node = gz.nodeIndexToRelative(node),
9322-
.operand = order,
9323-
});
9324-
return rvalue(gz, ri, .void_value, node);
9325-
},
93269317

93279318
.src => {
93289319
// Incorporate the source location into the source hash, so that

lib/std/zig/AstRlAnnotate.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -909,7 +909,6 @@ fn builtinCall(astrl: *AstRlAnnotate, block: ?*Block, ri: ResultInfo, node: Ast.
909909
.wasm_memory_size,
910910
.splat,
911911
.set_float_mode,
912-
.set_align_stack,
913912
.type_info,
914913
.work_item_id,
915914
.work_group_size,

lib/std/zig/BuiltinFn.zig

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,6 @@ pub const Tag = enum {
8181
rem,
8282
return_address,
8383
select,
84-
set_align_stack,
8584
set_eval_branch_quota,
8685
set_float_mode,
8786
set_runtime_safety,
@@ -736,14 +735,6 @@ pub const list = list: {
736735
.param_count = 4,
737736
},
738737
},
739-
.{
740-
"@setAlignStack",
741-
.{
742-
.tag = .set_align_stack,
743-
.param_count = 1,
744-
.illegal_outside_function = true,
745-
},
746-
},
747738
.{
748739
"@setEvalBranchQuota",
749740
.{

lib/std/zig/Zir.zig

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1982,9 +1982,6 @@ pub const Inst = struct {
19821982
/// Implement builtin `@setFloatMode`.
19831983
/// `operand` is payload index to `UnNode`.
19841984
set_float_mode,
1985-
/// Implement builtin `@setAlignStack`.
1986-
/// `operand` is payload index to `UnNode`.
1987-
set_align_stack,
19881985
/// Implements the `@errorCast` builtin.
19891986
/// `operand` is payload index to `BinNode`. `lhs` is dest type, `rhs` is operand.
19901987
error_cast,
@@ -4012,7 +4009,6 @@ fn findDeclsInner(
40124009
.wasm_memory_grow,
40134010
.prefetch,
40144011
.set_float_mode,
4015-
.set_align_stack,
40164012
.error_cast,
40174013
.await_nosuspend,
40184014
.breakpoint,

src/InternPool.zig

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5607,12 +5607,11 @@ pub const FuncAnalysis = packed struct(u32) {
56075607
branch_hint: std.builtin.BranchHint,
56085608
is_noinline: bool,
56095609
calls_or_awaits_errorable_fn: bool,
5610-
stack_alignment: Alignment,
56115610
/// True if this function has an inferred error set.
56125611
inferred_error_set: bool,
56135612
disable_instrumentation: bool,
56145613

5615-
_: u17 = 0,
5614+
_: u23 = 0,
56165615

56175616
pub const State = enum(u2) {
56185617
/// The runtime function has never been referenced.
@@ -8684,7 +8683,6 @@ pub fn getFuncDecl(
86848683
.branch_hint = .none,
86858684
.is_noinline = key.is_noinline,
86868685
.calls_or_awaits_errorable_fn = false,
8687-
.stack_alignment = .none,
86888686
.inferred_error_set = false,
86898687
.disable_instrumentation = false,
86908688
},
@@ -8788,7 +8786,6 @@ pub fn getFuncDeclIes(
87888786
.branch_hint = .none,
87898787
.is_noinline = key.is_noinline,
87908788
.calls_or_awaits_errorable_fn = false,
8791-
.stack_alignment = .none,
87928789
.inferred_error_set = true,
87938790
.disable_instrumentation = false,
87948791
},
@@ -8980,7 +8977,6 @@ pub fn getFuncInstance(
89808977
.branch_hint = .none,
89818978
.is_noinline = arg.is_noinline,
89828979
.calls_or_awaits_errorable_fn = false,
8983-
.stack_alignment = .none,
89848980
.inferred_error_set = false,
89858981
.disable_instrumentation = false,
89868982
},
@@ -9080,7 +9076,6 @@ pub fn getFuncInstanceIes(
90809076
.branch_hint = .none,
90819077
.is_noinline = arg.is_noinline,
90829078
.calls_or_awaits_errorable_fn = false,
9083-
.stack_alignment = .none,
90849079
.inferred_error_set = true,
90859080
.disable_instrumentation = false,
90869081
},
@@ -11859,21 +11854,6 @@ pub fn funcAnalysisUnordered(ip: *const InternPool, func: Index) FuncAnalysis {
1185911854
return @atomicLoad(FuncAnalysis, @constCast(ip).funcAnalysisPtr(func), .unordered);
1186011855
}
1186111856

11862-
pub fn funcMaxStackAlignment(ip: *InternPool, func: Index, new_stack_alignment: Alignment) void {
11863-
const unwrapped_func = func.unwrap(ip);
11864-
const extra_mutex = &ip.getLocal(unwrapped_func.tid).mutate.extra.mutex;
11865-
extra_mutex.lock();
11866-
defer extra_mutex.unlock();
11867-
11868-
const analysis_ptr = ip.funcAnalysisPtr(func);
11869-
var analysis = analysis_ptr.*;
11870-
analysis.stack_alignment = switch (analysis.stack_alignment) {
11871-
.none => new_stack_alignment,
11872-
else => |old_stack_alignment| old_stack_alignment.maxStrict(new_stack_alignment),
11873-
};
11874-
@atomicStore(FuncAnalysis, analysis_ptr, analysis, .release);
11875-
}
11876-
1187711857
pub fn funcSetCallsOrAwaitsErrorableFn(ip: *InternPool, func: Index) void {
1187811858
const unwrapped_func = func.unwrap(ip);
1187911859
const extra_mutex = &ip.getLocal(unwrapped_func.tid).mutate.extra.mutex;

src/Sema.zig

Lines changed: 0 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1326,11 +1326,6 @@ fn analyzeBodyInner(
13261326
i += 1;
13271327
continue;
13281328
},
1329-
.set_align_stack => {
1330-
try sema.zirSetAlignStack(block, extended);
1331-
i += 1;
1332-
continue;
1333-
},
13341329
.breakpoint => {
13351330
if (!block.is_comptime) {
13361331
_ = try block.addNoOp(.breakpoint);
@@ -6513,35 +6508,6 @@ pub fn analyzeExport(
65136508
});
65146509
}
65156510

6516-
fn zirSetAlignStack(sema: *Sema, block: *Block, extended: Zir.Inst.Extended.InstData) CompileError!void {
6517-
const pt = sema.pt;
6518-
const zcu = pt.zcu;
6519-
const extra = sema.code.extraData(Zir.Inst.UnNode, extended.operand).data;
6520-
const operand_src = block.builtinCallArgSrc(extra.node, 0);
6521-
const src = block.nodeOffset(extra.node);
6522-
const alignment = try sema.resolveAlign(block, operand_src, extra.operand);
6523-
6524-
const func = switch (sema.owner.unwrap()) {
6525-
.func => |func| func,
6526-
.cau => return sema.fail(block, src, "@setAlignStack outside of function scope", .{}),
6527-
};
6528-
6529-
if (alignment.order(Alignment.fromNonzeroByteUnits(256)).compare(.gt)) {
6530-
return sema.fail(block, src, "attempt to @setAlignStack({d}); maximum is 256", .{
6531-
alignment.toByteUnits().?,
6532-
});
6533-
}
6534-
6535-
switch (Value.fromInterned(func).typeOf(zcu).fnCallingConvention(zcu)) {
6536-
.naked => return sema.fail(block, src, "@setAlignStack in naked function", .{}),
6537-
.@"inline" => return sema.fail(block, src, "@setAlignStack in inline function", .{}),
6538-
else => {},
6539-
}
6540-
6541-
zcu.intern_pool.funcMaxStackAlignment(sema.func_index, alignment);
6542-
sema.allow_memoize = false;
6543-
}
6544-
65456511
fn zirDisableInstrumentation(sema: *Sema) CompileError!void {
65466512
const pt = sema.pt;
65476513
const zcu = pt.zcu;

src/Zcu.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3560,7 +3560,7 @@ pub fn callconvSupported(zcu: *Zcu, cc: std.builtin.CallingConvention) union(enu
35603560
else => false,
35613561
},
35623562
.stage2_x86_64 => switch (cc) {
3563-
.x86_64_sysv, .x86_64_win, .naked => true,
3563+
.x86_64_sysv, .x86_64_win, .naked => true, // stack alignment supported
35643564
else => false,
35653565
},
35663566
.stage2_aarch64 => switch (cc) {

src/arch/wasm/CodeGen.zig

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,7 +710,7 @@ stack_size: u32 = 0,
710710
/// The stack alignment, which is 16 bytes by default. This is specified by the
711711
/// tool-conventions: https://github.com/WebAssembly/tool-conventions/blob/main/BasicCABI.md
712712
/// and also what the llvm backend will emit.
713-
/// However, local variables or the usage of `@setAlignStack` can overwrite this default.
713+
/// However, local variables or the usage of `incoming_stack_alignment` in a `CallingConvention` can overwrite this default.
714714
stack_alignment: Alignment = .@"16",
715715

716716
// For each individual Wasm valtype we store a seperate free list which

src/arch/x86_64/CodeGen.zig

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ const verbose_tracking_log = std.log.scoped(.verbose_tracking);
1111
const wip_mir_log = std.log.scoped(.wip_mir);
1212
const math = std.math;
1313
const mem = std.mem;
14+
const target_util = @import("../../target.zig");
1415
const trace = @import("../../tracy.zig").trace;
1516

1617
const Air = @import("../../Air.zig");
@@ -872,7 +873,7 @@ pub fn generate(
872873
@intFromEnum(FrameIndex.stack_frame),
873874
FrameAlloc.init(.{
874875
.size = 0,
875-
.alignment = func.analysisUnordered(ip).stack_alignment.max(.@"1"),
876+
.alignment = target_util.stackAlignment(function.target.*, fn_type.fnCallingConvention(zcu)),
876877
}),
877878
);
878879
function.frame_allocs.set(

src/print_zir.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -567,7 +567,6 @@ const Writer = struct {
567567
.c_undef,
568568
.c_include,
569569
.set_float_mode,
570-
.set_align_stack,
571570
.wasm_memory_size,
572571
.int_from_error,
573572
.error_from_int,

src/target.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -610,3 +610,19 @@ pub inline fn backendSupportsFeature(backend: std.builtin.CompilerBackend, compt
610610
},
611611
};
612612
}
613+
614+
pub fn stackAlignment(target: std.Target, cc: std.builtin.CallingConvention) u64 {
615+
switch (cc) {
616+
inline else => |payload| switch (@TypeOf(payload)) {
617+
std.builtin.CallingConvention.CommonOptions,
618+
std.builtin.CallingConvention.X86RegparmOptions,
619+
std.builtin.CallingConvention.ArmInterruptOptions,
620+
std.builtin.CallingConvention.MipsInterruptOptions,
621+
std.builtin.CallingConvention.RiscvInterruptOptions,
622+
=> if (payload.incoming_stack_alignment) |a| return a,
623+
void => {},
624+
else => comptime unreachable,
625+
},
626+
}
627+
return target.stackAlignment();
628+
}

0 commit comments

Comments
 (0)