MLIR 22.0.0git
SliceWalk.h
Go to the documentation of this file.
1//===- SliceWalk.h - Helpers for performing IR slice walks ---*- 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#ifndef MLIR_ANALYSIS_SLICEWALK_H
10#define MLIR_ANALYSIS_SLICEWALK_H
11
12#include "mlir/IR/ValueRange.h"
13
14namespace mlir {
15
16/// A class to signal how to proceed with the walk of the backward slice:
17/// - Interrupt: Stops the walk.
18/// - AdvanceTo: Continues the walk to user-specified values.
19/// - Skip: Continues the walk, but skips the predecessors of the current value.
21public:
22 enum class WalkAction {
23 /// Stops the walk.
25 /// Continues the walk to user-specified values.
27 /// Continues the walk, but skips the predecessors of the current value.
29 };
30
32 : action(action), nextValues(nextValues) {}
33
34 /// Allows diagnostics to interrupt the walk.
37
38 /// Allows diagnostics to interrupt the walk.
41
42 /// Creates a continuation that interrupts the walk.
46
47 /// Creates a continuation that adds the user-specified `nextValues` to the
48 /// work list and advances the walk.
50 return WalkContinuation(WalkAction::AdvanceTo, nextValues);
51 }
52
53 /// Creates a continuation that advances the walk without adding any
54 /// predecessor values to the work list.
57 }
58
59 /// Returns true if the walk was interrupted.
60 bool wasInterrupted() const { return action == WalkAction::Interrupt; }
61
62 /// Returns true if the walk was skipped.
63 bool wasSkipped() const { return action == WalkAction::Skip; }
64
65 /// Returns true if the walk was advanced to user-specified values.
66 bool wasAdvancedTo() const { return action == WalkAction::AdvanceTo; }
67
68 /// Returns the next values to continue the walk with.
69 mlir::ArrayRef<mlir::Value> getNextValues() const { return nextValues; }
70
71private:
72 WalkAction action;
73 /// The next values to continue the walk with.
75};
76
77/// A callback that is invoked for each value encountered during the walk of the
78/// slice. The callback takes the current value, and returns the walk
79/// continuation, which determines if the walk should proceed and if yes, with
80/// which values.
82
83/// Walks the slice starting from the `rootValues` using a depth-first
84/// traversal. The walk calls the provided `walkCallback` for each value
85/// encountered in the slice and uses the returned walk continuation to
86/// determine how to proceed.
88 WalkCallback walkCallback);
89
90/// Computes a vector of all control predecessors of `value`. Relies on
91/// RegionBranchOpInterface, BranchOpInterface, and SelectLikeOpInterface to
92/// determine predecessors. Returns nullopt if `value` has no predecessors or
93/// when the relevant operations are missing the interface implementations.
94std::optional<SmallVector<Value>> getControlFlowPredecessors(Value value);
95
96} // namespace mlir
97
98#endif // MLIR_ANALYSIS_SLICEWALK_H
This class contains all of the information necessary to report a diagnostic to the DiagnosticEngine.
This class represents a diagnostic that is inflight and set to be reported.
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
A class to signal how to proceed with the walk of the backward slice:
Definition SliceWalk.h:20
@ Skip
Continues the walk, but skips the predecessors of the current value.
Definition SliceWalk.h:28
@ AdvanceTo
Continues the walk to user-specified values.
Definition SliceWalk.h:26
bool wasInterrupted() const
Returns true if the walk was interrupted.
Definition SliceWalk.h:60
WalkContinuation(WalkAction action, mlir::ValueRange nextValues)
Definition SliceWalk.h:31
bool wasAdvancedTo() const
Returns true if the walk was advanced to user-specified values.
Definition SliceWalk.h:66
static WalkContinuation skip()
Creates a continuation that advances the walk without adding any predecessor values to the work list.
Definition SliceWalk.h:55
static WalkContinuation advanceTo(mlir::ValueRange nextValues)
Creates a continuation that adds the user-specified nextValues to the work list and advances the walk...
Definition SliceWalk.h:49
mlir::ArrayRef< mlir::Value > getNextValues() const
Returns the next values to continue the walk with.
Definition SliceWalk.h:69
static WalkContinuation interrupt()
Creates a continuation that interrupts the walk.
Definition SliceWalk.h:43
WalkContinuation(mlir::Diagnostic &&)
Allows diagnostics to interrupt the walk.
Definition SliceWalk.h:35
WalkContinuation(mlir::InFlightDiagnostic &&)
Allows diagnostics to interrupt the walk.
Definition SliceWalk.h:39
bool wasSkipped() const
Returns true if the walk was skipped.
Definition SliceWalk.h:63
Include the generated interface declarations.
std::optional< SmallVector< Value > > getControlFlowPredecessors(Value value)
Computes a vector of all control predecessors of value.
WalkContinuation walkSlice(mlir::ValueRange rootValues, WalkCallback walkCallback)
Walks the slice starting from the rootValues using a depth-first traversal.
Definition SliceWalk.cpp:6
mlir::function_ref< WalkContinuation(mlir::Value)> WalkCallback
A callback that is invoked for each value encountered during the walk of the slice.
Definition SliceWalk.h:81
llvm::function_ref< Fn > function_ref
Definition LLVM.h:152