[llvm-dev] MachinePipeliner refactoring
Brendon Cahoon via llvm-dev
llvm-dev at lists.llvm.org
Mon Jul 15 17:35:33 PDT 2019
Hi James,
I also think that refactoring the code generation part is a great idea. That code is very complicated and difficult to maintain. I’ve wanted to rewrite that code for a long time, but just have never got to it. There are quite a few edge cases to handle (at least in the current code). I’ll take a deeper look at your patch. The abstractions that you mention, Stage and Block, are good ones. I think you’ll need to account for the cycle, or position within a Stage as well. It is a complex problem with a lot different edge cases when dealing with Phis, though I think they can be dealt with much better than the existing code.
Generating code for the 3 main parts, the prolog, kernel, and epilog, can be challenging because each has a unique difference that made is hard to generalize the code. For example, the prologs will not have an Phis, but the code generation needs to be aware of when the Phis occur in the sequence in order to get the correct remapped name. In the kernel, values may come from a previous iteration of the kernel. In the epilog, values can come from the same epilog block, the kernel, or a prolog block.
It would be great to be able to generate the compact representation that you describe, so that a prolog/epilog do not need to be generated. Hexagon has a special hardware loop instruction, spNloop, specifically for this. Although, the compiler does not generate that instruction currently. Perhaps it would be easier to generate this form first, and then convert it to a prolog(s), kernel, epilog(s) sequence.
There are a lot of subtle rules when dealing with the Phi instructions depending on when they are scheduled relative to uses. There are two different factors to account for – the stage and cycle when the Phi is scheduled. For example, a use of a Phi can be scheduled at an earlier cycle, but later stage. Several different combinations can occur, and each use of the Phi can have a different stage/cycle. Maintaining the map between new and old virtual registers depends on the stage, cycle, and the current block (prolog, kernel, or epilog).
The epilogs are generated in reverse order due to the way the instructions need to be laid out. The first epilog block after the kernel is going to have instructions scheduled in the last stage only, then the next epilog block, if needed, will have instructions from LastStage-1 first, then the LastStage.
Let’s say we have instructions x, y, z, that are scheduled in different iterations/stage, x in stage0, y in stage1, and z in stage2, and x generates a value used by y, which generates a value used by z. The generated code will look like:
Prolog 0 (this block ends with a conditional jump to Epilog 0):
X
Prolog 1 (this block ends with a conditional jump to Epilog 1):
Y
X
Kernel:
Z, Y, X
Epilog 0:
Z
Epilog 1:
Y
Z
The instructions in the prolog and epilog are generate in original program order within each stage, but in the last epilog stage, the instruction Y generates a value used by the subsequent Z.
The reduceLoopCount function is needed for an architecture that has a hardware loop instruction like Hexagon’s LOOP0. For each prolog generated, the loop counter in the instruction needs to be decremented. This could have been done only in the last prolog block, but it’s done every time a prolog block is generated instead (initially, I thought that would be easier since the prolog generation code wouldn’t need to know which block was the last one). But, later, I added code to handle the case when the loop can be removed completely. It seems reasonable to change this so that it’s updated only once, especially since find the loop instruction can require searching through the CFG.
Loop carried Phis are hard. Initially, I tried handling the cases for adding new Phis and existing Phis with one function. But, it turned out that adding new Phis, when a instruction use is generated in an earlier cycle, but later stage, doesn’t cause as many special cases as handling the different edge cases for an existing Phis. There are less combinations when adding a new Phis.
Yes, you’re correct about the use of “iteration” vs. “stage” (I even used them together earlier). The use of term stage comes from the paper, but I often use iteration when talking to others about software pipelining, especially if they have read the paper. I agree that we should be more consistent with using the terms.
Let me think some more about the questions you’ve asked. I do think it’s worthwhile to spend some time on creating a good abstraction and improving the code. I appreciate that you’ve started to think/work on this problem, so I’d like to help out to pursue this direction.
Thanks,
Brendon
From: James Molloy <james at jamesmolloy.co.uk>
Sent: Monday, July 15, 2019 11:05 AM
To: Jinsong Ji <jji at us.ibm.com>
Cc: Brendon Cahoon <bcahoon at quicinc.com>; Hal Finkel <hfinkel at anl.gov>; LLVM Dev <llvm-dev at lists.llvm.org>
Subject: [EXT] Re: MachinePipeliner refactoring
Hi Jingsong,
Thanks for testing out the prototype! I'm not surprised there are errors in that version; it's not 100% ready yet (mainly a demonstrator for the path I'd like to take). I'm really trying to work out if it makes sense to try and incrementally get from the current state of the world to that prototype incrementally, or if it's just so far away that a full-on refactor (or two code paths) is required. I suspect only Brendan really has the context to give insight there!
James
On Mon, 15 Jul 2019 at 16:32, Jinsong Ji <jji at us.ibm.com<mailto:jji at us.ibm.com>> wrote:
Hi James:
Personally, I like the idea of refactoring and more abstraction,
But unfortunately, I don't know enough about the edges cases either.
BTW: the prototype is still causing quite some Asseertions in PowerPC - some nodes are not generated in correct order.
Best,
Jinsong Ji (纪金松), PhD.
XL/LLVM on Power Compiler Development
E-mail: jji at us.ibm.com<mailto:jji at us.ibm.com>
[Inactive hide details for James Molloy ---07/15/2019 06:16:22 AM---Hi Brendan (and friends of MachinePipeliner, +llvm-dev for o]James Molloy ---07/15/2019 06:16:22 AM---Hi Brendan (and friends of MachinePipeliner, +llvm-dev for openness), Over the past week or so I've
From: James Molloy <james at jamesmolloy.co.uk<mailto:james at jamesmolloy.co.uk>>
To: LLVM Dev <llvm-dev at lists.llvm.org<mailto:llvm-dev at lists.llvm.org>>, jji at us.ibm.com<mailto:jji at us.ibm.com>, bcahoon at quicinc.com<mailto:bcahoon at quicinc.com>, Hal Finkel <hfinkel at anl.gov<mailto:hfinkel at anl.gov>>
Date: 07/15/2019 06:16 AM
Subject: [EXTERNAL] MachinePipeliner refactoring
________________________________
Hi Brendan (and friends of MachinePipeliner, +llvm-dev for openness),
Over the past week or so I've been attempting to extend the MachinePipeliner to support different idioms of code generation. To make this a bit more concrete, there are two areas where the currently generated code could be improved depending on architecture:
1) The epilog blocks peel off the final iterations in reverse order. This means that the overall execution of loop iterations isn't in a perfectly pipelined order. For architectures that have hardware constructs that insist on a first-in-first-out order (queues), the currently generated code cannot be used.
2) For architectures (like Hexagon) that have dedicated predicate register files, we can generate a compact representation of the loop by predicating stages of the loop kernel independently. In this case we can either have a prolog, epilog, or neither (wrapping the prolog and epilog inside the kernel by using PHIs of predicates).
At the moment, a lot of the code generation helper code in MachinePipeliner is tightly fit to its current code generation strategy ("If we're in the epilog, to this, else do this"). I'm keen to try and make some of the complex calculations it does, such as where PHIs should come from, more abstract so they can be reused and composed.
https://reviews.llvm.org/D64665 is my current best-effort. This generates perfect code for PowerPC, but causes a load of problems for Hexagon. It's become apparent that I don't know enough about some of the edge cases in the MachinePipeliner code to refactor this from scratch. I'm therefore looking for direction in factoring in an incremental fashion.
I think there are a couple of areas that I'd like to change, and I'd appreciate your ideas and opinions because I clearly don't know enough about the edge cases here.
a) TII->reduceLoopCount() is hard to understand. Understanding the intended semantics of this hook from the documentation, I've found, is hard. Its use appears to be strongly fit to Hexagon (there is even a comment about the removal of LOOP0 in the MachinePipeliner target agnostic code, which really shouldn't be there). Why it's called multiple times I don't understand (why can't we just call it once with the total number of iterations to peel?).
b) Understanding how loop-carried PHIs are linked together is really hard. There are two functions dedicated to this with many edge cases, which are linked with the prolog and epilog schedule. It'd be great to somehow factor these such that they are independent of the code generation strategy. Note that this is really important for some of the code gen strategies I mention at the beginning, because loop-carried PHIs in this case may actually end up being selects or uses of predicated instructions.
c) There is a slight conflation of "iteration" and "stage" in the documentation that makes it hard to follow what VRMap contains and the invariants between functions.
My intent in D64665 was to create two abstractions: "Stage" and "Block". Instead of referring to stages by index (VRMap), each Stage would take a prior Stage as input. Stages are contained inside Blocks, which handles predecessors and successors. I feel that arranging the code generation in this CFG-like way will make the flow of data much easier to analyze. Of course, a Stage doesn't just depend on a prior Stage - their loop carried inputs could come from any other Stage (and while I think I understand how this works, I clearly don't get all the edge cases).
What do you think of this abstraction? do you think it's doomed to failure because it's too simplistic to cover all the cases?
Do you have any suggestions of areas where we can start to factor out without a large-scale code breakage? I'm finding this hard to get my teeth into as the proposed code structure is so different from its current form.
Thanks for any opinions or suggestions!
Cheers,
James
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190716/0b3688de/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image001.gif
Type: image/gif
Size: 105 bytes
Desc: image001.gif
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20190716/0b3688de/attachment-0001.gif>
More information about the llvm-dev
mailing list