Skip to content

Commit 3bccec5

Browse files
committedApr 5, 2017
[OpenCL] Extended diagnostics for atomic initialization
Summary: I saw the same changes in the following review: https://reviews.llvm.org/D17438 I don't know in that way I could determine that atomic variable was initialized by macro ATOMIC_VAR_INIT. Anyway I added check that atomic variables can be initialize only in global scope. I think that we can discuss this change. Reviewers: Anastasia, cfe-commits Reviewed By: Anastasia Subscribers: bader, yaxunl Differential Revision: https://reviews.llvm.org/D30643 llvm-svn: 299537
1 parent 34e2978 commit 3bccec5

File tree

5 files changed

+31
-5
lines changed

5 files changed

+31
-5
lines changed
 

‎clang/include/clang/Basic/DiagnosticSemaKinds.td

+3-3
Original file line numberDiff line numberDiff line change
@@ -8286,9 +8286,9 @@ def err_opencl_return_value_with_address_space : Error<
82868286
"return value cannot be qualified with address space">;
82878287
def err_opencl_constant_no_init : Error<
82888288
"variable in constant address space must be initialized">;
8289-
def err_atomic_init_constant : Error<
8290-
"atomic variable can only be assigned to a compile time constant"
8291-
" in the declaration statement in the program scope">;
8289+
def err_opencl_atomic_init: Error<
8290+
"atomic variable can be %select{assigned|initialized}0 to a variable only "
8291+
"in global address space">;
82928292
def err_opencl_implicit_vector_conversion : Error<
82938293
"implicit conversions between vector types (%0 and %1) are not permitted">;
82948294
def err_opencl_invalid_type_array : Error<

‎clang/lib/Sema/SemaExpr.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -11121,7 +11121,7 @@ ExprResult Sema::CreateBuiltinBinOp(SourceLocation OpLoc,
1112111121
if (LHSTy->isAtomicType() || RHSTy->isAtomicType()) {
1112211122
SourceRange SR(LHSExpr->getLocStart(), RHSExpr->getLocEnd());
1112311123
if (BO_Assign == Opc)
11124-
Diag(OpLoc, diag::err_atomic_init_constant) << SR;
11124+
Diag(OpLoc, diag::err_opencl_atomic_init) << 0 << SR;
1112511125
else
1112611126
ResultTy = InvalidOperands(OpLoc, LHS, RHS);
1112711127
return ExprError();

‎clang/lib/Sema/SemaInit.cpp

+14
Original file line numberDiff line numberDiff line change
@@ -6502,6 +6502,20 @@ InitializationSequence::Perform(Sema &S,
65026502
<< Init->getSourceRange();
65036503
}
65046504

6505+
// OpenCL v2.0 s6.13.11.1. atomic variables can be initialized in global scope
6506+
QualType ETy = Entity.getType();
6507+
Qualifiers TyQualifiers = ETy.getQualifiers();
6508+
bool HasGlobalAS = TyQualifiers.hasAddressSpace() &&
6509+
TyQualifiers.getAddressSpace() == LangAS::opencl_global;
6510+
6511+
if (S.getLangOpts().OpenCLVersion >= 200 &&
6512+
ETy->isAtomicType() && !HasGlobalAS &&
6513+
Entity.getKind() == InitializedEntity::EK_Variable && Args.size() > 0) {
6514+
S.Diag(Args[0]->getLocStart(), diag::err_opencl_atomic_init) << 1 <<
6515+
SourceRange(Entity.getDecl()->getLocStart(), Args[0]->getLocEnd());
6516+
return ExprError();
6517+
}
6518+
65056519
// Diagnose cases where we initialize a pointer to an array temporary, and the
65066520
// pointer obviously outlives the temporary.
65076521
if (Args.size() == 1 && Args[0]->getType()->isArrayType() &&

‎clang/test/Parser/opencl-atomics-cl20.cl

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void atomic_ops_test() {
6767
foo(&i);
6868
// OpenCL v2.0 s6.13.11.8, arithemtic operations are not permitted on atomic types.
6969
i++; // expected-error {{invalid argument type 'atomic_int' (aka '_Atomic(int)') to unary expression}}
70-
i = 1; // expected-error {{atomic variable can only be assigned to a compile time constant in the declaration statement in the program scope}}
70+
i = 1; // expected-error {{atomic variable can be assigned to a variable only in global address space}}
7171
i += 1; // expected-error {{invalid operands to binary expression ('atomic_int' (aka '_Atomic(int)') and 'int')}}
7272
i = i + i; // expected-error {{invalid operands to binary expression ('atomic_int' (aka '_Atomic(int)') and 'atomic_int')}}
7373
}

‎clang/test/SemaOpenCL/atomic-init.cl

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// RUN: %clang_cc1 -O0 -cl-std=CL2.0 -fsyntax-only -verify %s
2+
3+
global atomic_int a1 = 0;
4+
5+
kernel void test_atomic_initialization() {
6+
a1 = 1; // expected-error {{atomic variable can be assigned to a variable only in global address space}}
7+
atomic_int a2 = 0; // expected-error {{atomic variable can be initialized to a variable only in global address space}}
8+
private atomic_int a3 = 0; // expected-error {{atomic variable can be initialized to a variable only in global address space}}
9+
local atomic_int a4 = 0; // expected-error {{'__local' variable cannot have an initializer}}
10+
global atomic_int a5 = 0; // expected-error {{function scope variable cannot be declared in global address space}}
11+
static global atomic_int a6 = 0;
12+
}

0 commit comments

Comments
 (0)
Please sign in to comment.