Skip to content

Commit e3c2884

Browse files
committed
Clean up trivially dead ops
1 parent 63f07c9 commit e3c2884

File tree

3 files changed

+68
-21
lines changed

3 files changed

+68
-21
lines changed

flang/lib/Optimizer/OpenMP/LowerWorkshare.cpp

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -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+
155163
static 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

flang/test/Transforms/OpenMP/lower-workshare3.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33

44
// tests if the correct values are stored
55

6-
func.func @wsfunc(%arg0: !fir.ref<!fir.array<42xi32>>) {
6+
func.func @wsfunc() {
77
omp.parallel {
88
// CHECK: fir.alloca
99
// CHECK: fir.alloca
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// RUN: fir-opt --split-input-file --lower-workshare --allow-unregistered-dialect %s | FileCheck %s
2+
3+
func.func @wsfunc() {
4+
%a = fir.alloca i32
5+
omp.parallel {
6+
omp.workshare {
7+
%t1 = "test.test1"() : () -> i32
8+
9+
%c1 = arith.constant 1 : index
10+
%c42 = arith.constant 42 : index
11+
12+
%c2 = arith.constant 2 : index
13+
"test.test3"(%c2) : (index) -> ()
14+
15+
"omp.workshare_loop_wrapper"() ({
16+
omp.loop_nest (%arg1) : index = (%c1) to (%c42) inclusive step (%c1) {
17+
"test.test2"() : () -> ()
18+
omp.yield
19+
}
20+
omp.terminator
21+
}) : () -> ()
22+
omp.terminator
23+
}
24+
omp.terminator
25+
}
26+
return
27+
}
28+
29+
// CHECK-LABEL: func.func @wsfunc() {
30+
// CHECK: %[[VAL_0:.*]] = fir.alloca i32
31+
// CHECK: omp.parallel {
32+
// CHECK: %[[VAL_1:.*]] = arith.constant true
33+
// CHECK: fir.if %[[VAL_1]] {
34+
// CHECK: omp.single {
35+
// CHECK: %[[VAL_2:.*]] = "test.test1"() : () -> i32
36+
// CHECK: %[[VAL_3:.*]] = arith.constant 2 : index
37+
// CHECK: "test.test3"(%[[VAL_3]]) : (index) -> ()
38+
// CHECK: omp.terminator
39+
// CHECK: }
40+
// CHECK: %[[VAL_4:.*]] = arith.constant 1 : index
41+
// CHECK: %[[VAL_5:.*]] = arith.constant 42 : index
42+
// CHECK: omp.wsloop nowait {
43+
// CHECK: omp.loop_nest (%[[VAL_6:.*]]) : index = (%[[VAL_4]]) to (%[[VAL_5]]) inclusive step (%[[VAL_4]]) {
44+
// CHECK: "test.test2"() : () -> ()
45+
// CHECK: omp.yield
46+
// CHECK: }
47+
// CHECK: omp.terminator
48+
// CHECK: }
49+
// CHECK: omp.barrier
50+
// CHECK: }
51+
// CHECK: omp.terminator
52+
// CHECK: }
53+
// CHECK: return
54+
// CHECK: }
55+

0 commit comments

Comments
 (0)