-
Notifications
You must be signed in to change notification settings - Fork 15.2k
[mlir][SCF] Add scf.index_switch support for populateSCFStructuralTypeConversionsAndLegality #160344
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
[mlir][SCF] Add scf.index_switch support for populateSCFStructuralTypeConversionsAndLegality #160344
Conversation
…eConversionsAndLegality
Thank you for submitting a Pull Request (PR) to the LLVM Project! This PR will be automatically labeled and the relevant teams will be notified. If you wish to, you can add reviewers by using the "Reviewers" section on this page. If this is not working for you, it is probably because you do not have write permissions for the repository. In which case you can instead tag reviewers by name in a comment by using If you have received no comments on your PR for a week, you can request a review by "ping"ing the PR by adding a comment “Ping”. The common courtesy "ping" rate is once a week. Please remember that you are asking for valuable time from other developers. If you have further questions, they may be answered by the LLVM GitHub User Guide. You can also ask questions in a comment on this PR, on the LLVM Discord or on the forums. |
@llvm/pr-subscribers-mlir Author: Artemy Skrebkov (ArtemySkrebkov) ChangesIn a downstream project, there is a need for a type conversion pattern for scf.index_switch operation. A test is added into Full diff: https://github.com/llvm/llvm-project/pull/160344.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp b/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
index b0c781c7aff11..c9ff7885f3a49 100644
--- a/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
@@ -185,6 +185,31 @@ class ConvertWhileOpTypes
};
} // namespace
+namespace {
+class ConvertIndexSwitchOpTypes
+ : public Structural1ToNConversionPattern<IndexSwitchOp,
+ ConvertIndexSwitchOpTypes> {
+public:
+ using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
+
+ std::optional<IndexSwitchOp>
+ convertSourceOp(IndexSwitchOp op, OneToNOpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter,
+ TypeRange dstTypes) const {
+ auto newOp = rewriter.create<IndexSwitchOp>(
+ op.getLoc(), dstTypes, op.getArg(), op.getCases(), op.getNumCases());
+
+ for (unsigned i = 0u; i < op.getNumRegions(); i++) {
+ if (failed(rewriter.convertRegionTypes(&op.getRegion(i), *typeConverter)))
+ return std::nullopt;
+ auto &dstRegion = newOp.getRegion(i);
+ rewriter.inlineRegionBefore(op.getRegion(i), dstRegion, dstRegion.end());
+ }
+ return newOp;
+ }
+};
+} // namespace
+
namespace {
// When the result types of a ForOp/IfOp get changed, the operand types of the
// corresponding yield op need to be changed. In order to trigger the
@@ -220,18 +245,19 @@ void mlir::scf::populateSCFStructuralTypeConversions(
const TypeConverter &typeConverter, RewritePatternSet &patterns,
PatternBenefit benefit) {
patterns.add<ConvertForOpTypes, ConvertIfOpTypes, ConvertYieldOpTypes,
- ConvertWhileOpTypes, ConvertConditionOpTypes>(
- typeConverter, patterns.getContext(), benefit);
+ ConvertWhileOpTypes, ConvertConditionOpTypes,
+ ConvertIndexSwitchOpTypes>(typeConverter, patterns.getContext(),
+ benefit);
}
void mlir::scf::populateSCFStructuralTypeConversionTarget(
const TypeConverter &typeConverter, ConversionTarget &target) {
- target.addDynamicallyLegalOp<ForOp, IfOp>(
+ target.addDynamicallyLegalOp<ForOp, IfOp, IndexSwitchOp>(
[&](Operation *op) { return typeConverter.isLegal(op->getResults()); });
target.addDynamicallyLegalOp<scf::YieldOp>([&](scf::YieldOp op) {
// We only have conversions for a subset of ops that use scf.yield
// terminators.
- if (!isa<ForOp, IfOp, WhileOp>(op->getParentOp()))
+ if (!isa<ForOp, IfOp, WhileOp, IndexSwitchOp>(op->getParentOp()))
return true;
return typeConverter.isLegal(op.getOperands());
});
diff --git a/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir b/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
index f5d6a08b7de31..00f13ed7c8149 100644
--- a/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
+++ b/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
@@ -86,3 +86,46 @@ func.func @while(%arg0: tensor<1024xf32, #SparseVector>, %c: i1) -> tensor<1024x
}
return %0: tensor<1024xf32, #SparseVector>
}
+
+// CHECK-LABEL: func.func @index_switch(
+// CHECK-SAME: %[[PRED:.*0]]: index,
+// CHECK-SAME: %[[VAL_A_1:.*1]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_A_2:.*2]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_A_3:.*3]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_A_4:.*4]]: !sparse_tensor.storage_specifier
+// CHECK-SAME: %[[VAL_B_1:.*5]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_B_2:.*6]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_B_3:.*7]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_B_4:.*8]]: !sparse_tensor.storage_specifier
+// CHECK-SAME: %[[VAL_C_1:.*9]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_C_2:.*10]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_C_3:.*11]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_C_4:.*12]]: !sparse_tensor.storage_specifier
+
+// CHECK: %[[RES:.*]]:4 = scf.index_switch %[[PRED]]
+// CHECK: case 1 {
+// CHECK: scf.yield %[[VAL_A_1]], %[[VAL_A_2]], %[[VAL_A_3]], %[[VAL_A_4]]
+// CHECK: case 2 {
+// CHECK: scf.yield %[[VAL_B_1]], %[[VAL_B_2]], %[[VAL_B_3]], %[[VAL_B_4]]
+// CHECK: default {
+// CHECK: scf.yield %[[VAL_C_1]], %[[VAL_C_2]], %[[VAL_C_3]], %[[VAL_C_4]]
+
+// CHECK: return %[[RES]]#0, %[[RES]]#1, %[[RES]]#2, %[[RES]]#3 :
+// CHECK-SAME: memref<?xindex>, memref<?xindex>, memref<?xf32>, !sparse_tensor.storage_specifier
+
+func.func @index_switch(%pred: index, %a: tensor<5xf32, #SparseVector>,
+ %b: tensor<5xf32, #SparseVector>,
+ %c: tensor<5xf32, #SparseVector>) -> tensor<5xf32, #SparseVector> {
+ %0 = scf.index_switch %pred -> tensor<5xf32, #SparseVector>
+ case 1 {
+ scf.yield %a : tensor<5xf32, #SparseVector>
+ }
+ case 2 {
+ scf.yield %b : tensor<5xf32, #SparseVector>
+ }
+ default {
+ scf.yield %c : tensor<5xf32, #SparseVector>
+ }
+
+ return %0 : tensor<5xf32, #SparseVector>
+}
|
@llvm/pr-subscribers-mlir-scf Author: Artemy Skrebkov (ArtemySkrebkov) ChangesIn a downstream project, there is a need for a type conversion pattern for scf.index_switch operation. A test is added into Full diff: https://github.com/llvm/llvm-project/pull/160344.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp b/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
index b0c781c7aff11..c9ff7885f3a49 100644
--- a/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
@@ -185,6 +185,31 @@ class ConvertWhileOpTypes
};
} // namespace
+namespace {
+class ConvertIndexSwitchOpTypes
+ : public Structural1ToNConversionPattern<IndexSwitchOp,
+ ConvertIndexSwitchOpTypes> {
+public:
+ using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
+
+ std::optional<IndexSwitchOp>
+ convertSourceOp(IndexSwitchOp op, OneToNOpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter,
+ TypeRange dstTypes) const {
+ auto newOp = rewriter.create<IndexSwitchOp>(
+ op.getLoc(), dstTypes, op.getArg(), op.getCases(), op.getNumCases());
+
+ for (unsigned i = 0u; i < op.getNumRegions(); i++) {
+ if (failed(rewriter.convertRegionTypes(&op.getRegion(i), *typeConverter)))
+ return std::nullopt;
+ auto &dstRegion = newOp.getRegion(i);
+ rewriter.inlineRegionBefore(op.getRegion(i), dstRegion, dstRegion.end());
+ }
+ return newOp;
+ }
+};
+} // namespace
+
namespace {
// When the result types of a ForOp/IfOp get changed, the operand types of the
// corresponding yield op need to be changed. In order to trigger the
@@ -220,18 +245,19 @@ void mlir::scf::populateSCFStructuralTypeConversions(
const TypeConverter &typeConverter, RewritePatternSet &patterns,
PatternBenefit benefit) {
patterns.add<ConvertForOpTypes, ConvertIfOpTypes, ConvertYieldOpTypes,
- ConvertWhileOpTypes, ConvertConditionOpTypes>(
- typeConverter, patterns.getContext(), benefit);
+ ConvertWhileOpTypes, ConvertConditionOpTypes,
+ ConvertIndexSwitchOpTypes>(typeConverter, patterns.getContext(),
+ benefit);
}
void mlir::scf::populateSCFStructuralTypeConversionTarget(
const TypeConverter &typeConverter, ConversionTarget &target) {
- target.addDynamicallyLegalOp<ForOp, IfOp>(
+ target.addDynamicallyLegalOp<ForOp, IfOp, IndexSwitchOp>(
[&](Operation *op) { return typeConverter.isLegal(op->getResults()); });
target.addDynamicallyLegalOp<scf::YieldOp>([&](scf::YieldOp op) {
// We only have conversions for a subset of ops that use scf.yield
// terminators.
- if (!isa<ForOp, IfOp, WhileOp>(op->getParentOp()))
+ if (!isa<ForOp, IfOp, WhileOp, IndexSwitchOp>(op->getParentOp()))
return true;
return typeConverter.isLegal(op.getOperands());
});
diff --git a/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir b/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
index f5d6a08b7de31..00f13ed7c8149 100644
--- a/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
+++ b/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
@@ -86,3 +86,46 @@ func.func @while(%arg0: tensor<1024xf32, #SparseVector>, %c: i1) -> tensor<1024x
}
return %0: tensor<1024xf32, #SparseVector>
}
+
+// CHECK-LABEL: func.func @index_switch(
+// CHECK-SAME: %[[PRED:.*0]]: index,
+// CHECK-SAME: %[[VAL_A_1:.*1]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_A_2:.*2]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_A_3:.*3]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_A_4:.*4]]: !sparse_tensor.storage_specifier
+// CHECK-SAME: %[[VAL_B_1:.*5]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_B_2:.*6]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_B_3:.*7]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_B_4:.*8]]: !sparse_tensor.storage_specifier
+// CHECK-SAME: %[[VAL_C_1:.*9]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_C_2:.*10]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_C_3:.*11]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_C_4:.*12]]: !sparse_tensor.storage_specifier
+
+// CHECK: %[[RES:.*]]:4 = scf.index_switch %[[PRED]]
+// CHECK: case 1 {
+// CHECK: scf.yield %[[VAL_A_1]], %[[VAL_A_2]], %[[VAL_A_3]], %[[VAL_A_4]]
+// CHECK: case 2 {
+// CHECK: scf.yield %[[VAL_B_1]], %[[VAL_B_2]], %[[VAL_B_3]], %[[VAL_B_4]]
+// CHECK: default {
+// CHECK: scf.yield %[[VAL_C_1]], %[[VAL_C_2]], %[[VAL_C_3]], %[[VAL_C_4]]
+
+// CHECK: return %[[RES]]#0, %[[RES]]#1, %[[RES]]#2, %[[RES]]#3 :
+// CHECK-SAME: memref<?xindex>, memref<?xindex>, memref<?xf32>, !sparse_tensor.storage_specifier
+
+func.func @index_switch(%pred: index, %a: tensor<5xf32, #SparseVector>,
+ %b: tensor<5xf32, #SparseVector>,
+ %c: tensor<5xf32, #SparseVector>) -> tensor<5xf32, #SparseVector> {
+ %0 = scf.index_switch %pred -> tensor<5xf32, #SparseVector>
+ case 1 {
+ scf.yield %a : tensor<5xf32, #SparseVector>
+ }
+ case 2 {
+ scf.yield %b : tensor<5xf32, #SparseVector>
+ }
+ default {
+ scf.yield %c : tensor<5xf32, #SparseVector>
+ }
+
+ return %0 : tensor<5xf32, #SparseVector>
+}
|
@llvm/pr-subscribers-mlir-sparse Author: Artemy Skrebkov (ArtemySkrebkov) ChangesIn a downstream project, there is a need for a type conversion pattern for scf.index_switch operation. A test is added into Full diff: https://github.com/llvm/llvm-project/pull/160344.diff 2 Files Affected:
diff --git a/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp b/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
index b0c781c7aff11..c9ff7885f3a49 100644
--- a/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
+++ b/mlir/lib/Dialect/SCF/Transforms/StructuralTypeConversions.cpp
@@ -185,6 +185,31 @@ class ConvertWhileOpTypes
};
} // namespace
+namespace {
+class ConvertIndexSwitchOpTypes
+ : public Structural1ToNConversionPattern<IndexSwitchOp,
+ ConvertIndexSwitchOpTypes> {
+public:
+ using Structural1ToNConversionPattern::Structural1ToNConversionPattern;
+
+ std::optional<IndexSwitchOp>
+ convertSourceOp(IndexSwitchOp op, OneToNOpAdaptor adaptor,
+ ConversionPatternRewriter &rewriter,
+ TypeRange dstTypes) const {
+ auto newOp = rewriter.create<IndexSwitchOp>(
+ op.getLoc(), dstTypes, op.getArg(), op.getCases(), op.getNumCases());
+
+ for (unsigned i = 0u; i < op.getNumRegions(); i++) {
+ if (failed(rewriter.convertRegionTypes(&op.getRegion(i), *typeConverter)))
+ return std::nullopt;
+ auto &dstRegion = newOp.getRegion(i);
+ rewriter.inlineRegionBefore(op.getRegion(i), dstRegion, dstRegion.end());
+ }
+ return newOp;
+ }
+};
+} // namespace
+
namespace {
// When the result types of a ForOp/IfOp get changed, the operand types of the
// corresponding yield op need to be changed. In order to trigger the
@@ -220,18 +245,19 @@ void mlir::scf::populateSCFStructuralTypeConversions(
const TypeConverter &typeConverter, RewritePatternSet &patterns,
PatternBenefit benefit) {
patterns.add<ConvertForOpTypes, ConvertIfOpTypes, ConvertYieldOpTypes,
- ConvertWhileOpTypes, ConvertConditionOpTypes>(
- typeConverter, patterns.getContext(), benefit);
+ ConvertWhileOpTypes, ConvertConditionOpTypes,
+ ConvertIndexSwitchOpTypes>(typeConverter, patterns.getContext(),
+ benefit);
}
void mlir::scf::populateSCFStructuralTypeConversionTarget(
const TypeConverter &typeConverter, ConversionTarget &target) {
- target.addDynamicallyLegalOp<ForOp, IfOp>(
+ target.addDynamicallyLegalOp<ForOp, IfOp, IndexSwitchOp>(
[&](Operation *op) { return typeConverter.isLegal(op->getResults()); });
target.addDynamicallyLegalOp<scf::YieldOp>([&](scf::YieldOp op) {
// We only have conversions for a subset of ops that use scf.yield
// terminators.
- if (!isa<ForOp, IfOp, WhileOp>(op->getParentOp()))
+ if (!isa<ForOp, IfOp, WhileOp, IndexSwitchOp>(op->getParentOp()))
return true;
return typeConverter.isLegal(op.getOperands());
});
diff --git a/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir b/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
index f5d6a08b7de31..00f13ed7c8149 100644
--- a/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
+++ b/mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
@@ -86,3 +86,46 @@ func.func @while(%arg0: tensor<1024xf32, #SparseVector>, %c: i1) -> tensor<1024x
}
return %0: tensor<1024xf32, #SparseVector>
}
+
+// CHECK-LABEL: func.func @index_switch(
+// CHECK-SAME: %[[PRED:.*0]]: index,
+// CHECK-SAME: %[[VAL_A_1:.*1]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_A_2:.*2]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_A_3:.*3]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_A_4:.*4]]: !sparse_tensor.storage_specifier
+// CHECK-SAME: %[[VAL_B_1:.*5]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_B_2:.*6]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_B_3:.*7]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_B_4:.*8]]: !sparse_tensor.storage_specifier
+// CHECK-SAME: %[[VAL_C_1:.*9]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_C_2:.*10]]: memref<?xindex>,
+// CHECK-SAME: %[[VAL_C_3:.*11]]: memref<?xf32>,
+// CHECK-SAME: %[[VAL_C_4:.*12]]: !sparse_tensor.storage_specifier
+
+// CHECK: %[[RES:.*]]:4 = scf.index_switch %[[PRED]]
+// CHECK: case 1 {
+// CHECK: scf.yield %[[VAL_A_1]], %[[VAL_A_2]], %[[VAL_A_3]], %[[VAL_A_4]]
+// CHECK: case 2 {
+// CHECK: scf.yield %[[VAL_B_1]], %[[VAL_B_2]], %[[VAL_B_3]], %[[VAL_B_4]]
+// CHECK: default {
+// CHECK: scf.yield %[[VAL_C_1]], %[[VAL_C_2]], %[[VAL_C_3]], %[[VAL_C_4]]
+
+// CHECK: return %[[RES]]#0, %[[RES]]#1, %[[RES]]#2, %[[RES]]#3 :
+// CHECK-SAME: memref<?xindex>, memref<?xindex>, memref<?xf32>, !sparse_tensor.storage_specifier
+
+func.func @index_switch(%pred: index, %a: tensor<5xf32, #SparseVector>,
+ %b: tensor<5xf32, #SparseVector>,
+ %c: tensor<5xf32, #SparseVector>) -> tensor<5xf32, #SparseVector> {
+ %0 = scf.index_switch %pred -> tensor<5xf32, #SparseVector>
+ case 1 {
+ scf.yield %a : tensor<5xf32, #SparseVector>
+ }
+ case 2 {
+ scf.yield %b : tensor<5xf32, #SparseVector>
+ }
+ default {
+ scf.yield %c : tensor<5xf32, #SparseVector>
+ }
+
+ return %0 : tensor<5xf32, #SparseVector>
+}
|
@Hardcode84 @matthias-springer I've seen your contributions to SCF dialect and infrastructure related to type conversion. This PR seems to fall to those categories, so can you please help reviewing the PR? |
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.
The fact scf structural type conversion only tested in SparseTensor tests and don't have a dedicated test (at least I wasn't able to find one) is not ideal, but I think that's out of scope for this PR.
LGTM.
Yeah. as far as I see it used to be tested also by https://github.com/llvm/llvm-project/blob/main/mlir/test/Dialect/SCF/bufferize.mlir before #113840 was merged, since then it is tested only by SparseTensor tests. @matthias-springer can you please have a look and help merging the PR if you are okay with the changes? Thanks. |
@ArtemySkrebkov Congratulations on having your first Pull Request (PR) merged into the LLVM Project! Your changes will be combined with recent changes from other authors, then tested by our build bots. If there is a problem with a build, you may receive a report in an email or a comment on this PR. Please check whether problems have been caused by your change specifically, as the builds can include changes from many authors. It is not uncommon for your change to be included in a build that fails due to someone else's changes, or infrastructure issues. How to do this, and the rest of the post-merge process, is covered in detail here. If your change does cause a problem, it may be reverted, or you can revert it yourself. This is a normal part of LLVM development. You can fix your changes and open a new PR to merge them again. If you don't get any reports, no action is required from you. Your changes are working as expected, well done! |
…eConversionsAndLegality (llvm#160344) In a downstream project, there is a need for a type conversion pattern for scf.index_switch operation. A test is added into `mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir` (not sure this functionality is really required for sparse tensors, but the test showcase that the new conversion pattern is functional)
In a downstream project, there is a need for a type conversion pattern for scf.index_switch operation. A test is added into
mlir/test/Dialect/SparseTensor/scf_1_N_conversion.mlir
(not sure this functionality is really required for sparse tensors, but the test showcase that the new conversion pattern is functional)