[flang] LoopVersioning emits versioned arguments in nondeterministic order (DenseMap iteration), making -fversion-loops-for-stride output vary between identical runs
With loop versioning enabled (-fversion-loops-for-stride; the driver turns it on by default at -O3/-Ofast, see shouldLoopVersion in clang/lib/Driver/ToolChains/Flang.cpp), flang -fc1 -emit-llvm -O2 -fversion-loops-for-stride produces different LLVM IR across identical invocations of the same input. The difference is the order in which the LoopVersioning pass emits the per-argument stride checks and the corresponding fir.box_dims/fir.convert/fir.coordinate_of rewrites for the two versioned array arguments (a and b swap places), which then propagates to the LLVM IR.
Reproducer (lv.f90):
subroutine s(a, b, n)
real :: a(:), b(:)
integer :: n, i
do i = 1, n
a(i) = b(i) * 2.0
end do
end subroutine
$ for i in 1 2 3 4 5 6 7 8; do flang -fc1 -emit-llvm -O2 -fversion-loops-for-stride lv.f90 -o out.$i.ll; done
$ md5sum out.*.ll | awk '{print $1}' | sort | uniq -c
6 9d18df9ff500007627d59c6fbd5c9766
2 c09360c8df36fa132d7b1394769dc7f3
Without -fversion-loops-for-stride the same six invocations are byte-identical. The pass itself is already nondeterministic — dumping the IR right after it also splits across runs:
$ for i in 1 2 3 4 5 6 7 8; do flang -fc1 -emit-llvm -O2 -fversion-loops-for-stride -mmlir --mlir-print-ir-after=loop-versioning lv.f90 -o /dev/null 2> lv.$i.txt; done
$ md5sum lv.*.txt | awk '{print $1}' | sort | uniq -c
3 1f90abf6f00e83f513c367513a4bd94f
5 5c99362ddf2ebde8dc4fa54d9e78505e
Representative diff between two such dumps (only the argument order differs):
- %7:3 = fir.box_dims %1, %c0_0 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
+ %7:3 = fir.box_dims %2, %c0_0 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
- %9:3 = fir.box_dims %2, %c0_1 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
+ %9:3 = fir.box_dims %1, %c0_1 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
Likely cause: flang/lib/Optimizer/Transforms/LoopVersioning.cpp collects the per-loop argument usage in llvm::DenseMap<mlir::Value, ArgInfo> usageInfo (struct ArgsUsageInLoop) and then builds the versioning list by iterating that map:
for (auto &arg : argsInLoop.usageInfo)
info.argsAndDims.push_back(arg.second);
DenseMap iteration order over mlir::Value keys depends on pointer values, so the order of argsAndDims — and therefore of the emitted fir.if chain and the fir.box_dims / fir.convert / fir.coordinate_of rewrites — varies between runs. Using llvm::MapVector (or sorting argsAndDims by the argument's position in argsOfInterest) should make the output deterministic.
Both outputs are semantically equivalent, so this is a reproducibility problem (reproducible builds, IR-diff-based testing) rather than a miscompile.
Observed with flang version 24.0.0git at f77e8156f86bf706a2a119c25f966bb158ed2312 (2026-09-22 main) and with a build of main from 2026-09-20 (ffeffce9253b), x86_64 Linux.
(Filed with the help of AI)
@llvm/issue-subscribers-flang-ir
Author: Eugene Epshteyn (eugeneepshteyn)
Reproducer (lv.f90):
subroutine s(a, b, n)
real :: a(:), b(:)
integer :: n, i
do i = 1, n
a(i) = b(i) * 2.0
end do
end subroutine
$ for i in 1 2 3 4 5 6 7 8; do flang -fc1 -emit-llvm -O2 -fversion-loops-for-stride lv.f90 -o out.$i.ll; done
$ md5sum out.*.ll | awk '{print $1}' | sort | uniq -c
6 9d18df9ff500007627d59c6fbd5c9766
2 c09360c8df36fa132d7b1394769dc7f3
Without -fversion-loops-for-stride the same six invocations are byte-identical. The pass itself is already nondeterministic — dumping the IR right after it also splits across runs:
$ for i in 1 2 3 4 5 6 7 8; do flang -fc1 -emit-llvm -O2 -fversion-loops-for-stride -mmlir --mlir-print-ir-after=loop-versioning lv.f90 -o /dev/null 2> lv.$i.txt; done
$ md5sum lv.*.txt | awk '{print $1}' | sort | uniq -c
3 1f90abf6f00e83f513c367513a4bd94f
5 5c99362ddf2ebde8dc4fa54d9e78505e
Representative diff between two such dumps (only the argument order differs):
- %7:3 = fir.box_dims %1, %c0_0 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
+ %7:3 = fir.box_dims %2, %c0_0 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
- %9:3 = fir.box_dims %2, %c0_1 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
+ %9:3 = fir.box_dims %1, %c0_1 : (!fir.box<!fir.array<?xf32>>, index) -> (index, index, index)
Likely cause: flang/lib/Optimizer/Transforms/LoopVersioning.cpp collects the per-loop argument usage in llvm::DenseMap<mlir::Value, ArgInfo> usageInfo (struct ArgsUsageInLoop) and then builds the versioning list by iterating that map:
for (auto &arg : argsInLoop.usageInfo)
info.argsAndDims.push_back(arg.second);
DenseMap iteration order over mlir::Value keys depends on pointer values, so the order of argsAndDims — and therefore of the emitted fir.if chain and the fir.box_dims / fir.convert / fir.coordinate_of rewrites — varies between runs. Using llvm::MapVector (or sorting argsAndDims by the argument's position in argsOfInterest) should make the output deterministic.
Both outputs are semantically equivalent, so this is a reproducibility problem (reproducible builds, IR-diff-based testing) rather than a miscompile.
Observed with flang version 24.0.0git at f77e8156f86bf706a2a119c25f966bb158ed2312 (2026-09-22 main) and with a build of main from 2026-09-20 (ffeffce9253b), x86_64 Linux.
Fixed by #233438