[llvm-branch-commits] [llvm] [LangRef] Make VP intrinsics with EVL > num elements non-UB (PR #240774)
Luke Lau via llvm-branch-commits
llvm-branch-commits at lists.llvm.org
Tue Nov 10 02:00:46 PST 2026
https://github.com/lukel97 created https://github.com/llvm/llvm-project/pull/240774
For the remaining VP intrinsics which don't access memory:
- vp.merge
- vp.reduce.*
- vp.cttz.elts
- vp.splice
- vp.reverse
These are marked as speculatable. However the LangRef states that if the EVL > the number of elements in the vector then the behaviour is undefined. The two are inconsistent, so this PR tightens the result in this scenario to be poison.
As a result the vp.reverse expansion needs to be updated to clamp the EVL so it doesn't read outside of the stack object.
vp.udiv/vp.sdiv/vp.urem/vp.srem are also included here even though they're not speculatable. They can still cause UB if division by zero or overflow occurs on any lane even when EVL > number of elts, so I've added lines to clarify this.
VP intrinsics that read/write memory need to remain UB to match how backends lower them, they just use the EVL verbatim without any clamping or checks.
---
<sub>Stack created with <a href="https://github.com/github/gh-stack">GitHub Stacks CLI</a> • <a href="https://gh.io/stacks-feedback">Give Feedback 💬</a></sub>
>From 40a48a5fbb720812fa31d26e4eaf2e498610a487 Mon Sep 17 00:00:00 2001
From: Luke Lau <luke at igalia.com>
Date: Tue, 10 Nov 2026 16:41:03 +0800
Subject: [PATCH] [LangRef] Make VP intrinsics with EVL > num elements non-UB
For the remaining VP intrinsics which don't access memory:
- vp.merge
- vp.reduce.*
- vp.cttz.elts
- vp.splice
- vp.reverse
These are marked as speculatable. However the LangRef states that if the EVL > the number of elements in the vector then the behaviour is undefined. The two are inconsistent, so this PR tightens the result in this scenario to be poison.
As a result the vp.reverse expansion needs to be updated to clamp the EVL so it doesn't read outside of the stack object.
vp.udiv/vp.sdiv/vp.urem/vp.srem are also included here even though they're not speculatable. They can still cause UB if division by zero or overflow occurs on any lane even when EVL > number of elts, so I've added lines to clarify this.
VP intrinsics that read/write memory need to remain UB to match how backends lower them, they just use the EVL verbatim without any clamping or checks.
---
llvm/docs/LangRef.md | 33 ++++++++++++++-----
.../SelectionDAG/LegalizeVectorTypes.cpp | 4 +++
llvm/lib/IR/IntrinsicInst.cpp | 8 ++---
llvm/test/CodeGen/RISCV/rvv/vp-reverse-int.ll | 11 +++++--
.../test/CodeGen/RISCV/rvv/vp-reverse-mask.ll | 15 ++++++---
5 files changed, 50 insertions(+), 21 deletions(-)
diff --git a/llvm/docs/LangRef.md b/llvm/docs/LangRef.md
index 8e208f72ee0564..ec762e4f0ffa8c 100644
--- a/llvm/docs/LangRef.md
+++ b/llvm/docs/LangRef.md
@@ -22546,10 +22546,13 @@ the range:
Note that for {ref}`scalable vector types <t_vector>` `W` is the runtime
length of the vector.
-The VP intrinsic has undefined behavior if `%evl > W`. The explicit vector
-length (%evl) creates a mask, %EVLmask, with all elements `0 <= i < %evl` set
-to True, and all other lanes `%evl <= i < W` to False. A new mask %M is
-calculated with an element-wise AND from %mask and %EVLmask:
+If `%evl > W`, VP intrinsics which access memory have undefined
+behavior. For all other VP intrinsics the result is poison.
+
+The explicit vector length (%evl) creates a mask, %EVLmask, with all
+elements `0 <= i < %evl` set to True, and all other lanes `%evl <= i <
+W` to False. A new mask %M is calculated with an element-wise AND
+from %mask and %EVLmask:
```
M = %mask AND %EVLmask
@@ -22558,8 +22561,8 @@ M = %mask AND %EVLmask
A vector operation `<opcode>` on vectors `A` and `B` calculates:
```
-A <opcode> B = { A[i] <opcode> B[i] M[i] = True, and
- { undef otherwise
+A <opcode> B = { A[i] <opcode> B[i] M[i] = True
+ { poison M[i] = False or poison
```
#### Optimization Hint
@@ -22653,6 +22656,9 @@ The '`llvm.vp.sdiv`' intrinsic performs signed division ({ref}`sdiv <i_sdiv>`)
of the first and second vector arguments on each enabled or poison lane. The
result on disabled and poison lanes is a {ref}`poison value <poisonvalues>`.
+Undefined behavior can still occur on enabled or poison lanes when `%evl` is
+greater than the number of vector elements.
+
##### Examples:
```llvm
@@ -22695,6 +22701,9 @@ The '`llvm.vp.udiv`' intrinsic performs unsigned division
or poison lane. The result on disabled and poison lanes is a
{ref}`poison value <poisonvalues>`.
+Undefined behavior can still occur on enabled or poison lanes when `%evl` is
+greater than the number of vector elements.
+
##### Examples:
```llvm
@@ -22737,6 +22746,9 @@ The '`llvm.vp.srem`' intrinsic computes the remainder of the signed division
or poison lane. The result on disabled and poison lanes is a
{ref}`poison value <poisonvalues>`.
+Undefined behavior can still occur on enabled or poison lanes when `%evl` is
+greater than the number of vector elements.
+
##### Examples:
```llvm
@@ -22779,6 +22791,9 @@ The '`llvm.vp.urem`' intrinsic computes the remainder of the unsigned division
or poison lane. The result on disabled and poison lanes is a
{ref}`poison value <poisonvalues>`.
+Undefined behavior can still occur on enabled or poison lanes when `%evl` is
+greater than the number of vector elements.
+
##### Examples:
```llvm
@@ -23614,7 +23629,7 @@ and `evl2` are unsigned integers indicating the explicit vector lengths of
`vec1` and `vec2` respectively. `imm`, `evl1` and `evl2` should
respect the following constraints: `-evl1 <= imm < evl1`, `0 <= evl1 <= VL`
and `0 <= evl2 <= VL`, where `VL` is the runtime vector factor. If these
-constraints are not satisfied the intrinsic has undefined behavior.
+constraints are not satisfied the result is poison.
##### Semantics:
@@ -23622,13 +23637,13 @@ Effectively, this intrinsic concatenates `vec1[0..evl1-1]` and
`vec2[0..evl2-1]` and creates the result vector by selecting the elements in a
window of size `evl2`, starting at index `imm` (for a positive immediate) of
the concatenated vector. Elements in the result vector beyond `evl2` are
-`undef`. If `imm` is negative the starting index is `evl1 + imm`. The result
+`poison`. If `imm` is negative the starting index is `evl1 + imm`. The result
vector of active vector length `evl2` contains `evl1 - imm` (`-imm` for
negative `imm`) elements from indices `[imm..evl1 - 1]`
(`[evl1 + imm..evl1 -1]` for negative `imm`) of `vec1` followed by the
first `evl2 - (evl1 - imm)` (`evl2 + imm` for negative `imm`) elements of
`vec2`. If `evl1 - imm` (`-imm`) >= `evl2`, only the first `evl2`
-elements are considered and the remaining are `undef`. The lanes in the result
+elements are considered and the remaining are `poison`. The lanes in the result
vector disabled by `mask` are `poison`.
##### Examples:
diff --git a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
index 4e56e25e9364db..0bab9ad48938b2 100644
--- a/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
+++ b/llvm/lib/CodeGen/SelectionDAG/LegalizeVectorTypes.cpp
@@ -3735,6 +3735,10 @@ void DAGTypeLegalizer::SplitVecRes_VP_REVERSE(SDNode *N, SDValue &Lo,
PtrInfo, MachineMemOperand::MOLoad, LocationSize::beforeOrAfterPointer(),
Alignment);
+ EVL = DAG.getNode(
+ ISD::UMIN, DL, EVL.getValueType(), EVL,
+ DAG.getElementCount(DL, EVL.getValueType(), VT.getVectorElementCount()));
+
unsigned EltWidth = VT.getScalarSizeInBits() / 8;
SDValue NumElemMinus1 =
DAG.getNode(ISD::SUB, DL, PtrVT, DAG.getZExtOrTrunc(EVL, DL, PtrVT),
diff --git a/llvm/lib/IR/IntrinsicInst.cpp b/llvm/lib/IR/IntrinsicInst.cpp
index 882d786241945b..299780403003d7 100644
--- a/llvm/lib/IR/IntrinsicInst.cpp
+++ b/llvm/lib/IR/IntrinsicInst.cpp
@@ -425,10 +425,10 @@ bool VPIntrinsic::canIgnoreVectorLengthParam() const {
if (!VLParam)
return true;
- // Note that the VP intrinsic causes undefined behavior if the Explicit Vector
- // Length parameter is strictly greater-than the number of vector elements of
- // the operation. This function returns true when this is detected statically
- // in the IR.
+ // Note that if the Explicit Vector Length parameter is strictly greater-than
+ // the number of vector elements of the operation, the result is poison (or
+ // the behavior is undefined for VP intrinsics that access memory). This
+ // function returns true when this is detected statically in the IR.
// Check whether "W == vscale * EC.getKnownMinValue()"
if (EC.isScalable()) {
diff --git a/llvm/test/CodeGen/RISCV/rvv/vp-reverse-int.ll b/llvm/test/CodeGen/RISCV/rvv/vp-reverse-int.ll
index 6c01efa36a851c..34bc2852f88404 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vp-reverse-int.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/vp-reverse-int.ll
@@ -679,12 +679,17 @@ define <vscale x 128 x i8> @test_vp_reverse_nxv128i8(<vscale x 128 x i8> %src, i
; CHECK-LABEL: test_vp_reverse_nxv128i8:
; CHECK: # %bb.0:
; CHECK-NEXT: csrr a1, vlenb
+; CHECK-NEXT: slli a2, a1, 4
+; CHECK-NEXT: bltu a0, a2, .LBB44_2
+; CHECK-NEXT: # %bb.1:
+; CHECK-NEXT: mv a0, a2
+; CHECK-NEXT: .LBB44_2:
; CHECK-NEXT: slli a1, a1, 3
; CHECK-NEXT: mv a2, a0
-; CHECK-NEXT: bltu a0, a1, .LBB44_2
-; CHECK-NEXT: # %bb.1:
+; CHECK-NEXT: bltu a0, a1, .LBB44_4
+; CHECK-NEXT: # %bb.3:
; CHECK-NEXT: mv a2, a1
-; CHECK-NEXT: .LBB44_2:
+; CHECK-NEXT: .LBB44_4:
; CHECK-NEXT: addi sp, sp, -80
; CHECK-NEXT: .cfi_def_cfa_offset 80
; CHECK-NEXT: sd ra, 72(sp) # 8-byte Folded Spill
diff --git a/llvm/test/CodeGen/RISCV/rvv/vp-reverse-mask.ll b/llvm/test/CodeGen/RISCV/rvv/vp-reverse-mask.ll
index a2e1ba486e0725..c5b5d7d1930fc5 100644
--- a/llvm/test/CodeGen/RISCV/rvv/vp-reverse-mask.ll
+++ b/llvm/test/CodeGen/RISCV/rvv/vp-reverse-mask.ll
@@ -302,15 +302,19 @@ define <vscale x 128 x i1> @test_vp_reverse_nxv128i1(<vscale x 128 x i1> %src, i
; CHECK: # %bb.0:
; CHECK-NEXT: vsetvli a1, zero, e8, m8, ta, ma
; CHECK-NEXT: vmv.v.i v16, 0
-; CHECK-NEXT: vmerge.vim v24, v16, 1, v0
-; CHECK-NEXT: vmv1r.v v0, v8
; CHECK-NEXT: csrr a2, vlenb
+; CHECK-NEXT: slli a1, a2, 4
+; CHECK-NEXT: bltu a0, a1, .LBB14_2
+; CHECK-NEXT: # %bb.1:
+; CHECK-NEXT: mv a0, a1
+; CHECK-NEXT: .LBB14_2:
+; CHECK-NEXT: vmerge.vim v24, v16, 1, v0
; CHECK-NEXT: slli a2, a2, 3
; CHECK-NEXT: mv a1, a0
-; CHECK-NEXT: bltu a0, a2, .LBB14_2
-; CHECK-NEXT: # %bb.1:
+; CHECK-NEXT: bltu a0, a2, .LBB14_4
+; CHECK-NEXT: # %bb.3:
; CHECK-NEXT: mv a1, a2
-; CHECK-NEXT: .LBB14_2:
+; CHECK-NEXT: .LBB14_4:
; CHECK-NEXT: addi sp, sp, -80
; CHECK-NEXT: .cfi_def_cfa_offset 80
; CHECK-NEXT: sd ra, 72(sp) # 8-byte Folded Spill
@@ -323,6 +327,7 @@ define <vscale x 128 x i1> @test_vp_reverse_nxv128i1(<vscale x 128 x i1> %src, i
; CHECK-NEXT: slli a3, a3, 4
; CHECK-NEXT: sub sp, sp, a3
; CHECK-NEXT: andi sp, sp, -64
+; CHECK-NEXT: vmv1r.v v0, v8
; CHECK-NEXT: addi a3, sp, 64
; CHECK-NEXT: add a4, a0, a3
; CHECK-NEXT: li a5, -1
More information about the llvm-branch-commits
mailing list