Skip to content

Commit 35388dc

Browse files
committed
[OpenCL] Fix address space for base method call (PR43145)
Clang was creating an UncheckedDerivedToBase ImplicitCastExpr that was also casting between address spaces. Insert an ImplicitCastExpr node for doing the address space conversion. Differential Revision: https://reviews.llvm.org/D69810
1 parent 5cf5876 commit 35388dc

File tree

2 files changed

+35
-2
lines changed

2 files changed

+35
-2
lines changed

clang/lib/Sema/SemaExpr.cpp

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2711,6 +2711,20 @@ Sema::PerformObjectMemberConversion(Expr *From,
27112711
FromRecordType = FromType;
27122712
DestType = DestRecordType;
27132713
}
2714+
2715+
LangAS FromAS = FromRecordType.getAddressSpace();
2716+
LangAS DestAS = DestRecordType.getAddressSpace();
2717+
if (FromAS != DestAS) {
2718+
QualType FromRecordTypeWithoutAS =
2719+
Context.removeAddrSpaceQualType(FromRecordType);
2720+
QualType FromTypeWithDestAS =
2721+
Context.getAddrSpaceQualType(FromRecordTypeWithoutAS, DestAS);
2722+
if (PointerConversions)
2723+
FromTypeWithDestAS = Context.getPointerType(FromTypeWithDestAS);
2724+
From = ImpCastExprToType(From, FromTypeWithDestAS,
2725+
CK_AddressSpaceConversion, From->getValueKind())
2726+
.get();
2727+
}
27142728
} else {
27152729
// No conversion necessary.
27162730
return From;

clang/test/CodeGenOpenCLCXX/addrspace-derived-base.cl

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ void foo() {
2828
class B2 {
2929
public:
3030
void baseMethod() const { }
31+
int &getRef() { return bb; }
3132
int bb;
3233
};
3334

@@ -46,7 +47,25 @@ void pr43145(const Derived *argDerived) {
4647

4748
void pr43145_2(B *argB) {
4849
Derived *x = (Derived*)argB;
50+
// CHECK-LABEL: @_Z9pr43145_2
51+
// CHECK: bitcast %struct.B addrspace(4)* %0 to %class.Derived addrspace(4)*
4952
}
5053

51-
// CHECK-LABEL: @_Z9pr43145_2
52-
// CHECK: bitcast %struct.B addrspace(4)* %0 to %class.Derived addrspace(4)*
54+
// Assigning to reference returned by base class method through derived class.
55+
56+
void pr43145_3(int n) {
57+
Derived d;
58+
d.getRef() = n;
59+
60+
// CHECK-LABEL: @_Z9pr43145_3
61+
// CHECK: addrspacecast %class.Derived* %d to %class.Derived addrspace(4)*
62+
// CHECK: bitcast i8 addrspace(4)* %add.ptr to %class.B2 addrspace(4)*
63+
// CHECK: call {{.*}} @_ZNU3AS42B26getRefEv
64+
65+
private Derived *pd = &d;
66+
pd->getRef() = n;
67+
68+
// CHECK: addrspacecast %class.Derived* %4 to %class.Derived addrspace(4)*
69+
// CHECK: bitcast i8 addrspace(4)* %add.ptr1 to %class.B2 addrspace(4)*
70+
// CHECK: call {{.*}} @_ZNU3AS42B26getRefEv
71+
}

0 commit comments

Comments
 (0)