Skip to content

Commit 36d9e83

Browse files
author
Anastasia Stulova
committed
[OpenCL][PR42033] Fix addr space deduction with template parameters
If dependent types appear in pointers or references we allow addr space deduction because the addr space in template argument will belong to the pointee and not the pointer or reference itself. We also don't diagnose addr space on a function return type after template instantiation. If any addr space for the return type was provided on a template parameter this will be diagnosed during the parsing of template definition. Differential Revision: https://reviews.llvm.org/D62584 llvm-svn: 366417
1 parent f26706f commit 36d9e83

File tree

5 files changed

+23
-10
lines changed

5 files changed

+23
-10
lines changed

clang/lib/Sema/SemaDecl.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7491,7 +7491,10 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) {
74917491
return;
74927492
}
74937493
}
7494-
} else if (T.getAddressSpace() != LangAS::opencl_private) {
7494+
} else if (T.getAddressSpace() != LangAS::opencl_private &&
7495+
// If we are parsing a template we didn't deduce an addr
7496+
// space yet.
7497+
T.getAddressSpace() != LangAS::Default) {
74957498
// Do not allow other address spaces on automatic variable.
74967499
Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl) << 1;
74977500
NewVD->setInvalidDecl();

clang/lib/Sema/SemaType.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7419,7 +7419,9 @@ static void deduceOpenCLImplicitAddrSpace(TypeProcessingState &State,
74197419
(T->isVoidType() && !IsPointee) ||
74207420
// Do not deduce addr spaces for dependent types because they might end
74217421
// up instantiating to a type with an explicit address space qualifier.
7422-
T->isDependentType() ||
7422+
// Except for pointer or reference types because the addr space in
7423+
// template argument can only belong to a pointee.
7424+
(T->isDependentType() && !T->isPointerType() && !T->isReferenceType()) ||
74237425
// Do not deduce addr space of decltype because it will be taken from
74247426
// its argument.
74257427
T->isDecltypeType() ||

clang/lib/Sema/TreeTransform.h

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5392,13 +5392,6 @@ QualType TreeTransform<Derived>::TransformFunctionProtoType(
53925392
if (ResultType.isNull())
53935393
return QualType();
53945394

5395-
// Return type can not be qualified with an address space.
5396-
if (ResultType.getAddressSpace() != LangAS::Default) {
5397-
SemaRef.Diag(TL.getReturnLoc().getBeginLoc(),
5398-
diag::err_attribute_address_function_type);
5399-
return QualType();
5400-
}
5401-
54025395
if (getDerived().TransformFunctionTypeParams(
54035396
TL.getBeginLoc(), TL.getParams(),
54045397
TL.getTypePtr()->param_type_begin(),

clang/test/SemaOpenCLCXX/address-space-deduction.cl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,3 +63,18 @@ public:
6363
//CHECK: -CXXConstructorDecl {{.*}} x3<T> 'void (const x3<T> &){{( __attribute__.*)?}} __generic'
6464
template <typename T>
6565
x3<T>::x3(const x3<T> &t) {}
66+
67+
template <class T>
68+
T xxx(T *in) {
69+
// This pointer can't be deduced to generic because addr space
70+
// will be taken from the template argument.
71+
//CHECK: `-VarDecl {{.*}} i 'T *' cinit
72+
T *i = in;
73+
T ii;
74+
return *i;
75+
}
76+
77+
__kernel void test() {
78+
int foo[10];
79+
xxx(&foo[0]);
80+
}

clang/test/SemaOpenCLCXX/address-space-templates.cl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
template <typename T>
44
struct S {
55
T a; // expected-error{{field may not be qualified with an address space}}
6-
T f1(); // expected-error{{function type may not be qualified with an address space}}
6+
T f1(); // we ignore address space on a return types.
77
void f2(T); // expected-error{{parameter may not be qualified with an address space}}
88
};
99

0 commit comments

Comments
 (0)