[llvm] [LV][EVL] Support cast instruction with EVL-vectorization (PR #108351)

Mel Chen via llvm-commits llvm-commits at lists.llvm.org
Mon Sep 23 02:36:15 PDT 2024


================
@@ -1381,16 +1384,73 @@ void VPWidenCastRecipe::execute(VPTransformState &State) {
   }
 }
 
+static bool isCastInstruction(unsigned Opcode) {
+  switch (Opcode) {
+  case Instruction::SExt:
+  case Instruction::ZExt:
+  case Instruction::Trunc:
+  case Instruction::FPExt:
+  case Instruction::FPTrunc:
+  case Instruction::FPToSI:
+  case Instruction::FPToUI:
+  case Instruction::SIToFP:
+  case Instruction::UIToFP:
+  case Instruction::PtrToInt:
+  case Instruction::IntToPtr:
+    return true;
+  default:
+    return false;
+  }
+}
+
+void VPWidenCastEVLRecipe::execute(VPTransformState &State) {
+  unsigned Opcode = getOpcode();
+  State.setDebugLocFrom(getDebugLoc());
+  assert(State.UF == 1 && "Expected only UF == 1 when vectorizing with "
+                          "explicit vector length.");
+
+  if (isCastInstruction(Opcode)) {
+    Value *SrcVal = State.get(getOperand(0), 0);
+    VectorType *DsType = VectorType::get(getResultType(), State.VF);
+
+    IRBuilderBase &BuilderIR = State.Builder;
+    VectorBuilder Builder(BuilderIR);
+    Value *Mask = BuilderIR.CreateVectorSplat(State.VF, BuilderIR.getTrue());
+    Builder.setMask(Mask).setEVL(State.get(getEVL(), 0, /*NeedsScalar=*/true));
+
+    Value *VPInst =
+        Builder.createVectorInstruction(Opcode, DsType, {SrcVal}, "vp.cast");
+
+    if (VPInst) {
+      if (auto *VecOp = dyn_cast<CastInst>(VPInst))
+        VecOp->copyIRFlags(getUnderlyingInstr());
+    }
+
+    State.set(this, VPInst, 0);
+    State.addMetadata(VPInst,
+                      dyn_cast_or_null<Instruction>(getUnderlyingValue()));
+  }
+}
+
 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
 void VPWidenCastRecipe::print(raw_ostream &O, const Twine &Indent,
                               VPSlotTracker &SlotTracker) const {
   O << Indent << "WIDEN-CAST ";
   printAsOperand(O, SlotTracker);
-  O << " = " << Instruction::getOpcodeName(Opcode) << " ";
+  O << " = " << Instruction::getOpcodeName(Opcode);
----------------
Mel-Chen wrote:

Un-related change?

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


More information about the llvm-commits mailing list