[LLVMCPU] Verify conv decomposition configs on generic convolutions too (#25253) ## Context We have inconsistent handling of named linalg operations v.s. generics. Ideally it should not matter in which form we see the operation, in all cases we should handle the same way if it is semantically identical. The usage of `llvm::isa` can prevent this in later stages of the pipeline, thus it is preferable to avoid checks on the type and rather use the op interface instead e.g. `linalg::isaConvolutionOpInterface` ## This PR `verifyConvTileAndDecomposeExpertConfig` returned early unless the op implemented `linalg::ConvolutionOpInterface`, which only named ops do, and read the spatial loop positions from a `TypeSwitch` over named ops. A generalized convolution selects `ConvTileAndDecomposeExpert` too (through `linalg::isaConvolutionOpOfType`), but skipped this verification. A lowering config that is rejected on the named op was accepted on its `linalg.generic` form. Since GlobalOptimization generalizes all convolutions, that is the form `iree-compile` hands to this pass. This PR matches the convolution with `linalg::isaConvolutionOpInterface` and takes the output image and filter loops from `linalg::inferConvolutionDims`. Named and generic forms are verified the same way, and the per-op layout table is gone (34 lines removed, 12 added in the pass). Ops with other than two spatial dims are still rejected as `unsupported conv types`. ## Tests `illegal_configuration.mlir` gets: * a second RUN line that generalizes the named ops first. On `main`, 6 of the existing expected errors are not produced in that mode; * an NCHW convolution and an NCHW pooling that cannot be decomposed; * an NCHW convolution with a config that is valid only when the spatial loops are read in NCHW order, which must not produce an error. ## Effect on `iree-compile` The verifier now runs on every convolution and pooling in the pipeline, so a config that strategy selection picks itself could in principle be rejected now. To check, I ran strategy selection over 103 single-op programs (convolutions and poolings in NHWC and NCHW, 1-D, 3-D, depthwise, grouped, QDQ variants, plus non-conv ops) in 9 flag configurations (default, data tiling, ukernels, im2col) for five targets: the Apple M4 host, RISC-V 64 (`spacemit-x60`), x86-64 (`haswell`, `skylake-avx512`) and AArch64 SVE (`neoverse-v1`, scalable vectorization). All 465 compilations per target pass selection and verification. The change cannot alter generated code, only reject configs. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Florian Walbroel <walbroel@roofline.ai> Co-authored-by: Claude Fable 5.2 <noreply@anthropic.com>
diff --git a/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUSelectLoweringStrategy.cpp b/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUSelectLoweringStrategy.cpp index 052adbc..9b59eec 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUSelectLoweringStrategy.cpp +++ b/compiler/src/iree/compiler/Codegen/LLVMCPU/LLVMCPUSelectLoweringStrategy.cpp
@@ -137,7 +137,8 @@ static LogicalResult verifyConvTileAndDecomposeExpertConfig( Operation *op, IREE::CPU::LoweringConfigAttr loweringConfig, Operation * /*rootOp*/) { - if (!isa<linalg::ConvolutionOpInterface>(op)) { + auto linalgOp = dyn_cast<linalg::LinalgOp>(op); + if (!linalgOp || !linalg::isaConvolutionOpInterface(linalgOp)) { return success(); } @@ -152,7 +153,6 @@ IREE::CPU::TilingLevel::DistributionTiles, IREE::CPU::TilingLevel::VectorCommonParallelTiles, IREE::CPU::TilingLevel::VectorReductionTiles}; - linalg::LinalgOp linalgOp = cast<linalg::LinalgOp>(op); SmallVector<int64_t> shapeAfterTiling = linalgOp.getStaticLoopRanges(); for (IREE::CPU::TilingLevel level : requiredLevels) { if (!loweringConfig.hasTilingLevel(llvm::to_underlying(level))) { @@ -190,40 +190,18 @@ } } - int64_t khSize, kwSize, ohSize, owSize; - auto isSizeExtracted = - TypeSwitch<Operation *, LogicalResult>(op) - .Case<linalg::Conv2DNhwcHwcfOp, linalg::DepthwiseConv2DNhwcHwcOp, - linalg::PoolingNhwcSumOp, linalg::PoolingNhwcMaxOp, - linalg::PoolingNhwcMaxUnsignedOp, linalg::PoolingNhwcMinOp, - linalg::PoolingNhwcMinUnsignedOp>([&](auto) { - // shape: N, OH, OW, OC, KH, KW, (IC) - khSize = shapeAfterTiling[4]; - kwSize = shapeAfterTiling[5]; - ohSize = shapeAfterTiling[1]; - owSize = shapeAfterTiling[2]; - return success(); - }) - .Case([&](linalg::Conv2DNchwFchwOp) { - // shape: N, OC, OH, OW, (IC), KH, KW - khSize = shapeAfterTiling[5]; - kwSize = shapeAfterTiling[6]; - ohSize = shapeAfterTiling[2]; - owSize = shapeAfterTiling[3]; - return success(); - }) - .Case<linalg::PoolingNchwSumOp, linalg::PoolingNchwMaxOp>([&](auto) { - // shape: N, OC, OH, OW, KH, KW - khSize = shapeAfterTiling[4]; - kwSize = shapeAfterTiling[5]; - ohSize = shapeAfterTiling[2]; - owSize = shapeAfterTiling[3]; - return success(); - }) - .Default(failure()); - if (failed(isSizeExtracted)) { + // The pipeline decomposes 2-D convolutions and poolings. Take the spatial + // loops from the indexing maps, so the layout and the op name do not matter. + FailureOr<linalg::ConvolutionDimensions> convDims = + linalg::inferConvolutionDims(linalgOp); + if (failed(convDims) || convDims->outputImage.size() != 2 || + convDims->filterLoop.size() != 2) { return op->emitOpError("unsupported conv types"); } + int64_t ohSize = shapeAfterTiling[convDims->outputImage[0]]; + int64_t owSize = shapeAfterTiling[convDims->outputImage[1]]; + int64_t khSize = shapeAfterTiling[convDims->filterLoop[0]]; + int64_t kwSize = shapeAfterTiling[convDims->filterLoop[1]]; bool removeH = (khSize == 1 && ohSize == 1); bool removeW = (kwSize == 1 && owSize == 1);
diff --git a/compiler/src/iree/compiler/Codegen/LLVMCPU/test/illegal_configuration.mlir b/compiler/src/iree/compiler/Codegen/LLVMCPU/test/illegal_configuration.mlir index 9a44261..48803e1 100644 --- a/compiler/src/iree/compiler/Codegen/LLVMCPU/test/illegal_configuration.mlir +++ b/compiler/src/iree/compiler/Codegen/LLVMCPU/test/illegal_configuration.mlir
@@ -1,4 +1,5 @@ // RUN: iree-opt --pass-pipeline='builtin.module(iree-llvmcpu-select-lowering-strategy)' --verify-diagnostics --split-input-file %s +// RUN: iree-opt --pass-pipeline='builtin.module(func.func(linalg-generalize-named-ops),iree-llvmcpu-select-lowering-strategy)' --verify-diagnostics --split-input-file %s #config = #iree_cpu.lowering_config<distribution = [64, 64], vector_common_parallel = [8, 32, 16], vector_reduction = [0, 0, 16]> #translation = #iree_codegen.translation_info<pipeline = #iree_cpu.pipeline<DoubleTilingExpert>> @@ -105,3 +106,44 @@ linalg.conv_1d_nwc_wcf {lowering_config = #config} ins(%0, %1 : memref<1x11x576xf32>, memref<5x576x576xf32>) outs(%2 : memref<1x7x576xf32>) return } + +// ----- + +// The spatial dims are taken from the indexing maps, so layouts other than +// NHWC are verified as well. OH is tiled to 1, but KH is not. + +#config = #iree_cpu.lowering_config<distribution = [0, 64, 7, 7, 0, 0, 0], vector_common_parallel = [6, 32, 1, 7, 0, 0, 0], vector_reduction = [0, 0, 0, 0, 4, 3, 3]> +#translation = #iree_codegen.translation_info<pipeline = #iree_cpu.pipeline<ConvTileAndDecomposeExpert>> +func.func @illegal_conv_config_nchw(%0: memref<36x512x9x9xf32>, %1: memref<512x512x3x3xf32>, %2: memref<36x512x7x7xf32>) attributes { + translation_info = #translation +} { + // expected-error @+1 {{can't decompose the conv op}} + linalg.conv_2d_nchw_fchw {lowering_config = #config} ins(%0, %1 : memref<36x512x9x9xf32>, memref<512x512x3x3xf32>) outs(%2 : memref<36x512x7x7xf32>) + return +} + +// ----- + +// OW and KW are tiled to 1, so the NCHW conv can be decomposed along W. No +// error is expected. + +#config = #iree_cpu.lowering_config<distribution = [0, 64, 7, 7, 0, 0, 0], vector_common_parallel = [6, 32, 7, 1, 0, 0, 0], vector_reduction = [0, 0, 0, 0, 4, 3, 1]> +#translation = #iree_codegen.translation_info<pipeline = #iree_cpu.pipeline<ConvTileAndDecomposeExpert>> +func.func @legal_conv_config_nchw(%0: memref<36x512x9x9xf32>, %1: memref<512x512x3x3xf32>, %2: memref<36x512x7x7xf32>) attributes { + translation_info = #translation +} { + linalg.conv_2d_nchw_fchw {lowering_config = #config} ins(%0, %1 : memref<36x512x9x9xf32>, memref<512x512x3x3xf32>) outs(%2 : memref<36x512x7x7xf32>) + return +} + +// ----- + +#config = #iree_cpu.lowering_config<distribution = [0, 64, 7, 7, 0, 0], vector_common_parallel = [1, 32, 1, 7, 0, 0], vector_reduction = [0, 0, 0, 0, 3, 3]> +#translation = #iree_codegen.translation_info<pipeline = #iree_cpu.pipeline<ConvTileAndDecomposeExpert>> +func.func @illegal_pooling_config_nchw(%0: memref<1x64x9x9xf32>, %1: memref<3x3xf32>, %2: memref<1x64x7x7xf32>) attributes { + translation_info = #translation +} { + // expected-error @+1 {{can't decompose the conv op}} + linalg.pooling_nchw_max {lowering_config = #config} ins(%0, %1 : memref<1x64x9x9xf32>, memref<3x3xf32>) outs(%2 : memref<1x64x7x7xf32>) + return +}