[llvm] [Analysis][AArch64] Add cost model for loop.dependence.{war/raw}.mask (PR #167551)

Benjamin Maxwell via llvm-commits llvm-commits at lists.llvm.org
Thu Dec 11 07:05:44 PST 2025


================
@@ -2139,6 +2139,53 @@ class BasicTTIImplBase : public TargetTransformInfoImplCRTPBase<T> {
       // Otherwise, fallback to default scalarization cost.
       break;
     }
+    case Intrinsic::loop_dependence_war_mask:
+    case Intrinsic::loop_dependence_raw_mask: {
+      // Compute the cost of the expanded version of these intrinsics:
+      //
+      // The possible expansions are...
+      //
+      // loop_dependence_war_mask:
+      //   diff = (ptrB - ptrA) / eltSize
+      //   cmp = icmp sle diff, 0
+      //   upper_bound = select cmp, -1, diff
+      //   mask = get_active_lane_mask lane_offset, upper_bound
+      //
+      // loop_dependence_raw_mask:
+      //   diff = (abs(ptrB - ptrA)) / eltSize
+      //   cmp = icmp eq diff, 0
+      //   upper_bound = select cmp, -1, diff
+      //   mask = get_active_lane_mask lane_offset, upper_bound
+      //
+      InstructionCost Cost = 0;
+      Type *PtrTy = ICA.getArgTypes()[0];
+      unsigned EltSizeInBytes =
+          cast<ConstantInt>(ICA.getArgs()[2])->getZExtValue();
+      bool IsReadAfterWrite = IID == Intrinsic::loop_dependence_raw_mask;
+
+      Cost +=
+          thisT()->getArithmeticInstrCost(Instruction::Sub, PtrTy, CostKind);
+      if (IsReadAfterWrite) {
+        IntrinsicCostAttributes AbsAttrs(Intrinsic::abs, PtrTy, {PtrTy}, {});
+        Cost += thisT()->getIntrinsicInstrCost(AbsAttrs, CostKind);
+      }
+      Cost += thisT()->getArithmeticInstrCost(
+          isPowerOf2_32(EltSizeInBytes) ? Instruction::AShr : Instruction::SDiv,
----------------
MacDue wrote:

Looks like `getArithmeticInstrCost` already handles that (it's a little higher cost than just an `AShr` as there's a few extra instructions to handle the sign correctly). 

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


More information about the llvm-commits mailing list