Repository navigation
Description
Sorting an array of structs with mir.ndslice.sorting.sort can trigger a slice-bounds assertion when compiled with DMD 2.105–2.112 and inlining enabled. The same program passes without inlining, with DMD 2.113, and with LDC 1.36.0.
Reproducer
Extracted from the existing StructB unittest in mir.ndslice.sorting. Tested with unchanged mir-algorithm master at 2da2123 and mir-core 1.7.4 on Linux x86-64.
Save as sorting_repro.d:
module sorting_repro;
import std.random;
import mir.ndslice.sorting : sort;
struct StructA { double val0, val1, val2; }
struct StructB { ulong productId; StructA strA; }
auto createStructBArray(uint nbTrades)
{
auto rnd = Random(42);
auto p = StructA(0, 0, 0);
StructB[] ret;
foreach (i; 0 .. nbTrades)
ret ~= StructB(uniform(0, nbTrades, rnd), p);
return ret;
}
void main()
{
auto arrayB = createStructBArray(10000).sort!((a,b) => a.productId < b.productId);
foreach (i; 1 .. arrayB.length)
assert(arrayB[i-1].productId <= arrayB[i].productId);
}Compile and run, supplying the dependency source paths:
dmd -O -inline -i -I/path/to/mir-algorithm/source -I/path/to/mir-core/source -run sorting_repro.dExpected: the array is sorted by productId and the program exits successfully.
Actual on affected configurations: an assertion in Slice.opSlice, called from quickSortImpl:
Slice.opSlice!0LU: right opSlice boundary must be less than or equal to the length of the given dimension.
The input is initialized, the comparison is ordinary integer ordering, and the random seed is fixed. This reproduces in an ordinary application without enabling library unittests.
Affected compiler versions
Each release comparison uses the same Mir source and that compiler release's bundled standard library and runtime.
| DMD minor releases | No optimization or inlining | -inline |
-O -inline |
|---|---|---|---|
| 2.100–2.104 | Pass | Pass | Pass |
| 2.105–2.107 | Pass | Pass | Fail |
| 2.108–2.112 | Pass | Fail | Fail |
| 2.113 | Pass | Pass | Pass |
Every minor release in the table was tested at its .0 release. Additional checks: 2.104.2 passes, 2.112.1 fails with inlining, and 2.113.1 passes. DMD 2.090.0 also passes all three configurations.
LDC 1.36.0 passes with default settings, -enable-inlining, and -O2 -enable-inlining.
These results support the following minor-version guard for a future workaround:
version (DigitalMars)
{
static if (__VERSION__ >= 2105 && __VERSION__ < 2113)
{
// Workaround, once developed and validated.
}
}This is not an exhaustive patch-release matrix. Windows and 32-bit behavior have not been tested for this sorting reproducer, so the platform scope of a workaround still needs validation.
Compiler investigation
A source bisect identifies DMD commit 543c4ebea3378abe148dfb7bcd983687eec1f181, Propagate NRVO variables in inliner, from dlang/dmd#22189, as the first change where this reproducer passes.
Its immediate parent, 34a6b55c01ba5e7f5b59d43022fdb8ec5142d8f6, fails. These comparisons used matching compiler build settings and a fixed DMD 2.112.1 standard library/runtime, with a passing no-inlining control.
This points to an older compiler inlining problem involving returned struct values. The precise incorrect transformation in this sorting case has not been reduced or proven; the bisect establishes the passing boundary.
Proposed follow-up
Investigate a narrowly scoped source workaround for affected DMD versions, retaining the existing path for unaffected compilers. The alternative must preserve sorting semantics and pass with optimization and inlining enabled.
No validated workaround is available yet. Introducing a named return value in Slice.opSlice, or a temporary before quicksort's slice assignment, did not resolve the failure. Disabling inlining globally for tests would not protect application callers.
The existing sorting regression should remain enabled across compilers, with inlining-enabled CI coverage considered separately.
AI-Assisted-By: Codex (GPT-6 Astra Medium)
Activity