Skip to content
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

[LangRef] Update the semantic of experimental.get.vector.length #104475

Merged
merged 4 commits into from
Aug 27, 2024

Conversation

mshockwave
Copy link
Member

The previous semantics of llvm.experimental.get.vector.length was too permissive such that it gave optimizers a hard time on anything related to the number of iterations of VP-vectorized loops.

This patch tries to address this by assigning it a set of stricter semantics similar to that of RVV's VSETVLI instructions, while being not too RISC-V specific and leaving room for other (future) targets.

The previous semantics of `llvm.experimental.get.vector.length` was too
permissive such that it gave optimizers a hard time on anything related
to the number of iterations of VP-vectorized loops.

This patch tries to address this by assigning it a set of stricter
semantics similar to that of RVV's VSETVLI instructions, while being not
too RISC-V specific and leaving room for other (future) targets.

Co-authored-by: Craig Topper <[email protected]>
@llvmbot
Copy link
Member

llvmbot commented Aug 15, 2024

@llvm/pr-subscribers-llvm-ir

Author: Min-Yih Hsu (mshockwave)

Changes

The previous semantics of llvm.experimental.get.vector.length was too permissive such that it gave optimizers a hard time on anything related to the number of iterations of VP-vectorized loops.

This patch tries to address this by assigning it a set of stricter semantics similar to that of RVV's VSETVLI instructions, while being not too RISC-V specific and leaving room for other (future) targets.


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

1 Files Affected:

  • (modified) llvm/docs/LangRef.rst (+13-7)
diff --git a/llvm/docs/LangRef.rst b/llvm/docs/LangRef.rst
index 5e5e9b9e8a93b1..22fd805a4685b1 100644
--- a/llvm/docs/LangRef.rst
+++ b/llvm/docs/LangRef.rst
@@ -19644,13 +19644,19 @@ in order to get the number of elements to process on each loop iteration. The
 result should be used to decrease the count for the next iteration until the
 count reaches zero.
 
-If the count is larger than the number of lanes in the type described by the
-last 2 arguments, this intrinsic may return a value less than the number of
-lanes implied by the type. The result will be at least as large as the result
-will be on any later loop iteration.
-
-This intrinsic will only return 0 if the input count is also 0. A non-zero input
-count will produce a non-zero result.
+Let ``%max_lanes`` be the number of lanes in the type described by ``%vf`` and
+``%scalable``, here are the constraints on the returned value:
+- If ``%cnt`` equals to 0, returns 0.
+- The returned value is always less or equal to ``%max_lanes``.
+- The returned value is always larger or equal to
+  ``ceil(%cnt / ceil(%cnt / %max_lanes))``.
+  - This implies that if ``%cnt`` is non-zero, the result should be non-zero
+    as well.
+  - This also implies that if ``%cnt`` is less than ``%max_lanes``, it has to
+    return ``%cnt``.
+- The returned values decrease monotonically in each loop iteration. That is,
+  the returned value of a iteration is at least as large as that of any later
+  iterations.
 
 '``llvm.experimental.vector.partial.reduce.add.*``' Intrinsic
 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Copy link
Collaborator

@topperc topperc left a comment

Choose a reason for hiding this comment

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

LGTM. but since I defined the equation, wait for someone else to approve too.

Let ``%max_lanes`` be the number of lanes in the type described by ``%vf`` and
``%scalable``, here are the constraints on the returned value:
- If ``%cnt`` equals to 0, returns 0.
- The returned value is always less or equal to ``%max_lanes``.
Copy link
Contributor

Choose a reason for hiding this comment

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

I'd probably say "less than or equal to"

Copy link
Member Author

Choose a reason for hiding this comment

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

It's fixed now.

llvm/docs/LangRef.rst Show resolved Hide resolved
llvm/docs/LangRef.rst Show resolved Hide resolved
llvm/docs/LangRef.rst Show resolved Hide resolved
llvm/docs/LangRef.rst Show resolved Hide resolved
count will produce a non-zero result.
- If ``%cnt`` equals to 0, returns 0.
- The returned value is always less than or equal to ``%max_lanes``.
- The returned value is always greater than or equal to ``ceil(%cnt / ceil(%cnt / %max_lanes))``.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- The returned value is always greater than or equal to ``ceil(%cnt / ceil(%cnt / %max_lanes))``.
- The returned value is always greater than or equal to ``ceil(%cnt / ceil(%cnt / %max_lanes))``, if ``%cnt`` is non-zero.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

- If ``%cnt`` equals to 0, returns 0.
- The returned value is always less than or equal to ``%max_lanes``.
- The returned value is always greater than or equal to ``ceil(%cnt / ceil(%cnt / %max_lanes))``.
- The returned values decrease monotonically in each loop iteration. That is,
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- The returned values decrease monotonically in each loop iteration. That is,
- The returned values are monotonically non-increasing in each loop iteration. That is,

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

- The returned value is always less than or equal to ``%max_lanes``.
- The returned value is always greater than or equal to ``ceil(%cnt / ceil(%cnt / %max_lanes))``.
- The returned values decrease monotonically in each loop iteration. That is,
the returned value of a iteration is at least as large as that of any later
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
the returned value of a iteration is at least as large as that of any later
the returned value of an iteration is at least as large as that of any later

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

- The returned value is always greater than or equal to ``ceil(%cnt / ceil(%cnt / %max_lanes))``.
- The returned values decrease monotonically in each loop iteration. That is,
the returned value of a iteration is at least as large as that of any later
iterations.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
iterations.
iteration.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.


Note that it has the following implications:

- If ``%cnt`` is non-zero, the result should be non-zero as well.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- If ``%cnt`` is non-zero, the result should be non-zero as well.
- If ``%cnt`` is non-zero, the return value is non-zero as well.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

Note that it has the following implications:

- If ``%cnt`` is non-zero, the result should be non-zero as well.
- If ``%cnt`` is less than ``%max_lanes``, it has to return ``%cnt``.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- If ``%cnt`` is less than ``%max_lanes``, it has to return ``%cnt``.
- If ``%cnt`` is less than or equal to ``%max_lanes``, the return value is equal to ``%cnt``.

Copy link
Member Author

Choose a reason for hiding this comment

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

Fixed.

iterations.

Note that it has the following implications:

Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
- The number of iterations is equal to ``ceil(%cnt / %max_lanes)``.

right?

Copy link
Member Author

Choose a reason for hiding this comment

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

That's correct, though since the %cnt value is decreased in every iteration, I phrase it slightly differently now.

Copy link
Contributor

@frasercrmck frasercrmck left a comment

Choose a reason for hiding this comment

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

LGTM

@mshockwave mshockwave merged commit e806370 into llvm:main Aug 27, 2024
7 of 9 checks passed
@mshockwave mshockwave deleted the patch/langref-get-vector-length branch August 27, 2024 16:38
5c4lar pushed a commit to 5c4lar/llvm-project that referenced this pull request Aug 29, 2024
…vm#104475)

The previous semantics of `llvm.experimental.get.vector.length` was too
permissive such that it gave optimizers a hard time on anything related
to the number of iterations of VP-vectorized loops.

This patch tries to address this by assigning it a set of stricter
semantics similar to that of RVV's VSETVLI instructions, while being not
too RISC-V specific and leaving room for other (future) targets.

---------

Co-authored-by: Craig Topper <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants