Skip to content

skc: fold constant StringConcat and ArrayUnsafeGet on constant arrays - #1689

Merged
mbouaziz merged 3 commits into
mainfrom
peephole-const-folds
Nov 17, 2026
Merged

mbouaziz merged 3 commits into
mainfrom
peephole-const-folds

Conversation

@mbouaziz

@mbouaziz mbouaziz commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

What

Two more constant folds in the peephole pass, one commit each. Both are pure
data operations whose result cannot depend on which prelude the compiler was
built with (so they are safe regardless of bootstrap vintage, unlike
Float.toString):

  1. skc: fold StringConcat of constant strings — a StringConcat whose
    arguments are all constant strings folds to a single constant string;
    previously only 0/1-argument concats folded. This also catches constants
    produced by other folds (e.g. (0.1).toString() + "!"). The parts are
    collected into a vector and the fold bails out (return peep) as soon as
    one argument is not a constant string.
  2. skc: fold ArrayUnsafeGet on constant arrays — ArrayUnsafeGet on a
    ConstantArray with a constant index folds to the element constant
    (values[i * valsPerSlot + tupleIndex]), so literal arrays with constant
    indices resolve at compile time, including tuple elements (tupleIndex).
    The fold only fires when provably in bounds (i >= 0,
    tupleIndex < valsPerSlot, slot < values.size()); out-of-bounds or
    non-constant arrays/indices keep the original instruction, since unsafe
    get is UB out of bounds and must not fold.
  3. skc: bump to 0.4.3.

Tests

  • Runtime/StringConcatConst: literal+literal concat, folded-float concat,
    empty-string concats, and a concat with a non-constant argument
    ("a" + 123.toString() + "b", not folded, still correct at runtime).
  • Runtime/ArrayUnsafeGetConst: scalar array at first/middle/last in-bounds
    indices; tuple array exercising tupleIndex 0 and 1; a non-constant index
    ("ab".length() - 1, not folded); a non-constant array (.clone(), not
    folded). The out-of-bounds guard itself can't be runtime-tested (unsafe
    get is UB there), so it is covered by the bounds checks in code.

Validation

  • Clean rebuild (make clean && make stage1 && make test): 1769/1769
    pass, including both new tests and Native/CompilerVersionCheck.
  • prelude suite with the new compiler: green (see below).

Notes

  • AI assistance: commits generated with Mistral Vibe (Co-Authored-By
    trailers); validation performed in-session as described above.

@mbouaziz
mbouaziz force-pushed the peephole-const-folds branch from 3b05188 to 152ca67 Compare November 17, 2026 13:08
@mbouaziz
mbouaziz enabled auto-merge November 17, 2026 13:44
@mbouaziz
mbouaziz force-pushed the peephole-const-folds branch 2 times, most recently from 3fe6736 to 5fb2492 Compare November 17, 2026 14:10
mbouaziz and others added 3 commits November 17, 2026 14:46
A StringConcat whose arguments are all constant strings now folds to a
single constant string; previously only 0/1-argument concats folded. This
also catches constants produced by other folds, e.g. a folded
Float.toString concatenated with a literal.

The parts are collected into a vector and the fold bails out as soon as one
argument is not a constant string, so non-constant concats are left alone.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
ArrayUnsafeGet on a ConstantArray with a constant index now folds to the
element constant, so literal arrays with constant indices (e.g.
Array[10, 20, 30] read at 0, or tuple elements via tupleIndex) resolve at
compile time.

The fold only fires when provably in bounds (index >= 0, tupleIndex within
the slot, computed slot < values.size()); out-of-bounds or non-constant
arrays/indices keep the original instruction (unsafe get is UB out of
bounds, so those must not fold).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Co-Authored-By: Claude Opus 5.5 (Anthropic, via Mistral Vibe; thinking: high) <noreply@anthropic.com>
@mbouaziz
mbouaziz force-pushed the peephole-const-folds branch from 5fb2492 to b21f58f Compare November 17, 2026 14:47
@mbouaziz
mbouaziz disabled auto-merge November 17, 2026 14:47
@mbouaziz
mbouaziz merged commit aad03d5 into main Nov 17, 2026
1 of 4 checks passed
@mbouaziz
mbouaziz deleted the peephole-const-folds branch November 17, 2026 14:47
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