Commit ef72b91ac2

Alex Rønne Petersen <alex@alexrp.com>
2024-10-15 21:28:42
compiler: Remove uses of defaultFunctionAlignment() in the frontend.
minFunctionAlignment() is something we can know ahead of time for any given target because it's a matter of ABI. However, defaultFunctionAlignment() is a matter of optimization and every backend can do it differently depending on any number of factors. For example, LLVM will base the choice on the CPU model in its aarch64 backend. So just don't use this value in the frontend.
1 parent d2f04e9
Changed files (3)
src/Sema.zig
@@ -26572,9 +26572,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
         if (val.isGenericPoison()) {
             break :blk null;
         }
-        const alignment = try sema.validateAlignAllowZero(block, align_src, try val.toUnsignedIntSema(pt));
-        const default = target_util.defaultFunctionAlignment(target);
-        break :blk if (alignment == default) .none else alignment;
+        break :blk try sema.validateAlignAllowZero(block, align_src, try val.toUnsignedIntSema(pt));
     } else if (extra.data.bits.has_align_ref) blk: {
         const align_ref: Zir.Inst.Ref = @enumFromInt(sema.code.extra[extra_index]);
         extra_index += 1;
@@ -26592,9 +26590,7 @@ fn zirFuncFancy(sema: *Sema, block: *Block, inst: Zir.Inst.Index) CompileError!A
             error.GenericPoison => break :blk null,
             else => |e| return e,
         };
-        const alignment = try sema.validateAlignAllowZero(block, align_src, try align_val.toUnsignedIntSema(pt));
-        const default = target_util.defaultFunctionAlignment(target);
-        break :blk if (alignment == default) .none else alignment;
+        break :blk try sema.validateAlignAllowZero(block, align_src, try align_val.toUnsignedIntSema(pt));
     } else .none;
 
     const @"addrspace": ?std.builtin.AddressSpace = if (extra.data.bits.has_addrspace_body) blk: {
src/target.zig
@@ -459,7 +459,8 @@ pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
     }
 }
 
-/// This function returns 1 if function alignment is not observable or settable.
+/// This function returns 1 if function alignment is not observable or settable. Note that this
+/// value will not necessarily match the backend's default function alignment (e.g. for LLVM).
 pub fn defaultFunctionAlignment(target: std.Target) Alignment {
     // Overrides of the minimum for performance.
     return switch (target.cpu.arch) {
src/Type.zig
@@ -1020,7 +1020,7 @@ pub fn abiAlignmentInner(
             },
 
             // represents machine code; not a pointer
-            .func_type => return .{ .scalar = target_util.defaultFunctionAlignment(target) },
+            .func_type => return .{ .scalar = target_util.minFunctionAlignment(target) },
 
             .simple_type => |t| switch (t) {
                 .bool,