|
18 | 18 | #include "flang/Optimizer/Dialect/FIROps.h"
|
19 | 19 | #include "flang/Optimizer/Dialect/FIRType.h"
|
20 | 20 | #include "flang/Optimizer/Dialect/Support/FIRContext.h"
|
| 21 | +#include "mlir/IR/Iterators.h" |
21 | 22 | #include "mlir/Transforms/DialectConversion.h"
|
22 |
| -#include "mlir/Transforms/RegionUtils.h" |
23 | 23 | #include "llvm/ADT/STLExtras.h"
|
24 | 24 | #include "llvm/Support/Debug.h"
|
25 | 25 |
|
@@ -325,6 +325,25 @@ class DummyScopeOpConversion
|
325 | 325 | }
|
326 | 326 | };
|
327 | 327 |
|
| 328 | +/// Simple DCE to erase fir.shape/shift/slice/unused shape operands after this |
| 329 | +/// pass (fir.shape and like have no codegen). |
| 330 | +/// mlir::RegionDCE is expensive and requires running |
| 331 | +/// mlir::eraseUnreachableBlocks. It does things that are not needed here, like |
| 332 | +/// removing unused block arguments. fir.shape/shift/slice cannot be block |
| 333 | +/// arguments. |
| 334 | +/// This helper does a naive backward walk of the IR. It is not even guaranteed |
| 335 | +/// to walk blocks according to backward dominance, but that is good enough for |
| 336 | +/// what is done here, fir.shape/shift/slice have no usages anymore. The |
| 337 | +/// backward walk allows getting rid of most of the unused operands, it is not a |
| 338 | +/// problem to leave some in the weird cases. |
| 339 | +static void simpleDCE(mlir::RewriterBase &rewriter, mlir::Operation *op) { |
| 340 | + op->walk<mlir::WalkOrder::PostOrder, mlir::ReverseIterator>( |
| 341 | + [&](mlir::Operation *subOp) { |
| 342 | + if (mlir::isOpTriviallyDead(subOp)) |
| 343 | + rewriter.eraseOp(subOp); |
| 344 | + }); |
| 345 | +} |
| 346 | + |
328 | 347 | class CodeGenRewrite : public fir::impl::CodeGenRewriteBase<CodeGenRewrite> {
|
329 | 348 | public:
|
330 | 349 | using CodeGenRewriteBase<CodeGenRewrite>::CodeGenRewriteBase;
|
@@ -356,7 +375,7 @@ class CodeGenRewrite : public fir::impl::CodeGenRewriteBase<CodeGenRewrite> {
|
356 | 375 | }
|
357 | 376 | // Erase any residual (fir.shape, fir.slice...).
|
358 | 377 | mlir::IRRewriter rewriter(&context);
|
359 |
| - (void)mlir::runRegionDCE(rewriter, mod->getRegions()); |
| 378 | + simpleDCE(rewriter, mod.getOperation()); |
360 | 379 | }
|
361 | 380 | };
|
362 | 381 |
|
|
0 commit comments