https://github.com/vtjnash updated https://github.com/llvm/llvm-project/pull/179330
>From 1cbd789dc93782ac5b950fee8fd272e5122f1f80 Mon Sep 17 00:00:00 2001 From: Jameson Nash <[email protected]> Date: Mon, 2 Feb 2026 20:47:07 +0000 Subject: [PATCH 1/2] [clang] remove non-functional SrcAddr parameter The conversion code always get the type of Src from the Src argument itself, so there is no point in also providing an arbitrary (and possibly incorrect) guess at what it might be. Also fixes CIR to actually return the casted value. --- clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 21 +++++++-------------- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 17 +++-------------- clang/lib/CIR/CodeGen/TargetInfo.cpp | 7 ++++--- clang/lib/CIR/CodeGen/TargetInfo.h | 2 -- clang/lib/CodeGen/CGAtomic.cpp | 3 +-- clang/lib/CodeGen/CGBuiltin.cpp | 18 ++++++++---------- clang/lib/CodeGen/CGCall.cpp | 10 ++++------ clang/lib/CodeGen/CGClass.cpp | 3 +-- clang/lib/CodeGen/CGDecl.cpp | 4 ++-- clang/lib/CodeGen/CGException.cpp | 5 ++--- clang/lib/CodeGen/CGExpr.cpp | 15 +++++++-------- clang/lib/CodeGen/CGExprCXX.cpp | 4 ++-- clang/lib/CodeGen/CGExprConstant.cpp | 4 +--- clang/lib/CodeGen/CGExprScalar.cpp | 7 +++---- clang/lib/CodeGen/CodeGenModule.cpp | 7 +++---- clang/lib/CodeGen/TargetInfo.cpp | 10 +++++----- clang/lib/CodeGen/TargetInfo.h | 10 ++-------- 17 files changed, 55 insertions(+), 92 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index 8bdf075aab695..c505e1057563b 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -1465,7 +1465,7 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) { "space"); mlir::Value v = getTargetHooks().performAddrSpaceCast( - *this, lv.getPointer(), srcAS, convertType(destTy)); + *this, lv.getPointer(), convertType(destTy)); return makeAddrLValue(Address(v, convertTypeForMem(e->getType()), lv.getAddress().getAlignment()), @@ -2504,20 +2504,13 @@ Address CIRGenFunction::createTempAlloca(mlir::Type ty, CharUnits align, // in C++ the auto variables are in the default address space. Therefore // cast alloca to the default address space when necessary. - LangAS allocaAS = alloca.getAddressSpace() - ? clang::getLangASFromTargetAS( - alloca.getAddressSpace().getValue().getUInt()) - : clang::LangAS::Default; - LangAS dstTyAS = clang::LangAS::Default; - if (getCIRAllocaAddressSpace()) { - dstTyAS = clang::getLangASFromTargetAS( - getCIRAllocaAddressSpace().getValue().getUInt()); - } + cir::PointerType dstTy; + if (getCIRAllocaAddressSpace()) + dstTy = builder.getPointerTo(ty, getCIRAllocaAddressSpace()); + else + dstTy = builder.getPointerTo(ty, clang::LangAS::Default); + v = getTargetHooks().performAddrSpaceCast(*this, v, dstTy); - if (dstTyAS != allocaAS) { - getTargetHooks().performAddrSpaceCast(*this, v, getCIRAllocaAddressSpace(), - builder.getPointerTo(ty, dstTyAS)); - } return Address(v, ty, align); } diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 940a0cb616b27..5947d619debb3 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -2138,21 +2138,10 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) { cgf.getLoc(subExpr->getExprLoc())); } - clang::QualType srcTy = subExpr->IgnoreImpCasts()->getType(); - if (srcTy->isPointerType() || srcTy->isReferenceType()) - srcTy = srcTy->getPointeeType(); - - clang::LangAS srcLangAS = srcTy.getAddressSpace(); - cir::TargetAddressSpaceAttr subExprAS; - if (clang::isTargetAddressSpace(srcLangAS)) - subExprAS = cir::toCIRTargetAddressSpace(cgf.getMLIRContext(), srcLangAS); - else - cgf.cgm.errorNYI(subExpr->getSourceRange(), - "non-target address space conversion"); - // Since target may map different address spaces in AST to the same address - // space, an address space conversion may end up as a bitcast. + // Since target may map different address spaces in AST to the same + // address space, an address space conversion may end up as a bitcast. return cgf.cgm.getTargetCIRGenInfo().performAddrSpaceCast( - cgf, Visit(subExpr), subExprAS, convertType(destTy)); + cgf, Visit(subExpr), convertType(destTy)); } case CK_AtomicToNonAtomic: { diff --git a/clang/lib/CIR/CodeGen/TargetInfo.cpp b/clang/lib/CIR/CodeGen/TargetInfo.cpp index dc29dc0204c19..a3a2c3a589415 100644 --- a/clang/lib/CIR/CodeGen/TargetInfo.cpp +++ b/clang/lib/CIR/CodeGen/TargetInfo.cpp @@ -91,9 +91,10 @@ bool TargetCIRGenInfo::isNoProtoCallVariadic( return false; } -mlir::Value TargetCIRGenInfo::performAddrSpaceCast( - CIRGenFunction &cgf, mlir::Value v, cir::TargetAddressSpaceAttr srcAddr, - mlir::Type destTy, bool isNonNull) const { +mlir::Value TargetCIRGenInfo::performAddrSpaceCast(CIRGenFunction &cgf, + mlir::Value v, + mlir::Type destTy, + bool isNonNull) const { // Since target may map different address spaces in AST to the same address // space, an address space conversion may end up as a bitcast. if (cir::GlobalOp globalOp = v.getDefiningOp<cir::GlobalOp>()) diff --git a/clang/lib/CIR/CodeGen/TargetInfo.h b/clang/lib/CIR/CodeGen/TargetInfo.h index bab838692e215..3e1a73126e7f4 100644 --- a/clang/lib/CIR/CodeGen/TargetInfo.h +++ b/clang/lib/CIR/CodeGen/TargetInfo.h @@ -54,10 +54,8 @@ class TargetCIRGenInfo { /// Perform address space cast of an expression of pointer type. /// \param V is the value to be casted to another address space. /// \param DestTy is the destination pointer type. - /// \param srcAS is theaddress space of \p V. /// \param IsNonNull is the flag indicating \p V is known to be non null. virtual mlir::Value performAddrSpaceCast(CIRGenFunction &cgf, mlir::Value v, - cir::TargetAddressSpaceAttr srcAddr, mlir::Type destTy, bool isNonNull = false) const; diff --git a/clang/lib/CodeGen/CGAtomic.cpp b/clang/lib/CodeGen/CGAtomic.cpp index 06ef2f5b8fb6c..42f1432989270 100644 --- a/clang/lib/CodeGen/CGAtomic.cpp +++ b/clang/lib/CodeGen/CGAtomic.cpp @@ -1145,8 +1145,7 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) { auto DestAS = getContext().getTargetAddressSpace(LangAS::opencl_generic); auto *DestType = llvm::PointerType::get(getLLVMContext(), DestAS); - return getTargetHooks().performAddrSpaceCast(*this, V, AS, DestType, - false); + return getTargetHooks().performAddrSpaceCast(*this, V, DestType, false); }; Args.add(RValue::get(CastToGenericAddrSpace(Ptr.emitRawPointer(*this), diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index 7f0933d6ceeeb..bf73b57e81c46 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -4452,12 +4452,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, AI->setAlignment(SuitableAlignmentInBytes); if (BuiltinID != Builtin::BI__builtin_alloca_uninitialized) initializeAlloca(*this, AI, Size, SuitableAlignmentInBytes); - LangAS AAS = getASTAllocaAddressSpace(); - LangAS EAS = E->getType()->getPointeeType().getAddressSpace(); - if (AAS != EAS) { + if (AI->getAddressSpace() != + CGM.getContext().getTargetAddressSpace( + E->getType()->getPointeeType().getAddressSpace())) { llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType()); - return RValue::get( - getTargetHooks().performAddrSpaceCast(*this, AI, AAS, Ty)); + return RValue::get(getTargetHooks().performAddrSpaceCast(*this, AI, Ty)); } return RValue::get(AI); } @@ -4474,12 +4473,11 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, AI->setAlignment(AlignmentInBytes); if (BuiltinID != Builtin::BI__builtin_alloca_with_align_uninitialized) initializeAlloca(*this, AI, Size, AlignmentInBytes); - LangAS AAS = getASTAllocaAddressSpace(); - LangAS EAS = E->getType()->getPointeeType().getAddressSpace(); - if (AAS != EAS) { + if (AI->getAddressSpace() != + CGM.getContext().getTargetAddressSpace( + E->getType()->getPointeeType().getAddressSpace())) { llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType()); - return RValue::get( - getTargetHooks().performAddrSpaceCast(*this, AI, AAS, Ty)); + return RValue::get(getTargetHooks().performAddrSpaceCast(*this, AI, Ty)); } return RValue::get(AI); } diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index 04f44146e1269..dc58218b7fff8 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -5341,12 +5341,11 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, // here, symmetrically with the handling we have for normal pointer args. if (SRetPtr.getAddressSpace() != RetAI.getIndirectAddrSpace()) { llvm::Value *V = SRetPtr.getBasePointer(); - LangAS SAS = getLangASFromTargetAS(SRetPtr.getAddressSpace()); llvm::Type *Ty = llvm::PointerType::get(getLLVMContext(), RetAI.getIndirectAddrSpace()); SRetPtr = SRetPtr.withPointer( - getTargetHooks().performAddrSpaceCast(*this, V, SAS, Ty, true), + getTargetHooks().performAddrSpaceCast(*this, V, Ty, true), SRetPtr.isKnownNonNull()); } IRCallArgs[IRFunctionArgs.getSRetArgNo()] = @@ -5490,8 +5489,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, // only the contextual values. If the address space mismatches, see if // we can look through a cast to a compatible address space value, // otherwise emit a copy. - llvm::Value *Val = getTargetHooks().performAddrSpaceCast( - *this, V, I->Ty.getAddressSpace(), T, true); + llvm::Value *Val = + getTargetHooks().performAddrSpaceCast(*this, V, T, true); if (ArgHasMaybeUndefAttr) Val = Builder.CreateFreeze(Val); IRCallArgs[FirstIRArg] = Val; @@ -5575,9 +5574,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, if (FirstIRArg < IRFuncTy->getNumParams() && V->getType() != IRFuncTy->getParamType(FirstIRArg)) { assert(V->getType()->isPointerTy() && "Only pointers can mismatch!"); - auto ActualAS = I->Ty.getAddressSpace(); V = getTargetHooks().performAddrSpaceCast( - *this, V, ActualAS, IRFuncTy->getParamType(FirstIRArg)); + *this, V, IRFuncTy->getParamType(FirstIRArg)); } if (ArgHasMaybeUndefAttr) diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp index 81ca858d29512..dabe37e670f84 100644 --- a/clang/lib/CodeGen/CGClass.cpp +++ b/clang/lib/CodeGen/CGClass.cpp @@ -2278,8 +2278,7 @@ void CodeGenFunction::EmitCXXConstructorCall( unsigned TargetThisAS = getContext().getTargetAddressSpace(ThisAS); llvm::Type *NewType = llvm::PointerType::get(getLLVMContext(), TargetThisAS); - ThisPtr = - getTargetHooks().performAddrSpaceCast(*this, ThisPtr, ThisAS, NewType); + ThisPtr = getTargetHooks().performAddrSpaceCast(*this, ThisPtr, NewType); } // Push the this ptr. diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index 0cc0742c45c95..54bd1425725df 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -304,7 +304,7 @@ llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl( llvm::Constant *Addr = GV; if (AS != ExpectedAS) { Addr = getTargetCodeGenInfo().performAddrSpaceCast( - *this, GV, AS, + *this, GV, llvm::PointerType::get(getLLVMContext(), getContext().getTargetAddressSpace(ExpectedAS))); } @@ -2738,7 +2738,7 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg, auto DestAS = getContext().getTargetAddressSpace(DestLangAS); auto *T = llvm::PointerType::get(getLLVMContext(), DestAS); DeclPtr = DeclPtr.withPointer( - getTargetHooks().performAddrSpaceCast(*this, V, SrcLangAS, T, true), + getTargetHooks().performAddrSpaceCast(*this, V, T, true), DeclPtr.isKnownNonNull()); } diff --git a/clang/lib/CodeGen/CGException.cpp b/clang/lib/CodeGen/CGException.cpp index e9d20672ce185..ac972689368b9 100644 --- a/clang/lib/CodeGen/CGException.cpp +++ b/clang/lib/CodeGen/CGException.cpp @@ -1143,7 +1143,6 @@ static void emitCatchDispatchBlock(CodeGenFunction &CGF, llvm::Function *llvm_eh_typeid_for = CGF.CGM.getIntrinsic(llvm::Intrinsic::eh_typeid_for, {CGF.VoidPtrTy}); llvm::Type *argTy = llvm_eh_typeid_for->getArg(0)->getType(); - LangAS globAS = CGF.CGM.GetGlobalVarAddressSpace(nullptr); // Load the selector value. llvm::Value *selector = CGF.getSelectorFromSlot(); @@ -1159,8 +1158,8 @@ static void emitCatchDispatchBlock(CodeGenFunction &CGF, assert(typeValue && "fell into catch-all case!"); // With opaque ptrs, only the address space can be a mismatch. if (typeValue->getType() != argTy) - typeValue = CGF.getTargetHooks().performAddrSpaceCast(CGF, typeValue, - globAS, argTy); + typeValue = + CGF.getTargetHooks().performAddrSpaceCast(CGF, typeValue, argTy); // Figure out the next block. bool nextIsEnd; diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index a2eb4d5930829..7716d9abb5910 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -129,9 +129,9 @@ RawAddress CodeGenFunction::MaybeCastStackAddressSpace(RawAddress Alloca, // builder. if (!ArraySize) Builder.SetInsertPoint(getPostAllocaInsertPoint()); - V = getTargetHooks().performAddrSpaceCast( - *this, V, getASTAllocaAddressSpace(), Builder.getPtrTy(DestAddrSpace), - /*IsNonNull=*/true); + V = getTargetHooks().performAddrSpaceCast(*this, V, + Builder.getPtrTy(DestAddrSpace), + /*IsNonNull=*/true); } return RawAddress(V, Alloca.getElementType(), Alloca.getAlignment(), @@ -484,7 +484,7 @@ static RawAddress createReferenceTemporary(CodeGenFunction &CGF, llvm::Constant *C = GV; if (AS != LangAS::Default) C = TCG.performAddrSpaceCast( - CGF.CGM, GV, AS, + CGF.CGM, GV, llvm::PointerType::get( CGF.getLLVMContext(), CGF.getContext().getTargetAddressSpace(LangAS::Default))); @@ -3666,8 +3666,8 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { if (AS != T.getAddressSpace()) { auto TargetAS = getContext().getTargetAddressSpace(T.getAddressSpace()); auto PtrTy = llvm::PointerType::get(CGM.getLLVMContext(), TargetAS); - auto ASC = getTargetHooks().performAddrSpaceCast(CGM, ATPO.getPointer(), - AS, PtrTy); + auto ASC = + getTargetHooks().performAddrSpaceCast(CGM, ATPO.getPointer(), PtrTy); ATPO = ConstantAddress(ASC, ATPO.getElementType(), ATPO.getAlignment()); } @@ -6119,8 +6119,7 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { LValue LV = EmitLValue(E->getSubExpr()); QualType DestTy = getContext().getPointerType(E->getType()); llvm::Value *V = getTargetHooks().performAddrSpaceCast( - *this, LV.getPointer(*this), - E->getSubExpr()->getType().getAddressSpace(), ConvertType(DestTy)); + *this, LV.getPointer(*this), ConvertType(DestTy)); return MakeAddrLValue(Address(V, ConvertTypeForMem(E->getType()), LV.getAddress().getAlignment()), E->getType(), LV.getBaseInfo(), LV.getTBAAInfo()); diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp index 78c10ec757bca..fc4c3d699ceda 100644 --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -113,7 +113,7 @@ RValue CodeGenFunction::EmitCXXDestructorCall( if (SrcAS != DstAS) { QualType DstTy = DtorDecl->getThisType(); llvm::Type *NewType = CGM.getTypes().ConvertType(DstTy); - This = getTargetHooks().performAddrSpaceCast(*this, This, SrcAS, NewType); + This = getTargetHooks().performAddrSpaceCast(*this, This, NewType); } CallArgList Args; @@ -2182,7 +2182,7 @@ llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { auto MaybeASCast = [=](auto &&TypeInfo) { if (GlobAS == LangAS::Default) return TypeInfo; - return getTargetHooks().performAddrSpaceCast(CGM, TypeInfo, GlobAS, PtrTy); + return getTargetHooks().performAddrSpaceCast(CGM, TypeInfo, PtrTy); }; if (E->isTypeOperand()) { diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index 0eec4dba4824a..b785b4b083c04 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -1227,10 +1227,8 @@ class ConstExprEmitter auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType()); if (!C) return nullptr; - LangAS srcAS = subExpr->getType()->getPointeeType().getAddressSpace(); llvm::Type *destTy = ConvertType(E->getType()); - return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, srcAS, - destTy); + return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, destTy); } case CK_LValueToRValue: { diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 9548b25507274..832b7cf781fdf 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -2564,8 +2564,8 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { // detail, and doing an AS cast here still retains the semantics the user // expects. It is desirable to remove this iff a better solution is found. if (auto A = dyn_cast<llvm::Argument>(Src); A && A->hasStructRetAttr()) - return CGF.CGM.getTargetCodeGenInfo().performAddrSpaceCast( - CGF, Src, E->getType().getAddressSpace(), DstTy); + return CGF.CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGF, Src, + DstTy); assert( (!SrcTy->isPtrOrPtrVectorTy() || !DstTy->isPtrOrPtrVectorTy() || @@ -2710,8 +2710,7 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { // Since target may map different address spaces in AST to the same address // space, an address space conversion may end up as a bitcast. return CGF.CGM.getTargetCodeGenInfo().performAddrSpaceCast( - CGF, Visit(E), E->getType()->getPointeeType().getAddressSpace(), - ConvertType(DestTy)); + CGF, Visit(E), ConvertType(DestTy)); } case CK_AtomicToNonAtomic: case CK_NonAtomicToAtomic: diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index d50c9605a30b3..2f02859e8b174 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -5690,8 +5690,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS); if (DAddrSpace != ExpectedAS) { return getTargetCodeGenInfo().performAddrSpaceCast( - *this, GV, DAddrSpace, - llvm::PointerType::get(getLLVMContext(), TargetAS)); + *this, GV, llvm::PointerType::get(getLLVMContext(), TargetAS)); } return GV; @@ -5925,7 +5924,7 @@ castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM, auto AS = CGM.GetGlobalConstantAddressSpace(); if (AS != LangAS::Default) Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast( - CGM, GV, AS, + CGM, GV, llvm::PointerType::get( CGM.getLLVMContext(), CGM.getContext().getTargetAddressSpace(LangAS::Default))); @@ -7352,7 +7351,7 @@ ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( llvm::Constant *CV = GV; if (AddrSpace != LangAS::Default) CV = getTargetCodeGenInfo().performAddrSpaceCast( - *this, GV, AddrSpace, + *this, GV, llvm::PointerType::get( getLLVMContext(), getContext().getTargetAddressSpace(LangAS::Default))); diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp index 342a3af0ac1ee..3cdb76e2a760c 100644 --- a/clang/lib/CodeGen/TargetInfo.cpp +++ b/clang/lib/CodeGen/TargetInfo.cpp @@ -148,13 +148,14 @@ LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, return D ? D->getType().getAddressSpace() : LangAS::Default; } -llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( - CodeGen::CodeGenFunction &CGF, llvm::Value *Src, LangAS SrcAddr, - llvm::Type *DestTy, bool isNonNull) const { +llvm::Value * +TargetCodeGenInfo::performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, + llvm::Value *Src, llvm::Type *DestTy, + bool isNonNull) const { // Since target may map different address spaces in AST to the same address // space, an address space conversion may end up as a bitcast. if (auto *C = dyn_cast<llvm::Constant>(Src)) - return performAddrSpaceCast(CGF.CGM, C, SrcAddr, DestTy); + return performAddrSpaceCast(CGF.CGM, C, DestTy); // Try to preserve the source's name to make IR more readable. return CGF.Builder.CreateAddrSpaceCast( Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : ""); @@ -162,7 +163,6 @@ llvm::Value *TargetCodeGenInfo::performAddrSpaceCast( llvm::Constant * TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, - LangAS SrcAddr, llvm::Type *DestTy) const { // Since target may map different address spaces in AST to the same address // space, an address space conversion may end up as a bitcast. diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h index db06584d766bf..6ea8dfc79d3a1 100644 --- a/clang/lib/CodeGen/TargetInfo.h +++ b/clang/lib/CodeGen/TargetInfo.h @@ -322,28 +322,22 @@ class TargetCodeGenInfo { virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; } Address performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, Address Addr, - LangAS SrcAddr, llvm::Type *DestTy, + llvm::Type *DestTy, bool IsNonNull = false) const; /// Perform address space cast of an expression of pointer type. /// \param V is the LLVM value to be casted to another address space. - /// \param SrcAddr is the language address space of \p V. - /// \param DestAddr is the targeted language address space. /// \param DestTy is the destination LLVM pointer type. /// \param IsNonNull is the flag indicating \p V is known to be non null. virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, - llvm::Value *V, LangAS SrcAddr, - llvm::Type *DestTy, + llvm::Value *V, llvm::Type *DestTy, bool IsNonNull = false) const; /// Perform address space cast of a constant expression of pointer type. /// \param V is the LLVM constant to be casted to another address space. - /// \param SrcAddr is the language address space of \p V. - /// \param DestAddr is the targeted language address space. /// \param DestTy is the destination LLVM pointer type. virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *V, - LangAS SrcAddr, llvm::Type *DestTy) const; /// Get address space of pointer parameter for __cxa_atexit. >From c2e457e530c49277ba7b952fb93314caa850f95c Mon Sep 17 00:00:00 2001 From: Jameson Nash <[email protected]> Date: Wed, 4 Feb 2026 19:46:15 +0000 Subject: [PATCH 2/2] remove virtual entirely, just keep for setting name and other generic checks --- clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 5 ++--- clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp | 6 +---- clang/lib/CIR/CodeGen/CIRGenFunction.h | 6 +++++ clang/lib/CIR/CodeGen/TargetInfo.cpp | 12 ---------- clang/lib/CIR/CodeGen/TargetInfo.h | 7 ------ clang/lib/CodeGen/CGAtomic.cpp | 2 +- clang/lib/CodeGen/CGBuiltin.cpp | 4 ++-- clang/lib/CodeGen/CGCall.cpp | 11 ++++----- clang/lib/CodeGen/CGClass.cpp | 2 +- clang/lib/CodeGen/CGDecl.cpp | 9 ++++---- clang/lib/CodeGen/CGException.cpp | 3 +-- clang/lib/CodeGen/CGExpr.cpp | 26 +++++++++------------- clang/lib/CodeGen/CGExprCXX.cpp | 6 ++--- clang/lib/CodeGen/CGExprConstant.cpp | 4 ++-- clang/lib/CodeGen/CGExprScalar.cpp | 6 ++--- clang/lib/CodeGen/CodeGenFunction.h | 6 +++++ clang/lib/CodeGen/CodeGenModule.cpp | 25 +++++++++------------ clang/lib/CodeGen/CodeGenModule.h | 7 ++++++ clang/lib/CodeGen/TargetInfo.cpp | 21 ----------------- clang/lib/CodeGen/TargetInfo.h | 19 ---------------- 20 files changed, 64 insertions(+), 123 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index c505e1057563b..b2f80a0a7da79 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -1464,8 +1464,7 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) { "emitCastLValue: address space conversion from unknown address " "space"); - mlir::Value v = getTargetHooks().performAddrSpaceCast( - *this, lv.getPointer(), convertType(destTy)); + mlir::Value v = performAddrSpaceCast(lv.getPointer(), convertType(destTy)); return makeAddrLValue(Address(v, convertTypeForMem(e->getType()), lv.getAddress().getAlignment()), @@ -2509,7 +2508,7 @@ Address CIRGenFunction::createTempAlloca(mlir::Type ty, CharUnits align, dstTy = builder.getPointerTo(ty, getCIRAllocaAddressSpace()); else dstTy = builder.getPointerTo(ty, clang::LangAS::Default); - v = getTargetHooks().performAddrSpaceCast(*this, v, dstTy); + v = performAddrSpaceCast(v, dstTy); return Address(v, ty, align); } diff --git a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp index 5947d619debb3..9abd6a76e01db 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp @@ -2137,11 +2137,7 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) { return cgf.cgm.emitNullConstant(destTy, cgf.getLoc(subExpr->getExprLoc())); } - - // Since target may map different address spaces in AST to the same - // address space, an address space conversion may end up as a bitcast. - return cgf.cgm.getTargetCIRGenInfo().performAddrSpaceCast( - cgf, Visit(subExpr), convertType(destTy)); + return cgf.performAddrSpaceCast(Visit(subExpr), convertType(destTy)); } case CK_AtomicToNonAtomic: { diff --git a/clang/lib/CIR/CodeGen/CIRGenFunction.h b/clang/lib/CIR/CodeGen/CIRGenFunction.h index adcf4d56e3892..804492f441d60 100644 --- a/clang/lib/CIR/CodeGen/CIRGenFunction.h +++ b/clang/lib/CIR/CodeGen/CIRGenFunction.h @@ -2065,6 +2065,12 @@ class CIRGenFunction : public CIRGenTypeCache { const Twine &name = "tmp", Address *alloca = nullptr, mlir::OpBuilder::InsertPoint ip = {}); + mlir::Value performAddrSpaceCast(mlir::Value v, mlir::Type destTy) const { + if (cir::GlobalOp globalOp = v.getDefiningOp<cir::GlobalOp>()) + cgm.errorNYI("Global op addrspace cast"); + return builder.createAddrSpaceCast(v, destTy); + } + //===--------------------------------------------------------------------===// // OpenMP Emission //===--------------------------------------------------------------------===// diff --git a/clang/lib/CIR/CodeGen/TargetInfo.cpp b/clang/lib/CIR/CodeGen/TargetInfo.cpp index a3a2c3a589415..5a0c854db9125 100644 --- a/clang/lib/CIR/CodeGen/TargetInfo.cpp +++ b/clang/lib/CIR/CodeGen/TargetInfo.cpp @@ -90,15 +90,3 @@ bool TargetCIRGenInfo::isNoProtoCallVariadic( // For everything else, we just prefer false unless we opt out. return false; } - -mlir::Value TargetCIRGenInfo::performAddrSpaceCast(CIRGenFunction &cgf, - mlir::Value v, - mlir::Type destTy, - bool isNonNull) const { - // Since target may map different address spaces in AST to the same address - // space, an address space conversion may end up as a bitcast. - if (cir::GlobalOp globalOp = v.getDefiningOp<cir::GlobalOp>()) - cgf.cgm.errorNYI("Global op addrspace cast"); - // Try to preserve the source's name to make IR more readable. - return cgf.getBuilder().createAddrSpaceCast(v, destTy); -} diff --git a/clang/lib/CIR/CodeGen/TargetInfo.h b/clang/lib/CIR/CodeGen/TargetInfo.h index 3e1a73126e7f4..79325c2d35c4d 100644 --- a/clang/lib/CIR/CodeGen/TargetInfo.h +++ b/clang/lib/CIR/CodeGen/TargetInfo.h @@ -51,13 +51,6 @@ class TargetCIRGenInfo { virtual cir::TargetAddressSpaceAttr getCIRAllocaAddressSpace() const { return {}; } - /// Perform address space cast of an expression of pointer type. - /// \param V is the value to be casted to another address space. - /// \param DestTy is the destination pointer type. - /// \param IsNonNull is the flag indicating \p V is known to be non null. - virtual mlir::Value performAddrSpaceCast(CIRGenFunction &cgf, mlir::Value v, - mlir::Type destTy, - bool isNonNull = false) const; /// Determine whether a call to an unprototyped functions under /// the given calling convention should use the variadic diff --git a/clang/lib/CodeGen/CGAtomic.cpp b/clang/lib/CodeGen/CGAtomic.cpp index 42f1432989270..fb3a5663834ed 100644 --- a/clang/lib/CodeGen/CGAtomic.cpp +++ b/clang/lib/CodeGen/CGAtomic.cpp @@ -1145,7 +1145,7 @@ RValue CodeGenFunction::EmitAtomicExpr(AtomicExpr *E) { auto DestAS = getContext().getTargetAddressSpace(LangAS::opencl_generic); auto *DestType = llvm::PointerType::get(getLLVMContext(), DestAS); - return getTargetHooks().performAddrSpaceCast(*this, V, DestType, false); + return performAddrSpaceCast(V, DestType); }; Args.add(RValue::get(CastToGenericAddrSpace(Ptr.emitRawPointer(*this), diff --git a/clang/lib/CodeGen/CGBuiltin.cpp b/clang/lib/CodeGen/CGBuiltin.cpp index bf73b57e81c46..245c3f6f914c8 100644 --- a/clang/lib/CodeGen/CGBuiltin.cpp +++ b/clang/lib/CodeGen/CGBuiltin.cpp @@ -4456,7 +4456,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, CGM.getContext().getTargetAddressSpace( E->getType()->getPointeeType().getAddressSpace())) { llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType()); - return RValue::get(getTargetHooks().performAddrSpaceCast(*this, AI, Ty)); + return RValue::get(performAddrSpaceCast(AI, Ty)); } return RValue::get(AI); } @@ -4477,7 +4477,7 @@ RValue CodeGenFunction::EmitBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID, CGM.getContext().getTargetAddressSpace( E->getType()->getPointeeType().getAddressSpace())) { llvm::Type *Ty = CGM.getTypes().ConvertType(E->getType()); - return RValue::get(getTargetHooks().performAddrSpaceCast(*this, AI, Ty)); + return RValue::get(performAddrSpaceCast(AI, Ty)); } return RValue::get(AI); } diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp index dc58218b7fff8..67c3f01e333f4 100644 --- a/clang/lib/CodeGen/CGCall.cpp +++ b/clang/lib/CodeGen/CGCall.cpp @@ -5344,9 +5344,8 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, llvm::Type *Ty = llvm::PointerType::get(getLLVMContext(), RetAI.getIndirectAddrSpace()); - SRetPtr = SRetPtr.withPointer( - getTargetHooks().performAddrSpaceCast(*this, V, Ty, true), - SRetPtr.isKnownNonNull()); + SRetPtr = SRetPtr.withPointer(performAddrSpaceCast(V, Ty), + SRetPtr.isKnownNonNull()); } IRCallArgs[IRFunctionArgs.getSRetArgNo()] = getAsNaturalPointerTo(SRetPtr, RetTy); @@ -5489,8 +5488,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, // only the contextual values. If the address space mismatches, see if // we can look through a cast to a compatible address space value, // otherwise emit a copy. - llvm::Value *Val = - getTargetHooks().performAddrSpaceCast(*this, V, T, true); + llvm::Value *Val = performAddrSpaceCast(V, T); if (ArgHasMaybeUndefAttr) Val = Builder.CreateFreeze(Val); IRCallArgs[FirstIRArg] = Val; @@ -5574,8 +5572,7 @@ RValue CodeGenFunction::EmitCall(const CGFunctionInfo &CallInfo, if (FirstIRArg < IRFuncTy->getNumParams() && V->getType() != IRFuncTy->getParamType(FirstIRArg)) { assert(V->getType()->isPointerTy() && "Only pointers can mismatch!"); - V = getTargetHooks().performAddrSpaceCast( - *this, V, IRFuncTy->getParamType(FirstIRArg)); + V = performAddrSpaceCast(V, IRFuncTy->getParamType(FirstIRArg)); } if (ArgHasMaybeUndefAttr) diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp index dabe37e670f84..831f63084fd6c 100644 --- a/clang/lib/CodeGen/CGClass.cpp +++ b/clang/lib/CodeGen/CGClass.cpp @@ -2278,7 +2278,7 @@ void CodeGenFunction::EmitCXXConstructorCall( unsigned TargetThisAS = getContext().getTargetAddressSpace(ThisAS); llvm::Type *NewType = llvm::PointerType::get(getLLVMContext(), TargetThisAS); - ThisPtr = getTargetHooks().performAddrSpaceCast(*this, ThisPtr, NewType); + ThisPtr = performAddrSpaceCast(ThisPtr, NewType); } // Push the this ptr. diff --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp index 54bd1425725df..c66112e0e5bfb 100644 --- a/clang/lib/CodeGen/CGDecl.cpp +++ b/clang/lib/CodeGen/CGDecl.cpp @@ -303,8 +303,8 @@ llvm::Constant *CodeGenModule::getOrCreateStaticVarDecl( LangAS ExpectedAS = Ty.getAddressSpace(); llvm::Constant *Addr = GV; if (AS != ExpectedAS) { - Addr = getTargetCodeGenInfo().performAddrSpaceCast( - *this, GV, + Addr = performAddrSpaceCast( + GV, llvm::PointerType::get(getLLVMContext(), getContext().getTargetAddressSpace(ExpectedAS))); } @@ -2737,9 +2737,8 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg, CGM.getDataLayout().getAllocaAddrSpace()); auto DestAS = getContext().getTargetAddressSpace(DestLangAS); auto *T = llvm::PointerType::get(getLLVMContext(), DestAS); - DeclPtr = DeclPtr.withPointer( - getTargetHooks().performAddrSpaceCast(*this, V, T, true), - DeclPtr.isKnownNonNull()); + DeclPtr = DeclPtr.withPointer(performAddrSpaceCast(V, T), + DeclPtr.isKnownNonNull()); } // Push a destructor cleanup for this parameter if the ABI requires it. diff --git a/clang/lib/CodeGen/CGException.cpp b/clang/lib/CodeGen/CGException.cpp index ac972689368b9..2f1df6e9a8a5c 100644 --- a/clang/lib/CodeGen/CGException.cpp +++ b/clang/lib/CodeGen/CGException.cpp @@ -1158,8 +1158,7 @@ static void emitCatchDispatchBlock(CodeGenFunction &CGF, assert(typeValue && "fell into catch-all case!"); // With opaque ptrs, only the address space can be a mismatch. if (typeValue->getType() != argTy) - typeValue = - CGF.getTargetHooks().performAddrSpaceCast(CGF, typeValue, argTy); + typeValue = CGF.performAddrSpaceCast(typeValue, argTy); // Figure out the next block. bool nextIsEnd; diff --git a/clang/lib/CodeGen/CGExpr.cpp b/clang/lib/CodeGen/CGExpr.cpp index 7716d9abb5910..fdfdedb0ccf31 100644 --- a/clang/lib/CodeGen/CGExpr.cpp +++ b/clang/lib/CodeGen/CGExpr.cpp @@ -129,9 +129,7 @@ RawAddress CodeGenFunction::MaybeCastStackAddressSpace(RawAddress Alloca, // builder. if (!ArraySize) Builder.SetInsertPoint(getPostAllocaInsertPoint()); - V = getTargetHooks().performAddrSpaceCast(*this, V, - Builder.getPtrTy(DestAddrSpace), - /*IsNonNull=*/true); + V = performAddrSpaceCast(V, Builder.getPtrTy(DestAddrSpace)); } return RawAddress(V, Alloca.getElementType(), Alloca.getAlignment(), @@ -460,7 +458,6 @@ static RawAddress createReferenceTemporary(CodeGenFunction &CGF, const MaterializeTemporaryExpr *M, const Expr *Inner, RawAddress *Alloca = nullptr) { - auto &TCG = CGF.getTargetHooks(); switch (M->getStorageDuration()) { case SD_FullExpression: case SD_Automatic: { @@ -483,11 +480,10 @@ static RawAddress createReferenceTemporary(CodeGenFunction &CGF, GV->setAlignment(alignment.getAsAlign()); llvm::Constant *C = GV; if (AS != LangAS::Default) - C = TCG.performAddrSpaceCast( - CGF.CGM, GV, - llvm::PointerType::get( - CGF.getLLVMContext(), - CGF.getContext().getTargetAddressSpace(LangAS::Default))); + C = CGF.CGM.performAddrSpaceCast( + GV, llvm::PointerType::get( + CGF.getLLVMContext(), + CGF.getContext().getTargetAddressSpace(LangAS::Default))); // FIXME: Should we put the new global into a COMDAT? return RawAddress(C, GV->getValueType(), alignment); } @@ -3660,14 +3656,14 @@ LValue CodeGenFunction::EmitDeclRefLValue(const DeclRefExpr *E) { AlignmentSource::Decl); if (const auto *TPO = dyn_cast<TemplateParamObjectDecl>(ND)) { - auto ATPO = CGM.GetAddrOfTemplateParamObject(TPO); + ConstantAddress ATPO = CGM.GetAddrOfTemplateParamObject(TPO); auto AS = getLangASFromTargetAS(ATPO.getAddressSpace()); if (AS != T.getAddressSpace()) { auto TargetAS = getContext().getTargetAddressSpace(T.getAddressSpace()); - auto PtrTy = llvm::PointerType::get(CGM.getLLVMContext(), TargetAS); - auto ASC = - getTargetHooks().performAddrSpaceCast(CGM, ATPO.getPointer(), PtrTy); + llvm::Type *PtrTy = + llvm::PointerType::get(CGM.getLLVMContext(), TargetAS); + llvm::Constant *ASC = CGM.performAddrSpaceCast(ATPO.getPointer(), PtrTy); ATPO = ConstantAddress(ASC, ATPO.getElementType(), ATPO.getAlignment()); } @@ -6118,8 +6114,8 @@ LValue CodeGenFunction::EmitCastLValue(const CastExpr *E) { case CK_AddressSpaceConversion: { LValue LV = EmitLValue(E->getSubExpr()); QualType DestTy = getContext().getPointerType(E->getType()); - llvm::Value *V = getTargetHooks().performAddrSpaceCast( - *this, LV.getPointer(*this), ConvertType(DestTy)); + llvm::Value *V = + performAddrSpaceCast(LV.getPointer(*this), ConvertType(DestTy)); return MakeAddrLValue(Address(V, ConvertTypeForMem(E->getType()), LV.getAddress().getAlignment()), E->getType(), LV.getBaseInfo(), LV.getTBAAInfo()); diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp index fc4c3d699ceda..c8e1fe69da9c7 100644 --- a/clang/lib/CodeGen/CGExprCXX.cpp +++ b/clang/lib/CodeGen/CGExprCXX.cpp @@ -113,7 +113,7 @@ RValue CodeGenFunction::EmitCXXDestructorCall( if (SrcAS != DstAS) { QualType DstTy = DtorDecl->getThisType(); llvm::Type *NewType = CGM.getTypes().ConvertType(DstTy); - This = getTargetHooks().performAddrSpaceCast(*this, This, NewType); + This = performAddrSpaceCast(This, NewType); } CallArgList Args; @@ -2179,10 +2179,10 @@ llvm::Value *CodeGenFunction::EmitCXXTypeidExpr(const CXXTypeidExpr *E) { llvm::Type *PtrTy = Int8PtrTy; LangAS GlobAS = CGM.GetGlobalVarAddressSpace(nullptr); - auto MaybeASCast = [=](auto &&TypeInfo) { + auto MaybeASCast = [=](llvm::Constant *TypeInfo) { if (GlobAS == LangAS::Default) return TypeInfo; - return getTargetHooks().performAddrSpaceCast(CGM, TypeInfo, PtrTy); + return CGM.performAddrSpaceCast(TypeInfo, PtrTy); }; if (E->isTypeOperand()) { diff --git a/clang/lib/CodeGen/CGExprConstant.cpp b/clang/lib/CodeGen/CGExprConstant.cpp index b785b4b083c04..c316642a87baf 100644 --- a/clang/lib/CodeGen/CGExprConstant.cpp +++ b/clang/lib/CodeGen/CGExprConstant.cpp @@ -1224,11 +1224,11 @@ class ConstExprEmitter } case CK_AddressSpaceConversion: { - auto C = Emitter.tryEmitPrivate(subExpr, subExpr->getType()); + llvm::Constant *C = Emitter.tryEmitPrivate(subExpr, subExpr->getType()); if (!C) return nullptr; llvm::Type *destTy = ConvertType(E->getType()); - return CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGM, C, destTy); + return CGM.performAddrSpaceCast(C, destTy); } case CK_LValueToRValue: { diff --git a/clang/lib/CodeGen/CGExprScalar.cpp b/clang/lib/CodeGen/CGExprScalar.cpp index 832b7cf781fdf..d21e017bd2b56 100644 --- a/clang/lib/CodeGen/CGExprScalar.cpp +++ b/clang/lib/CodeGen/CGExprScalar.cpp @@ -2564,8 +2564,7 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { // detail, and doing an AS cast here still retains the semantics the user // expects. It is desirable to remove this iff a better solution is found. if (auto A = dyn_cast<llvm::Argument>(Src); A && A->hasStructRetAttr()) - return CGF.CGM.getTargetCodeGenInfo().performAddrSpaceCast(CGF, Src, - DstTy); + return CGF.performAddrSpaceCast(Src, DstTy); assert( (!SrcTy->isPtrOrPtrVectorTy() || !DstTy->isPtrOrPtrVectorTy() || @@ -2709,8 +2708,7 @@ Value *ScalarExprEmitter::VisitCastExpr(CastExpr *CE) { } // Since target may map different address spaces in AST to the same address // space, an address space conversion may end up as a bitcast. - return CGF.CGM.getTargetCodeGenInfo().performAddrSpaceCast( - CGF, Visit(E), ConvertType(DestTy)); + return CGF.performAddrSpaceCast(Visit(E), ConvertType(DestTy)); } case CK_AtomicToNonAtomic: case CK_NonAtomicToAtomic: diff --git a/clang/lib/CodeGen/CodeGenFunction.h b/clang/lib/CodeGen/CodeGenFunction.h index 044b9834b1a92..b308fd4b74806 100644 --- a/clang/lib/CodeGen/CodeGenFunction.h +++ b/clang/lib/CodeGen/CodeGenFunction.h @@ -447,6 +447,12 @@ class CodeGenFunction : public CodeGenTypeCache { return PostAllocaInsertPt; } + // Try to preserve the source's name to make IR more readable. + llvm::Value *performAddrSpaceCast(llvm::Value *Src, llvm::Type *DestTy) { + return Builder.CreateAddrSpaceCast( + Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : ""); + } + /// API for captured statement code generation. class CGCapturedStmtInfo { public: diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp index 2f02859e8b174..cacbabc0290ec 100644 --- a/clang/lib/CodeGen/CodeGenModule.cpp +++ b/clang/lib/CodeGen/CodeGenModule.cpp @@ -5688,10 +5688,9 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty, D ? D->getType().getAddressSpace() : (LangOpts.OpenCL ? LangAS::opencl_global : LangAS::Default); assert(getContext().getTargetAddressSpace(ExpectedAS) == TargetAS); - if (DAddrSpace != ExpectedAS) { - return getTargetCodeGenInfo().performAddrSpaceCast( - *this, GV, llvm::PointerType::get(getLLVMContext(), TargetAS)); - } + if (DAddrSpace != ExpectedAS) + return performAddrSpaceCast( + GV, llvm::PointerType::get(getLLVMContext(), TargetAS)); return GV; } @@ -5923,11 +5922,10 @@ castStringLiteralToDefaultAddressSpace(CodeGenModule &CGM, if (!CGM.getLangOpts().OpenCL) { auto AS = CGM.GetGlobalConstantAddressSpace(); if (AS != LangAS::Default) - Cast = CGM.getTargetCodeGenInfo().performAddrSpaceCast( - CGM, GV, - llvm::PointerType::get( - CGM.getLLVMContext(), - CGM.getContext().getTargetAddressSpace(LangAS::Default))); + Cast = CGM.performAddrSpaceCast( + GV, llvm::PointerType::get( + CGM.getLLVMContext(), + CGM.getContext().getTargetAddressSpace(LangAS::Default))); } return Cast; } @@ -7350,11 +7348,10 @@ ConstantAddress CodeGenModule::GetAddrOfGlobalTemporary( setTLSMode(GV, *VD); llvm::Constant *CV = GV; if (AddrSpace != LangAS::Default) - CV = getTargetCodeGenInfo().performAddrSpaceCast( - *this, GV, - llvm::PointerType::get( - getLLVMContext(), - getContext().getTargetAddressSpace(LangAS::Default))); + CV = performAddrSpaceCast( + GV, llvm::PointerType::get( + getLLVMContext(), + getContext().getTargetAddressSpace(LangAS::Default))); // Update the map with the new temporary. If we created a placeholder above, // replace it with the new global now. diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h index cc18e21d45759..3ed1dd7a57225 100644 --- a/clang/lib/CodeGen/CodeGenModule.h +++ b/clang/lib/CodeGen/CodeGenModule.h @@ -1872,6 +1872,13 @@ class CodeGenModule : public CodeGenTypeCache { return TrapReasonBuilder(&getDiags(), DiagID, TR); } + llvm::Constant *performAddrSpaceCast(llvm::Constant *Src, + llvm::Type *DestTy) { + // Since target may map different address spaces in AST to the same address + // space, an address space conversion may end up as a bitcast. + return llvm::ConstantExpr::getPointerCast(Src, DestTy); + } + std::optional<llvm::Attribute::AttrKind> StackProtectorAttribute(const Decl *D) const; diff --git a/clang/lib/CodeGen/TargetInfo.cpp b/clang/lib/CodeGen/TargetInfo.cpp index 3cdb76e2a760c..51d3ad384f934 100644 --- a/clang/lib/CodeGen/TargetInfo.cpp +++ b/clang/lib/CodeGen/TargetInfo.cpp @@ -148,27 +148,6 @@ LangAS TargetCodeGenInfo::getGlobalVarAddressSpace(CodeGenModule &CGM, return D ? D->getType().getAddressSpace() : LangAS::Default; } -llvm::Value * -TargetCodeGenInfo::performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, - llvm::Value *Src, llvm::Type *DestTy, - bool isNonNull) const { - // Since target may map different address spaces in AST to the same address - // space, an address space conversion may end up as a bitcast. - if (auto *C = dyn_cast<llvm::Constant>(Src)) - return performAddrSpaceCast(CGF.CGM, C, DestTy); - // Try to preserve the source's name to make IR more readable. - return CGF.Builder.CreateAddrSpaceCast( - Src, DestTy, Src->hasName() ? Src->getName() + ".ascast" : ""); -} - -llvm::Constant * -TargetCodeGenInfo::performAddrSpaceCast(CodeGenModule &CGM, llvm::Constant *Src, - llvm::Type *DestTy) const { - // Since target may map different address spaces in AST to the same address - // space, an address space conversion may end up as a bitcast. - return llvm::ConstantExpr::getPointerCast(Src, DestTy); -} - llvm::SyncScope::ID TargetCodeGenInfo::getLLVMSyncScopeID(const LangOptions &LangOpts, SyncScope Scope, diff --git a/clang/lib/CodeGen/TargetInfo.h b/clang/lib/CodeGen/TargetInfo.h index 6ea8dfc79d3a1..6394ef4cb0180 100644 --- a/clang/lib/CodeGen/TargetInfo.h +++ b/clang/lib/CodeGen/TargetInfo.h @@ -321,25 +321,6 @@ class TargetCodeGenInfo { /// Get the AST address space for alloca. virtual LangAS getASTAllocaAddressSpace() const { return LangAS::Default; } - Address performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, Address Addr, - llvm::Type *DestTy, - bool IsNonNull = false) const; - - /// Perform address space cast of an expression of pointer type. - /// \param V is the LLVM value to be casted to another address space. - /// \param DestTy is the destination LLVM pointer type. - /// \param IsNonNull is the flag indicating \p V is known to be non null. - virtual llvm::Value *performAddrSpaceCast(CodeGen::CodeGenFunction &CGF, - llvm::Value *V, llvm::Type *DestTy, - bool IsNonNull = false) const; - - /// Perform address space cast of a constant expression of pointer type. - /// \param V is the LLVM constant to be casted to another address space. - /// \param DestTy is the destination LLVM pointer type. - virtual llvm::Constant *performAddrSpaceCast(CodeGenModule &CGM, - llvm::Constant *V, - llvm::Type *DestTy) const; - /// Get address space of pointer parameter for __cxa_atexit. virtual LangAS getAddrSpaceOfCxaAtexitPtrParam() const { return LangAS::Default; _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
