This is an archive of the discontinued LLVM Phabricator instance.

[Support] Implement LLVM_ENABLE_REVERSE_ITERATION for StringMap
ClosedPublic

Authored by MaskRay on Jul 20 2023, 12:25 AM.

Details

Summary

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.

Diff Detail

Event Timeline

MaskRay created this revision.Jul 20 2023, 12:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2023, 12:25 AM
Herald added subscribers: hoy, wlei, wenlei and 4 others. · View Herald Transcript
MaskRay requested review of this revision.Jul 20 2023, 12:25 AM
Herald added a project: Restricted Project. · View Herald TranscriptJul 20 2023, 12:25 AM
jhenderson accepted this revision.Jul 20 2023, 12:41 AM

Looks good from my point of view, but probably worth giving it more time for others to look at. Do we need some documentation for the new variable?

This revision is now accepted and ready to land.Jul 20 2023, 12:41 AM

Thank you for favoring this patch:) You may notice that I have reported issues to two patches and I'd like them to be addressed first. At the very least I have to wait for the unit test from D149162 to be fixed as it cannot be worked around by UNSUPPORTED: reverse_iteration.

For the documentation, I am thinking of

**LLVM_ENABLE_REVERSE_ITERATION**:BOOL
  Some set-like containers (e.g., DenseMap, StringMap) have unspecified iteration order. Reverse the iteration order for them to catch accidental reliance. Defaults to OFF.

near LLVM_ENABLE_EXPENSIVE_CHECKS commit 0aa27cd29ba3f2cf98b1f48bc6e653d8a66630e8 (@RKSimon) for

foad added a subscriber: foad.Jul 20 2023, 1:00 AM
MaskRay added inline comments.
llvm/test/CodeGen/MIR/AMDGPU/virtreg-uses-unallocatable-class.mir
2

@arsenm perhaps you can help fix this issue :)

MaskRay edited the summary of this revision. (Show Details)Jul 20 2023, 10:55 PM

https://llvm.org/docs/CMake.html documents "LLVM_REVERSE_ITERATION". Is this an error in the doc (it should be "LLVM_ENABLE_REVERSE_ITERATION")?

https://llvm.org/docs/CMake.html documents "LLVM_REVERSE_ITERATION". Is this an error in the doc (it should be "LLVM_ENABLE_REVERSE_ITERATION")?

It seems that both -DLLVM_REVERSE_ITERATION=1 and -DLLVM_ENABLE_REVERSE_ITERATION=1 are supported... I think this may not be intentional, and we probably should converge on one spelling for the CMake variable.

llvm-zorg has one instance of LLVM_REVERSE_ITERATION, so perhaps LLVM_REVERSE_ITERATION is preferred...

MaskRay edited the summary of this revision. (Show Details)Jul 21 2023, 8:41 AM
This revision was landed with ongoing or failed builds.Jul 21 2023, 8:46 AM
This revision was automatically updated to reflect the committed changes.
foad added inline comments.Jul 24 2023, 8:06 AM
llvm/test/CodeGen/MIR/AMDGPU/virtreg-uses-unallocatable-class.mir
2

MIRParserImpl::setupRegisterInfo iterates over the StringMap PerFunctionMIParsingState::VRegInfosNamed. This only seems to affect the order in which error messages are printed. We could fix the test with CHECK-DAG:.

dblaikie added inline comments.Jul 24 2023, 1:54 PM
llvm/lib/Support/StringMap.cpp
89–90

Does this actually cause reverse iteration? I'd guess it causes some other iteration, but not necessarily reverse? Maybe doesn't really matter, or maybe it'd be good to rename the reverse iteration thing?

(or maybe we could/should move to something more like Abseil that randomizes hash seeds/iteration order more aggressively/broadly, and doesn't require opt-in?)

llvm/test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s
1

Got bugs/outstanding reviews tracking these issues?

llvm/utils/lit/lit/llvm/config.py
123–125

Do you plan to remove this feature in a fairly short time frame? Might be good to have a bug or review identifier here to track cleaning this up?

MaskRay marked 3 inline comments as done.Jul 24 2023, 2:45 PM
MaskRay added inline comments.
llvm/lib/Support/StringMap.cpp
89–90

It's not.

89–90

It's approximate reverse iteration, so the commit message actually says:

... shuffle StringMap iteration order (actually flipping the hash so that elements not in the
same bucket are reversed) to catch violations,

If two elements are hashed to share the same low order bits, whether or not the hash is inverted, their iteration order will be decided by the insertion order (linear probing characteristic). This makes the approach worse than a full-blown approach that changes many places to do a reversed iteration. Personally I feel such a full-blown approach is probably going to make the code too complex...

llvm/test/CodeGen/MIR/AMDGPU/virtreg-uses-unallocatable-class.mir
2

Thank you for mentioning the fix. Filed https://github.com/llvm/llvm-project/issues/64085

Ideally diagnostics have the same order... I assume that MIRParser will not become slower if we add a llvm::sort

llvm/test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s
1

Filed https://github.com/llvm/llvm-project/issues/64085 Only 2 tests need to be fixed :)

llvm/utils/lit/lit/llvm/config.py
123–125

Replied on the other comment. Yes, this is hopefully short-term.

dblaikie added inline comments.Jul 26 2023, 12:01 PM
llvm/test/ExecutionEngine/RuntimeDyld/ARM/MachO_ARM_PIC_relocations.s
1

Would've thought it'd be better/easier to wait to fix 2 tests than to commit this with these additional UNSPPORTEDs and then remove them, etc. But ah well.