-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][acc] Add LegalizeDataValues support for DeclareEnterOp #138008
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
4c458ea
add support for acc.declare_enter
SusanTan 03b89b1
add support for enter data
SusanTan cd70987
change how dominfo is computed
SusanTan 014a8b8
leave out initialization
SusanTan 823d31d
add lit test
SusanTan 4814f8f
change to support multiple exits
SusanTan cb0a1b0
remove remaining enter_data
SusanTan a132a37
change the logic for when exitops is empty
SusanTan 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 |
|---|---|---|
|
|
@@ -10,6 +10,7 @@ | |
|
|
||
| #include "mlir/Dialect/Func/IR/FuncOps.h" | ||
| #include "mlir/Dialect/OpenACC/OpenACC.h" | ||
| #include "mlir/IR/Dominance.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Transforms/RegionUtils.h" | ||
| #include "llvm/Support/ErrorHandling.h" | ||
|
|
@@ -70,8 +71,38 @@ static void replaceAllUsesInAccComputeRegionsWith(Value orig, Value replacement, | |
| } | ||
| } | ||
|
|
||
| // Helper function to process declare enter/exit pairs | ||
| static void processDeclareEnterExit( | ||
| acc::DeclareEnterOp op, llvm::SmallVector<std::pair<Value, Value>> &values, | ||
| DominanceInfo &domInfo, PostDominanceInfo &postDomInfo) { | ||
| // For declare enter/exit pairs, verify there is exactly one exit op using the | ||
| // token | ||
| if (!op.getToken().hasOneUse()) | ||
| op.emitError("declare enter token must have exactly one use"); | ||
| Operation *user = *op.getToken().getUsers().begin(); | ||
| auto declareExit = dyn_cast<acc::DeclareExitOp>(user); | ||
| if (!declareExit) | ||
| op.emitError("declare enter token must be used by declare exit op"); | ||
|
|
||
| for (auto p : values) { | ||
| Value hostVal = std::get<0>(p); | ||
| Value deviceVal = std::get<1>(p); | ||
| for (auto &use : llvm::make_early_inc_range(hostVal.getUses())) { | ||
| Operation *owner = use.getOwner(); | ||
| if (!domInfo.dominates(op.getOperation(), owner) || | ||
| !postDomInfo.postDominates(declareExit.getOperation(), owner)) | ||
| continue; | ||
| if (insideAccComputeRegion(owner)) | ||
| use.set(deviceVal); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| template <typename Op> | ||
| static void collectAndReplaceInRegion(Op &op, bool hostToDevice) { | ||
| static void | ||
| collectAndReplaceInRegion(Op &op, bool hostToDevice, | ||
| DominanceInfo *domInfo = nullptr, | ||
| PostDominanceInfo *postDomInfo = nullptr) { | ||
| llvm::SmallVector<std::pair<Value, Value>> values; | ||
|
|
||
| if constexpr (std::is_same_v<Op, acc::LoopOp>) { | ||
|
|
@@ -82,16 +113,24 @@ static void collectAndReplaceInRegion(Op &op, bool hostToDevice) { | |
| if constexpr (!std::is_same_v<Op, acc::KernelsOp> && | ||
| !std::is_same_v<Op, acc::DataOp> && | ||
| !std::is_same_v<Op, acc::DeclareOp> && | ||
| !std::is_same_v<Op, acc::HostDataOp>) { | ||
| !std::is_same_v<Op, acc::HostDataOp> && | ||
| !std::is_same_v<Op, acc::DeclareEnterOp>) { | ||
| collectVars(op.getReductionOperands(), values, hostToDevice); | ||
| collectVars(op.getPrivateOperands(), values, hostToDevice); | ||
| collectVars(op.getFirstprivateOperands(), values, hostToDevice); | ||
| } | ||
| } | ||
|
|
||
| for (auto p : values) | ||
| replaceAllUsesInAccComputeRegionsWith<Op>(std::get<0>(p), std::get<1>(p), | ||
| op.getRegion()); | ||
| if constexpr (std::is_same_v<Op, acc::DeclareEnterOp>) { | ||
| assert(domInfo && postDomInfo && | ||
| "Dominance info required for DeclareEnterOp"); | ||
| processDeclareEnterExit(op, values, *domInfo, *postDomInfo); | ||
| } else { | ||
| for (auto p : values) { | ||
| replaceAllUsesInAccComputeRegionsWith<Op>(std::get<0>(p), std::get<1>(p), | ||
| op.getRegion()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| class LegalizeDataValuesInRegion | ||
|
|
@@ -105,6 +144,10 @@ class LegalizeDataValuesInRegion | |
| func::FuncOp funcOp = getOperation(); | ||
| bool replaceHostVsDevice = this->hostToDevice.getValue(); | ||
|
|
||
| // Get dominance info for the function | ||
| DominanceInfo domInfo(funcOp); | ||
|
||
| PostDominanceInfo postDomInfo(funcOp); | ||
|
|
||
| funcOp.walk([&](Operation *op) { | ||
| if (!isa<ACC_COMPUTE_CONSTRUCT_AND_LOOP_OPS>(*op) && | ||
| !(isa<ACC_DATA_CONSTRUCT_STRUCTURED_OPS>(*op) && | ||
|
|
@@ -125,6 +168,9 @@ class LegalizeDataValuesInRegion | |
| collectAndReplaceInRegion(declareOp, replaceHostVsDevice); | ||
| } else if (auto hostDataOp = dyn_cast<acc::HostDataOp>(*op)) { | ||
| collectAndReplaceInRegion(hostDataOp, replaceHostVsDevice); | ||
| } else if (auto declareEnterOp = dyn_cast<acc::DeclareEnterOp>(*op)) { | ||
| collectAndReplaceInRegion(declareEnterOp, replaceHostVsDevice, &domInfo, | ||
| &postDomInfo); | ||
| } else { | ||
| llvm_unreachable("unsupported acc region op"); | ||
| } | ||
|
|
||
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.
This should not be added to this list because although it can result from a structured declare construct, the operation itself allows unstructured flow.
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.
yep, i realized that was a mistake, changed it in the next commit