diff --git a/clang/lib/Sema/SemaDecl.cpp b/clang/lib/Sema/SemaDecl.cpp --- a/clang/lib/Sema/SemaDecl.cpp +++ b/clang/lib/Sema/SemaDecl.cpp @@ -6290,6 +6290,8 @@ void Sema::deduceOpenCLAddressSpace(ValueDecl *Decl) { if (Decl->getType().hasAddressSpace()) return; + if (Decl->getType()->isDependentType()) + return; if (VarDecl *Var = dyn_cast(Decl)) { QualType Type = Var->getType(); if (Type->isSamplerT() || Type->isVoidType()) @@ -7859,6 +7861,7 @@ if (NewVD->isFileVarDecl() || NewVD->isStaticLocal() || NewVD->hasExternalStorage()) { if (!T->isSamplerT() && + !T->isDependentType() && !(T.getAddressSpace() == LangAS::opencl_constant || (T.getAddressSpace() == LangAS::opencl_global && (getLangOpts().OpenCLVersion == 200 || diff --git a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp --- a/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp +++ b/clang/lib/Sema/SemaTemplateInstantiateDecl.cpp @@ -3625,6 +3625,9 @@ if (InsertPos) VarTemplate->AddSpecialization(Var, InsertPos); + if (SemaRef.getLangOpts().OpenCL) + SemaRef.deduceOpenCLAddressSpace(Var); + // Substitute the nested name specifier, if any. if (SubstQualifier(D, Var)) return nullptr; @@ -4895,6 +4898,9 @@ // Instantiate the initializer. InstantiateVariableInitializer(VarSpec, PatternDecl, TemplateArgs); + if (getLangOpts().OpenCL) + deduceOpenCLAddressSpace(VarSpec); + return VarSpec; } diff --git a/clang/test/SemaOpenCLCXX/address-space-deduction.cl b/clang/test/SemaOpenCLCXX/address-space-deduction.cl --- a/clang/test/SemaOpenCLCXX/address-space-deduction.cl +++ b/clang/test/SemaOpenCLCXX/address-space-deduction.cl @@ -5,6 +5,11 @@ //CHECK: |-VarDecl {{.*}} foo 'const __global int' constexpr int foo = 0; +//CHECK: |-VarDecl {{.*}} foo1 'T' cinit +//CHECK: `-VarTemplateSpecializationDecl {{.*}} used foo1 '__global long':'__global long' cinit +template +T foo1 = 0; + class c { public: //CHECK: `-VarDecl {{.*}} foo2 'const __global int' @@ -30,7 +35,7 @@ template struct x1 { -//CHECK: -CXXMethodDecl {{.*}} operator= 'x1 &(const x1 &__private){{( __attribute__.*)?}} __generic' +//CHECK: -CXXMethodDecl {{.*}} operator= 'x1 &(const x1 &){{( __attribute__.*)?}} __generic' //CHECK: -CXXMethodDecl {{.*}} operator= '__generic x1 &(const __generic x1 &__private){{( __attribute__.*)?}} __generic' x1& operator=(const x1& xx) { y = xx.y; @@ -41,7 +46,7 @@ template struct x2 { -//CHECK: -CXXMethodDecl {{.*}} foo 'void (x1 *__private){{( __attribute__.*)?}} __generic' +//CHECK: -CXXMethodDecl {{.*}} foo 'void (x1 *){{( __attribute__.*)?}} __generic' //CHECK: -CXXMethodDecl {{.*}} foo 'void (__generic x1 *__private){{( __attribute__.*)?}} __generic' void foo(x1* xx) { m[0] = *xx; @@ -57,10 +62,10 @@ template class x3 : public T { public: - //CHECK: -CXXConstructorDecl {{.*}} x3 'void (const x3 &__private){{( __attribute__.*)?}} __generic' + //CHECK: -CXXConstructorDecl {{.*}} x3 'void (const x3 &){{( __attribute__.*)?}} __generic' x3(const x3 &t); }; -//CHECK: -CXXConstructorDecl {{.*}} x3 'void (const x3 &__private){{( __attribute__.*)?}} __generic' +//CHECK: -CXXConstructorDecl {{.*}} x3 'void (const x3 &){{( __attribute__.*)?}} __generic' template x3::x3(const x3 &t) {} @@ -68,7 +73,8 @@ T xxx(T *in1, T in2) { // This pointer can't be deduced to generic because addr space // will be taken from the template argument. - //CHECK: `-VarDecl {{.*}} '__private T *__private' cinit + //CHECK: `-VarDecl {{.*}} 'T *' cinit + //CHECK: `-VarDecl {{.*}} i '__private int *__private' cinit T *i = in1; T ii; __private T *ptr = ⅈ @@ -111,4 +117,5 @@ t3(&x); t4(&p); t5(&p); + long f1 = foo1; }