@@ -152,6 +152,14 @@ static bool isTransitivelyUsedOutside(Value v, SingleRegion sr) {
152152 return false ;
153153}
154154
155+ // / We clone pure operations in both the parallel and single blocks. this
156+ // / functions cleans them up if they end up with no uses
157+ static void cleanupBlock (Block *block) {
158+ for (Operation &op : llvm::make_early_inc_range (*block))
159+ if (isOpTriviallyDead (&op))
160+ op.erase ();
161+ }
162+
155163static void parallelizeRegion (Region &sourceRegion, Region &targetRegion,
156164 IRMapping &rootMapping, Location loc) {
157165 OpBuilder rootBuilder (sourceRegion.getContext ());
@@ -258,13 +266,8 @@ static void parallelizeRegion(Region &sourceRegion, Region &targetRegion,
258266 singleOperands.copyprivateVars =
259267 moveToSingle (std::get<SingleRegion>(opOrSingle), allocaBuilder,
260268 singleBuilder, parallelBuilder);
269+ cleanupBlock (singleBlock);
261270 for (auto var : singleOperands.copyprivateVars ) {
262- Type ty;
263- if (auto firAlloca = var.getDefiningOp <fir::AllocaOp>()) {
264- ty = firAlloca.getAllocatedType ();
265- } else {
266- ty = LLVM::LLVMPointerType::get (allocaBuilder.getContext ());
267- }
268271 mlir::func::FuncOp funcOp =
269272 createCopyFunc (loc, var.getType (), firCopyFuncBuilder);
270273 singleOperands.copyprivateSyms .push_back (SymbolRefAttr::get (funcOp));
@@ -302,6 +305,9 @@ static void parallelizeRegion(Region &sourceRegion, Region &targetRegion,
302305
303306 rootBuilder.clone (*block.getTerminator (), rootMapping);
304307 }
308+
309+ for (Block &targetBlock : targetRegion)
310+ cleanupBlock (&targetBlock);
305311}
306312
307313// / Lowers workshare to a sequence of single-thread regions and parallel loops
@@ -372,20 +378,6 @@ class LowerWorksharePass
372378
373379 lowerWorkshare (wsOp);
374380 });
375-
376- // Do folding
377- for (Operation *isolatedParent : parents) {
378- RewritePatternSet patterns (&getContext ());
379- GreedyRewriteConfig config;
380- // prevent the pattern driver form merging blocks
381- config.enableRegionSimplification =
382- mlir::GreedySimplifyRegionLevel::Disabled;
383- if (failed (applyPatternsAndFoldGreedily (isolatedParent,
384- std::move (patterns), config))) {
385- emitError (isolatedParent->getLoc (), " error in lower workshare\n " );
386- signalPassFailure ();
387- }
388- }
389381 }
390382};
391383} // namespace
0 commit comments