Skip to content

Commit 979c034

Browse files
committed
Update AddrCast to match OG and address other nits
1 parent 57443a9 commit 979c034

File tree

7 files changed

+88
-45
lines changed

7 files changed

+88
-45
lines changed

clang/include/clang/CIR/Dialect/IR/CIRTypes.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,9 @@
1313
#ifndef CLANG_CIR_DIALECT_IR_CIRTYPES_H
1414
#define CLANG_CIR_DIALECT_IR_CIRTYPES_H
1515

16+
#include "mlir/IR/Attributes.h"
1617
#include "mlir/IR/BuiltinAttributes.h"
18+
#include "mlir/IR/MLIRContext.h"
1719
#include "mlir/IR/Types.h"
1820
#include "mlir/Interfaces/DataLayoutInterfaces.h"
1921
#include "clang/Basic/AddressSpaces.h"
@@ -38,6 +40,15 @@ bool isValidFundamentalIntWidth(unsigned width);
3840
/// void, or abstract types.
3941
bool isSized(mlir::Type ty);
4042

43+
//===----------------------------------------------------------------------===//
44+
// AddressSpace helpers
45+
//===----------------------------------------------------------------------===//
46+
cir::TargetAddressSpaceAttr toCIRTargetAddressSpace(mlir::MLIRContext &context,
47+
clang::LangAS langAS);
48+
49+
bool isMatchingAddressSpace(cir::TargetAddressSpaceAttr cirAS,
50+
clang::LangAS as);
51+
4152
} // namespace cir
4253

4354
//===----------------------------------------------------------------------===//

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,6 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "CIRGenCall.h"
15-
#include "CIRGenConstantEmitter.h"
1615
#include "CIRGenFunction.h"
1716
#include "CIRGenModule.h"
1817
#include "CIRGenValue.h"
@@ -22,6 +21,7 @@
2221
#include "clang/AST/Expr.h"
2322
#include "clang/AST/GlobalDecl.h"
2423
#include "clang/Basic/Builtins.h"
24+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
2525
#include "clang/CIR/MissingFeatures.h"
2626
#include "llvm/Support/ErrorHandling.h"
2727

@@ -58,24 +58,6 @@ static RValue emitBuiltinBitOp(CIRGenFunction &cgf, const CallExpr *e,
5858
return RValue::get(result);
5959
}
6060

61-
// Initialize the alloca with the given size and alignment according to the lang
62-
// opts. Supporting only the trivial non-initialization for now.
63-
static void initializeAlloca(CIRGenFunction &cgf,
64-
[[maybe_unused]] mlir::Value allocaAddr,
65-
[[maybe_unused]] mlir::Value size,
66-
[[maybe_unused]] CharUnits alignmentInBytes) {
67-
68-
switch (cgf.getLangOpts().getTrivialAutoVarInit()) {
69-
case LangOptions::TrivialAutoVarInitKind::Uninitialized:
70-
// Nothing to initialize.
71-
return;
72-
case LangOptions::TrivialAutoVarInitKind::Zero:
73-
case LangOptions::TrivialAutoVarInitKind::Pattern:
74-
cgf.cgm.errorNYI("trivial auto var init");
75-
return;
76-
}
77-
}
78-
7961
RValue CIRGenFunction::emitRotate(const CallExpr *e, bool isRotateLeft) {
8062
mlir::Value input = emitScalarExpr(e->getArg(0));
8163
mlir::Value amount = emitScalarExpr(e->getArg(1));
@@ -190,28 +172,34 @@ RValue CIRGenFunction::emitBuiltinExpr(const GlobalDecl &gd, unsigned builtinID,
190172
builder.getUInt8Ty(), "bi_alloca", suitableAlignmentInBytes, size);
191173

192174
// Initialize the allocated buffer if required.
193-
if (builtinID != Builtin::BI__builtin_alloca_uninitialized)
194-
initializeAlloca(*this, allocaAddr, size, suitableAlignmentInBytes);
175+
if (builtinID != Builtin::BI__builtin_alloca_uninitialized) {
176+
177+
switch (getLangOpts().getTrivialAutoVarInit()) {
178+
case LangOptions::TrivialAutoVarInitKind::Uninitialized:
179+
// Nothing to initialize.
180+
break;
181+
case LangOptions::TrivialAutoVarInitKind::Zero:
182+
case LangOptions::TrivialAutoVarInitKind::Pattern:
183+
cgm.errorNYI("trivial auto var init");
184+
break;
185+
}
186+
}
195187

196188
// An alloca will always return a pointer to the alloca (stack) address
197189
// space. This address space need not be the same as the AST / Language
198190
// default (e.g. in C / C++ auto vars are in the generic address space). At
199191
// the AST level this is handled within CreateTempAlloca et al., but for the
200192
// builtin / dynamic alloca we have to handle it here.
201193

202-
LangAS allocaAddrSpace = clang::LangAS::Default;
203-
if (getCIRAllocaAddressSpace()) {
204-
allocaAddrSpace = clang::getLangASFromTargetAS(
205-
getCIRAllocaAddressSpace().getValue().getUInt());
206-
}
207-
LangAS exprAddrSpace = e->getType()->getPointeeType().getAddressSpace();
208-
if (exprAddrSpace != allocaAddrSpace) {
194+
if (!cir::isMatchingAddressSpace(
195+
getCIRAllocaAddressSpace(),
196+
e->getType()->getPointeeType().getAddressSpace())) {
209197
cgm.errorNYI(e->getSourceRange(), "Non-default address space for alloca");
210198
}
211199

212200
// Bitcast the alloca to the expected type.
213201
return RValue::get(builder.createBitcast(
214-
allocaAddr, builder.getVoidPtrTy(allocaAddrSpace)));
202+
allocaAddr, builder.getVoidPtrTy(getCIRAllocaAddressSpace())));
215203
}
216204

217205
case Builtin::BIcos:

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@
2424
#include "clang/AST/ExprCXX.h"
2525
#include "clang/Basic/AddressSpaces.h"
2626
#include "clang/Basic/TargetInfo.h"
27+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
2728
#include "clang/CIR/Dialect/IR/CIRDialect.h"
29+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
2830
#include "clang/CIR/MissingFeatures.h"
2931
#include <optional>
3032

@@ -1199,16 +1201,6 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
11991201
case CK_AtomicToNonAtomic:
12001202
case CK_ToUnion:
12011203
case CK_BaseToDerived:
1202-
case CK_AddressSpaceConversion: {
1203-
LValue lv = emitLValue(e->getSubExpr());
1204-
QualType destTy = getContext().getPointerType(e->getType());
1205-
mlir::Value v = getTargetHooks().performAddrSpaceCast(
1206-
*this, lv.getPointer(), convertType(destTy));
1207-
1208-
return makeAddrLValue(Address(v, convertTypeForMem(e->getType()),
1209-
lv.getAddress().getAlignment()),
1210-
e->getType(), lv.getBaseInfo());
1211-
}
12121204
case CK_ObjCObjectLValueCast:
12131205
case CK_VectorSplat:
12141206
case CK_ConstructorConversion:
@@ -1222,7 +1214,23 @@ LValue CIRGenFunction::emitCastLValue(const CastExpr *e) {
12221214

12231215
return {};
12241216
}
1217+
case CK_AddressSpaceConversion: {
1218+
LValue lv = emitLValue(e->getSubExpr());
1219+
QualType destTy = getContext().getPointerType(e->getType());
1220+
1221+
clang::LangAS srcLangAS = e->getSubExpr()->getType().getAddressSpace();
1222+
cir::TargetAddressSpaceAttr srcAS;
1223+
if (clang::isTargetAddressSpace(srcLangAS)) {
1224+
srcAS = cir::toCIRTargetAddressSpace(getMLIRContext(), srcLangAS);
1225+
}
1226+
1227+
mlir::Value v = getTargetHooks().performAddrSpaceCast(
1228+
*this, lv.getPointer(), srcAS, convertType(destTy));
12251229

1230+
return makeAddrLValue(Address(v, convertTypeForMem(e->getType()),
1231+
lv.getAddress().getAlignment()),
1232+
e->getType(), lv.getBaseInfo());
1233+
}
12261234
case CK_LValueBitCast: {
12271235
// This must be a reinterpret_cast (or c-style equivalent).
12281236
const auto *ce = cast<ExplicitCastExpr>(e);
@@ -2311,7 +2319,7 @@ Address CIRGenFunction::createTempAlloca(mlir::Type ty, CharUnits align,
23112319
}
23122320

23132321
if (dstTyAS != allocaAS) {
2314-
getTargetHooks().performAddrSpaceCast(*this, v,
2322+
getTargetHooks().performAddrSpaceCast(*this, v, getCIRAllocaAddressSpace(),
23152323
builder.getPointerTo(ty, dstTyAS));
23162324
}
23172325
return Address(v, ty, align);

clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "clang/AST/Expr.h"
1717
#include "clang/AST/StmtVisitor.h"
18+
#include "clang/CIR/Dialect/IR/CIRTypes.h"
1819
#include "clang/CIR/MissingFeatures.h"
1920

2021
#include "mlir/IR/Location.h"
@@ -1884,10 +1885,16 @@ mlir::Value ScalarExprEmitter::VisitCastExpr(CastExpr *ce) {
18841885
return cgf.cgm.emitNullConstant(destTy,
18851886
cgf.getLoc(subExpr->getExprLoc()));
18861887
}
1888+
1889+
clang::LangAS srcLangAS = subExpr->getType().getAddressSpace();
1890+
cir::TargetAddressSpaceAttr subExprAS;
1891+
if (clang::isTargetAddressSpace(srcLangAS)) {
1892+
subExprAS = cir::toCIRTargetAddressSpace(cgf.getMLIRContext(), srcLangAS);
1893+
}
18871894
// Since target may map different address spaces in AST to the same address
18881895
// space, an address space conversion may end up as a bitcast.
18891896
return cgf.cgm.getTargetCIRGenInfo().performAddrSpaceCast(
1890-
cgf, Visit(subExpr), convertType(destTy));
1897+
cgf, Visit(subExpr), subExprAS, convertType(destTy));
18911898
}
18921899

18931900
case CK_AtomicToNonAtomic: {

clang/lib/CIR/CodeGen/TargetInfo.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "TargetInfo.h"
22
#include "ABIInfo.h"
33
#include "CIRGenFunction.h"
4+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
45
#include "clang/CIR/Dialect/IR/CIRDialect.h"
56

67
using namespace clang;
@@ -71,10 +72,9 @@ bool TargetCIRGenInfo::isNoProtoCallVariadic(
7172
return false;
7273
}
7374

74-
mlir::Value TargetCIRGenInfo::performAddrSpaceCast(CIRGenFunction &cgf,
75-
mlir::Value v,
76-
mlir::Type destTy,
77-
bool isNonNull) const {
75+
mlir::Value TargetCIRGenInfo::performAddrSpaceCast(
76+
CIRGenFunction &cgf, mlir::Value v, cir::TargetAddressSpaceAttr srcAddr,
77+
mlir::Type destTy, bool isNonNull) const {
7878
// Since target may map different address spaces in AST to the same address
7979
// space, an address space conversion may end up as a bitcast.
8080
if (cir::GlobalOp globalOp = v.getDefiningOp<cir::GlobalOp>())

clang/lib/CIR/CodeGen/TargetInfo.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "ABIInfo.h"
1818
#include "CIRGenTypes.h"
1919
#include "clang/Basic/AddressSpaces.h"
20+
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
2021

2122
#include <memory>
2223
#include <utility>
@@ -53,8 +54,10 @@ class TargetCIRGenInfo {
5354
/// Perform address space cast of an expression of pointer type.
5455
/// \param V is the value to be casted to another address space.
5556
/// \param DestTy is the destination pointer type.
57+
/// \param srcAS is theaddress space of \p V.
5658
/// \param IsNonNull is the flag indicating \p V is known to be non null.
5759
virtual mlir::Value performAddrSpaceCast(CIRGenFunction &cgf, mlir::Value v,
60+
cir::TargetAddressSpaceAttr srcAddr,
5861
mlir::Type destTy,
5962
bool isNonNull = false) const;
6063

clang/lib/CIR/Dialect/IR/CIRTypes.cpp

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,16 @@
1212

1313
#include "clang/CIR/Dialect/IR/CIRTypes.h"
1414

15+
#include "mlir/IR/BuiltinAttributes.h"
1516
#include "mlir/IR/DialectImplementation.h"
17+
#include "mlir/IR/MLIRContext.h"
18+
#include "clang/Basic/AddressSpaces.h"
1619
#include "clang/CIR/Dialect/IR/CIRAttrs.h"
1720
#include "clang/CIR/Dialect/IR/CIRDialect.h"
1821
#include "clang/CIR/Dialect/IR/CIRTypesDetails.h"
1922
#include "clang/CIR/MissingFeatures.h"
23+
#include "llvm/ADT/APInt.h"
24+
#include "llvm/ADT/APSInt.h"
2025
#include "llvm/ADT/TypeSwitch.h"
2126

2227
//===----------------------------------------------------------------------===//
@@ -807,6 +812,27 @@ mlir::LogicalResult cir::VectorType::verify(
807812
// TargetAddressSpace definitions
808813
//===----------------------------------------------------------------------===//
809814

815+
cir::TargetAddressSpaceAttr
816+
cir::toCIRTargetAddressSpace(mlir::MLIRContext &context, clang::LangAS langAS) {
817+
return cir::TargetAddressSpaceAttr::get(
818+
&context,
819+
IntegerAttr::get(&context,
820+
llvm::APSInt(clang::toTargetAddressSpace(langAS))));
821+
}
822+
823+
bool cir::isMatchingAddressSpace(cir::TargetAddressSpaceAttr cirAS,
824+
clang::LangAS as) {
825+
// If there is no CIR target attr, consider it "default" and only match
826+
// when the AST address space is LangAS::Default.
827+
if (!cirAS)
828+
return as == clang::LangAS::Default;
829+
830+
if (!isTargetAddressSpace(as))
831+
return false;
832+
833+
return cirAS.getValue().getUInt() == toTargetAddressSpace(as);
834+
}
835+
810836
mlir::ParseResult parseTargetAddressSpace(mlir::AsmParser &p,
811837
cir::TargetAddressSpaceAttr &attr) {
812838
if (failed(p.parseKeyword("target_address_space")))

0 commit comments

Comments
 (0)