[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:15:55 PDT 2024


================
@@ -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);
----------------
Stylie777 wrote:

The change to `&&value` was not the correct change, so have reverted this back to `&value` for `DoNotOptimize`.

In these test cases, we have found that the `const&` overload was not allowing for comparison of the pointers correctly, even when the values were the same. See explanation from @ostannard below:

"think that's because DoNotOptimize only uses the value as an input. This prevents the compiler from removing and data-flow leading up to it, but it can still "see" that one pointer came from new, and the other came from the address of a global, so they "can't" alias
This works for me, using both input and output from the inline asm, so the compiler can't follow the data-flow through it:"
```
template <class Tp>
inline Tp DoNotOptimize(Tp value) {
    asm volatile("" : "+r,m"(value) : : "memory");
    return value;
}
```

I have now scaled back the changes to only apply to the tests rather than change the function itself.



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


More information about the libcxx-commits mailing list