-
Notifications
You must be signed in to change notification settings - Fork 14.7k
[MCA][X86] Pretend To Have a Stack Engine #153348
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,11 +36,31 @@ void X86InstrPostProcess::setMemBarriers(std::unique_ptr<Instruction> &Inst, | |
} | ||
} | ||
|
||
void X86InstrPostProcess::useStackEngine(std::unique_ptr<Instruction> &Inst, | ||
const MCInst &MCI) { | ||
if (X86::isPOP(MCI.getOpcode())) { | ||
assert(Inst->getUses().size() == 1 && | ||
"Expected pop instruction to only use stack pointer register"); | ||
Inst->getUses().clear(); | ||
} | ||
if (X86::isPUSH(MCI.getOpcode())) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NOTE: there is also PUSHF and POPF (for the purpose of this analysis, you can treat them like normal PUSH/POP). Edit: on a second thought, we don't know whether PUSHF/POPF are also optimized, or whether they force a synchronization. That needs to be tested on real hw. For now, I think you can ignore them. |
||
auto *StackRegisterUse = | ||
llvm::find_if(Inst->getUses(), [](const ReadState &State) { | ||
return State.getRegisterID() == X86::RSP; | ||
}); | ||
assert( | ||
StackRegisterUse != Inst->getUses().end() && | ||
"Expected push instruction to implicitly use stack pointer register."); | ||
Inst->getUses().erase(StackRegisterUse); | ||
} | ||
} | ||
|
||
void X86InstrPostProcess::postProcessInstruction( | ||
std::unique_ptr<Instruction> &Inst, const MCInst &MCI) { | ||
// Currently, we only modify certain instructions' IsALoadBarrier and | ||
// IsAStoreBarrier flags. | ||
setMemBarriers(Inst, MCI); | ||
useStackEngine(Inst, MCI); | ||
} | ||
|
||
} // namespace mca | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
# NOTE: Assertions have been autogenerated by utils/update_mca_test_checks.py | ||
# RUN: llvm-mca -mtriple=x86_64-unknown-unknown -mcpu=skylake -timeline -iterations=1 < %s | FileCheck %s | ||
|
||
popq %rax | ||
popq %rcx | ||
popq %rdx | ||
popq %rbx | ||
popq %r12 | ||
|
||
# CHECK: Iterations: 1 | ||
# CHECK-NEXT: Instructions: 5 | ||
# CHECK-NEXT: Total Cycles: 11 | ||
# CHECK-NEXT: Total uOps: 10 | ||
|
||
# CHECK: Dispatch Width: 6 | ||
# CHECK-NEXT: uOps Per Cycle: 0.91 | ||
# CHECK-NEXT: IPC: 0.45 | ||
# CHECK-NEXT: Block RThroughput: 2.5 | ||
|
||
# CHECK: Instruction Info: | ||
# CHECK-NEXT: [1]: #uOps | ||
# CHECK-NEXT: [2]: Latency | ||
# CHECK-NEXT: [3]: RThroughput | ||
# CHECK-NEXT: [4]: MayLoad | ||
# CHECK-NEXT: [5]: MayStore | ||
# CHECK-NEXT: [6]: HasSideEffects (U) | ||
|
||
# CHECK: [1] [2] [3] [4] [5] [6] Instructions: | ||
# CHECK-NEXT: 2 6 0.50 * popq %rax | ||
# CHECK-NEXT: 2 6 0.50 * popq %rcx | ||
# CHECK-NEXT: 2 6 0.50 * popq %rdx | ||
# CHECK-NEXT: 2 6 0.50 * popq %rbx | ||
# CHECK-NEXT: 2 6 0.50 * popq %r12 | ||
|
||
# CHECK: Resources: | ||
# CHECK-NEXT: [0] - SKLDivider | ||
# CHECK-NEXT: [1] - SKLFPDivider | ||
# CHECK-NEXT: [2] - SKLPort0 | ||
# CHECK-NEXT: [3] - SKLPort1 | ||
# CHECK-NEXT: [4] - SKLPort2 | ||
# CHECK-NEXT: [5] - SKLPort3 | ||
# CHECK-NEXT: [6] - SKLPort4 | ||
# CHECK-NEXT: [7] - SKLPort5 | ||
# CHECK-NEXT: [8] - SKLPort6 | ||
# CHECK-NEXT: [9] - SKLPort7 | ||
|
||
# CHECK: Resource pressure per iteration: | ||
# CHECK-NEXT: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] | ||
# CHECK-NEXT: - - 1.00 1.00 2.00 3.00 - 1.00 2.00 - | ||
|
||
# CHECK: Resource pressure by instruction: | ||
# CHECK-NEXT: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Instructions: | ||
# CHECK-NEXT: - - - - - 1.00 - - 1.00 - popq %rax | ||
# CHECK-NEXT: - - - - 1.00 - - 1.00 - - popq %rcx | ||
# CHECK-NEXT: - - - 1.00 - 1.00 - - - - popq %rdx | ||
# CHECK-NEXT: - - 1.00 - 1.00 - - - - - popq %rbx | ||
# CHECK-NEXT: - - - - - 1.00 - - 1.00 - popq %r12 | ||
|
||
# CHECK: Timeline view: | ||
# CHECK-NEXT: 0 | ||
# CHECK-NEXT: Index 0123456789 | ||
|
||
# CHECK: [0,0] DeeeeeeER . popq %rax | ||
# CHECK-NEXT: [0,1] DeeeeeeER . popq %rcx | ||
# CHECK-NEXT: [0,2] D=eeeeeeER. popq %rdx | ||
# CHECK-NEXT: [0,3] .DeeeeeeER. popq %rbx | ||
# CHECK-NEXT: [0,4] .D=eeeeeeER popq %r12 | ||
|
||
# CHECK: Average Wait times (based on the timeline view): | ||
# CHECK-NEXT: [0]: Executions | ||
# CHECK-NEXT: [1]: Average time spent waiting in a scheduler's queue | ||
# CHECK-NEXT: [2]: Average time spent waiting in a scheduler's queue while ready | ||
# CHECK-NEXT: [3]: Average time elapsed from WB until retire stage | ||
|
||
# CHECK: [0] [1] [2] [3] | ||
# CHECK-NEXT: 0. 1 1.0 1.0 0.0 popq %rax | ||
# CHECK-NEXT: 1. 1 1.0 1.0 0.0 popq %rcx | ||
# CHECK-NEXT: 2. 1 2.0 2.0 0.0 popq %rdx | ||
# CHECK-NEXT: 3. 1 1.0 1.0 0.0 popq %rbx | ||
# CHECK-NEXT: 4. 1 2.0 2.0 0.0 popq %r12 | ||
# CHECK-NEXT: 1 1.4 1.4 0.0 <total> |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
# NOTE: Assertions have been autogenerated by utils/update_mca_test_checks.py | ||
# RUN: llvm-mca -mtriple=x86_64-unknown-unknown -mcpu=skylake -timeline -iterations=1 < %s | FileCheck %s | ||
|
||
pushq %rax | ||
pushq %rcx | ||
pushq %rdx | ||
pushq %rbx | ||
pushq %r12 | ||
|
||
# CHECK: Iterations: 1 | ||
# CHECK-NEXT: Instructions: 5 | ||
# CHECK-NEXT: Total Cycles: 9 | ||
# CHECK-NEXT: Total uOps: 15 | ||
|
||
# CHECK: Dispatch Width: 6 | ||
# CHECK-NEXT: uOps Per Cycle: 1.67 | ||
# CHECK-NEXT: IPC: 0.56 | ||
# CHECK-NEXT: Block RThroughput: 5.0 | ||
|
||
# CHECK: Instruction Info: | ||
# CHECK-NEXT: [1]: #uOps | ||
# CHECK-NEXT: [2]: Latency | ||
# CHECK-NEXT: [3]: RThroughput | ||
# CHECK-NEXT: [4]: MayLoad | ||
# CHECK-NEXT: [5]: MayStore | ||
# CHECK-NEXT: [6]: HasSideEffects (U) | ||
|
||
# CHECK: [1] [2] [3] [4] [5] [6] Instructions: | ||
# CHECK-NEXT: 3 2 1.00 * pushq %rax | ||
# CHECK-NEXT: 3 2 1.00 * pushq %rcx | ||
# CHECK-NEXT: 3 2 1.00 * pushq %rdx | ||
# CHECK-NEXT: 3 2 1.00 * pushq %rbx | ||
# CHECK-NEXT: 3 2 1.00 * pushq %r12 | ||
|
||
# CHECK: Resources: | ||
# CHECK-NEXT: [0] - SKLDivider | ||
# CHECK-NEXT: [1] - SKLFPDivider | ||
# CHECK-NEXT: [2] - SKLPort0 | ||
# CHECK-NEXT: [3] - SKLPort1 | ||
# CHECK-NEXT: [4] - SKLPort2 | ||
# CHECK-NEXT: [5] - SKLPort3 | ||
# CHECK-NEXT: [6] - SKLPort4 | ||
# CHECK-NEXT: [7] - SKLPort5 | ||
# CHECK-NEXT: [8] - SKLPort6 | ||
# CHECK-NEXT: [9] - SKLPort7 | ||
|
||
# CHECK: Resource pressure per iteration: | ||
# CHECK-NEXT: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] | ||
# CHECK-NEXT: - - 1.00 1.00 1.00 2.00 5.00 1.00 2.00 2.00 | ||
|
||
# CHECK: Resource pressure by instruction: | ||
# CHECK-NEXT: [0] [1] [2] [3] [4] [5] [6] [7] [8] [9] Instructions: | ||
# CHECK-NEXT: - - - - - - 1.00 - 1.00 1.00 pushq %rax | ||
# CHECK-NEXT: - - - - - 1.00 1.00 1.00 - - pushq %rcx | ||
# CHECK-NEXT: - - - 1.00 1.00 - 1.00 - - - pushq %rdx | ||
# CHECK-NEXT: - - 1.00 - - - 1.00 - - 1.00 pushq %rbx | ||
# CHECK-NEXT: - - - - - 1.00 1.00 - 1.00 - pushq %r12 | ||
|
||
# CHECK: Timeline view: | ||
# CHECK-NEXT: Index 012345678 | ||
|
||
# CHECK: [0,0] DeeER. . pushq %rax | ||
# CHECK-NEXT: [0,1] D=eeER . pushq %rcx | ||
# CHECK-NEXT: [0,2] .D=eeER . pushq %rdx | ||
# CHECK-NEXT: [0,3] .D==eeER. pushq %rbx | ||
# CHECK-NEXT: [0,4] . D==eeER pushq %r12 | ||
|
||
# CHECK: Average Wait times (based on the timeline view): | ||
# CHECK-NEXT: [0]: Executions | ||
# CHECK-NEXT: [1]: Average time spent waiting in a scheduler's queue | ||
# CHECK-NEXT: [2]: Average time spent waiting in a scheduler's queue while ready | ||
# CHECK-NEXT: [3]: Average time elapsed from WB until retire stage | ||
|
||
# CHECK: [0] [1] [2] [3] | ||
# CHECK-NEXT: 0. 1 1.0 1.0 0.0 pushq %rax | ||
# CHECK-NEXT: 1. 1 2.0 1.0 0.0 pushq %rcx | ||
# CHECK-NEXT: 2. 1 2.0 1.0 0.0 pushq %rdx | ||
# CHECK-NEXT: 3. 1 3.0 1.0 0.0 pushq %rbx | ||
# CHECK-NEXT: 4. 1 3.0 1.0 0.0 pushq %r12 | ||
# CHECK-NEXT: 1 2.2 1.0 0.0 <total> |
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A use of RSP is "free" only if the definition of RSP comes from another PUSH/POP/RET. That is because the stack engine only tracks RSP updates from those known stack opcodes. The stack engine computes a delta between the new RSP value and the old RSP value. That delta is then used as an implicit addend operand to other RSP reads performed by other stack operations.
The first time a stack operation is encountered, RSP should be treated like a normal read dependency, and we would pay the full cost of waiting on RSP. Any other following stack operations will be able to eliminate their dependency from RSP.
A write to RSP which isn't from a stack operation would force a synchronization. So, the next stack operation won't be able to benefit from that optimization. We would basically need to go back to square one.
In your case:
Bottom line: your change is mostly OK. However, you need to add some sort of post-processing state/context;
as you post-process instructions, you should record where the last definition of RSP came from. Your logic would only eliminate the dependency if the definition was coming from a known stack operation.
I hope it makes sense.
-Andrea