Skip to content

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

Open
jmh530 wants to merge 1 commit into
libmir:masterfrom
jmh530:codex/dmd-inline-indexing
Open

jmh530 wants to merge 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)

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

Rewrite the stride multiplication for DMD 2.113 and later on
x86/x64, retaining the original expression for other configurations.
Reference the compiler issue and document when to bound the workaround.

Add a guarded regression test preserving the scoped ownership and
indexing sequence that triggers the failure.

AI-Assisted-By: Codex (GPT-6 Astra Medium)
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

1 participant