<p dir="ltr"><br>
On May 15, 2015 2:40 PM, "Francisco Jerez" <<a href="mailto:currojerez@riseup.net">currojerez@riseup.net</a>> wrote:<br>
><br>
> Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> writes:<br>
><br>
> > On Fri, May 15, 2015 at 9:51 AM, Francisco Jerez <<a href="mailto:currojerez@riseup.net">currojerez@riseup.net</a>> wrote:<br>
> >> Jason Ekstrand <<a href="mailto:jason@jlekstrand.net">jason@jlekstrand.net</a>> writes:<br>
> >><br>
> >>> I haven't said much about this series up until now.  I've mostly sat<br>
> >>> and watched and focused my time on other things.  As I said in the<br>
> >>> meeting today, I think that part of the problem here is that there are<br>
> >>> at least 3 "refactors" in here besides the new feature.  By<br>
> >>> "refactors", I mean "new ways of solving problem X".  Part of the<br>
> >>> problem with the discussion is that all of this has been conflated and<br>
> >>> we aren't actually talking about how to solve each of those problems.<br>
> >>> I'm going to try and break it out here to try and aid in the<br>
> >>> discussion.<br>
> >>><br>
> >> Thanks a lot Jason for giving a more constructive turn to this<br>
> >> discussion. :)<br>
> >><br>
> >>> 1) The builder.  I think *everyone* likes the builder.  The argument<br>
> >>> here is not over whether or not we want it.  People have expressed<br>
> >>> concern that adding the builder now without actually doing the<br>
> >>> refactor will result in duplicated code that will get out-of-sync.  I<br>
> >>> think the best solution here is to go ahead and do the builder stuff<br>
> >>> immediately after the branch point.<br>
> >>><br>
> >> Agreed.<br>
> >><br>
> >>> 2) SIMD8 instruction splitting.  As I said in the call, we've done<br>
> >>> this a variety of ways in the past usually by emitting the split<br>
> >>> instructions in the visitor and manually using half() and<br>
> >>> inst->force_sechalf.  We also have a few places that I think still<br>
> >>> split the code in the generator.  What you're doing is a somewhat more<br>
> >>> elegant version of the "emit it in the visitor" stuff that we've done<br>
> >>> in the past.  You've got your zip/unzip functions etc. to handle<br>
> >>> splitting and combining and then you have a loop to actually emit the<br>
> >>> sends.  Really, it's fairly nice and concise.  The question is whether<br>
> >>> or not it's really what we want to do (more on this in a bit).<br>
> >>><br>
> >> There is another closely related problem that this infrastructure solves<br>
> >> and may not be obvious in this series (it was in my original branch<br>
> >> though [1]), but I think it's worth keeping in mind, I'll call it 2.1:<br>
> >> Some of the surface messages lacked SIMD4x2 variants, so a strided copy<br>
> >> is required to transpose the original AoS vector into SoA form.  This<br>
> >> turns out to be algorithmically identical to the strided copy required<br>
> >> to "unzip" a SIMD16 vector, so it can be accomplished without additional<br>
> >> effort.<br>
> >><br>
> >> These transformations are largely transparent for the functions building<br>
> >> typed/untyped surface messages, they are hidden behind an interface that<br>
> >> doesn't seem to have received any attention yet:<br>
> >><br>
> >>  - struct vector_layout: This represents the layout of a vector in a<br>
> >>    payload.  It's initialized based on the restrictions of the hardware<br>
> >>    for the specific message we want to send, and the current code<br>
> >>    generation parameters (i.e. SIMD mode).<br>
> >><br>
> >>  - emit_insert(): This takes a vector with the layout native to the EU<br>
> >>    (e.g. an fs_reg) and performs the required transformations to turn it<br>
> >>    into a form suitable for the shared unit according to a vector_layout<br>
> >>    struct (what may involve unzipping, a strided copy or no work at<br>
> >>    all).<br>
> >><br>
> >>  - emit_extract(): The converse of emit_insert().  This takes a payload<br>
> >>    fragment (array_reg) and transforms it into a vector of native<br>
> >>    layout, according to a vector_layout struct.<br>
> >><br>
> >> My intention was to come up with an interface more general than SIMD16<br>
> >> vector splitting and SIMD4x2 to SIMD8 conversion alone, I was also<br>
> >> thinking texturing instruction payload quirks (which might require<br>
> >> padding or different permutations of the same values depending on the<br>
> >> generation even if the SIMD mode is natively supported -- let's call<br>
> >> this 2.2) and framebuffer writes (2.3).<br>
> ><br>
> > Thank you for pointing out what else you were trying to do with it.  I<br>
> > understand that you're trying to solve a dozen different problems and<br>
> > that the infrastructure you've created is one that you think solves<br>
> > all of them at the same time.  It helps to know what all problems you<br>
> > were trying to solve.  However, since we are putting SIMD4x2 aside for<br>
> > now, in the interest of keeping us from getting side-tracked, let's<br>
> > table this for now.  It's a problem that may need solving, but it's a<br>
> > *different* problem.<br>
> ><br>
> If it can be solved with the same approach, it is a closely related<br>
> problem, isn't it?  And wouldn't it be a bit silly to knowingly<br>
> implement a less general solution when the more general solution<br>
> involves *less* work and while we know we'll want to address the<br>
> remaining problems solved by the more general solution eventually? ;)</p>
<p dir="ltr">Perhaps, but trying to solve all the problems at the same time is why the conversation has gone nowhere on the past.  We need to keep things split out.</p>
<p dir="ltr">> >>> 3) Array reg.  This is something of a helper to (2).<br>
> >><br>
> >> In fact the primary motivation for array_reg wasn't instruction<br>
> >> splitting, but emit_collect(), more on that later.<br>
> >><br>
> >>> I agree that it's nice to be able to have access to the size of a<br>
> >>> register without having to look at the allocator.  The problem here<br>
> >>> (and why we don't have a size in fs_reg) is that we copy the reigster<br>
> >>> around.  Every register does have some sort of "underlying storage"<br>
> >>> that it shares with other registers with the same number.  But that<br>
> >>> storage is represented by the size in the allocator and nothing else.<br>
> >>> Is that bad?  Maybe.  Should we have a way of passing the size around<br>
> >>> with it?  Maybe.  In any case, I think it's best to table this<br>
> >>> discussion until we've talked a bit about (2) because I think the<br>
> >>> resolution there will make (3) more clear.<br>
> >>><br>
> >> I think the mechanism we use to determine the size of a register hardly<br>
> >> ever involves the allocator, and there are good reasons not to rely on<br>
> >> it: It only works for VGRFs, can easily break with register coalescing,<br>
> >> and it makes "slicing" and other zero-copy transformations of registers<br>
> >> difficult (e.g. taking an individual element out of a VGRF array without<br>
> >> copying it into a one-component register).  Typically the size of a<br>
> >> register is implicitly assumed by the instruction using it, and where<br>
> >> it's not (because the instruction expects a variable length payload) it<br>
> >> is provided separately from the register via "mlen".  Neither of these<br>
> >> two options works here as will be obvious shortly.<br>
> ><br>
> > Understood.<br>
> ><br>
> >>> One note on 3 before we table it.  People have been pulling<br>
> >>> emit_collect out as a straw-man and beating on it but I really do like<br>
> >>> it in principle.  There are piles of times where you want a payload<br>
> >>> and you have to allocate an array, do stuff, and then put it into the<br>
> >>> payload and it's really annoying.  It would be much easier if we had a<br>
> >>> helper that just did it all in one function and that's what<br>
> >>> emit_collect tries to do.  *Thank you* for finally trying to solve<br>
> >>> that problem and make payloads less painful!  However, I do wish it<br>
> >>> were implemented a bit differently but I'll make those comments on the<br>
> >>> patch itself.<br>
> >>><br>
> >><br>
> >> Yeah.  I think it was Matt who mentioned in the call that emit_collect()<br>
> >> is really complex.  I completely agree with that statement.  It needs to<br>
> >> take care of a bunch of things to build the payload correctly:<br>
> >><br>
> >>  - Allocate a register that will hold the result, calculating the<br>
> >>    correct total size based on the amount of data we want to<br>
> >>    concatenate.<br>
> >><br>
> >>  - Calculate the correct header size.<br>
> >><br>
> >>  - Allocate and release memory of the correct size to hold the array of<br>
> >>    LOAD_PAYLOAD sources, also based on the amount of data we want to<br>
> >>    concatenate.<br>
> >><br>
> >>  - Split up the arguments to be passed to LOAD_PAYLOAD as sources in<br>
> >>    scalars of the correct width (which depends on whether the argument<br>
> >>    is part of the header or not, and what the dispatch width is), and<br>
> >>    initialize the array.<br>
> >><br>
> >>  - Create a LOAD_PAYLOAD instruction with the result of the previous<br>
> >>    steps.<br>
> >><br>
> >> Each one of these tasks is non-trivial, repetitive, can conceivably go<br>
> >> wrong, and until now duplicated wherever we had to build a payload --<br>
> >> obscuring the actually meaningful work being done.  This is why I think<br>
> >> that the assertion that emit_collect() makes code difficult to<br>
> >> understand is backwards.  Thanks to emit_collect() the task of building<br>
> >> a payload is tremendously simplified.<br>
> ><br>
> > Yes.  I really don't think people are disagreeing with you too badly<br>
> > about emit_collect().  I also don't think it's as complicated as you<br>
> > make it sound.<br>
> ><br>
> Heh, maybe "tremendously" wasn't the word I was looking for, but it's<br>
> appreciably simplified. :P<br>
><br>
> >> For emit_collect() to be possible there has to be some way to represent<br>
> >> a register+size pair, because it needs to know the size of each<br>
> >> argument, and because its result is itself a register+size pair -- The<br>
> >> caller cannot do anything useful with a payload of unknown size, unless<br>
> >> it re-calculates the size of the result what would defeat much of the<br>
> >> purpose of emit_collect().<br>
> >><br>
> >> The biggest benefit from being able to represent a register+size pair as<br>
> >> a value is that emit_collect() can easily be composed with other<br>
> >> functions (including with itself), so the construction of specific<br>
> >> chunks of the payload can easily be decoupled and re-used.  My code<br>
> >> takes advantage of this feature extensively in order to share code that<br>
> >> constructs surface message headers or transforms some value into the<br>
> >> form expected by the shared unit (doing vector splitting or a strided<br>
> >> copy).<br>
> ><br>
> > Yes, I understand that it's useful for that.  Hence the suggestion of<br>
> > either a reg/size pair struct or passing the size as an argument.  The<br>
> > struct has the advantage, as you said, of allowing chaining.<br>
> ><br>
> >>> ### SIMD16 Instruction Splitting ###<br>
> >>><br>
> >>> SIMD16 instruction splitting is an unfortunate fact of our hardware.<br>
> >>> There are a variety of times we have to do it including dual-source FB<br>
> >>> writes, some texturing, math ops on older gens and maybe another place<br>
> >>> or two.  Classically, this has been done in one of two places: The<br>
> >>> visitor as we emit the code, or the generator.  The problem with doing<br>
> >>> it in the generator is that we can't schedule it and, if it involves a<br>
> >>> payload, it's not really possible.  The result is that we usually do<br>
> >>> it in the visitor these days.<br>
> >>><br>
> >>> Unfortunately, even in the visitor, it's gen-specific and annoying.<br>
> >>> It gets even worse when you're working with something such as the<br>
> >>> untyped surface read/write messages that work with multi-component<br>
> >>> values that have to be zipped/unzipped to use Curro's terminology.<br>
> >>> Curro came up with some helper functions to make it substantially less<br>
> >>> annoying but it still involves nasty looping.<br>
> >>><br>
> >>> At some point in the past I proposed a completely different and more<br>
> >>> solution to this problem.  Unfortunately, while I've talked to Matt &<br>
> >>> Ken about it, it's never really been discussed all that publicly so<br>
> >>> Curro may not be aware of it.  I'm going to lay it out here for<br>
> >>> Curro's sake as well as the sake of public record.<br>
> >>><br>
> >>> The solution involves first changing the way we handle sends into a<br>
> >>> two step process.  First, we emit a logical instruction that contains<br>
> >>> all of the data needed for the actual instruction.  Then, we convert<br>
> >>> from the logical to the actual in a lowering pass.  Take, for example,<br>
> >>> FB writes with which I am fairly familiar.  We would first emit a<br>
> >>> logical FS_FB_WRITE_# instruction that has separate sources for color,<br>
> >>> depth, replicated alpha, etc.   Then, in the lower_fb_writes pass<br>
> >>> (which we would have to implement), we would construct the payload<br>
> >>> from the sources provided on the logical instruction and emit the<br>
> >>> actual LOAD_PAYLOAD and FB_WRITE instructions.  This lower_fb_writes<br>
> >>> function would then get called before the optimization loop so that<br>
> >>> the rest of the optimization could would never see it.<br>
> >>><br>
> >>> Second, we add a split_instruction helper that would take a SIMD16<br>
> >>> instruction and naively split it into two SIMD8 instructions.  Such a<br>
> >>> helper really shouldn't be that hard to write.  It would have to know<br>
> >>> how to take a SIMD16 vec4 and unzip it into two SIMD8 vec4's but that<br>
> >>> shouldn't be bad.  Any of these new logical send instructions would<br>
> >>> have their values as separate sources so they should be safe to split.<br>
> >>><br>
> >>> Third, we add a lower_simd16_to_simd8 pass that walks the<br>
> >>> instructions, picks out the ones that need splitting, and calls<br>
> >>> split_instruction on them.  All of the gen-specific SIMD8 vs. SIMD16<br>
> >>> knowledge would be contained in this one pass.  This pass would happen<br>
> >>> between actually emitting code and running lower_fb_writes (or<br>
> >>> whatever other send lowering passes we have).<br>
> >>><br>
> >>> Finally, and this is the icing on the cake, we write a<br>
> >>> lower_simd32_to_simd16 pass that goes through and lowers all SIMD32<br>
> >>> instructions (that act on full-sized data-types) to SIMD16<br>
> >>> instructions.  Once the rest of the work is done, we get this pass,<br>
> >>> and with it SIMD32 mode, almost for free.<br>
> >>><br>
> >>> I know this approach looks like more work and, to be honest, it may<br>
> >>> be.  However, I think it makes a lot of things far more<br>
> >>> straightforward.  In particular, it means that people working on the<br>
> >>> visitor code don't have to think about whether or not an instruction<br>
> >>> needs splitting.  You also don't have to deal with the complexity of<br>
> >>> zipping/unzipping sources every time.  Instead, we put all that code<br>
> >>> in one place and get to stop thinking about it.  Also, if we *ever*<br>
> >>> want to get SIMD32, we will need some sort of automatic instruction<br>
> >>> splitting and this seems like a reasonable way to do it.<br>
> >>><br>
> >>> I've talked to Ken about this approach and he's 100% on-board.  I<br>
> >>> don't remember what Matt thinks of it.  If we like the approach, then<br>
> >>> we should just split the tasks up and make it happen.  It's a bit of<br>
> >>> refactoring but it shouldn't be terrible.  If we wanted to demo it, I<br>
> >>> would probably suggest starting with FB writes as those are fairly<br>
> >>> complex but yet self-contained.  They also have a case where we do<br>
> >>> split an instruction so it would be interesting to see what the code<br>
> >>> looks like before and after.<br>
> >>><br>
> >><br>
> >> I generally like your proposal.  I guess the question we need to answer<br>
> >> is whether we want this complexity to be in a lowering pass or in a<br>
> >> helper function used to build the send-like instruction -- In either<br>
> >> case we need code to handle zipping and unzipping of SIMD16 vectors,<br>
> >> it's just about whether this code is called by a lowering pass or higher<br>
> >> up in the visitor.<br>
> >><br>
> >> I can think of several benefits of the approach you propose over mine:<br>
> >><br>
> >>  - It's more transparent for the visitor code emitting the message -- I<br>
> >>    completely agree with you that the explicit loops are rather ugly.<br>
> >><br>
> >>  - Instructions with explicit separate sources are likely to be more<br>
> >>    suitable for certain optimization passes.  Pull constant loads use a<br>
> >>    similar approach with an expression-style opcode which is at some<br>
> >>    point lowered to a load payload and send message.  This may not be<br>
> >>    terribly important at this point because of the optimizations already<br>
> >>    performed in GLSL IR and NIR and due to the nature of the majority of<br>
> >>    opcodes that don't support SIMD16, but it still seems appealing.<br>
> ><br>
> > There's one more really big one that you missed:<br>
> ><br>
> > It scales!  We can't afford to have a for loop for every ADD and MUL<br>
> > instruction.  Sure, we might be able to afford it on sends, but not<br>
> > for everything.<br>
> ><br>
> Well, I doubt you'd want to implement your proposal to the letter for<br>
> non-send instructions: For those we don't need separate lowered and<br>
> non-lowered instructions because there's no payload to assemble, so we<br>
> can just do with a single opcode with different execution widths.  When<br>
> you take payloads out of the picture all the disadvantages mentioned of<br>
> the lowering pass approach no longer apply, so I totally agree with you<br>
> that we want a general lowering pass to lower instructions that expect<br>
> their arguments as separate sources in their final form.  In any case<br>
> send-like instructions need a somewhat different (and less scalable)<br>
> treatment.<br>
><br>
> >> Some disadvantages come to my mind too:<br>
> >><br>
> >>  - It increases the amount of work required to add a new send-like<br>
> >>    instruction because you need lowered and unlowered variants for each.<br>
> >><br>
> >>  - It seems tricky to get right when splitting an instruction in halves<br>
> >>    involves changing the actual contents of the payload beyond zipping<br>
> >>    and unzipping its arguments -- This might not seem like a big deal<br>
> >>    right now, but it will be a problem when we implement SIMD32.  The<br>
> >>    surface messages that take a sample mask as argument are a good<br>
> >>    example, because they only have 16 bits of space for it so you<br>
> >>    actually need to provide different values depending on the "slot<br>
> >>    group" the message is meant for.  This can be worked around easily in<br>
> >>    the visitor by shifting the sample mask register but it seems harder<br>
> >>    to fix up later.<br>
> ><br>
> > Why do sample masks need to be part of the logical instruction?  Can't<br>
> > we figure that out when we lower from logical to physical based on the<br>
> > quarter control?<br>
> ><br>
> You can surely do anything you want during the logical-to-physical<br>
> conversion, including rewriting the header, the problem is that it that<br>
> forces you to have a pile of message-specific handling code in the<br>
> lowering pass.  How are you planning to address that?  With a separate<br>
> lowering pass for each message opcode or a general one with<br>
> message-specific knowledge?</p>
<p dir="ltr">Yes, the lowering pass will have *all* of the message-specific information.  Probably broken out into helper functions exactly the way the message emit code is broken out now.  The lowering pass then just knows what helper to call for what instruction.</p>
<p dir="ltr">> >>  - My intention was to handle vector layout peculiarities beyond<br>
> >>    SIMDN-to-SIMD8 splitting under a consistent framework, leaving it<br>
> >>    under the control of the helper function that builds the message<br>
> >>    which quirks to apply.  With the lowering pass approach you'd either<br>
> >>    need message-specific lowering passes, message-specific code in a<br>
> >>    general lowering pass, or some way for the builder code to pass<br>
> >>    vector_layout-like metadata to the lowering pass -- In the first two<br>
> >>    cases the policy on how to format each source in the payload would be<br>
> >>    "delocalized" between the visitor and lowering pass, what I don't<br>
> >>    particularly like.<br>
> ><br>
> > We already do that and we would continue to.  The logical instruction<br>
> > wouldn't be much more than a serialized version of the helper<br>
> > function.<br>
> ><br>
> > Thanks for your reply.<br>
> > --Jason<br>
> ><br>
> >>> Thoughts?<br>
> >>><br>
> >>> Question for Curro: Supposing for the moment that we decide to do<br>
> >>> SIMD16 -> SIMD8 splitting this way, how much of the array_reg stuff<br>
> >>> would still be needed for image_load_store?<br>
> >>><br>
> >> I think I've already answered this question, but I'll answer it again<br>
> >> here for clarity -- It would still help with building and passing around<br>
> >> payloads concisely using emit_collect(), regardless of how we implement<br>
> >> SIMD16 instruction splitting.<br>
> >><br>
> >>> I hope this e-mail provides us with some good talking points and a way forward.<br>
> >>> --Jason<br>
> >><br>
> >> Thanks Jason!<br>
> >><br>
> >> [1] <a href="http://cgit.freedesktop.org/~currojerez/mesa/log/?h=image-load-store">http://cgit.freedesktop.org/~currojerez/mesa/log/?h=image-load-store</a><br>
</p>