[llvm] 9996e71 - [Support] Implement LLVM_ENABLE_REVERSE_ITERATION for StringMap

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 21 08:46:57 PDT 2023


Author: Fangrui Song
Date: 2023-07-21T08:46:51-07:00
New Revision: 9996e71f2d73a76dc62eb12fc3012aae91014b27

URL: https://github.com/llvm/llvm-project/commit/9996e71f2d73a76dc62eb12fc3012aae91014b27
DIFF: https://github.com/llvm/llvm-project/commit/9996e71f2d73a76dc62eb12fc3012aae91014b27.diff

LOG: [Support] Implement LLVM_ENABLE_REVERSE_ITERATION for StringMap

ProgrammersManual.html says

> StringMap iteration order, however, is not guaranteed to be deterministic, so any uses which require that should instead use a std::map.

This patch makes -DLLVM_REVERSE_ITERATION=on (currently
-DLLVM_ENABLE_REVERSE_ITERATION=on works as well) shuffle StringMap
iteration order (actually flipping the hash so that elements not in the
same bucket are reversed) to catch violations, similar to D35043 for
DenseMap. This should help change the hash function (e.g., D142862,
D155781).

With a lot of fixes, there are still some violations. This patch
implements the "reverse_iteration" lit feature to skip such tests.
Eventually we should remove this feature.

`ninja check-{llvm,clang,clang-tools}` are clean with
`#define LLVM_ENABLE_REVERSE_ITERATION 1`.

Reviewed By: jhenderson

Differential Revision: https://reviews.llvm.org/D155789

Added: 
    

Modified: 
    llvm/lib/Support/StringMap.cpp
    llvm/test/CMakeLists.txt
    llvm/test/CodeGen/MIR/AMDGPU/virtreg-uses-unallocatable-class.mir
    llvm/test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s
    llvm/test/TableGen/GlobalISelCombinerMatchTableEmitter/pattern-parsing.td
    llvm/test/lit.site.cfg.py.in
    llvm/utils/lit/lit/llvm/config.py

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Support/StringMap.cpp b/llvm/lib/Support/StringMap.cpp
index cf8f0930459d45..82c6e755284f15 100644
--- a/llvm/lib/Support/StringMap.cpp
+++ b/llvm/lib/Support/StringMap.cpp
@@ -12,6 +12,7 @@
 
 #include "llvm/ADT/StringMap.h"
 #include "llvm/Support/MathExtras.h"
+#include "llvm/Support/ReverseIteration.h"
 #include "llvm/Support/xxhash.h"
 
 using namespace llvm;
@@ -85,6 +86,8 @@ unsigned StringMapImpl::LookupBucketFor(StringRef Name) {
   if (NumBuckets == 0)
     init(16);
   unsigned FullHashValue = xxHash64(Name);
+  if (shouldReverseIterate())
+    FullHashValue = ~FullHashValue;
   unsigned BucketNo = FullHashValue & (NumBuckets - 1);
   unsigned *HashTable = getHashTable(TheTable, NumBuckets);
 
@@ -140,6 +143,8 @@ int StringMapImpl::FindKey(StringRef Key) const {
   if (NumBuckets == 0)
     return -1; // Really empty table?
   unsigned FullHashValue = xxHash64(Key);
+  if (shouldReverseIterate())
+    FullHashValue = ~FullHashValue;
   unsigned BucketNo = FullHashValue & (NumBuckets - 1);
   unsigned *HashTable = getHashTable(TheTable, NumBuckets);
 

diff  --git a/llvm/test/CMakeLists.txt b/llvm/test/CMakeLists.txt
index 089d374f282d78..8cd77b72c98734 100644
--- a/llvm/test/CMakeLists.txt
+++ b/llvm/test/CMakeLists.txt
@@ -22,6 +22,7 @@ llvm_canonicalize_cmake_booleans(
   LLVM_INLINER_MODEL_AUTOGENERATED
   LLVM_RAEVICT_MODEL_AUTOGENERATED
   LLVM_ENABLE_EXPENSIVE_CHECKS
+  LLVM_ENABLE_REVERSE_ITERATION
   LLVM_INCLUDE_DXIL_TESTS
   LLVM_TOOL_LLVM_DRIVER_BUILD
   )

diff  --git a/llvm/test/CodeGen/MIR/AMDGPU/virtreg-uses-unallocatable-class.mir b/llvm/test/CodeGen/MIR/AMDGPU/virtreg-uses-unallocatable-class.mir
index 3025a60f843cdf..e5daee29a52daf 100644
--- a/llvm/test/CodeGen/MIR/AMDGPU/virtreg-uses-unallocatable-class.mir
+++ b/llvm/test/CodeGen/MIR/AMDGPU/virtreg-uses-unallocatable-class.mir
@@ -1,3 +1,4 @@
+# UNSUPPORTED: reverse_iteration
 # RUN: not llc -mtriple=amdgcn-- -mcpu=gfx900 -run-pass=none -o - %s 2>&1 | FileCheck %s
 
 # Check a diagnostic is emitted if non-allocatable classes are used

diff  --git a/llvm/test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s b/llvm/test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s
index d826b37b6b0007..495c8aed23d27b 100644
--- a/llvm/test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s
+++ b/llvm/test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s
@@ -1,3 +1,4 @@
+# UNSUPPORTED: reverse_iteration
 # RUN: rm -rf %t && mkdir -p %t
 # RUN: llvm-mc -triple=armv7s-apple-ios7.0.0 -filetype=obj -o %t/foo.o %s
 # RUN: llvm-rtdyld -triple=armv7s-apple-ios7.0.0 -verify -check=%s %t/foo.o

diff  --git a/llvm/test/TableGen/GlobalISelCombinerMatchTableEmitter/pattern-parsing.td b/llvm/test/TableGen/GlobalISelCombinerMatchTableEmitter/pattern-parsing.td
index 6c52e9505c1b4d..c0bacf9cea36f6 100644
--- a/llvm/test/TableGen/GlobalISelCombinerMatchTableEmitter/pattern-parsing.td
+++ b/llvm/test/TableGen/GlobalISelCombinerMatchTableEmitter/pattern-parsing.td
@@ -1,3 +1,4 @@
+// UNSUPPORTED: reverse_iteration
 // RUN: llvm-tblgen -I %p/../../../include -gen-global-isel-combiner-matchtable \
 // RUN:     -gicombiner-stop-after-parse -combiners=MyCombiner %s | \
 // RUN: FileCheck %s

diff  --git a/llvm/test/lit.site.cfg.py.in b/llvm/test/lit.site.cfg.py.in
index 911d5091ef1eb9..57ee2100dfb7d0 100644
--- a/llvm/test/lit.site.cfg.py.in
+++ b/llvm/test/lit.site.cfg.py.in
@@ -58,6 +58,7 @@ config.have_tflite = @LLVM_HAVE_TFLITE@
 config.llvm_inliner_model_autogenerated = @LLVM_INLINER_MODEL_AUTOGENERATED@
 config.llvm_raevict_model_autogenerated = @LLVM_RAEVICT_MODEL_AUTOGENERATED@
 config.expensive_checks = @LLVM_ENABLE_EXPENSIVE_CHECKS@
+config.reverse_iteration = @LLVM_ENABLE_REVERSE_ITERATION@
 config.dxil_tests = @LLVM_INCLUDE_DXIL_TESTS@
 config.have_llvm_driver = @LLVM_TOOL_LLVM_DRIVER_BUILD@
 

diff  --git a/llvm/utils/lit/lit/llvm/config.py b/llvm/utils/lit/lit/llvm/config.py
index 26f86ed089113d..16cc2968034bf7 100644
--- a/llvm/utils/lit/lit/llvm/config.py
+++ b/llvm/utils/lit/lit/llvm/config.py
@@ -120,6 +120,9 @@ def __init__(self, lit_config, config):
         if have_zstd:
             features.add("zstd")
 
+        if getattr(config, "reverse_iteration", None):
+            features.add("reverse_iteration")
+
         # Check if we should run long running tests.
         long_tests = lit_config.params.get("run_long_tests", None)
         if lit.util.pythonize_bool(long_tests):


        


More information about the llvm-commits mailing list