Skip to content

fix(comp): a constant array passed to an atomic op is held in a local on C too - #2351

Open
rbmr wants to merge 1 commit into
bendlang:mainfrom
rbmr:fix/c-const-array-atomic
Open

rbmr wants to merge 1 commit into
bendlang:mainfrom
rbmr:fix/c-const-array-atomic

Conversation

@rbmr

@rbmr rbmr commented Nov 25, 2026

Copy link
Copy Markdown
Contributor

On the native lane, an Array.atomic.* call whose array argument is a constant (ALeaf{...}, ANode{...}) stores into a copy that is then dropped. This program passes --check-only:

import Base

def peek(r: Array<U32> & U32) -> U32:
  (a, x) = r
  x

def cell(r: Array<U32> & U32) -> U32:
  (a, o) = r
  peek(Array.get(U32, a, 0))

def main() -> IO(Unit):
  IO.print(U32.show(cell(Array.atomic.add(ALeaf{5}, 0, 3))))

bend f.bend and -o f.js print 8. -o f prints 5. The same happens with all nine atomics, with ALeaf and ANode, and with F32 cells (fadd). An array built at run time, including Array.new, is not affected.

Cause

The atomics' C text names the array three times: a32_add(blk_ptr(e.mem, blk_loc(e.mem, $0), blk_at($0, $1, 0)), ...). emit_intr substitutes an argument's text directly when op_bare says it is safe to repeat, and op_bare holds a constant ALeaf/ANode in a local only when FL.js, the guard tests/run/const_array_op_js.bend covers. On C a constant array's text is blk_new(...), so each of the three names builds its own array:

Term _a_0 = blk_new(e, 0, 0, 0, 1, _fv_0);
Term _a_1 = a32_add(blk_ptr(e.mem, blk_loc(e.mem, blk_new(e, 0, 0, 0, 1, _fv_0)), blk_at(blk_new(e, 0, 0, 0, 1, _fv_0), 0ull, 0)), (u32)3ull);

This started with #1817 (3ed8746). Before it, emit_intr held every argument of an op whose C text is a list of templates. Native C built from 3ed8746^ prints 8.

Change

  • op_bare: a constant ALeaf/ANode is held in a local on both lanes. The negative-F32 part stays JS-only. +2 ttok (65,989 → 65,991).
  • tests/run/const_array_atomic.bend: add on an ALeaf and cas on an ANode. It prints 5, 6 on main's native lane and 8, 9 with this change. The interpreter and JS print 8, 9 either way.

Verified

Linux x86_64, Bun 1.4.0, clang 23, main 6180be5.

  • JS and MJS output: byte-identical for all 1,963 .bend files under tests/, bench/ and demos/.

  • C output: it changes in 3 files, all Array.clone of a constant array (tests/reg/array_keep_cow, array_copy_pair, array_clone_boxed). The copy now reads the held array instead of building the constant a second time:

    -    Term _a_1 = blk_copy(e, blk_node(e, blk_node(e, blk_new(e, 0, 0, 0, 1, _fv_0), ...)));
    +    Term _a_1 = blk_copy(e, _a_0);
  • Native tests: the suite gives the same result per test as on main, plus the new test.

  • Types: tsc --noEmit on comp.ts reports the same diagnostics.

Found while testing recent array changes with Claude Code.

… on C too

The atomics' C text names the array three times. op_bare held a constant
ALeaf/ANode in a local only on the JS lane, so on C each name rebuilt the
constant and the store went into a copy that was dropped:
Array.atomic.add(ALeaf{5}, 0, 3) left the returned array's cell at 5,
where the interpreter and JS give 8. Since bendlang#1817.
@Osraka

Osraka commented Nov 28, 2026

Copy link
Copy Markdown
Contributor

Confirmed on macOS arm64 with Bun 1.4.5, using this PR's tests/run/const_array_atomic.bend unchanged.

  • 6180be5b: interpreter and generated JS print 8 and 9; native C (one runtime thread) prints 5 and 6.
  • 88ffb00f: interpreter, generated JS, and native C all print 8 and 9, matching the fixture.

All six commands exited 0, so the native regression on main is an output mismatch, not a compilation failure. I only ran this fixture, not the full gate.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants