Skip to content

Commit 95ee215

Browse files
authored
Migrate uses of bar.dyn_cast<Foo>() to dyn_cast<Foo>(bar) (#394)
* Migrate method dyn_cast to llvm::dyn_cast ```bash perl -i -pe 's/([^ ]*)\.dyn_cast<([^>]*)>\(\)/llvm::dyn_cast<\2>(\1)/g' **/*.cpp **/*.h **/*.cc ``` * manually migrate some dyn_cast_or_null * strip llvm:: prefix * clang-format --------- Co-authored-by: Jeremy Kun <[email protected]>
1 parent 69c116a commit 95ee215

23 files changed

+265
-272
lines changed

lib/polygeist/Ops.cpp

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1631,9 +1631,9 @@ class CopySimplification final : public OpRewritePattern<T> {
16311631
Type elTy = dstTy.getElementType();
16321632

16331633
size_t width = 1;
1634-
if (auto IT = elTy.dyn_cast<IntegerType>())
1634+
if (auto IT = dyn_cast<IntegerType>(elTy))
16351635
width = IT.getWidth() / 8;
1636-
else if (auto FT = elTy.dyn_cast<FloatType>())
1636+
else if (auto FT = dyn_cast<FloatType>(elTy))
16371637
width = FT.getWidth() / 8;
16381638
else {
16391639
// TODO extend to llvm compatible type
@@ -1734,9 +1734,9 @@ class SetSimplification final : public OpRewritePattern<T> {
17341734
return failure();
17351735

17361736
size_t width = 1;
1737-
if (auto IT = elTy.dyn_cast<IntegerType>())
1737+
if (auto IT = dyn_cast<IntegerType>(elTy))
17381738
width = IT.getWidth() / 8;
1739-
else if (auto FT = elTy.dyn_cast<FloatType>())
1739+
else if (auto FT = dyn_cast<FloatType>(elTy))
17401740
width = FT.getWidth() / 8;
17411741
else {
17421742
// TODO extend to llvm compatible type
@@ -1787,7 +1787,7 @@ class SetSimplification final : public OpRewritePattern<T> {
17871787
SmallVector<Value> idxs;
17881788
Value val;
17891789

1790-
if (auto IT = elTy.dyn_cast<IntegerType>())
1790+
if (auto IT = dyn_cast<IntegerType>(elTy))
17911791
val =
17921792
rewriter.create<arith::ConstantIntOp>(op.getLoc(), 0, IT.getWidth());
17931793
else {
@@ -2479,7 +2479,7 @@ class SelectOfExt final : public OpRewritePattern<arith::SelectOp> {
24792479

24802480
LogicalResult matchAndRewrite(arith::SelectOp op,
24812481
PatternRewriter &rewriter) const override {
2482-
auto ty = op.getType().dyn_cast<IntegerType>();
2482+
auto ty = dyn_cast<IntegerType>(op.getType());
24832483
if (!ty)
24842484
return failure();
24852485
IntegerAttr lhs, rhs;
@@ -2589,10 +2589,10 @@ class CmpProp final : public OpRewritePattern<CmpIOp> {
25892589
v.getDefiningOp<LLVM::UndefOp>() ||
25902590
v.getDefiningOp<polygeist::UndefOp>();
25912591
if (auto extOp = v.getDefiningOp<ExtUIOp>())
2592-
if (auto it = extOp.getIn().getType().dyn_cast<IntegerType>())
2592+
if (auto it = dyn_cast<IntegerType>(extOp.getIn().getType()))
25932593
change |= it.getWidth() == 1;
25942594
if (auto extOp = v.getDefiningOp<ExtSIOp>())
2595-
if (auto it = extOp.getIn().getType().dyn_cast<IntegerType>())
2595+
if (auto it = dyn_cast<IntegerType>(extOp.getIn().getType()))
25962596
change |= it.getWidth() == 1;
25972597
}
25982598
if (!change) {
@@ -3175,7 +3175,7 @@ bool valueCmp(Cmp cmp, Value bval, ValueOrInt val) {
31753175
}
31763176
}
31773177

3178-
if (auto baval = bval.dyn_cast<BlockArgument>()) {
3178+
if (auto baval = dyn_cast<BlockArgument>(bval)) {
31793179
if (affine::AffineForOp afFor =
31803180
dyn_cast<affine::AffineForOp>(baval.getOwner()->getParentOp())) {
31813181
auto for_lb = afFor.getLowerBoundMap().getResults()[baval.getArgNumber()];
@@ -3487,7 +3487,7 @@ bool valueCmp(Cmp cmp, AffineExpr expr, size_t numDim, ValueRange operands,
34873487

34883488
// Range is [lb, ub)
34893489
bool rangeIncludes(Value bval, ValueOrInt lb, ValueOrInt ub) {
3490-
if (auto baval = bval.dyn_cast<BlockArgument>()) {
3490+
if (auto baval = dyn_cast<BlockArgument>(bval)) {
34913491
if (affine::AffineForOp afFor =
34923492
dyn_cast<affine::AffineForOp>(baval.getOwner()->getParentOp())) {
34933493
return valueCmp(
@@ -3621,7 +3621,7 @@ struct AffineIfSinking : public OpRewritePattern<affine::AffineIfOp> {
36213621
if (!opd) {
36223622
return failure();
36233623
}
3624-
auto ival = op.getOperands()[opd.getPosition()].dyn_cast<BlockArgument>();
3624+
auto ival = dyn_cast<BlockArgument>(op.getOperands()[opd.getPosition()]);
36253625
if (!ival) {
36263626
return failure();
36273627
}
@@ -3663,7 +3663,7 @@ struct AffineIfSinking : public OpRewritePattern<affine::AffineIfOp> {
36633663
if (!par.getRegion().isAncestor(v.getParentRegion()) ||
36643664
op.getThenRegion().isAncestor(v.getParentRegion()))
36653665
return;
3666-
if (auto ba = v.dyn_cast<BlockArgument>()) {
3666+
if (auto ba = dyn_cast<BlockArgument>(v)) {
36673667
if (ba.getOwner()->getParentOp() == par) {
36683668
return;
36693669
}
@@ -4285,7 +4285,7 @@ struct MergeNestedAffineParallelIf
42854285
continue;
42864286
}
42874287
if (auto dim = cur.dyn_cast<AffineDimExpr>()) {
4288-
auto ival = operands[dim.getPosition()].dyn_cast<BlockArgument>();
4288+
auto ival = dyn_cast<BlockArgument>(operands[dim.getPosition()]);
42894289
if (!ival || ival.getOwner()->getParentOp() != op) {
42904290
rhs = rhs + dim;
42914291
if (failure)
@@ -4315,7 +4315,7 @@ struct MergeNestedAffineParallelIf
43154315

43164316
if (auto dim = bop.getLHS().dyn_cast<AffineDimExpr>()) {
43174317
auto ival =
4318-
operands[dim.getPosition()].dyn_cast<BlockArgument>();
4318+
dyn_cast<BlockArgument>(operands[dim.getPosition()]);
43194319
if (!ival || ival.getOwner()->getParentOp() != op) {
43204320
rhs = rhs + bop;
43214321
// While legal, this may run before parallel merging
@@ -4511,7 +4511,7 @@ struct MergeParallelInductions
45114511
continue;
45124512
}
45134513
if (auto dim = cur.dyn_cast<AffineDimExpr>()) {
4514-
auto ival = operands[dim.getPosition()].dyn_cast<BlockArgument>();
4514+
auto ival = dyn_cast<BlockArgument>(operands[dim.getPosition()]);
45154515
if (!ival || ival.getOwner()->getParentOp() != op) {
45164516
rhs = rhs + dim;
45174517
continue;
@@ -4538,7 +4538,7 @@ struct MergeParallelInductions
45384538
}
45394539

45404540
if (auto dim = bop.getLHS().dyn_cast<AffineDimExpr>()) {
4541-
auto ival = operands[dim.getPosition()].dyn_cast<BlockArgument>();
4541+
auto ival = dyn_cast<BlockArgument>(operands[dim.getPosition()]);
45424542
if (!ival || ival.getOwner()->getParentOp() != op) {
45434543
rhs = rhs + bop;
45444544
continue;
@@ -4983,8 +4983,8 @@ template <typename T> struct BufferElimination : public OpRewritePattern<T> {
49834983
auto opd = map.getResults()[0].dyn_cast<AffineDimExpr>();
49844984
if (!opd)
49854985
continue;
4986-
auto val = ((Value)load.getMapOperands()[opd.getPosition()])
4987-
.dyn_cast<BlockArgument>();
4986+
auto val = dyn_cast<BlockArgument>(
4987+
((Value)load.getMapOperands()[opd.getPosition()]));
49884988
if (!val)
49894989
continue;
49904990

@@ -5033,8 +5033,8 @@ template <typename T> struct BufferElimination : public OpRewritePattern<T> {
50335033
auto opd = map.getResults()[0].dyn_cast<AffineDimExpr>();
50345034
if (!opd)
50355035
continue;
5036-
auto val = ((Value)load.getMapOperands()[opd.getPosition()])
5037-
.dyn_cast<BlockArgument>();
5036+
auto val = dyn_cast<BlockArgument>(
5037+
((Value)load.getMapOperands()[opd.getPosition()]));
50385038
if (!val)
50395039
continue;
50405040

@@ -5279,7 +5279,7 @@ struct AffineBufferElimination : public OpRewritePattern<T> {
52795279
}
52805280
if (storeIdxs[pair.index()].isValue) {
52815281
Value auval = storeIdxs[pair.index()].v_val;
5282-
BlockArgument bval = auval.dyn_cast<BlockArgument>();
5282+
BlockArgument bval = dyn_cast<BlockArgument>(auval);
52835283
if (!bval) {
52845284
LLVM_DEBUG(llvm::dbgs() << " + non bval expr " << bval << "\n");
52855285
continue;
@@ -5339,7 +5339,7 @@ struct AffineBufferElimination : public OpRewritePattern<T> {
53395339
if (!VI.isValue)
53405340
continue;
53415341
auto V = VI.v_val;
5342-
auto BA = V.dyn_cast<BlockArgument>();
5342+
auto BA = dyn_cast<BlockArgument>(V);
53435343
if (!BA) {
53445344
LLVM_DEBUG(llvm::dbgs() << " + non map oper " << V << "\n");
53455345
return failure();
@@ -5426,7 +5426,7 @@ struct AffineBufferElimination : public OpRewritePattern<T> {
54265426
if (!VI.isValue)
54275427
continue;
54285428
auto V = VI.v_val;
5429-
auto BA = V.dyn_cast<BlockArgument>();
5429+
auto BA = dyn_cast<BlockArgument>(V);
54305430
Operation *c = BA.getOwner()->getParentOp();
54315431
if (isa<affine::AffineParallelOp>(c) || isa<scf::ParallelOp>(c)) {
54325432
Operation *tmp = store;
@@ -5468,7 +5468,7 @@ struct AffineBufferElimination : public OpRewritePattern<T> {
54685468
if (storeIdxSet.count(V))
54695469
continue;
54705470

5471-
if (auto BA = V.dyn_cast<BlockArgument>()) {
5471+
if (auto BA = dyn_cast<BlockArgument>(V)) {
54725472
Operation *parent = BA.getOwner()->getParentOp();
54735473

54745474
if (auto sop = storeVal.getDefiningOp())
@@ -5518,10 +5518,10 @@ struct AffineBufferElimination : public OpRewritePattern<T> {
55185518
if (!isa<MemoryEffects::Read>(res.getEffect()))
55195519
return false;
55205520
unsigned addr = 0;
5521-
if (auto MT = v.getType().dyn_cast<MemRefType>())
5521+
if (auto MT = dyn_cast<MemRefType>(v.getType()))
55225522
addr = MT.getMemorySpaceAsInt();
55235523
else if (auto LT =
5524-
v.getType().dyn_cast<LLVM::LLVMPointerType>())
5524+
dyn_cast<LLVM::LLVMPointerType>(v.getType()))
55255525
addr = LT.getAddressSpace();
55265526
else
55275527
return false;

lib/polygeist/Passes/AffineCFG.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@ static bool legalCondition(Value en, bool dim = false) {
144144
// if (!outer || legalCondition(IC.getOperand(), false)) return true;
145145
//}
146146
if (!dim)
147-
if (auto BA = en.dyn_cast<BlockArgument>()) {
147+
if (auto BA = dyn_cast<BlockArgument>(en)) {
148148
if (isa<affine::AffineForOp, affine::AffineParallelOp>(
149149
BA.getOwner()->getParentOp()))
150150
return true;
@@ -730,7 +730,7 @@ static void setLocationAfter(PatternRewriter &b, mlir::Value val) {
730730
it++;
731731
b.setInsertionPoint(val.getDefiningOp()->getBlock(), it);
732732
}
733-
if (auto bop = val.dyn_cast<mlir::BlockArgument>())
733+
if (auto bop = dyn_cast<mlir::BlockArgument>(val))
734734
b.setInsertionPoint(bop.getOwner(), bop.getOwner()->begin());
735735
}
736736

@@ -745,7 +745,7 @@ struct IndexCastMovement : public OpRewritePattern<IndexCastOp> {
745745
}
746746

747747
mlir::Value val = op.getOperand();
748-
if (auto bop = val.dyn_cast<mlir::BlockArgument>()) {
748+
if (auto bop = dyn_cast<mlir::BlockArgument>(val)) {
749749
if (op.getOperation()->getBlock() != bop.getOwner()) {
750750
op.getOperation()->moveBefore(bop.getOwner(), bop.getOwner()->begin());
751751
return success();
@@ -1010,7 +1010,7 @@ bool isValidIndex(Value val) {
10101010
if (val.getDefiningOp<ConstantIntOp>())
10111011
return true;
10121012

1013-
if (auto ba = val.dyn_cast<BlockArgument>()) {
1013+
if (auto ba = dyn_cast<BlockArgument>(val)) {
10141014
auto *owner = ba.getOwner();
10151015
assert(owner);
10161016

lib/polygeist/Passes/CanonicalizeFor.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ struct ForBreakAddUpgrade : public OpRewritePattern<scf::ForOp> {
110110

111111
auto condition = outerIfOp.getCondition();
112112
// and that the outermost if's condition is an iter arg of the for
113-
auto condArg = condition.dyn_cast<BlockArgument>();
113+
auto condArg = dyn_cast<BlockArgument>(condition);
114114
if (!condArg)
115115
return failure();
116116
if (condArg.getOwner()->getParentOp() != forOp)
@@ -121,7 +121,7 @@ struct ForBreakAddUpgrade : public OpRewritePattern<scf::ForOp> {
121121
// and is false unless coming from inside the if
122122
auto forYieldOp = cast<scf::YieldOp>(block.getTerminator());
123123
auto opres =
124-
forYieldOp.getOperand(condArg.getArgNumber() - 1).dyn_cast<OpResult>();
124+
dyn_cast<OpResult>(forYieldOp.getOperand(condArg.getArgNumber() - 1));
125125
if (!opres)
126126
return failure();
127127
if (opres.getOwner() != outerIfOp)
@@ -143,7 +143,7 @@ struct ForBreakAddUpgrade : public OpRewritePattern<scf::ForOp> {
143143
if (opres.getResultNumber() == regionArg.getArgNumber() - 1)
144144
continue;
145145

146-
auto opres2 = forYieldOperand.dyn_cast<OpResult>();
146+
auto opres2 = dyn_cast<OpResult>(forYieldOperand);
147147
if (!opres2)
148148
continue;
149149
if (opres2.getOwner() != outerIfOp)
@@ -627,13 +627,13 @@ yop2.results()[idx]);
627627
*/
628628

629629
bool isTopLevelArgValue(Value value, Region *region) {
630-
if (auto arg = value.dyn_cast<BlockArgument>())
630+
if (auto arg = dyn_cast<BlockArgument>(value))
631631
return arg.getParentRegion() == region;
632632
return false;
633633
}
634634

635635
bool isBlockArg(Value value) {
636-
if (auto arg = value.dyn_cast<BlockArgument>())
636+
if (auto arg = dyn_cast<BlockArgument>(value))
637637
return true;
638638
return false;
639639
}
@@ -642,7 +642,7 @@ bool dominateWhile(Value value, WhileOp loop) {
642642
if (Operation *op = value.getDefiningOp()) {
643643
DominanceInfo dom(loop);
644644
return dom.properlyDominates(op, loop);
645-
} else if (auto arg = value.dyn_cast<BlockArgument>()) {
645+
} else if (auto arg = dyn_cast<BlockArgument>(value)) {
646646
return arg.getOwner()->getParentOp()->isProperAncestor(loop);
647647
} else {
648648
assert("????");
@@ -682,11 +682,11 @@ struct WhileToForHelper {
682682
negativeStep = false;
683683

684684
auto condOp = loop.getConditionOp();
685-
indVar = cmpIOp.getLhs().dyn_cast<BlockArgument>();
685+
indVar = dyn_cast<BlockArgument>(cmpIOp.getLhs());
686686
Type extType = nullptr;
687687
// todo handle ext
688688
if (auto ext = cmpIOp.getLhs().getDefiningOp<ExtSIOp>()) {
689-
indVar = ext.getIn().dyn_cast<BlockArgument>();
689+
indVar = dyn_cast<BlockArgument>(ext.getIn());
690690
extType = ext.getType();
691691
}
692692
// Condition is not the same as an induction variable
@@ -1004,7 +1004,7 @@ struct MoveWhileAndDown : public OpRewritePattern<WhileOp> {
10041004

10051005
Value extraCmp = andIOp->getOperand(1 - i);
10061006
Value lookThrough = nullptr;
1007-
if (auto BA = extraCmp.dyn_cast<BlockArgument>()) {
1007+
if (auto BA = dyn_cast<BlockArgument>(extraCmp)) {
10081008
lookThrough = oldYield.getOperand(BA.getArgNumber());
10091009
}
10101010
if (!helper.computeLegality(/*sizeCheck*/ false, lookThrough)) {
@@ -1341,7 +1341,7 @@ struct MoveWhileDown2 : public OpRewritePattern<WhileOp> {
13411341
// yield i:pair<2>
13421342
// }
13431343
if (!std::get<0>(pair).use_empty()) {
1344-
if (auto blockArg = elseYielded.dyn_cast<BlockArgument>())
1344+
if (auto blockArg = dyn_cast<BlockArgument>(elseYielded))
13451345
if (blockArg.getOwner() == &op.getBefore().front()) {
13461346
if (afterYield.getResults()[blockArg.getArgNumber()] ==
13471347
std::get<2>(pair) &&
@@ -1580,7 +1580,7 @@ struct WhileCmpOffset : public OpRewritePattern<WhileOp> {
15801580
if (addI.getOperand(1).getDefiningOp() &&
15811581
!op.getBefore().isAncestor(
15821582
addI.getOperand(1).getDefiningOp()->getParentRegion()))
1583-
if (auto blockArg = addI.getOperand(0).dyn_cast<BlockArgument>()) {
1583+
if (auto blockArg = dyn_cast<BlockArgument>(addI.getOperand(0))) {
15841584
if (blockArg.getOwner() == &op.getBefore().front()) {
15851585
auto rng = llvm::make_early_inc_range(blockArg.getUses());
15861586

@@ -1859,7 +1859,7 @@ struct WhileLICM : public OpRewritePattern<WhileOp> {
18591859
auto isDefinedOutsideOfBody = [&](Value value) {
18601860
auto *definingOp = value.getDefiningOp();
18611861
if (!definingOp) {
1862-
if (auto ba = value.dyn_cast<BlockArgument>())
1862+
if (auto ba = dyn_cast<BlockArgument>(value))
18631863
definingOp = ba.getOwner()->getParentOp();
18641864
assert(definingOp);
18651865
}
@@ -2125,7 +2125,7 @@ struct WhileShiftToInduction : public OpRewritePattern<WhileOp> {
21252125
if (!matchPattern(cmpIOp.getRhs(), m_Zero()))
21262126
return failure();
21272127

2128-
auto indVar = cmpIOp.getLhs().dyn_cast<BlockArgument>();
2128+
auto indVar = dyn_cast<BlockArgument>(cmpIOp.getLhs());
21292129
if (!indVar)
21302130
return failure();
21312131

@@ -2144,7 +2144,7 @@ struct WhileShiftToInduction : public OpRewritePattern<WhileOp> {
21442144
if (!matchPattern(shiftOp.getRhs(), m_One()))
21452145
return failure();
21462146

2147-
auto prevIndVar = shiftOp.getLhs().dyn_cast<BlockArgument>();
2147+
auto prevIndVar = dyn_cast<BlockArgument>(shiftOp.getLhs());
21482148
if (!prevIndVar)
21492149
return failure();
21502150

lib/polygeist/Passes/CollectKernelStatistics.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -197,7 +197,7 @@ std::array<StrideTy, 3> estimateStride(mlir::OperandRange indices,
197197
} else {
198198
return UNKNOWN;
199199
}
200-
} else if (auto ba = v.dyn_cast<BlockArgument>()) {
200+
} else if (auto ba = dyn_cast<BlockArgument>(v)) {
201201
return 0;
202202
if (isa<gpu::GPUFuncOp>(ba.getOwner()->getParentOp())) {
203203
return 0;
@@ -339,7 +339,7 @@ static void generateAlternativeKernelDescs(mlir::ModuleOp m) {
339339
isa<arith::SubFOp>(&op) || isa<arith::AddFOp>(&op) ||
340340
isa<arith::RemFOp>(&op) || false) {
341341
int width =
342-
op.getOperand(0).getType().dyn_cast<FloatType>().getWidth();
342+
dyn_cast<FloatType>(op.getOperand(0).getType()).getWidth();
343343
addTo(floatOps, width, blockTrips);
344344
} else if (isa<arith::MulIOp>(&op) || isa<arith::DivUIOp>(&op) ||
345345
isa<arith::DivSIOp>(&op) || isa<arith::SubIOp>(&op) ||

0 commit comments

Comments
 (0)