Repository navigation
Initialize slice range lengths directly to avoid DMD array-fill issues - #503
Merged
Merged
Conversation
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)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Initialize the one-element length array in
Slice.opSlicewith[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:
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)