-
Notifications
You must be signed in to change notification settings - Fork 839
[Util] Implement InferIntDivisibilityOpInterface for affine ops #22723
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 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
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
68 changes: 68 additions & 0 deletions
68
compiler/src/iree/compiler/Dialect/Util/Transforms/TestIntegerDivisibilityAnalysis.cpp
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 |
|---|---|---|
| @@ -0,0 +1,68 @@ | ||
| // Copyright 2025 The IREE Authors | ||
| // | ||
| // Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
| // See https://llvm.org/LICENSE.txt for license information. | ||
| // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
|
||
| #include "iree/compiler/Dialect/Util/Analysis/IntegerDivisibilityAnalysis.h" | ||
| #include "iree/compiler/Dialect/Util/Transforms/Passes.h" | ||
| #include "mlir/Analysis/DataFlow/DeadCodeAnalysis.h" | ||
| #include "mlir/Analysis/DataFlowFramework.h" | ||
|
|
||
| namespace mlir::iree_compiler::IREE::Util { | ||
|
|
||
| #define GEN_PASS_DEF_TESTINTEGERDIVISIBILITYANALYSISPASS | ||
| #include "iree/compiler/Dialect/Util/Transforms/Passes.h.inc" | ||
|
|
||
| namespace { | ||
|
|
||
| class TestIntegerDivisibilityAnalysisPass | ||
| : public impl::TestIntegerDivisibilityAnalysisPassBase< | ||
| TestIntegerDivisibilityAnalysisPass> { | ||
| public: | ||
| void runOnOperation() override { | ||
| Operation *rootOp = getOperation(); | ||
| MLIRContext *context = &getContext(); | ||
|
|
||
| // The pass is rooted on `iree_unregistered.test_int_divisibility` ops, | ||
| // which are expected to have a single operand for which to annotate | ||
| // divisibility information. | ||
| SmallVector<std::pair<Operation *, Value>> queryOps; | ||
| rootOp->walk([&](Operation *op) { | ||
| if (op->getName().getStringRef() == | ||
| "iree_unregistered.test_int_divisibility" && | ||
| op->getNumOperands() == 1) { | ||
| queryOps.emplace_back(op, op->getOperand(0)); | ||
| } | ||
| }); | ||
|
|
||
| DataFlowSolver solver; | ||
| // DeadCodeAnalysis is the base analysis that allows the solver to traverse | ||
| // control flow. We include it to make the divisibility analysis more | ||
| // powerful. | ||
| solver.load<dataflow::DeadCodeAnalysis>(); | ||
| solver.load<IntegerDivisibilityAnalysis>(); | ||
| if (failed(solver.initializeAndRun(rootOp))) { | ||
| return signalPassFailure(); | ||
| } | ||
|
|
||
| for (auto &[op, value] : queryOps) { | ||
| auto *lattice = solver.lookupState<IntegerDivisibilityLattice>(value); | ||
| if (!lattice || lattice->getValue().isUninitialized()) { | ||
| op->setAttr("divisibility", StringAttr::get(context, "uninitialized")); | ||
| continue; | ||
| } | ||
|
|
||
| // Format for the divisibility information is "udiv = X, sdiv = Y". | ||
| const auto &div = lattice->getValue().getValue(); | ||
| std::string result; | ||
| llvm::raw_string_ostream os(result); | ||
| os << "udiv = " << div.udiv() << ", sdiv = " << div.sdiv(); | ||
| op->setAttr("divisibility", StringAttr::get(context, os.str())); | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| } // namespace | ||
|
|
||
| } // namespace mlir::iree_compiler::IREE::Util |
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
188 changes: 188 additions & 0 deletions
188
...er/src/iree/compiler/Dialect/Util/Transforms/test/test_integer_divisibility_analysis.mlir
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 |
|---|---|---|
| @@ -0,0 +1,188 @@ | ||
| // RUN: iree-opt --split-input-file --iree-util-test-integer-divisibility-analysis --allow-unregistered-dialect %s | FileCheck %s | ||
|
|
||
| // CHECK-LABEL: @affine_apply_mul_divisibility | ||
| util.func @affine_apply_mul_divisibility(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 8> : index | ||
| %1 = affine.apply affine_map<(d0) -> (d0 * 4)>(%0) | ||
| // CHECK: divisibility = "udiv = 32, sdiv = 32" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_mul_negative | ||
| util.func @affine_apply_mul_negative(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 8> : index | ||
| %1 = affine.apply affine_map<(d0) -> (d0 * -4)>(%0) | ||
| // CHECK: divisibility = "udiv = 32, sdiv = 32" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_add_gcd | ||
| util.func @affine_apply_add_gcd(%arg0 : index, %arg1 : index) -> index { | ||
| %0:2 = util.assume.int %arg0<udiv = 16>, | ||
| %arg1<udiv = 24> : index, index | ||
| %1 = affine.apply affine_map<(d0, d1) -> (d0 + d1)>(%0#0, %0#1) | ||
| // CHECK: divisibility = "udiv = 8, sdiv = 8" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_floordiv_exact | ||
| util.func @affine_apply_floordiv_exact(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 64> : index | ||
| %1 = affine.apply affine_map<(d0) -> (d0 floordiv 4)>(%0) | ||
| // CHECK: divisibility = "udiv = 16, sdiv = 16" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_ceildiv_exact | ||
| util.func @affine_apply_ceildiv_exact(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 64> : index | ||
| %1 = affine.apply affine_map<(d0) -> (d0 ceildiv 4)>(%0) | ||
| // CHECK: divisibility = "udiv = 16, sdiv = 16" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_floordiv_non_exact | ||
| util.func @affine_apply_floordiv_non_exact(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 20> : index | ||
| %1 = affine.apply affine_map<(d0) -> (d0 floordiv 3)>(%0) | ||
| // CHECK: divisibility = "udiv = 1, sdiv = 1" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_mod | ||
| util.func @affine_apply_mod(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 16> : index | ||
| %1 = affine.apply affine_map<(d0) -> (d0 mod 16)>(%0) | ||
| // CHECK: divisibility = "udiv = 1, sdiv = 1" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_composition | ||
| util.func @affine_apply_composition(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 8> : index | ||
| %1 = affine.apply affine_map<(d0) -> (d0 * 4 + 16)>(%0) | ||
| // CHECK: divisibility = "udiv = 16, sdiv = 16" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_with_symbol | ||
| util.func @affine_apply_with_symbol(%arg0 : index, %arg1 : index) -> index { | ||
| %0:2 = util.assume.int %arg0<udiv = 16>, | ||
| %arg1<udiv = 16> : index, index | ||
| %1 = affine.apply affine_map<(d0)[s0] -> (d0 + s0)>(%0#0)[%0#1] | ||
| // CHECK: divisibility = "udiv = 16, sdiv = 16" | ||
Max191 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_min_uniform_divisibility | ||
| util.func @affine_min_uniform_divisibility(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 16> : index | ||
| %1 = affine.min affine_map<(d0) -> (d0, d0 + 64)>(%0) | ||
| // CHECK: divisibility = "udiv = 16, sdiv = 16" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_min_different_divisibilities | ||
| util.func @affine_min_different_divisibilities(%arg0 : index, %arg1 : index) -> index { | ||
| %0:2 = util.assume.int %arg0<udiv = 16>, | ||
| %arg1<udiv = 24> : index, index | ||
| %1 = affine.min affine_map<(d0, d1) -> (d0, d1)>(%0#0, %0#1) | ||
| // CHECK: divisibility = "udiv = 8, sdiv = 8" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_max_uniform_divisibility | ||
| util.func @affine_max_uniform_divisibility(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 32> : index | ||
| %1 = affine.max affine_map<(d0) -> (d0, d0 - 64)>(%0) | ||
| // CHECK: divisibility = "udiv = 32, sdiv = 32" | ||
| %2 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| util.return %2 : index | ||
| } | ||
Max191 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_max_different_divisibilities | ||
| util.func @affine_max_different_divisibilities(%arg0 : index, %arg1 : index, %arg2 : index) -> index { | ||
| %0:3 = util.assume.int %arg0<udiv = 12>, | ||
| %arg1<udiv = 24>, | ||
| %arg2<udiv = 18> : index, index, index | ||
| %3 = affine.max affine_map<(d0, d1, d2) -> (d0, d1, d2)>(%0#0, %0#1, %0#2) | ||
| // CHECK: divisibility = "udiv = 6, sdiv = 6" | ||
| %4 = "iree_unregistered.test_int_divisibility"(%3) : (index) -> index | ||
| util.return %4 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_constant | ||
| util.func @affine_apply_constant() -> index { | ||
| %0 = affine.apply affine_map<() -> (64)>() | ||
| // CHECK: divisibility = "udiv = 64, sdiv = 64" | ||
| %1 = "iree_unregistered.test_int_divisibility"(%0) : (index) -> index | ||
| util.return %1 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @affine_apply_chained_operations | ||
| util.func @affine_apply_chained_operations(%arg0 : index) -> index { | ||
| %0 = util.assume.int %arg0<udiv = 4> : index | ||
| %1 = affine.apply affine_map<(d0) -> (d0 * 8)>(%0) | ||
| %2 = affine.apply affine_map<(d0) -> (d0 + 16)>(%1) | ||
| // CHECK: divisibility = "udiv = 16, sdiv = 16" | ||
| %3 = "iree_unregistered.test_int_divisibility"(%2) : (index) -> index | ||
| util.return %3 : index | ||
| } | ||
|
|
||
| // ----- | ||
|
|
||
| // CHECK-LABEL: @complex_chained_affine_ops | ||
| util.func @complex_chained_affine_ops(%arg0 : index, %arg1 : index, %arg2 : index) -> index { | ||
| %0:3 = util.assume.int %arg0<udiv = 210>, | ||
| %arg1<udiv = 7>, | ||
| %arg2<udiv = 15> : index, index, index | ||
| %1 = affine.apply affine_map<(d0, d1) -> (d0 + 2 * d1)>(%0#0, %0#1) | ||
| // CHECK: divisibility = "udiv = 14, sdiv = 14" | ||
| %div_1 = "iree_unregistered.test_int_divisibility"(%1) : (index) -> index | ||
| %2 = affine.max affine_map<(d0, d1) -> (d0 floordiv 6, d1 * 3)>(%0#0, %0#2) | ||
| // CHECK: divisibility = "udiv = 5, sdiv = 5" | ||
| %div_2 = "iree_unregistered.test_int_divisibility"(%2) : (index) -> index | ||
| %3 = affine.min affine_map<(d0)[s0] -> (2 * (s0 * d0 - 14) ceildiv 7, d0 floordiv 3 * 2)>(%2)[%1] | ||
| // CHECK: divisibility = "udiv = 2, sdiv = 2" | ||
| %div_3 = "iree_unregistered.test_int_divisibility"(%3) : (index) -> index | ||
| util.return %div_3 : index | ||
| } | ||
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.