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

via llvm-commits llvm-commits at lists.llvm.org
Wed May 15 08:59:22 PDT 2024


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

>From 7af38ff80a9f09cb221c8537fa731ae670b052bc Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Henrik=20Lindstr=C3=B6m?= <henrik at lxm.se>
Date: Wed, 15 May 2024 17:55:38 +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 | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/lib/Transforms/Scalar/SROA.cpp b/llvm/lib/Transforms/Scalar/SROA.cpp
index 096c6d1b1fad2..756daf5bb41fa 100644
--- a/llvm/lib/Transforms/Scalar/SROA.cpp
+++ b/llvm/lib/Transforms/Scalar/SROA.cpp
@@ -630,7 +630,7 @@ class AllocaSlices {
     int OldSize = Slices.size();
     Slices.append(NewSlices.begin(), NewSlices.end());
     auto SliceI = Slices.begin() + OldSize;
-    llvm::sort(SliceI, Slices.end());
+    std::stable_sort(SliceI, Slices.end());
     std::inplace_merge(Slices.begin(), SliceI, Slices.end());
   }
 
@@ -5122,7 +5122,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