<table border="1" cellspacing="0" cellpadding="8">
<tr>
<th>Issue</th>
<td>
<a href=https://github.com/llvm/llvm-project/issues/123291>123291</a>
</td>
</tr>
<tr>
<th>Summary</th>
<td>
Conversion of Affine Loops to GPU Dialect Fails with 'Invalid Dimension or Symbol Identifier'
</td>
</tr>
<tr>
<th>Labels</th>
<td>
new issue
</td>
</tr>
<tr>
<th>Assignees</th>
<td>
</td>
</tr>
<tr>
<th>Reporter</th>
<td>
lhw414
</td>
</tr>
</table>
<pre>
## Description
While attempting to convert an affine loop-based MLIR to the GPU dialect using the `convert-affine-for-to-gpu` pass, I encounter the following error:
```plaintext
sample.mlir:8:14: error: 'affine.load' op index must be a valid dimension or symbol identifier
%0 = affine.load %arg0[%arg1, %arg2] : memref<6x12xf32>
^
sample.mlir:8:14: note: see current operation: %8 = "affine.load"(%arg0, %7, %arg13) <{map = affine_map<(d0, d1) -> (d0, d1)>}> : (memref<6x12xf32>, index, index) -> f32
```
The mlir-opt command used to reproduce this issue is:
```bash
../build/bin/mlir-opt sample.mlir -o sample_output.mlir \
-pass-pipeline="builtin.module(func.func(convert-affine-for-to-gpu{gpu-block-dims=1 gpu-thread-dims=0}))"
```
Here is the original input MLIR:
```mlir
module {
memref.global "private" @global_seed : memref<i64> = dense<0>
func.func @main(%arg0: memref<6x12xf32>) -> memref<6x12xf32> {
%cst = arith.constant 0.000000e+00 : f32
%alloc = memref.alloc() {alignment = 64 : i64} : memref<6x12xf32>
affine.for %arg1 = 0 to 6 {
affine.for %arg2 = 0 to 12 {
%0 = affine.load %arg0[%arg1, %arg2] : memref<6x12xf32>
}
}
return %alloc : memref<6x12xf32>
}
}
```
The resulting intermediate IR dump after the failure is as follows:
```mlir
// -----// IR Dump After ConvertAffineForToGPU Failed (convert-affine-for-to-gpu) //-----
"func.func"() <{function_type = (memref<6x12xf32>) -> memref<6x12xf32>, sym_name = "main"}> ({
^bb0(%arg0: memref<6x12xf32>):
%0 = "arith.constant"() <{value = 0.000000e+00 : f32}> : () -> f32
%1 = "memref.alloc"() <{alignment = 64 : i64, operandSegmentSizes = array<i32: 0, 0>}> : () -> memref<6x12xf32>
%2 = "arith.constant"() <{value = 0 : index}> : () -> index
%3 = "arith.constant"() <{value = 6 : index}> : () -> index
%4 = "arith.subi"(%3, %2) <{overflowFlags = #arith.overflow<none>}> : (index, index) -> index
%5 = "arith.constant"() <{value = 1 : index}> : () -> index
%6 = "arith.constant"() <{value = 1 : index}> : () -> index
"gpu.launch"(%4, %6, %6, %6, %6, %6) <{operandSegmentSizes = array<i32: 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0>}> ({
^bb0(%arg1: index, %arg2: index, %arg3: index, %arg4: index, %arg5: index, %arg6: index, %arg7: index, %arg8: index, %arg9: index, %arg10: index, %arg11: index, %arg12: index):
%7 = "arith.addi"(%2, %arg1) <{overflowFlags = #arith.overflow<none>}> : (index, index) -> index
"affine.for"() <{lowerBoundMap = affine_map<() -> (0)>, operandSegmentSizes = array<i32: 0, 0, 0>, step = 1 : index, upperBoundMap = affine_map<() -> (12)>}> ({
^bb0(%arg13: index):
%8 = "affine.load"(%arg0, %7, %arg13) <{map = affine_map<(d0, d1) -> (d0, d1)>}> : (memref<6x12xf32>, index, index) -> f32
"affine.yield"() : () -> ()
}) : () -> ()
"gpu.terminator"() : () -> ()
}) {workgroup_attributions = 0 : i64} : (index, index, index, index, index, index) -> ()
"func.return"(%1) : (memref<6x12xf32>) -> ()
}) : () -> ()
```
## Questions
1. Is there an issue with the original MLIR input?
- Are there any preconditions or required passes that I missed before applying convert-affine-for-to-gpu?
2. Could this be a problem in the convert-affine-for-to-gpu implementation?
- The error suggests that the indices for affine.load are not considered valid dimensions or symbols. However, %arg1 and %arg2 are induction variables of affine.for loops, which are typically valid.
3. What additional passes should be applied before convert-affine-for-to-gpu?
- For instance, should I run canonicalize, lower-affine, or similar passes to simplify the IR and ensure compatibility?
4. Are there any reference backends or examples for converting MLIR with linalg or affine dialects to the GPU dialect?
- I am particularly interested in examples or documentation that describe the process and highlight best practices for such conversions.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUWE1v4zjS_jXMpWBBomzLOfjg2O13DMwA73bPYo4BJZUlblOklh9Je379gqQsy4mdnhxmgTUCWymRxfp4qlhVzBjeSMQ1WTyRxe6BOdsqvRbt6zybP5SqPq0JzQnNYYem0ry3XEmSbki6-aPlAoFZi11vuWzAKqiUfEFtgUlgxyOXCEKpflYygzX89uvhq19kW4T_-_9_Qs2ZwMqCM2F3i0CW6cBhFrfPjkrPrJo1vSPLFHpmDKFbOADKSjlpUYd9RyWEevVcUGulSb6JIpJlGv96wbi0-MOSdGNY1wtMOsH9whXJN9mc5JtxKxBaxNMToVhNaAGqBy5r_AGdMxZKBAYvTPAaat6hNFxJUBrMqSuVAF6jtPzIUZN0A8OH0EUKJN_BhLMnMt2k3vThKfO6xUdKFjvwwnTYaTySfLv8kdEfx5yS_MuEb2S--PKBXlJZ9L8GESqnNUoLqkfNgiuDvotVkI1QeqU4JXR1ljFKVlwkzHJCH4HkW1I8dayfKPfcsd7T6aoO--rMr5yR_Atc07wqxS7Qgxirm8rSbTT-5GHg5hdMnBx9_nuL4G0wU72FSnUdkzU4jz-rQGOvVe0qBNtyA9wYh8DNe8CUzLQk3SQJofvScVH7Xy4J3Y-8J_aGmRr-fVbO9s5GKllsSbqZedTOet6j4BJJviOUepaWy6RTtRNI6OroZJX4L0JX90OgeGp6NyuFqr7Pat4Zku8y8CTbamT1mZZ6q9LH8HfDQr-g9kqHyFGaN1wyAVz2zoYQfW-MAKl0E4UFUjwFAEZnJY1QJRMeO73mL8wioRTIPI30Z4NYXwOZL-fR5TuoURok-TY9g3o0g-fQMW_wEYH3guGMhlsvR2FDBFbGRphqbtukUtJYJi2kSRo-SOhTmgZhI7KGbUwIVYWNg8qBECR79AcwwRvZ-bDya5bzwMFrWfw0hIdwOyp9jqrAI_VYXU6Ev7GSXlZm9Grp35VtPKoGo8QnjdZpOTXRBxzinuH7fchqNE6Ea8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