# Reverse iteration bots

**URL:** <https://discourse.llvm.org/t/reverse-iteration-bots/72224>\
**Category:** Project Infrastructure\
**Tags:** llvm\
**Created:** [July 23, 2023, 2:22am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224 "2023-07-23T02:22:45Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![MaskRay](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/maskray/32/81_2.png) [@MaskRay](https://discourse.llvm.org/u/MaskRay)\
**Post date:** [July 23, 2023, 2:22am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/1 "2023-07-23T02:22:45Z")

</div>

CMake `-DLLVM_ENABLE_REVERSE_ITERATION=on` makes some LLVM containers (currently `DenseMap` (`DenseSet`) and `StringMap` (`StringSet`) (since [D155789](https://reviews.llvm.org/D155789)) to be iterated in a reversed order[1]. This is a good way to prevent reliance on the unspecified iteration order [2].

```auto
for (auto x : aStringMap) {
  // do something that is sensitive to the order. // bad pattern
}

```

Currently there is one builder [`reverse-iteration`](https://lab.llvm.org/buildbot/#/builders/54) (maintained by @mgrang, thanks!) that checks these subprojects `llvm;clang;polly`.

I wonder whether we can have `-DLLVM_ENABLE_REVERSE_ITERATION=on` coverage for `lld;flang;lldb;bolt;mlir`. This will require adding `-DLLVM_ENABLE_REVERSE_ITERATION=on` to certain builders (`buildbot/osuosl/master/config/builders.py`?).

Unlike `-DLLVM_ENABLE_EXPENSIVE_CHECKS=ON`, `-DLLVM_ENABLE_REVERSE_ITERATION=on` has no performance overhead.

* * *

BTW, `-DLLVM_ENABLE_REVERSE_ITERATION=on` and `-DLLVM_REVERSE_ITERATION=on` have the same effect. I wonder whether we should converge to one spelling. `LLVM_ENABLE_REVERSE_ITERATION` is also a C++ macro name, so I personally prefer `-DLLVM_ENABLE_REVERSE_ITERATION=on` more.

[1]: `StringMap` is not completely reversely iterated. For simplicity we just apply bitwise NOT to the hash value. To change `xxHash64` (xxh64) to `xxh3_64bits` for `StringMap`, I have fixed ~20 bugs in `llvm;clang;lld/wasm;lldb;flang`.  
[2]: [https://llvm.org/docs/ProgrammersManual.html](https://llvm.org/docs/ProgrammersManual.html) even says that the order can be non-deterministic.

---

<div class="post-metadata">

**Author:** ![efriedma-quic](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/efriedma-quic/32/2053_2.png) [@efriedma-quic](https://discourse.llvm.org/u/efriedma-quic)\
**Post date:** [July 24, 2023, 4:00am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/2 "2023-07-24T04:00:00Z")

</div>

> maintained by @mgrang, thanks

The bot is maintained by Qualcomm. @mgrang is no longer working at Qualcomm, so he can’t help you with the bot.

Not sure how much spare capacity we have on the hexagon-build-02/03 bots, but getting coverage for flang and lld would be useful for us. If you post a patch for the reverse-iteration config, I’ll help get it reviewed. (buildbot/osuosl/master/config/builders.py is the right path.)

---

<div class="post-metadata">

**Author:** ![MaskRay](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/maskray/32/81_2.png) [@MaskRay](https://discourse.llvm.org/u/MaskRay)\
**Post date:** [July 24, 2023, 4:38am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/3 "2023-07-24T04:38:36Z")

</div>

Thank you! If the capacity is a problem, I am wondering whether we can get the coverage for mlir/flang by adding `-DLLVM_ENABLE_REVERSE_ITERATION=on` to some other builders.

For example, there are multiple builders named `flang-aarch64-*` on `linaro-*` workers (@DavidSpickett). We can add `-DLLVM_ENABLE_REVERSE_ITERATION=on` to one of the worker and then get coverage for `check-flang`.

Similarly, there are multiple `mlir-*` builders testing `check-mlir`. We can change one to use `-DLLVM_ENABLE_REVERSE_ITERATION=on`.

The `reverse-iteration` bot can possibly do additional `check-lld`. `check-lld` tests are very fast, so hopefully there will be no capacity problem.

---

<div class="post-metadata">

**Author:** ![DavidSpickett](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/davidspickett/32/1984_2.png) [@DavidSpickett](https://discourse.llvm.org/u/DavidSpickett)\
**Post date:** [July 24, 2023, 9:49am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/4 "2023-07-24T09:49:51Z")

</div>

I oversee the bots but flang isn’t my usual area, so we (Linaro) will talk it over internally. I/we will get back to you shortly.

---

<div class="post-metadata">

**Author:** ![DavidSpickett](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/davidspickett/32/1984_2.png) [@DavidSpickett](https://discourse.llvm.org/u/DavidSpickett)\
**Post date:** [July 27, 2023, 11:41am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/5 "2023-07-27T11:41:59Z")

</div>

Linaro will cover flang with `LLVM_ENABLE_REVERSE_ITERATION`. We just need some time to figure out what form that will take, new bot, or adding to an existing one.

> [@MaskRay](#):
>
> Similarly, there are multiple `mlir-*` builders testing `check-mlir`. We can change one to use `-DLLVM_ENABLE_REVERSE_ITERATION=on`.

Consider putting reverse\_it in the name or tagging it if possible. Saves people digging into the cmake after a long time being confused why they can’t reproduce the issue.

---

<div class="post-metadata">

**Author:** ![DavidSpickett](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/davidspickett/32/1984_2.png) [@DavidSpickett](https://discourse.llvm.org/u/DavidSpickett)\
**Post date:** [August 7, 2023, 11:21am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/6 "2023-08-07T11:21:22Z")

</div>

[https://reviews.llvm.org/D157274](https://reviews.llvm.org/D157274)

---

<div class="post-metadata">

**Author:** ![DavidSpickett](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/davidspickett/32/1984_2.png) [@DavidSpickett](https://discourse.llvm.org/u/DavidSpickett)\
**Post date:** [August 29, 2023, 12:58pm UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/7 "2023-08-29T12:58:07Z")

</div>

The flang bot is online now [Buildbot](https://lab.llvm.org/buildbot/#/builders/266) (pretty slow right now, it’ll catch up as the ccache fills up).

---

<div class="post-metadata">

**Author:** ![MaskRay](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/maskray/32/81_2.png) [@MaskRay](https://discourse.llvm.org/u/MaskRay)\
**Post date:** [August 30, 2023, 1:04am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/8 "2023-08-30T01:04:07Z")

</div>

Thank you!

If `-DCMAKE_BUILD_TYPE=Debug` builds are too slow, perhaps try `-O1`?

---

<div class="post-metadata">

**Author:** ![DavidSpickett](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/davidspickett/32/1984_2.png) [@DavidSpickett](https://discourse.llvm.org/u/DavidSpickett)\
**Post date:** [August 30, 2023, 8:12am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/9 "2023-08-30T08:12:03Z")

</div>

I could but the point is to keep checking the debug configuration as well. Times are up across the board for flang which is to be expected after a restart, debug reverse iteration isn’t an outlier anyway.

---

<div class="post-metadata">

**Author:** ![MaskRay](https://sea1.discourse-cdn.com/flex021/user_avatar/discourse.llvm.org/maskray/32/81_2.png) [@MaskRay](https://discourse.llvm.org/u/MaskRay)\
**Post date:** [June 21, 2024, 7:06am UTC](https://discourse.llvm.org/t/reverse-iteration-bots/72224/10 "2024-06-21T07:06:42Z")

</div>

LLVM\_ENABLE\_REVERSE\_ITERATION only applies to pointer-like types.

```auto
% cat llvm/include/llvm/Support/ReverseIteration.h
...
template<class T = void *>
bool shouldReverseIterate() {
#if LLVM_ENABLE_REVERSE_ITERATION
  return detail::IsPointerLike<T>::value;
#else
  return false;
#endif
}

```

Code might rely on the iteration order of `DenseMap<StringRef, X>` and making  
`DenseMap`/`hash_value(StringRef)` replacement difficult.  
Code might rely on `hash_value(StringRef)` and use it for on-disk serialization.

I’ve fixed quite a few issues in f8f4235612b9 c025bd1fdbbd 89e8e63f47ff  
86eb6bf6715c eb8d03656549 0ea6b8e476c2 58d7a6e0e636 8ea31db27211  
255986e27fcf.

This effort allows us to try replacing the algorithm in Hashing.h.  
Using a non-deterministic seed for Hashing.h should help: [[Hashing] Use a non-deterministic seed by MaskRay · Pull Request #96282 · llvm/llvm-project · GitHub](https://github.com/llvm/llvm-project/pull/96282)

See also [Determinism in hash tables · Issue #339 · abseil/abseil-cpp · GitHub](https://github.com/abseil/abseil-cpp/issues/339)
