> -----Original Message-----
> From: Robin Dapp <[email protected]>
> Sent: 09 November 2026 14:16
> To: Tamar Christina <[email protected]>; Richard Biener
> <[email protected]>; Robin Dapp <[email protected]>
> Cc: [email protected]
> Subject: Re: [PATCH 2/3] vect: Add index-based early-break type/kind.
> 
> >> I don't see how it makes sense to specify the value for an empty mask
> >> with _LEN but not with _MASK.  Can we instead add an optional value-at-
> zero
> >> argument (to both) that we'd somehow validate with predicates like we
> >> validate the BIAS for LEN?
> >
> > I honestly don't like re-defining INDEX_FIRST_ACTIVE to return -1.
> > I haven't looked at the patch in detail yet but I think doing so is likely 
> > why
> > All the changes in live operations were needed, because now you have to
> > actually deal with the -1.
> 
> The idea is that the scalar branch after COND_LEN_INDEX_FIRST_ACTIVE is
> the only consumer of -1 (this is all vec_num == 1 for now) and every
> live out just sees the "mismatch index" (!= -1).  That way, we can just
> reuse the index for both the latch as well as the live values.  We don't
> have an "extract first active" so we would need to implement this as
>   idx = COND_LEN_INDEX_FIRST_ACTIVE + VEC_EXTRACT (idx)
> again.  In the end, it would just be one additional vfirst outside of
> the loop (and one live-out vector mask), so not too bad.
> 

That should already be supported for memory, but indeed for registers
You have to deal with the oddity that compares on RISC-V can return
non-zero for inactive lanes.  So you need a LEN operation there.

> > I so I think adding the parameter makes it hard because as I mentioned
> before
> > For non-masked targets like Adv. SIMD this ends up using operations
> undefined
> > at zero.  And if RVV then only implements -1 as value-at-zero it means we
> still
> > need to define two paths in the vectorizer.
> 
> To play devil's advocate :)  Doesn't Adv SIMD's implementation depend on
> clz which has exactly this ambiguity of being unspecified for an empty
> input and thus a separate param?
> 

It doesn't need the extra parameter because clz is never used in GIMPLE where
you have this ambiguity.  The CLZ is used in RTL only and AArch64 has  defined
semantics for CLZ on 0.   But also because it's known not to be called when the
mask is empty the ambiguity of CLZ never comes into effect, which is kinda the
point :)

This and due to the fact that not all targets have a packed representation for
boolean masks is explicitly the reason why this operation is *not* open coded
as CLZ and why COUNT_ACTIVE is not a popcount.

> > I really don't like that.  My suggestion before was to make a new
> IFN_VEC_COMPRESS
> > or something that returns a boolean and leave *_INDEX_FIRST_ACTIVE
> alone.  This
> > means we don't have to split the UNCOND/COND/LEN paths.
> 
> >> > +  gimple_cond_set_condition (cond_stmt, index_based_p ? GE_EXPR :
> >> NE_EXPR,  new_temp, cst);
> 
> > My preference would really be to have an IFN that does not enforce an
> > exact
> > return value, just so we can make a boolean out of it.  I think the patch
> shrinks
> > significantly with that as it's only a local decision.
> 
> Yeah, I see where you're coming from (and understand that we want to
> modify the existing structure as little as possible).  The few changes
> in vectorizable_live... are for the "index" handling (but not because of
> the -1 in particular except for the hunk you quoted).
> 

Yeah, I see it's for handling the compare results in the increment.
I do still find it odd that the ISA allows this, and I also don't see how the
LEN of the consumer can differ from the producer.  But that's the ISA
so we have to deal with it..

> If we absolutely not want to bifurcate here (i.e. introduce an index
> type/kind), I guess I can adapt at the cost of a popcount in the loop, a
> redundant vfirst in the (cold) exit block and a live mask like:
> 

I'm not actually against the enum, but the choices seem to be mutually
exclusive, so it seems like the right decision can be made locally, but in..

>   bla = IFN_OTHER_NAME # would defer to COND_LEN_COUNT_ACTIVE for
> RVV
>   branch bla
>   # exit
>   bla = COND_LEN_INDEX_FIRST_ACTIVE # vfirst
>   live_bla = VEC_EXTRACT (..., bla);
> 

The second vfirst won't be redundant. It'll be removed by CSE in RTL.
That's how it works for Adv. SIMD as well.  Both the compare and the
extract code redo the same computation using addhn, and the one in
the exit is removed because it's the exact same one as in the loop.

So as long as your RTL reflects that the operations are the same (
which they should be as they operate on the same inputs and len) then
the second vfirst should be removed leaving you with the optimal case
no?

So I think sharing that part is pre-mature :)

Kind Regards,
Tamar

> That's still ok, although not as nice as with one vfirst.  And at some
> point, of course, I'd like to optimize this to one.  But the popcount
> solution would still be better than what we have now, so I'd take it.
> 
> --
> Regards
>  Robin

Reply via email to