Repository navigation
Closed
Labels
bug 🐞Something isn't working
Description
What happened?
A pattern FuseTilableForallConsumers seems to be caught in a loop of creating ops and failing, causing a hang.
debug print looks like repeated sequences of:
//===-------------------------------------------===//
Processing operation : 'linalg.generic'(0x598460fbecb0) {
* Pattern mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers : 'linalg.generic -> ()' {
[pattern-application:1] PatternApplicator.cpp:209 Trying to match "mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers"
[pattern-logging-listener:1] PatternLoggingListener.cpp:10 mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers | notifyOperationInserted | linalg.generic
** Insert : 'linalg.generic'(0x598460fbecb0)
[pattern-logging-listener:1] PatternLoggingListener.cpp:10 mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers | notifyOperationInserted | tensor.expand_shape
** Insert : 'tensor.expand_shape'(0x5984610a4730)
[pattern-logging-listener:1] PatternLoggingListener.cpp:10 mlir::iree_compiler::(anonymous namespace)::FuseTilableForallConsumers | notifyOperationInserted | tensor.dim
** Insert : 'tensor.dim'(0x598461082100)
** Match Failure : could not fetch consumer to fuse
[pattern-application:1] PatternApplicator.cpp:224 -> matchAndRewrite failed
} -> failure : pattern failed to match
} -> failure : all patterns failed to match
//===-------------------------------------------===//
Steps to reproduce your issue
Apply
iree-opt --pass-pipeline="builtin.module(func.func(iree-codegen-combine-layout-transformation))" --debug
to:
func.func @conv_2d_bfloat16_forward_16x225x225x64_nhwc_64x3x1x64_fhwc_nhwf_1x1s_1x0p_1x1d_1g$async_dispatch_0_conv_16x225x225x64x3x64_bf16xbf16xf32() attributes {translation_info = #iree_codegen.translation_info<pipeline = LLVMGPUTileAndFuse workgroup_size = [256, 1, 1] subgroup_size = 64, {gpu_pipeline_options = #iree_gpu.pipeline_options<prefetch_shared_memory = true, use_igemm_convolution = true>, llvm_func_attrs = {"amdgpu-waves-per-eu" = "2"}}>} {
%c256 = arith.constant 256 : index
%c512 = arith.constant 512 : index
%c3 = arith.constant 3 : index
%c1 = arith.constant 1 : index
%cst = arith.constant 0.000000e+00 : bf16
%cst_0 = arith.constant 0.000000e+00 : f32
%c0 = arith.constant 0 : index
%0 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(0) alignment(64) offset(%c0) flags("ReadOnly|Indirect") {iree_gpu.use_rocdl_buffer_instructions} : !iree_tensor_ext.dispatch.tensor<readonly:tensor<16x225x225x64xbf16>>
%1 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(1) alignment(64) offset(%c0) flags("ReadOnly|Indirect") : !iree_tensor_ext.dispatch.tensor<readonly:tensor<64x192xbf16>>
%2 = hal.interface.binding.subspan layout(<bindings = [#hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, "ReadOnly|Indirect">, #hal.pipeline.binding<storage_buffer, Indirect>], flags = Indirect>) binding(2) alignment(64) offset(%c0) flags(Indirect) {iree_gpu.use_rocdl_buffer_instructions} : !iree_tensor_ext.dispatch.tensor<writeonly:tensor<16x225x225x64xbf16>>
%3 = iree_tensor_ext.dispatch.tensor.load %0, offsets = [0, 0, 0, 0], sizes = [16, 225, 225, 64], strides = [1, 1, 1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<16x225x225x64xbf16>> -> tensor<16x225x225x64xbf16>
%4 = iree_tensor_ext.dispatch.tensor.load %1, offsets = [0, 0], sizes = [64, 192], strides = [1, 1] : !iree_tensor_ext.dispatch.tensor<readonly:tensor<64x192xbf16>> -> tensor<64x192xbf16>
%5 = tensor.empty() : tensor<16x225x225x64xbf16>
%6 = scf.forall (%arg0, %arg1, %arg2) in (4, 225, 15) shared_outs(%arg3 = %5) -> (tensor<16x225x225x64xbf16>) {
%7 = affine.apply affine_map<(d0) -> (d0 * 16)>(%arg2)
%8 = affine.apply affine_map<(d0) -> (d0 * 4)>(%arg0)
%9 = affine.min affine_map<(d0) -> (d0 * -16 + 225, 16)>(%arg2)
%extracted_slice = tensor.extract_slice %arg3[%8, %arg1, %7, 0] [4, 1, %9, 64] [1, 1, 1, 1] : tensor<16x225x225x64xbf16> to tensor<4x1x?x64xbf16>
%10 = affine.min affine_map<(d0) -> (d0 * -16 + 225, 0, 16)>(%arg2)
%11 = affine.min affine_map<(d0, d1) -> (16, d0 - d1)>(%9, %10)
%12 = bufferization.alloc_tensor() {memory_space = #gpu.address_space<workgroup>} : tensor<4x16x1x64xbf16>
%13 = bufferization.alloc_tensor() {memory_space = #gpu.address_space<workgroup>} : tensor<64x64xbf16>
%14 = tensor.empty(%9) : tensor<4x1x1x?x4x16xf32>
%15 = scf.forall (%arg4, %arg5, %arg6, %arg7, %arg8) in (1, 1, 1, 4, 64) shared_outs(%arg9 = %14) -> (tensor<4x1x1x?x4x16xf32>) {
%17:3 = affine.delinearize_index %arg8 into (4, 16) : index, index, index
%18 = affine.linearize_index disjoint [%17#1, %c0] by (4, 4) : index
%19 = affine.max affine_map<(d0, d1) -> (d0 - d1, 0)>(%9, %18)
%20 = affine.min affine_map<(d0) -> (d0, 4)>(%19)
%21 = affine.max affine_map<(d0) -> (-d0 + 4, 0)>(%arg7)
%22 = affine.min affine_map<(d0) -> (d0, 1)>(%21)
%23 = affine.max affine_map<(d0) -> (-d0 + 16, 0)>(%17#2)
%24 = affine.min affine_map<(d0) -> (d0, 1)>(%23)
%25 = tensor.empty() : tensor<4x1x1x1x4x1xf32>
%26 = linalg.fill ins(%cst_0 : f32) outs(%25 : tensor<4x1x1x1x4x1xf32>) -> tensor<4x1x1x1x4x1xf32>
%27 = scf.for %arg10 = %c0 to %c3 step %c1 iter_args(%arg11 = %26) -> (tensor<4x1x1x1x4x1xf32>) {
%31 = iree_gpu.barrier_region ins(%12 : tensor<4x16x1x64xbf16>) {
^bb0(%arg12: tensor<4x16x1x64xbf16>):
%36 = scf.for %arg13 = %c0 to %c512 step %c256 iter_args(%arg14 = %arg12) -> (tensor<4x16x1x64xbf16>) {
%37 = affine.apply affine_map<(d0, d1, d2, d3, d4, d5) -> (d0 + d1 + d2 * 64 + d3 * 256 + d4 * 256 + d5 * 256)>(%arg13, %arg8, %arg7, %c0, %c0, %c0)
%38:4 = affine.delinearize_index %37 into (4, 16, 1, 8) : index, index, index, index
%39 = affine.apply affine_map<(d0) -> (d0 * 8)>(%38#3)
%extracted_slice_7 = tensor.extract_slice %arg14[%38#0, %38#1, 0, %39] [1, 1, 1, 8] [1, 1, 1, 1] : tensor<4x16x1x64xbf16> to tensor<1x1x1x8xbf16>
%40 = affine.min affine_map<(d0, d1, d2) -> (16, d1 - d2, d0)>(%38#1, %9, %10)
%41 = affine.min affine_map<(d0, d1) -> (1, d0 - d1)>(%11, %40)
%42 = affine.apply affine_map<(d0) -> (-d0 + 1)>(%41)
%43 = affine.apply affine_map<(d0)[s0] -> (d0 * 4 + s0)>(%arg0)[%38#0]
%44 = affine.apply affine_map<(d0)[s0, s1] -> (d0 * 16 + s0 + s1)>(%arg2)[%40, %10]
%extracted_slice_8 = tensor.extract_slice %3[%43, 0, %44, 0] [1, 225, %41, 64] [1, 1, 1, 1] : tensor<16x225x225x64xbf16> to tensor<1x225x?x64xbf16>
%padded = tensor.pad %extracted_slice_8 low[0, 1, 0, 0] high[0, 1, 0, 0] {
^bb0(%arg15: index, %arg16: index, %arg17: index, %arg18: index):
tensor.yield %cst : bf16
} : tensor<1x225x?x64xbf16> to tensor<1x227x?x64xbf16>
%45 = tensor.empty(%41) : tensor<1x?x1x8xbf16>
%46 = affine.apply affine_map<(d0, d1) -> (d0 * 64 + d1 * 8)>(%arg10, %38#3)
%47 = iree_linalg_ext.im2col strides = [1] dilations = [1] kernel_size = [3] m_offset = [%arg1] * [1] k_offset = [%46] * [1] batch_pos = [0, 2] m_pos = [1] k_pos = [3] input_k_perm = [0, 1] ins(%padded : tensor<1x227x?x64xbf16>) outs(%45 : tensor<1x?x1x8xbf16>) -> tensor<1x?x1x8xbf16>
%padded_9 = tensor.pad %47 low[0, 0, 0, 0] high[0, %42, 0, 0] {
^bb0(%arg15: index, %arg16: index, %arg17: index, %arg18: index):
tensor.yield %cst : bf16
} : tensor<1x?x1x8xbf16> to tensor<1x1x1x8xbf16>
%48 = linalg.copy {lowering_config = #iree_gpu.derived_thread_config} ins(%padded_9 : tensor<1x1x1x8xbf16>) outs(%extracted_slice_7 : tensor<1x1x1x8xbf16>) -> tensor<1x1x1x8xbf16>
%inserted_slice = tensor.insert_slice %48 into %arg14[%38#0, %38#1, 0, %39] [1, 1, 1, 8] [1, 1, 1, 1] : tensor<1x1x1x8xbf16> into tensor<4x16x1x64xbf16>
scf.yield %inserted_slice : tensor<4x16x1x64xbf16>
} {unroll_loop}
iree_gpu.yield %36 : tensor<4x16x1x64xbf16>
} : tensor<4x16x1x64xbf16>
%32 = iree_gpu.barrier_region ins(%13 : tensor<64x64xbf16>) {
^bb0(%arg12: tensor<64x64xbf16>):
%36 = scf.for %arg13 = %c0 to %c512 step %c256 iter_args(%arg14 = %arg12) -> (tensor<64x64xbf16>) {
%37 = affine.apply affine_map<(d0, d1, d2, d3, d4, d5) -> (d0 + d1 + d2 * 64 + d3 * 256 + d4 * 256 + d5 * 256)>(%arg13, %arg8, %arg7, %c0, %c0, %c0)
%38:2 = affine.delinearize_index %37 into (64, 8) : index, index
%39 = affine.apply affine_map<(d0) -> (d0 * 8)>(%38#1)
%extracted_slice_7 = tensor.extract_slice %arg14[%38#0, %39] [1, 8] [1, 1] : tensor<64x64xbf16> to tensor<1x8xbf16>
%40 = affine.apply affine_map<(d0, d1) -> (d0 * 64 + d1 * 8)>(%arg10, %38#1)
%extracted_slice_8 = tensor.extract_slice %4[%38#0, %40] [1, 8] [1, 1] : tensor<64x192xbf16> to tensor<1x8xbf16>
%41 = linalg.copy {lowering_config = #iree_gpu.derived_thread_config} ins(%extracted_slice_8 : tensor<1x8xbf16>) outs(%extracted_slice_7 : tensor<1x8xbf16>) -> tensor<1x8xbf16>
%inserted_slice = tensor.insert_slice %41 into %arg14[%38#0, %39] [1, 8] [1, 1] : tensor<1x8xbf16> into tensor<64x64xbf16>
scf.yield %inserted_slice : tensor<64x64xbf16>
} {unroll_loop}
iree_gpu.yield %36 : tensor<64x64xbf16>
} : tensor<64x64xbf16>
%expanded = tensor.expand_shape %32 [[0, 1], [2, 3]] output_shape [4, 16, 4, 16] : tensor<64x64xbf16> into tensor<4x16x4x16xbf16>
%extracted_slice_2 = tensor.extract_slice %expanded[%arg7, %17#2, 0, %18] [1, 1, 4, 4] [1, 1, 1, 1] : tensor<4x16x4x16xbf16> to tensor<1x1x4x4xbf16>
%expanded_3 = tensor.expand_shape %31 [[0], [1, 2], [3], [4, 5]] output_shape [4, 1, 16, 1, 4, 16] : tensor<4x16x1x64xbf16> into tensor<4x1x16x1x4x16xbf16>
%extracted_slice_4 = tensor.extract_slice %expanded_3[0, 0, %17#2, 0, 0, %18] [4, 1, 1, 1, 4, 4] [1, 1, 1, 1, 1, 1] : tensor<4x1x16x1x4x16xbf16> to tensor<4x1x1x1x4x4xbf16>
%33 = tensor.empty() : tensor<4x1x1x4x1x4xbf16>
%transposed_5 = linalg.transpose ins(%extracted_slice_4 : tensor<4x1x1x1x4x4xbf16>) outs(%33 : tensor<4x1x1x4x1x4xbf16>) permutation = [0, 1, 3, 4, 2, 5]
%34 = tensor.empty() : tensor<1x4x1x4xbf16>
%transposed_6 = linalg.transpose ins(%extracted_slice_2 : tensor<1x1x4x4xbf16>) outs(%34 : tensor<1x4x1x4xbf16>) permutation = [0, 2, 1, 3]
%35 = iree_codegen.inner_tiled ins(%transposed_5, %transposed_6) outs(%arg11) {indexing_maps = [affine_map<(d0, d1, d2, d3, d4) -> (d0, d2, d1, d4)>, affine_map<(d0, d1, d2, d3, d4) -> (d3, d4)>, affine_map<(d0, d1, d2, d3, d4) -> (d0, d1, d2, d3)>], iterator_types = [#linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<parallel>, #linalg.iterator_type<reduction>], kind = #iree_gpu.mma_layout<MFMA_F32_16x16x16_BF16>, lowering_config = #iree_gpu.lowering_config<{mma_kind = #iree_gpu.mma_layout<MFMA_F32_16x16x16_BF16>, padding = [4, 1, 16, 64, 64], promote_operands = [0, 1], reduction = [0, 0, 0, 0, 4], subgroup = [4, 1, 1, 1, 0], subgroup_m_count = 1 : i64, subgroup_n_count = 4 : i64, workgroup = [4, 1, 16, 64, 0]}>} : tensor<4x1x1x4x1x4xbf16>, tensor<1x4x1x4xbf16> into tensor<4x1x1x1x4x1xf32>
scf.yield %35 : tensor<4x1x1x1x4x1xf32>
}
%28 = tensor.empty() : tensor<4x1x1x4x1x1xf32>
%transposed = linalg.transpose ins(%27 : tensor<4x1x1x1x4x1xf32>) outs(%28 : tensor<4x1x1x4x1x1xf32>) permutation = [0, 1, 2, 4, 3, 5]
%29 = tensor.empty() : tensor<4x1x1x4x1x1xf32>
%30 = linalg.copy ins(%transposed : tensor<4x1x1x4x1x1xf32>) outs(%29 : tensor<4x1x1x4x1x1xf32>) -> tensor<4x1x1x4x1x1xf32>
%extracted_slice_1 = tensor.extract_slice %30[0, 0, 0, 0, 0, 0] [4, 1, 1, %20, %22, %24] [1, 1, 1, 1, 1, 1] : tensor<4x1x1x4x1x1xf32> to tensor<4x1x1x?x?x?xf32>
scf.forall.in_parallel {
tensor.parallel_insert_slice %extracted_slice_1 into %arg9[0, 0, 0, %18, %arg7, %17#2] [4, 1, 1, %20, %22, %24] [1, 1, 1, 1, 1, 1] : tensor<4x1x1x?x?x?xf32> into tensor<4x1x1x?x4x16xf32>
}
} {mapping = [#gpu.thread<linear_dim_4>, #gpu.thread<linear_dim_3>, #gpu.thread<linear_dim_2>, #gpu.thread<linear_dim_1>, #gpu.thread<linear_dim_0>]}
%collapsed = tensor.collapse_shape %15 [[0], [1], [2, 3], [4, 5]] : tensor<4x1x1x?x4x16xf32> into tensor<4x1x?x64xf32>
%16 = linalg.generic {indexing_maps = [affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>, affine_map<(d0, d1, d2, d3) -> (d0, d1, d2, d3)>], iterator_types = ["parallel", "parallel", "parallel", "parallel"]} ins(%collapsed : tensor<4x1x?x64xf32>) outs(%extracted_slice : tensor<4x1x?x64xbf16>) {
^bb0(%in: f32, %out: bf16):
%17 = arith.truncf %in : f32 to bf16
linalg.yield %17 : bf16
} -> tensor<4x1x?x64xbf16>
scf.forall.in_parallel {
tensor.parallel_insert_slice %16 into %arg3[%8, %arg1, %7, 0] [4, 1, %9, 64] [1, 1, 1, 1] : tensor<4x1x?x64xbf16> into tensor<16x225x225x64xbf16>
}
} {mapping = [#iree_codegen.workgroup_mapping<z>, #iree_codegen.workgroup_mapping<y>, #iree_codegen.workgroup_mapping<x>]}
iree_tensor_ext.dispatch.tensor.store %6, %2, offsets = [0, 0, 0, 0], sizes = [16, 225, 225, 64], strides = [1, 1, 1, 1] : tensor<16x225x225x64xbf16> -> !iree_tensor_ext.dispatch.tensor<writeonly:tensor<16x225x225x64xbf16>>
return
}What component(s) does this issue relate to?
Compiler
Version information
I haven't bisected a commit yet, but I'm looking at 9323cd2 and the issue is present.
Will update if I find an earlier commit.
Additional context
No response
Metadata
Metadata
Assignees
Labels
bug 🐞Something isn't working
Type
Projects
- StatusShow more project fieldsDone
Activity
nirvedhmeshram commentedon Aug 7, 2025
I dont have broarder context here but after some debugging it seems this section of code https://github.com/iree-org/iree/blob/main/compiler/src/iree/compiler/Codegen/Common/Transforms.cpp#L75-L91 is causing trouble and in this specific case is not necessary. I noticed without it we are able to fuse the consumer into the thread loop @Max191
hanhanW commentedon Aug 8, 2025
I also hit the hang issue after we enable more fusion in data-tiling fusion; reverting 9c77258 fixes the issue.
Max191 commentedon Aug 8, 2025
Thanks for investigating a bit guys, we can revert the commit for now and I will triage the issue. The commit was just for encoding fusions with multi-result dispatches, which is not on by default right now anyway.
Max191 commentedon Aug 8, 2025
Revert PR: #21632
Revert "[Codegen] Fix dominance issues blocking consumer fusions (#21……
Max191 commentedon Aug 8, 2025
Taking a look at this now. As a separate question, @zjgarvey could you also share the full original dump before the hang? The input IR that you shared looks off, since the trunc is happening after all the unpacking, so I'm curious how we got this IR.
zjgarvey commentedon Aug 8, 2025
@Max191 Here is the IR dump, the source IR, and a compile command.
Notably, without the tuning specs, it will actually fail compilation instead of hanging.
hanhanW commentedon Aug 8, 2025
@Max191 do you need a repro from me? The IR could be simpler because it happens in the first distribution level.
Max191 commentedon Aug 8, 2025
Ok, this looks like something that we were aware of, but hadn't seen yet until now. This is the dump after GPUPackToIntrinsics:
The problem is that the unpack and extract_slice cannot fold into each other anymore:
This is because of the recently updated semantics of unpack: llvm/llvm-project#150272
We will need to implement some propagation patterns for tensor.extract_slice ops through consumer ops, but it is tricky because extract_slice is a special op with respect to tiling, so we have to be careful. I will think about a reasonable way to handle this.
Max191 commentedon Aug 8, 2025
No, the one Zach shared is good enough. I see what went wrong there. I also have a fix for the original issue with the hang, which I'll send out shortly.
Max191 commentedon Aug 8, 2025
This reapplies the commit with the fix: #21637
Reapply "[Codegen] Fix dominance issues blocking consumer fusions (#2…
Revert "[Codegen] Fix dominance issues blocking consumer fusions (ire…
Reapply "[Codegen] Fix dominance issues blocking consumer fusions (ir…
Max191 commentedon Aug 12, 2025
This is fixed now
Max191 commentedon Aug 12, 2025
There was another problem that this issue brought up (unpack propagation not happening), but that is tracked in another issue: #21635
Revert "[Codegen] Fix dominance issues blocking consumer fusions (#21……
Reapply "[Codegen] Fix dominance issues blocking consumer fusions (#2…