Skip to content

Commit 1415365

Browse files
authored
[MLIR][LLVM]: Add an IR utility to perform slice walking (#103053)
This commit introduces a slicing utility that can be used to walk arbitrary IR slices. It additionally ships logic to determine control flow predecessors, which allows users to walk backward slices without dealing with both `RegionBranchOpInterface` and `BranchOpInterface`. This utility is used to improve the `noalias` propagation in the LLVM dialect's inliner interface. Before this change, it broke down as soon as pointer were passed through region control flow operations.
1 parent 6f6422f commit 1415365

File tree

5 files changed

+333
-76
lines changed

5 files changed

+333
-76
lines changed
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
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+
14+
namespace 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.
20+
class WalkContinuation {
21+
public:
22+
enum class WalkAction {
23+
/// Stops the walk.
24+
Interrupt,
25+
/// Continues the walk to user-specified values.
26+
AdvanceTo,
27+
/// Continues the walk, but skips the predecessors of the current value.
28+
Skip
29+
};
30+
31+
WalkContinuation(WalkAction action, mlir::ValueRange nextValues)
32+
: action(action), nextValues(nextValues) {}
33+
34+
/// Allows diagnostics to interrupt the walk.
35+
explicit WalkContinuation(mlir::Diagnostic &&)
36+
: action(WalkAction::Interrupt) {}
37+
38+
/// Allows diagnostics to interrupt the walk.
39+
explicit WalkContinuation(mlir::InFlightDiagnostic &&)
40+
: action(WalkAction::Interrupt) {}
41+
42+
/// Creates a continuation that interrupts the walk.
43+
static WalkContinuation interrupt() {
44+
return WalkContinuation(WalkAction::Interrupt, {});
45+
}
46+
47+
/// Creates a continuation that adds the user-specified `nextValues` to the
48+
/// work list and advances the walk.
49+
static WalkContinuation advanceTo(mlir::ValueRange nextValues) {
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.
55+
static WalkContinuation skip() {
56+
return WalkContinuation(WalkAction::Skip, {});
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+
71+
private:
72+
WalkAction action;
73+
/// The next values to continue the walk with.
74+
mlir::SmallVector<mlir::Value> nextValues;
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.
81+
using WalkCallback = mlir::function_ref<WalkContinuation(mlir::Value)>;
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.
87+
WalkContinuation walkSlice(mlir::ValueRange rootValues,
88+
WalkCallback walkCallback);
89+
90+
/// Computes a vector of all control predecessors of `value`. Relies on
91+
/// RegionBranchOpInterface and BranchOpInterface to determine predecessors.
92+
/// Returns nullopt if `value` has no predecessors or when the relevant
93+
/// operations are missing the interface implementations.
94+
std::optional<SmallVector<Value>> getControlFlowPredecessors(Value value);
95+
96+
} // namespace mlir
97+
98+
#endif // MLIR_ANALYSIS_SLICEWALK_H

mlir/lib/Analysis/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ add_mlir_library(MLIRAnalysis
2929
Liveness.cpp
3030
CFGLoopInfo.cpp
3131
SliceAnalysis.cpp
32+
SliceWalk.cpp
3233
TopologicalSortUtils.cpp
3334

3435
AliasAnalysis/LocalAliasAnalysis.cpp

mlir/lib/Analysis/SliceWalk.cpp

Lines changed: 139 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,139 @@
1+
#include "mlir/Analysis/SliceWalk.h"
2+
#include "mlir/Interfaces/ControlFlowInterfaces.h"
3+
4+
using namespace mlir;
5+
6+
WalkContinuation mlir::walkSlice(ValueRange rootValues,
7+
WalkCallback walkCallback) {
8+
// Search the backward slice starting from the root values.
9+
SmallVector<Value> workList = rootValues;
10+
llvm::SmallDenseSet<Value, 16> seenValues;
11+
while (!workList.empty()) {
12+
// Search the backward slice of the current value.
13+
Value current = workList.pop_back_val();
14+
15+
// Skip the current value if it has already been seen.
16+
if (!seenValues.insert(current).second)
17+
continue;
18+
19+
// Call the walk callback with the current value.
20+
WalkContinuation continuation = walkCallback(current);
21+
if (continuation.wasInterrupted())
22+
return continuation;
23+
if (continuation.wasSkipped())
24+
continue;
25+
26+
assert(continuation.wasAdvancedTo());
27+
// Add the next values to the work list if the walk should continue.
28+
workList.append(continuation.getNextValues().begin(),
29+
continuation.getNextValues().end());
30+
}
31+
32+
return WalkContinuation::skip();
33+
}
34+
35+
/// Returns the operands from all predecessor regions that match `operandNumber`
36+
/// for the `successor` region within `regionOp`.
37+
static SmallVector<Value>
38+
getRegionPredecessorOperands(RegionBranchOpInterface regionOp,
39+
RegionSuccessor successor,
40+
unsigned operandNumber) {
41+
SmallVector<Value> predecessorOperands;
42+
43+
// Returns true if `successors` contains `successor`.
44+
auto isContained = [](ArrayRef<RegionSuccessor> successors,
45+
RegionSuccessor successor) {
46+
auto *it = llvm::find_if(successors, [&successor](RegionSuccessor curr) {
47+
return curr.getSuccessor() == successor.getSuccessor();
48+
});
49+
return it != successors.end();
50+
};
51+
52+
// Search the operand ranges on the region operation itself.
53+
SmallVector<Attribute> operandAttributes(regionOp->getNumOperands());
54+
SmallVector<RegionSuccessor> successors;
55+
regionOp.getEntrySuccessorRegions(operandAttributes, successors);
56+
if (isContained(successors, successor)) {
57+
OperandRange operands = regionOp.getEntrySuccessorOperands(successor);
58+
predecessorOperands.push_back(operands[operandNumber]);
59+
}
60+
61+
// Search the operand ranges on region terminators.
62+
for (Region &region : regionOp->getRegions()) {
63+
for (Block &block : region) {
64+
auto terminatorOp =
65+
dyn_cast<RegionBranchTerminatorOpInterface>(block.getTerminator());
66+
if (!terminatorOp)
67+
continue;
68+
SmallVector<Attribute> operandAttributes(terminatorOp->getNumOperands());
69+
SmallVector<RegionSuccessor> successors;
70+
terminatorOp.getSuccessorRegions(operandAttributes, successors);
71+
if (isContained(successors, successor)) {
72+
OperandRange operands = terminatorOp.getSuccessorOperands(successor);
73+
predecessorOperands.push_back(operands[operandNumber]);
74+
}
75+
}
76+
}
77+
78+
return predecessorOperands;
79+
}
80+
81+
/// Returns the predecessor branch operands that match `blockArg`, or nullopt if
82+
/// some of the predecessor terminators do not implement the BranchOpInterface.
83+
static std::optional<SmallVector<Value>>
84+
getBlockPredecessorOperands(BlockArgument blockArg) {
85+
Block *block = blockArg.getOwner();
86+
87+
// Search the predecessor operands for all predecessor terminators.
88+
SmallVector<Value> predecessorOperands;
89+
for (auto it = block->pred_begin(); it != block->pred_end(); ++it) {
90+
Block *predecessor = *it;
91+
auto branchOp = dyn_cast<BranchOpInterface>(predecessor->getTerminator());
92+
if (!branchOp)
93+
return std::nullopt;
94+
SuccessorOperands successorOperands =
95+
branchOp.getSuccessorOperands(it.getSuccessorIndex());
96+
// Store the predecessor operand if the block argument matches an operand
97+
// and is not produced by the terminator.
98+
if (Value operand = successorOperands[blockArg.getArgNumber()])
99+
predecessorOperands.push_back(operand);
100+
}
101+
102+
return predecessorOperands;
103+
}
104+
105+
std::optional<SmallVector<Value>>
106+
mlir::getControlFlowPredecessors(Value value) {
107+
SmallVector<Value> result;
108+
if (OpResult opResult = dyn_cast<OpResult>(value)) {
109+
auto regionOp = dyn_cast<RegionBranchOpInterface>(opResult.getOwner());
110+
// If the interface is not implemented, there are no control flow
111+
// predecessors to work with.
112+
if (!regionOp)
113+
return std::nullopt;
114+
// Add the control flow predecessor operands to the work list.
115+
RegionSuccessor region(regionOp->getResults());
116+
SmallVector<Value> predecessorOperands = getRegionPredecessorOperands(
117+
regionOp, region, opResult.getResultNumber());
118+
return predecessorOperands;
119+
}
120+
121+
auto blockArg = cast<BlockArgument>(value);
122+
Block *block = blockArg.getOwner();
123+
// Search the region predecessor operands for structured control flow.
124+
if (block->isEntryBlock()) {
125+
if (auto regionBranchOp =
126+
dyn_cast<RegionBranchOpInterface>(block->getParentOp())) {
127+
RegionSuccessor region(blockArg.getParentRegion());
128+
SmallVector<Value> predecessorOperands = getRegionPredecessorOperands(
129+
regionBranchOp, region, blockArg.getArgNumber());
130+
return predecessorOperands;
131+
}
132+
// If the interface is not implemented, there are no control flow
133+
// predecessors to work with.
134+
return std::nullopt;
135+
}
136+
137+
// Search the block predecessor operands for unstructured control flow.
138+
return getBlockPredecessorOperands(blockArg);
139+
}

mlir/lib/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.cpp

Lines changed: 41 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
//===----------------------------------------------------------------------===//
1313

1414
#include "mlir/Dialect/LLVMIR/Transforms/InlinerInterfaceImpl.h"
15+
#include "mlir/Analysis/SliceWalk.h"
1516
#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
1617
#include "mlir/IR/Matchers.h"
1718
#include "mlir/Interfaces/DataLayoutInterfaces.h"
@@ -221,86 +222,45 @@ static ArrayAttr concatArrayAttr(ArrayAttr lhs, ArrayAttr rhs) {
221222
return ArrayAttr::get(lhs.getContext(), result);
222223
}
223224

224-
/// Attempts to return the underlying pointer value that `pointerValue` is based
225-
/// on. This traverses down the chain of operations to the last operation
226-
/// producing the base pointer and returns it. If it encounters an operation it
227-
/// cannot further traverse through, returns the operation's result.
228-
static Value getUnderlyingObject(Value pointerValue) {
229-
while (true) {
230-
if (auto gepOp = pointerValue.getDefiningOp<LLVM::GEPOp>()) {
231-
pointerValue = gepOp.getBase();
232-
continue;
233-
}
234-
235-
if (auto addrCast = pointerValue.getDefiningOp<LLVM::AddrSpaceCastOp>()) {
236-
pointerValue = addrCast.getOperand();
237-
continue;
238-
}
239-
240-
break;
241-
}
242-
243-
return pointerValue;
244-
}
245-
246225
/// Attempts to return the set of all underlying pointer values that
247226
/// `pointerValue` is based on. This function traverses through select
248-
/// operations and block arguments unlike getUnderlyingObject.
249-
static SmallVector<Value> getUnderlyingObjectSet(Value pointerValue) {
227+
/// operations and block arguments.
228+
static FailureOr<SmallVector<Value>>
229+
getUnderlyingObjectSet(Value pointerValue) {
250230
SmallVector<Value> result;
251-
252-
SmallVector<Value> workList{pointerValue};
253-
// Avoid dataflow loops.
254-
SmallPtrSet<Value, 4> seen;
255-
do {
256-
Value current = workList.pop_back_val();
257-
current = getUnderlyingObject(current);
258-
259-
if (!seen.insert(current).second)
260-
continue;
261-
262-
if (auto selectOp = current.getDefiningOp<LLVM::SelectOp>()) {
263-
workList.push_back(selectOp.getTrueValue());
264-
workList.push_back(selectOp.getFalseValue());
265-
continue;
231+
WalkContinuation walkResult = walkSlice(pointerValue, [&](Value val) {
232+
if (auto gepOp = val.getDefiningOp<LLVM::GEPOp>())
233+
return WalkContinuation::advanceTo(gepOp.getBase());
234+
235+
if (auto addrCast = val.getDefiningOp<LLVM::AddrSpaceCastOp>())
236+
return WalkContinuation::advanceTo(addrCast.getOperand());
237+
238+
// TODO: Add a SelectLikeOpInterface and use it in the slicing utility.
239+
if (auto selectOp = val.getDefiningOp<LLVM::SelectOp>())
240+
return WalkContinuation::advanceTo(
241+
{selectOp.getTrueValue(), selectOp.getFalseValue()});
242+
243+
// Attempt to advance to control flow predecessors.
244+
std::optional<SmallVector<Value>> controlFlowPredecessors =
245+
getControlFlowPredecessors(val);
246+
if (controlFlowPredecessors)
247+
return WalkContinuation::advanceTo(*controlFlowPredecessors);
248+
249+
// For all non-control flow results, consider `val` an underlying object.
250+
if (isa<OpResult>(val)) {
251+
result.push_back(val);
252+
return WalkContinuation::skip();
266253
}
267254

268-
if (auto blockArg = dyn_cast<BlockArgument>(current)) {
269-
Block *parentBlock = blockArg.getParentBlock();
270-
271-
// Attempt to find all block argument operands for every predecessor.
272-
// If any operand to the block argument wasn't found in a predecessor,
273-
// conservatively add the block argument to the result set.
274-
SmallVector<Value> operands;
275-
bool anyUnknown = false;
276-
for (auto iter = parentBlock->pred_begin();
277-
iter != parentBlock->pred_end(); iter++) {
278-
auto branch = dyn_cast<BranchOpInterface>((*iter)->getTerminator());
279-
if (!branch) {
280-
result.push_back(blockArg);
281-
anyUnknown = true;
282-
break;
283-
}
284-
285-
Value operand = branch.getSuccessorOperands(
286-
iter.getSuccessorIndex())[blockArg.getArgNumber()];
287-
if (!operand) {
288-
result.push_back(blockArg);
289-
anyUnknown = true;
290-
break;
291-
}
292-
293-
operands.push_back(operand);
294-
}
295-
296-
if (!anyUnknown)
297-
llvm::append_range(workList, operands);
298-
299-
continue;
300-
}
255+
// If this place is reached, `val` is a block argument that is not
256+
// understood. Therefore, we conservatively interrupt.
257+
// Note: Dealing with function arguments is not necessary, as the slice
258+
// would have to go through an SSACopyOp first.
259+
return WalkContinuation::interrupt();
260+
});
301261

302-
result.push_back(current);
303-
} while (!workList.empty());
262+
if (walkResult.wasInterrupted())
263+
return failure();
304264

305265
return result;
306266
}
@@ -363,9 +323,14 @@ static void createNewAliasScopesFromNoAliasParameter(
363323

364324
// Find the set of underlying pointers that this pointer is based on.
365325
SmallPtrSet<Value, 4> basedOnPointers;
366-
for (Value pointer : pointerArgs)
367-
llvm::copy(getUnderlyingObjectSet(pointer),
326+
for (Value pointer : pointerArgs) {
327+
FailureOr<SmallVector<Value>> underlyingObjectSet =
328+
getUnderlyingObjectSet(pointer);
329+
if (failed(underlyingObjectSet))
330+
return;
331+
llvm::copy(*underlyingObjectSet,
368332
std::inserter(basedOnPointers, basedOnPointers.begin()));
333+
}
369334

370335
bool aliasesOtherKnownObject = false;
371336
// Go through the based on pointers and check that they are either:

0 commit comments

Comments
 (0)