[LLVMGPU] Add a check for the number of workgroup tile sizes in a config (#24916) Adds a check to ensure that the number of work group tile sizes in a configuration is not greater than the op's number of loops. Also made the verifier's error diagnostics more specific, i.e., tell us which index in the config is wrong, the expected number of tiles sizes, e.t.c. This is inspired by how its done in [LLVMCPU'S verifiers](https://github.com/iree-org/iree/blob/2e978ebfec686692c67ce75ade42c54dbac67e87/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUSelectLoweringStrategy.cpp#L87). `Assisted-by: Claude` Signed-off-by: Ayokunle Amodu [ayokunle321@gmail.com](mailto:ayokunle321@gmail.com) Signed-off-by: Ayokunle Amodu <ayokunle321@gmail.com>
diff --git a/compiler/src/iree/compiler/Codegen/LLVMGPU/Verifiers.cpp b/compiler/src/iree/compiler/Codegen/LLVMGPU/Verifiers.cpp index 81b002e..5b03911 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMGPU/Verifiers.cpp +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/Verifiers.cpp
@@ -30,27 +30,38 @@ size = reductionTileSizes.size(); if (size > numLoops) { - return op->emitOpError("expected number of reduction tile size is equal " - "or less than number of loops"); + return op->emitOpError("expected no more than ") + << numLoops << " tile sizes in the reduction tiling level, but " + << size << " were set"; } for (size_t i = 0; i < size; ++i) { if (reductionTileSizes[i] > 0 && cast<linalg::LinalgOp>(op).getIteratorTypesArray()[i] != utils::IteratorType::reduction) { return op->emitOpError( - "expected to non-zero reduction tile has reduction iterator"); + "expected only reduction dims to be set in the reduction " + "tiling level, but tile size at index (") + << i << ") was also set"; } } SmallVector<int64_t> workgroupTileSizes = loweringConfig.getWorkgroupTileSizes(); size = workgroupTileSizes.size(); + + if (size > numLoops) { + return op->emitOpError("expected no more than ") + << numLoops << " tile sizes in the workgroup tiling level, but " + << size << " were set"; + } for (size_t i = 0; i < size; ++i) { if (workgroupTileSizes[i] > 0 && cast<linalg::LinalgOp>(op).getIteratorTypesArray()[i] != utils::IteratorType::parallel) { return op->emitOpError( - "expected to non-zero workgroup tile has parallel iterator"); + "expected only parallel dims to be set in the workgroup " + "tiling level, but tile size at index (") + << i << ") was also set"; } }
diff --git a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/BUILD.bazel b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/BUILD.bazel index 19e2a10..84b6091 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/BUILD.bazel +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/BUILD.bazel
@@ -50,6 +50,7 @@ "gpu_pipeline_data_tiling.mlir", "gpu_pipeline_relayout_ops.mlir", "horizontal_fusion_pipeline.mlir", + "illegal_configuration.mlir", "legalize.mlir", "legalize_nd_vectors.mlir", "linalg_transform.mlir",
diff --git a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/CMakeLists.txt b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/CMakeLists.txt index 5aaef03..865f0e1 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/CMakeLists.txt +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/CMakeLists.txt
@@ -44,6 +44,7 @@ "gpu_pipeline_data_tiling.mlir" "gpu_pipeline_relayout_ops.mlir" "horizontal_fusion_pipeline.mlir" + "illegal_configuration.mlir" "legalize.mlir" "legalize_nd_vectors.mlir" "linalg_transform.mlir"
diff --git a/compiler/src/iree/compiler/Codegen/LLVMGPU/test/illegal_configuration.mlir b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/illegal_configuration.mlir new file mode 100644 index 0000000..d9a969d --- /dev/null +++ b/compiler/src/iree/compiler/Codegen/LLVMGPU/test/illegal_configuration.mlir
@@ -0,0 +1,51 @@ +// RUN: iree-opt --iree-gpu-test-target=gfx942 --pass-pipeline='builtin.module(iree-llvmgpu-select-lowering-strategy)' --verify-diagnostics --split-input-file %s + +#config = #iree_gpu.lowering_config<{reduction = [0, 0, 32, 1, 0]}> +#translation = #iree_codegen.translation_info<pipeline = #iree_gpu.pipeline<VectorDistribute> workgroup_size = [64, 1, 1] subgroup_size = 64> +func.func @too_many_reduction_tile_sizes(%a: tensor<64x64xf32>, %b: tensor<64x64xf32>, %c: tensor<64x64xf32>) -> tensor<64x64xf32> + attributes {translation_info = #translation} { + // expected-error @+1 {{expected no more than 3 tile sizes in the reduction tiling level, but 5 were set}} + %0 = linalg.matmul {lowering_config = #config} + ins(%a, %b : tensor<64x64xf32>, tensor<64x64xf32>) + outs(%c : tensor<64x64xf32>) -> tensor<64x64xf32> + return %0 : tensor<64x64xf32> +} + +// ----- + +#config = #iree_gpu.lowering_config<{workgroup = [64, 64, 0, 1, 0]}> +#translation = #iree_codegen.translation_info<pipeline = #iree_gpu.pipeline<VectorDistribute> workgroup_size = [64, 1, 1] subgroup_size = 64> +func.func @too_many_workgroup_tile_sizes(%a: tensor<64x64xf32>, %b: tensor<64x64xf32>, %c: tensor<64x64xf32>) -> tensor<64x64xf32> + attributes {translation_info = #translation} { + // expected-error @+1 {{expected no more than 3 tile sizes in the workgroup tiling level, but 5 were set}} + %0 = linalg.matmul {lowering_config = #config} + ins(%a, %b : tensor<64x64xf32>, tensor<64x64xf32>) + outs(%c : tensor<64x64xf32>) -> tensor<64x64xf32> + return %0 : tensor<64x64xf32> +} + +// ----- + +#config = #iree_gpu.lowering_config<{reduction = [32, 0, 0]}> +#translation = #iree_codegen.translation_info<pipeline = #iree_gpu.pipeline<VectorDistribute> workgroup_size = [64, 1, 1] subgroup_size = 64> +func.func @reduction_tile_on_parallel_dim(%a: tensor<64x64xf32>, %b: tensor<64x64xf32>, %c: tensor<64x64xf32>) -> tensor<64x64xf32> + attributes {translation_info = #translation} { + // expected-error @+1 {{expected only reduction dims to be set in the reduction tiling level, but tile size at index (0) was also set}} + %0 = linalg.matmul {lowering_config = #config} + ins(%a, %b : tensor<64x64xf32>, tensor<64x64xf32>) + outs(%c : tensor<64x64xf32>) -> tensor<64x64xf32> + return %0 : tensor<64x64xf32> +} + +// ----- + +#config = #iree_gpu.lowering_config<{workgroup = [0, 0, 32]}> +#translation = #iree_codegen.translation_info<pipeline = #iree_gpu.pipeline<VectorDistribute> workgroup_size = [64, 1, 1] subgroup_size = 64> +func.func @workgroup_tile_on_reduction_dim(%a: tensor<64x64xf32>, %b: tensor<64x64xf32>, %c: tensor<64x64xf32>) -> tensor<64x64xf32> + attributes {translation_info = #translation} { + // expected-error @+1 {{expected only parallel dims to be set in the workgroup tiling level, but tile size at index (2) was also set}} + %0 = linalg.matmul {lowering_config = #config} + ins(%a, %b : tensor<64x64xf32>, tensor<64x64xf32>) + outs(%c : tensor<64x64xf32>) -> tensor<64x64xf32> + return %0 : tensor<64x64xf32> +}