Skip to content

Commit 35f7f3d

Browse files
seven-milesmeenai
authored andcommitted
[CIR][CodeGen][NFC] Refactor performAddrSpaceCast to consume CIR address space attributes (llvm#747)
Originally `TargetCodeGenInfo::performAddrSpaceCast` consumes two *unused* parameters typed `clang::LangAS`. This PR changes its type to `mlir::cir::AddressSpaceAttr` to better fit the need of CIRGen. In [D32248: CodeGen: Cast alloca to expected address space](https://reviews.llvm.org/D32248), the author explained why these AS parameters are not used: > This is just the default implementation. The idea is that targets that need to do something more complex on a particular conversion — e.g. to make sure that null pointers are translated correctly when they have different bit-patterns — can easily do so. Further more, I'm confident that the CIR AS is also capable of providing custom behaviors like above for those targets.
1 parent a3e59c3 commit 35f7f3d

File tree

5 files changed

+20
-12
lines changed

5 files changed

+20
-12
lines changed

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1055,8 +1055,9 @@ RValue CIRGenFunction::buildBuiltinExpr(const GlobalDecl GD, unsigned BuiltinID,
10551055
// the AST level this is handled within CreateTempAlloca et al., but for the
10561056
// builtin / dynamic alloca we have to handle it here.
10571057
assert(!MissingFeatures::addressSpace());
1058-
LangAS AAS = getASTAllocaAddressSpace();
1059-
LangAS EAS = E->getType()->getPointeeType().getAddressSpace();
1058+
auto AAS = builder.getAddrSpaceAttr(getASTAllocaAddressSpace());
1059+
auto EAS = builder.getAddrSpaceAttr(
1060+
E->getType()->getPointeeType().getAddressSpace());
10601061
if (EAS != AAS) {
10611062
assert(false && "Non-default address space for alloca NYI");
10621063
}

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1999,9 +1999,11 @@ LValue CIRGenFunction::buildCastLValue(const CastExpr *E) {
19991999
case CK_AddressSpaceConversion: {
20002000
LValue LV = buildLValue(E->getSubExpr());
20012001
QualType DestTy = getContext().getPointerType(E->getType());
2002+
auto SrcAS = builder.getAddrSpaceAttr(
2003+
E->getSubExpr()->getType().getAddressSpace());
2004+
auto DestAS = builder.getAddrSpaceAttr(E->getType().getAddressSpace());
20022005
mlir::Value V = getTargetHooks().performAddrSpaceCast(
2003-
*this, LV.getPointer(), E->getSubExpr()->getType().getAddressSpace(),
2004-
E->getType().getAddressSpace(), ConvertType(DestTy));
2006+
*this, LV.getPointer(), SrcAS, DestAS, ConvertType(DestTy));
20052007
assert(!MissingFeatures::tbaa());
20062008
return makeAddrLValue(Address(V, getTypes().convertTypeForMem(E->getType()),
20072009
LV.getAddress().getAlignment()),

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1604,9 +1604,12 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *CE) {
16041604
}
16051605
// Since target may map different address spaces in AST to the same address
16061606
// space, an address space conversion may end up as a bitcast.
1607+
auto SrcAS = CGF.builder.getAddrSpaceAttr(
1608+
E->getType()->getPointeeType().getAddressSpace());
1609+
auto DestAS = CGF.builder.getAddrSpaceAttr(
1610+
DestTy->getPointeeType().getAddressSpace());
16071611
return CGF.CGM.getTargetCIRGenInfo().performAddrSpaceCast(
1608-
CGF, Visit(E), E->getType()->getPointeeType().getAddressSpace(),
1609-
DestTy->getPointeeType().getAddressSpace(), ConvertType(DestTy));
1612+
CGF, Visit(E), SrcAS, DestAS, ConvertType(DestTy));
16101613
}
16111614
case CK_AtomicToNonAtomic:
16121615
llvm_unreachable("NYI");

clang/lib/CIR/CodeGen/TargetInfo.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -530,8 +530,9 @@ ABIArgInfo X86_64ABIInfo::classifyReturnType(QualType RetTy) const {
530530
}
531531

532532
mlir::Value TargetCIRGenInfo::performAddrSpaceCast(
533-
CIRGenFunction &CGF, mlir::Value Src, clang::LangAS SrcAddr,
534-
clang::LangAS DestAddr, mlir::Type DestTy, bool IsNonNull) const {
533+
CIRGenFunction &CGF, mlir::Value Src, mlir::cir::AddressSpaceAttr SrcAddr,
534+
mlir::cir::AddressSpaceAttr DestAddr, mlir::Type DestTy,
535+
bool IsNonNull) const {
535536
// Since target may map different address spaces in AST to the same address
536537
// space, an address space conversion may end up as a bitcast.
537538
if (auto globalOp = Src.getDefiningOp<mlir::cir::GlobalOp>())

clang/lib/CIR/CodeGen/TargetInfo.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "ABIInfo.h"
1818
#include "CIRGenValue.h"
1919
#include "mlir/IR/Types.h"
20+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
2021

2122
#include <memory>
2223

@@ -68,13 +69,13 @@ class TargetCIRGenInfo {
6869

6970
/// Perform address space cast of an expression of pointer type.
7071
/// \param V is the value to be casted to another address space.
71-
/// \param SrcAddr is the language address space of \p V.
72-
/// \param DestAddr is the targeted language address space.
72+
/// \param SrcAddr is the CIR address space of \p V.
73+
/// \param DestAddr is the targeted CIR address space.
7374
/// \param DestTy is the destination pointer type.
7475
/// \param IsNonNull is the flag indicating \p V is known to be non null.
7576
virtual mlir::Value performAddrSpaceCast(CIRGenFunction &CGF, mlir::Value V,
76-
clang::LangAS SrcAddr,
77-
clang::LangAS DestAddr,
77+
mlir::cir::AddressSpaceAttr SrcAddr,
78+
mlir::cir::AddressSpaceAttr DestAddr,
7879
mlir::Type DestTy,
7980
bool IsNonNull = false) const;
8081

0 commit comments

Comments
 (0)