From 1f154f0893c6ad5fadbe76d85bfd921a5446e13c Mon Sep 17 00:00:00 2001 From: rpkak Date: Mon, 5 Oct 2026 09:13:33 +0200 Subject: [PATCH 1/5] Sema: emit shr instead of shr_exact when @shrExact is used and wantSafety is true The safety check of @shrExact is to shift the result the same amount back to the left and compare if this matches the input of the @shrExact. But since the 'lshr exact' llvm instruction was used, llvm was allowed to assume, that the result of the left-shift matches the input. --- src/Sema.zig | 39 ++++++++++++++++++--------------------- 1 file changed, 18 insertions(+), 21 deletions(-) diff --git a/src/Sema.zig b/src/Sema.zig index 7991ce342b33cd0cfac74aa2f6b99847b742425c..bf12c2b5840e4537f6328f3b14de51ebe062430a 100644 --- a/src/Sema.zig +++ b/src/Sema.zig @@ -1231,8 +1231,8 @@ fn analyzeBodyInner( .ref => try sema.zirRef(block, inst), .deref => try sema.zirDeref(block, inst), .ref_deref => try sema.zirRefDeref(block, inst), - .shr => try sema.zirShr(block, inst, .shr), - .shr_exact => try sema.zirShr(block, inst, .shr_exact), + .shr => try sema.zirShr(block, inst, false), + .shr_exact => try sema.zirShr(block, inst, true), .slice_end => try sema.zirSliceEnd(block, inst), .slice_sentinel => try sema.zirSliceSentinel(block, inst), .slice_start => try sema.zirSliceStart(block, inst), @@ -13176,7 +13176,7 @@ fn zirShr( sema: *Sema, block: *Block, inst: Zir.Inst.Index, - air_tag: Air.Inst.Tag, + exact: bool, ) CompileError!Air.Inst.Ref { const pt = sema.pt; const zcu = pt.zcu; @@ -13188,16 +13188,15 @@ fn zirShr( const rhs_ty = sema.typeOf(rhs); const src = block.nodeOffset(inst_data.src_node); - const lhs_src = switch (air_tag) { - .shr => block.src(.{ .node_offset_bin_lhs = inst_data.src_node }), - .shr_exact => block.builtinCallArgSrc(inst_data.src_node, 0), - else => unreachable, - }; - const rhs_src = switch (air_tag) { - .shr => block.src(.{ .node_offset_bin_rhs = inst_data.src_node }), - .shr_exact => block.builtinCallArgSrc(inst_data.src_node, 1), - else => unreachable, - }; + const lhs_src = if (exact) + block.builtinCallArgSrc(inst_data.src_node, 0) + else + block.src(.{ .node_offset_bin_lhs = inst_data.src_node }); + + const rhs_src = if (exact) + block.builtinCallArgSrc(inst_data.src_node, 1) + else + block.src(.{ .node_offset_bin_rhs = inst_data.src_node }); try sema.checkVectorizableBinaryOperands(block, src, lhs_ty, rhs_ty, lhs_src, rhs_src); const scalar_ty = lhs_ty.scalarType(zcu); @@ -13208,11 +13207,7 @@ fn zirShr( const runtime_src = rs: { if (maybe_rhs_val) |rhs_val| { if (maybe_lhs_val) |lhs_val| { - return .fromValue(try arith.shr(sema, block, lhs_ty, rhs_ty, lhs_val, rhs_val, src, lhs_src, rhs_src, switch (air_tag) { - .shr => .shr, - .shr_exact => .shr_exact, - else => unreachable, - })); + return .fromValue(try arith.shr(sema, block, lhs_ty, rhs_ty, lhs_val, rhs_val, src, lhs_src, rhs_src, if (exact) .shr_exact else .shr)); } if (rhs_val.isUndef(zcu)) { return sema.failWithUseOfUndef(block, rhs_src, null); @@ -13269,8 +13264,10 @@ fn zirShr( break :rs rhs_src; }; try sema.requireRuntimeBlock(block, src, runtime_src); - const result = try block.addBinOp(air_tag, lhs, rhs); - if (block.wantSafety()) { + + const safety = block.wantSafety(); + const result = try block.addBinOp(if (!exact or safety) .shr else .shr_exact, lhs, rhs); + if (safety) { const bit_count = scalar_ty.intInfo(zcu).bits; if (!std.math.isPowerOfTwo(bit_count)) { const bit_count_val = try pt.intValue(rhs_ty.scalarType(zcu), bit_count); @@ -13286,7 +13283,7 @@ fn zirShr( try sema.addSafetyCheck(block, src, ok, .shift_rhs_too_big); } - if (air_tag == .shr_exact) { + if (exact) { const back = try block.addBinOp(.shl, result, rhs); const ok = if (rhs_ty.zigTypeTag(zcu) == .vector) ok: { -- 2.54.0 From dc76b08e2a409ad9c41bdbf3574d30b6d3b6e512 Mon Sep 17 00:00:00 2001 From: rpkak Date: Mon, 5 Oct 2026 09:29:03 +0200 Subject: [PATCH 2/5] Revert "langref: work around powerpc LLVM miscompilation in `runtime_shrExact_overflow`" This reverts commit 9373a963a1aec1341b62cfe2170f824de620f41a. --- doc/langref/runtime_shrExact_overflow.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/langref/runtime_shrExact_overflow.zig b/doc/langref/runtime_shrExact_overflow.zig index afb9c4b9c1aec75cfbbc49110abe702806d2c449..32cfc33ca2decbe246369effda9308b029d99d07 100644 --- a/doc/langref/runtime_shrExact_overflow.zig +++ b/doc/langref/runtime_shrExact_overflow.zig @@ -7,7 +7,7 @@ pub fn main() void { const y = @shrExact(x, 2); std.debug.print("value: {}\n", .{y}); - if ((builtin.cpu.arch.isPowerPC() or builtin.cpu.arch.isRISCV() or builtin.cpu.arch.isLoongArch() or builtin.cpu.arch == .s390x) and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); + if ((builtin.cpu.arch.isRISCV() or builtin.cpu.arch.isLoongArch() or builtin.cpu.arch == .s390x) and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); } // exe=fail -- 2.54.0 From 2af80a25e0082951cfba60e99924b0cdc8224532 Mon Sep 17 00:00:00 2001 From: rpkak Date: Mon, 5 Oct 2026 09:29:03 +0200 Subject: [PATCH 3/5] Revert "langref: work around s390x LLVM miscompilation in `runtime_shrExact_overflow`" This reverts commit 4dd05b5a64549149ba1f3e5ecb5a1ad14ac1564c. --- doc/langref/runtime_shrExact_overflow.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/langref/runtime_shrExact_overflow.zig b/doc/langref/runtime_shrExact_overflow.zig index 32cfc33ca2decbe246369effda9308b029d99d07..98da0dde1191d434baa623c998bbeced7a7cb518 100644 --- a/doc/langref/runtime_shrExact_overflow.zig +++ b/doc/langref/runtime_shrExact_overflow.zig @@ -7,7 +7,7 @@ pub fn main() void { const y = @shrExact(x, 2); std.debug.print("value: {}\n", .{y}); - if ((builtin.cpu.arch.isRISCV() or builtin.cpu.arch.isLoongArch() or builtin.cpu.arch == .s390x) and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); + if ((builtin.cpu.arch.isRISCV() or builtin.cpu.arch.isLoongArch()) and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); } // exe=fail -- 2.54.0 From 7c3e35d9eab57e06a3ead61e22621996cf9bee03 Mon Sep 17 00:00:00 2001 From: rpkak Date: Mon, 5 Oct 2026 09:29:03 +0200 Subject: [PATCH 4/5] Revert "langref: runtime_shrExact_overflow does not work as expected on LoongArch" This reverts commit c547a05b656118bef790478c26b439e07c858c76. --- doc/langref/runtime_shrExact_overflow.zig | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/doc/langref/runtime_shrExact_overflow.zig b/doc/langref/runtime_shrExact_overflow.zig index 98da0dde1191d434baa623c998bbeced7a7cb518..1350e9da75d4e1dc0035066679288f8eb14c7127 100644 --- a/doc/langref/runtime_shrExact_overflow.zig +++ b/doc/langref/runtime_shrExact_overflow.zig @@ -7,7 +7,7 @@ pub fn main() void { const y = @shrExact(x, 2); std.debug.print("value: {}\n", .{y}); - if ((builtin.cpu.arch.isRISCV() or builtin.cpu.arch.isLoongArch()) and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); + if (builtin.cpu.arch.isRISCV() and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); } // exe=fail -- 2.54.0 From 8d47bbdbe032cb49aac829ba349669a63cc17943 Mon Sep 17 00:00:00 2001 From: rpkak Date: Mon, 5 Oct 2026 09:29:04 +0200 Subject: [PATCH 5/5] Revert "langref: runtime_shrExact_overflow does not work as expected on RISC-V" This reverts commit 9f064dff477a100b95438c7842253dfb20224553. --- doc/langref/runtime_shrExact_overflow.zig | 3 --- 1 file changed, 3 deletions(-) diff --git a/doc/langref/runtime_shrExact_overflow.zig b/doc/langref/runtime_shrExact_overflow.zig index 1350e9da75d4e1dc0035066679288f8eb14c7127..cc7d7908fb7cf9ac49c3459217a208e422765704 100644 --- a/doc/langref/runtime_shrExact_overflow.zig +++ b/doc/langref/runtime_shrExact_overflow.zig @@ -1,4 +1,3 @@ -const builtin = @import("builtin"); const std = @import("std"); pub fn main() void { @@ -6,8 +5,6 @@ pub fn main() void { _ = &x; const y = @shrExact(x, 2); std.debug.print("value: {}\n", .{y}); - - if (builtin.cpu.arch.isRISCV() and builtin.zig_backend == .stage2_llvm) @panic("https://github.com/ziglang/zig/issues/24304"); } // exe=fail -- 2.54.0