Skip to content

Commit

Permalink
[Support] Implement LLVM_ENABLE_REVERSE_ITERATION for StringMap
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MaskRay committed Jul 21, 2023
1 parent 6de5922 commit 9996e71
Show file tree
Hide file tree
Showing 7 changed files with 13 additions and 0 deletions.
5 changes: 5 additions & 0 deletions llvm/lib/Support/StringMap.cpp
Expand Up @@ -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;
Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);

Expand Down
1 change: 1 addition & 0 deletions llvm/test/CMakeLists.txt
Expand Up @@ -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
)
Expand Down
@@ -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
Expand Down
@@ -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
Expand Down
@@ -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
Expand Down
1 change: 1 addition & 0 deletions llvm/test/lit.site.cfg.py.in
Expand Up @@ -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@

Expand Down
3 changes: 3 additions & 0 deletions llvm/utils/lit/lit/llvm/config.py
Expand Up @@ -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):
Expand Down

0 comments on commit 9996e71

Please sign in to comment.