[llvm] [LV] Convert scatter w/uniform addr and mask being header mask to scalar store. (PR #172799)

Elvis Wang via llvm-commits llvm-commits at lists.llvm.org
Mon May 11 21:55:38 PDT 2026


================
@@ -948,6 +925,60 @@ Value *VPInstruction::generate(VPTransformState &State) {
   }
 }
 
+InstructionCost VPRecipeWithIRFlags::getCostForRecipeWithOpcodeAndTypes(
+    unsigned Opcode, Type *ValTy, Instruction *I, ElementCount VF,
+    VPCostContext &Ctx) {
+  switch (Opcode) {
+  case VPInstruction::LastActiveLane: {
+    // LastActiveLane computes the index of the last active lane in a
+    // predicate mask: NOT + cttz_elts + SUB.
+    if (VF.isScalar())
+      return Ctx.TTI.getCmpSelInstrCost(Instruction::ICmp, ValTy,
+                                        CmpInst::makeCmpResultType(ValTy),
+                                        CmpInst::ICMP_EQ, Ctx.CostKind);
+    auto *PredTy = toVectorTy(ValTy, VF);
+    IntrinsicCostAttributes Attrs(Intrinsic::experimental_cttz_elts,
+                                  Type::getInt64Ty(Ctx.LLVMCtx),
+                                  {PredTy, Type::getInt1Ty(Ctx.LLVMCtx)});
+    InstructionCost Cost = Ctx.TTI.getIntrinsicInstrCost(Attrs, Ctx.CostKind);
+    // Add cost of NOT operation on the predicate.
+    Cost += Ctx.TTI.getArithmeticInstrCost(
+        Instruction::Xor, PredTy, Ctx.CostKind,
+        {TargetTransformInfo::OK_AnyValue, TargetTransformInfo::OP_None},
+        {TargetTransformInfo::OK_UniformConstantValue,
+         TargetTransformInfo::OP_None});
+    // Add cost of SUB operation on the index.
+    Cost += Ctx.TTI.getArithmeticInstrCost(
+        Instruction::Sub, Type::getInt64Ty(Ctx.LLVMCtx), Ctx.CostKind);
+    return Cost;
+  }
+  case VPInstruction::ExtractLane: {
+    // Compute the cost of ExtractLane for a vector with the given scalar
+    // element type. ExtractLane extracts an element at a runtime-determined
+    // index.
+    if (VF.isScalar())
+      return 0;
+    auto *VecTy = toVectorTy(ValTy, VF);
+    return Ctx.TTI.getVectorInstrCost(Instruction::ExtractElement, VecTy,
+                                      Ctx.CostKind);
+  }
+  case Instruction::Store: {
+    assert(I && "Querying cost for store must have underlying instruction.");
+    unsigned AS = getLoadStoreAddressSpace(I);
+    TTI::OperandValueInfo OpInfo = TTI::getOperandInfo(I->getOperand(0));
+    return Ctx.TTI.getMemoryOpCost(Opcode, ValTy, getLoadStoreAlignment(I), AS,
+                                   Ctx.CostKind, OpInfo, I);
+  }
+  case Instruction::Sub: {
+    if (!VF.isScalar())
+      ValTy = toVectorTy(ValTy, VF);
----------------
ElvisWang123 wrote:

Updated, thanks!

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


More information about the llvm-commits mailing list