[Mlir-commits] [mlir] [flang][mlir] fix sort irreflexibility violation in #155348 (PR #164833)

Emilio Cota llvmlistbot at llvm.org
Thu Oct 23 08:15:52 PDT 2025


https://github.com/cota created https://github.com/llvm/llvm-project/pull/164833

This fixes strict weak ordering checks violations from #155348 when running these two tests:

    mlir/test/Dialect/OpenMP/omp-offload-privatization-prepare.mlir
    mlir/test/Dialect/OpenMP/omp-offload-privatization-prepare-by-value.mlir

Sample error:

    /stable/src/libcxx/include/__debug_utils/strict_weak_ordering_check.h:50: libc++ Hardening assertion !__comp(*(__first + __a), *(__first + __b)) failed: Your comparator is not a valid strict-weak ordering

This is because (x < x) should be false, not true, to meet the irreflexibility property. (Note that .contains(x, x) returns true.)

I'm afraid that even after this commit we can't guarantee a strict weak ordering, because we can't guarantee transitivity of equivalence by sorting with a strict dominance function. However the tests are not failing anymore, and I am not at all familiar with this code so I will leave this concern up to the original author for consideration. (Ideas without any further context: I would consider a topological sort or walking a dominator tree.)

Reference on std::sort and strict weak ordering:
  https://danlark.org/2022/04/20/changing-stdsort-at-googles-scale-and-beyond/

>From ecdd1aceb698b7e5e6cbcaf25968aa932e93b539 Mon Sep 17 00:00:00 2001
From: Emilio Cota <ecg at google.com>
Date: Thu, 23 Oct 2025 10:59:57 -0400
Subject: [PATCH] [flang][mlir] fix sort irreflexibility violation in #155348

This fixes strict weak ordering checks violations from #155348
when running these two tests:

    mlir/test/Dialect/OpenMP/omp-offload-privatization-prepare.mlir
    mlir/test/Dialect/OpenMP/omp-offload-privatization-prepare-by-value.mlir

Sample error:

    /stable/src/libcxx/include/__debug_utils/strict_weak_ordering_check.h:50: libc++ Hardening assertion !__comp(*(__first + __a), *(__first + __b)) failed: Your comparator is not a valid strict-weak ordering

This is because (x < x) should be false, not true, to meet
the irreflexibility property. (Note that .contains(x, x) returns true.)

I'm afraid that even after this commit we can't guarantee a
strict weak ordering, because we can't guarantee transitivity of
equivalence by sorting with a strict dominance function. However the tests
are not failing anymore, and I am not at all familiar with this code so
I will leave this concern up to the original author for consideration.
(Ideas without any further context: I would consider a topological sort
or walking a dominator tree.)

Reference on std::sort and strict weak ordering:
  https://danlark.org/2022/04/20/changing-stdsort-at-googles-scale-and-beyond/
---
 .../OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp   | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp b/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp
index a9125ec8f74c3..c117d9b034b7a 100644
--- a/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp
+++ b/mlir/lib/Dialect/OpenMP/Transforms/OpenMPOffloadPrivatizationPrepare.cpp
@@ -189,7 +189,9 @@ class PrepareForOMPOffloadPrivatizationPass
 
         DominanceInfo dom;
         llvm::sort(chainOfOps, [&](Operation *l, Operation *r) {
-          return dom.dominates(l, r);
+          if (l == r)
+            return false;
+          return dom.properlyDominates(l, r);
         });
 
         rewriter.setInsertionPoint(chainOfOps.front());



More information about the Mlir-commits mailing list