From cabb0bc32ee394b51a4f3ff4fe7fac4df47fe573 Mon Sep 17 00:00:00 2001 From: Anastasia Stulova Date: Thu, 18 Jul 2019 09:12:49 +0000 Subject: [PATCH] [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 git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@366417 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Sema/SemaDecl.cpp | 5 ++++- lib/Sema/SemaType.cpp | 4 +++- lib/Sema/TreeTransform.h | 7 ------- test/SemaOpenCLCXX/address-space-deduction.cl | 15 +++++++++++++++ test/SemaOpenCLCXX/address-space-templates.cl | 2 +- 5 files changed, 23 insertions(+), 10 deletions(-) diff --git a/lib/Sema/SemaDecl.cpp b/lib/Sema/SemaDecl.cpp index cc91ec5946..72b4f6bbcd 100644 --- a/lib/Sema/SemaDecl.cpp +++ b/lib/Sema/SemaDecl.cpp @@ -7491,7 +7491,10 @@ void Sema::CheckVariableDeclarationType(VarDecl *NewVD) { return; } } - } else if (T.getAddressSpace() != LangAS::opencl_private) { + } else if (T.getAddressSpace() != LangAS::opencl_private && + // If we are parsing a template we didn't deduce an addr + // space yet. + T.getAddressSpace() != LangAS::Default) { // Do not allow other address spaces on automatic variable. Diag(NewVD->getLocation(), diag::err_as_qualified_auto_decl) << 1; NewVD->setInvalidDecl(); diff --git a/lib/Sema/SemaType.cpp b/lib/Sema/SemaType.cpp index bb71db7609..29acf6177e 100644 --- a/lib/Sema/SemaType.cpp +++ b/lib/Sema/SemaType.cpp @@ -7419,7 +7419,9 @@ static void deduceOpenCLImplicitAddrSpace(TypeProcessingState &State, (T->isVoidType() && !IsPointee) || // Do not deduce addr spaces for dependent types because they might end // up instantiating to a type with an explicit address space qualifier. - T->isDependentType() || + // Except for pointer or reference types because the addr space in + // template argument can only belong to a pointee. + (T->isDependentType() && !T->isPointerType() && !T->isReferenceType()) || // Do not deduce addr space of decltype because it will be taken from // its argument. T->isDecltypeType() || diff --git a/lib/Sema/TreeTransform.h b/lib/Sema/TreeTransform.h index 3b841ec649..8df18b5c27 100644 --- a/lib/Sema/TreeTransform.h +++ b/lib/Sema/TreeTransform.h @@ -5392,13 +5392,6 @@ QualType TreeTransform::TransformFunctionProtoType( if (ResultType.isNull()) return QualType(); - // Return type can not be qualified with an address space. - if (ResultType.getAddressSpace() != LangAS::Default) { - SemaRef.Diag(TL.getReturnLoc().getBeginLoc(), - diag::err_attribute_address_function_type); - return QualType(); - } - if (getDerived().TransformFunctionTypeParams( TL.getBeginLoc(), TL.getParams(), TL.getTypePtr()->param_type_begin(), diff --git a/test/SemaOpenCLCXX/address-space-deduction.cl b/test/SemaOpenCLCXX/address-space-deduction.cl index 08668951db..4283ec41e6 100644 --- a/test/SemaOpenCLCXX/address-space-deduction.cl +++ b/test/SemaOpenCLCXX/address-space-deduction.cl @@ -63,3 +63,18 @@ public: //CHECK: -CXXConstructorDecl {{.*}} x3 'void (const x3 &){{( __attribute__.*)?}} __generic' template x3::x3(const x3 &t) {} + +template +T xxx(T *in) { + // This pointer can't be deduced to generic because addr space + // will be taken from the template argument. + //CHECK: `-VarDecl {{.*}} i 'T *' cinit + T *i = in; + T ii; + return *i; +} + +__kernel void test() { + int foo[10]; + xxx(&foo[0]); +} diff --git a/test/SemaOpenCLCXX/address-space-templates.cl b/test/SemaOpenCLCXX/address-space-templates.cl index 48fbdc7642..3fb935766e 100644 --- a/test/SemaOpenCLCXX/address-space-templates.cl +++ b/test/SemaOpenCLCXX/address-space-templates.cl @@ -3,7 +3,7 @@ template struct S { T a; // expected-error{{field may not be qualified with an address space}} - T f1(); // expected-error{{function type may not be qualified with an address space}} + T f1(); // we ignore address space on a return types. void f2(T); // expected-error{{parameter may not be qualified with an address space}} }; -- 2.40.0