<div dir="ltr"><div class="gmail_quote"><div dir="ltr">On Thu, Jan 17, 2019 at 3:34 PM Francisco Jerez <<a href="mailto:currojerez@riseup.net">currojerez@riseup.net</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">Jason Ekstrand <<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>> writes:<br>
<br>
> Bah... previous e-mail unfinished.  Please ignore.<br>
><br>
> On Thu, Jan 17, 2019 at 4:15 AM Francisco Jerez <<a href="mailto:currojerez@riseup.net" target="_blank">currojerez@riseup.net</a>><br>
> wrote:<br>
><br>
>> Jason Ekstrand <<a href="mailto:jason@jlekstrand.net" target="_blank">jason@jlekstrand.net</a>> writes:<br>
>><br>
>> > The pass was discovered to cause problems with the MUL+MACH combination<br>
>> > we emit for nir_[iu]mul_high.  In an experimental branch of mine, I ran<br>
>> > into issues where the MUL+MACH ended up using a strided source due to<br>
>> > working on half of a uint64_t and the new lowering pass helpfully tried<br>
>> > to fix the multiply which wrote to an unstriated accumulator.<br>
>><br>
>> > Not only did the multiply not need to be fixed<br>
>><br>
>> That's far from clear, and inconsistent with what this patch is doing,<br>
>> since the fix is still being applied (Wouldn't it make sense to clarify<br>
>> that in the commit message since it's slightly misleading about it?).<br>
>><br>
>> The original instruction was technically violating the first CHV/BXT<br>
>> double-precision regioning restriction before the pass was introduced,<br>
>> that's why it made any changes in the first place.  The integer<br>
>> multiplication lowering code was just lucky enough that violating the<br>
>> restriction didn't matter in this case, but I doubt that the reason for<br>
>> that had anything to do with the accumulator being the explicit<br>
>> destination...<br>
>><br>
><br>
> Explicit, no, but I do suspect that does have to do with it being the<br>
> accumulator.  This restriction isn't theoretical; if you violate it<br>
> with a GRF, you will get data corruption; I've seen it myself.<br>
<br>
The BSpec language is vague and frequently inconsistent.  Obviously it<br>
was being violated before because the text doesn't name the accumulator<br>
as an exemption from that rule.  The fact that you've seen it blow up<br>
with corruption before doesn't guarantee it will always blow up under<br>
the conditions stated on the hardware spec (because those conditions are<br>
a highly imperfect abstraction of the hardware logic rather than the<br>
hardware logic itself).  It's because the restriction (as it's<br>
enunciated in the BSpec) was purely theoretical that the MULH<br>
implementation worked in the first place.<br>
<br>
> I could see two possible explanations:<br>
><br>
>  1. Under the hood the accumulator is written with a Q type and an internal<br>
> stride of 8 bytes, hence the restriction does apply but is implicitly<br>
> satisfied for D type source strides of 1 and 2.<br>
>  2. The data path to the accumulator is a special case in the hardware and<br>
> doesn't use the normal general-purpose regioning logic and so doesn't<br>
> require the restriction.<br>
><br>
<br>
I don't see any evidence for any of these explanations.  I believe that<br>
the actual reason why the MULH implementation didn't suffer the effects<br>
of violating these restrictions is that in fact they don't apply to<br>
*any* 32x16-bit integer multiply operations at all despite what the<br>
hardware spec says, whether the destination is the accumulator or not.<br>
<br>
I've verified it by doing a daily CI run on the following patch:<br>
<br>
<a href="https://cgit.freedesktop.org/~currojerez/mesa/commit/?h=jenkins&id=c1a32c4e1e53d70b0c8f6254f0f53f0230b7e21b" rel="noreferrer" target="_blank">https://cgit.freedesktop.org/~currojerez/mesa/commit/?h=jenkins&id=c1a32c4e1e53d70b0c8f6254f0f53f0230b7e21b</a><br>
<br>
It disables legalization of the integer multiply instruction and then<br>
adds a hack to lower_integer_multiplication() for it to intentionally<br>
break the alignment rule.  No regressions on CHV/BXT/GLK.  My reading of<br>
the simulator confirms that 32x16-bit multiplication isn't affected by<br>
the restriction.<br></blockquote><div><br></div><div>That explanation makes sense especially when combined with the fact that DxD -> Q and DxD -> D was added on gen8 and DxW -> D or DxW -> acc0 has been around for a long time.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
I'm tempted to send a patch that disables regioning alignment lowering<br>
for 32x16-bit integer multiplication strictly for performance.  But<br>
that's really an orthogonal change to this patch, since due to the issue<br>
of precision loss we still need to make sure not to touch accumulator<br>
destinations in instructions that *do* have this restriction.<br></blockquote><div><br></div><div>I just searched through the fs code and implementing MULH is the only use of the accumulator in the scalar back-end.  Given that it explicitly only does a DxW multiply, changing the lowering pass to only apply to DxD multiplies would guarantee correct use of the accumulator.  If we think this is the real reason, then I'd be a fan of such an approach.<br></div><div><br></div><div>As a side-note, we really should add a NIR opcode for DxD -> Q multiply and use that when lowering 64-bit multiplicatoin.<br></div><div><br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> I don't find the first one very convincing at all.  Among other things, if<br>
> it were the true reason, it would imply that we would need to use a stride<br>
> of exactly 2 on D type sources which but empirical evidence suggests that<br>
> "mul(8) acc0<1> g5<8,8,1>UD g9<16,8,2>UW" works just fine.<br>
><br>
><br>
>> > but the "fix" ended up breaking it because a MOV to the accumulator is<br>
>> > not the same as using it as a multiply destination due to the magic<br>
>> > way the 33/64 bits of the<br>
>><br>
>> Technically it has 66 bits (it wasn't a typo when I said that to you<br>
>> earlier on IRC).  That's how it can t hold the result of a SIMD16<br>
>> 16x16-bit integer multiplication with 33-bit signed precision per scalar<br>
>> component.<br>
>><br>
><br>
> Yes, there are 33 bits available for WxW multiplies but this is dealing<br>
> with a DxD multiply which only has 64 bits according to this bit of bspec<br>
> text:<br>
><br>
> As there are only 64 bits per channel in DWord mode (D and UD), it is<br>
> sufficient to store the multiplication result of two DWord operands as long<br>
> as the post source modified sources are still within 32 bits. If any one<br>
> source is type UD and is negated, the negated result becomes 33 bits. The<br>
> DWord multiplication result is then 65 bits, bigger than the storage<br>
> capacity of accumulators. Consequently, the results are unpredictable.<br>
><br>
><br>
<br>
Yes, I came across that text a thousand times yesterday while I was<br>
helping you debug this issue.  I'm aware that MUL/MACH perform a 64-bit<br>
precision computation that leads to the update of the 66 bits per<br>
component of the accumulator (if you don't believe me dump the<br>
accumulator in FULSIM after doing a multiplication operation that gives<br>
a negative result, and count digits).  One thing is not in conflict with<br>
the other.<br>
<br>
>> > accumulator are handled for different instruction types.<br>
>> ><br>
>> > Fixes: efa4e4bc5fc "intel/fs: Introduce regioning lowering pass"<br>
>> > Cc: Francisco Jerez <<a href="mailto:currojerez@riseup.net" target="_blank">currojerez@riseup.net</a>><br>
>> > ---<br>
>> >  src/intel/compiler/brw_fs_lower_regioning.cpp | 16 +++++++++++++++-<br>
>> >  1 file changed, 15 insertions(+), 1 deletion(-)<br>
>> ><br>
>> > diff --git a/src/intel/compiler/brw_fs_lower_regioning.cpp<br>
>> b/src/intel/compiler/brw_fs_lower_regioning.cpp<br>
>> > index cc4163b4c2c..b8a89e82272 100644<br>
>> > --- a/src/intel/compiler/brw_fs_lower_regioning.cpp<br>
>> > +++ b/src/intel/compiler/brw_fs_lower_regioning.cpp<br>
>> > @@ -53,7 +53,13 @@ namespace {<br>
>> >     unsigned<br>
>> >     required_dst_byte_stride(const fs_inst *inst)<br>
>> >     {<br>
>> > -      if (type_sz(inst->dst.type) < get_exec_type_size(inst) &&<br>
>> > +      if (inst->dst.is_accumulator()) {<br>
>> > +         /* Even though it's not explicitly documented in the PRMs or<br>
>> the<br>
>> > +          * BSpec, writes to the accumulator appear to not need any<br>
>> special<br>
>> > +          * treatment with respect too their destination stride<br>
>> alignment.<br>
>> > +          */<br>
>><br>
>> The code is not really doing what the comment says.  The<br>
>> destination/source stride alignment restriction will still be honored<br>
>> for this instruction.  It's just that the destination *has* to be left<br>
>> untouched while doing that in the case of an integer MUL/MACH<br>
>> instruction (that's the only reason I asked you to return the original<br>
>> byte stride of the destination), because splitting off the region into a<br>
>> MOV would lead to data loss due to the inconsistent semantics of the<br>
>> accumulator destination for integer MUL/MACH (which update the whole 66<br>
>> bits) and every other integer arithmetic instruction (which update the<br>
>> bottom 33 bits and *apparently* leave the top 33 bits uninitialized) --<br>
>> IOW this is only here so that the assert below doesn't fire.<br>
>><br>
><br>
> Ok, now I'm very confused.  It sounds to me like you still think it's<br>
> broken but it's less broken than a MOV to acc0 so we should just go with<br>
> it?<br>
<br>
I don't see where I said such a thing.<br></blockquote><div><br></div><div>I think I am now properly parsing what you said above.  It's not that the new thing is broken, it's that it moves the workaround from destination to source.  That's not at all obvious from modifying that function.  In any case, I get what's happening now.<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
> That's very disconcerting....  If there's more investigation you'd<br>
> like to see done, I'm willing to poke at it a bit more and see if we can<br>
> get a better understanding.<br>
><br>
><br>
>> > +         return inst->dst.stride * type_sz(inst->dst.type);<br>
>> > +      } else if (type_sz(inst->dst.type) < get_exec_type_size(inst) &&<br>
>><br>
>> The code changes themselves are just as I wished, so this gets my:<br>
>><br>
>> Reviewed-by: Francisco Jerez <<a href="mailto:currojerez@riseup.net" target="_blank">currojerez@riseup.net</a>><br>
>><br>
>> assuming that you clarify the commit message and comment above.<br>
>><br>
><br>
> I agree that the commit message above was a bit terse but it's entirely<br>
> unclear what it *should* say.  How about something like this:<br>
><br>
> BEGIN COMMITMSG<br>
> In some shaders, you can end up with a stride in the source of a<br>
> SHADER_OPCODE_MULH.  One way this can happen is if the MULH is acting on<br>
> the top bits of a 64-bit value due to 64-bit integer lowering.  In this<br>
> case, the compiler will produce something like this:<br>
><br>
> mul(8)          acc0<1>UD       g5<8,4,2>UD     0x0004UW        { align1 1Q<br>
> };<br>
> mach(8)         g6<1>UD         g5<8,4,2>UD     0x00000004UD    { align1 1Q<br>
> AccWrEnable };<br>
><br>
> The new region fixup pass looks at the MUL and sees a strided source and<br>
> unstrided destination and determines that the sequence is illegal.  It then<br>
> attempts to fix the illegal stride by replacing the destination of the MUL<br>
> with a temporary and emitting a MOV into the accumulator:<br>
><br>
> mul(8)          g9<2>UD         g5<8,4,2>UD     0x0004UW        { align1 1Q<br>
> };<br>
> mov(8)          acc0<1>UD       g9<8,4,2>UD                     { align1 1Q<br>
> };<br>
> mach(8)         g6<1>UD         g5<8,4,2>UD     0x00000004UD    { align1 1Q<br>
> AccWrEnable };<br>
><br>
> Unfortunately, this new sequence isn't correct because MOV accesses the<br>
> accumulator with a different precision to MUL and, instead of filling the<br>
> bottom 32 bits with the source and zeroing the top 32 bits, it leaves the<br>
> top 32 (or maybe 31) bits alone and full of garbage.  When the MACH comes<br>
> along and tries to complete the multiplication, the result is correct in<br>
> the bottom 32 bits (which we throw away) and garbage in the top 32 bits<br>
> which are actually returned by MACH.<br>
><br>
> This commit does two things:  First, it adds an assert to ensure that we<br>
> don't try to rewrite accumulator destinations of MUL instructions so we can<br>
> avoid this precision issue.  Second, it modifies required_dst_byte_stride<br>
> to require a tightly packed stride so that we fix up the sources instead<br>
> and the actual code which gets emitted is this:<br>
><br>
> mov(8)          g9<1>UD         g5<8,4,2>UD                     { align1 1Q<br>
> };<br>
> mul(8)          acc0<1>UD       g9<8,8,1>UD     0x0004UW        { align1 1Q<br>
> };<br>
> mach(8)         g6<1>UD         g5<8,4,2>UD     0x00000004UD    { align1 1Q<br>
> AccWrEnable };<br>
> END COMMITMSG<br>
><br>
> Is that a better commit message?<br>
<br>
Seems somewhat more accurate to me.  Please fix the comment too in<br>
addition.<br></blockquote><div><br></div><div>Ok, I'll send out a v2.  We can fix the bug now and decide on whether to apply it to DxW or not later.</div><div><br></div><div>--Jason<br></div><div> </div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
><br>
> You may be saying to yourself, "See, the working code has an unstrided<br>
> source because we fix the source; the restriction still applies"  Well... I<br>
> tried that too...  I modified the fixup pass to just completely ignore the<br>
> restriction if it has an accumulator destination and it emits the following:<br>
><br>
> mul(8)          acc0<1>UD       g5<8,4,2>UD     0x0004UW        { align1 1Q<br>
> };<br>
> mach(8)         g6<1>UD         g5<8,4,2>UD     0x00000004UD    { align1 1Q<br>
> AccWrEnable };<br>
><br>
> which works perfectly well.  I suspect that the source strides still have<br>
> to match but I really don't think the hardware cares about the destination<br>
> stride when it's an accumulator.<br>
><br>
<br>
See above for the reason why that works.  It doesn't matter whether the<br>
destination is the accumulator or not.<br>
<br>
> --Jason<br>
><br>
><br>
>> >            !is_byte_raw_mov(inst)) {<br>
>> >           return get_exec_type_size(inst);<br>
>> >        } else {<br>
>> > @@ -316,6 +322,14 @@ namespace {<br>
>> >     bool<br>
>> >     lower_dst_region(fs_visitor *v, bblock_t *block, fs_inst *inst)<br>
>> >     {<br>
>> > +      /* We cannot replace the result of an integer multiply which<br>
>> writes the<br>
>> > +       * accumulator because MUL+MACH pairs act on the accumulator as a<br>
>> 64-bit<br>
>> > +       * value whereas the MOV will act on only 32 or 33 bits of the<br>
>> > +       * accumulator.<br>
>> > +       */<br>
>> > +      assert(inst->opcode != BRW_OPCODE_MUL ||<br>
>> !inst->dst.is_accumulator() ||<br>
>> > +             brw_reg_type_is_floating_point(inst->dst.type));<br>
>> > +<br>
>> >        const fs_builder ibld(v, block, inst);<br>
>> >        const unsigned stride = required_dst_byte_stride(inst) /<br>
>> >                                type_sz(inst->dst.type);<br>
>> > --<br>
>> > 2.20.1<br>
>><br>
</blockquote></div></div>