Skip to content

prelude: native block copy for Sequence::join fill loop (fixes #1718 item 1) - #1719

Open
mbouaziz wants to merge 3 commits into
mainfrom
feat/join-block-copy
Open

mbouaziz wants to merge 3 commits into
mainfrom
feat/join-block-copy

Conversation

@mbouaziz

@mbouaziz mbouaziz commented Nov 19, 2026 •

Copy link
Copy Markdown
Contributor

Implements item 1 of #1718 on top of merged #1716, in the compiler itself plus a runtime fallback. 3 commits:

  1. prelude: add native copyStringBytesTo block copy (String bytes -> Array<UInt8>) — SKIP_Sequence_copyStringBytesTo in runtime/string.c (single memcpy of a string's UTF-8 bytes into an Array<UInt8> region at an offset; returns bytes copied) + the @cpp_extern native fun declaration in module Sequence. This is the fallback for the pinned stage0, which doesn't know the intrinsic — the prelude CI job compiles with stage0, so the C symbol must exist.
  2. prelude: use copyStringBytesTo in Sequence::join fill loop — each part/separator is copied with one block-copy call instead of a per-byte Bytes::get/Array::set loop.
  3. compiler: lower Sequence.copyStringBytesTo to an llvm.memcpy — the compiler-direct part: IntrinsicsInliner now recognizes the call and rewrites it to a load of the string's byte size from its header (sk_string_t: u32 at -4 on 64-bit, -8 on wasm32) plus a single llvm.memcpy.p0.p0.i64 into the destination array at the offset, returning the size. No C code involved on stage1+.

Why (verified codegen, see #1718)

The previous per-byte fill loop was not vectorized (zero SIMD instructions in the emitted IR; per-byte external SKIP_String_getByte call + bounds-check branch + scalar store block LLVM's loop vectorizer). With this change, compiling a Vector<String>.join(", ") probe with the freshly built stage1 (--emit llvm-ir --release -O 2) shows 6 llvm.memcpy calls and 0 references to the C fallback; the fill loop body is one memcpy per part/separator. With stage0 the same program links and runs against the C fallback.

Validation

  • stage1 built from this branch (make STAGE=1, skc 0.4.7 (adfcadf8)).
  • Compiler language suite with stage1 (SKTEST_PROFILE=release PATH=stage1/bin:$PATH ../../bin/run-sktest.sh): 1769/1769 pass.
  • Prelude suite with stage1 (exercises the llvm.memcpy path incl. testJoinMultibyte): 146/146 pass; with pinned stage0 (C fallback path): 146/146 pass.
  • Probe binary linked with stage1 prints the correct joined string.
  • skfmt clean; CircleCI on the branch head.

AI-assisted: implemented with Mistral Vibe (Co-Authored-By trailers on the commits).

mbouaziz and others added 2 commits November 19, 2026 21:55
…ay<UInt8>)

Adds SKIP_Sequence_copyStringBytesTo in runtime/string.c: a single
memcpy of a string's UTF-8 bytes into an Array<UInt8> region at a given
offset (no bounds check; returns bytes copied), plus the module-level
native declaration in Sequence.sk. Meant for building byte buffers one
block at a time instead of per-byte get/set loops, which do not
vectorize (per-byte external call + bounds-check branch).

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
Each part (and each separator) is now copied into the output buffer
with a single native memcpy instead of a per-byte Bytes::get/Array::set
loop. See #1718 for the codegen analysis that motivated this.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
The prelude declares copyStringBytesTo as a @cpp_extern native (the C
implementation in runtime/string.c stays as the fallback for compilers
that don't know it, e.g. the pinned stage0). This teaches the compiler
itself to perform the block copy: the intrinsic is rewritten to a load
of the string's byte size from its header plus a single
llvm.memcpy.p0.p0.i64 into the destination array at the given offset,
returning the number of bytes copied.

Motivated by codegen inspection (#1718): the per-byte copy loop does
not vectorize (per-byte external call + bounds-check branch), while a
single memcpy is one call.

Generated by Mistral Vibe.
Co-Authored-By: Mistral Vibe <vibe@mistral.ai>
@mbouaziz
mbouaziz enabled auto-merge November 19, 2026 23:48

This branch has not been deployed

No deployments
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