From: Anastasia Stulova Date: Wed, 30 Sep 2015 14:08:20 +0000 (+0000) Subject: [OpenCL 2.0] Enable program scope variables, Section 6.5.1. X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=9dc179d618b695b365ca457b3a378a17cb2a4d49;p=clang [OpenCL 2.0] Enable program scope variables, Section 6.5.1. - Remove virtual SC_OpenCLWorkGroupLocal storage type specifier as it conflicts with static local variables now and prevents diagnosing static local address space variables correctly. - Allow static local and global variables (OpenCL2.0 s6.8 and s6.5.1). - Improve diagnostics of allowed ASes for variables in different scopes: (i) Global or static local variables have to be in global or constant ASes (OpenCL1.2 s6.5, OpenCL2.0 s6.5.1); (ii) Non-kernel function variables can't be declared in local or constant ASes (OpenCL1.1 s6.5.2 and s6.5.3). http://reviews.llvm.org/D13105 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@248906 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/clang/Basic/DiagnosticSemaKinds.td b/include/clang/Basic/DiagnosticSemaKinds.td index 455cdfa7f2..cac97ce05f 100644 --- a/include/clang/Basic/DiagnosticSemaKinds.td +++ b/include/clang/Basic/DiagnosticSemaKinds.td @@ -7445,6 +7445,8 @@ def err_opencl_ptrptr_kernel_param : Error< "kernel parameter cannot be declared as a pointer to a pointer">; def err_opencl_private_ptr_kernel_param : Error< "kernel parameter cannot be declared as a pointer to the __private address space">; +def err_opencl_non_kernel_variable : Error< + "non-kernel function variable cannot be declared in %0 address space">; def err_static_function_scope : Error< "variables in function scope cannot be declared static">; def err_opencl_bitfields : Error< @@ -7472,7 +7474,7 @@ def err_sampler_argument_required : Error< def err_wrong_sampler_addressspace: Error< "sampler type cannot be used with the __local and __global address space qualifiers">; def err_opencl_global_invalid_addr_space : Error< - "global variables must have a constant address space qualifier">; + "program scope variable must reside in %0 address space">; def err_opencl_no_main : Error<"%select{function|kernel}0 cannot be called 'main'">; def err_opencl_kernel_attr : Error<"attribute %0 can only be applied to a kernel function">; diff --git a/include/clang/Basic/Specifiers.h b/include/clang/Basic/Specifiers.h index d95a77f88e..47d2f31c0f 100644 --- a/include/clang/Basic/Specifiers.h +++ b/include/clang/Basic/Specifiers.h @@ -178,7 +178,6 @@ namespace clang { SC_PrivateExtern, // These are only legal on variables. - SC_OpenCLWorkGroupLocal, SC_Auto, SC_Register }; diff --git a/lib/AST/Decl.cpp b/lib/AST/Decl.cpp index 8cd2e17af8..594cfa359c 100644 --- a/lib/AST/Decl.cpp +++ b/lib/AST/Decl.cpp @@ -1749,7 +1749,6 @@ const char *VarDecl::getStorageClassSpecifierString(StorageClass SC) { case SC_None: break; case SC_Auto: return "auto"; case SC_Extern: return "extern"; - case SC_OpenCLWorkGroupLocal: return "<>"; case SC_PrivateExtern: return "__private_extern__"; case SC_Register: return "register"; case SC_Static: return "static"; diff --git a/lib/AST/DeclPrinter.cpp b/lib/AST/DeclPrinter.cpp index 2c3cee2993..374c34ae8e 100644 --- a/lib/AST/DeclPrinter.cpp +++ b/lib/AST/DeclPrinter.cpp @@ -416,7 +416,7 @@ void DeclPrinter::VisitFunctionDecl(FunctionDecl *D) { case SC_Extern: Out << "extern "; break; case SC_Static: Out << "static "; break; case SC_PrivateExtern: Out << "__private_extern__ "; break; - case SC_Auto: case SC_Register: case SC_OpenCLWorkGroupLocal: + case SC_Auto: case SC_Register: llvm_unreachable("invalid for functions"); } diff --git a/lib/CodeGen/CGDecl.cpp b/lib/CodeGen/CGDecl.cpp index 6570785972..07684f9144 100644 --- a/lib/CodeGen/CGDecl.cpp +++ b/lib/CodeGen/CGDecl.cpp @@ -143,7 +143,7 @@ void CodeGenFunction::EmitVarDecl(const VarDecl &D) { // Don't emit it now, allow it to be emitted lazily on its first use. return; - if (D.getStorageClass() == SC_OpenCLWorkGroupLocal) + if (D.getType().getAddressSpace() == LangAS::opencl_local) return CGM.getOpenCLRuntime().EmitWorkGroupLocalVarDecl(*this, D); assert(D.hasLocalStorage()); diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index 592f393d46..571c9baa04 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -5687,12 +5687,6 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, } if (getLangOpts().OpenCL) { - // Set up the special work-group-local storage class for variables in the - // OpenCL __local address space. - if (R.getAddressSpace() == LangAS::opencl_local) { - SC = SC_OpenCLWorkGroupLocal; - } - // OpenCL v1.2 s6.9.b p4: // The sampler type cannot be used with the __local and __global address // space qualifiers. @@ -5759,8 +5753,6 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, break; case SC_PrivateExtern: llvm_unreachable("C storage class in c++!"); - case SC_OpenCLWorkGroupLocal: - llvm_unreachable("OpenCL storage class in c++!"); } } @@ -6034,7 +6026,6 @@ Sema::ActOnVariableDeclarator(Scope *S, Declarator &D, DeclContext *DC, case SC_Static: case SC_Extern: case SC_PrivateExtern: - case SC_OpenCLWorkGroupLocal: break; } } else if (SC == SC_Register) { @@ -6428,31 +6419,79 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) { // This includes arrays of objects with address space qualifiers, but not // automatic variables that point to other address spaces. // ISO/IEC TR 18037 S5.1.2 - if (NewVD->hasLocalStorage() && T.getAddressSpace() != 0) { + if (!getLangOpts().OpenCL + && NewVD->hasLocalStorage() && T.getAddressSpace() != 0) { Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl); NewVD->setInvalidDecl(); return; } - // OpenCL v1.2 s6.5 - All program scope variables must be declared in the - // __constant address space. - if (getLangOpts().OpenCL && NewVD->isFileVarDecl() - && T.getAddressSpace() != LangAS::opencl_constant - && !T->isSamplerT()){ - Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space); - NewVD->setInvalidDecl(); - return; - } - // OpenCL v1.2 s6.8 -- The static qualifier is valid only in program // scope. - if ((getLangOpts().OpenCLVersion >= 120) - && NewVD->isStaticLocal()) { + if (getLangOpts().OpenCLVersion == 120 && + !getOpenCLOptions().cl_clang_storage_class_specifiers && + NewVD->isStaticLocal()) { Diag(NewVD->getLocation(), diag::err_static_function_scope); NewVD->setInvalidDecl(); return; } + // OpenCL v1.2 s6.5 - All program scope variables must be declared in the + // __constant address space. + // OpenCL v2.0 s6.5.1 - Variables defined at program scope and static + // variables inside a function can also be declared in the global + // address space. + if (getLangOpts().OpenCL) { + if (NewVD->isFileVarDecl()) { + if (!T->isSamplerT() && + !(T.getAddressSpace() == LangAS::opencl_constant || + (T.getAddressSpace() == LangAS::opencl_global && + getLangOpts().OpenCLVersion == 200))) { + if (getLangOpts().OpenCLVersion == 200) + Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space) + << "global or constant"; + else + Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space) + << "constant"; + NewVD->setInvalidDecl(); + return; + } + } else { + // OpenCL v2.0 s6.5.1 - Variables defined at program scope and static + // variables inside a function can also be declared in the global + // address space. + if (NewVD->isStaticLocal() && + !(T.getAddressSpace() == LangAS::opencl_constant || + (T.getAddressSpace() == LangAS::opencl_global && + getLangOpts().OpenCLVersion == 200))) { + if (getLangOpts().OpenCLVersion == 200) + Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space) + << "global or constant"; + else + Diag(NewVD->getLocation(), diag::err_opencl_global_invalid_addr_space) + << "constant"; + NewVD->setInvalidDecl(); + return; + } + // OpenCL v1.1 s6.5.2 and s6.5.3 no local or constant variables + // in functions. + if (T.getAddressSpace() == LangAS::opencl_constant || + T.getAddressSpace() == LangAS::opencl_local) { + FunctionDecl *FD = getCurFunctionDecl(); + if (FD && !FD->hasAttr()) { + if (T.getAddressSpace() == LangAS::opencl_constant) + Diag(NewVD->getLocation(), diag::err_opencl_non_kernel_variable) + << "constant"; + else + Diag(NewVD->getLocation(), diag::err_opencl_non_kernel_variable) + << "local"; + NewVD->setInvalidDecl(); + return; + } + } + } + } + if (NewVD->hasLocalStorage() && T.isObjCGCWeak() && !NewVD->hasAttr()) { if (getLangOpts().getGC() != LangOptions::NonGC) @@ -9158,7 +9197,7 @@ void Sema::AddInitializerToDecl(Decl *RealDecl, Expr *Init, // OpenCL 1.1 6.5.2: "Variables allocated in the __local address space inside // a kernel function cannot be initialized." - if (VDecl->getStorageClass() == SC_OpenCLWorkGroupLocal) { + if (VDecl->getType().getAddressSpace() == LangAS::opencl_local) { Diag(VDecl->getLocation(), diag::err_local_cant_init); VDecl->setInvalidDecl(); return; @@ -9729,8 +9768,6 @@ void Sema::ActOnCXXForRangeDecl(Decl *D) { case SC_Register: Error = 4; break; - case SC_OpenCLWorkGroupLocal: - llvm_unreachable("Unexpected storage class"); } if (Error != -1) { Diag(VD->getOuterLocStart(), diag::err_for_range_storage_class) diff --git a/test/Parser/opencl-storage-class.cl b/test/Parser/opencl-storage-class.cl index 874329b62d..3d9aef59f0 100644 --- a/test/Parser/opencl-storage-class.cl +++ b/test/Parser/opencl-storage-class.cl @@ -8,7 +8,7 @@ void test_storage_class_specs() auto int d; // expected-error {{OpenCL does not support the 'auto' storage class specifier}} #pragma OPENCL EXTENSION cl_clang_storage_class_specifiers : enable - static int e; + static int e; // expected-error {{program scope variable must reside in constant address space}} register int f; extern int g; auto int h; diff --git a/test/SemaOpenCL/storageclass.cl b/test/SemaOpenCL/storageclass.cl index d2678f2010..c7d8ab9846 100644 --- a/test/SemaOpenCL/storageclass.cl +++ b/test/SemaOpenCL/storageclass.cl @@ -1,14 +1,29 @@ // RUN: %clang_cc1 %s -verify -pedantic -fsyntax-only -cl-std=CL1.2 -static constant int A = 0; +static constant int G1 = 0; +constant int G2 = 0; +int G3 = 0; // expected-error{{program scope variable must reside in constant address space}} +global int G4 = 0; // expected-error{{program scope variable must reside in constant address space}} -int X = 0; // expected-error{{global variables must have a constant address space qualifier}} - -// static is not allowed at local scope. void kernel foo() { - static int X = 5; // expected-error{{variables in function scope cannot be declared static}} - auto int Y = 7; // expected-error{{OpenCL does not support the 'auto' storage class specifier}} + // static is not allowed at local scope before CL2.0 + static int S1 = 5; // expected-error{{variables in function scope cannot be declared static}} + static constant int S2 = 5; // expected-error{{variables in function scope cannot be declared static}} + + constant int L1 = 0; + local int L2; + + auto int L3 = 7; // expected-error{{OpenCL does not support the 'auto' storage class specifier}} } static void kernel bar() { // expected-error{{kernel functions cannot be declared static}} } + +void f() { + constant int L1 = 0; // expected-error{{non-kernel function variable cannot be declared in constant address space}} + local int L2; // expected-error{{non-kernel function variable cannot be declared in local address space}} + { + constant int L1 = 0; // expected-error{{non-kernel function variable cannot be declared in constant address space}} + local int L2; // expected-error{{non-kernel function variable cannot be declared in local address space}} + } +} diff --git a/tools/libclang/CIndex.cpp b/tools/libclang/CIndex.cpp index e34ee346fc..dfbb5a1c6b 100644 --- a/tools/libclang/CIndex.cpp +++ b/tools/libclang/CIndex.cpp @@ -6602,8 +6602,6 @@ enum CX_StorageClass clang_Cursor_getStorageClass(CXCursor C) { return CX_SC_Static; case SC_PrivateExtern: return CX_SC_PrivateExtern; - case SC_OpenCLWorkGroupLocal: - return CX_SC_OpenCLWorkGroupLocal; case SC_Auto: return CX_SC_Auto; case SC_Register: