[clang] 769cc33 - [OpenCL] Simplify use of C11 atomic types.
Anastasia Stulova via cfe-commits
cfe-commits at lists.llvm.org
Fri May 14 09:43:22 PDT 2021
Author: Anastasia Stulova
Date: 2021-05-14T17:43:00+01:00
New Revision: 769cc335e6e63e5eac0c0ac849de44714326e20b
URL: https://github.com/llvm/llvm-project/commit/769cc335e6e63e5eac0c0ac849de44714326e20b
DIFF: https://github.com/llvm/llvm-project/commit/769cc335e6e63e5eac0c0ac849de44714326e20b.diff
LOG: [OpenCL] Simplify use of C11 atomic types.
Remove requirements on extension pragma in atomic types
because it has not respected the spec wrt disabling types
and hasn't been useful either. With this change, the
developers can use atomic types from the extensions if they
are supported without enabling the pragma just like the builtin
functions
This patch does not break backward compatibility since the
extension pragma is still supported and it makes the behavior of
the compiler less strict by accepting code without needless and
inconsistent pragma statements.
Differential Revision: https://reviews.llvm.org/D100976
Added:
Modified:
clang/lib/Sema/Sema.cpp
clang/test/Parser/opencl-atomics-cl20.cl
Removed:
################################################################################
diff --git a/clang/lib/Sema/Sema.cpp b/clang/lib/Sema/Sema.cpp
index 72e2ee613cbf4..36fcadfc9b9b7 100644
--- a/clang/lib/Sema/Sema.cpp
+++ b/clang/lib/Sema/Sema.cpp
@@ -313,8 +313,7 @@ void Sema::Initialize() {
// OpenCLC v2.0, s6.13.11.6 requires that atomic_flag is implemented as
// 32-bit integer and OpenCLC v2.0, s6.1.1 int is always 32-bit wide.
addImplicitTypedef("atomic_flag", Context.getAtomicType(Context.IntTy));
- auto AtomicSizeT = Context.getAtomicType(Context.getSizeType());
- addImplicitTypedef("atomic_size_t", AtomicSizeT);
+
// OpenCL v2.0 s6.13.11.6:
// - The atomic_long and atomic_ulong types are supported if the
@@ -327,6 +326,23 @@ void Sema::Initialize() {
// atomic_intptr_t, atomic_uintptr_t, atomic_size_t and
// atomic_ptr
diff _t are supported if the cl_khr_int64_base_atomics and
// cl_khr_int64_extended_atomics extensions are supported.
+
+ auto AddPointerSizeDependentTypes = [&]() {
+ auto AtomicSizeT = Context.getAtomicType(Context.getSizeType());
+ auto AtomicIntPtrT = Context.getAtomicType(Context.getIntPtrType());
+ auto AtomicUIntPtrT = Context.getAtomicType(Context.getUIntPtrType());
+ auto AtomicPtrDiffT =
+ Context.getAtomicType(Context.getPointerDiffType());
+ addImplicitTypedef("atomic_size_t", AtomicSizeT);
+ addImplicitTypedef("atomic_intptr_t", AtomicIntPtrT);
+ addImplicitTypedef("atomic_uintptr_t", AtomicUIntPtrT);
+ addImplicitTypedef("atomic_ptr
diff _t", AtomicPtrDiffT);
+ };
+
+ if (Context.getTypeSize(Context.getSizeType()) == 32) {
+ AddPointerSizeDependentTypes();
+ }
+
std::vector<QualType> Atomic64BitTypes;
if (getOpenCLOptions().isSupported("cl_khr_int64_base_atomics",
getLangOpts()) &&
@@ -335,35 +351,18 @@ void Sema::Initialize() {
if (getOpenCLOptions().isSupported("cl_khr_fp64", getLangOpts())) {
auto AtomicDoubleT = Context.getAtomicType(Context.DoubleTy);
addImplicitTypedef("atomic_double", AtomicDoubleT);
- setOpenCLExtensionForType(AtomicDoubleT, "cl_khr_fp64");
Atomic64BitTypes.push_back(AtomicDoubleT);
}
auto AtomicLongT = Context.getAtomicType(Context.LongTy);
auto AtomicULongT = Context.getAtomicType(Context.UnsignedLongTy);
- auto AtomicIntPtrT = Context.getAtomicType(Context.getIntPtrType());
- auto AtomicUIntPtrT = Context.getAtomicType(Context.getUIntPtrType());
- auto AtomicPtrDiffT =
- Context.getAtomicType(Context.getPointerDiffType());
-
addImplicitTypedef("atomic_long", AtomicLongT);
addImplicitTypedef("atomic_ulong", AtomicULongT);
- addImplicitTypedef("atomic_intptr_t", AtomicIntPtrT);
- addImplicitTypedef("atomic_uintptr_t", AtomicUIntPtrT);
- addImplicitTypedef("atomic_ptr
diff _t", AtomicPtrDiffT);
- Atomic64BitTypes.push_back(AtomicLongT);
- Atomic64BitTypes.push_back(AtomicULongT);
- if (Context.getTypeSize(AtomicSizeT) == 64) {
- Atomic64BitTypes.push_back(AtomicSizeT);
- Atomic64BitTypes.push_back(AtomicIntPtrT);
- Atomic64BitTypes.push_back(AtomicUIntPtrT);
- Atomic64BitTypes.push_back(AtomicPtrDiffT);
+
+ if (Context.getTypeSize(Context.getSizeType()) == 64) {
+ AddPointerSizeDependentTypes();
}
}
-
- for (auto &I : Atomic64BitTypes)
- setOpenCLExtensionForType(I,
- "cl_khr_int64_base_atomics cl_khr_int64_extended_atomics");
}
diff --git a/clang/test/Parser/opencl-atomics-cl20.cl b/clang/test/Parser/opencl-atomics-cl20.cl
index 2f5f913d28738..c3f86b6a44c49 100644
--- a/clang/test/Parser/opencl-atomics-cl20.cl
+++ b/clang/test/Parser/opencl-atomics-cl20.cl
@@ -1,66 +1,78 @@
// RUN: %clang_cc1 %s -triple spir-unknown-unknown -verify -pedantic -fsyntax-only
-// RUN: %clang_cc1 %s -triple spir-unknown-unknown -verify -fsyntax-only -cl-std=CL2.0 -DCL20
-// RUN: %clang_cc1 %s -triple spir64-unknown-unknown -verify -fsyntax-only -cl-std=CL2.0 -DCL20
-// RUN: %clang_cc1 %s -triple spir-unknown-unknown -verify -fsyntax-only -cl-std=CL2.0 -DCL20 -DEXT -Wpedantic-core-features
+// RUN: %clang_cc1 %s -triple spir-unknown-unknown -verify -fsyntax-only -cl-std=CL2.0 -cl-ext=-cl_khr_int64_base_atomics
+// RUN: %clang_cc1 %s -triple spir64-unknown-unknown -verify -fsyntax-only -cl-std=CL2.0
+// RUN: %clang_cc1 %s -triple spir64-unknown-unknown -verify -fsyntax-only -cl-std=CLC++
+// RUN: %clang_cc1 %s -triple spir64-unknown-unknown -verify -fsyntax-only -cl-std=CL2.0 -cl-ext=-cl_khr_int64_base_atomics
-#ifdef EXT
-#pragma OPENCL EXTENSION cl_khr_int64_base_atomics:enable
-#pragma OPENCL EXTENSION cl_khr_int64_extended_atomics:enable
-#pragma OPENCL EXTENSION cl_khr_fp64:enable
-#if __OPENCL_C_VERSION__ >= CL_VERSION_1_2
-// expected-warning at -2{{OpenCL extension 'cl_khr_fp64' is core feature or supported optional core feature - ignoring}}
-#endif
+#if defined(__OPENCL_CPP_VERSION__) || __OPENCL_C_VERSION__ >= CL_VERSION_1_2
+#define LANG_VER_OK
#endif
void atomic_types_test() {
// OpenCL v2.0 s6.13.11.6 defines supported atomic types.
+
+// Non-optional types
atomic_int i;
atomic_uint ui;
+ atomic_float f;
+ atomic_flag fl;
+#if !defined(LANG_VER_OK)
+// expected-error at -5 {{use of undeclared identifier 'atomic_int'}}
+// expected-error at -5 {{use of undeclared identifier 'atomic_uint'}}
+// expected-error at -5 {{use of undeclared identifier 'atomic_float'}}
+// expected-error at -5 {{use of undeclared identifier 'atomic_flag'}}
+#endif
+
+// Optional types
atomic_long l;
atomic_ulong ul;
- atomic_float f;
atomic_double d;
- atomic_flag fl;
+ atomic_size_t s;
atomic_intptr_t ip;
atomic_uintptr_t uip;
- atomic_size_t s;
atomic_ptr
diff _t pd;
-// OpenCL v2.0 s6.13.11.8, _Atomic type specifier and _Atomic type qualifier
-// are not supported by OpenCL.
- _Atomic int i; // expected-error {{use of undeclared identifier '_Atomic'}}
-}
-#ifndef CL20
-// expected-error at -16 {{use of undeclared identifier 'atomic_int'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_uint'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_long'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_ulong'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_float'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_double'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_flag'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_intptr_t'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_uintptr_t'}}
-// expected-error at -16 {{use of undeclared identifier 'atomic_size_t'}}
+// Optional type identifiers are not added in earlier version or if at least
+// one of the extensions is not supported. Here we check with
+// `cl_khr_int64_base_atomics` only.
+#if !defined(LANG_VER_OK) || !defined(cl_khr_int64_base_atomics)
+// expected-error at -11 {{use of undeclared identifier 'atomic_long'}}
+// expected-error at -11 {{use of undeclared identifier 'atomic_ulong'}}
+// expected-error at -11 {{use of undeclared identifier 'atomic_double'}}
+#if defined(LANG_VER_OK)
+// expected-error at -15 {{expected ';' after expression}}
+// expected-error at -16 {{use of undeclared identifier 'l'}}
+// expected-error at -16 {{expected ';' after expression}}
+// expected-error at -17 {{use of undeclared identifier 'ul'}}
+#endif
+#if !defined(LANG_VER_OK) || defined(__SPIR64__)
+// expected-error at -18 {{use of undeclared identifier 'atomic_size_t'}}
// expected-error at -16 {{use of undeclared identifier 'atomic_ptr
diff _t'}}
-#elif !EXT
-// expected-error at -26 {{use of type 'atomic_long' (aka '_Atomic(long)') requires cl_khr_int64_base_atomics support}}
-// expected-error at -27 {{use of type 'atomic_long' (aka '_Atomic(long)') requires cl_khr_int64_extended_atomics support}}
-// expected-error at -27 {{use of type 'atomic_ulong' (aka '_Atomic(unsigned long)') requires cl_khr_int64_base_atomics support}}
-// expected-error at -28 {{use of type 'atomic_ulong' (aka '_Atomic(unsigned long)') requires cl_khr_int64_extended_atomics support}}
-// expected-error at -27 {{use of type 'atomic_double' (aka '_Atomic(double)') requires cl_khr_int64_base_atomics support}}
-// expected-error at -28 {{use of type 'atomic_double' (aka '_Atomic(double)') requires cl_khr_int64_extended_atomics support}}
-#if __LP64__
-// expected-error-re at -28 {{use of type 'atomic_intptr_t' (aka '_Atomic({{.+}})') requires cl_khr_int64_base_atomics support}}
-// expected-error-re at -29 {{use of type 'atomic_intptr_t' (aka '_Atomic({{.+}})') requires cl_khr_int64_extended_atomics support}}
-// expected-error-re at -29 {{use of type 'atomic_uintptr_t' (aka '_Atomic({{.+}})') requires cl_khr_int64_base_atomics support}}
-// expected-error-re at -30 {{use of type 'atomic_uintptr_t' (aka '_Atomic({{.+}})') requires cl_khr_int64_extended_atomics support}}
-// expected-error-re at -30 {{use of type 'atomic_size_t' (aka '_Atomic({{.+}})') requires cl_khr_int64_base_atomics support}}
-// expected-error-re at -31 {{use of type 'atomic_size_t' (aka '_Atomic({{.+}})') requires cl_khr_int64_extended_atomics support}}
-// expected-error-re at -31 {{use of type 'atomic_ptr
diff _t' (aka '_Atomic({{.+}})') requires cl_khr_int64_base_atomics support}}
-// expected-error-re at -32 {{use of type 'atomic_ptr
diff _t' (aka '_Atomic({{.+}})') requires cl_khr_int64_extended_atomics support}}
+#if !defined(LANG_VER_OK)
+// expected-error at -20 {{use of undeclared identifier 'atomic_intptr_t'}}
+// expected-error at -20 {{use of undeclared identifier 'atomic_uintptr_t'}}
+#else
+// expected-error at -24 {{expected ';' after expression}}
+// expected-error at -25 {{use of undeclared identifier 's'}}
+// expected-error at -25 {{unknown type name 'atomic_intptr_t'; did you mean 'atomic_int'?}}
+// expected-note@* {{'atomic_int' declared here}}
+// expected-error at -26 {{unknown type name 'atomic_uintptr_t'; did you mean 'atomic_uint'?}}
+// expected-note@* {{'atomic_uint' declared here}}
+#endif
#endif
#endif
-#ifdef CL20
+// OpenCL v2.0 s6.13.11.8, _Atomic type specifier and _Atomic type qualifier
+// are not supported by OpenCL.
+ _Atomic int i;
+#ifdef __OPENCL_C_VERSION__
+// expected-error at -2 {{use of undeclared identifier '_Atomic'}}
+#else
+ // expected-error at -4 {{unknown type name '_Atomic'}}
+#endif
+}
+
+#if defined(LANG_VER_OK)
+int atomic_uint; //expected-error{{redefinition of 'atomic_uint' as
diff erent kind of symbol}}
void foo(atomic_int * ptr) {}
void atomic_ops_test() {
atomic_int i;
@@ -71,4 +83,6 @@ void atomic_ops_test() {
i += 1; // expected-error {{invalid operands to binary expression ('__private atomic_int' (aka '__private _Atomic(int)') and 'int')}}
i = i + i; // expected-error {{invalid operands to binary expression ('__private atomic_int' (aka '__private _Atomic(int)') and '__private atomic_int')}}
}
+#else
+__constant int atomic_uint = 1;
#endif
More information about the cfe-commits
mailing list