<div dir="ltr"><div>Ahmed,</div><div><br></div><div> Yes, this is the case, I'm sure many other 'spots' in DAGCombiner use this same check or use a similar check with LegalOperations. It just seems like bad form to have core code that generates an illegal node that legalization cannot seem to handle, unless I'm missing something, which is entirely possible. Potentially we are using the wrong LegalAction, though each I've tried breaks at different points so I don't think that's it.</div><div><br></div><div> Yes, it is breaking during the legalize phase, depending on which TargetLowering callback method we use. For example, Custom will let it through to instructions selection, which it breaks at the that phase, otherwise I believe it breaks during legalization. If we use Expand instead, the assert during Legalize is: "EXTLOAD should always be supported". I don't really understand that message :)</div><div><br></div><div> The pre-legalization canonicalization makes sense to me but either 1) legalize should be able to handle everything or 2) nodes that aren't going to be legal anyways should not be produced (though this obviously contradicts the term pre-legalize).</div><div><br></div><div>  The SelectionDAGBuilder does not generate 8 bit loads nor 8 bit exloads.</div><div><br></div><div> There are other places we could fix this issue, accepting 8 bit extloads and then expanding it back further down the pipe, but this does seem a bit hacky.</div><div><br></div><div>Thanks.</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Wed, Mar 4, 2015 at 12:53 PM, Ahmed Bougacha <span dir="ltr"><<a href="mailto:ahmed.bougacha@gmail.com" target="_blank">ahmed.bougacha@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">+Chandler, Hal, Owen, who - among others - know much more than I do.<br>
<span><br>
On Tue, Mar 3, 2015 at 11:57 AM, Ryan Taylor <<a href="mailto:ryta1203@gmail.com">ryta1203@gmail.com</a>> wrote:<br>
> 1) It's crashing because LD1 is produced due to LegalOperations=false in<br>
> pre-legalize pass. Then Legalization does not know how to handle it so it<br>
> asserts on a default case.<br>
<br>
</span>Yes, and where, and on what, does the assert fire?  8-bit load<br>
legalization I assume?<br>
<span><br>
> I don't know if it's a reasonable expectation or<br>
> not but we do not have support for it. I have not tried overriding<br>
> shouldReduceLoadWidth.<br>
><br>
> 2) I see, that makes sense to some degree, I'm curious if you can provide an<br>
> example? It doesn't seem good to generate something pre-legalize<br>
> (target-independent) that you can't then handle when you find that it's<br>
> illegal in the very next step that is legalization.<br>
<br>
</span>I believe the general idea is one of separation of concerns:  the way<br>
I see it, the DAGCombiner's job is to canonicalize so that other<br>
(perhaps target-specific) transformations only need to handle the<br>
canonical representation.  Usually, that means optimizing code:  stuff<br>
like e.g. (sub x, x) shouldn't be special-cased in each target (at<br>
least not on ISD nodes ;))<br>
The legalizer (or rather, the various legalizers) later does its own<br>
job, legalizing, after which all code must be "legal".  The further<br>
rounds of DAGCombining just maintain that invariant.<br>
<br>
So really, this is a legalization problem:  for instance, is it not<br>
possible for the SelectionDAGBuilder to generate 8-bit loads as well?<br>
In this specific case, I'd rather we avoid the problem altogether, and<br>
would be fine with not doing this kind of transform if the resulting<br>
loads is illegal (and one might argue for the same in all the other<br>
"!LegalOperations" combines).<br>
<br>
But the actual solution seems to be with the load legalization:  I<br>
wouldn't be surprised if many other spots assumed 8-bit loads are<br>
legal (as you noticed after disabling ReduceLoadWidth?), and this is<br>
your real problem.<br>
<br>
Take this with a grain of salt though, I only ever look at<br>
bog-standard X86-ish targets.  The cc'd people might have a better<br>
argument.<br>
<span class="HOEnZb"><font color="#888888"><br>
-Ahmed<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
> I'm guessing, from what<br>
> you and others have said, that it's just expected that every machine will<br>
> support 8-bit extloads and if not then the target should handle the undoing<br>
> of the target-independent generated 'optimization' via LegalizeDAG and<br>
> TargetLowering callback methods?<br>
><br>
> 3) I have not tried running it on any other target, sorry, I suppose I<br>
> should but I didn't want to take the time if I'm not following the right<br>
> path here.<br>
><br>
> Thanks.<br>
><br>
> We'd love to hear what the community thinks would be the 'best' solution. We<br>
> are trying not to change any 'core' code.<br>
><br>
><br>
><br>
> On Tue, Mar 3, 2015 at 2:08 PM, Ahmed Bougacha <<a href="mailto:ahmed.bougacha@gmail.com">ahmed.bougacha@gmail.com</a>><br>
> wrote:<br>
>><br>
>> On Tue, Mar 3, 2015 at 10:35 AM, Ryan Taylor <<a href="mailto:ryta1203@gmail.com">ryta1203@gmail.com</a>> wrote:<br>
>> > I'm curious about this code in ReduceLoadWidth (and in DAGCombiner in<br>
>> > general):<br>
>> ><br>
>> > if (LegalOperations && !TLI.isLoadExtLegal(ExtType, ExtVT))<br>
>> >    return SDValue();<br>
>> ><br>
>> > LegalOperations is false for the first pre-legalize pass and true for<br>
>> > the<br>
>> > post-legalize pass. The first pass is target-independent yes? So that<br>
>> > makes<br>
>> > sense.<br>
>> ><br>
>> > The issue we are having is this: we don't support 8 bit loads and we<br>
>> > don't<br>
>> > support 8 bit extloads, so we end up with LD1 with zext after either the<br>
>> > first pass or the second pass (depending on the test case). If we add<br>
>> > the<br>
>> > TargetLowering callback method setLoadExtAction(ISD::ZEXTLOAD, MVT::i8,<br>
>> > Expand) then it crashes during legalization<br>
>><br>
>> This part is surprising.  What happens?  This seems to me like the<br>
>> correct solution.<br>
>><br>
>> I'm guessing it's because there's no good way to legalize 8 bit loads?<br>
>>  I have no idea what happens when those are illegal, as I'd expect<br>
>> them to be always available?<br>
>><br>
>><br>
>> Here's a cheap hack though: I see that ReduceLoadWidth is predicated on<br>
>> the<br>
>> TLI "shouldReduceLoadWidth" hook.  Did you try overriding that to<br>
>> avoid creating 8 bit loads?<br>
>><br>
>> > and if we don't have that in<br>
>> > then it crashes during instruction selection.<br>
>> ><br>
>> > There are two ways to fix this:<br>
>> ><br>
>> > 1) Add the setLoadExtAction AND comment out 'LegalOperations &&' in the<br>
>> > conditional. (this solves the problem)<br>
>> ><br>
>> > 2) Create a custom expand to undo the optimization added by<br>
>> > ReduceLoadWidth.<br>
>> ><br>
>> > The 2nd approach seems more in line with what LLVM infrastructure wants<br>
>> > but<br>
>> > it seems silly to have to undo an optimization?<br>
>> ><br>
>> > Essentially, we have some bit packing structures and the code is trying<br>
>> > to<br>
>> > get the upper bits. The initial dag generates an LD2 with srl (which<br>
>> > makes<br>
>> > sense, it's what we want). The DAGCombiner then goes in and changes that<br>
>> > LD2<br>
>> > with srl to an LD1 zextload, which we don't support.<br>
>> ><br>
>> > Why is LegalOperations really needed here? What is the purpose and point<br>
>> > of<br>
>> > this? It seems you could eliminate this and be all the better for it.<br>
>><br>
>> FWIW I somewhat agree, and believe this is a common "problem":  we<br>
>> eagerly generate obviously-illegal nodes, because we're before<br>
>> legalisation, so it's OK, right?  Except, it's sometimes hard to<br>
>> recover from.  Most of the time, it's a good thing, precisely because<br>
>> it catches patterns that would be disturbed by legalization.<br>
>><br>
>> Did you try running the integration tests - at least X86 - after<br>
>> changing the condition?<br>
>><br>
>> -Ahmed<br>
>><br>
>> > Thanks.<br>
>> ><br>
>> > _______________________________________________<br>
>> > LLVM Developers mailing list<br>
>> > <a href="mailto:LLVMdev@cs.uiuc.edu">LLVMdev@cs.uiuc.edu</a>         <a href="http://llvm.cs.uiuc.edu" target="_blank">http://llvm.cs.uiuc.edu</a><br>
>> > <a href="http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev" target="_blank">http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev</a><br>
>> ><br>
><br>
><br>
</div></div></blockquote></div><br></div>