[llvm] [utils/TableGen/X86CompressEVEXTablesEmitter.cpp] Make sure the tablegen output for the `checkPredicate` function is deterministic (PR #84533)

Argyrios Kyrtzidis via llvm-commits llvm-commits at lists.llvm.org
Fri Mar 8 10:47:34 PST 2024


https://github.com/akyrtzi created https://github.com/llvm/llvm-project/pull/84533

The output for the `checkPredicate` function was depending on a `std::map` iteration that was not deterministic from run to run, so the contents of `X86GenCompressEVEXTables.inc` were not deterministic.

Make a change to sort the entries before iterating over them.

>From 803bd407242a21d536c3d73d0d2b652c8c1e23ea Mon Sep 17 00:00:00 2001
From: Argyrios Kyrtzidis <kyrtzidis at apple.com>
Date: Fri, 8 Mar 2024 10:41:16 -0800
Subject: [PATCH] [utils/TableGen/X86CompressEVEXTablesEmitter.cpp] Make sure
 the tablegen output for the `checkPredicate` function is deterministic

The output for the `checkPredicate` function was depending on a `std::map` iteration that was not deterministic from run to run,
so the contents of `X86GenCompressEVEXTables.inc` were not deterministic.

Make a change to sort the entries before iterating over them.
---
 .../TableGen/X86CompressEVEXTablesEmitter.cpp  | 18 +++++++++++++++---
 1 file changed, 15 insertions(+), 3 deletions(-)

diff --git a/llvm/utils/TableGen/X86CompressEVEXTablesEmitter.cpp b/llvm/utils/TableGen/X86CompressEVEXTablesEmitter.cpp
index b96d16b9797cf3..326a9dfbcfefaa 100644
--- a/llvm/utils/TableGen/X86CompressEVEXTablesEmitter.cpp
+++ b/llvm/utils/TableGen/X86CompressEVEXTablesEmitter.cpp
@@ -82,15 +82,27 @@ void X86CompressEVEXTablesEmitter::printTable(const std::vector<Entry> &Table,
 
 void X86CompressEVEXTablesEmitter::printCheckPredicate(
     const PredicateInstMap &PredicateInsts, raw_ostream &OS) {
+  // Create a sorted list of the map entries so that the tablegen output is
+  // deterministic.
+  typedef std::pair<StringRef, const std::vector<const CodeGenInstruction *> *>
+      PredicateInstsTy;
+  std::vector<PredicateInstsTy> SortedPredicateInsts;
+  for (const auto &[Key, Val] : PredicateInsts) {
+    SortedPredicateInsts.push_back({Key->getValueAsString("CondString"), &Val});
+  }
+  llvm::sort(SortedPredicateInsts,
+             [](const PredicateInstsTy &LHS, const PredicateInstsTy &RHS) {
+               return LHS.first < RHS.first;
+             });
 
   OS << "static bool checkPredicate(unsigned Opc, const X86Subtarget "
         "*Subtarget) {\n"
      << "  switch (Opc) {\n"
      << "  default: return true;\n";
-  for (const auto &[Key, Val] : PredicateInsts) {
-    for (const auto &Inst : Val)
+  for (const auto &[Key, Val] : SortedPredicateInsts) {
+    for (const auto &Inst : *Val)
       OS << "  case X86::" << Inst->TheDef->getName() << ":\n";
-    OS << "    return " << Key->getValueAsString("CondString") << ";\n";
+    OS << "    return " << Key << ";\n";
   }
 
   OS << "  }\n";



More information about the llvm-commits mailing list