Skip to content

Boolean array literals read back their own elements - #537

Merged
ASDAlexander77 merged 1 commit into
mainfrom
fix-bool-const-array-packing
Oct 11, 2026
Merged

ASDAlexander77 merged 1 commit into
mainfrom
fix-bool-const-array-packing

Conversation

@ASDAlexander77

Copy link
Copy Markdown
Owner

Problem

The elements of a boolean array literal read back wrong:

const FLAGS = [false, true, false, true];
let n = 0;
for (let i = 0; i < FLAGS.length; i++) if (FLAGS[i]) n++;
print(n);          // 10, expected 2
print(FLAGS[1]);   // false, expected true

Reported in docs/bugs/native-ts-graphics-findings.md, issue 2 (docs/bugs/ntg_boolean_array_elements.ts).

Cause

getOrCreateGlobalArray (LLVMCodeHelper.h) emits the constant data of an integer or float array literal as an LLVM vector constant: <4 x i1> <i1 false, i1 true, ...>. A vector of i1 is packed into bits in memory, but the array that the data is copied into is read with getelementptr i1 / load i1, one byte per element. So element 1 read the second byte of a 1-byte bit field, which is whatever follows it. const c: boolean[] = [...] took another path and was fine.

Fix

When the element type is narrower than a byte, the data is an LLVM array ([4 x i1], one byte per element) built from a tensor-shaped DenseElementsAttr. Wider element types keep the vector, so their layout doesn't change.

before: @a_… = internal constant <2 x i1> <i1 false, i1 true>
after:  @a_… = internal constant [2 x i1] [i1 false, i1 true]

Testing

  • New 00bool_const_array.ts (compile + JIT, and in TSLANG_CORPUS so it runs under rc and none too): a global literal, locals, for-of, and 12 elements (more than one byte of bits). The current release fails with assertion failed: FLAGS[1].
  • Full ctest: 3919/3919 pass.

🤖 Generated with Claude Code

The constant data of an array literal was an LLVM vector. A vector of
i1 is packed into bits, but elements are read one byte apart, so in
`const FLAGS = [false, true, false, true]` `FLAGS[1]` was false,
`if (FLAGS[i])` was never taken and a count of the true elements came out
as 10. Elements narrower than a byte now get an array, a byte per element;
wider ones keep the vector.

Reported in docs/bugs/native-ts-graphics-findings.md (issue 2).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@ASDAlexander77
ASDAlexander77 merged commit 47c4d65 into main Oct 11, 2026
2 checks passed
@ASDAlexander77
ASDAlexander77 deleted the fix-bool-const-array-packing branch October 11, 2026 12:31
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.

1 participant