Skip to content

Commit d3ae87e

Browse files
author
Anastasia Stulova
committedMar 6, 2019
[PR40778] Add addr space conversion when binding reference to a temporary.
This change fixes temporary materialization to happen in the right (default) address space when binding to it a reference of different type. It adds address space conversion afterwards to match the addr space of a reference. Differential Revision: https://reviews.llvm.org/D58634 llvm-svn: 355499
1 parent 3f37538 commit d3ae87e

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed
 

‎clang/include/clang/AST/Type.h

+5
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,11 @@ class Qualifiers {
317317
qs.removeObjCLifetime();
318318
return qs;
319319
}
320+
Qualifiers withoutAddressSpace() const {
321+
Qualifiers qs = *this;
322+
qs.removeAddressSpace();
323+
return qs;
324+
}
320325

321326
bool hasObjCLifetime() const { return Mask & LifetimeMask; }
322327
ObjCLifetime getObjCLifetime() const {

‎clang/lib/Sema/SemaInit.cpp

+17-4
Original file line numberDiff line numberDiff line change
@@ -4760,7 +4760,15 @@ static void TryReferenceInitializationCore(Sema &S,
47604760
// copy-initialization (8.5). The reference is then bound to the
47614761
// temporary. [...]
47624762

4763-
InitializedEntity TempEntity = InitializedEntity::InitializeTemporary(cv1T1);
4763+
// Ignore address space of reference type at this point and perform address
4764+
// space conversion after the reference binding step.
4765+
QualType cv1T1IgnoreAS =
4766+
T1Quals.hasAddressSpace()
4767+
? S.Context.getQualifiedType(T1, T1Quals.withoutAddressSpace())
4768+
: cv1T1;
4769+
4770+
InitializedEntity TempEntity =
4771+
InitializedEntity::InitializeTemporary(cv1T1IgnoreAS);
47644772

47654773
// FIXME: Why do we use an implicit conversion here rather than trying
47664774
// copy-initialization?
@@ -4795,8 +4803,9 @@ static void TryReferenceInitializationCore(Sema &S,
47954803
// than, cv2; otherwise, the program is ill-formed.
47964804
unsigned T1CVRQuals = T1Quals.getCVRQualifiers();
47974805
unsigned T2CVRQuals = T2Quals.getCVRQualifiers();
4798-
if (RefRelationship == Sema::Ref_Related &&
4799-
(T1CVRQuals | T2CVRQuals) != T1CVRQuals) {
4806+
if ((RefRelationship == Sema::Ref_Related &&
4807+
(T1CVRQuals | T2CVRQuals) != T1CVRQuals) ||
4808+
!T1Quals.isAddressSpaceSupersetOf(T2Quals)) {
48004809
Sequence.SetFailed(InitializationSequence::FK_ReferenceInitDropsQualifiers);
48014810
return;
48024811
}
@@ -4810,7 +4819,11 @@ static void TryReferenceInitializationCore(Sema &S,
48104819
return;
48114820
}
48124821

4813-
Sequence.AddReferenceBindingStep(cv1T1, /*bindingTemporary=*/true);
4822+
Sequence.AddReferenceBindingStep(cv1T1IgnoreAS, /*bindingTemporary=*/true);
4823+
4824+
if (T1Quals.hasAddressSpace())
4825+
Sequence.AddQualificationConversionStep(cv1T1, isLValueRef ? VK_LValue
4826+
: VK_XValue);
48144827
}
48154828

48164829
/// Attempt character array initialization from a string literal
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
//RUN: %clang_cc1 %s -cl-std=c++ -triple spir -emit-llvm -o - | FileCheck %s
2+
3+
int bar(const unsigned int &i);
4+
// CHECK-LABEL: define spir_func void @_Z3foov()
5+
void foo() {
6+
// The generic addr space reference parameter object will be bound
7+
// to a temporary value allocated in private addr space. We need an
8+
// addrspacecast before passing the value to the function.
9+
// CHECK: [[REF:%.*]] = alloca i32
10+
// CHECK: store i32 1, i32* [[REF]]
11+
// CHECK: [[REG:%[0-9]+]] = addrspacecast i32* [[REF]] to i32 addrspace(4)*
12+
// CHECK: call spir_func i32 @_Z3barRU3AS4Kj(i32 addrspace(4)* nonnull dereferenceable(4) [[REG]])
13+
bar(1);
14+
}

0 commit comments

Comments
 (0)