Skip to content

Commit

Permalink
[OpenCL] Initialize temporaries in the private address space
Browse files Browse the repository at this point in the history
This patch fixes initializing temporaries, which are currently initialized
without an address space, meaning that no constructor can ever be applicable.
Now they will be constructed in the private addrspace.

Fixes the second issue in PR43296.

Reviewed By: Anastasia

Differential Revision: https://reviews.llvm.org/D107553
  • Loading branch information
OleStrohm committed Sep 13, 2021
1 parent b01d223 commit 8008009
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 4 deletions.
11 changes: 9 additions & 2 deletions clang/include/clang/Sema/Initialization.h
Expand Up @@ -335,8 +335,15 @@ class alignas(8) InitializedEntity {
}

/// Create the initialization entity for a temporary.
static InitializedEntity InitializeTemporary(TypeSourceInfo *TypeInfo) {
return InitializeTemporary(TypeInfo, TypeInfo->getType());
static InitializedEntity InitializeTemporary(ASTContext &Context,
TypeSourceInfo *TypeInfo) {
QualType Type = TypeInfo->getType();
if (Context.getLangOpts().OpenCLCPlusPlus) {
assert(!Type.hasAddressSpace() && "Temporary already has address space!");
Type = Context.getAddrSpaceQualType(Type, LangAS::opencl_private);
}

return InitializeTemporary(TypeInfo, Type);
}

/// Create the initialization entity for a temporary.
Expand Down
6 changes: 4 additions & 2 deletions clang/lib/Sema/SemaExprCXX.cpp
Expand Up @@ -1453,7 +1453,8 @@ Sema::BuildCXXTypeConstructExpr(TypeSourceInfo *TInfo,
"List initialization must have initializer list as expression.");
SourceRange FullRange = SourceRange(TyBeginLoc, RParenOrBraceLoc);

InitializedEntity Entity = InitializedEntity::InitializeTemporary(TInfo);
InitializedEntity Entity =
InitializedEntity::InitializeTemporary(Context, TInfo);
InitializationKind Kind =
Exprs.size()
? ListInitialization
Expand Down Expand Up @@ -5290,7 +5291,8 @@ static bool evaluateTypeTrait(Sema &S, TypeTrait Kind, SourceLocation KWLoc,
S, Sema::ExpressionEvaluationContext::Unevaluated);
Sema::SFINAETrap SFINAE(S, /*AccessCheckingSFINAE=*/true);
Sema::ContextRAII TUContext(S, S.Context.getTranslationUnitDecl());
InitializedEntity To(InitializedEntity::InitializeTemporary(Args[0]));
InitializedEntity To(
InitializedEntity::InitializeTemporary(S.Context, Args[0]));
InitializationKind InitKind(InitializationKind::CreateDirect(KWLoc, KWLoc,
RParenLoc));
InitializationSequence Init(S, To, InitKind, ArgExprs);
Expand Down
2 changes: 2 additions & 0 deletions clang/test/SemaOpenCLCXX/addrspace-constructors.clcpp
Expand Up @@ -32,6 +32,8 @@ kernel void k() {
__local X lx;
__private X x;

__private X tx = X();

__private Y py;
__constant Y cy1; // expected-error{{variable in constant address space must be initialized}}
__constant Y cy2(1); // expected-error{{no matching constructor for initialization of '__constant Y'}}
Expand Down
19 changes: 19 additions & 0 deletions clang/test/SemaOpenCLCXX/temporaries.clcpp
@@ -0,0 +1,19 @@
// RUN: %clang_cc1 %s -pedantic -ast-dump | FileCheck %s

struct X {
X() __private = default;
};

// CHECK: VarDecl {{.*}} gx
// CHECK: CXXTemporaryObjectExpr {{.*}} '__private X'
__global X gx = X();

void k() {
// CHECK: VarDecl {{.*}} x1
// CHECK: CXXTemporaryObjectExpr {{.*}} '__private X'
X x1 = X();

// CHECK: VarDecl {{.*}} x2
// CHECK: CXXConstructExpr {{.*}} 'const __private X'
const X x2;
}

0 comments on commit 8008009

Please sign in to comment.