[llvm] [LLVM] Add `llvm.masked.compress` intrinsic (PR #92289)

Lawrence Benson via llvm-commits llvm-commits at lists.llvm.org
Wed May 22 03:45:58 PDT 2024


================
@@ -6279,7 +6279,9 @@ SDValue SelectionDAG::FoldConstantArithmetic(unsigned Opcode, const SDLoc &DL,
   // We can't create a scalar CONCAT_VECTORS so skip it. It will break
   // for concats involving SPLAT_VECTOR. Concats of BUILD_VECTORS are handled by
   // foldCONCAT_VECTORS in getNode before this is called.
-  if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS)
+  // MCOMPRESS is not defined for scalars. We handle constants before this call.
+  if (Opcode >= ISD::BUILTIN_OP_END || Opcode == ISD::CONCAT_VECTORS ||
+      Opcode == ISD::MCOMPRESS)
----------------
lawben wrote:

I had the same thought and did some digging. I think this is somewhat coincidental, as a few things have to happen to run into this case. I also believe there is a bug in, e.g., `VECTOR_DEINTERLAVE` that you compared to.

`FoldConstantArithmetic` on the scalar elements of a vector is only applied when...

1. the node has one or two operands, not more (disqualifies, e.g., `VECTOR_SPLICE`, `INSERT_SUBVECTOR`, as both have a third index operand)
2. all operands are vectors with the same number of elements (disquaifies, e.g., `EXTRACT_SUBVECTOR`, as the index argument is not a vector)
3. the vectors must all be `BUILD_VECTOR`, `VECTOR_SPLAT`, or `UNDEF`.
4. the op code must actually be invalid for scalar types (disqualifies, e.g., all `VECTOR_REDUCE*`)

on top of that, this crash only happens when the ISD op code has `asserts` for `isVector()` in `getNode()`. e.g., `VECTOR_DEINTERLEAVE` does not have this. so technically, we can create an invalid `VECTOR_DEINTERLEAVE` node with two scalars, which sounds like a bug to me. 

```cpp
SDValue test = getNode(ISD::VECTOR_DEINTERLEAVE, DL, MVT::i32, 
                       getConstant(0, DL, MVT::i32), 
                       getConstant(1, DL, MVT::i32));
```

This code happily creates a SelectionDAG node.

```
Creating new node: t24: i32 = vector_deinterleave Constant:i32<0>, Constant:i32<1>
```

Due to the suggestion of @RKSimon and @topperc, I added some asserts to avoid this case and I crash. 

**Option 1:** Allow scalar operands for these vector operations.
**Option 2:** Make `FoldConstantArithmetic` more aware of vector-only functions, and skip them.

What are your thougths on this?

https://github.com/llvm/llvm-project/pull/92289


More information about the llvm-commits mailing list