Skip to content

Commit 1f31795

Browse files
committed
[NFC] Cleanup code to get back in synch for upstreaming.
Differential Revision: https://reviews.llvm.org/D124410
1 parent e6f44a3 commit 1f31795

File tree

1 file changed

+50
-45
lines changed

1 file changed

+50
-45
lines changed

flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp

Lines changed: 50 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -25,29 +25,26 @@
2525
// Codegen rewrite: rewriting of subgraphs of ops
2626
//===----------------------------------------------------------------------===//
2727

28-
using namespace fir;
29-
using namespace mlir;
30-
3128
#define DEBUG_TYPE "flang-codegen-rewrite"
3229

3330
static void populateShape(llvm::SmallVectorImpl<mlir::Value> &vec,
34-
ShapeOp shape) {
31+
fir::ShapeOp shape) {
3532
vec.append(shape.getExtents().begin(), shape.getExtents().end());
3633
}
3734

3835
// Operands of fir.shape_shift split into two vectors.
3936
static void populateShapeAndShift(llvm::SmallVectorImpl<mlir::Value> &shapeVec,
4037
llvm::SmallVectorImpl<mlir::Value> &shiftVec,
41-
ShapeShiftOp shift) {
42-
auto endIter = shift.getPairs().end();
43-
for (auto i = shift.getPairs().begin(); i != endIter;) {
38+
fir::ShapeShiftOp shift) {
39+
for (auto i = shift.getPairs().begin(), endIter = shift.getPairs().end();
40+
i != endIter;) {
4441
shiftVec.push_back(*i++);
4542
shapeVec.push_back(*i++);
4643
}
4744
}
4845

4946
static void populateShift(llvm::SmallVectorImpl<mlir::Value> &vec,
50-
ShiftOp shift) {
47+
fir::ShiftOp shift) {
5148
vec.append(shift.getOrigins().begin(), shift.getOrigins().end());
5249
}
5350

@@ -72,27 +69,26 @@ namespace {
7269
/// (!fir.ref<!fir.array<?xi32>>, index, index, index, index, index) ->
7370
/// !fir.box<!fir.array<?xi32>>
7471
/// ```
75-
class EmboxConversion : public mlir::OpRewritePattern<EmboxOp> {
72+
class EmboxConversion : public mlir::OpRewritePattern<fir::EmboxOp> {
7673
public:
7774
using OpRewritePattern::OpRewritePattern;
7875

7976
mlir::LogicalResult
80-
matchAndRewrite(EmboxOp embox,
77+
matchAndRewrite(fir::EmboxOp embox,
8178
mlir::PatternRewriter &rewriter) const override {
82-
auto shapeVal = embox.getShape();
8379
// If the embox does not include a shape, then do not convert it
84-
if (shapeVal)
80+
if (auto shapeVal = embox.getShape())
8581
return rewriteDynamicShape(embox, rewriter, shapeVal);
86-
if (auto boxTy = embox.getType().dyn_cast<BoxType>())
87-
if (auto seqTy = boxTy.getEleTy().dyn_cast<SequenceType>())
82+
if (auto boxTy = embox.getType().dyn_cast<fir::BoxType>())
83+
if (auto seqTy = boxTy.getEleTy().dyn_cast<fir::SequenceType>())
8884
if (seqTy.hasConstantShape())
8985
return rewriteStaticShape(embox, rewriter, seqTy);
9086
return mlir::failure();
9187
}
9288

93-
mlir::LogicalResult rewriteStaticShape(EmboxOp embox,
89+
mlir::LogicalResult rewriteStaticShape(fir::EmboxOp embox,
9490
mlir::PatternRewriter &rewriter,
95-
SequenceType seqTy) const {
91+
fir::SequenceType seqTy) const {
9692
auto loc = embox.getLoc();
9793
llvm::SmallVector<mlir::Value> shapeOpers;
9894
auto idxTy = rewriter.getIndexType();
@@ -101,41 +97,42 @@ class EmboxConversion : public mlir::OpRewritePattern<EmboxOp> {
10197
auto extVal = rewriter.create<mlir::arith::ConstantOp>(loc, idxTy, iAttr);
10298
shapeOpers.push_back(extVal);
10399
}
104-
auto xbox = rewriter.create<cg::XEmboxOp>(
100+
auto xbox = rewriter.create<fir::cg::XEmboxOp>(
105101
loc, embox.getType(), embox.getMemref(), shapeOpers, llvm::None,
106102
llvm::None, llvm::None, llvm::None, embox.getTypeparams());
107103
LLVM_DEBUG(llvm::dbgs() << "rewriting " << embox << " to " << xbox << '\n');
108104
rewriter.replaceOp(embox, xbox.getOperation()->getResults());
109105
return mlir::success();
110106
}
111107

112-
mlir::LogicalResult rewriteDynamicShape(EmboxOp embox,
108+
mlir::LogicalResult rewriteDynamicShape(fir::EmboxOp embox,
113109
mlir::PatternRewriter &rewriter,
114110
mlir::Value shapeVal) const {
115111
auto loc = embox.getLoc();
116-
auto shapeOp = dyn_cast<ShapeOp>(shapeVal.getDefiningOp());
117112
llvm::SmallVector<mlir::Value> shapeOpers;
118113
llvm::SmallVector<mlir::Value> shiftOpers;
119-
if (shapeOp) {
114+
if (auto shapeOp = mlir::dyn_cast<fir::ShapeOp>(shapeVal.getDefiningOp())) {
120115
populateShape(shapeOpers, shapeOp);
121116
} else {
122-
auto shiftOp = dyn_cast<ShapeShiftOp>(shapeVal.getDefiningOp());
117+
auto shiftOp =
118+
mlir::dyn_cast<fir::ShapeShiftOp>(shapeVal.getDefiningOp());
123119
assert(shiftOp && "shape is neither fir.shape nor fir.shape_shift");
124120
populateShapeAndShift(shapeOpers, shiftOpers, shiftOp);
125121
}
126122
llvm::SmallVector<mlir::Value> sliceOpers;
127123
llvm::SmallVector<mlir::Value> subcompOpers;
128124
llvm::SmallVector<mlir::Value> substrOpers;
129125
if (auto s = embox.getSlice())
130-
if (auto sliceOp = dyn_cast_or_null<SliceOp>(s.getDefiningOp())) {
126+
if (auto sliceOp =
127+
mlir::dyn_cast_or_null<fir::SliceOp>(s.getDefiningOp())) {
131128
sliceOpers.assign(sliceOp.getTriples().begin(),
132129
sliceOp.getTriples().end());
133130
subcompOpers.assign(sliceOp.getFields().begin(),
134131
sliceOp.getFields().end());
135132
substrOpers.assign(sliceOp.getSubstr().begin(),
136133
sliceOp.getSubstr().end());
137134
}
138-
auto xbox = rewriter.create<cg::XEmboxOp>(
135+
auto xbox = rewriter.create<fir::cg::XEmboxOp>(
139136
loc, embox.getType(), embox.getMemref(), shapeOpers, shiftOpers,
140137
sliceOpers, subcompOpers, substrOpers, embox.getTypeparams());
141138
LLVM_DEBUG(llvm::dbgs() << "rewriting " << embox << " to " << xbox << '\n');
@@ -156,22 +153,24 @@ class EmboxConversion : public mlir::OpRewritePattern<EmboxOp> {
156153
/// %5 = fircg.ext_rebox %3(%13) origin %12 : (!fir.box<!fir.array<?xi32>>,
157154
/// index, index) -> !fir.box<!fir.array<?xi32>>
158155
/// ```
159-
class ReboxConversion : public mlir::OpRewritePattern<ReboxOp> {
156+
class ReboxConversion : public mlir::OpRewritePattern<fir::ReboxOp> {
160157
public:
161158
using OpRewritePattern::OpRewritePattern;
162159

163160
mlir::LogicalResult
164-
matchAndRewrite(ReboxOp rebox,
161+
matchAndRewrite(fir::ReboxOp rebox,
165162
mlir::PatternRewriter &rewriter) const override {
166163
auto loc = rebox.getLoc();
167164
llvm::SmallVector<mlir::Value> shapeOpers;
168165
llvm::SmallVector<mlir::Value> shiftOpers;
169166
if (auto shapeVal = rebox.getShape()) {
170-
if (auto shapeOp = dyn_cast<ShapeOp>(shapeVal.getDefiningOp()))
167+
if (auto shapeOp = mlir::dyn_cast<fir::ShapeOp>(shapeVal.getDefiningOp()))
171168
populateShape(shapeOpers, shapeOp);
172-
else if (auto shiftOp = dyn_cast<ShapeShiftOp>(shapeVal.getDefiningOp()))
169+
else if (auto shiftOp =
170+
mlir::dyn_cast<fir::ShapeShiftOp>(shapeVal.getDefiningOp()))
173171
populateShapeAndShift(shapeOpers, shiftOpers, shiftOp);
174-
else if (auto shiftOp = dyn_cast<ShiftOp>(shapeVal.getDefiningOp()))
172+
else if (auto shiftOp =
173+
mlir::dyn_cast<fir::ShiftOp>(shapeVal.getDefiningOp()))
175174
populateShift(shiftOpers, shiftOp);
176175
else
177176
return mlir::failure();
@@ -180,7 +179,8 @@ class ReboxConversion : public mlir::OpRewritePattern<ReboxOp> {
180179
llvm::SmallVector<mlir::Value> subcompOpers;
181180
llvm::SmallVector<mlir::Value> substrOpers;
182181
if (auto s = rebox.getSlice())
183-
if (auto sliceOp = dyn_cast_or_null<SliceOp>(s.getDefiningOp())) {
182+
if (auto sliceOp =
183+
mlir::dyn_cast_or_null<fir::SliceOp>(s.getDefiningOp())) {
184184
sliceOpers.append(sliceOp.getTriples().begin(),
185185
sliceOp.getTriples().end());
186186
subcompOpers.append(sliceOp.getFields().begin(),
@@ -189,7 +189,7 @@ class ReboxConversion : public mlir::OpRewritePattern<ReboxOp> {
189189
sliceOp.getSubstr().end());
190190
}
191191

192-
auto xRebox = rewriter.create<cg::XReboxOp>(
192+
auto xRebox = rewriter.create<fir::cg::XReboxOp>(
193193
loc, rebox.getType(), rebox.getBox(), shapeOpers, shiftOpers,
194194
sliceOpers, subcompOpers, substrOpers);
195195
LLVM_DEBUG(llvm::dbgs()
@@ -212,30 +212,33 @@ class ReboxConversion : public mlir::OpRewritePattern<ReboxOp> {
212212
/// (!fir.ref<!fir.array<?xi32>>, index, index, index, index, index, index) ->
213213
/// !fir.ref<i32>
214214
/// ```
215-
class ArrayCoorConversion : public mlir::OpRewritePattern<ArrayCoorOp> {
215+
class ArrayCoorConversion : public mlir::OpRewritePattern<fir::ArrayCoorOp> {
216216
public:
217217
using OpRewritePattern::OpRewritePattern;
218218

219219
mlir::LogicalResult
220-
matchAndRewrite(ArrayCoorOp arrCoor,
220+
matchAndRewrite(fir::ArrayCoorOp arrCoor,
221221
mlir::PatternRewriter &rewriter) const override {
222222
auto loc = arrCoor.getLoc();
223223
llvm::SmallVector<mlir::Value> shapeOpers;
224224
llvm::SmallVector<mlir::Value> shiftOpers;
225225
if (auto shapeVal = arrCoor.getShape()) {
226-
if (auto shapeOp = dyn_cast<ShapeOp>(shapeVal.getDefiningOp()))
226+
if (auto shapeOp = mlir::dyn_cast<fir::ShapeOp>(shapeVal.getDefiningOp()))
227227
populateShape(shapeOpers, shapeOp);
228-
else if (auto shiftOp = dyn_cast<ShapeShiftOp>(shapeVal.getDefiningOp()))
228+
else if (auto shiftOp =
229+
mlir::dyn_cast<fir::ShapeShiftOp>(shapeVal.getDefiningOp()))
229230
populateShapeAndShift(shapeOpers, shiftOpers, shiftOp);
230-
else if (auto shiftOp = dyn_cast<ShiftOp>(shapeVal.getDefiningOp()))
231+
else if (auto shiftOp =
232+
mlir::dyn_cast<fir::ShiftOp>(shapeVal.getDefiningOp()))
231233
populateShift(shiftOpers, shiftOp);
232234
else
233235
return mlir::failure();
234236
}
235237
llvm::SmallVector<mlir::Value> sliceOpers;
236238
llvm::SmallVector<mlir::Value> subcompOpers;
237239
if (auto s = arrCoor.getSlice())
238-
if (auto sliceOp = dyn_cast_or_null<SliceOp>(s.getDefiningOp())) {
240+
if (auto sliceOp =
241+
mlir::dyn_cast_or_null<fir::SliceOp>(s.getDefiningOp())) {
239242
sliceOpers.append(sliceOp.getTriples().begin(),
240243
sliceOp.getTriples().end());
241244
subcompOpers.append(sliceOp.getFields().begin(),
@@ -244,7 +247,7 @@ class ArrayCoorConversion : public mlir::OpRewritePattern<ArrayCoorOp> {
244247
"Don't allow substring operations on array_coor. This "
245248
"restriction may be lifted in the future.");
246249
}
247-
auto xArrCoor = rewriter.create<cg::XArrayCoorOp>(
250+
auto xArrCoor = rewriter.create<fir::cg::XArrayCoorOp>(
248251
loc, arrCoor.getType(), arrCoor.getMemref(), shapeOpers, shiftOpers,
249252
sliceOpers, subcompOpers, arrCoor.getIndices(),
250253
arrCoor.getTypeparams());
@@ -255,20 +258,22 @@ class ArrayCoorConversion : public mlir::OpRewritePattern<ArrayCoorOp> {
255258
}
256259
};
257260

258-
class CodeGenRewrite : public CodeGenRewriteBase<CodeGenRewrite> {
261+
class CodeGenRewrite : public fir::CodeGenRewriteBase<CodeGenRewrite> {
259262
public:
260263
void runOnOperation() override final {
261264
auto op = getOperation();
262265
auto &context = getContext();
263266
mlir::OpBuilder rewriter(&context);
264267
mlir::ConversionTarget target(context);
265-
target.addLegalDialect<mlir::arith::ArithmeticDialect, FIROpsDialect,
266-
FIRCodeGenDialect, mlir::func::FuncDialect>();
267-
target.addIllegalOp<ArrayCoorOp>();
268-
target.addIllegalOp<ReboxOp>();
269-
target.addDynamicallyLegalOp<EmboxOp>([](EmboxOp embox) {
270-
return !(embox.getShape() ||
271-
embox.getType().cast<BoxType>().getEleTy().isa<SequenceType>());
268+
target.addLegalDialect<mlir::arith::ArithmeticDialect, fir::FIROpsDialect,
269+
fir::FIRCodeGenDialect, mlir::func::FuncDialect>();
270+
target.addIllegalOp<fir::ArrayCoorOp>();
271+
target.addIllegalOp<fir::ReboxOp>();
272+
target.addDynamicallyLegalOp<fir::EmboxOp>([](fir::EmboxOp embox) {
273+
return !(embox.getShape() || embox.getType()
274+
.cast<fir::BoxType>()
275+
.getEleTy()
276+
.isa<fir::SequenceType>());
272277
});
273278
mlir::RewritePatternSet patterns(&context);
274279
patterns.insert<EmboxConversion, ArrayCoorConversion, ReboxConversion>(

0 commit comments

Comments
 (0)