<div dir="ltr"><div class="gmail_extra"><div class="gmail_quote">On Thu, Sep 14, 2017 at 12:40 PM, Connor Abbott <span dir="ltr"><<a href="mailto:cwabbott0@gmail.com" target="_blank">cwabbott0@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div class="HOEnZb"><div class="h5">On Tue, Sep 12, 2017 at 2:09 PM, Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> wrote:<br>
> On Tue, Sep 12, 2017 at 10:12 AM, Ian Romanick <<a href="mailto:idr@freedesktop.org">idr@freedesktop.org</a>> wrote:<br>
>><br>
>> On 09/11/2017 11:17 PM, Kenneth Graunke wrote:<br>
>> > On Monday, September 11, 2017 9:23:05 PM PDT Ian Romanick wrote:<br>
>> >> On 09/08/2017 01:59 AM, Kenneth Graunke wrote:<br>
>> >>> On Thursday, September 7, 2017 4:26:04 PM PDT Jordan Justen wrote:<br>
>> >>>> On 2017-09-06 14:12:41, Daniel Schürmann wrote:<br>
>> >>>>> Hello together!<br>
>> >>>>> Recently, we had a small discussion (off the list) about the NIR<br>
>> >>>>> serialization, which was previously discussed in [RFC] ARB_gl_spirv<br>
>> >>>>> and<br>
>> >>>>> NIR backend for radeonsi.<br>
>> >>>>><br>
>> >>>>> As this topic could be interesting to more people, I would like to<br>
>> >>>>> share, what was talked about so far (You might want to read from<br>
>> >>>>> bottom up).<br>
>> >>>>><br>
>> >>>>> TL;DR:<br>
>> >>>>> - NIR serialization is in demand for shader cache<br>
>> >>>>> - could be done either directly (NIR binary form) or via SPIR-V<br>
>> >>>>> - Ian et al. are working on GLSL IR -> SPIR-V transformation, which<br>
>> >>>>> could be adapted for a NIR -> SPIR-V pass<br>
>> >>>>> - in NIR representation, some type information is lost<br>
>> >>>>> - thus, a serialization via SPIR-V could NOT be a glslang<br>
>> >>>>> alternative<br>
>> >>>>> (otoh, the GLSL IR->SPIR-V pass could), but only for spirv-opt (if<br>
>> >>>>> the<br>
>> >>>>> output is valid SPIR-V)<br>
>> >>>><br>
>> >>>> Ian,<br>
>> >>>><br>
>> >>>> Tim was suggesting that we might look at serializing nir for the i965<br>
>> >>>> shader cache. Based on this email, it sounds like serialized nir<br>
>> >>>> would<br>
>> >>>> not be enough for the shader cache as some GLSL type info would be<br>
>> >>>> lost. It sounds like GLSL IR => SPIR-V would be good enough. Is that<br>
>> >>>> right?<br>
>> >>>><br>
>> >>>> I don't think we have a strict requirement for the GLSL IR => SPIR-V<br>
>> >>>> path for GL 4.6, right? So, this is more of a 'nice-to-have'?<br>
>> >>>><br>
>> >>>> I'm not sure we'd want to make i965 shader cache depend on a<br>
>> >>>> nice-to-have feature. (Unless we're pretty sure it'll be available<br>
>> >>>> soon.)<br>
>> >>>><br>
>> >>>> But, it would be nice to not have to fallback to compiling the GLSL<br>
>> >>>> for i965 shader cache, so it would be worth waiting a little bit to<br>
>> >>>> be<br>
>> >>>> able to rely on a SPIR-V serialization of the GLSL IR.<br>
>> >>>><br>
>> >>>> What do you suggest?<br>
>> >>>><br>
>> >>>> -Jordan<br>
>> >>><br>
>> >>> We shouldn't use SPIR-V for the shader cache.<br>
>> >>><br>
>> >>> The compilation process for GLSL is: GLSL -> GLSL IR -> NIR -> i965<br>
>> >>> IRs.<br>
>> >>> Storing the content at one of those points, and later loading it and<br>
>> >>> resuming the normal compilation process from that point...that's<br>
>> >>> totally<br>
>> >>> reasonable.<br>
>> >>><br>
>> >>> Having a fallback for "some things in the cache but not all the<br>
>> >>> variants<br>
>> >>> we needed" suddenly take a different compilation pipeline, i.e. SPIR-V<br>
>> >>> -> NIR -> ... seems risky.  It's a different compilation path that we<br>
>> >>> don't normally use.  And one you'd only hit in limited circumstances.<br>
>> >>> There's a lot of potential for really obscure bugs.<br>
>> >><br>
>> >> Since we're going to expose exactly that path for GL_ARB_spirv / OpenGL<br>
>> >> 4.6, we'd better make sure it works always.  Right?<br>
>> ><br>
>> > In addition to the old pipeline:<br>
>> ><br>
>> > - GLSL from the app -> GLSL IR -> NIR -> i965 IR<br>
>> ><br>
>> > GL_ARB_spirv and OpenGL 4.6 add a second pipeline:<br>
>> ><br>
>> > - SPIR-V from the app -> NIR -> i965 IR<br>
>> ><br>
>> > Both of those absolutely have to work.  But these:<br>
>> ><br>
>> > - GLSL -> GLSL IR -> NIR -> SPIR-V -> NIR -> i965 IRs<br>
>> > - GLSL -> GLSL IR -> SPIR-V -> NIR -> i965 IRs<br>
>> ><br>
>> > aren't required to work, or even be supported.  It makes a lot of sense<br>
>> > to support them - both for testing purposes, and as an alternative to<br>
>> > glslang, for a broader tooling ecosystem.<br>
>> ><br>
>> > The thing that concerns me is that if you use SPIR-V for the cache, you<br>
>> > need these paths to not just work, but be _indistinguishable_ from one<br>
>> > another:<br>
>> ><br>
>> > - GLSL -> GLSL IR -> NIR -> ...<br>
>> > - GLSL -> GLSL IR -> NIR -> SPIR-V, then SPIR-V -> NIR -> ...<br>
>> ><br>
>> > Otherwise the original compile and partially-cached recompile might have<br>
>> > different properties.  For example, if the the SPIR-V step messes with<br>
>> > variables or instruction ordering a little, it could trip up the loop<br>
>> > unroller so the original compiler gets unrolled, and the recompile from<br>
>> > partial cache doesn't get unrolled.  I don't want to have to debug that.<br>
>><br>
>> That is a very compelling argument.  If we want Mesa to be an<br>
>> alternative to glslang, I think we would like to have that property, but<br>
>> it's not a hard requirement for that use case.<br>
><br>
><br>
> I also find that argument rather compelling.  The SPIR-V -> NIR pass is<br>
> *not* a simple pass.  It does piles of lowering and things on-the-fly as<br>
> well as creating temporary variables for various things.  The best we could<br>
> hope to guarnatee would be that NIR -> SPIR-V -> NIR -> vars_to_ssa -> CSE<br>
> is idempotent.  Even that might be a bit of a stretch.<br>
><br>
>><br>
>> > One could avoid this by making the original compile always go through<br>
>> > SPIR-V, and just drop glsl_to_nir altogether, so both take the same<br>
>> > paths.  But...it's kind of an unnecessary step in the common case...<br>
>><br>
>> We may eventually partially do that, but that shouldn't block (any)<br>
>> other work.  In the short term it would likely add compile overhead that<br>
>> many would find unacceptable... by virtue of being non-zero.<br>
>><br>
>> > Just serializing/reading back the NIR and resuming the compile from the<br>
>> > exact same IR would also solve that problem.<br>
>> ><br>
>> > Or, just being -really- careful with the translator, I guess...<br>
>> ><br>
>> >> One nice thing about SPIR-V is that all of the handling of uniform<br>
>> >> layouts, initial uniform values, attribute locations, etc. is already<br>
>> >> serialized.  If I'm not mistaken, that was one of the big pain points<br>
>> >> for all of the existing on-disk storage methods.  All of that has been<br>
>> >> sorted out for SPIR-V, and we have to make it work anyway.<br>
>> ><br>
>> > That is pretty nice.  I don't recall it being that painful, but, not<br>
>> > reinventing things is kind of nice too...<br>
>><br>
>> Maybe the right answer is to share some things from SPIR-V (e.g., the<br>
>> way it describes I/O) to reduce duplication, but serialize NIR<br>
>> instructions and control flow in a "native" format.<br>
><br>
><br>
> I strongly suspect that there is some overestimation of how much code<br>
> nir_serialize would actually be here.  I just looked at nir_clone.c and it's<br>
> 781 lines.  It could probably drop by 50-100 LOC if we didn't expose helpers<br>
> for also cloning of single functions, variables, and constants.  I would<br>
> expect nir_serialize to be able to handle serialization and deserialization<br>
> in about 2x the code of nir_clone.c.  By comparison, SPIR-V -> NIR is 8159<br>
> LOC and counting (that doesn't include generated anything) and I would<br>
> expect GLSL -> SPIR-V to be similarly sized.<br>
<br>
</div></div>I just finished typing up a nir_serialize implementation, although I<br>
haven't debugged it yet. It wound up being 1150 lines, including<br>
comments and whatnot:<br>
<a href="https://cgit.freedesktop.org/~cwabbott0/mesa/commit/?h=nir-serialize&id=2bacd646460328940c5021d1bdaced09a45ed947" rel="noreferrer" target="_blank">https://cgit.freedesktop.org/~<wbr>cwabbott0/mesa/commit/?h=nir-<wbr>serialize&id=<wbr>2bacd646460328940c5021d1bdaced<wbr>09a45ed947</a><br></blockquote><div><br></div><div>Taking the lock... I've got a test harness written for it and am working on fixing some of the bugs. :-) <br></div></div></div></div>