[libcxx-commits] [libcxx] [libcxx] Improve libcxx tests when using Optimizations. (PR #88897)
Jack Styles via libcxx-commits
libcxx-commits at lists.llvm.org
Fri Apr 26 01:11:30 PDT 2024
https://github.com/Stylie777 updated https://github.com/llvm/llvm-project/pull/88897
>From d7a98d09798a0fb13a1c91a6b0f1616380d770f2 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` to utilize L Value
Reference Variant.
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 within the test to be an L Value reference, to correctly
utlize the correct variant of `DoNotOptimize` that allows for successful
comparison of the pointers.
---
.../new.delete.array/new.size_align.replace.indirect.pass.cpp | 3 ++-
.../new.size_align_nothrow.replace.indirect.pass.cpp | 3 ++-
.../new.size_align_nothrow.replace.indirect.pass.cpp | 3 ++-
3 files changed, 6 insertions(+), 3 deletions(-)
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp
index 0b540e09bab3cc..9446581b1709d1 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align.replace.indirect.pass.cpp
@@ -51,7 +51,8 @@ int main(int, char**) {
// Test with an overaligned type
{
new_called = delete_called = 0;
- OverAligned* x = DoNotOptimize(new OverAligned[3]);
+ OverAligned* dummy_data_block = new OverAligned[3];
+ OverAligned* x = DoNotOptimize(dummy_data_block);
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(static_cast<void*>(x) == DummyData);
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1);
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp
index 227b20f0b1e18b..6c12a9b65f129a 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.array/new.size_align_nothrow.replace.indirect.pass.cpp
@@ -55,7 +55,8 @@ int main(int, char**) {
// Test with an overaligned type
{
new_called = delete_called = 0;
- OverAligned* x = DoNotOptimize(new (std::nothrow) OverAligned[3]);
+ OverAligned* dummy_data_block = new (std::nothrow) OverAligned[3];
+ OverAligned* x = DoNotOptimize(dummy_data_block);
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(static_cast<void*>(x) == DummyData);
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1);
diff --git a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp
index 7eab0729f9ef1a..df0c454ecd3e17 100644
--- a/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp
+++ b/libcxx/test/std/language.support/support.dynamic/new.delete/new.delete.single/new.size_align_nothrow.replace.indirect.pass.cpp
@@ -54,7 +54,8 @@ int main(int, char**) {
// Test with an overaligned type
{
new_called = delete_called = 0;
- OverAligned* x = DoNotOptimize(new (std::nothrow) OverAligned);
+ OverAligned* dummy_data_block = new (std::nothrow) OverAligned;
+ OverAligned* x = DoNotOptimize(dummy_data_block);
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(static_cast<void*>(x) == DummyData);
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_called == 1);
>From ac2508d5017cc8d48fc3b854697860838f47ba12 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.array/new.size_align.replace.pass.cpp | 3 ++-
.../new.delete.single/new.size_align.replace.pass.cpp | 3 ++-
.../new.delete.single/new.size_align_nothrow.replace.pass.cpp | 3 ++-
3 files changed, 6 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..3a7b9c8ead76ac 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,8 @@ int main(int, char**) {
// Test with an overaligned type
{
new_called = delete_called = 0;
- OverAligned* x = new OverAligned[3];
+ OverAligned* dummy_data_block = new OverAligned[3];
+ OverAligned* x = DoNotOptimize(dummy_data_block);
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..3bc7c220301abb 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,8 @@ int main(int, char**) {
// Test with an overaligned type
{
new_called = delete_called = 0;
- OverAligned* x = new OverAligned;
+ OverAligned* dummy_data_block = new OverAligned;
+ OverAligned* x = DoNotOptimize(dummy_data_block);
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..374ee9675ea555 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,8 @@ int main(int, char**) {
// Test with an overaligned type
{
new_nothrow_called = delete_called = 0;
- OverAligned* x = new (std::nothrow) OverAligned;
+ OverAligned* dummy_data_block = new (std::nothrow) OverAligned;
+ OverAligned* x = DoNotOptimize(dummy_data_block);
assert(static_cast<void*>(x) == DummyData);
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_nothrow_called == 1);
>From 384ef88dd781abaf2e193f567179717ea68d7f05 Mon Sep 17 00:00:00 2001
From: Jack Styles <jack.styles at arm.com>
Date: Wed, 24 Apr 2024 16:59:07 +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 +-
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 3a7b9c8ead76ac..b5acd0d22bb0ae 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
@@ -49,7 +49,7 @@ int main(int, char**) {
{
new_called = delete_called = 0;
OverAligned* dummy_data_block = new OverAligned[3];
- OverAligned* x = DoNotOptimize(dummy_data_block);
+ OverAligned* x = DoNotOptimize(dummy_data_block);
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 3bc7c220301abb..8c8abdb4f77345 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
@@ -49,7 +49,7 @@ int main(int, char**) {
{
new_called = delete_called = 0;
OverAligned* dummy_data_block = new OverAligned;
- OverAligned* x = DoNotOptimize(dummy_data_block);
+ OverAligned* x = DoNotOptimize(dummy_data_block);
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 374ee9675ea555..bbac19cb00def3 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* dummy_data_block = new (std::nothrow) OverAligned;
+ OverAligned* dummy_data_block = new (std::nothrow) OverAligned;
OverAligned* x = DoNotOptimize(dummy_data_block);
assert(static_cast<void*>(x) == DummyData);
ASSERT_WITH_OPERATOR_NEW_FALLBACKS(new_nothrow_called == 1);
More information about the libcxx-commits
mailing list