[llvm] [SROA] Use stable sort for slices to avoid non-determinism (PR #91609)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 9 08:52:33 PDT 2024


https://github.com/henke9600 created https://github.com/llvm/llvm-project/pull/91609

Found this while trying to build a LLVM toolchain reproducibly from both Debian 12 and FreeBSD 14. With these changes they come out bit-by-bit identical.

Previously there was a mix of stable and unstable sorts for slices, now only stable sorts are used.

>From d03bd8a5b3f02eae2b5ef92fce9e47f8424fde2f Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Lindstr=C3=B6m?= <henrik at lxm.se>
Date: Thu, 9 May 2024 16:53:16 +0200
Subject: [PATCH] [SROA] Use stable sort for slices to avoid non-determinism

The use of unstable sorting here was found to cause non-deterministic output.
---
 llvm/lib/Transforms/Scalar/SROA.cpp | 7 ++-----
 1 file changed, 2 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 096c6d1b1fad2..857cf6b77ea20 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -627,11 +627,8 @@ class AllocaSlices {
   /// everything so that the usual ordering properties of the alloca's slices
   /// hold.
   void insert(ArrayRef<Slice> NewSlices) {
-    int OldSize = Slices.size();
     Slices.append(NewSlices.begin(), NewSlices.end());
-    auto SliceI = Slices.begin() + OldSize;
-    llvm::sort(SliceI, Slices.end());
-    std::inplace_merge(Slices.begin(), SliceI, Slices.end());
+    llvm::stable_sort(Slices);
   }
 
   // Forward declare the iterator and range accessor for walking the
@@ -5122,7 +5119,7 @@ bool SROA::splitAlloca(AllocaInst &AI, AllocaSlices &AS) {
   }
 
   if (!IsSorted)
-    llvm::sort(AS);
+    llvm::stable_sort(AS);
 
   /// Describes the allocas introduced by rewritePartition in order to migrate
   /// the debug info.



More information about the llvm-commits mailing list