Skip to content

Work around DMD's inlined contiguous slice indexing regression - #500

Merged
jmh530 merged 1 commit into
libmir:masterfrom
jmh530:codex/dmd-inline-indexing
Nov 6, 2026
Merged

jmh530 merged 1 commit into
libmir:masterfrom
jmh530:codex/dmd-inline-indexing

Conversation

@jmh530

@jmh530 jmh530 commented Nov 2, 2026

Copy link
Copy Markdown
Contributor

Fixes #499.
Related compiler report: dlang/dmd#24145.

DMD 2.113.x can reuse stale register values after compound multiplication, causing valid contiguous slice accesses to fail bounds checks when inlined.

Rewrite the stride multiplication in contiguous Slice.indexStride for DMD 2.113 and later on x86/x64. Other compilers, architectures, and older DMD versions retain the original expression. The workaround preserves inlining, allocation behavior, function attributes, and the public API.

The compiler guard has no upper bound yet. Its comment explains that one should be added when a fixed compiler version can be distinguished using __VERSION__.

Add a mir_ndslice_test regression preserving the scoped ownership and indexing sequence that triggers the failure. The test remains enabled across compilers.

Validation

The regression fails before the workaround and passes afterward. The final conditional change passes all 19 modules imported by the ndslice test runner with:

  • Windows DMD 2.113.1: -inline.
  • Windows LDC 1.43.0: -O2 -enable-inlining.
  • Linux DMD 2.113.1: -inline.
  • Linux DMD 2.113.1: -O -inline -release -boundscheck=off.

Compiler-guard checks confirm that the workaround is enabled for DMD 2.113.1 and disabled for DMD 2.112.1 and LDC. Both x86 and x64 DMD guard selections were checked; 32-bit runtime testing was not performed.

The original indexing reproducer passes on DMD 2.112.1 with the workaround disabled. Its broader ndslice run encounters a sorting assertion also reproduced on unchanged upstream source.

These are focused ndslice checks, not a full mir-algorithm test-suite run.

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

@jmh530

jmh530 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

dlang/dmd#24150 was merged, fixing dlang/dmd#24145

@jmh530

jmh530 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@9il Rebased to fix merge conflict.

As mentioned above, the DMD issue was merged, but this still can affect the 2.113 releases. I assume there won't be another 2.113 release.

@jmh530

jmh530 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

But the fix still isn't in beta, even though it's in nightly, so it may make sense to wait to merge until it's in a beta and then tweak the inequality to an equality.

@9il

9il commented Nov 6, 2026

Copy link
Copy Markdown
Member

hmm, can't it be a single line PR ball = _lengths[i + 1] * ball;
don't it generate the same code with LDC anyway as before? if so, just make a single line and merge

Compute the accumulated stride with explicit multiplication instead
of compound assignment. This avoids DMD's stale-register bug during
optimized, inlined contiguous slice indexing.

Use the expression unconditionally, without compiler or version
guards. LDC generates identical optimized code for both expressions
in the tested x86 and x64 configurations.

Retain the regression test preserving the scoped ownership and
indexing sequence that triggers the failure.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
@jmh530
jmh530 force-pushed the codex/dmd-inline-indexing branch from 4522dcc to b85f6a5 Compare November 6, 2026 19:24
@jmh530

jmh530 commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

@9il ok, made the main code change one line. Otherwise kept the regression test

@jmh530
jmh530 merged commit ba6d483 into libmir:master Nov 6, 2026
25 of 26 checks passed
@jmh530
jmh530 deleted the codex/dmd-inline-indexing branch November 6, 2026 19:29
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.

Contiguous slice indexing miscompiles with DMD 2.113.x and -inline

2 participants