llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MLIR] convert affine for to gpu lowering pipeline encounter error

Open youbenye opened this issue 4 years ago • 3 comments

Hi, i am trying to lowering from affine.for to gpu.launch, but got an assertsion "error: 'affine.for' op operand cannot be used as a dimension id "

Here is a simple testcase to reproduce it:

func.func @serving_default(%arg0: memref<2x3xf32> {tf_saved_model.index_path = ["b"]}, %arg1: memref<2x3xf32> {tf_saved_model.index_path = ["a"]}, %arg2: memref<2x3xf32> {tf_saved_model.index_path = ["c"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "b:0,a:0", outputs = "Add:0"}, tf_saved_model.exported_names = ["serving_default"]} {
   %0 = memref.alloc() {alignment = 128 : i64} : memref<2x3xf32>
   affine.for %arg3 = 0 to 2 {
     affine.for %arg4 = 0 to 3 {
       affine.for %arg5 = affine_map<(d0) -> (d0)>(%arg3) to affine_map<(d0) -> (d0 + 1)>(%arg3) {
         affine.for %arg6 = affine_map<(d0) -> (d0)>(%arg4) to affine_map<(d0) -> (d0 + 1)>(%arg4) {
           %1 = affine.load %arg1[%arg5, %arg6] : memref<2x3xf32>
           %2 = affine.load %arg0[%arg5, %arg6] : memref<2x3xf32>
           %3 = arith.addf %1, %2 : f32
           affine.store %3, %0[%arg5, %arg6] : memref<2x3xf32>
         }
       }
     }
   }
   memref.copy %0, %arg2 : memref<2x3xf32> to memref<2x3xf32>
   return
 }

here is full traced error log:

 loc("./simple_add/simple_add.mlir":3:10): error: 'affine.for' op operand cannot be used as a dimension id
 // -----// IR Dump After ConvertAffineForToGPU Failed (convert-affine-for-to-gpu) //----- //
 "func.func"() ({
 ^bb0(%arg0: memref<2x3xf32>, %arg1: memref<2x3xf32>, %arg2: memref<2x3xf32>):
   %0 = "memref.alloc"() {alignment = 128 : i64, operand_segment_sizes = array<i32: 0, 0>} : () -> memref<2x3xf32>
   %1 = "arith.constant"() {value = 0 : index} : () -> index
   %2 = "arith.constant"() {value = 2 : index} : () -> index
   %3 = "arith.subi"(%2, %1) : (index, index) -> index
   %4 = "arith.constant"() {value = 1 : index} : () -> index
   %5 = "arith.constant"() {value = 0 : index} : () -> index
   %6 = "arith.constant"() {value = 3 : index} : () -> index
   %7 = "arith.subi"(%6, %5) : (index, index) -> index
   %8 = "arith.constant"() {value = 1 : index} : () -> index
   %9 = "arith.constant"() {value = 1 : index} : () -> index
   "gpu.launch"(%3, %9, %9, %7, %9, %9) ({
   ^bb0(%arg3: index, %arg4: index, %arg5: index, %arg6: index, %arg7: index, %arg8: index, %arg9: index, %arg10: index, %arg11: index, %arg12: index, %arg13: index, %arg14:     index):
     %10 = "arith.addi"(%1, %arg3) : (index, index) -> index
     %11 = "arith.addi"(%5, %arg6) : (index, index) -> index
     "affine.for"(%10, %10) ({
     ^bb0(%arg15: index):
       "affine.for"(%11, %11) ({
       ^bb0(%arg16: index):
         %12 = "affine.load"(%arg1, %arg15, %arg16) {map = affine_map<(d0, d1) -> (d0, d1)>} : (memref<2x3xf32>, index, index) -> f32
         %13 = "affine.load"(%arg0, %arg15, %arg16) {map = affine_map<(d0, d1) -> (d0, d1)>} : (memref<2x3xf32>, index, index) -> f32
         %14 = "arith.addf"(%12, %13) : (f32, f32) -> f32
         "affine.store"(%14, %0, %arg15, %arg16) {map = affine_map<(d0, d1) -> (d0, d1)>} : (f32, memref<2x3xf32>, index, index) -> ()
         "affine.yield"() : () -> ()
       }) {lower_bound = affine_map<(d0) -> (d0)>, step = 1 : index, upper_bound = affine_map<(d0) -> (d0 + 1)>} : (index, index) -> ()
       "affine.yield"() : () -> ()
     }) {lower_bound = affine_map<(d0) -> (d0)>, step = 1 : index, upper_bound = affine_map<(d0) -> (d0 + 1)>} : (index, index) -> ()
     "gpu.terminator"() : () -> ()
   }) {operand_segment_sizes = array<i32: 0, 1, 1, 1, 1, 1, 1, 0>} : (index, index, index, index, index, index) -> ()
   "memref.copy"(%0, %arg2) : (memref<2x3xf32>, memref<2x3xf32>) -> ()
   "func.return"() : () -> ()
 }) {arg_attrs = [{tf_saved_model.index_path = ["b"]}, {tf_saved_model.index_path = ["a"]}, {tf_saved_model.index_path = ["c"]}], function_type = (memref<2x3xf32>, memref<2x3    xf32>, memref<2x3xf32>) -> (), sym_name = "serving_default", tf.entry_function = {control_outputs = "", inputs = "b:0,a:0", outputs = "Add:0"}, tf_saved_model.exported_names     = ["serving_default"]} : () -> ()

I am wondering whether additional settings are required before call convert-affine-for-to-gpu. It would be very nice if anyone could help me!

youbenye avatar Sep 09 '22 09:09 youbenye

@llvm/issue-subscribers-mlir

llvmbot avatar Sep 09 '22 14:09 llvmbot

I confirmed that this crash is fixed by #121058.

Men-cotton avatar May 19 '26 13:05 Men-cotton

@llvm/issue-subscribers-mlir-gpu

Author: youbenye

Hi, i am trying to lowering from affine.for to gpu.launch, but got an assertsion `"error: 'affine.for' op operand cannot be used as a dimension id "`

Here is a simple testcase to reproduce it:

func.func @<!-- -->serving_default(%arg0: memref&lt;2x3xf32&gt; {tf_saved_model.index_path = ["b"]}, %arg1: memref&lt;2x3xf32&gt; {tf_saved_model.index_path = ["a"]}, %arg2: memref&lt;2x3xf32&gt; {tf_saved_model.index_path = ["c"]}) attributes {tf.entry_function = {control_outputs = "", inputs = "b:0,a:0", outputs = "Add:0"}, tf_saved_model.exported_names = ["serving_default"]} {
   %0 = memref.alloc() {alignment = 128 : i64} : memref&lt;2x3xf32&gt;
   affine.for %arg3 = 0 to 2 {
     affine.for %arg4 = 0 to 3 {
       affine.for %arg5 = affine_map&lt;(d0) -&gt; (d0)&gt;(%arg3) to affine_map&lt;(d0) -&gt; (d0 + 1)&gt;(%arg3) {
         affine.for %arg6 = affine_map&lt;(d0) -&gt; (d0)&gt;(%arg4) to affine_map&lt;(d0) -&gt; (d0 + 1)&gt;(%arg4) {
           %1 = affine.load %arg1[%arg5, %arg6] : memref&lt;2x3xf32&gt;
           %2 = affine.load %arg0[%arg5, %arg6] : memref&lt;2x3xf32&gt;
           %3 = arith.addf %1, %2 : f32
           affine.store %3, %0[%arg5, %arg6] : memref&lt;2x3xf32&gt;
         }
       }
     }
   }
   memref.copy %0, %arg2 : memref&lt;2x3xf32&gt; to memref&lt;2x3xf32&gt;
   return
 }

here is full traced error log:

 loc("./simple_add/simple_add.mlir":3:10): error: 'affine.for' op operand cannot be used as a dimension id
 // -----// IR Dump After ConvertAffineForToGPU Failed (convert-affine-for-to-gpu) //----- //
 "func.func"() ({
 ^bb0(%arg0: memref&lt;2x3xf32&gt;, %arg1: memref&lt;2x3xf32&gt;, %arg2: memref&lt;2x3xf32&gt;):
   %0 = "memref.alloc"() {alignment = 128 : i64, operand_segment_sizes = array&lt;i32: 0, 0&gt;} : () -&gt; memref&lt;2x3xf32&gt;
   %1 = "arith.constant"() {value = 0 : index} : () -&gt; index
   %2 = "arith.constant"() {value = 2 : index} : () -&gt; index
   %3 = "arith.subi"(%2, %1) : (index, index) -&gt; index
   %4 = "arith.constant"() {value = 1 : index} : () -&gt; index
   %5 = "arith.constant"() {value = 0 : index} : () -&gt; index
   %6 = "arith.constant"() {value = 3 : index} : () -&gt; index
   %7 = "arith.subi"(%6, %5) : (index, index) -&gt; index
   %8 = "arith.constant"() {value = 1 : index} : () -&gt; index
   %9 = "arith.constant"() {value = 1 : index} : () -&gt; index
   "gpu.launch"(%3, %9, %9, %7, %9, %9) ({
   ^bb0(%arg3: index, %arg4: index, %arg5: index, %arg6: index, %arg7: index, %arg8: index, %arg9: index, %arg10: index, %arg11: index, %arg12: index, %arg13: index, %arg14:     index):
     %10 = "arith.addi"(%1, %arg3) : (index, index) -&gt; index
     %11 = "arith.addi"(%5, %arg6) : (index, index) -&gt; index
     "affine.for"(%10, %10) ({
     ^bb0(%arg15: index):
       "affine.for"(%11, %11) ({
       ^bb0(%arg16: index):
         %12 = "affine.load"(%arg1, %arg15, %arg16) {map = affine_map&lt;(d0, d1) -&gt; (d0, d1)&gt;} : (memref&lt;2x3xf32&gt;, index, index) -&gt; f32
         %13 = "affine.load"(%arg0, %arg15, %arg16) {map = affine_map&lt;(d0, d1) -&gt; (d0, d1)&gt;} : (memref&lt;2x3xf32&gt;, index, index) -&gt; f32
         %14 = "arith.addf"(%12, %13) : (f32, f32) -&gt; f32
         "affine.store"(%14, %0, %arg15, %arg16) {map = affine_map&lt;(d0, d1) -&gt; (d0, d1)&gt;} : (f32, memref&lt;2x3xf32&gt;, index, index) -&gt; ()
         "affine.yield"() : () -&gt; ()
       }) {lower_bound = affine_map&lt;(d0) -&gt; (d0)&gt;, step = 1 : index, upper_bound = affine_map&lt;(d0) -&gt; (d0 + 1)&gt;} : (index, index) -&gt; ()
       "affine.yield"() : () -&gt; ()
     }) {lower_bound = affine_map&lt;(d0) -&gt; (d0)&gt;, step = 1 : index, upper_bound = affine_map&lt;(d0) -&gt; (d0 + 1)&gt;} : (index, index) -&gt; ()
     "gpu.terminator"() : () -&gt; ()
   }) {operand_segment_sizes = array&lt;i32: 0, 1, 1, 1, 1, 1, 1, 0&gt;} : (index, index, index, index, index, index) -&gt; ()
   "memref.copy"(%0, %arg2) : (memref&lt;2x3xf32&gt;, memref&lt;2x3xf32&gt;) -&gt; ()
   "func.return"() : () -&gt; ()
 }) {arg_attrs = [{tf_saved_model.index_path = ["b"]}, {tf_saved_model.index_path = ["a"]}, {tf_saved_model.index_path = ["c"]}], function_type = (memref&lt;2x3xf32&gt;, memref&lt;2x3    xf32&gt;, memref&lt;2x3xf32&gt;) -&gt; (), sym_name = "serving_default", tf.entry_function = {control_outputs = "", inputs = "b:0,a:0", outputs = "Add:0"}, tf_saved_model.exported_names     = ["serving_default"]} : () -&gt; ()

I am wondering whether additional settings are required before call convert-affine-for-to-gpu. It would be very nice if anyone could help me!