Skip to content

Commit 6236496

Browse files
committed
[OpenCL] Fix address space for implicit conversion (PR43145)
Clang was creating a DerivedToBase ImplicitCastExpr that was also casting between address spaces as part of the second step in the standard conversion sequence. Defer the address space conversion to the third step in the sequence instead, such that we get a separate ImplicitCastExpr for the address space conversion. Differential Revision: https://reviews.llvm.org/D70605
1 parent d62026e commit 6236496

File tree

2 files changed

+31
-3
lines changed

2 files changed

+31
-3
lines changed

clang/lib/Sema/SemaExprCXX.cpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4095,9 +4095,26 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
40954095
<< From->getSourceRange();
40964096
}
40974097

4098+
// Defer address space conversion to the third conversion.
4099+
QualType FromPteeType = From->getType()->getPointeeType();
4100+
QualType ToPteeType = ToType->getPointeeType();
4101+
QualType NewToType = ToType;
4102+
if (!FromPteeType.isNull() && !ToPteeType.isNull() &&
4103+
FromPteeType.getAddressSpace() != ToPteeType.getAddressSpace()) {
4104+
NewToType = Context.removeAddrSpaceQualType(ToPteeType);
4105+
NewToType = Context.getAddrSpaceQualType(NewToType,
4106+
FromPteeType.getAddressSpace());
4107+
if (ToType->isObjCObjectPointerType())
4108+
NewToType = Context.getObjCObjectPointerType(NewToType);
4109+
else if (ToType->isBlockPointerType())
4110+
NewToType = Context.getBlockPointerType(NewToType);
4111+
else
4112+
NewToType = Context.getPointerType(NewToType);
4113+
}
4114+
40984115
CastKind Kind;
40994116
CXXCastPath BasePath;
4100-
if (CheckPointerConversion(From, ToType, Kind, BasePath, CStyle))
4117+
if (CheckPointerConversion(From, NewToType, Kind, BasePath, CStyle))
41014118
return ExprError();
41024119

41034120
// Make sure we extend blocks if necessary.
@@ -4108,8 +4125,8 @@ Sema::PerformImplicitConversion(Expr *From, QualType ToType,
41084125
From = E.get();
41094126
}
41104127
if (getLangOpts().allowsNonTrivialObjCLifetimeQualifiers())
4111-
CheckObjCConversion(SourceRange(), ToType, From, CCK);
4112-
From = ImpCastExprToType(From, ToType, Kind, VK_RValue, &BasePath, CCK)
4128+
CheckObjCConversion(SourceRange(), NewToType, From, CCK);
4129+
From = ImpCastExprToType(From, NewToType, Kind, VK_RValue, &BasePath, CCK)
41134130
.get();
41144131
break;
41154132
}

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,3 +69,14 @@ void pr43145_3(int n) {
6969
// CHECK: bitcast i8 addrspace(4)* %add.ptr1 to %class.B2 addrspace(4)*
7070
// CHECK: call {{.*}} @_ZNU3AS42B26getRefEv
7171
}
72+
73+
// Implicit conversion of derived to base.
74+
75+
void functionWithBaseArgPtr(class B2 *b) {}
76+
void functionWithBaseArgRef(class B2 &b) {}
77+
78+
void pr43145_4() {
79+
Derived d;
80+
functionWithBaseArgPtr(&d);
81+
functionWithBaseArgRef(d);
82+
}

0 commit comments

Comments
 (0)