MLIR 23.0.0git
RemoveDeadValues.cpp
Go to the documentation of this file.
1//===- RemoveDeadValues.cpp - Remove Dead Values --------------------------===//
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// The goal of this pass is optimization (reducing runtime) by removing
10// unnecessary instructions. Unlike other passes that rely on local information
11// gathered from patterns to accomplish optimization, this pass uses a full
12// analysis of the IR, specifically, liveness analysis, and is thus more
13// powerful.
14//
15// Currently, this pass performs the following optimizations:
16// (A) Removes function arguments that are not live,
17// (B) Removes function return values that are not live across all callers of
18// the function,
19// (C) Removes unneccesary operands, results, region arguments, and region
20// terminator operands of region branch ops, and,
21// (D) Removes simple and region branch ops that have all non-live results and
22// don't affect memory in any way,
23//
24// iff
25//
26// the IR doesn't have any non-function symbol ops, non-call symbol user ops and
27// branch ops.
28//
29// Here, a "simple op" refers to an op that isn't a symbol op, symbol-user op,
30// region branch op, branch op, region branch terminator op, or return-like.
31//
32//===----------------------------------------------------------------------===//
33
37#include "mlir/IR/Builders.h"
39#include "mlir/IR/Dialect.h"
40#include "mlir/IR/Operation.h"
42#include "mlir/IR/SymbolTable.h"
43#include "mlir/IR/Value.h"
44#include "mlir/IR/ValueRange.h"
45#include "mlir/IR/Visitors.h"
50#include "mlir/Pass/Pass.h"
51#include "mlir/Support/LLVM.h"
54#include "llvm/ADT/STLExtras.h"
55#include "llvm/Support/Debug.h"
56#include "llvm/Support/DebugLog.h"
57#include <cassert>
58#include <cstddef>
59#include <memory>
60#include <optional>
61#include <vector>
62
63#define DEBUG_TYPE "remove-dead-values"
64
65namespace mlir {
66#define GEN_PASS_DEF_REMOVEDEADVALUES
67#include "mlir/Transforms/Passes.h.inc"
68} // namespace mlir
69
70using namespace mlir;
71using namespace mlir::dataflow;
72
73//===----------------------------------------------------------------------===//
74// RemoveDeadValues Pass
75//===----------------------------------------------------------------------===//
76
77namespace {
78
79// Set of structures below to be filled with operations and arguments to erase.
80// This is done to separate analysis and tree modification phases,
81// otherwise analysis is operating on half-deleted tree which is incorrect.
82
83struct FunctionToCleanUp {
84 FunctionOpInterface funcOp;
85 BitVector nonLiveArgs;
86 BitVector nonLiveRets;
87};
88
89struct ResultsToCleanup {
90 Operation *op;
91 BitVector nonLive;
92};
93
94struct OperandsToCleanup {
95 Operation *op;
96 BitVector nonLive;
97 // Optional: For CallOpInterface ops, stores the callee function.
98 Operation *callee = nullptr;
99 // Determines whether the operand should be replaced with a ub.poison result
100 // or erased entirely.
101 bool replaceWithPoison = false;
102};
103
104struct BlockArgsToCleanup {
105 Block *b;
106 BitVector nonLiveArgs;
107};
108
109struct SuccessorOperandsToCleanup {
110 BranchOpInterface branch;
111 unsigned successorIndex;
112 BitVector nonLiveOperands;
113};
114
115struct RDVFinalCleanupList {
116 SmallVector<Operation *> operations;
117 SmallVector<FunctionToCleanUp> functions;
118 SmallVector<OperandsToCleanup> operands;
119 SmallVector<ResultsToCleanup> results;
120 SmallVector<BlockArgsToCleanup> blocks;
121 SmallVector<SuccessorOperandsToCleanup> successorOperands;
122};
123
124// Some helper functions...
125
126/// Return true iff at least one value in `values` is live, given the liveness
127/// information in `la`.
128static bool hasLive(ValueRange values, const DenseSet<Value> &nonLiveSet,
130 for (Value value : values) {
131 if (nonLiveSet.contains(value)) {
132 LDBG() << "Value " << value << " is already marked non-live (dead)";
133 continue;
134 }
135
136 const Liveness *liveness = la.getLiveness(value);
137 if (!liveness) {
138 LDBG() << "Value " << value
139 << " has no liveness info, conservatively considered live";
140 return true;
141 }
142 if (liveness->isLive) {
143 LDBG() << "Value " << value << " is live according to liveness analysis";
144 return true;
145 } else {
146 LDBG() << "Value " << value << " is dead according to liveness analysis";
147 }
148 }
149 return false;
150}
151
152/// Return a BitVector of size `values.size()` where its i-th bit is 1 iff the
153/// i-th value in `values` is live, given the liveness information in `la`.
154static BitVector markLives(ValueRange values, const DenseSet<Value> &nonLiveSet,
156 BitVector lives(values.size(), true);
157
158 for (auto [index, value] : llvm::enumerate(values)) {
159 if (nonLiveSet.contains(value)) {
160 lives.reset(index);
161 LDBG() << "Value " << value
162 << " is already marked non-live (dead) at index " << index;
163 continue;
164 }
165
166 const Liveness *liveness = la.getLiveness(value);
167 // It is important to note that when `liveness` is null, we can't tell if
168 // `value` is live or not. So, the safe option is to consider it live. Also,
169 // the execution of this pass might create new SSA values when erasing some
170 // of the results of an op and we know that these new values are live
171 // (because they weren't erased) and also their liveness is null because
172 // liveness analysis ran before their creation.
173 if (!liveness) {
174 LDBG() << "Value " << value << " at index " << index
175 << " has no liveness info, conservatively considered live";
176 continue;
177 }
178 if (!liveness->isLive) {
179 lives.reset(index);
180 LDBG() << "Value " << value << " at index " << index
181 << " is dead according to liveness analysis";
182 } else {
183 LDBG() << "Value " << value << " at index " << index
184 << " is live according to liveness analysis";
185 }
186 }
187
188 return lives;
189}
190
191/// Collects values marked as "non-live" in the provided range and inserts them
192/// into the nonLiveSet. A value is considered "non-live" if the corresponding
193/// index in the `nonLive` bit vector is set.
194static void collectNonLiveValues(DenseSet<Value> &nonLiveSet, ValueRange range,
195 const BitVector &nonLive) {
196 for (auto [index, result] : llvm::enumerate(range)) {
197 if (!nonLive[index])
198 continue;
199 nonLiveSet.insert(result);
200 LDBG() << "Marking value " << result << " as non-live (dead) at index "
201 << index;
202 }
203}
204
205/// Drop the uses of the i-th result of `op` and then erase it iff toErase[i]
206/// is 1.
207static void dropUsesAndEraseResults(RewriterBase &rewriter, Operation *op,
208 BitVector toErase) {
209 assert(op->getNumResults() == toErase.size() &&
210 "expected the number of results in `op` and the size of `toErase` to "
211 "be the same");
212 for (auto idx : toErase.set_bits())
213 op->getResult(idx).dropAllUses();
214 rewriter.eraseOpResults(op, toErase);
215}
216
217/// Process a simple operation `op` using the liveness analysis `la`.
218/// If the operation has no memory effects and none of its results are live:
219/// 1. Add the operation to a list for future removal, and
220/// 2. Mark all its results as non-live values
221///
222/// The operation `op` is assumed to be simple. A simple operation is one that
223/// is NOT:
224/// - Function-like
225/// - Call-like
226/// - A region branch operation
227/// - A branch operation
228/// - A region branch terminator
229/// - Return-like
230static void processSimpleOp(Operation *op, RunLivenessAnalysis &la,
231 DenseSet<Value> &nonLiveSet,
232 RDVFinalCleanupList &cl) {
233 // Operations that have dead operands can be erased regardless of their
234 // side effects. The liveness analysis would not have marked an SSA value as
235 // "dead" if it had a side-effecting user that is reachable.
236 bool hasDeadOperand =
237 markLives(op->getOperands(), nonLiveSet, la).flip().any();
238 if (hasDeadOperand) {
239 LDBG() << "Simple op has dead operands, so the op must be dead: "
240 << OpWithFlags(op,
241 OpPrintingFlags().skipRegions().printGenericOpForm());
242 assert(!hasLive(op->getResults(), nonLiveSet, la) &&
243 "expected the op to have no live results");
244 cl.operations.push_back(op);
245 collectNonLiveValues(nonLiveSet, op->getResults(),
246 BitVector(op->getNumResults(), true));
247 return;
248 }
249
250 if (!isMemoryEffectFree(op) || hasLive(op->getResults(), nonLiveSet, la)) {
251 LDBG() << "Simple op is not memory effect free or has live results, "
252 "preserving it: "
253 << OpWithFlags(op,
254 OpPrintingFlags().skipRegions().printGenericOpForm());
255 return;
256 }
257
258 LDBG()
259 << "Simple op has all dead results and is memory effect free, scheduling "
260 "for removal: "
261 << OpWithFlags(op, OpPrintingFlags().skipRegions().printGenericOpForm());
262 cl.operations.push_back(op);
263 collectNonLiveValues(nonLiveSet, op->getResults(),
264 BitVector(op->getNumResults(), true));
265}
266
267/// Process a function-like operation `funcOp` using the liveness analysis `la`
268/// and the IR in `module`. If it is not public or external:
269/// (1) Adding its non-live arguments to a list for future removal.
270/// (2) Marking their corresponding operands in its callers for removal.
271/// (3) Identifying and enqueueing unnecessary terminator operands
272/// (return values that are non-live across all callers) for removal.
273/// (4) Enqueueing the non-live arguments and return values for removal.
274/// (5) Collecting the uses of these return values in its callers for future
275/// removal.
276/// (6) Marking all its results as non-live values.
277static void processFuncOp(FunctionOpInterface funcOp, Operation *module,
278 RunLivenessAnalysis &la, DenseSet<Value> &nonLiveSet,
279 RDVFinalCleanupList &cl) {
280 LDBG() << "Processing function op: "
281 << OpWithFlags(funcOp,
282 OpPrintingFlags().skipRegions().printGenericOpForm());
283 if (funcOp.isPublic() || funcOp.isExternal()) {
284 LDBG() << "Function is public or external, skipping: "
285 << funcOp.getOperation()->getName();
286 return;
287 }
288
289 // Get the list of unnecessary (non-live) arguments in `nonLiveArgs`.
290 SmallVector<Value> arguments(funcOp.getArguments());
291 BitVector nonLiveArgs = markLives(arguments, nonLiveSet, la);
292 nonLiveArgs = nonLiveArgs.flip();
293
294 // Do (1).
295 for (auto [index, arg] : llvm::enumerate(arguments))
296 if (arg && nonLiveArgs[index])
297 nonLiveSet.insert(arg);
298
299 // Do (2). (Skip creating generic operand cleanup entries for call ops.
300 // Call arguments will be removed in the call-site specific segment-aware
301 // cleanup, avoiding generic eraseOperands bitvector mechanics.)
302 SymbolTable::UseRange uses = *funcOp.getSymbolUses(module);
303 for (SymbolTable::SymbolUse use : uses) {
304 Operation *callOp = use.getUser();
305 assert(isa<CallOpInterface>(callOp) && "expected a call-like user");
306 // Push an empty operand cleanup entry so that call-site specific logic in
307 // cleanUpDeadVals runs (it keys off CallOpInterface). The BitVector is
308 // intentionally all false to avoid generic erasure.
309 // Store the funcOp as the callee to avoid expensive symbol lookup later.
310 cl.operands.push_back({callOp, BitVector(callOp->getNumOperands(), false),
311 funcOp.getOperation()});
312 }
313
314 // Do (3).
315 // Get the list of unnecessary terminator operands (return values that are
316 // non-live across all callers) in `nonLiveRets`. There is a very important
317 // subtlety here. Unnecessary terminator operands are NOT the operands of the
318 // terminator that are non-live. Instead, these are the return values of the
319 // callers such that a given return value is non-live across all callers. Such
320 // corresponding operands in the terminator could be live. An example to
321 // demonstrate this:
322 // func.func private @f(%arg0: memref<i32>) -> (i32, i32) {
323 // %c0_i32 = arith.constant 0 : i32
324 // %0 = arith.addi %c0_i32, %c0_i32 : i32
325 // memref.store %0, %arg0[] : memref<i32>
326 // return %c0_i32, %0 : i32, i32
327 // }
328 // func.func @main(%arg0: i32, %arg1: memref<i32>) -> (i32) {
329 // %1:2 = call @f(%arg1) : (memref<i32>) -> i32
330 // return %1#0 : i32
331 // }
332 // Here, we can see that %1#1 is never used. It is non-live. Thus, @f doesn't
333 // need to return %0. But, %0 is live. And, still, we want to stop it from
334 // being returned, in order to optimize our IR. So, this demonstrates how we
335 // can make our optimization strong by even removing a live return value (%0),
336 // since it forwards only to non-live value(s) (%1#1).
337 size_t numReturns = funcOp.getNumResults();
338 BitVector nonLiveRets(numReturns, true);
339 for (SymbolTable::SymbolUse use : uses) {
340 Operation *callOp = use.getUser();
341 assert(isa<CallOpInterface>(callOp) && "expected a call-like user");
342 BitVector liveCallRets = markLives(callOp->getResults(), nonLiveSet, la);
343 nonLiveRets &= liveCallRets.flip();
344 }
345
346 // Note that in the absence of control flow ops forcing the control to go from
347 // the entry (first) block to the other blocks, the control never reaches any
348 // block other than the entry block, because every block has a terminator.
349 for (Block &block : funcOp.getBlocks()) {
350 Operation *returnOp = block.getTerminator();
351 if (!returnOp->hasTrait<OpTrait::ReturnLike>())
352 continue;
353 if (returnOp && returnOp->getNumOperands() == numReturns)
354 cl.operands.push_back({returnOp, nonLiveRets});
355 }
356
357 // Do (4).
358 cl.functions.push_back({funcOp, nonLiveArgs, nonLiveRets});
359
360 // Do (5) and (6).
361 if (numReturns == 0)
362 return;
363 for (SymbolTable::SymbolUse use : uses) {
364 Operation *callOp = use.getUser();
365 assert(isa<CallOpInterface>(callOp) && "expected a call-like user");
366 cl.results.push_back({callOp, nonLiveRets});
367 collectNonLiveValues(nonLiveSet, callOp->getResults(), nonLiveRets);
368 }
369}
370
371/// Process a region branch operation `regionBranchOp` using the liveness
372/// information in `la`. The processing involves two scenarios:
373///
374/// Scenario 1: If the operation has no memory effects and none of its results
375/// are live:
376/// 1.1. Enqueue all its uses for deletion.
377/// 1.2. Enqueue the branch itself for deletion.
378///
379/// Scenario 2: Otherwise:
380/// 2.1. Find all operands that are forwarded to only dead region successor
381/// inputs. I.e., forwarded to block arguments / op results that we do
382/// not want to keep.
383/// 2.2. Also find operands who's values are dead (i.e., are scheduled for
384/// erasure) due to other operations.
385/// 2.3. Enqueue all such operands for replacement with ub.poison.
386///
387/// Note: In scenario 2, block arguments and op results are not removed.
388/// However, the IR is simplified such that canonicalization patterns can
389/// remove them later.
390static void processRegionBranchOp(RegionBranchOpInterface regionBranchOp,
392 DenseSet<Value> &nonLiveSet,
393 RDVFinalCleanupList &cl) {
394 LDBG() << "Processing region branch op: "
395 << OpWithFlags(regionBranchOp,
396 OpPrintingFlags().skipRegions().printGenericOpForm());
397
398 // Scenario 1. This is the only case where the entire `regionBranchOp`
399 // is removed. It will not happen in any other scenario. Note that in this
400 // case, a non-forwarded operand of `regionBranchOp` could be live/non-live.
401 // It could never be live because of this op but its liveness could have been
402 // attributed to something else.
403 if (isMemoryEffectFree(regionBranchOp.getOperation()) &&
404 !hasLive(regionBranchOp->getResults(), nonLiveSet, la)) {
405 cl.operations.push_back(regionBranchOp.getOperation());
406 return;
407 }
408
409 // Mapping from operands to forwarded successor inputs. An operand can be
410 // forwarded to multiple successors.
411 //
412 // Example:
413 //
414 // %0 = scf.while : () -> i32 {
415 // scf.condition(...) %forwarded_value : i32
416 // } do {
417 // ^bb0(%arg0: i32):
418 // scf.yield
419 // }
420 // // No uses of %0.
421 //
422 // In the above example, %forwarded_value is forwarded to %arg0 and %0. Both
423 // %arg0 and %0 are dead, so %forwarded_value can be replaced with a
424 // ub.poison result.
425 //
426 // operandToSuccessorInputs[%forwarded_value] = {%arg0, %0}
427 //
428 RegionBranchSuccessorMapping operandToSuccessorInputs;
429 regionBranchOp.getSuccessorOperandInputMapping(operandToSuccessorInputs);
430
431 DenseMap<Operation *, BitVector> deadOperandsPerOp;
432 for (auto [opOperand, successorInputs] : operandToSuccessorInputs) {
433 // Helper function to mark the operand as dead, to be replaced with a
434 // ub.poison result.
435 auto markOperandDead = [&opOperand = opOperand, &deadOperandsPerOp]() {
436 // Create an entry in `deadOperandsPerOp` (initialized to "false", i.e.,
437 // no "dead" op operands) if it's the first time that we are seeing an op
438 // operand for this op. Otherwise, just take the existing bit vector from
439 // the map.
440 BitVector &deadOperands =
441 deadOperandsPerOp
442 .try_emplace(opOperand->getOwner(),
443 opOperand->getOwner()->getNumOperands(), false)
444 .first->second;
445 deadOperands.set(opOperand->getOperandNumber());
446 };
447
448 // The operand value is scheduled for removal. Mark it as dead.
449 if (!hasLive(opOperand->get(), nonLiveSet, la)) {
450 markOperandDead();
451 continue;
452 }
453
454 // If one of the successor inputs is live, the respective operand must be
455 // kept. Otherwise, ub.poison can be passed as operand.
456 if (!hasLive(successorInputs, nonLiveSet, la))
457 markOperandDead();
458 }
459
460 for (auto [op, deadOperands] : deadOperandsPerOp) {
461 cl.operands.push_back(
462 {op, deadOperands, nullptr, /*replaceWithPoison=*/true});
463 }
464}
465
466/// Steps to process a `BranchOpInterface` operation:
467///
468/// When a non-forwarded operand is dead (e.g., the condition value of a
469/// conditional branch op), the entire operation is dead.
470///
471/// Otherwise, iterate through each successor block of `branchOp`.
472/// (1) For each successor block, gather all operands from all successors.
473/// (2) Fetch their associated liveness analysis data and collect for future
474/// removal.
475/// (3) Identify and collect the dead operands from the successor block
476/// as well as their corresponding arguments.
477
478static void processBranchOp(BranchOpInterface branchOp, RunLivenessAnalysis &la,
479 DenseSet<Value> &nonLiveSet,
480 RDVFinalCleanupList &cl) {
481 LDBG() << "Processing branch op: " << *branchOp;
482
483 // Check for dead non-forwarded operands.
484 BitVector deadNonForwardedOperands =
485 markLives(branchOp->getOperands(), nonLiveSet, la).flip();
486 unsigned numSuccessors = branchOp->getNumSuccessors();
487 for (unsigned succIdx = 0; succIdx < numSuccessors; ++succIdx) {
488 SuccessorOperands successorOperands =
489 branchOp.getSuccessorOperands(succIdx);
490 // Remove all non-forwarded operands from the bit vector.
491 for (OpOperand &opOperand : successorOperands.getMutableForwardedOperands())
492 deadNonForwardedOperands[opOperand.getOperandNumber()] = false;
493 }
494 if (deadNonForwardedOperands.any()) {
495 cl.operations.push_back(branchOp.getOperation());
496 return;
497 }
498
499 for (unsigned succIdx = 0; succIdx < numSuccessors; ++succIdx) {
500 Block *successorBlock = branchOp->getSuccessor(succIdx);
501
502 // Do (1)
503 SuccessorOperands successorOperands =
504 branchOp.getSuccessorOperands(succIdx);
505 SmallVector<Value> operandValues;
506 for (unsigned operandIdx = 0; operandIdx < successorOperands.size();
507 ++operandIdx) {
508 operandValues.push_back(successorOperands[operandIdx]);
509 }
510
511 // Do (2)
512 BitVector successorNonLive =
513 markLives(operandValues, nonLiveSet, la).flip();
514 collectNonLiveValues(nonLiveSet, successorBlock->getArguments(),
515 successorNonLive);
516
517 // Do (3)
518 cl.blocks.push_back({successorBlock, successorNonLive});
519 cl.successorOperands.push_back({branchOp, succIdx, successorNonLive});
520 }
521}
522
523/// Create ub.poison ops for the given values. If a value has no uses, return
524/// an "empty" value.
525static SmallVector<Value> createPoisonedValues(OpBuilder &b,
526 ValueRange values) {
527 return llvm::map_to_vector(values, [&](Value value) {
528 if (value.use_empty())
529 return Value();
530 return ub::PoisonOp::create(b, value.getLoc(), value.getType()).getResult();
531 });
532}
533
534namespace {
535/// A listener that keeps track of ub.poison ops.
536struct TrackingListener : public RewriterBase::Listener {
537 void notifyOperationErased(Operation *op) override {
538 if (auto poisonOp = dyn_cast<ub::PoisonOp>(op))
539 poisonOps.erase(poisonOp);
540 }
541 void notifyOperationInserted(Operation *op,
542 OpBuilder::InsertPoint previous) override {
543 if (auto poisonOp = dyn_cast<ub::PoisonOp>(op))
544 poisonOps.insert(poisonOp);
545 }
546 DenseSet<ub::PoisonOp> poisonOps;
547};
548} // namespace
549
550/// Removes dead values collected in RDVFinalCleanupList.
551/// To be run once when all dead values have been collected.
552static void cleanUpDeadVals(MLIRContext *ctx, RDVFinalCleanupList &list) {
553 LDBG() << "Starting cleanup of dead values...";
554
555 // New ub.poison ops may be inserted during cleanup. Some of these ops may no
556 // longer be needed after the cleanup. A tracking listener keeps track of all
557 // new ub.poison ops, so that they can be removed again after the cleanup.
558 TrackingListener listener;
559 IRRewriter rewriter(ctx, &listener);
560
561 // 1. Blocks, We must remove the block arguments and successor operands before
562 // deleting the operation, as they may reside in the region operation.
563 LDBG() << "Cleaning up " << list.blocks.size() << " block argument lists";
564 for (auto &b : list.blocks) {
565 // blocks that are accessed via multiple codepaths processed once
566 if (b.b->getNumArguments() != b.nonLiveArgs.size())
567 continue;
568 LDBG_OS([&](raw_ostream &os) {
569 os << "Erasing non-live arguments [";
570 llvm::interleaveComma(b.nonLiveArgs.set_bits(), os);
571 os << "] from block #" << b.b->computeBlockNumber() << " in region #"
572 << b.b->getParent()->getRegionNumber() << " of operation "
573 << OpWithFlags(b.b->getParent()->getParentOp(),
574 OpPrintingFlags().skipRegions().printGenericOpForm());
575 });
576 // Note: Iterate from the end to make sure that that indices of not yet
577 // processes arguments do not change.
578 for (int i = b.nonLiveArgs.size() - 1; i >= 0; --i) {
579 if (!b.nonLiveArgs[i])
580 continue;
581 b.b->getArgument(i).dropAllUses();
582 b.b->eraseArgument(i);
583 }
584 }
585
586 // 2. Successor Operands
587 LDBG() << "Cleaning up " << list.successorOperands.size()
588 << " successor operand lists";
589 for (auto &op : list.successorOperands) {
590 SuccessorOperands successorOperands =
591 op.branch.getSuccessorOperands(op.successorIndex);
592 // blocks that are accessed via multiple codepaths processed once
593 if (successorOperands.size() != op.nonLiveOperands.size())
594 continue;
595 LDBG_OS([&](raw_ostream &os) {
596 os << "Erasing non-live successor operands [";
597 llvm::interleaveComma(op.nonLiveOperands.set_bits(), os);
598 os << "] from successor " << op.successorIndex << " of branch: "
599 << OpWithFlags(op.branch.getOperation(),
600 OpPrintingFlags().skipRegions().printGenericOpForm());
601 });
602 // it iterates backwards because erase invalidates all successor indexes
603 for (int i = successorOperands.size() - 1; i >= 0; --i) {
604 if (!op.nonLiveOperands[i])
605 continue;
606 successorOperands.erase(i);
607 }
608 }
609
610 // 3. Functions
611 LDBG() << "Cleaning up " << list.functions.size() << " functions";
612 // Record which function arguments were erased so we can shrink call-site
613 // argument segments for CallOpInterface operations (e.g. ops using
614 // AttrSizedOperandSegments) in the next phase.
616 for (auto &f : list.functions) {
617 LDBG() << "Cleaning up function: " << f.funcOp.getOperation()->getName()
618 << " (" << f.funcOp.getOperation() << ")";
619 LDBG_OS([&](raw_ostream &os) {
620 os << " Erasing non-live arguments [";
621 llvm::interleaveComma(f.nonLiveArgs.set_bits(), os);
622 os << "]\n";
623 os << " Erasing non-live return values [";
624 llvm::interleaveComma(f.nonLiveRets.set_bits(), os);
625 os << "]";
626 });
627 // Drop all uses of the dead arguments.
628 for (auto deadIdx : f.nonLiveArgs.set_bits())
629 f.funcOp.getArgument(deadIdx).dropAllUses();
630 // Some functions may not allow erasing arguments or results. These calls
631 // return failure in such cases without modifying the function, so it's okay
632 // to proceed.
633 if (succeeded(f.funcOp.eraseArguments(f.nonLiveArgs))) {
634 // Record only if we actually erased something.
635 if (f.nonLiveArgs.any())
636 erasedFuncArgs.try_emplace(f.funcOp.getOperation(), f.nonLiveArgs);
637 }
638 (void)f.funcOp.eraseResults(f.nonLiveRets);
639 }
640
641 // 4. Operands
642 LDBG() << "Cleaning up " << list.operands.size() << " operand lists";
643 for (OperandsToCleanup &o : list.operands) {
644 // Handle call-specific cleanup only when we have a cached callee reference.
645 // This avoids expensive symbol lookup and is defensive against future
646 // changes.
647 bool handledAsCall = false;
648 if (o.callee && isa<CallOpInterface>(o.op)) {
649 auto call = cast<CallOpInterface>(o.op);
650 auto it = erasedFuncArgs.find(o.callee);
651 if (it != erasedFuncArgs.end()) {
652 const BitVector &deadArgIdxs = it->second;
653 MutableOperandRange args = call.getArgOperandsMutable();
654 // First, erase the call arguments corresponding to erased callee
655 // args. We iterate backwards to preserve indices.
656 for (unsigned argIdx : llvm::reverse(deadArgIdxs.set_bits()))
657 args.erase(argIdx);
658 // If this operand cleanup entry also has a generic nonLive bitvector,
659 // clear bits for call arguments we already erased above to avoid
660 // double-erasing (which could impact other segments of ops with
661 // AttrSizedOperandSegments).
662 if (o.nonLive.any()) {
663 // Map the argument logical index to the operand number(s) recorded.
664 int operandOffset = call.getArgOperands().getBeginOperandIndex();
665 for (int argIdx : deadArgIdxs.set_bits()) {
666 int operandNumber = operandOffset + argIdx;
667 if (operandNumber < static_cast<int>(o.nonLive.size()))
668 o.nonLive.reset(operandNumber);
669 }
670 }
671 handledAsCall = true;
672 }
673 }
674 // Perform generic operand erasure for:
675 // - Non-call operations
676 // - Call operations without cached callee (where handledAsCall is false)
677 // But skip call operations that were already handled via segment-aware path
678 if (!handledAsCall && o.nonLive.any()) {
679 LDBG_OS([&](raw_ostream &os) {
680 os << "Erasing non-live operands [";
681 llvm::interleaveComma(o.nonLive.set_bits(), os);
682 os << "] from operation: "
683 << OpWithFlags(o.op,
684 OpPrintingFlags().skipRegions().printGenericOpForm());
685 });
686 if (o.replaceWithPoison) {
687 rewriter.setInsertionPoint(o.op);
688 for (auto deadIdx : o.nonLive.set_bits()) {
689 o.op->setOperand(
690 deadIdx, createPoisonedValues(rewriter, o.op->getOperand(deadIdx))
691 .front());
692 }
693 } else {
694 o.op->eraseOperands(o.nonLive);
695 }
696 }
697 }
698
699 // 5. Results
700 LDBG() << "Cleaning up " << list.results.size() << " result lists";
701 for (auto &r : list.results) {
702 LDBG_OS([&](raw_ostream &os) {
703 os << "Erasing non-live results [";
704 llvm::interleaveComma(r.nonLive.set_bits(), os);
705 os << "] from operation: "
706 << OpWithFlags(r.op,
707 OpPrintingFlags().skipRegions().printGenericOpForm());
708 });
709 dropUsesAndEraseResults(rewriter, r.op, r.nonLive);
710 }
711
712 // 6. Operations
713 LDBG() << "Cleaning up " << list.operations.size() << " operations";
714 for (Operation *op : list.operations) {
715 LDBG() << "Erasing operation: "
716 << OpWithFlags(op,
717 OpPrintingFlags().skipRegions().printGenericOpForm());
718 rewriter.setInsertionPoint(op);
719 if (op->hasTrait<OpTrait::IsTerminator>()) {
720 // When erasing a terminator, insert an unreachable op in its place.
721 ub::UnreachableOp::create(rewriter, op->getLoc());
722 }
723 op->dropAllUses();
724 rewriter.eraseOp(op);
725 }
726
727 // 7. Remove all dead poison ops.
728 for (ub::PoisonOp poisonOp : listener.poisonOps) {
729 if (poisonOp.use_empty())
730 poisonOp.erase();
731 }
732
733 LDBG() << "Finished cleanup of dead values";
734}
735
736struct RemoveDeadValues : public impl::RemoveDeadValuesBase<RemoveDeadValues> {
737 void runOnOperation() override;
738};
739} // namespace
740
741void RemoveDeadValues::runOnOperation() {
742 auto &la = getAnalysis<RunLivenessAnalysis>();
743 Operation *module = getOperation();
744
745 // Tracks values eligible for erasure - complements liveness analysis to
746 // identify "droppable" values.
747 DenseSet<Value> deadVals;
748
749 // Maintains a list of Ops, values, branches, etc., slated for cleanup at the
750 // end of this pass.
751 RDVFinalCleanupList finalCleanupList;
752
753 module->walk([&](Operation *op) {
754 if (auto funcOp = dyn_cast<FunctionOpInterface>(op)) {
755 processFuncOp(funcOp, module, la, deadVals, finalCleanupList);
756 } else if (auto regionBranchOp = dyn_cast<RegionBranchOpInterface>(op)) {
757 processRegionBranchOp(regionBranchOp, la, deadVals, finalCleanupList);
758 } else if (auto branchOp = dyn_cast<BranchOpInterface>(op)) {
759 processBranchOp(branchOp, la, deadVals, finalCleanupList);
760 } else if (op->hasTrait<::mlir::OpTrait::IsTerminator>()) {
761 // Nothing to do here because this is a terminator op and it should be
762 // honored with respect to its parent
763 } else if (isa<CallOpInterface>(op)) {
764 // Nothing to do because this op is associated with a function op and gets
765 // cleaned when the latter is cleaned.
766 } else {
767 processSimpleOp(op, la, deadVals, finalCleanupList);
768 }
769 });
770
771 MLIRContext *context = module->getContext();
772 cleanUpDeadVals(context, finalCleanupList);
773
774 if (!canonicalize)
775 return;
776
777 // Canonicalize all region branch ops.
778 SmallVector<Operation *> opsToCanonicalize;
779 module->walk([&](RegionBranchOpInterface regionBranchOp) {
780 opsToCanonicalize.push_back(regionBranchOp.getOperation());
781 });
782 // Collect all canonicalization patterns for region branch ops.
783 RewritePatternSet owningPatterns(context);
784 DenseSet<RegisteredOperationName> populatedPatterns;
785 for (Operation *op : opsToCanonicalize)
786 if (std::optional<RegisteredOperationName> info = op->getRegisteredInfo())
787 if (populatedPatterns.insert(*info).second)
788 info->getCanonicalizationPatterns(owningPatterns, context);
789 if (failed(applyOpPatternsGreedily(opsToCanonicalize,
790 std::move(owningPatterns)))) {
791 module->emitError("greedy pattern rewrite failed to converge");
792 signalPassFailure();
793 }
794}
795
796std::unique_ptr<Pass> mlir::createRemoveDeadValuesPass() {
797 return std::make_unique<RemoveDeadValues>();
798}
b
Return true if permutation is a valid permutation of the outer_dims_perm (case OuterOrInnerPerm::Oute...
Block represents an ordered list of Operations.
Definition Block.h:33
BlockArgListType getArguments()
Definition Block.h:97
Block * getSuccessor(unsigned i)
Definition Block.cpp:274
This class coordinates rewriting a piece of IR outside of a pattern rewrite, providing a way to keep ...
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
This class provides a mutable adaptor for a range of operands.
Definition ValueRange.h:118
void erase(unsigned subStart, unsigned subLen=1)
Erase the operands within the given sub-range.
This class helps build Operations.
Definition Builders.h:207
void setInsertionPoint(Block *block, Block::iterator insertPoint)
Set the insertion point to the specified location.
Definition Builders.h:398
This class represents an operand of an operation.
Definition Value.h:257
Set of flags used to control the behavior of the various IR print methods (e.g.
This class provides the API for ops that are known to be terminators.
A wrapper class that allows for printing an operation with a set of flags, useful to act as a "stream...
Definition Operation.h:1111
Operation is the basic unit of execution within MLIR.
Definition Operation.h:88
Value getOperand(unsigned idx)
Definition Operation.h:350
bool hasTrait()
Returns true if the operation was registered with a particular trait, e.g.
Definition Operation.h:749
void dropAllUses()
Drop all uses of results of this operation.
Definition Operation.h:834
void setOperand(unsigned idx, Value value)
Definition Operation.h:351
void eraseOperands(unsigned idx, unsigned length=1)
Erase the operands starting at position idx and ending at position 'idx'+'length'.
Definition Operation.h:360
OpResult getResult(unsigned idx)
Get the 'idx'th result of this operation.
Definition Operation.h:407
Location getLoc()
The source location the operation was defined or derived from.
Definition Operation.h:223
std::optional< RegisteredOperationName > getRegisteredInfo()
If this operation has a registered operation description, return it.
Definition Operation.h:123
unsigned getNumOperands()
Definition Operation.h:346
operand_range getOperands()
Returns an iterator on the underlying Value's.
Definition Operation.h:378
result_range getResults()
Definition Operation.h:415
unsigned getNumResults()
Return the number of results held by this operation.
Definition Operation.h:404
This class coordinates the application of a rewrite on a set of IR, providing a way for clients to tr...
virtual void eraseOp(Operation *op)
This method erases an operation that is known to have no uses.
Operation * eraseOpResults(Operation *op, const BitVector &eraseIndices)
Erase the specified results of the given operation.
This class models how operands are forwarded to block arguments in control flow.
void erase(unsigned subStart, unsigned subLen=1)
Erase operands forwarded to the successor.
MutableOperandRange getMutableForwardedOperands() const
Get the range of operands that are simply forwarded to the successor.
unsigned size() const
Returns the amount of operands passed to the successor.
This class represents a specific symbol use.
This class implements a range of SymbolRef uses.
This class provides an abstraction over the different types of ranges over Values.
Definition ValueRange.h:387
This class represents an instance of an SSA value in the MLIR system, representing a computable value...
Definition Value.h:96
bool use_empty() const
Returns true if this value has no uses.
Definition Value.h:208
void dropAllUses()
Drop all uses of this object from their respective owners.
Definition Value.h:144
Type getType() const
Return the type of this value.
Definition Value.h:105
Location getLoc() const
Return the location of this value.
Definition Value.cpp:24
detail::InFlightRemark failed(Location loc, RemarkOpts opts)
Report an optimization remark that failed.
Definition Remarks.h:573
Include the generated interface declarations.
DenseMap< OpOperand *, SmallVector< Value > > RegionBranchSuccessorMapping
A mapping from successor operands to successor inputs.
llvm::DenseSet< ValueT, ValueInfoT > DenseSet
Definition LLVM.h:128
bool isMemoryEffectFree(Operation *op)
Returns true if the given operation is free of memory effects.
LogicalResult applyOpPatternsGreedily(ArrayRef< Operation * > ops, const FrozenRewritePatternSet &patterns, GreedyRewriteConfig config=GreedyRewriteConfig(), bool *changed=nullptr, bool *allErased=nullptr)
Rewrite the specified ops by repeatedly applying the highest benefit patterns in a greedy worklist dr...
std::unique_ptr< Pass > createRemoveDeadValuesPass()
Creates an optimization pass to remove dead values.
llvm::DenseMap< KeyT, ValueT, KeyInfoT, BucketT > DenseMap
Definition LLVM.h:126
This trait indicates that a terminator operation is "return-like".
This lattice represents, for a given value, whether or not it is "live".
Runs liveness analysis on the IR defined by op.
const Liveness * getLiveness(Value val)