Skip to content

Initialize slice range lengths directly to avoid DMD array-fill issues - #503

Merged
9il merged 1 commit into
libmir:masterfrom
jmh530:codex/explicit-slice-length
Nov 3, 2026
Merged

9il merged 1 commit into
libmir:masterfrom
jmh530:codex/explicit-slice-length

Conversation

@jmh530

@jmh530 jmh530 commented Nov 3, 2026

Copy link
Copy Markdown
Contributor

Initialize the one-element length array in Slice.opSlice with [j - i] instead of scalar initialization. This preserves the API, slice semantics, and attributes while avoiding unnecessary array-fill code generation.

DMD's scalar-fill implementation can overwrite live registers during optimized, inlined compilation, producing incorrect slice dimensions. This was observed while testing mir-stat cross products and reduced to DMD issue #24171. The compiler correction is proposed in DMD PR #24172.

The explicit array literal is used unconditionally. Even with the DMD correction applied, scalar initialization still emits a one-iteration fill loop; the literal avoids that loop. LDC produces identical optimized code for both forms in the tested kernels.

Coverage and validation

Added guarded tests for dynamic bounds, range indices, nested and transformed slices, empty ranges, const views, and write-through aliasing. The tests retain @safe pure nothrow @nogc.

All 19 enabled test modules passed with:

  • Windows x64 DMD 2.113.1, debug and optimized release builds.
  • Windows x64 LDC 1.43, optimized release build.
  • Linux x64 DMD 2.113.1, optimized release build.

Focused compatibility checks also passed with DMD 2.094.0 and 2.102.0.

The original mir-stat cross-product reproducer fails with scalar initialization and passes with the literal under DMD 2.113.1 using -O -inline -release -boundscheck=off. Both cases retained the same separate indexing workaround from #500.

Performance

Windows x64 slicing microbenchmarks using the fixed DMD build showed approximately 74–75% less elapsed time for rectangular subviews and 23–27% less for subviews involving transpose/reversal. These results were repeated across two batches of 20 samples per variant. LDC generated identical kernel assembly for both forms.

These are slice-heavy microbenchmarks, not equivalent improvements to complete numerical workloads. Full mir-stat suite validation remains outstanding.

AI-Assisted-By: Codex (GPT-6 Astra Medium)

Construct the one-element length array directly in Slice.opSlice. This avoids DMD's scalar-fill code generation, which can overwrite live registers, and eliminates unnecessary fill loops even with the compiler's register-allocation fix applied.

Preserve slice semantics and attributes without compiler-specific guards. Cover dynamic bounds, nested and transformed slices, empty ranges, const views, and write-through aliasing.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
@jmh530
jmh530 requested a review from 9il November 3, 2026 12:27
@9il
9il merged commit 8014dd7 into libmir:master Nov 3, 2026
25 of 26 checks passed
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