Skip to content

[mlir] IntegerRangeAnalysis: don't loop over splat attr #115229

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 3 commits into from
Nov 7, 2024

Conversation

IanWood1
Copy link
Contributor

@IanWood1 IanWood1 commented Nov 6, 2024

If the DenseIntElementsAttr is a splat value, there is no need to loop over the entire attr. Instead, just update with the splat value.

@IanWood1 IanWood1 changed the title [mlir] IntegerRangeAnalysis: dont loop over splat attr [mlir] IntegerRangeAnalysis: don't loop over splat attr Nov 6, 2024
@IanWood1 IanWood1 marked this pull request as ready for review November 6, 2024 22:45
@llvmbot
Copy link
Member

llvmbot commented Nov 6, 2024

@llvm/pr-subscribers-mlir-arith

@llvm/pr-subscribers-mlir

Author: Ian Wood (IanWood1)

Changes

If the DenseIntElementsAttr is a splat value, there is no need to loop over the entire attr. Instead, just update with the splat value.


Full diff: https://github.com/llvm/llvm-project/pull/115229.diff

1 Files Affected:

  • (modified) mlir/lib/Dialect/Arith/IR/InferIntRangeInterfaceImpls.cpp (+7)
diff --git a/mlir/lib/Dialect/Arith/IR/InferIntRangeInterfaceImpls.cpp b/mlir/lib/Dialect/Arith/IR/InferIntRangeInterfaceImpls.cpp
index 8682294c8a6972..6646c189eb1c3e 100644
--- a/mlir/lib/Dialect/Arith/IR/InferIntRangeInterfaceImpls.cpp
+++ b/mlir/lib/Dialect/Arith/IR/InferIntRangeInterfaceImpls.cpp
@@ -10,6 +10,7 @@
 #include "mlir/Interfaces/InferIntRangeInterface.h"
 #include "mlir/Interfaces/Utils/InferIntRangeCommon.h"
 
+#include "llvm/ADT/STLExtras.h"
 #include "llvm/Support/Debug.h"
 #include <optional>
 
@@ -42,6 +43,12 @@ void arith::ConstantOp::inferResultRanges(ArrayRef<ConstantIntRanges> argRanges,
   }
   if (auto arrayCstAttr =
           llvm::dyn_cast_or_null<DenseIntElementsAttr>(getValue())) {
+    if (arrayCstAttr.isSplat()) {
+      setResultRange(getResult(), ConstantIntRanges::constant(
+                                      arrayCstAttr.getSplatValue<APInt>()));
+      return;
+    }
+
     std::optional<ConstantIntRanges> result;
     for (const APInt &val : arrayCstAttr) {
       auto range = ConstantIntRanges::constant(val);

Copy link
Contributor

@Hardcode84 Hardcode84 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you add some test? LGTM otherwise.

@IanWood1
Copy link
Contributor Author

IanWood1 commented Nov 7, 2024

Can you add some test? LGTM otherwise.

f54cdc5 added tests to check the for the correct handling of splat values and change should really only affect the performance of very large splat constants. So, I'm not sure how this should be tested.

@Hardcode84
Copy link
Contributor

Can you add some test? LGTM otherwise.

f54cdc5 added tests to check the for the correct handling of splat values and change should really only affect the performance of very large splat constants. So, I'm not sure how this should be tested.

Oh, yeah, I forgot we already had functional test for splats ). LGTM.

Copy link
Contributor

@krzysz00 krzysz00 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Approved, since there's already a dense<3> in the vector test suite

@IanWood1 IanWood1 merged commit 3deee23 into llvm:main Nov 7, 2024
8 checks passed
IanWood1 added a commit that referenced this pull request Nov 7, 2024
IanWood1 added a commit that referenced this pull request Nov 7, 2024
Hitting assertion in IREE
https://github.com/iree-org/iree/actions/runs/11732283897/job/32684201665?pr=19066

```
iree-compile: /__w/iree/iree/third_party/llvm-project/mlir/include/mlir/IR/BuiltinAttributes.h:423: auto mlir::DenseElementsAttr::getValues() const [T = llvm::APInt]: Assertion `succeeded(range) && "element type cannot be iterated"' failed.
```


Reverts #115229
@joker-eph
Copy link
Collaborator

change should really only affect the performance of very large splat constants. So, I'm not sure how this should be tested.

In this case we tag the PR with [NFC] (no functional change) to convey this.

IanWood1 added a commit that referenced this pull request Nov 8, 2024
Reland #115229 which was
reverted by #115388 because it
was hitting an assertion in IREE. From the original change: If the
`DenseIntElementsAttr` is a splat value, there is no need to loop over
the entire attr. Instead, just update with the splat value.


The problem with the original implementation is that `SplatElementsAttr`
might be an attr of non `APInt` (e.g. float) elements. Instead, check if
`DenseIntElementsAttr` is splat and use the splat value. Added a test to
ensure there's no crash when handling float attrs.
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
If the `DenseIntElementsAttr` is a splat value, there is no need to loop
over the entire attr. Instead, just update with the splat value.
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
…115388)

Hitting assertion in IREE
https://github.com/iree-org/iree/actions/runs/11732283897/job/32684201665?pr=19066

```
iree-compile: /__w/iree/iree/third_party/llvm-project/mlir/include/mlir/IR/BuiltinAttributes.h:423: auto mlir::DenseElementsAttr::getValues() const [T = llvm::APInt]: Assertion `succeeded(range) && "element type cannot be iterated"' failed.
```


Reverts llvm#115229
Groverkss pushed a commit to iree-org/llvm-project that referenced this pull request Nov 15, 2024
…5399)

Reland llvm#115229 which was
reverted by llvm#115388 because it
was hitting an assertion in IREE. From the original change: If the
`DenseIntElementsAttr` is a splat value, there is no need to loop over
the entire attr. Instead, just update with the splat value.


The problem with the original implementation is that `SplatElementsAttr`
might be an attr of non `APInt` (e.g. float) elements. Instead, check if
`DenseIntElementsAttr` is splat and use the splat value. Added a test to
ensure there's no crash when handling float attrs.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants