Hi,
I’m trying to work out the behavior of llvm-mca on instructions with ProcResGroups. My current understanding is:
When an instruction requests a port group (e.g., HWPort015) and all of its atomic sub-resources (e.g., HWPort0,HWPort1,HWPort5), HWPort015 is marked as “reserved” and is issued in parallel with HWPort0, HWPort1, and HWPort5, blocking future instructions from reserving HWPort015 for the duration but not explicitly blocking any of HWPort0, HWPort1, or HWPort5 (although those ports are in fact blocked because the instruction also requested usage of those ports).
When an instruction requests a port group (e.g., HWPort015) but only some of its atomic sub-resources (e.g., HWPort0 and HWPort1 but not HWPort5), then HWPort015 is scheduled according to the round robin scheduler, which in this case would decide to dispatch it on Port5.
This (I believe) explains the following reported timings on a basic block which consists of a single instruction with no dependencies and a small NumMicroOps (i.e., only bottlenecked by resource availability), where I have tried out different port maps and ResourceCycles (all of these are for 100 iterations):
- When the resource mapping is: { HWPort0: 2 cycles, HWPort01: 2 cycles }, the instruction has a Total Cycles of 200, because the round-robin scheduler always assigns the HWPort01 resource to execute on HWPort1, so each iteration requires 2 cycles total.
- When the resource mapping is: { HWPort0: 2 cycles, HWPort1: 2 cycles, HWPort01: 2 cycles }, the instruction still has a Total Cycles of 200, because HWPort01 is marked as “reserved” and therefore issued in parallel to HWPort0 and HWPort1, so each iteration still requires 2 cycles total.
The one case that still confuses me is:
- When the resource mapping is: { HWPort0: 2 cycles, HWPort01: 4 cycles }, the instruction has a Total Cycles of 300. This seems to be because issuing to HWPort01 does not always block when I intuitively think it should (e.g., instructions are issued on Cycle 1, Cycle 3 (?), Cycle 7, Cycle 9 (?), and so on, where (?) indicates that I don’t think it should be possible to issue then, but it does anyway).
Is this understanding of llvm-mca’s behavior correct? Are these observations intentional design decisions, limitations of llvm-mca’s model, bugs, or something else?
_______________________________________________
Thanks!
-Alex
LLVM Developers mailing list
llvm...@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
On May 9, 2020, at 5:12 PM, Andrea Di Biagio via llvm-dev <llvm...@lists.llvm.org> wrote:The llvm scheduling model is quite simple and doesn't allow mca to accurately simulate the execution of individual uOPs. That limitation is sort-of acceptable if you consider how the scheduling model framework was originally designed with a different goal in mind (i.e. machine scheduling). The lack of expressiveness of the llvm scheduling model unfortunately limits the accuracy of llvm-mca: we know the number of uOPs of an instruction. However we don't know which resources are consumed by which micro-opcodes. So we cannot accurately simulate the independent execution of individual opcodes of an instruction.
Another "problem" is that it is not possible to describe when uOPs effectively start consuming resources. At the moment, the expectation is that resource consumption always starts at relative cycle #0 (relative to the instruction issue cycle).
Example: an horizontal add on x86 is usually decoded into a pair of shuffles uOPs and a single (data-dependent) vector ADD uOP.The ADD uOP doesn't execute immediately because it needs to wait for the other two shuffle uOPs. It means that the ALU pipe is still available at relative cycle #0 and it is only consumed starting from relative cycle #1 (ssuming that both shuffles can start execution at relative cycle #0). In practice, the llvm scheduling model only allows us to declare which pipeline resources are consumed, and for how long (in number cycles). So we cannot accurately describe to mca that the delayed consumption of the ALU pipe.Now think about what happens if: the first shuffle uOP consumes 1cy of HWPort0, and the second shuffle uOp consumes 1cy of HWPort1, and the ADD consumes 1cy of HWPort01. We end up in that "odd" situation you described where HWPort01 is "reserved" for 1cy.In reality, that 1cy of HWPort01 should have started 1cy after the other two opcodes. At that point, both pipelines would have been seen available.
In conclusion, the presence of a "reserved" flag is not ideal, but it is sort-of a consequence of the above mentioned two limitations (plus the way how the Haswell and Broadwell models were originally designed).I hope it helps,-Andrea
Thanks, that’s very helpful!
Also, sorry for the miscue on that bug with the 2/4 cycles — I realize now that that’s an artifact of a change that I made to not crash when resource groups overlap without all atomic subunits being specified:
`echo 'fxrstor (%rsp)' | llvm-mca -mtriple=x86_64-unknown-unknown -march=x86-64 -mcpu=haswell`
crashes (because fxrstor requests `HWPort0,HWPort6,HWPort23,HWPort05,HWPort06,HWPort15,HWPort0156`, so HWPort0156 ends up asserting because 0,1,5, and 6 are all taken), so I added:
```
--- a/llvm/lib/MCA/HardwareUnits/ResourceManager.cpp
+++ b/llvm/lib/MCA/HardwareUnits/ResourceManager.cpp
@@ -292,7 +292,7 @@ void ResourceManager::issueInstruction(
ResourceState &RS = *Resources[Index];
- if (!R.second.isReserved()) {
+ if (!R.second.isReserved() && RS.isReady()) {
ResourceRef Pipe = selectPipe(R.first);
use(Pipe);
BusyResources[Pipe] += CS.size();
```
which is probably the cause of that weird behavior I reported.
I’m also somewhat curious about what “NumUnits” is modeling: I haven’t totally worked through the code yet, but it seems that when more (not necessarily atomic) sub-resources of a resource group are requested, more “NumUnits” of that group is requested. This doesn’t seem particularly intuitive to me, at least in my mental model of Haswell scheduling (and also leads to some infinite loops like `echo ‘fldenv (%rsp)' | llvm-mca -mtriple=x86_64-unknown-unknown -march=x86-64 -mcpu=haswell`, which requests HWPort0,HWPort1,HWPort01,HWPort05, and HWPort015, meaning that HWPort015 never schedules because it requests 4 “NumUnits” but only 3 are ever available). Is there some particular behavior that this is modeling?
Hi Alex,On Sun, May 10, 2020 at 4:00 PM Alex Renda <re...@csail.mit.edu> wrote:Thanks, that’s very helpful!
Also, sorry for the miscue on that bug with the 2/4 cycles — I realize now that that’s an artifact of a change that I made to not crash when resource groups overlap without all atomic subunits being specified:
`echo 'fxrstor (%rsp)' | llvm-mca -mtriple=x86_64-unknown-unknown -march=x86-64 -mcpu=haswell`
crashes (because fxrstor requests `HWPort0,HWPort6,HWPort23,HWPort05,HWPort06,HWPort15,HWPort0156`, so HWPort0156 ends up asserting because 0,1,5, and 6 are all taken), so I added:
```
--- a/llvm/lib/MCA/HardwareUnits/ResourceManager.cpp
+++ b/llvm/lib/MCA/HardwareUnits/ResourceManager.cpp
@@ -292,7 +292,7 @@ void ResourceManager::issueInstruction(
ResourceState &RS = *Resources[Index];
- if (!R.second.isReserved()) {
+ if (!R.second.isReserved() && RS.isReady()) {
ResourceRef Pipe = selectPipe(R.first);
use(Pipe);
BusyResources[Pipe] += CS.size();
```
which is probably the cause of that weird behavior I reported.
I’m also somewhat curious about what “NumUnits” is modeling: I haven’t totally worked through the code yet, but it seems that when more (not necessarily atomic) sub-resources of a resource group are requested, more “NumUnits” of that group is requested. This doesn’t seem particularly intuitive to me, at least in my mental model of Haswell scheduling (and also leads to some infinite loops like `echo ‘fldenv (%rsp)' | llvm-mca -mtriple=x86_64-unknown-unknown -march=x86-64 -mcpu=haswell`, which requests HWPort0,HWPort1,HWPort01,HWPort05, and HWPort015, meaning that HWPort015 never schedules because it requests 4 “NumUnits” but only 3 are ever available). Is there some particular behavior that this is modeling?The issue with fxrstor is unfortunately a bug. I'll see if I can fix it.
Strictly speaking, that "NumUnits" quantity is literally the number of processor resource units consumed. By construction, it should never exceed the actual number of resource units declared by a processor resource in the scheduling model (i.e. this particular field of MCProcResourceDesc - https://llvm.org/doxygen/structllvm_1_1MCProcResourceDesc.html#a9d4d0cc34fcce4779dc4445d8265fffc).
The reason why you see the crash/assert is because the number of resource units for scheduler resources is incorrectly set for that instruction by the instruction builder in llvm-mca ( lib/MCA/InstrBuilder.cpp - function `initializeUsedResources()`). I am looking at it.
Hi Andrea,
Yep, that commit fixes that issue for me too.
One last question, just to make 100% sure I understand the specification that llvm-mca is solving, and its design constraints:
{HWPort01: 3} should _always_ mean (at the instruction specification level, even if not in the llvm-mca implementation) that there are 3 cycles dispatched to either of HWPort0 or HWPort1, regardless of whether HWPort0 or HWPort1 are additionally specified, right? And so the decision to use the “reserved” flag is one way of implementing that specification, and not necessarily a reflection of any desired execution behavior beyond that?
Thanks!
-Alex