diff --git a/src/Zcu/PerThread.zig b/src/Zcu/PerThread.zig index 8c789491b5b3121c3ddebd99451ff0f169d5b157..0346ca21f1310a23ffab3c03ccdfc4585135b2f4 100644 --- a/src/Zcu/PerThread.zig +++ b/src/Zcu/PerThread.zig @@ -1866,9 +1866,13 @@ fn analyzeNavVal( }; if (is_owned_fn) { - // linksection etc are legal, except some targets do not support function alignment. - if (zir_decl.align_body != null and !target_util.supportsFunctionAlignment(zcu.getTarget())) { - return sema.fail(&block, align_src, "target does not support function alignment", .{}); + // linksection etc are legal, except some targets do not support function alignment + // greater than 1. `align(1)` is a no-op, so it is allowed even on those targets. + if (zir_decl.align_body != null and + modifiers.@"align" != .@"1" and + !target_util.supportsFunctionAlignment(zcu.getTarget())) + { + return sema.fail(&block, align_src, "target does not support function alignment greater than 1", .{}); } } else if (nav_ty.comptimeOnly(zcu)) { // alignment, linksection, addrspace annotations are not allowed for comptime-only types. diff --git a/test/behavior/align.zig b/test/behavior/align.zig index 83a100533d77a81a0a77ef38def5e61cb2a29f79..3ef1249e78a49c1abf71d2a42e9c4d130acf4ef3 100644 --- a/test/behavior/align.zig +++ b/test/behavior/align.zig @@ -312,7 +312,7 @@ test "function alignment" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - // function alignment is a compile error on wasm + // function alignment greater than 1 is a compile error on wasm if (native_arch.isWasm()) return error.SkipZigTest; const S = struct { @@ -341,7 +341,7 @@ test "implicitly decreasing fn alignment" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - // function alignment is a compile error on wasm + // function alignment greater than 1 is a compile error on wasm if (native_arch.isWasm()) return error.SkipZigTest; try testImplicitlyDecreaseFnAlign(alignedSmall, 1234); @@ -364,7 +364,7 @@ test "@alignCast functions" { if (builtin.zig_backend == .stage2_sparc64) return error.SkipZigTest; // TODO if (builtin.zig_backend == .stage2_spirv) return error.SkipZigTest; - // function alignment is a compile error on wasm + // function alignment greater than 1 is a compile error on wasm if (native_arch.isWasm()) return error.SkipZigTest; try expect(fnExpectsOnly1(simple4) == 0x19); @@ -510,7 +510,7 @@ test "align(N) on functions" { return error.SkipZigTest; } - // function alignment is a compile error on wasm + // function alignment greater than 1 is a compile error on wasm if (native_arch.isWasm()) return error.SkipZigTest; if (native_arch.isThumb()) return error.SkipZigTest; diff --git a/test/cases/compile_errors/align_n_expr_function_pointers_is_a_compile_error.zig b/test/cases/compile_errors/align_n_expr_function_pointers_is_a_compile_error.zig index 2f9907d4892ce0a9e75397b12a60895ba12c2954..d94ed6a83b144ddd295714491843252acf30c03e 100644 --- a/test/cases/compile_errors/align_n_expr_function_pointers_is_a_compile_error.zig +++ b/test/cases/compile_errors/align_n_expr_function_pointers_is_a_compile_error.zig @@ -1,8 +1,8 @@ -export fn foo() align(1) void { +export fn foo() align(2) void { return; } // error // target=wasm32-freestanding-none // -// :1:23: error: target does not support function alignment +// :1:23: error: target does not support function alignment greater than 1 diff --git a/test/cases/compile_errors/function_alignment_on_unsupported_target.zig b/test/cases/compile_errors/function_alignment_on_unsupported_target.zig index 6b70dc94e498c535e21abf510558657260b82120..8c9d4482e47a912b6e23750446165c70843c1ced 100644 --- a/test/cases/compile_errors/function_alignment_on_unsupported_target.zig +++ b/test/cases/compile_errors/function_alignment_on_unsupported_target.zig @@ -3,4 +3,4 @@ export fn entry() align(64) void {} // error // target=nvptx-cuda,nvptx64-cuda,spirv32-opengl,spirv32-vulkan,spirv32-opencl,spirv64-opencl,spirv64-amdhsa,wasm32-freestanding,wasm64-freestanding // -// :1:25: error: target does not support function alignment +// :1:25: error: target does not support function alignment greater than 1 diff --git a/test/cases/function_alignment_one_on_virtual_target.zig b/test/cases/function_alignment_one_on_virtual_target.zig new file mode 100644 index 0000000000000000000000000000000000000000..60cbd6ddaabddf0003f2cf9d242ef750c2470b0d --- /dev/null +++ b/test/cases/function_alignment_one_on_virtual_target.zig @@ -0,0 +1,5 @@ +export fn foo() align(1) void {} + +// compile +// backend=selfhosted +// target=wasm32-freestanding