Skip to content

Commit 948b91a

Browse files
committed
[NFC][sanitizer] Atomix relaxed in TwoLevelMap
This is NOOP in x86_64. On arch64 it avoids Data Memory Barrier with visible improvements on micro benchmarks. Reviewed By: dvyukov Differential Revision: https://reviews.llvm.org/D112391
1 parent e124074 commit 948b91a

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

compiler-rt/lib/sanitizer_common/sanitizer_flat_map.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,16 @@ class TwoLevelMap {
129129
}
130130

131131
T *GetOrCreate(uptr idx) const {
132-
T *res = Get(idx);
132+
DCHECK_LT(idx, kSize1);
133+
// This code needs to use memory_order_acquire/consume, but we use
134+
// memory_order_relaxed for performance reasons (matters for arm64). We
135+
// expect memory_order_relaxed to be effectively equivalent to
136+
// memory_order_consume in this case for all relevant architectures: all
137+
// dependent data is reachable only by dereferencing the resulting pointer.
138+
// If relaxed load fails to see stored ptr, the code will fall back to
139+
// Create() and reload the value again with locked mutex as a memory
140+
// barrier.
141+
T *res = reinterpret_cast<T *>(atomic_load_relaxed(&map1_[idx]));
133142
if (LIKELY(res))
134143
return res;
135144
return Create(idx);

0 commit comments

Comments
 (0)