Repository navigation
gen-zig CSE hoists count + 1 above var count and across its reassignment #8197
Description
Re-filed from #6292. An attempt claimed that issue on 2026-10-05 and left an empty branch, and a dispatch row that has spent its retry ceiling keeps its issue for good - so no bee could take it again, however much the brief improved. The work below is carried over unchanged.
Refs #6063
The reference path (t27c gen + zig test) fails to compile two specs because the Zig backend's common-subexpression pass hoists an expression above the declaration of the mutable local it reads.
specs/fpga/apb_bridge.t27 line 169:
fn apb_port_count(cfg: ApbConfig) -> u32 {
var count : u32 = 0;
count = count + 1;
count = count + 1;
...
t27c gen (master 47621f4) emits:
fn apb_port_count(cfg: ApbConfig) u32 {
const _cse1 = count + 1;
const _cse6 = count + 1;
const _cse7 = count + 1;
var count: u32 = 0;
and zig test answers use of undeclared identifier 'count'. Same in specs/fpga/axi4.t27. Besides the order, count + 1 is not a common subexpression at all: count is reassigned between the uses, so even after the declaration the CSE would change the result.
Found while landing #6291: t27b now passes both specs (all tests), but they stay outside the honest denominator until the reference compiles them. (specs/fpga/linker.t27 is the same situation through .align = 4, already #4807.)
Boundary
specs/fpga/apb_bridge.t27specs/fpga/axi4.t27specs/fpga/linker.t27
User Scenarios
- Given a spec with a mutable local variable that is reassigned between uses, when the Zig backend runs its common-subexpression elimination pass, then the pass must not hoist expressions that read the variable above its declaration.
- Given a spec where the same expression
count + 1appears multiple times butcountis reassigned between appearances, when the CSE pass runs, then it must not treat those appearances as a common subexpression.
Requirements
- FR-001: The Zig backend's CSE pass MUST ensure that any hoisted expression only references variables that are already declared in the current scope at the hoist point.
- FR-002: The Zig backend's CSE pass MUST NOT combine expressions that read a mutable variable if that variable is reassigned between the expression occurrences.
- FR-003: The compiler MUST emit valid Zig code that compiles with
zig testfor all specs in thespecs/fpga/directory.
Success Criteria
t27c gen specs/fpga/apb_bridge.t27produces Zig code that compiles withzig testand exits 0t27c gen specs/fpga/axi4.t27produces Zig code that compiles withzig testand exits 0t27c gen specs/fpga/linker.t27produces Zig code that compiles withzig testand exits 0- All three specs pass the full test suite (3 specs × their respective test counts)
The instruments
Every one of these reads and prints; none of them writes. Run them from the repository root, with <spec> replaced by the file in ## Boundary:
.t27 is not Rust and not Zig, and a bee fluent in either writes one by
accident. Measured 2026-09-20: a bee filled eight bodies in
specs/file/watcher.t27 with return Ok(());, Err(FileError::WatcherNotFound)
and for i in 0..watchers.length. The parse ratchet refused the whole file --
Unexpected token in expression: RParen, line 123 -- and a spec that does not
parse generates nothing, so every test it already carried stopped running.
At the top level the parser accepts exactly these eight forms, with an optional
pub, and no others:
const var fn enum struct test invariant bench
There is no trait, no impl, no type X = ..., no generics, no macro. What
a bee reaches for from another language, and what happens:
- Rust's result sugar (
Ok(()),Err(E::V),?) is not a construct here. - Rust macros (
println!,format!,vec!) are not constructs here. - A range loop (
for i in 0..n) is Rust and Zig, not this language. - Zig builtins (
catch unreachable,@intCast,@constCast) are not constructs here. - Cross-module reuse (
use other::fn;) parses and then generates a comment and an unqualified call, so the Zig fails withuse of undeclared identifier(use a::b; generates no import, so no spec can reuse a function from another spec #4298). It does not work yet.
t27c parse <spec> answers in one line whether what you wrote is the language.
Run it before you report, every time.
t27c spec-status <spec>- the compiler's one-word verdict: IMPLEMENTED, PARTIAL, UNWRITTEN, NOPARSE, NOFN. Exit code is 0 whatever it says, so read the word.t27c symbols <spec>- every name the file declares, with its kind. Answers "does this already exist here?" before you add it.t27c outline <spec>- per function: its locals, what it calls, what it returns. The contract you are implementing against.t27c coverage <spec>- which functions have a test and which do not. The issue asks for tests; this is how you check you wrote them.t27c lint <spec>- style and shape warnings. It OVER-REPORTShas no test or invariant: measured 2026-09-20 over 60 specs it printed 677 of those wherecoveragefound 190 untested functions, disagreeing on 59 of the 60 - it warns aboutbit_to_trit_pairinspecs/base/ternary_encoding.t27, whichtest bit_to_trit_pair_zerocalls on line 249. Read it as a hint;coverageis the answer.t27c typecheck <spec>- types, before generation. PrintsTypecheck OK (0 errors, 0 warnings)or the errors.t27c test-report <spec>- builds this spec and runs its own tests, which is what the oracle does.BLOCKEDmeans the generated Zig does not compile, with the error beside it.python3 tools/dupe_scan.py --name <function>- where that function already lives, if it does. 576 of 4021 bodies here are byte-identical copies.
The rest of the toolbelt is in docs/BEE_TOOLBELT.md.
Activity
gHashTag commentedon Oct 11, 2026
Another instance, found while porting musl cos into specs/tri/t27b/libm.t27 for #8677 (PR #8919): gen-zig's CSE hoisted a repeated expression to the top of the function across a reassignment of one of its operands. Some drafts produced Zig that did not compile. Others compiled and computed wrong values with no diagnostic, which is the worse case: the reference path then 'passes' the wrong answer. The merged port is written around it (no repeated expression spans a reassignment), and the generated Zig for libm.t27 contains no hoist. t27b's own guard is cse_hoist_defect in cli/t27b/src/lower.rs (being ported in #8592). Raising the priority: this is a silent miscompile in the reference backend, see #7703 and #2629.
optimizer: a repeated expression takes a temp only while its operands…