Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 4 additions & 8 deletions mlir/include/mlir/Analysis/SliceAnalysis.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,17 +140,13 @@ void getForwardSlice(Value root, SetVector<Operation *> *forwardSlice,
/// Assuming all local orders match the numbering order:
/// {1, 2, 5, 3, 4, 6}
///
/// This function returns whether the backwards slice was able to be
/// successfully computed, and failure if it was unable to determine the slice.
LogicalResult getBackwardSlice(Operation *op,
SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options = {});
void getBackwardSlice(Operation *op, SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options = {});

/// Value-rooted version of `getBackwardSlice`. Return the union of all backward
/// slices for the op defining or owning the value `root`.
LogicalResult getBackwardSlice(Value root,
SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options = {});
void getBackwardSlice(Value root, SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options = {});

/// Iteratively computes backward slices and forward slices until
/// a fixed point is reached. Returns an `SetVector<Operation *>` which
Expand Down
8 changes: 2 additions & 6 deletions mlir/include/mlir/Query/Matcher/SliceMatchers.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,9 +113,7 @@ bool BackwardSliceMatcher<Matcher>::matches(
}
return true;
};
LogicalResult result = getBackwardSlice(rootOp, &backwardSlice, options);
assert(result.succeeded() && "expected backward slice to succeed");
(void)result;
getBackwardSlice(rootOp, &backwardSlice, options);
return options.inclusive ? backwardSlice.size() > 1
: backwardSlice.size() >= 1;
}
Expand Down Expand Up @@ -143,9 +141,7 @@ class PredicateBackwardSliceMatcher {
options.filter = [&](Operation *subOp) {
return !filterMatcher.match(subOp);
};
LogicalResult result = getBackwardSlice(rootOp, &backwardSlice, options);
assert(result.succeeded() && "expected backward slice to succeed");
(void)result;
getBackwardSlice(rootOp, &backwardSlice, options);
return options.inclusive ? backwardSlice.size() > 1
: backwardSlice.size() >= 1;
}
Expand Down
92 changes: 38 additions & 54 deletions mlir/lib/Analysis/SliceAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,52 +105,47 @@ void mlir::getForwardSlice(Value root, SetVector<Operation *> *forwardSlice,
forwardSlice->insert(v.rbegin(), v.rend());
}

static LogicalResult getBackwardSliceImpl(Operation *op,
DenseSet<Operation *> &visited,
SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options) {
static void getBackwardSliceImpl(Operation *op, DenseSet<Operation *> &visited,
SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options) {
if (!op)
return success();
return;

// Evaluate whether we should keep this def.
// This is useful in particular to implement scoping; i.e. return the
// transitive backwardSlice in the current scope.
if (options.filter && !options.filter(op))
return success();
return;

auto processValue = [&](Value value) {
if (auto *definingOp = value.getDefiningOp()) {
if (backwardSlice->count(definingOp) == 0 &&
visited.insert(definingOp).second)
return getBackwardSliceImpl(definingOp, visited, backwardSlice,
options);
visited.insert(definingOp).second) {
getBackwardSliceImpl(definingOp, visited, backwardSlice, options);
return;
}

visited.erase(definingOp);
} else if (auto blockArg = dyn_cast<BlockArgument>(value)) {
if (options.omitBlockArguments)
return success();

Block *block = blockArg.getOwner();
Operation *parentOp = block->getParentOp();
// TODO: determine whether we want to recurse backward into the other
// blocks of parentOp, which are not technically backward unless they flow
// into us. For now, just bail.
if (parentOp && backwardSlice->count(parentOp) == 0) {
if (!parentOp->hasTrait<OpTrait::IsIsolatedFromAbove>() &&
parentOp->getNumRegions() == 1 &&
parentOp->getRegion(0).hasOneBlock()) {
return getBackwardSliceImpl(parentOp, visited, backwardSlice,
options);
}
return;
}
auto blockArg = cast<BlockArgument>(value);
if (options.omitBlockArguments)
return;

Block *block = blockArg.getOwner();
Operation *parentOp = block->getParentOp();
// TODO: determine whether we want to recurse backward into the other
// blocks of parentOp, which are not technically backward unless they flow
// into us. For now, just bail.
if (parentOp && backwardSlice->count(parentOp) == 0) {
if (!parentOp->hasTrait<OpTrait::IsIsolatedFromAbove>() &&
parentOp->getNumRegions() == 1 &&
parentOp->getRegion(0).hasOneBlock()) {
getBackwardSliceImpl(parentOp, visited, backwardSlice, options);
}
} else {
return failure();
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note that this case can never happen: An SSA value is always either an OpResult or a BlockArgument.

}
return success();
};

bool succeeded = true;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: This variable is always true.


if (!options.omitUsesFromAbove &&
!op->hasTrait<OpTrait::IsIsolatedFromAbove>()) {
llvm::for_each(op->getRegions(), [&](Region &region) {
Expand All @@ -162,44 +157,38 @@ static LogicalResult getBackwardSliceImpl(Operation *op,
region.walk([&](Operation *op) {
for (OpOperand &operand : op->getOpOperands()) {
if (!descendents.contains(operand.get().getParentRegion()))
if (!processValue(operand.get()).succeeded()) {
return WalkResult::interrupt();
}
processValue(operand.get());
}
return WalkResult::advance();
});
});
}
llvm::for_each(op->getOperands(), processValue);

backwardSlice->insert(op);
return success(succeeded);
}

LogicalResult mlir::getBackwardSlice(Operation *op,
SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options) {
void mlir::getBackwardSlice(Operation *op,
SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options) {
DenseSet<Operation *> visited;
visited.insert(op);
LogicalResult result =
getBackwardSliceImpl(op, visited, backwardSlice, options);
getBackwardSliceImpl(op, visited, backwardSlice, options);

if (!options.inclusive) {
// Don't insert the top level operation, we just queried on it and don't
// want it in the results.
backwardSlice->remove(op);
}
return result;
}

LogicalResult mlir::getBackwardSlice(Value root,
SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options) {
void mlir::getBackwardSlice(Value root, SetVector<Operation *> *backwardSlice,
const BackwardSliceOptions &options) {
if (Operation *definingOp = root.getDefiningOp()) {
return getBackwardSlice(definingOp, backwardSlice, options);
getBackwardSlice(definingOp, backwardSlice, options);
return;
}
Operation *bbAargOwner = cast<BlockArgument>(root).getOwner()->getParentOp();
return getBackwardSlice(bbAargOwner, backwardSlice, options);
Operation *bbArgOwner = cast<BlockArgument>(root).getOwner()->getParentOp();
getBackwardSlice(bbArgOwner, backwardSlice, options);
}

SetVector<Operation *>
Expand All @@ -215,10 +204,7 @@ mlir::getSlice(Operation *op, const BackwardSliceOptions &backwardSliceOptions,
auto *currentOp = (slice)[currentIndex];
// Compute and insert the backwardSlice starting from currentOp.
backwardSlice.clear();
LogicalResult result =
getBackwardSlice(currentOp, &backwardSlice, backwardSliceOptions);
assert(result.succeeded());
(void)result;
getBackwardSlice(currentOp, &backwardSlice, backwardSliceOptions);
slice.insert_range(backwardSlice);

// Compute and insert the forwardSlice starting from currentOp.
Expand All @@ -241,9 +227,7 @@ static bool dependsOnCarriedVals(Value value,
sliceOptions.filter = [&](Operation *op) {
return !ancestorOp->isAncestor(op);
};
LogicalResult result = getBackwardSlice(value, &slice, sliceOptions);
assert(result.succeeded());
(void)result;
getBackwardSlice(value, &slice, sliceOptions);

// Check that none of the operands of the operations in the backward slice are
// loop iteration arguments, and neither is the value itself.
Expand Down
5 changes: 1 addition & 4 deletions mlir/lib/Conversion/VectorToGPU/VectorToGPU.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,7 @@ getSliceContract(Operation *op,
auto *currentOp = (slice)[currentIndex];
// Compute and insert the backwardSlice starting from currentOp.
backwardSlice.clear();
LogicalResult result =
getBackwardSlice(currentOp, &backwardSlice, backwardSliceOptions);
assert(result.succeeded() && "expected a backward slice");
(void)result;
getBackwardSlice(currentOp, &backwardSlice, backwardSliceOptions);
slice.insert_range(backwardSlice);

// Compute and insert the forwardSlice starting from currentOp.
Expand Down
12 changes: 3 additions & 9 deletions mlir/lib/Dialect/Linalg/Transforms/HoistPadding.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,10 @@ static void computeBackwardSlice(tensor::PadOp padOp,
SetVector<Value> valuesDefinedAbove;
getUsedValuesDefinedAbove(padOp.getRegion(), padOp.getRegion(),
valuesDefinedAbove);
for (Value v : valuesDefinedAbove) {
LogicalResult result = getBackwardSlice(v, &backwardSlice, sliceOptions);
assert(result.succeeded() && "expected a backward slice");
(void)result;
}
for (Value v : valuesDefinedAbove)
getBackwardSlice(v, &backwardSlice, sliceOptions);
// Then, add the backward slice from padOp itself.
LogicalResult result =
getBackwardSlice(padOp.getOperation(), &backwardSlice, sliceOptions);
assert(result.succeeded() && "expected a backward slice");
(void)result;
getBackwardSlice(padOp.getOperation(), &backwardSlice, sliceOptions);
}

//===----------------------------------------------------------------------===//
Expand Down
10 changes: 3 additions & 7 deletions mlir/lib/Dialect/NVGPU/TransformOps/NVGPUTransformOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,13 +281,9 @@ static void getPipelineStages(
return visited->getBlock() == forOp.getBody();
});
options.inclusive = true;
for (Operation &op : forOp.getBody()->getOperations()) {
if (stage0Ops.contains(&op)) {
LogicalResult result = getBackwardSlice(&op, &dependencies, options);
assert(result.succeeded() && "expected a backward slice");
(void)result;
}
}
for (Operation &op : forOp.getBody()->getOperations())
if (stage0Ops.contains(&op))
getBackwardSlice(&op, &dependencies, options);

for (Operation &op : forOp.getBody()->getOperations()) {
if (!dependencies.contains(&op) && !isa<scf::YieldOp>(op))
Expand Down
7 changes: 2 additions & 5 deletions mlir/lib/Dialect/SCF/Transforms/TileUsingInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1970,11 +1970,8 @@ checkAssumptionForLoop(Operation *loopOp, Operation *consumerOp,
return !dominanceInfo.properlyDominates(op, *firstUserOfLoop);
};
llvm::SetVector<Operation *> slice;
for (auto operand : consumerOp->getOperands()) {
LogicalResult result = getBackwardSlice(operand, &slice, options);
assert(result.succeeded() && "expected a backward slice");
(void)result;
}
for (auto operand : consumerOp->getOperands())
getBackwardSlice(operand, &slice, options);

if (!slice.empty()) {
// If consumerOp has one producer, which is also the user of loopOp.
Expand Down
11 changes: 3 additions & 8 deletions mlir/lib/Transforms/Utils/RegionUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1116,9 +1116,7 @@ LogicalResult mlir::moveOperationDependencies(RewriterBase &rewriter,
return !dominance.properlyDominates(sliceBoundaryOp, insertionPoint);
};
llvm::SetVector<Operation *> slice;
LogicalResult result = getBackwardSlice(op, &slice, options);
assert(result.succeeded() && "expected a backward slice");
(void)result;
getBackwardSlice(op, &slice, options);

// If the slice contains `insertionPoint` cannot move the dependencies.
if (slice.contains(insertionPoint)) {
Expand Down Expand Up @@ -1182,11 +1180,8 @@ LogicalResult mlir::moveValueDefinitions(RewriterBase &rewriter,
return !dominance.properlyDominates(sliceBoundaryOp, insertionPoint);
};
llvm::SetVector<Operation *> slice;
for (auto value : prunedValues) {
LogicalResult result = getBackwardSlice(value, &slice, options);
assert(result.succeeded() && "expected a backward slice");
(void)result;
}
for (auto value : prunedValues)
getBackwardSlice(value, &slice, options);

// If the slice contains `insertionPoint` cannot move the dependencies.
if (slice.contains(insertionPoint)) {
Expand Down
5 changes: 1 addition & 4 deletions mlir/test/lib/Dialect/Affine/TestVectorizationUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,10 +154,7 @@ void VectorizerTestPass::testBackwardSlicing(llvm::raw_ostream &outs) {
patternTestSlicingOps().match(f, &matches);
for (auto m : matches) {
SetVector<Operation *> backwardSlice;
LogicalResult result =
getBackwardSlice(m.getMatchedOperation(), &backwardSlice);
assert(result.succeeded() && "expected a backward slice");
(void)result;
getBackwardSlice(m.getMatchedOperation(), &backwardSlice);
outs << "\nmatched: " << *m.getMatchedOperation()
<< " backward static slice: ";
for (auto *op : backwardSlice)
Expand Down
4 changes: 1 addition & 3 deletions mlir/test/lib/IR/TestSlicing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ static LogicalResult createBackwardSliceFunction(Operation *op,
options.omitBlockArguments = omitBlockArguments;
// TODO: Make this default.
options.omitUsesFromAbove = false;
LogicalResult result = getBackwardSlice(op, &slice, options);
assert(result.succeeded() && "expected a backward slice");
(void)result;
getBackwardSlice(op, &slice, options);
for (Operation *slicedOp : slice)
builder.clone(*slicedOp, mapper);
func::ReturnOp::create(builder, loc);
Expand Down