Skip to content

Commit 66a6ed2

Browse files
committed
Update on "[CIR][Lowering] Lower globals with global_view initializer"
Adds lowering logic for CIR's global_view attributes with no indexes. This is done by converting the global to a region-initialized LLVM global operation, where the region returns the address of the global used in the gloval_view initializer attribute. [ghstack-poisoned]
2 parents 4e1c5cb + 64d0816 commit 66a6ed2

File tree

4 files changed

+25
-28
lines changed

4 files changed

+25
-28
lines changed

clang/lib/CIR/CodeGen/CIRGenBuilder.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,19 @@ class CIRGenBuilderTy : public mlir::OpBuilder {
104104
//
105105

106106
/// Get constant address of a global variable as an MLIR attribute.
107-
mlir::Attribute getGlobalViewAttr(mlir::cir::GlobalOp globalOp) {
107+
/// This wrapper infers the attribute type through the global op.
108+
mlir::cir::GlobalViewAttr getGlobalViewAttr(mlir::cir::GlobalOp globalOp,
109+
mlir::ArrayAttr indices = {}) {
108110
auto type = getPointerTo(globalOp.getSymType());
109-
auto symRef = mlir::FlatSymbolRefAttr::get(globalOp.getSymNameAttr());
110-
return mlir::cir::GlobalViewAttr::get(type, symRef);
111+
return getGlobalViewAttr(type, globalOp, indices);
112+
}
113+
114+
/// Get constant address of a global variable as an MLIR attribute.
115+
mlir::cir::GlobalViewAttr getGlobalViewAttr(mlir::cir::PointerType type,
116+
mlir::cir::GlobalOp globalOp,
117+
mlir::ArrayAttr indices = {}) {
118+
auto symbol = mlir::FlatSymbolRefAttr::get(globalOp.getSymNameAttr());
119+
return mlir::cir::GlobalViewAttr::get(type, symbol, indices);
111120
}
112121

113122
mlir::TypedAttr getZeroAttr(mlir::Type t) {

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -997,15 +997,13 @@ namespace {
997997
/// A struct which can be used to peephole certain kinds of finalization
998998
/// that normally happen during l-value emission.
999999
struct ConstantLValue {
1000-
// FIXME(cir): does it make sense to have both parent and child type here?
1001-
using SymbolTy = mlir::SymbolRefAttr;
1002-
llvm::PointerUnion<mlir::Value, SymbolTy, mlir::Attribute> Value;
1000+
llvm::PointerUnion<mlir::Value, mlir::Attribute> Value;
10031001
bool HasOffsetApplied;
10041002

10051003
/*implicit*/ ConstantLValue(mlir::Value value, bool hasOffsetApplied = false)
10061004
: Value(value), HasOffsetApplied(hasOffsetApplied) {}
10071005

1008-
/*implicit*/ ConstantLValue(SymbolTy address) : Value(address) {}
1006+
/*implicit*/ ConstantLValue(mlir::SymbolRefAttr address) : Value(address) {}
10091007

10101008
ConstantLValue(std::nullptr_t) : ConstantLValue({}, false) {}
10111009
ConstantLValue(mlir::Attribute value) : Value(value) {}
@@ -1095,15 +1093,13 @@ mlir::Attribute ConstantLValueEmitter::tryEmit() {
10951093
return {};
10961094

10971095
// Apply the offset if necessary and not already done.
1098-
if (!result.HasOffsetApplied && !value.is<mlir::SymbolRefAttr>()) {
1096+
if (!result.HasOffsetApplied && !value.is<mlir::Attribute>()) {
10991097
value = applyOffset(result).Value;
11001098
}
11011099

11021100
// Convert to the appropriate type; this could be an lvalue for
11031101
// an integer. FIXME: performAddrSpaceCast
11041102
if (destTy.isa<mlir::cir::PointerType>()) {
1105-
if (value.is<mlir::SymbolRefAttr>())
1106-
return value.get<mlir::SymbolRefAttr>();
11071103
if (value.is<mlir::Attribute>())
11081104
return value.get<mlir::Attribute>();
11091105
llvm_unreachable("NYI");

clang/lib/CIR/CodeGen/CIRGenItaniumCXXABI.cpp

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1107,9 +1107,8 @@ mlir::Attribute CIRGenItaniumRTTIBuilder::BuildTypeInfo(mlir::Location loc,
11071107
if (OldGV && !OldGV.isDeclaration()) {
11081108
assert(!OldGV.hasAvailableExternallyLinkage() &&
11091109
"available_externally typeinfos not yet implemented");
1110-
return mlir::cir::GlobalViewAttr::get(
1111-
CGM.getBuilder().getUInt8PtrTy(),
1112-
mlir::FlatSymbolRefAttr::get(OldGV.getSymNameAttr()));
1110+
return CGM.getBuilder().getGlobalViewAttr(CGM.getBuilder().getUInt8PtrTy(),
1111+
OldGV);
11131112
}
11141113

11151114
// Check if there is already an external RTTI descriptor for this type.
@@ -1275,10 +1274,9 @@ void CIRGenItaniumRTTIBuilder::BuildVTablePointer(mlir::Location loc,
12751274
} else {
12761275
SmallVector<mlir::Attribute, 4> offsets{
12771276
mlir::cir::IntAttr::get(PtrDiffTy, 2)};
1278-
field = mlir::cir::GlobalViewAttr::get(
1279-
builder.getUInt8PtrTy(),
1280-
mlir::FlatSymbolRefAttr::get(VTable.getSymNameAttr()),
1281-
mlir::ArrayAttr::get(builder.getContext(), offsets));
1277+
auto indices = mlir::ArrayAttr::get(builder.getContext(), offsets);
1278+
field = CGM.getBuilder().getGlobalViewAttr(CGM.getBuilder().getUInt8PtrTy(),
1279+
VTable, indices);
12821280
}
12831281

12841282
assert(field && "expected attribute");
@@ -1347,9 +1345,7 @@ CIRGenItaniumRTTIBuilder::GetAddrOfExternalRTTIDescriptor(mlir::Location loc,
13471345
llvm_unreachable("NYI");
13481346
}
13491347

1350-
return mlir::cir::GlobalViewAttr::get(
1351-
builder.getUInt8PtrTy(),
1352-
mlir::FlatSymbolRefAttr::get(GV.getSymNameAttr()));
1348+
return builder.getGlobalViewAttr(builder.getUInt8PtrTy(), GV);
13531349
}
13541350

13551351
mlir::Attribute CIRGenItaniumRTTIBuilder::BuildTypeInfo(
@@ -1374,9 +1370,8 @@ mlir::Attribute CIRGenItaniumRTTIBuilder::BuildTypeInfo(
13741370
// for global pointers. This is very ARM64-specific.
13751371
llvm_unreachable("NYI");
13761372
} else {
1377-
TypeNameField = mlir::cir::GlobalViewAttr::get(
1378-
builder.getUInt8PtrTy(),
1379-
mlir::FlatSymbolRefAttr::get(TypeName.getSymNameAttr()));
1373+
TypeNameField =
1374+
builder.getGlobalViewAttr(builder.getUInt8PtrTy(), TypeName);
13801375
}
13811376
Fields.push_back(TypeNameField);
13821377

@@ -1539,9 +1534,7 @@ mlir::Attribute CIRGenItaniumRTTIBuilder::BuildTypeInfo(
15391534
assert(!UnimplementedFeature::setDSOLocal());
15401535
CIRGenModule::setInitializer(GV, init);
15411536

1542-
return mlir::cir::GlobalViewAttr::get(
1543-
builder.getUInt8PtrTy(),
1544-
mlir::FlatSymbolRefAttr::get(GV.getSymNameAttr()));
1537+
return builder.getGlobalViewAttr(builder.getUInt8PtrTy(), GV);;
15451538
}
15461539

15471540
mlir::Attribute CIRGenItaniumCXXABI::getAddrOfRTTIDescriptor(mlir::Location loc,

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -708,8 +708,7 @@ CIRGenModule::getAddrOfGlobalVarAttr(const VarDecl *D, mlir::Type Ty,
708708
Ty = getTypes().convertTypeForMem(ASTTy);
709709

710710
auto globalOp = buildGlobal(D, Ty, IsForDefinition);
711-
auto symRef = mlir::FlatSymbolRefAttr::get(globalOp.getSymNameAttr());
712-
return mlir::cir::GlobalViewAttr::get(builder.getPointerTo(Ty), symRef);
711+
return builder.getGlobalViewAttr(builder.getPointerTo(Ty), globalOp);
713712
}
714713

715714
mlir::Operation* CIRGenModule::getWeakRefReference(const ValueDecl *VD) {

0 commit comments

Comments
 (0)