Repository navigation
[0.31][Backend] Shorten wide immediate materialization #31
Description
Engineering closure — 2026-09-25
Engineering closure through 0.31 follows the maintainer’s September 25 instruction to skip #30 and exclude 0.32. Release version promotion, fresh distribution bundles, the final release gate and independent release review remain under #30. This closure neither transfers the 0.30 review waiver nor claims an independent security audit or published 0.31 release.
Commit 648bb836, boundary review and acceptance map, isolated attribution, and reproduction/coverage map reconcile the checklist below.
Accepted decisions — 2026-09-21
D2: use one typed plan for sizing, emission, and relaxation. Choose among existing short forms and signed-12-bit decomposition with coalesced shifts; retain the old correct expansion as a non-growing fallback. Use the existing ISA and preserve the separate fixed 20-byte trampoline. Actual encoded CKB-VM execution is required before acceptance.
Full decision record — baseline: 8d6e200caa58d81429912df925a9322f691cb41d; research baseline remains 6d75a6e7. The choices below were the original open questions; this decision record now governs implementation. The issue stays open until its implementation acceptance passes.
Parent: #30
Target: 0.31. Baseline: branch 0.30, commit 6d75a6e7e4ce55b940697428d895eda74a51ca86. Implementation and engineering acceptance are complete; the accepted decisions define the bounded scope and deferred work.
Problem and evidence
src/codegen/assembler.rs constructs every LiForm::Large value from eight bytes using 15 instructions. src/codegen/assembler.rs assigns that form 60 bytes.
For li a0, 72057594037927936 (2^56), existing supported instructions suffice:
addi a0, zero, 1
slli a0, a0, 56This is 8 instruction bytes instead of 60. The 52-byte saving is local to this sequence; it is not a measured whole-contract reduction.
Decisions to record
- Which deterministic materialization algorithm covers the full signed/unsigned 64-bit domain? Start by evaluating signed chunks and coalesced shifts, retaining a correct fallback.
- How should equal-size sequences be ranked for execution cost? State the target/profile assumptions and avoid claiming globally minimal code without a proof.
- Can sizing and emission consume one planned sequence so branch relaxation cannot disagree with actual encoding?
- Are additional instruction forms needed? Start with the existing surface; justify any extension separately.
Acceptance
- Record the chosen algorithm, tie-break rules, and fallback.
- The 2^56 case uses at most 8 instruction bytes and produces the exact RV64 value.
- Cover signed-12 and signed-32 boundaries, sign extension, i64::MIN/MAX, u64::MAX, sparse/dense bit patterns, and deterministic randomized execution checks.
- Check encoded length against planned length; exercise branch relaxation and compile-through ELF generation with changed instruction sizes.
- Publish before/after ELF and cycles for representative wide-constant contracts and the existing matched/growth corpus. Do not extrapolate the local 52-byte saving.
- Run
./scripts/cellscript_gate.sh backendfor the implementation and./scripts/cellscript_gate.sh cibefore claiming merge readiness. Retain fresh cost reports from the exact tested commit. Preserve accept/reject behavior and independent artifact validation; changes to machine contracts must update producers, checker, lowering records, source maps, and negative mutation tests together.
Implementation area: assembler and backend. Can proceed independently of policy/frame redesign after measurement conventions are agreed.
Activity
a19q3 commentedon Sep 21, 2026
Research notes, 2026-09-21. CellScript source checked at
6d75a6e7. These are proposed experiments and decisions, not implementation results.The first implementation can stay within the existing instruction surface. LLVM's RISC-V immediate materializer is a useful reference: peel signed low 12-bit chunks, recurse on the remaining high bits, then emit shifts/additions while unwinding; sparse constants allow larger shifts. Its RV64 base cases also explain why blindly using LUI+ADDI near the signed-32-bit boundary is wrong. LLVM has extension-specific alternatives that should not be imported into CellScript by default.
A small experiment
I wrote a separate BigInt bit-vector model with the current short ADDI/LUI/LUI+ADDI cases, signed-12-bit recursion, and coalesced shifts. It normalizes an accepted literal to its signed 64-bit bit pattern after input-range validation. No ADDIW, compressed instructions, or new ISA extensions are needed for this candidate.
The model passed 231,462 checks: all integers from -65,536 through 65,535, 384 power-of-two boundary checks, 100,000 deterministic 64-bit samples, and six displayed examples. These checks include repeated values. It checked reconstructed bits, immediate/shift ranges, and that the candidate was no longer than the existing expansion. The longest candidate observed was eight instructions; this sampling result is not a proof of an eight-instruction bound.
0x7fffffff0x800000000x01000000000000000x80000000000000000xffffffffffffffff0x123456789abcdef0For the all-ones literal,
addi rd, zero, -1produces the required bit pattern. This does not change the source language's signedness rules. For0x7fffffff, the candidate isaddi 1; slli 31; addi -1; an ADDIW-based alternative can be considered later. The RV64 specification specifies LUI's sign extension and ADDIW's 32-bit-result behavior.This is a host arithmetic model, not an assembler test, CKB-VM execution, or whole-ELF measurement. Existing short-form logic was modeled rather than called from Rust. Before acceptance, execute the actual emitted bytes and use a separate oracle.
Implementation details worth deciding first
x0; the model below only reconstructs a writable destination.Reproducible host model (Node.js; no dependencies)
This standalone research script writes its JSON report under /tmp. It does not load or modify CellScript.
a19q3 commentedon Sep 23, 2026
Review after the 0.31 implementation, 2026-09-23.
D2 is implemented at
65df937f. The issue's “Implementation is planned” paragraph now describes historical status. The immediate planner validates reconstructed RV64 values, shares its plan with layout/emission, and retains the non-growing fallback. The fixed entry trampoline remains separate. Backend and CI passed; see the final implementation and validation record.Remaining work:
x0, and the stable equal-length tie-break. Confirm that sizing, encoding and branch relaxation consume the same plan and that trampoline code does not enter this path. Attach the review to the exact commit and relevant boundary/encoded-VM tests.No new materialization algorithm or ISA extension is needed to finish the accepted 0.31 scope. A global-minimum proof was never the acceptance criterion. This child can complete once its implementation evidence and boundary review are accepted; version bump, fresh downstream bundles and publication belong to #30's release work.
a19q3 commentedon Sep 25, 2026
Engineering acceptance complete at 648bb836.
Validation on clean commit
648bb8362a82586821c0d4ae9f8b2250dd56ab11: localdev, fullci, and fullbackendpassed; GitHub CI also passed and retains the fresh cost/shape/strict-audit artifacts. The final clean-source cost pair passes another 6,782-metric comparison against the archived candidate. The full backend gate rebuilt pinned CKBf7fa4436and passed the local-node 43-action, 17-Lock and 26-stateful-scenario matrix. Local acceptance report SHA-256:8a1aeb4221409f187e82a342c7335db19432a3fd800e9182e1c65806655a889c. This is local-node evidence, not a new public-network deployment.The D2-only control now has archived source identities, report pairs and a reproducible command. Baseline versus D2 passes 6,782 comparable metrics with no regression. For 8 actions with a 32-byte argument: ELF 11,680 → 10,000 bytes, worst successful transaction 14,158 → 13,678 cycles, selected-group rejection 12,455 → 11,975 cycles. Encoded-VM tests cover literal boundaries, x0, far branches and the fixed trampoline; no new ISA or materialization algorithm was added.
Boundary review / acceptance map · attribution and exact report pairs · frozen coverage and reproduction.
Engineering closure through 0.31 follows the maintainer’s September 25 instruction to skip #30 and exclude 0.32. Release version promotion, fresh distribution bundles, the final release gate and independent release review remain under #30. This closure neither transfers the 0.30 review waiver nor claims an independent security audit or published 0.31 release.