No, this is not RVV wanted. There is the example that how RVV uses length and mask: for (int i = 0; i < n; i++) if (cond[i] == 0) a[i] = b[i] + c[i]; The gimple IR should be this: AVL = #number of element to be updated. TVL = #number of total element to be updated vect_0 = vector of cond[i] vect_1 = vector of a[i] vect_2 = vector of b[i] vect_3 = vector of c[i] .... body: AVL = WHILE_LEN (TVL,.....) This is new pattern I add for RVV to calculate the active vector length, the pattern assume AVL <= TVL (always). mask_0 = vect_0 == {0,0,0,....} comparison to generate mask for predicate. vect_1 = len_cond_len (mask0, vect_1, vect_2, vect_3, AVL) This is also the new pattern, When predicate (mask bit = 1 && elment index < AVL) is 1, update vect_1 = vect_2 + vect_3. Otherwise vect_1 = vect_1) ...... TVL = TVL - AVL (decrease the counter, until it becomes 0 to exit the loop) if (TVL ==0) exit loop ...... juzhe.zhong@rivai.ai From: Richard Sandiford Date: 2022-09-29 17:24 To: Richard Biener via Gcc-patches CC: Andrew Stubbs; Richard Biener; juzhe.zhong Subject: Re: [PATCH] vect: while_ult for integer mask Richard Biener via Gcc-patches writes: > On Wed, Sep 28, 2022 at 5:06 PM Andrew Stubbs wrote: >> >> This patch is a prerequisite for some amdgcn patches I'm working on to >> support shorter vector lengths (having fixed 64 lanes tends to miss >> optimizations, and masking is not supported everywhere yet). >> >> The problem is that, unlike AArch64, I'm not using different mask modes >> for different sized vectors, so all loops end up using the while_ultsidi >> pattern, regardless of vector length. In theory I could use SImode for >> V32, HImode for V16, etc., but there's no mode to fit V4 or V2 so >> something else is needed. Moving to using vector masks in the backend >> is not a natural fit for GCN, and would be a huge task in any case. >> >> This patch adds an additional length operand so that we can distinguish >> the different uses in the back end and don't end up with more lanes >> enabled than there ought to be. >> >> I've made the extra operand conditional on the mode so that I don't have >> to modify the AArch64 backend; that uses while_ family of >> operators in a lot of places and uses iterators, so it would end up >> touching a lot of code just to add an inactive operand, plus I don't >> have a way to test it properly. I've confirmed that AArch64 builds and >> expands while_ult correctly in a simple example. >> >> OK for mainline? > > Hmm, but you could introduce BI4mode and BI2mode for V4 and V2, no? > Not sure if it is possible to have two partial integer modes and use those. Might be difficult to do cleanly, since BI is very much a special case. But I agree that that would better fit the existing scheme. Otherwise: operand0[0] = operand1 < operand2; for (i = 1; i < operand3; i++) operand0[i] = operand0[i - 1] && (operand1 + i < operand2); looks like a "length and mask" operation, which IIUC is also what RVV wanted? (Wasn't at the Cauldron, so not entirely sure.) Perhaps the difference is that in this case the length must be constant. (Or is that true for RVV as well?) Thanks, Richard