[llvm] [IR][RFC] Add @llvm.mask.beforefirst intrinsic (PR #203874)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Mon Jun 15 06:27:41 PDT 2026
lukel97 wrote:
> Code Modelling: I think we are long past the point where cost modelling implemented as "the sum of the cost of each instruction" is fit for purpose.
Can you elaborate on this a bit more? My understanding was that with the advancements in VPlan we're now getting representations much closer to the underlying IR which in turn makes costing per individual recipe much more accurate.
But to give a sense for the cost modeling aspect on RISC-V, because it doesn't have a get.active.lane.mask instruction a loop operating on `<vscale x 8 x i8>` will need to generate two `<vscale x 8 x i64>` vectors (one for the step vector, one to splat the result of cttz.elts). So the mask is 32 times more expensive than what it actually should be:
```
Cost of 32 for VF vscale x 16: EMIT vp<%uncountable.exit.mask> = active lane mask ir<0>, vp<%6>, ir<1>
```
> Code Generation: This sounds fixable by either moving get-active-lane-mask expansion into operation legalisation so you have the option to DAGCombine the sequence beforehand, or match the sequence earlier as a target specific instcombine or codegen-prepare?
We could, but then on RISC-V we'll likely have to add a RISC-V specific RISCVISD::VMSBF node anyway, and reimplement the same combine AArch64 does. I think there's an opportunity to share this across targets and avoid duplicating the logic.
> I'm not specifically against adding intrinsics like this but I thought the current precedent is to only add things to the IR that are strictly required. I think you need one of the area leads to confirm or deny this because if the rational holds here then I suspect there are many more idioms we could add or extent intrinsics for. For example, when designing the partial reductions I specifically ignored signedness, which would have made the cost modelling much simpler, because that like here could be modelled using existing IR.
I agree that not everything needs an intrinsic, but I thought this was a complex enough pattern where having a dedicated canonical intrinsic is easier than jumping through hoops trying to cost model it accurately in the loop vectorizer or recombine it in the backend.
I think we can also lower this to `blsmsk` on X86, so it would be nice to avoid having to write the same combine three times across three different backends.
>From what I understand the max/min intrinsics are similar in this regard in that whilst you can represent them with an icmp + select sequence, the intrinsic is an easier canonical form. Similarly for get.active.lane.mask which can be represented with a llvm.stepvector + icmp.
https://github.com/llvm/llvm-project/pull/203874
More information about the llvm-commits
mailing list