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

    <tr>
        <th>Summary</th>
        <td>
            --vector-to-rocdl broken on gfx1030 backend
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            bug,
            backend:AMDGPU,
            mlir:gpu,
            mlir:vectorops,
            mlir
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          krzysz00
      </td>
    </tr>
</table>

<pre>
    # Symptoms
```
$ ninja check-mlir 
[0/1] Running the MLIR regression tests
-- Testing: 1488 tests, 32 workers --
Testing:  0.. 10.. 20.. 30.. 
FAIL: MLIR :: Integration/GPU/ROCM/vector-transferops.mlir (91 of 1488)
******************** TEST 'MLIR :: Integration/GPU/ROCM/vector-transferops.mlir' FAILED ********************
Script:
--
: 'RUN: at line 1';   /home/kdrewnia/llvm-project/build/bin/mlir-opt /home/kdrewnia/llvm-project/mlir/test/Integration/GPU/ROCM/vector-transferops.mlir    -convert-scf-to-cf    -gpu-kernel-outlining    -pass-pipeline='gpu.module(strip-debuginfo,convert-gpu-to-rocdl,gpu-to-hsaco{chip=gfx1030})'    -gpu-to-llvm  | /home/kdrewnia/llvm-project/build/bin/mlir-cpu-runner    --shared-libs=/home/kdrewnia/llvm-project/build/lib/libmlir_rocm_runtime.so    --shared-libs=/home/kdrewnia/llvm-project/build/lib/libmlir_runner_utils.so    --entry-point-result=void  | /home/kdrewnia/llvm-project/build/bin/FileCheck /home/kdrewnia/llvm-project/mlir/test/Integration/GPU/ROCM/vector-transferops.mlir
--
Exit Code: 1

Command Output (stderr):
--
/home/kdrewnia/llvm-project/mlir/test/Integration/GPU/ROCM/vector-transferops.mlir:82:12: error: CHECK: expected string not found in input
 // CHECK: [1.23, 2.46, 2.46, 1.23]
           ^
<stdin>:1:1: note: scanning from here
Unranked Memref base@ = 0x75ad310 rank = 1 offset = 0 sizes = [4] strides = [1] data = 
^
<stdin>:2:1: note: possible intended match here
[1.23, 1.23, 1.23, 1.23]
^

Input file: <stdin>
Check file: /home/kdrewnia/llvm-project/mlir/test/Integration/GPU/ROCM/vector-transferops.mlir

-dump-input=help explains the following input dump.

Input was:
<<<<<<
            1: Unranked Memref base@ = 0x75ad310 rank = 1 offset = 0 sizes = [4] strides = [1] data =  
check:82'0     X~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ error: no match found
            2: [1.23, 1.23, 1.23, 1.23] 
check:82'0     ~~~~~~~~~~~~~~~~~~~~~~~~~
check:82'1     ?                         possible intended match
            3: Unranked Memref base@ = 0x75ad310 rank = 1 offset = 0 sizes = [4] strides = [1] data =  
check:82'0     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
            4: [1.23, 1.23, 1.23, 1.23] 
check:82'0     ~~~~~~~~~~~~~~~~~~~~~~~~~
>>>>>>

--

********************
Testing:  0.. 10.. 20.. 30.. 40.. 50.. 60.. 70.. 80.. 90..
********************
Failed Tests (1):
  MLIR :: Integration/GPU/ROCM/vector-transferops.mlir


Testing Time: 26.89s
  Unsupported:  181
  Passed     : 1365
  Failed     :    1
```

Note that only the transferops test fails - all other ROCm integration tests pass.

# The likely cause

In `--vector-to-rocdl`, the buffer description structure that is needed for certain memory instructions is defined using [certain magic constants](https://github.com/llvm/llvm-project/blob/main/mlir/lib/Conversion/VectorToROCDL/VectorToROCDL.cpp#L113)

These magic constants are correct for gfx9xx GPUs. However, the appropriate values of these constants is now backend-dependent. Specifically
- For gfx10xx chips (RDNA 2), bit 120 in the buffer descriptor must be 1. See [the RDNA manual, table 37 on page 74 (big PDF)](https://developer.amd.com/wp-content/resources/RDNA_Shader_ISA.pdf)
- For gfx9 chips, such as the CDNA line, bit 120 is reserved and must be 0. See [the CDNA v2, table 36 on page 74](https://developer.amd.com/wp-content/resources/CDNA2_Shader_ISA_18November2021.pdf)

This means that the intrinsic calls emitted by `--vector-to-rocdl` will be incorrect on one of the two backends (going off of my interpretation of the documentation as written).

Since `--vector-to-rocdl` doesn't know about the target GPU, there's no obvious way around this.

In the AMDGPU dialect, we use a `targetIsRDNA` attribute to determine whether to set or clear bit 120, but this isn't really a general solution. https://github.com/llvm/llvm-project/blob/main/mlir/lib/Conversion/AMDGPUToROCDL/AMDGPUToROCDL.cpp#L166

# Potential fixes

1. The AMDGPU backend gains additional intrinsics that handle setting/clearing that bit at issue depending on the compilation target.
2. It turns out the documentation isn't quite correct and we could get away with setting the bit to 1 everywhere. This would require getting some clarity out of the hardware people though.
3. Remove this `--vector-to-rocdl` conversion.

# Configuration notes
**System:** Ubuntu 20.04, ROCm 5.2.0 installed from AMD repositories
**Build flags:** 
``
`-DCMAKE_CXX_COMPILER=/opt/rocm/llvm/bin/clang++ -DCMAKE_C_COMPILER=/opt/rocm/llvm/bin/clang -DLLVM_ENABLE_PROJECTS=mlir -DLLVM_TARGETS_TO_BUILD=X86\;AMDGPU -DLLVM_ENABLE_ASSERTIONS=ON -DMLIR_ENABLE_ROCM_RUNNER=ON -DMLIR_ENABLE_ROCM_CONVERSIONS=ON -DMLIR_ROCM_RUNNER_ENABLED=ON -DMLIR_INCLUDE_INTEGRATION_TESTS=ON -DCMAKE_BUILD_TYPE=Debug
```

```
$ rocm_agent_enumerator
gfx000
gfx1030
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJztWcly4zgS_Rr5giCDi9aDD7YkV3vaW0hyRc1JAZKQxDZJcADQsurr-yVIavEy011VU1OHUdDgAiCRmXi5wZFMduedIGTzXV4ameuON-l4F52-11z1a9BlRVr8wVm8EfGTk2epYk1X79LrBFd-pzdhs6rAqDUzG8Fub65nTIm1ElqnsmBGaNMQdxy2wBtGdsIL5neHw6Y3GLM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