Skip to content

Commit e979ecd

Browse files
committed
[mlir][transforms]-Extract dead code elimination util
Extract the dead code elimination into an exposed util file to have a simple usage of the cpp API. Change-Id: Ic013ba06b778f8d5221bea226131e5343b543f51
1 parent 754b946 commit e979ecd

File tree

5 files changed

+92
-42
lines changed

5 files changed

+92
-42
lines changed
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//===- DCE.h - Dead Code Elimination -----------------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file declares methods for eliminating dead code.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TRANSFORMS_DCE_H_
14+
#define MLIR_TRANSFORMS_DCE_H_
15+
16+
namespace mlir {
17+
18+
class Operation;
19+
class RewriterBase;
20+
21+
/// Eliminate dead code within the given `target`.
22+
void deadCodeElimination(RewriterBase &rewriter, Operation *target);
23+
24+
} // namespace mlir
25+
26+
#endif // MLIR_TRANSFORMS_DCE_H_

mlir/lib/Dialect/Transform/IR/TransformOps.cpp

Lines changed: 2 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include "mlir/Pass/PassManager.h"
3232
#include "mlir/Pass/PassRegistry.h"
3333
#include "mlir/Transforms/CSE.h"
34+
#include "mlir/Transforms/DCEUtils.h"
3435
#include "mlir/Transforms/DialectConversion.h"
3536
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
3637
#include "mlir/Transforms/LoopInvariantCodeMotionUtils.h"
@@ -314,48 +315,7 @@ DiagnosedSilenceableFailure transform::ApplyDeadCodeEliminationOp::applyToOne(
314315
if (!payloadCheck.succeeded())
315316
return payloadCheck;
316317

317-
// Maintain a worklist of potentially dead ops.
318-
SetVector<Operation *> worklist;
319-
320-
// Helper function that adds all defining ops of used values (operands and
321-
// operands of nested ops).
322-
auto addDefiningOpsToWorklist = [&](Operation *op) {
323-
op->walk([&](Operation *op) {
324-
for (Value v : op->getOperands())
325-
if (Operation *defOp = v.getDefiningOp())
326-
if (target->isProperAncestor(defOp))
327-
worklist.insert(defOp);
328-
});
329-
};
330-
331-
// Helper function that erases an op.
332-
auto eraseOp = [&](Operation *op) {
333-
// Remove op and nested ops from the worklist.
334-
op->walk([&](Operation *op) {
335-
const auto *it = llvm::find(worklist, op);
336-
if (it != worklist.end())
337-
worklist.erase(it);
338-
});
339-
rewriter.eraseOp(op);
340-
};
341-
342-
// Initial walk over the IR.
343-
target->walk<WalkOrder::PostOrder>([&](Operation *op) {
344-
if (op != target && isOpTriviallyDead(op)) {
345-
addDefiningOpsToWorklist(op);
346-
eraseOp(op);
347-
}
348-
});
349-
350-
// Erase all ops that have become dead.
351-
while (!worklist.empty()) {
352-
Operation *op = worklist.pop_back_val();
353-
if (!isOpTriviallyDead(op))
354-
continue;
355-
addDefiningOpsToWorklist(op);
356-
eraseOp(op);
357-
}
358-
318+
deadCodeElimination(rewriter, target);
359319
return DiagnosedSilenceableFailure::success();
360320
}
361321

mlir/lib/Transforms/Utils/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ add_mlir_library(MLIRTransformUtils
22
CFGToSCF.cpp
33
CommutativityUtils.cpp
44
ControlFlowSinkUtils.cpp
5+
DCEUtils.cpp
56
DialectConversion.cpp
67
FoldUtils.cpp
78
GreedyPatternRewriteDriver.cpp
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//===- DCEUtils.cpp - Dead Code Elimination ------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This transformation implements method for eliminating dead code.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#include "mlir/Transforms/DCEUtils.h"
14+
#include "mlir/IR/PatternMatch.h"
15+
#include "mlir/Interfaces/SideEffectInterfaces.h"
16+
#include "llvm/ADT/SetVector.h"
17+
18+
using namespace mlir;
19+
20+
void mlir::deadCodeElimination(RewriterBase &rewriter, Operation *target) {
21+
// Maintain a worklist of potentially dead ops.
22+
mlir::SetVector<Operation *> worklist;
23+
24+
// Helper function that adds all defining ops of used values (operands and
25+
// operands of nested ops).
26+
auto addDefiningOpsToWorklist = [&](Operation *op) {
27+
op->walk([&](Operation *op) {
28+
for (Value v : op->getOperands())
29+
if (Operation *defOp = v.getDefiningOp())
30+
if (target->isProperAncestor(defOp))
31+
worklist.insert(defOp);
32+
});
33+
};
34+
35+
// Helper function that erases an op.
36+
auto eraseOp = [&](Operation *op) {
37+
// Remove op and nested ops from the worklist.
38+
op->walk([&](Operation *op) {
39+
const auto *it = llvm::find(worklist, op);
40+
if (it != worklist.end())
41+
worklist.erase(it);
42+
});
43+
rewriter.eraseOp(op);
44+
};
45+
46+
// Initial walk over the IR.
47+
target->walk<WalkOrder::PostOrder>([&](Operation *op) {
48+
if (op != target && isOpTriviallyDead(op)) {
49+
addDefiningOpsToWorklist(op);
50+
eraseOp(op);
51+
}
52+
});
53+
54+
// Erase all ops that have become dead.
55+
while (!worklist.empty()) {
56+
Operation *op = worklist.pop_back_val();
57+
if (!isOpTriviallyDead(op))
58+
continue;
59+
addDefiningOpsToWorklist(op);
60+
eraseOp(op);
61+
}
62+
}

utils/bazel/llvm-project-overlay/mlir/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8022,6 +8022,7 @@ cc_library(
80228022
"include/mlir/Transforms/CFGToSCF.h",
80238023
"include/mlir/Transforms/CommutativityUtils.h",
80248024
"include/mlir/Transforms/ControlFlowSinkUtils.h",
8025+
"include/mlir/Transforms/DCEUtils.h",
80258026
"include/mlir/Transforms/DialectConversion.h",
80268027
"include/mlir/Transforms/FoldUtils.h",
80278028
"include/mlir/Transforms/GreedyPatternRewriteDriver.h",

0 commit comments

Comments
 (0)