From 96b94e610b84988a851214620b0ecd544584bab7 Mon Sep 17 00:00:00 2001 From: Joey Gouly Date: Fri, 3 Jan 2014 14:16:55 +0000 Subject: [PATCH] [OpenCL] Variables in the constant address space must be initialized. llvm-svn: 198417 --- .../clang/Basic/DiagnosticSemaKinds.td | 2 ++ clang/lib/Sema/SemaDecl.cpp | 10 +++++++ clang/test/Misc/languageOptsOpenCL.cl | 26 ++++++++++--------- clang/test/SemaOpenCL/event_t.cl | 2 +- clang/test/SemaOpenCL/invalid-kernel-attrs.cl | 6 ++--- 5 files changed, 30 insertions(+), 16 deletions(-) diff --git a/clang/include/clang/Basic/DiagnosticSemaKinds.td b/clang/include/clang/Basic/DiagnosticSemaKinds.td index f803ecbd52bb..f7017bf315f5 100644 --- a/clang/include/clang/Basic/DiagnosticSemaKinds.td +++ b/clang/include/clang/Basic/DiagnosticSemaKinds.td @@ -6714,6 +6714,8 @@ def err_opencl_global_invalid_addr_space : Error< 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">; +def err_opencl_constant_no_init : Error< + "variable in constant address space must be initialized">; } // end of sema category let CategoryName = "OpenMP Issue" in { diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp index 42e1564fb5cb..532aa2217705 100644 --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -8589,6 +8589,16 @@ void Sema::ActOnUninitializedDecl(Decl *RealDecl, return; } + // OpenCL v1.1 s6.5.3: variables declared in the constant address space must + // be initialized. + if (!Var->isInvalidDecl() && + Var->getType().getAddressSpace() == LangAS::opencl_constant && + !Var->getInit()) { + Diag(Var->getLocation(), diag::err_opencl_constant_no_init); + Var->setInvalidDecl(); + return; + } + switch (Var->isThisDeclarationADefinition()) { case VarDecl::Definition: if (!Var->isStaticDataMember() || !Var->getAnyInitializer()) diff --git a/clang/test/Misc/languageOptsOpenCL.cl b/clang/test/Misc/languageOptsOpenCL.cl index c81db995808f..82a8f3614f37 100644 --- a/clang/test/Misc/languageOptsOpenCL.cl +++ b/clang/test/Misc/languageOptsOpenCL.cl @@ -3,17 +3,19 @@ // Test the forced language options for OpenCL are set correctly. -__constant int v0[(sizeof(int) == 4) -1]; -__constant int v1[(__alignof(int) == 4) -1]; -__constant int v2[(sizeof(long) == 8) -1]; -__constant int v3[(__alignof(long) == 8) -1]; -__constant int v4[(sizeof(long long) == 16) -1]; -__constant int v5[(__alignof(long long) == 16) -1]; -__constant int v6[(sizeof(float) == 4) -1]; -__constant int v7[(__alignof(float) == 4) -1]; +kernel void test() { + int v0[(sizeof(int) == 4) - 1]; + int v1[(__alignof(int)== 4) - 1]; + int v2[(sizeof(long) == 8) - 1]; + int v3[(__alignof(long)== 8) - 1]; + int v4[(sizeof(long long) == 16) - 1]; + int v5[(__alignof(long long)== 16) - 1]; + int v6[(sizeof(float) == 4) - 1]; + int v7[(__alignof(float)== 4) - 1]; #pragma OPENCL EXTENSION cl_khr_fp64 : enable -__constant int v8[(sizeof(double)==8) -1]; -__constant int v9[(__alignof(double)==8) -1]; + int v8[(sizeof(double) == 8) - 1]; + int v9[(__alignof(double)== 8) - 1]; #pragma OPENCL EXTENSION cl_khr_fp16 : enable -__constant int v10[(sizeof(half) == 2) -1]; -__constant int v11[(__alignof(half) == 2) -1]; + int v10[(sizeof(half) == 2) - 1]; + int v11[(__alignof(half) == 2) - 1]; +} diff --git a/clang/test/SemaOpenCL/event_t.cl b/clang/test/SemaOpenCL/event_t.cl index 5ab5c1011a62..e09883948cc6 100644 --- a/clang/test/SemaOpenCL/event_t.cl +++ b/clang/test/SemaOpenCL/event_t.cl @@ -4,7 +4,7 @@ event_t glb_evt; // expected-error {{the event_t type cannot be used to declare constant struct evt_s { event_t evt; // expected-error {{the event_t type cannot be used to declare a structure or union field}} -} evt_str; +} evt_str = {0}; void foo(event_t evt); // expected-note {{passing argument to parameter 'evt' here}} diff --git a/clang/test/SemaOpenCL/invalid-kernel-attrs.cl b/clang/test/SemaOpenCL/invalid-kernel-attrs.cl index 81ce1227cbbb..83298369d3a7 100644 --- a/clang/test/SemaOpenCL/invalid-kernel-attrs.cl +++ b/clang/test/SemaOpenCL/invalid-kernel-attrs.cl @@ -20,11 +20,11 @@ __attribute__((work_group_size_hint(8,16,32))) void kernel9(){} // expected-erro __attribute__((vec_type_hint(char))) void kernel10(){} // expected-error {{attribute 'vec_type_hint' can only be applied to a kernel}} -constant int foo1 __attribute__((reqd_work_group_size(8,16,32))); // expected-error {{'reqd_work_group_size' attribute only applies to functions}} +constant int foo1 __attribute__((reqd_work_group_size(8,16,32))) = 0; // expected-error {{'reqd_work_group_size' attribute only applies to functions}} -constant int foo2 __attribute__((work_group_size_hint(8,16,32))); // expected-error {{'work_group_size_hint' attribute only applies to functions}} +constant int foo2 __attribute__((work_group_size_hint(8,16,32))) = 0; // expected-error {{'work_group_size_hint' attribute only applies to functions}} -constant int foo3 __attribute__((vec_type_hint(char))); // expected-error {{'vec_type_hint' attribute only applies to functions}} +constant int foo3 __attribute__((vec_type_hint(char))) = 0; // expected-error {{'vec_type_hint' attribute only applies to functions}} void f_kernel_image2d_t( kernel image2d_t image ) { // expected-error {{'kernel' attribute only applies to functions}} int __kernel x; // expected-error {{'__kernel' attribute only applies to functions}}