-
Notifications
You must be signed in to change notification settings - Fork 2.4k
[AMD] getBackwardSlice variant with handling for op regions #4993
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,7 @@ | |
| #include "mlir/IR/Verifier.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Pass/PassManager.h" | ||
| #include "mlir/Transforms/RegionUtils.h" | ||
| #include "triton/Dialect/TritonGPU/IR/Dialect.h" | ||
| #include "triton/Dialect/TritonGPU/Transforms/Utility.h" | ||
| #include <deque> | ||
|
|
@@ -24,6 +25,73 @@ static bool isLocalLoadOrDotLayoutConversion(Operation *op) { | |
| return false; | ||
| } | ||
|
|
||
| // Copy of mlir::getBackwardSlice with changes to handle nested regions. | ||
| // This is a temporary local fix until these changes are upstreamed to mlir. | ||
| static void getDeepBackwardSlice(Operation *op, | ||
| SetVector<Operation *> *backwardSlice, | ||
| const BackwardSliceOptions &options) { | ||
| if (!op || op->hasTrait<OpTrait::IsIsolatedFromAbove>()) | ||
| 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; | ||
|
|
||
| SetVector<Value> usedValues; | ||
| Block *opBlock = op->getBlock(); | ||
| auto f = [&](OpOperand *nestedValue) { | ||
| // Filter out values that are not defined in the block | ||
| // that contains 'op'. This is to avoid including values | ||
| // that are defined in the nested regions of 'op'. | ||
| if (auto *nestedOp = nestedValue->get().getDefiningOp()) { | ||
| if (opBlock == nestedOp->getBlock()) { | ||
| usedValues.insert(nestedValue->get()); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| // collect all the values used in the nested regions of this op | ||
| // SetVector<Region*> nestedRegions; | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should this line be removed? |
||
| for (auto ®ion : op->getRegions()) { | ||
| region.walk([&](Region *nestedRegion) { | ||
| mlir::visitUsedValuesDefinedAbove(*nestedRegion, *nestedRegion, f); | ||
| }); | ||
| } | ||
|
|
||
| // collect all the values used in the op | ||
| for (const auto &en : llvm::enumerate(op->getOperands())) { | ||
| usedValues.insert(en.value()); | ||
| } | ||
|
|
||
| for (const auto &en : llvm::enumerate(usedValues)) { | ||
| auto operand = en.value(); | ||
| if (auto *definingOp = operand.getDefiningOp()) { | ||
| if (backwardSlice->count(definingOp) == 0) | ||
| getDeepBackwardSlice(definingOp, backwardSlice, options); | ||
| } else if (auto blockArg = dyn_cast<BlockArgument>(operand)) { | ||
| if (options.omitBlockArguments) | ||
| continue; | ||
|
|
||
| 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) { | ||
| assert(parentOp->getNumRegions() == 1 && | ||
| parentOp->getRegion(0).getBlocks().size() == 1); | ||
| getDeepBackwardSlice(parentOp, backwardSlice, options); | ||
| } | ||
| } else { | ||
| llvm_unreachable("No definingOp and not a block argument."); | ||
| } | ||
| } | ||
|
|
||
| backwardSlice->insert(op); | ||
| } | ||
|
|
||
| // Search through block to find earliest insertion point for move op. This can | ||
| // be either an atomic op or last usage of source pointer. Search ends when move | ||
| // op is encountered. | ||
|
|
@@ -221,8 +289,7 @@ class TritonAMDGPUReorderInstructionsPass | |
| // Only move ops residing in the same block. | ||
| return defBlock == block; | ||
| }; | ||
| mlir::getBackwardSlice(op, &backwardSet, options); | ||
| backwardSet.insert(op); | ||
| getDeepBackwardSlice(op, &backwardSet, options); | ||
|
|
||
| // Don't move a local_store if its source is a load from | ||
| // the same iteration. | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Consider making this fix to the MLIR code base, perhaps through
BackwardSliceOptions?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah BackwardSliceOptions will be a good place to handle this variant.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we should consider landing such general changes to upstream mlir. Then we can update llvm submodule to bring it in.