<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/118220>118220</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [AMDGPU] Unexpected occupancy calculation w.r.t. LDS size
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AMDGPU
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          lucas-rami
      </td>
    </tr>
</table>

<pre>
    I am struggling to understand the logic behind the computation of the occupancy with respect to the LDS size for a given function, in particular how it fits in the overall occupancy computation for the function. In particular, it seems to me that there is a discrepancy in the meaning of the achievable occupancy number computed with respect to the number of used SGPRs/VGPRs as opposed to LDS size. Current implementation of `GCNSubtarget::computeOccupancy` pasted below for future reference.

```cpp
unsigned GCNSubtarget::computeOccupancy(const Function &F, unsigned LDSSize,
                                        unsigned NumSGPRs,
                                        unsigned NumVGPRs) const {
  unsigned Occupancy =
      std::min(getMaxWavesPerEU(), getOccupancyWithLocalMemSize(LDSSize, F));
  if (NumSGPRs)
    Occupancy = std::min(Occupancy, getOccupancyWithNumSGPRs(NumSGPRs));
  if (NumVGPRs)
 Occupancy = std::min(Occupancy, getOccupancyWithNumVGPRs(NumVGPRs));
 return Occupancy;
}
```

On gfx908 for example, `getMaxWavesPerEU() == 10`. If the number of SGPRs and VGPRs used in the function is low enough to support maximum occupancy, `getOccupancyWithNumSGPRs(NumSGPRs) == getOccupancyWithNumVGPRs(NumVGPRs) == 10` as well, which makes sense to me. However, even if `LDSSize == 0` (or any low enough number so that the LDS should not restrict occupancy), `getOccupancyWithLocalMemSize(LDSSize, F) == 8`, and as a consequence `GCNSubtarget::computeOccupancy` returns 8. This seems to be because [`getOccupancyWithLocalMemSize`](https://github.com/llvm/llvm-project/blob/4a074330c6d587be51494f651f539c8055be201c/llvm/lib/Target/AMDGPU/AMDGPUSubtarget.cpp#L58-L101) performs a semantically different calculation that the two other occupancy-computing methods (which as the FIXMEs in the method suggest is perhaps not the one we should do). I would have assumed that no or low LDS usage would return the same number as `getMaxWavesPerEU()`.

I just wanted to check if I am missing something here or if there is indeed a problem in this context.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVtty2zYQ_RroZcccEtSFetCDY0WpZ-wkUydOX0FwSSIhARYXyc7XdxakLrm1buvRjGQSOLtncfZghXOq0YgbtnjFFtuZCL41dtMFKdyVFb2alaZ63tyC6MF5G5qmU7oBbyDoCq3zQlfgW4TONEpCia2aHkjTD8ELr4wGU8dHRsowCC2f4aB8CxbdgNITGL292z6AU18RamNBQKP2qKEOWhIE4zegNAzCeiVDJyy05gDKQ628ozcRf49WdN1FnMskCJZWHSETuL0EjBE8OMTeUUo9gm-Fpy0WQTkQUCknLY7IU8gehaaCTAyFbBXuRdldktWhL9FOuWD1U_LTGlNDcFjBw5v3vzvGd4_0DcKBGQZDL7w51SmBm2Atag-qHzrsUZ-rzZbpm5u3D6H0wjboWX7N8uspgXfHxNgyhUE4SqnEzhxihergg0WwWKNFLTFh6TV9lun4kcPA0uugo2wqeEEUXkijnYfdVHdgfLmjap9A7rYPD-orMn7D0mt44d9p99vQT-X6r9sfx-1rGBNlq1cR6LTkxAVYvj3FcL4aGfdKM1406O_F0yexR_ce7euPjBeMr4lng_6E8En59s5I0d1jP3IuzuxhF3esWT4moGpgvDjzW0-xv8nn-zwuCv9j6DPWN7A_Cfl4EfJ_xXs8x3v8IZ5FH6w-Bxgfs9X2UnKjAt9paOqndVpEmeKTINFTTLZMf177eFr5FjICSeC2_q7THsbm0hWMbRY7b-rro0lQ41NroDahaan9XBgGYz304kn1oT_3-TmXF5T8mNvLCvYNE7KDA3YdxTu0SrbQiy_owKF2ODpXAr-ZA-4xmhqSj6roCZPUjnARjfGC_FY_X_KciuTMyQNH22lN6CrQxpN_eaukv-S__kUJ_l7wx2wKOmp-Ew9EkN1SN-KfgVzo5YY2KspBkcCHVrmzn5cI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