[compiler-rt] [rtsan][test] Prevent test check from being optimized out in LTO builds (PR #122524)

Paul Kirth via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 10 12:59:39 PST 2025


https://github.com/ilovepi updated https://github.com/llvm/llvm-project/pull/122524

>From b1516c4ab8e580bf0564b0fd142eaecbd38f50aa Mon Sep 17 00:00:00 2001
From: Paul Kirth <paulkirth at google.com>
Date: Fri, 10 Jan 2025 12:56:14 -0800
Subject: [PATCH] =?UTF-8?q?[=F0=9D=98=80=F0=9D=97=BD=F0=9D=97=BF]=20initia?=
 =?UTF-8?q?l=20version?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

Created using spr 1.3.6-beta.1
---
 .../lib/rtsan/tests/rtsan_test_functional.cpp       | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp b/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
index ef11b71f167e1b..40c4f3b129129d 100644
--- a/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
+++ b/compiler-rt/lib/rtsan/tests/rtsan_test_functional.cpp
@@ -145,13 +145,24 @@ TEST(TestRtsan, LaunchingAThreadDiesWhenRealtime) {
 
 namespace {
 void InvokeStdFunction(std::function<void()> &&function) { function(); }
+
+template <typename T>
+void HideMemoryFromCompiler(T* memory) {
+  // Pass the pointer to an empty assembly block as an input, and inform
+  // the compiler that memory is read to and possibly modified. This should not
+  // be architecture specific, since the asm block is empty.
+  __asm__ __volatile__("" ::"r"(memory) : "memory");
+}
 } // namespace
 
 TEST(TestRtsan, CopyingALambdaWithLargeCaptureDiesWhenRealtime) {
   std::array<float, 16> lots_of_data;
   auto LargeLambda = [lots_of_data]() mutable {
-    // Stop everything getting optimised out
     lots_of_data[3] = 0.25f;
+    // In LTO builds, this lambda can be optimized away, since the compiler can
+    // see through the memory accesses after inlining across TUs. Ensure it can
+    // no longer reason about the memory access, so that won't happen.
+    HideMemoryFromCompiler(&lots_of_data[3]);
     EXPECT_EQ(16u, lots_of_data.size());
     EXPECT_EQ(0.25f, lots_of_data[3]);
   };



More information about the llvm-commits mailing list