[AMDGPU] Provide a LDS bank-conflict/layout annotation option
[AMDGPU] Provide a LDS bank-conflict/layout annotation option
Summary
In AMD DSLs layouts are typically a first class concept that can statically determine the expected number of bank conflicts. For example, in Triton/Gluon here is the related code that calculates bank conflicts for a particular layout. Under normal circumstance bank conflicts are directly proportional to instruction latency/required LDS cycles. For example, here is some simple microbenchmark data produced by my agent comparing from a conflict free layout vs a 2-way conflicting layout based on the program counters.
| Kind | base (conflict-free) | conflict_length=2 |
|---|---|---|
ds_read_b64 |
2 LDS cycles | 4 LDS cycles |
ds_read_b128 |
4 LDS cycles | 8 LDS cycles |
ds_read2_b64 |
8 LDS cycles | 16 LDS cycles |
This issue proposes allowing high-level languages to provide various LDS operations with an annotation that gives layout/conflict information so that it can be used by LLVM cost modeling. As LLVM can contain additional transformations to combine or split loads, we likely need an annotation about access pattern, and simply producing the summary statistic for N in a "N-way conflict" is insufficient.
Proposed Usage
The primary usage of such an annotation is the update the expected latency from the provide cost model based on the presence or absence of bank conflicts. Currently a single latency number if hardcoded per architecture for the expected latency at lib/Target/AMDGPU/SISchedule.td. While the exact estimate may need refining as we consider more hardware details, at a minimum the presence or absence of bank conflicts should result in different latency estimates.
Those latency estimates already impact the variety of machine scheduling steps, such as the GCNSchedStrategy and AMDGPUCoExecSchedStrategy. In addition, they can impact several other cost based optimizations, such as the BranchWeightCostModel in SIPreEmitPeephole.
Candidly the actual opportunity on each of these passes will need to be determined through prototyping (although I'd expect the biggest wins to be in the register scheduling strategies) and the exact benefits are likely kernel dependent. As part of the investigation I will be exploring the impact of adding annotations to this benchmark suite and doing a detailed exploration of the cost benefits.
One additional opportunity is to identify existing transformations that do not have accurate cost estimates and integrate those to leverage this information. One such example is the SILoadStoreOptimizer on gfx950, where we transform two ds_read_b64 instructions into a single ds_read2_b64. I will open a separate PR for the foundation of this work, but in general I have found that in some cases the profitability rules need to consider bank conflicts as the ds_read2_b64 can remove a conflict (and without the conflict difference the transformation is less profitable if we aren't at risk of register spilling or losing occupancy).
For load transformation this arises from the uneven base instruction cost I've seen between load options. In the SILoadStoreOptimizer for example, we can transform two ds_read_b64 instructions into a single ds_read2_b64. This has benefits, but as we can see from the microbenchmark table it appears that as we increase the number of bank conflicts, such a transformation may not be justified due to the increased cost. I will explore this transformation in more detail in a followup PR.
Requirements
I don't want to be prescriptive with the exact annotation format since i am most familiar with Triton's linear layouts and my views may be biased. However, in general I think there are a few desirable requirements to ensure this functions as a "performance hint" and avoid degrading performance.
-
Any annotation should be possible to transform as either split or combine loads (e.g. .g. 2 ds_read_b64 -> ds_read2_b64). It is not a requirement to make this part of the load combination decision, but for eventual machine scheduling this should ultimately be accurate whichever representation is chosen, or if we cannot prove the impact of the transformation should be discarded.
-
Lane predication/control-flow needs to be well defined for the annotation, as it directly impacts the cost accuracy. In particular, if exec masking results in the perceived "conflict" not executing, then our model will be inaccurate. Our options here are typically: drop the annotation, only keep the annotation if we can statically prove the exec masking, accept the annotation as an upper bound and make it the user's job/choice to omit it, or expose the handling as a knob to the user.
-
Annotation information should not block other optimizations by default, unless we explicitly add the cost consideration as a choice (e.g. CSE, LICM). I don't expect this to be an issue, but we should be explicit that this performance hint should be a no-op except where the latency prediction is consumed.
I look forward to hearing your feedback/thoughts. I am currently building a prototype with Triton/TLX example kernels, and I will update this issue with benchmark data as it is ready.
@llvm/issue-subscribers-backend-amdgpu
Author: Nick Riasanovsky (njriasan)
[AMDGPU] Provide a LDS bank-conflict/layout annotation option
Summary
In AMD DSLs layouts are typically a first class concept that can statically determine the expected number of bank conflicts. For example, in Triton/Gluon here is the related code that calculates bank conflicts for a particular layout. Under normal circumstance bank conflicts are directly proportional to instruction latency/required LDS cycles. For example, here is some simple microbenchmark data produced by my agent comparing from a conflict free layout vs a 2-way conflicting layout based on the program counters.
| Kind | base (conflict-free) | conflict_length=2 |
|---|---|---|
ds_read_b64 |
2 cycles | 4 cycles |
ds_read_b128 |
4 cycles | 8 cycles |
ds_read2_b64 |
8 cycles | 16 cycles |
This issue proposes allowing high-level languages to provide various LDS operations with an annotation that gives layout/conflict information so that it can be used by LLVM cost modeling. As LLVM can contain additional transformations to combine or split loads, we likely need an annotation about access pattern, and simply producing the summary statistic for N in a "N-way conflict" is insufficient.
Proposed Usage
The primary usage of such an annotation is the update the expected latency from the provide cost model based on the presence or absence of bank conflicts. Currently a single latency number if hardcoded per architecture for the expected latency at lib/Target/AMDGPU/SISchedule.td. While the exact estimate may need refining as we consider more hardware details, at a minimum the presence or absence of bank conflicts should result in different latency estimates.
Those latency estimates already impact the variety of machine scheduling steps, such as the GCNSchedStrategy and AMDGPUCoExecSchedStrategy. In addition, they can impact several other cost based optimizations, such as the BranchWeightCostModel in SIPreEmitPeephole.
Candidly the actual opportunity on each of these passes will need to be determined through prototyping (although I'd expect the biggest wins to be in the register scheduling strategies) and the exact benefits are likely kernel dependent. As part of the investigation I will be exploring the impact of adding annotations to this benchmark suite and doing a detailed exploration of the cost benefits.
One additional opportunity is to identify existing transformations that do not have accurate cost estimates and integrate those to leverage this information. One such example is the SILoadStoreOptimizer on gfx950, where we transform two ds_read_b64 instructions into a single ds_read2_b64. I will open a separate PR for the foundation of this work, but in general I have found that in some cases the profitability rules need to consider bank conflicts as the ds_read2_b64 can remove a conflict (and without the conflict difference the transformation is less profitable if we aren't at risk of register spilling or losing occupancy).
For load transformation this arises from the uneven base instruction cost I've seen between load options. In the SILoadStoreOptimizer for example, we can transform two ds_read_b64 instructions into a single ds_read2_b64. This has benefits, but as we can see from the microbenchmark table it appears that as we increase the number of bank conflicts, such a transformation may not be justified due to the increased cost. I will explore this transformation in more detail in a followup PR.
Requirements
I don't want to be prescriptive with the exact annotation format since i am most familiar with Triton's linear layouts and my views may be biased. However, in general I think there are a few desirable requirements to ensure this functions as a "performance hint" and avoid degrading performance.
-
Any annotation should be possible to transform as either split or combine loads (e.g. .g. 2 ds_read_b64 -> ds_read2_b64). It is not a requirement to make this part of the load combination decision, but for eventual machine scheduling this should ultimately be accurate whichever representation is chosen, or if we cannot prove the impact of the transformation should be discarded.
-
Lane predication/control-flow needs to be well defined for the annotation, as it directly impacts the cost accuracy. In particular, if exec masking results in the perceived "conflict" not executing, then our model will be inaccurate. Our options here are typically: drop the annotation, only keep the annotation if we can statically prove the exec masking, accept the annotation as an upper bound and make it the user's job/choice to omit it, or expose the handling as a knob to the user.
-
Annotation information should not block other optimizations by default, unless we explicitly add the cost consideration as a choice (e.g. CSE, LICM). I don't expect this to be an issue, but we should be explicit that this performance hint should be a no-op except where the latency prediction is consumed.
I look forward to hearing your feedback/thoughts. I am currently building a prototype with Triton/TLX example kernels, and I will update this issue with benchmark data as it is ready.
Adding @jrbyrnes and @kerbowa to see if you have any feedback.