Skip to content

Commit e571d1c

Browse files
authored
[CIR] Simplify ConstantOp accesses and its getDefiningOp (#1764)
- Replaces dyn_cast<cir::ConstantOp>(v.getDefiningOp()) and similar with v.getDefiningOp<cir::ConstantOp>() - Adds `getValueAttr`, `getIntValue` and `getBoolValue` methods to ConstantOp
1 parent d3ee486 commit e571d1c

File tree

15 files changed

+94
-99
lines changed

15 files changed

+94
-99
lines changed

clang/include/clang/CIR/Dialect/IR/CIROps.td

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,21 @@ def CIR_ConstantOp : CIR_Op<"const",[
459459
return ptrAttr.isNullValue();
460460
return false;
461461
}
462+
463+
template <typename T>
464+
T getValueAttr() { return mlir::dyn_cast<T>(getValue()); }
465+
466+
llvm::APInt getIntValue() {
467+
if (const auto intAttr = getValueAttr<cir::IntAttr>())
468+
return intAttr.getValue();
469+
llvm_unreachable("Expected an IntAttr in ConstantOp");
470+
}
471+
472+
bool getBoolValue() {
473+
if (const auto boolAttr = getValueAttr<cir::BoolAttr>())
474+
return boolAttr.getValue();
475+
llvm_unreachable("Expected a BoolAttr in ConstantOp");
476+
}
462477
}];
463478

464479
let hasFolder = 1;

clang/lib/CIR/CodeGen/CIRGenAtomic.cpp

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -330,40 +330,41 @@ Address AtomicInfo::CreateTempAlloca() const {
330330
return TempAlloca;
331331
}
332332

333-
// If the value comes from a ConstOp + IntAttr, retrieve and skip a series
334-
// of casts if necessary.
335-
//
336-
// FIXME(cir): figure out warning issue and move this to CIRBaseBuilder.h
337-
static cir::IntAttr getConstOpIntAttr(mlir::Value v) {
338-
mlir::Operation *op = v.getDefiningOp();
339-
cir::IntAttr constVal;
340-
while (auto c = dyn_cast<cir::CastOp>(op))
341-
op = c.getOperand().getDefiningOp();
342-
if (auto c = dyn_cast<cir::ConstantOp>(op)) {
343-
if (mlir::isa<cir::IntType>(c.getType()))
344-
constVal = mlir::cast<cir::IntAttr>(c.getValue());
345-
}
346-
return constVal;
333+
static mlir::Value stripCasts(mlir::Value value) {
334+
while (auto castOp = value.getDefiningOp<cir::CastOp>())
335+
value = castOp.getOperand();
336+
return value;
337+
}
338+
339+
static cir::ConstantOp extractConstant(mlir::Value v) {
340+
return stripCasts(v).getDefiningOp<cir::ConstantOp>();
341+
}
342+
343+
// If the value comes from a ConstOp + IntAttr, retrieve and skip a series of
344+
// casts if necessary.
345+
static cir::IntAttr extractIntAttr(mlir::Value v) {
346+
if (auto c = extractConstant(v))
347+
return c.getValueAttr<cir::IntAttr>();
348+
return {};
347349
}
348350

349351
// Inspect a value that is the strong/weak flag for a compare-exchange. If it
350352
// is a constant of intergral or boolean type, set `val` to the constant's
351353
// boolean value and return true. Otherwise leave `val` unchanged and return
352354
// false.
353355
static bool isCstWeak(mlir::Value weakVal, bool &val) {
354-
mlir::Operation *op = weakVal.getDefiningOp();
355-
while (auto c = dyn_cast<cir::CastOp>(op)) {
356-
op = c.getOperand().getDefiningOp();
357-
}
358-
if (auto c = dyn_cast<cir::ConstantOp>(op)) {
359-
if (mlir::isa<cir::IntType>(c.getType())) {
360-
val = mlir::cast<cir::IntAttr>(c.getValue()).getUInt() != 0;
356+
if (auto c = extractConstant(weakVal)) {
357+
if (auto attr = c.getValueAttr<cir::IntAttr>()) {
358+
val = attr.getUInt() != 0;
361359
return true;
362-
} else if (mlir::isa<cir::BoolType>(c.getType())) {
363-
val = mlir::cast<cir::BoolAttr>(c.getValue()).getValue();
360+
}
361+
362+
if (auto attr = c.getValueAttr<cir::BoolAttr>()) {
363+
val = attr.getValue();
364364
return true;
365365
}
366366
}
367+
367368
return false;
368369
}
369370

@@ -456,7 +457,7 @@ static void emitAtomicCmpXchgFailureSet(
456457
cir::MemOrder SuccessOrder, cir::MemScopeKind Scope) {
457458

458459
cir::MemOrder FailureOrder;
459-
if (auto ordAttr = getConstOpIntAttr(FailureOrderVal)) {
460+
if (auto ordAttr = extractIntAttr(FailureOrderVal)) {
460461
// We should not ever get to a case where the ordering isn't a valid CABI
461462
// value, but it's hard to enforce that in general.
462463
auto ord = ordAttr.getUInt();
@@ -805,7 +806,7 @@ static void emitAtomicOp(CIRGenFunction &CGF, AtomicExpr *Expr, Address Dest,
805806
}
806807

807808
// Handle constant scope.
808-
if (getConstOpIntAttr(Scope)) {
809+
if (extractIntAttr(Scope)) {
809810
assert(!cir::MissingFeatures::syncScopeID());
810811
llvm_unreachable("NYI");
811812
return;
@@ -1208,7 +1209,7 @@ RValue CIRGenFunction::emitAtomicExpr(AtomicExpr *E) {
12081209
E->getOp() == AtomicExpr::AO__scoped_atomic_load ||
12091210
E->getOp() == AtomicExpr::AO__scoped_atomic_load_n;
12101211

1211-
if (auto ordAttr = getConstOpIntAttr(Order)) {
1212+
if (auto ordAttr = extractIntAttr(Order)) {
12121213
// We should not ever get to a case where the ordering isn't a valid CABI
12131214
// value, but it's hard to enforce that in general.
12141215
auto ord = ordAttr.getUInt();

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -382,14 +382,11 @@ static mlir::Value makeAtomicFenceValue(CIRGenFunction &cgf,
382382
auto &builder = cgf.getBuilder();
383383
mlir::Value orderingVal = cgf.emitScalarExpr(expr->getArg(0));
384384

385-
auto constOrdering =
386-
mlir::dyn_cast<cir::ConstantOp>(orderingVal.getDefiningOp());
385+
auto constOrdering = orderingVal.getDefiningOp<cir::ConstantOp>();
387386
if (!constOrdering)
388387
llvm_unreachable("NYI: variable ordering not supported");
389388

390-
auto constOrderingAttr =
391-
mlir::dyn_cast<cir::IntAttr>(constOrdering.getValue());
392-
if (constOrderingAttr) {
389+
if (auto constOrderingAttr = constOrdering.getValueAttr<cir::IntAttr>()) {
393390
cir::MemOrder ordering =
394391
static_cast<cir::MemOrder>(constOrderingAttr.getUInt());
395392

clang/lib/CIR/CodeGen/CIRGenBuiltinAArch64.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2123,10 +2123,7 @@ castVecOfFPTypeToVecOfIntWithSameWidth(CIRGenBuilderTy &builder,
21232123

21242124
/// Get integer from a mlir::Value that is an int constant or a constant op.
21252125
static int64_t getIntValueFromConstOp(mlir::Value val) {
2126-
auto constOp = mlir::cast<cir::ConstantOp>(val.getDefiningOp());
2127-
return (mlir::cast<cir::IntAttr>(constOp.getValue()))
2128-
.getValue()
2129-
.getSExtValue();
2126+
return val.getDefiningOp<cir::ConstantOp>().getIntValue().getSExtValue();
21302127
}
21312128

21322129
static mlir::Value emitNeonSplat(CIRGenBuilderTy &builder, mlir::Location loc,

clang/lib/CIR/CodeGen/CIRGenBuiltinX86.cpp

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -68,10 +68,7 @@ translateX86ToMsvcIntrin(unsigned BuiltinID) {
6868

6969
/// Get integer from a mlir::Value that is an int constant or a constant op.
7070
static int64_t getIntValueFromConstOp(mlir::Value val) {
71-
auto constOp = mlir::cast<cir::ConstantOp>(val.getDefiningOp());
72-
return (mlir::cast<cir::IntAttr>(constOp.getValue()))
73-
.getValue()
74-
.getSExtValue();
71+
return val.getDefiningOp<cir::ConstantOp>().getIntValue().getSExtValue();
7572
}
7673

7774
// Convert the mask from an integer type to a vector of i1.
@@ -274,9 +271,8 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
274271
case X86::BI__builtin_ia32_vec_ext_v4di: {
275272
unsigned NumElts = cast<cir::VectorType>(Ops[0].getType()).getSize();
276273

277-
auto constOp = cast<cir::ConstantOp>(Ops[1].getDefiningOp());
278-
auto intAttr = cast<cir::IntAttr>(constOp.getValue());
279-
uint64_t index = intAttr.getValue().getZExtValue();
274+
uint64_t index =
275+
Ops[1].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
280276

281277
index &= NumElts - 1;
282278

@@ -301,9 +297,8 @@ mlir::Value CIRGenFunction::emitX86BuiltinExpr(unsigned BuiltinID,
301297
case X86::BI__builtin_ia32_vec_set_v4di: {
302298
unsigned NumElts = cast<cir::VectorType>(Ops[0].getType()).getSize();
303299

304-
auto constOp = cast<cir::ConstantOp>(Ops[2].getDefiningOp());
305-
auto intAttr = cast<cir::IntAttr>(constOp.getValue());
306-
uint64_t index = intAttr.getValue().getZExtValue();
300+
uint64_t index =
301+
Ops[2].getDefiningOp<cir::ConstantOp>().getIntValue().getZExtValue();
307302

308303
index &= NumElts - 1;
309304

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1833,13 +1833,16 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
18331833
// llvm::BranchInst *zeroCheckBranch = nullptr;
18341834

18351835
// Optimize for a constant count.
1836-
auto constantCount = dyn_cast<cir::ConstantOp>(numElements.getDefiningOp());
1837-
if (constantCount) {
1838-
auto constIntAttr = mlir::dyn_cast<cir::IntAttr>(constantCount.getValue());
1839-
// Just skip out if the constant count is zero.
1840-
if (constIntAttr && constIntAttr.getUInt() == 0)
1841-
return;
1842-
// Otherwise, emit the check.
1836+
if (auto constantCount = numElements.getDefiningOp<cir::ConstantOp>()) {
1837+
if (auto constIntAttr = constantCount.getValueAttr<cir::IntAttr>()) {
1838+
// Just skip out if the constant count is zero.
1839+
if (constIntAttr.getUInt() == 0)
1840+
return;
1841+
// Otherwise, emit the check.
1842+
}
1843+
1844+
if (constantCount.use_empty())
1845+
constantCount.erase();
18431846
} else {
18441847
llvm_unreachable("NYI");
18451848
}
@@ -1902,9 +1905,6 @@ void CIRGenFunction::emitCXXAggrConstructorCall(
19021905
builder.create<cir::YieldOp>(loc);
19031906
});
19041907
}
1905-
1906-
if (constantCount.use_empty())
1907-
constantCount.erase();
19081908
}
19091909

19101910
static bool canEmitDelegateCallArgs(CIRGenFunction &CGF,

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1149,13 +1149,16 @@ void CIRGenFunction::emitDestroy(Address addr, QualType type,
11491149
bool checkZeroLength = true;
11501150

11511151
// But if the array length is constant, we can suppress that.
1152-
auto constantCount = dyn_cast<cir::ConstantOp>(length.getDefiningOp());
1153-
if (constantCount) {
1154-
auto constIntAttr = mlir::dyn_cast<cir::IntAttr>(constantCount.getValue());
1155-
// ...and if it's constant zero, we can just skip the entire thing.
1156-
if (constIntAttr && constIntAttr.getUInt() == 0)
1157-
return;
1158-
checkZeroLength = false;
1152+
if (auto constantCount = length.getDefiningOp<cir::ConstantOp>()) {
1153+
if (auto constIntAttr = constantCount.getValueAttr<cir::IntAttr>()) {
1154+
// ...and if it's constant zero, we can just skip the entire thing.
1155+
if (constIntAttr.getUInt() == 0)
1156+
return;
1157+
checkZeroLength = false;
1158+
}
1159+
1160+
if (constantCount.use_empty())
1161+
constantCount.erase();
11591162
} else {
11601163
llvm_unreachable("NYI");
11611164
}
@@ -1164,8 +1167,6 @@ void CIRGenFunction::emitDestroy(Address addr, QualType type,
11641167
mlir::Value end; // Use this for future non-constant counts.
11651168
emitArrayDestroy(begin, end, type, elementAlign, destroyer, checkZeroLength,
11661169
useEHCleanupForArray);
1167-
if (constantCount.use_empty())
1168-
constantCount.erase();
11691170
}
11701171

11711172
CIRGenFunction::Destroyer *

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1680,17 +1680,16 @@ static bool isPreserveAIArrayBase(CIRGenFunction &CGF, const Expr *ArrayBase) {
16801680

16811681
static mlir::IntegerAttr getConstantIndexOrNull(mlir::Value idx) {
16821682
// TODO(cir): should we consider using MLIRs IndexType instead of IntegerAttr?
1683-
if (auto constantOp = dyn_cast<cir::ConstantOp>(idx.getDefiningOp()))
1684-
return mlir::dyn_cast<mlir::IntegerAttr>(constantOp.getValue());
1683+
if (auto constantOp = idx.getDefiningOp<cir::ConstantOp>())
1684+
return constantOp.getValueAttr<mlir::IntegerAttr>();
16851685
return {};
16861686
}
16871687

16881688
static CharUnits getArrayElementAlign(CharUnits arrayAlign, mlir::Value idx,
16891689
CharUnits eltSize) {
16901690
// If we have a constant index, we can use the exact offset of the
16911691
// element we're accessing.
1692-
auto constantIdx = getConstantIndexOrNull(idx);
1693-
if (constantIdx) {
1692+
if (auto constantIdx = getConstantIndexOrNull(idx)) {
16941693
CharUnits offset = constantIdx.getValue().getZExtValue() * eltSize;
16951694
return arrayAlign.alignmentAtOffset(offset);
16961695
// Otherwise, use the worst-case alignment for any element.

clang/lib/CIR/CodeGen/CIRGenExprCXX.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1099,12 +1099,12 @@ void CIRGenFunction::emitNewArrayInitializer(
10991099

11001100
// If all elements have already been initialized, skip any further
11011101
// initialization.
1102-
auto ConstOp = dyn_cast<cir::ConstantOp>(NumElements.getDefiningOp());
1103-
if (ConstOp) {
1104-
auto ConstIntAttr = mlir::dyn_cast<cir::IntAttr>(ConstOp.getValue());
1105-
// Just skip out if the constant count is zero.
1106-
if (ConstIntAttr && ConstIntAttr.getUInt() <= InitListElements)
1107-
return;
1102+
if (auto ConstOp = NumElements.getDefiningOp<cir::ConstantOp>()) {
1103+
if (auto ConstIntAttr = ConstOp.getValueAttr<cir::IntAttr>()) {
1104+
// Just skip out if the constant count is zero.
1105+
if (ConstIntAttr.getUInt() <= InitListElements)
1106+
return;
1107+
}
11081108
}
11091109

11101110
assert(Init && "have trailing elements to initialize but no initializer");

clang/lib/CIR/CodeGen/CIRGenExprConst.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2113,8 +2113,7 @@ mlir::Attribute ConstantEmitter::emitAbstract(SourceLocation loc,
21132113
mlir::Attribute ConstantEmitter::emitNullForMemory(mlir::Location loc,
21142114
CIRGenModule &CGM,
21152115
QualType T) {
2116-
auto cstOp =
2117-
dyn_cast<cir::ConstantOp>(CGM.emitNullConstant(T, loc).getDefiningOp());
2116+
auto cstOp = CGM.emitNullConstant(T, loc).getDefiningOp<cir::ConstantOp>();
21182117
assert(cstOp && "expected cir.const op");
21192118
return emitForMemory(CGM, cstOp.getValue(), T);
21202119
}

0 commit comments

Comments
 (0)