Skip to content

Commit

Permalink
Clean up trivially dead ops
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanradanov committed Aug 4, 2024
1 parent 79a9335 commit 7af84b2
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 21 deletions.
32 changes: 12 additions & 20 deletions flang/lib/Optimizer/OpenMP/LowerWorkshare.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,14 @@ static bool isTransitivelyUsedOutside(Value v, SingleRegion sr) {
return false;
}

/// We clone pure operations in both the parallel and single blocks. this
/// functions cleans them up if they end up with no uses
static void cleanupBlock(Block *block) {
for (Operation &op : llvm::make_early_inc_range(*block))
if (isOpTriviallyDead(&op))
op.erase();
}

static void parallelizeRegion(Region &sourceRegion, Region &targetRegion,
IRMapping &rootMapping, Location loc) {
OpBuilder rootBuilder(sourceRegion.getContext());
Expand Down Expand Up @@ -258,13 +266,8 @@ static void parallelizeRegion(Region &sourceRegion, Region &targetRegion,
singleOperands.copyprivateVars =
moveToSingle(std::get<SingleRegion>(opOrSingle), allocaBuilder,
singleBuilder, parallelBuilder);
cleanupBlock(singleBlock);
for (auto var : singleOperands.copyprivateVars) {
Type ty;
if (auto firAlloca = var.getDefiningOp<fir::AllocaOp>()) {
ty = firAlloca.getAllocatedType();
} else {
ty = LLVM::LLVMPointerType::get(allocaBuilder.getContext());
}
mlir::func::FuncOp funcOp =
createCopyFunc(loc, var.getType(), firCopyFuncBuilder);
singleOperands.copyprivateSyms.push_back(SymbolRefAttr::get(funcOp));
Expand Down Expand Up @@ -302,6 +305,9 @@ static void parallelizeRegion(Region &sourceRegion, Region &targetRegion,

rootBuilder.clone(*block.getTerminator(), rootMapping);
}

for (Block &targetBlock : targetRegion)
cleanupBlock(&targetBlock);
}

/// Lowers workshare to a sequence of single-thread regions and parallel loops
Expand Down Expand Up @@ -372,20 +378,6 @@ class LowerWorksharePass

lowerWorkshare(wsOp);
});

// Do folding
for (Operation *isolatedParent : parents) {
RewritePatternSet patterns(&getContext());
GreedyRewriteConfig config;
// prevent the pattern driver form merging blocks
config.enableRegionSimplification =
mlir::GreedySimplifyRegionLevel::Disabled;
if (failed(applyPatternsAndFoldGreedily(isolatedParent,
std::move(patterns), config))) {
emitError(isolatedParent->getLoc(), "error in lower workshare\n");
signalPassFailure();
}
}
}
};
} // namespace
2 changes: 1 addition & 1 deletion flang/test/Transforms/OpenMP/lower-workshare3.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

// tests if the correct values are stored

func.func @wsfunc(%arg0: !fir.ref<!fir.array<42xi32>>) {
func.func @wsfunc() {
omp.parallel {
// CHECK: fir.alloca
// CHECK: fir.alloca
Expand Down
55 changes: 55 additions & 0 deletions flang/test/Transforms/OpenMP/lower-workshare4.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
// RUN: fir-opt --split-input-file --lower-workshare --allow-unregistered-dialect %s | FileCheck %s

func.func @wsfunc() {
%a = fir.alloca i32
omp.parallel {
omp.workshare {
%t1 = "test.test1"() : () -> i32

%c1 = arith.constant 1 : index
%c42 = arith.constant 42 : index

%c2 = arith.constant 2 : index
"test.test3"(%c2) : (index) -> ()

"omp.workshare_loop_wrapper"() ({
omp.loop_nest (%arg1) : index = (%c1) to (%c42) inclusive step (%c1) {
"test.test2"() : () -> ()
omp.yield
}
omp.terminator
}) : () -> ()
omp.terminator
}
omp.terminator
}
return
}

// CHECK-LABEL: func.func @wsfunc() {
// CHECK: %[[VAL_0:.*]] = fir.alloca i32
// CHECK: omp.parallel {
// CHECK: %[[VAL_1:.*]] = arith.constant true
// CHECK: fir.if %[[VAL_1]] {
// CHECK: omp.single {
// CHECK: %[[VAL_2:.*]] = "test.test1"() : () -> i32
// CHECK: %[[VAL_3:.*]] = arith.constant 2 : index
// CHECK: "test.test3"(%[[VAL_3]]) : (index) -> ()
// CHECK: omp.terminator
// CHECK: }
// CHECK: %[[VAL_4:.*]] = arith.constant 1 : index
// CHECK: %[[VAL_5:.*]] = arith.constant 42 : index
// CHECK: omp.wsloop nowait {
// CHECK: omp.loop_nest (%[[VAL_6:.*]]) : index = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_4]]) {
// CHECK: "test.test2"() : () -> ()
// CHECK: omp.yield
// CHECK: }
// CHECK: omp.terminator
// CHECK: }
// CHECK: omp.barrier
// CHECK: }
// CHECK: omp.terminator
// CHECK: }
// CHECK: return
// CHECK: }

0 comments on commit 7af84b2

Please sign in to comment.