Skip to content

Commit 9404955

Browse files
author
Anastasia Stulova
committed
[PR40778] Preserve addr space in Derived to Base cast.
The address space for the Base class pointer when up-casting from Derived should be taken from the Derived class pointer. Differential Revision: https://reviews.llvm.org/D53818 llvm-svn: 355606
1 parent eb39991 commit 9404955

File tree

3 files changed

+31
-3
lines changed

3 files changed

+31
-3
lines changed

clang/lib/CodeGen/CGClass.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,8 @@ Address CodeGenFunction::GetAddressOfBaseClass(
302302

303303
// Get the base pointer type.
304304
llvm::Type *BasePtrTy =
305-
ConvertType((PathEnd[-1])->getType())->getPointerTo();
305+
ConvertType((PathEnd[-1])->getType())
306+
->getPointerTo(Value.getType()->getPointerAddressSpace());
306307

307308
QualType DerivedTy = getContext().getRecordType(Derived);
308309
CharUnits DerivedAlign = CGM.getClassPointerAlignment(Derived);

clang/lib/Sema/SemaExpr.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2660,10 +2660,15 @@ Sema::PerformObjectMemberConversion(Expr *From,
26602660
bool PointerConversions = false;
26612661
if (isa<FieldDecl>(Member)) {
26622662
DestRecordType = Context.getCanonicalType(Context.getTypeDeclType(RD));
2663+
auto FromPtrType = FromType->getAs<PointerType>();
2664+
DestRecordType = Context.getAddrSpaceQualType(
2665+
DestRecordType, FromPtrType
2666+
? FromType->getPointeeType().getAddressSpace()
2667+
: FromType.getAddressSpace());
26632668

2664-
if (FromType->getAs<PointerType>()) {
2669+
if (FromPtrType) {
26652670
DestType = Context.getPointerType(DestRecordType);
2666-
FromRecordType = FromType->getPointeeType();
2671+
FromRecordType = FromPtrType->getPointeeType();
26672672
PointerConversions = true;
26682673
} else {
26692674
DestType = DestRecordType;
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %clang_cc1 %s -triple spir -cl-std=c++ -emit-llvm -O0 -o - | FileCheck %s
2+
3+
struct B {
4+
int mb;
5+
};
6+
7+
class D : public B {
8+
public:
9+
int getmb() { return mb; }
10+
};
11+
12+
void foo() {
13+
D d;
14+
//CHECK: addrspacecast %class.D* %d to %class.D addrspace(4)*
15+
//CHECK: call i32 @_ZNU3AS41D5getmbEv(%class.D addrspace(4)*
16+
d.getmb();
17+
}
18+
19+
//Derived and Base are in the same address space.
20+
21+
//CHECK: define linkonce_odr i32 @_ZNU3AS41D5getmbEv(%class.D addrspace(4)* %this)
22+
//CHECK: bitcast %class.D addrspace(4)* %this1 to %struct.B addrspace(4)*

0 commit comments

Comments
 (0)