Index: lib/CodeGen/CGClass.cpp =================================================================== --- lib/CodeGen/CGClass.cpp +++ lib/CodeGen/CGClass.cpp @@ -2013,16 +2013,16 @@ bool NewPointerIsChecked) { CallArgList Args; - LangAS SlotAS = E->getType().getAddressSpace(); - QualType ThisType = D->getThisType(); - LangAS ThisAS = ThisType.getTypePtr()->getPointeeType().getAddressSpace(); llvm::Value *ThisPtr = This.getPointer(); - if (SlotAS != ThisAS) { - unsigned TargetThisAS = getContext().getTargetAddressSpace(ThisAS); + QualType ThisType = D->getThisType(); + auto ThisArgAS = ThisPtr->getType()->getPointerAddressSpace(); + auto ThisParAS = getContext().getTargetAddressSpace( + ThisType.getTypePtr()->getPointeeType().getAddressSpace()); + if (ThisArgAS != ThisParAS) { llvm::Type *NewType = - ThisPtr->getType()->getPointerElementType()->getPointerTo(TargetThisAS); - ThisPtr = getTargetHooks().performAddrSpaceCast(*this, This.getPointer(), - ThisAS, SlotAS, NewType); + ThisPtr->getType()->getPointerElementType()->getPointerTo(ThisParAS); + ThisPtr = + Builder.CreatePointerBitCastOrAddrSpaceCast(This.getPointer(), NewType); } // Push the this ptr. Args.add(RValue::get(ThisPtr), D->getThisType()); Index: lib/Sema/SemaInit.cpp =================================================================== --- lib/Sema/SemaInit.cpp +++ lib/Sema/SemaInit.cpp @@ -7321,7 +7321,8 @@ ExprResult Sema::PerformQualificationConversion(Expr *E, QualType Ty, ExprValueKind VK, CheckedConversionKind CCK) { - CastKind CK = (Ty.getAddressSpace() != E->getType().getAddressSpace()) + CastKind CK = (!(VK == VK_RValue && Ty->getPointeeType().isNull()) && + Ty.getAddressSpace() != E->getType().getAddressSpace()) ? CK_AddressSpaceConversion : CK_NoOp; return ImpCastExprToType(E, Ty, CK, VK, /*BasePath=*/nullptr, CCK); Index: test/CodeGenCXX/address-space-of-this.cpp =================================================================== --- /dev/null +++ test/CodeGenCXX/address-space-of-this.cpp @@ -0,0 +1,9 @@ +// RUN: %clang_cc1 %s -std=c++14 -triple=spir -emit-llvm -o - | FileCheck %s +// RUN: %clang_cc1 %s -std=c++17 -triple=spir -emit-llvm -o - | FileCheck %s + +struct MyType { + MyType(int i) : i(i) {} + int i; +}; +//CHECK: call void @_ZN6MyTypeC1Ei(%struct.MyType* addrspacecast (%struct.MyType addrspace(10)* @m to %struct.MyType*), i32 123) +MyType __attribute__((address_space(10))) m = 123;