Skip to content

[mlir] Add support for vector types whose number of elements are from… #68030

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

mshahneo
Copy link
Contributor

@mshahneo mshahneo commented Oct 2, 2023

… a range of values.

Add types and predicates for Vector, Fixed Vector, and Scalable Vector whose number of elements is from a given allowedRanges list. The list has two values, start and end of the range (inclusive).

@mshahneo mshahneo requested a review from kuhar October 2, 2023 20:32
@llvmbot llvmbot added mlir:core MLIR Core Infrastructure mlir mlir:ods labels Oct 2, 2023
@llvmbot
Copy link
Member

llvmbot commented Oct 2, 2023

@llvm/pr-subscribers-mlir-core
@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-ods

Changes

… a range of values.

Add types and predicates for Vector, Fixed Vector, and Scalable Vector whose number of elements is from a given allowedRanges list. The list has two values, start and end of the range (inclusive).


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

1 Files Affected:

  • (modified) mlir/include/mlir/IR/CommonTypeConstraints.td (+70)
diff --git a/mlir/include/mlir/IR/CommonTypeConstraints.td b/mlir/include/mlir/IR/CommonTypeConstraints.td
index 4fc14e30b8a10d0..703122547df7493 100644
--- a/mlir/include/mlir/IR/CommonTypeConstraints.td
+++ b/mlir/include/mlir/IR/CommonTypeConstraints.td
@@ -546,6 +546,76 @@ class ScalableVectorOfRankAndLengthAndType<list<int> allowedRanks,
   ScalableVectorOfLength<allowedLengths>.summary,
   "::mlir::VectorType">;
 
+// Whether the number of elements of a vector is from the given
+// `allowedRanges` list, the list has two values, start and end
+// of the range (inclusive).
+class IsVectorOfLengthRangePred<list<int> allowedRanges>
+  : And<[IsVectorTypePred,
+        And<[CPred<[{$_self.cast<::mlir::VectorType>().getNumElements()>= }] # allowedRanges[0]>,
+            CPred<[{$_self.cast<::mlir::VectorType>().getNumElements() <= }] # allowedRanges[1]>]>]>;
+
+// Whether the number of elements of a fixed-length vector is from the given
+// `allowedRanges` list, the list has two values, start and end of the range (inclusive).
+class IsFixedVectorOfLengthRangePred<list<int> allowedRanges>
+  : And<[IsFixedVectorTypePred,
+        And<[CPred<[{$_self.cast<::mlir::VectorType>().getNumElements() >= }] # allowedRanges[0]>,
+            CPred<[{$_self.cast<::mlir::VectorType>().getNumElements() <= }] # allowedRanges[1]>]>]>;
+
+// Whether the number of elements of a scalable vector is from the given
+// `allowedRanges` list, the list has two values, start and end of the range (inclusive).
+class IsScalableVectorOfLengthRangePred<list<int> allowedRanges>
+  : And<[IsScalableVectorTypePred,
+        And<[CPred<[{$_self.cast<::mlir::VectorType>().getNumElements() >= }] # allowedRanges[0]>,
+            CPred<[{$_self.cast<::mlir::VectorType>().getNumElements() <= }] # allowedRanges[1]>]>]>;
+
+// Any vector where the number of elements is from the given
+// `allowedRanges` list.
+class VectorOfLengthRange<list<int> allowedRanges>
+  : Type<IsVectorOfLengthRangePred<allowedRanges>,
+    " of length " # !interleave(allowedRanges, "-"),
+    "::mlir::VectorType">;
+
+// Any fixed-length vector where the number of elements is from the given
+// `allowedRanges` list.
+class FixedVectorOfLengthRange<list<int> allowedRanges>
+  : Type<IsFixedVectorOfLengthRangePred<allowedRanges>,
+    " of length " # !interleave(allowedRanges, "-"),
+    "::mlir::VectorType">;
+
+// Any scalable vector where the number of elements is from the given
+// `allowedRanges` list.
+class ScalableVectorOfLengthRange<list<int> allowedRanges>
+  : Type<IsScalableVectorOfLengthRangePred<allowedRanges>,
+    " of length " # !interleave(allowedRanges, "-"),
+    "::mlir::VectorType">;
+
+// Any vector where the number of elements is from the given
+// `allowedRanges` list and the type is from the given `allowedTypes`
+// list.
+class VectorOfLengthRangeAndType<list<int> allowedRanges, list<Type> allowedTypes>
+  : Type<And<[VectorOf<allowedTypes>.predicate, VectorOfLengthRange<allowedRanges>.predicate]>,
+        VectorOf<allowedTypes>.summary # VectorOfLengthRange<allowedRanges>.summary,
+        "::mlir::VectorType">;
+
+// Any fixed-length vector where the number of elements is from the given
+// `allowedRanges` list and the type is from the given `allowedTypes`
+// list.
+class FixedVectorOfLengthRangeAndType<list<int> allowedRanges, list<Type> allowedTypes>
+  : Type<
+      And<[FixedVectorOf<allowedTypes>.predicate, FixedVectorOfLengthRange<allowedRanges>.predicate]>,
+      FixedVectorOf<allowedTypes>.summary # FixedVectorOfLengthRange<allowedRanges>.summary,
+      "::mlir::VectorType">;
+
+// Any scalable vector where the number of elements is from the given
+// `allowedRanges` list and the type is from the given `allowedTypes`
+// list.
+class ScalableVectorOfLengthRangeAndType<list<int> allowedRanges, list<Type> allowedTypes>
+  : Type<
+      And<[ScalableVectorOf<allowedTypes>.predicate, ScalableVectorOfLengthRange<allowedRanges>.predicate]>,
+      ScalableVectorOf<allowedTypes>.summary # ScalableVectorOfLengthRange<allowedRanges>.summary,
+      "::mlir::VectorType">;
+
+
 def AnyVector : VectorOf<[AnyType]>;
 // Temporary vector type clone that allows gradual transition to 0-D vectors.
 def AnyVectorOfAnyRank : VectorOfAnyRankOf<[AnyType]>;

Copy link
Member

@kuhar kuhar left a comment

Choose a reason for hiding this comment

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

Do you expect these to be used outside of the SPIR-V dialect? If not, what do you think about moving them there?

Copy link
Contributor

@banach-space banach-space left a comment

Choose a reason for hiding this comment

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

The new constraints that you are adding here are somewhat similar to what @MacDue adds in #68586. You may want to compare against that.

Separately, what about tests?

@MacDue
Copy link
Member

MacDue commented Oct 12, 2023

The new constraints that you are adding here are somewhat similar to what @MacDue adds in #68586. You may want to compare against that.

Since these are using the total number of elements (rather than a specific dim), these don't overlap (in functionality).

@mshahneo
Copy link
Contributor Author

The new constraints that you are adding here are somewhat similar to what @MacDue adds in #68586. You may want to compare against that.

Since these are using the total number of elements (rather than a specific dim), these don't overlap (in functionality).

Thank you so much, I also thought that reviewing that PR :).

@mshahneo
Copy link
Contributor Author

The new constraints that you are adding here are somewhat similar to what @MacDue adds in #68586. You may want to compare against that.

Separately, what about tests?

About the tests, This PR was part of a larger PR. The next part of this PR (#68034) has the tests that uses this constraint in SPIRV. I couldn't think of a way to add test case just for this one.

@mshahneo
Copy link
Contributor Author

Do you expect these to be used outside of the SPIR-V dialect? If not, what do you think about moving them there?

I do think in some of the dialects this can be used. Hence, put this in common constraints. But in current upstream MLIR, SPIR-V is probably the only user.

… a range of values.

Add types and predicates for Vector, Fixed Vector, and Scalable Vector
whose number of elements is from a given `allowedRanges` list.
The list has two values, start and end of the range (inclusive).
@mshahneo mshahneo force-pushed the PR-vector-of-range-of-elements branch from 8bb290a to 3b06881 Compare October 12, 2023 16:24
@banach-space
Copy link
Contributor

Perhaps I am missing something, but I don't see e.g. ScalableVectorOfMinLengthRangeAndType to be used in your other patch. And in general, does SPIR-V require scalable vectors?

About the tests, This PR was part of a larger PR. The next part of this PR (#68034) has the tests that uses this constraint in SPIRV.

Does the other patch include tests for scalable vectors?

Small PRs (like this one) are definitely preferable, but they should also be self-contained. That's not really the case here though - without tests or other code that would validate/use the new constraints, it's hard to tell whether that's what we need (or, indeed, whether we need it at all).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
mlir:core MLIR Core Infrastructure mlir:ods mlir
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants