[llvm] [NVPTX] Don't use stack memory when bitcasting to/from v2i8 (PR #113928)

Justin Fargnoli via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 29 13:46:13 PDT 2024


================
@@ -2311,6 +2315,47 @@ NVPTXTargetLowering::LowerCONCAT_VECTORS(SDValue Op, SelectionDAG &DAG) const {
   return DAG.getBuildVector(Node->getValueType(0), dl, Ops);
 }
 
+SDValue NVPTXTargetLowering::LowerBITCAST(SDValue Op, SelectionDAG &DAG) const {
+  // Handle bitcasting to/from v2i8 without hitting the default promotion
+  // strategy which goes through stack memory.
+  SDNode *Node = Op.getNode();
+  SDLoc DL(Node);
+
+  auto maybeBitcast = [&](EVT VT, SDValue Value) {
+    if (Value->getValueType(0) == VT)
+      return Value;
+    return DAG.getNode(ISD::BITCAST, DL, VT, Value);
+  };
+
+  EVT ToVT = Op->getValueType(0);
+  EVT FromVT = Op->getOperand(0)->getValueType(0);
+
+  if (ToVT == MVT::v2i8) {
+    // Bitcast to i16 and unpack elements into a vector
+    SDValue AsInt = maybeBitcast(MVT::i16, Op->getOperand(0));
+    SDValue Vec0 = DAG.getNode(ISD::TRUNCATE, DL, MVT::i8, AsInt);
+    SDValue Const8 = DAG.getConstant(8, DL, MVT::i16);
+    SDValue Vec1 =
+        DAG.getNode(ISD::TRUNCATE, DL, MVT::i8,
+                    DAG.getNode(ISD::SRL, DL, MVT::i16, {AsInt, Const8}));
+    return DAG.getNode(ISD::BUILD_VECTOR, DL, MVT::v2i8, {Vec0, Vec1});
----------------
justinfargnoli wrote:

This snippet is only used by the call in `ReplaceNodeResults()`. The `else if` snippet below is only used by the call in `LowerOperation()`. 

Let's split these cases into separate methods: 
- `ReplaceBitcast()` for the `if (ToVT == MVT::v2i8)` snippet.
- `LowerBITCAST()` for the `else if (FromVT == MVT::v2i8)` snippet.

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


More information about the llvm-commits mailing list