Skip to content

Commit fdf51f8

Browse files
kumasentoivanradanov
authored andcommitted
[PlutoTransform] canonicalize and segfault fix
[PlutoTransform] canonicalize after dedup cast [PlutoTransform] fix dedup segfault
1 parent 7113245 commit fdf51f8

File tree

2 files changed

+39
-27
lines changed

2 files changed

+39
-27
lines changed

tools/polymer/lib/Target/OpenScop/ConvertToOpenScop.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -233,10 +233,6 @@ void OslScopBuilder::buildScopContext(OslScop *scop,
233233
FlatAffineValueConstraints *domain = it.second.getDomain();
234234
FlatAffineValueConstraints cst(*domain);
235235

236-
ctx.mergeAndAlignIdsWithOther(0, &cst);
237-
ctx.append(cst);
238-
ctx.removeRedundantConstraints();
239-
240236
LLVM_DEBUG(dbgs() << "Statement:\n");
241237
LLVM_DEBUG(it.second.getCaller().dump());
242238
LLVM_DEBUG(it.second.getCallee().dump());
@@ -251,6 +247,10 @@ void OslScopBuilder::buildScopContext(OslScop *scop,
251247
dbgs() << " * " << value << '\n';
252248
});
253249

250+
ctx.mergeAndAlignIdsWithOther(0, &cst);
251+
ctx.append(cst);
252+
ctx.removeRedundantConstraints();
253+
254254
LLVM_DEBUG(dbgs() << "Updated context: \n");
255255
LLVM_DEBUG(ctx.dump());
256256

tools/polymer/lib/Transforms/PlutoTransform.cc

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -132,28 +132,6 @@ static mlir::FuncOp plutoTransform(mlir::FuncOp f, OpBuilder &rewriter,
132132
return g;
133133
}
134134

135-
static void dedupIndexCast(FuncOp f) {
136-
Block &entry = f.getBlocks().front();
137-
llvm::MapVector<Value, Value> argToCast;
138-
SmallVector<Operation *> toErase;
139-
for (auto &op : entry) {
140-
if (auto indexCast = dyn_cast<arith::IndexCastOp>(&op)) {
141-
auto arg = indexCast.getOperand().dyn_cast<BlockArgument>();
142-
if (argToCast.count(arg)) {
143-
LLVM_DEBUG(dbgs() << "Found duplicated index_cast: " << indexCast
144-
<< '\n');
145-
indexCast.replaceAllUsesWith(argToCast.lookup(arg));
146-
toErase.push_back(indexCast);
147-
} else {
148-
argToCast[arg] = indexCast;
149-
}
150-
}
151-
}
152-
153-
for (auto op : toErase)
154-
op->erase();
155-
}
156-
157135
namespace {
158136
class PlutoTransformPass
159137
: public mlir::PassWrapper<PlutoTransformPass,
@@ -183,7 +161,6 @@ class PlutoTransformPass
183161

184162
m.walk([&](mlir::FuncOp f) {
185163
if (!f->getAttr("scop.stmt") && !f->hasAttr("scop.ignored")) {
186-
dedupIndexCast(f);
187164
funcOps.push_back(f);
188165
}
189166
});
@@ -300,10 +277,45 @@ struct PlutoParallelizePass
300277
};
301278
} // namespace
302279

280+
static void dedupIndexCast(FuncOp f) {
281+
if (f.getBlocks().empty())
282+
return;
283+
284+
Block &entry = f.getBlocks().front();
285+
llvm::MapVector<Value, Value> argToCast;
286+
SmallVector<Operation *> toErase;
287+
for (auto &op : entry) {
288+
if (auto indexCast = dyn_cast<arith::IndexCastOp>(&op)) {
289+
auto arg = indexCast.getOperand().dyn_cast<BlockArgument>();
290+
if (argToCast.count(arg)) {
291+
LLVM_DEBUG(dbgs() << "Found duplicated index_cast: " << indexCast
292+
<< '\n');
293+
indexCast.replaceAllUsesWith(argToCast.lookup(arg));
294+
toErase.push_back(indexCast);
295+
} else {
296+
argToCast[arg] = indexCast;
297+
}
298+
}
299+
}
300+
301+
for (auto op : toErase)
302+
op->erase();
303+
}
304+
305+
namespace {
306+
struct DedupIndexCastPass
307+
: public mlir::PassWrapper<DedupIndexCastPass,
308+
OperationPass<mlir::FuncOp>> {
309+
void runOnOperation() override { dedupIndexCast(getOperation()); }
310+
};
311+
} // namespace
312+
303313
void polymer::registerPlutoTransformPass() {
304314
PassPipelineRegistration<PlutoOptPipelineOptions>(
305315
"pluto-opt", "Optimization implemented by PLUTO.",
306316
[](OpPassManager &pm, const PlutoOptPipelineOptions &pipelineOptions) {
317+
pm.addPass(std::make_unique<DedupIndexCastPass>());
318+
pm.addPass(createCanonicalizerPass());
307319
pm.addPass(std::make_unique<PlutoTransformPass>(pipelineOptions));
308320
pm.addPass(createCanonicalizerPass());
309321
if (pipelineOptions.generateParallel) {

0 commit comments

Comments
 (0)