[libcxx-commits] [libcxx] [libcxx] Improve libcxx tests when using Optimizations. (PR #88897)

Jack Styles via libcxx-commits libcxx-commits at lists.llvm.org
Wed Apr 24 08:10:53 PDT 2024


https://github.com/Stylie777 updated https://github.com/llvm/llvm-project/pull/88897

>From 3f94851e8bd4efac25dad7d922f997fc53e684f3 Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Tue, 16 Apr 2024 11:55:54 +0100
Subject: [PATCH 1/3] [libcxx] Update `DoNotOptimize` variant to accept L and R
 value variables.

As part of the libcxx tests, there is a function provided called `DoNotOptimize`.
The tests currently utilise a version which only uses the value as an input,
leading to the compiler not removing information relating to the data-flow for
the value. This leads to issues when comparing the pointer values, where one is a
global pointer and the other a pointer which is local to the function being
executed.

Another version of `DoNotOptimize` is available which allows for the value
to be an input and output of the inline assembly, which allows for the
pointers to be successfully compared to one another. This function however
only accepts an L Value reference, where the values are initialized as R
value reference, so use the variant of `DoNotOptmize` described in the first
paragraph.

To enable the use of the second variant in the tests, the type of reference
has been updated so it allows both R and L value references as the input
value. This enables the use of the correct version of the function for the
tests.
---
 libcxx/test/support/test_macros.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index 7b2dcbb52d0c88..f88b187f5a8250 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -296,7 +296,7 @@ inline Tp const& DoNotOptimize(Tp const& value) {
 }
 
 template <class Tp>
-inline Tp& DoNotOptimize(Tp& value) {
+inline Tp& DoNotOptimize(Tp&& value) {
 #if defined(__clang__)
   asm volatile("" : "+r,m"(value) : : "memory");
 #else

>From 48a1b8e1220590bb2b8058361f45ea989bb5b0b8 Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Wed, 17 Apr 2024 08:44:31 +0100
Subject: [PATCH 2/3] [libcxx] Add `DoNotOptimize` function call to libcxx
 tests where appropriate

It has been seen, at high levels of optimizations, issues with comparing the
pointer values in some libcxx tests attaining to the assert comparing the pointer
of a global variable to that of one created locally within the test. These tests
follow the same logic as others that utilize the `DoNotOptimize` function, however
these tests do not.

To ensure the correct behaviour at high levels of optimization, these tests now utilize
the function when building the tests. This allows for the correct behaviour to be
observed when running the tests.
---
 .../new.delete/new.delete.array/new.size_align.replace.pass.cpp | 2 +-
 .../new.delete.single/new.size_align.replace.pass.cpp           | 2 +-
 .../new.delete.single/new.size_align_nothrow.replace.pass.cpp   | 2 +-
 3 files changed, 3 insertions(+), 3 deletions(-)

diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
index 2d021ecb30e793..bba4a4707e4204 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
@@ -48,7 +48,7 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_called = delete_called = 0;
-        OverAligned* x = new OverAligned[3];
+        OverAligned* x = DoNotOptimize(new OverAligned[3]);
         assert(static_cast<void*>(x) == DummyData);
         assert(new_called == 1);
 
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
index e5ef5f1669752b..c7c5dd2f7b86ea 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
@@ -48,7 +48,7 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_called = delete_called = 0;
-        OverAligned* x = new OverAligned;
+        OverAligned* x = DoNotOptimize(new OverAligned);
         assert(static_cast<void*>(x) == DummyData);
         assert(new_called == 1);
 
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
index 9a5b53e039025c..476bc994c302cd 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
@@ -47,7 +47,7 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_nothrow_called = delete_called = 0;
-        OverAligned* x = new (std::nothrow) OverAligned;
+        OverAligned* x = DoNotOptimize(new (std::nothrow) OverAligned);
         assert(static_cast<void*>(x) == DummyData);
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_nothrow_called == 1);
 

>From f605a784e6b4e07df516ab31126b1478c39b251c Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Wed, 24 Apr 2024 16:02:25 +0100
Subject: [PATCH 3/3] [NFC] Formatting changes

---
 .../new.delete/new.delete.array/new.size_align.replace.pass.cpp | 2 +-
 .../new.delete.single/new.size_align.replace.pass.cpp           | 2 +-
 .../new.delete.single/new.size_align_nothrow.replace.pass.cpp   | 2 +-
 libcxx/test/support/test_macros.h                               | 2 +-
 4 files changed, 4 insertions(+), 4 deletions(-)

diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
index bba4a4707e4204..f3e3d7da498995 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.pass.cpp
@@ -48,7 +48,7 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_called = delete_called = 0;
-        OverAligned* x = DoNotOptimize(new OverAligned[3]);
+        OverAligned* x             = DoNotOptimize(new OverAligned[3]);
         assert(static_cast<void*>(x) == DummyData);
         assert(new_called == 1);
 
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
index c7c5dd2f7b86ea..fb1994b2c018ed 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align.replace.pass.cpp
@@ -48,7 +48,7 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_called = delete_called = 0;
-        OverAligned* x = DoNotOptimize(new OverAligned);
+        OverAligned* x             = DoNotOptimize(new OverAligned);
         assert(static_cast<void*>(x) == DummyData);
         assert(new_called == 1);
 
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
index 476bc994c302cd..a170a9eb6a8d93 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.pass.cpp
@@ -47,7 +47,7 @@ int main(int, char**) {
     // Test with an overaligned type
     {
         new_nothrow_called = delete_called = 0;
-        OverAligned* x = DoNotOptimize(new (std::nothrow) OverAligned);
+        OverAligned* x                     = DoNotOptimize(new (std::nothrow) OverAligned);
         assert(static_cast<void*>(x) == DummyData);
         ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_nothrow_called == 1);
 
diff --git a/libcxx/test/support/test_macros.h b/libcxx/test/support/test_macros.h
index f88b187f5a8250..aeb5c407b6bc30 100644
--- a/libcxx/test/support/test_macros.h
+++ b/libcxx/test/support/test_macros.h
@@ -297,7 +297,7 @@ inline Tp const& DoNotOptimize(Tp const& value) {
 
 template <class Tp>
 inline Tp& DoNotOptimize(Tp&& value) {
-#if defined(__clang__)
+#  if defined(__clang__)
   asm volatile("" : "+r,m"(value) : : "memory");
 #else
   asm volatile("" : "+m,r"(value) : : "memory");



More information about the libcxx-commits mailing list