1*6d33362dSJordan Rupprecht //===----------------------------------------------------------------------===//
2*6d33362dSJordan Rupprecht //
3*6d33362dSJordan Rupprecht // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4*6d33362dSJordan Rupprecht // See https://llvm.org/LICENSE.txt for license information.
5*6d33362dSJordan Rupprecht // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6*6d33362dSJordan Rupprecht //
7*6d33362dSJordan Rupprecht //===----------------------------------------------------------------------===//
8*6d33362dSJordan Rupprecht 
9*6d33362dSJordan Rupprecht // REQUIRES: clang
10*6d33362dSJordan Rupprecht 
11*6d33362dSJordan Rupprecht // Adding "-fsanitize=thread" directly causes many platforms to fail (because
12*6d33362dSJordan Rupprecht // they don't support tsan), and causes other sanitizer builds to fail (e.g.
13*6d33362dSJordan Rupprecht // asan and tsan don't mix). Instead, require the tsan feature.
14*6d33362dSJordan Rupprecht // REQUIRES: tsan
15*6d33362dSJordan Rupprecht 
16*6d33362dSJordan Rupprecht // This test verifies behavior specified by [atomics.types.operations.req]/21:
17*6d33362dSJordan Rupprecht //
18*6d33362dSJordan Rupprecht //     When only one memory_order argument is supplied, the value of success is
19*6d33362dSJordan Rupprecht //     order, and the value of failure is order except that a value of
20*6d33362dSJordan Rupprecht //     memory_order_acq_rel shall be replaced by the value memory_order_acquire
21*6d33362dSJordan Rupprecht //     and a value of memory_order_release shall be replaced by the value
22*6d33362dSJordan Rupprecht //     memory_order_relaxed.
23*6d33362dSJordan Rupprecht //
24*6d33362dSJordan Rupprecht // This test mirrors replace_failure_order.pass.cpp. However, we also want to
25*6d33362dSJordan Rupprecht // verify the codegen is correct. This verifies a bug where memory_order_acq_rel
26*6d33362dSJordan Rupprecht // was not being replaced with memory_order_acquire in external
27*6d33362dSJordan Rupprecht // TSAN-instrumented tests.
28*6d33362dSJordan Rupprecht 
29*6d33362dSJordan Rupprecht // RUN: %{cxx} -c %s %{flags} %{compile_flags} -O2 -stdlib=libc++ -S -emit-llvm -o %t.ll
30*6d33362dSJordan Rupprecht 
31*6d33362dSJordan Rupprecht #include <atomic>
32*6d33362dSJordan Rupprecht 
33*6d33362dSJordan Rupprecht // Note: libc++ tests do not use on FileCheck.
34*6d33362dSJordan Rupprecht // RUN: grep -E "call i32 @__tsan_atomic32_compare_exchange_val\(.*, i32 1, i32 4, i32 2\)" %t.ll
strong_memory_order_acq_rel(std::atomic<int> * a,int cmp)35*6d33362dSJordan Rupprecht bool strong_memory_order_acq_rel(std::atomic<int>* a, int cmp) {
36*6d33362dSJordan Rupprecht   return a->compare_exchange_strong(cmp, 1, std::memory_order_acq_rel);
37*6d33362dSJordan Rupprecht }
38