| Issue |
240372
|
| Summary |
[SLPVectorizer] LLVM 20 wrong-code in reduced QM31 two-row fold for znver4
|
| Labels |
new issue
|
| Assignees |
|
| Reporter |
teddyjfpender
|
Zig 0.15.2 / bundled LLVM 20 produces a wrong optimized-IR result for a reduced two-row extension-field fold when targeting `x86_64-linux-gnu -mcpu=znver4`. The pass controls point to SLP vectorization. We retain a `noinline` boundary at complex-field multiplication as the production workaround.
## Reproduction and evidence limits
The standalone source below is 147 lines, SHA256 `615f72e52f9d93a1c803a95165a258203ace7cefd984003654aa84be4377dde2`.
```sh
zig version # 0.15.2
zig build-obj repro.zig -OReleaseFast -target x86_64-linux-gnu -mcpu=znver4 \
-fno-emit-bin -femit-llvm-ir=repro.ll
```
The affected optimized IR, after removing target-specific function attributes and retargeting for execution on macOS/arm64 without rerunning optimization, yields the wrong first row below. This is optimized-IR evidence, **not a claim that the isolated AVX512 machine code has been executed**. Native isolated AVX512 execution is still unavailable. The full production fold failed on the AVX512 hosted allocation before reduction; native isolated AVX2 tests pass in Debug, ReleaseSafe and ReleaseFast on two EPYC 7763 allocations.
| First output row | limbs |
| --- | --- |
| independent integer field oracle | `1502722116, 2034726799, 546122179, 519690040` |
| affected optimized IR | `1502722116, 2034726799, 546122187, 519690016` |
For a capable AVX512 host, the intended native reproducer is `zig test repro.zig -OReleaseFast -mcpu=znver4`; this exact isolated native invocation is unverified here.
## Controls
Using the same unoptimized full-fold IR and LLVM `opt -passes='default<O3>'`:
- LLVM 20.1.8 default pipeline: wrong result.
- LLVM 20.1.8 `-vectorize-slp=false`: correct result.
- LLVM 20.1.8 `-disable-vector-combine`: wrong result.
- LLVM 22 default pipeline: correct result.
- The explicitly wrapping arithmetic variant still produces the wrong result.
- All inputs are canonical M31 values. The base-field `u64` product is at most `(2^31-2)^2`; sums and folded products remain within their integer types. The complex subtraction branches do not require implicit modulo-2^64 wrap.
- Exact pre-restructure main `4e260db9605d16fef5d405cc3c280e3042b22e35` and restructured source show the same full-owner failure, so this was not introduced by moving the code.
- Keeping `CM31.mul` behind a `noinline` boundary preserves the arithmetic and avoids the failure in the production path.
The original full-fold golden digest and proof checks remain unchanged. Downstream integration: [stwo-zig PR #323](https://github.com/teddyjfpender/stwo-zig/pull/323). The raw compiler/IR controls are retained in [the immutable evidence backup](https://github.com/teddyjfpender/stwo-zig/tree/bcb3c66d/backup-evidence) (LFS archive with hashes). The isolated source and reduced controls are also preserved in [the minimal-repro backup](https://github.com/teddyjfpender/stwo-zig/tree/3da377b3/backup-evidence).
## Standalone source
```zig
//! Standalone scalar case from the frozen first FRI multiplication.
//! Run with `zig test <this-file> -ODebug` or `-OReleaseFast`.
//! These scalar bodies preserve main 4e260db96's Karatsuba arithmetic.
//! A passing case does not establish that the full-context failure is reduced.
const std = @import("std");
const p: u32 = 2147483647;
const M31 = struct {
v: u32,
inline fn add(a: M31, b: M31) M31 {
var s: u32 = a.v + b.v;
if (s >= p) s -= p;
return .{ .v = s };
}
inline fn sub(a: M31, b: M31) M31 {
if (a.v >= b.v) return .{ .v = a.v - b.v };
return .{ .v = (a.v + p) - b.v };
}
inline fn mul(a: M31, b: M31) M31 {
const product: u64 = @as(u64, a.v) * @as(u64, b.v);
const folded = (product & @as(u64, p)) + (product >> 31);
const r: u32 = @intCast(folded);
return .{ .v = if (r >= p) r - p else r };
}
};
const CM31 = struct {
a: M31,
b: M31,
inline fn add(lhs: CM31, rhs: CM31) CM31 {
return .{ .a = lhs.a.add(rhs.a), .b = lhs.b.add(rhs.b) };
}
inline fn sub(lhs: CM31, rhs: CM31) CM31 {
return .{ .a = lhs.a.sub(rhs.a), .b = lhs.b.sub(rhs.b) };
}
inline fn square(self: CM31) CM31 {
const ab = self.a.mul(self.b);
return .{ .a = self.a.mul(self.a).sub(self.b.mul(self.b)), .b = ab.add(ab) };
}
inline fn mul(lhs: CM31, rhs: CM31) CM31 {
const ac = lhs.a.mul(rhs.a);
const bd = lhs.b.mul(rhs.b);
const lhs_sum = lhs.a.add(lhs.b);
const rhs_sum = rhs.a.add(rhs.b);
const cross = lhs_sum.mul(rhs_sum);
var imaginary = cross.sub(ac);
imaginary = imaginary.sub(bd);
return .{ .a = ac.sub(bd), .b = imaginary };
}
};
const QM31 = struct {
c0: CM31,
c1: CM31,
inline fn zero() QM31 {
return field(&.{ 0, 0, 0, 0 });
}
inline fn isBase(self: QM31) bool {
return self.c0.b.v == 0 and self.c1.a.v == 0 and self.c1.b.v == 0;
}
inline fn add(lhs: QM31, rhs: QM31) QM31 {
return .{ .c0 = lhs.c0.add(rhs.c0), .c1 = lhs.c1.add(rhs.c1) };
}
inline fn sub(lhs: QM31, rhs: QM31) QM31 {
return .{ .c0 = lhs.c0.sub(rhs.c0), .c1 = lhs.c1.sub(rhs.c1) };
}
inline fn mulM31(lhs: QM31, rhs: M31) QM31 {
// Exact generic four-lane arithmetic used on the failing x86 target.
const lanes: @Vector(4, u32) = .{ lhs.c0.a.v, lhs.c0.b.v, lhs.c1.a.v, lhs.c1.b.v };
const wide: @Vector(4, u64) = lanes;
const product = wide * @as(@Vector(4, u64), @splat(rhs.v));
const folded = (product & @as(@Vector(4, u64), @splat(p))) + (product >> @as(@Vector(4, u6), @splat(31)));
const reduced: @Vector(4, u32) = @truncate(folded);
const modulus: @Vector(4, u32) = @splat(p);
const value = @select(u32, reduced >= modulus, reduced -% modulus, reduced);
return field(&.{ value[0], value[1], value[2], value[3] });
}
inline fn square(self: QM31) QM31 {
if (self.isBase()) return field(&.{ self.c0.a.mul(self.c0.a).v, 0, 0, 0 });
const a2 = self.c0.square();
const b2 = self.c1.square();
const ab = self.c0.mul(self.c1);
const two_a = b2.a.add(b2.a);
const two_b = b2.b.add(b2.b);
const rbd = CM31{ .a = two_a.sub(b2.b), .b = b2.a.add(two_b) };
return .{ .c0 = a2.add(rbd), .c1 = ab.add(ab) };
}
inline fn mul(lhs: QM31, rhs: QM31) QM31 {
if (!@inComptime()) {
if (rhs.isBase()) return lhs.mulM31(rhs.c0.a);
if (lhs.isBase()) return rhs.mulM31(lhs.c0.a);
}
const ac = lhs.c0.mul(rhs.c0);
const bd = lhs.c1.mul(rhs.c1);
const lhs_sum = lhs.c0.add(lhs.c1);
const rhs_sum = rhs.c0.add(rhs.c1);
var cross = lhs_sum.mul(rhs_sum);
cross = cross.sub(ac);
cross = cross.sub(bd);
const two_a = bd.a.add(bd.a);
const two_b = bd.b.add(bd.b);
const rbd = CM31{ .a = two_a.sub(bd.b), .b = bd.a.add(two_b) };
return .{ .c0 = ac.add(rbd), .c1 = cross };
}
};
fn field(words: *const [4]u32) QM31 {
// Runtime checks stay active in ReleaseFast, excluding noncanonical inputs.
for (words) |word| if (word >= p) @panic("noncanonical standalone operand");
return .{ .c0 = .{ .a = .{ .v = words[0] }, .b = .{ .v = words[1] } }, .c1 = .{ .a = .{ .v = words[2] }, .b = .{ .v = words[3] } } };
}
// Reduction control: canonical coset log-size 8, half-coset log-size 7.
// Inverse y coordinates are independently computed over Python integers from
// generator (2,1268011823), indices 2^22 + bitReverse7(i)*2^24. Their independent
// complete fold reproduces the existing frozen digest. This control can pass
// while the full owner still fails; it cannot qualify production proof output.
fn reducedFold(dst: []QM31, src: []const QM31, alpha: QM31, inverse: []const u32) void {
const alpha_sq = alpha.square();
var i: usize = 0;
while (i < dst.len) : (i += 1) {
var f0 = src[2 * i];
var f1 = src[2 * i + 1];
const tmp = f0;
f0 = tmp.add(f1);
f1 = tmp.sub(f1).mulM31(.{ .v = inverse[i] });
const fp = alpha.mul(f1).add(f0);
dst[i] = dst[i].mul(alpha_sq).add(fp);
}
}
// C entry retains the same fold context in build-obj optimized-IR reproductions.
export fn reduced_fri_fold(src: *const [4]QM31, dst: *[2]QM31, inverse: *const [2]u32) void {
reducedFold(dst, src, field(&.{ 5, 7, 11, 13 }), inverse);
}
test "minimal two-row pinned FRI optimizer repro" {
const input = [_]QM31{
field(&.{ 1, 3, 5, 11 }), field(&.{ 2, 5, 8, 18 }),
field(&.{ 3, 7, 11, 25 }), field(&.{ 4, 9, 14, 32 }),
};
var output = [_]QM31{QM31.zero()} ** 2;
const inverse = [_]u32{ 843843091, 843843091 };
@call(.never_inline, reduced_fri_fold, .{ &input, &output, &inverse });
const actual = [_]u32{ output[0].c0.a.v, output[0].c0.b.v, output[0].c1.a.v, output[0].c1.b.v };
try std.testing.expectEqual([4]u32{ 1502722116, 2034726799, 546122179, 519690040 }, actual);
}
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs