[mlir][shard] Hardening sharding propagation and partitioning#183028
Merged
[mlir][shard] Hardening sharding propagation and partitioning#183028
Conversation
Member
|
@llvm/pr-subscribers-mlir Author: Frank Schlimbach (fschlimb) ChangesSimple fixes to avoid failures. Full diff: https://github.com/llvm/llvm-project/pull/183028.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/Shard/Transforms/Partition.cpp b/mlir/lib/Dialect/Shard/Transforms/Partition.cpp
index 8b73bdd7ea60b..74fc59c4f6d14 100644
--- a/mlir/lib/Dialect/Shard/Transforms/Partition.cpp
+++ b/mlir/lib/Dialect/Shard/Transforms/Partition.cpp
@@ -532,7 +532,8 @@ shardedBlockArgumentTypes(Block &block,
block.getArguments(), std::back_inserter(res),
[&symbolTableCollection](BlockArgument arg) {
auto rankedTensorArg = dyn_cast<TypedValue<RankedTensorType>>(arg);
- if (!rankedTensorArg || rankedTensorArg.getType().getRank() == 0) {
+ if (!rankedTensorArg || rankedTensorArg.getType().getRank() == 0 ||
+ rankedTensorArg.use_empty()) {
return arg.getType();
}
@@ -666,7 +667,8 @@ partitionOperation(ShardOp shardOp, IRMapping &partitionMap,
static LogicalResult checkFullyAnnotated(Block &block) {
for (const BlockArgument &arg : block.getArguments()) {
auto rankedTensorArg = dyn_cast<TypedValue<RankedTensorType>>(arg);
- if (!rankedTensorArg || rankedTensorArg.getType().getRank() == 0)
+ if (!rankedTensorArg || rankedTensorArg.getType().getRank() == 0 ||
+ rankedTensorArg.getNumUses() < 1)
continue;
if (rankedTensorArg.getNumUses() > 1)
@@ -674,6 +676,7 @@ static LogicalResult checkFullyAnnotated(Block &block) {
<< "Cannot partition: expected a single use for block argument "
<< arg.getArgNumber() << " in block "
<< block.computeBlockNumber();
+
Operation *useOp = *rankedTensorArg.getUsers().begin();
auto shardOp = dyn_cast<ShardOp>(useOp);
if (!shardOp)
diff --git a/mlir/lib/Dialect/Shard/Transforms/ShardingPropagation.cpp b/mlir/lib/Dialect/Shard/Transforms/ShardingPropagation.cpp
index f954131ed7910..cff02d4f03143 100644
--- a/mlir/lib/Dialect/Shard/Transforms/ShardingPropagation.cpp
+++ b/mlir/lib/Dialect/Shard/Transforms/ShardingPropagation.cpp
@@ -379,6 +379,10 @@ struct ShardingPropagation
shardingOp.printLoopTypesAndIndexingMaps(llvm::dbgs());
});
+ // Nothing to propagate if there is no sharding annotation in the block.
+ if (block.getOps<shard::ShardOp>().empty())
+ return;
+
auto traverse = [&](auto &&range, OpBuilder &builder,
const char *order) -> bool {
for (Operation &op : range) {
|
28a1b6e to
739beec
Compare
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds defensive checks to prevent failures in MLIR's Shard dialect transformation passes. The changes add early returns and guards to handle edge cases where sharding operations or uses are absent, preventing potential assertion failures or crashes during sharding propagation and partitioning.
Changes:
- Added early return in ShardingPropagation when no ShardOp annotations exist in the block
- Added checks to skip unused block arguments in both
shardedBlockArgumentTypesandcheckFullyAnnotatedfunctions - Minor formatting improvement with blank line addition
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
| mlir/lib/Dialect/Shard/Transforms/ShardingPropagation.cpp | Adds early return optimization when block has no ShardOp operations, avoiding unnecessary traversal |
| mlir/lib/Dialect/Shard/Transforms/Partition.cpp | Adds defensive checks to handle unused block arguments in type deduction and validation logic, plus formatting cleanup |
Contributor
Author
|
Will enable llvm/lighthouse#57 |
tudinhh
pushed a commit
to tudinhh/llvm-project
that referenced
this pull request
Feb 26, 2026
HendrikHuebner
pushed a commit
to HendrikHuebner/llvm-project
that referenced
this pull request
Mar 10, 2026
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Simple fixes to avoid failures.