Skip to content

Commit 7215a15

Browse files
authored
[CPU] Drop TilingConfig from KernelDispatch.cpp (#21567)
The revision implements `getAvailableTilingInfo` in `IREE::CPU::LoweringConfigAttr`, and replace all the uses of `TilingConfig`'s version with it. It also deletes the method from `TilingConfig` and makes corresponding changes to the codebase. The lowering config propagation is disabled if the root op does not use `IREE::CPU::LoweringConfigAttr`, because we need the method for the propagation. However, there is still a case that it uses Codegen lowering config, which is `LinalgExt::CustomOp` dispatch. The zeros in tiling config are dropped in the propagation. Thus, there is a change in lit test. It is a reasonable change, because it keeps the same behavior as GPU backends. See ab88871 --------- Signed-off-by: hanhanW <[email protected]>
1 parent 4ef8251 commit 7215a15

File tree

8 files changed

+73
-89
lines changed

8 files changed

+73
-89
lines changed

compiler/src/iree/compiler/Codegen/Common/CPU/CPUPrepareUkernels.cpp

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -151,11 +151,10 @@ static LogicalResult reduceDefiningOp(PatternRewriter &rewriter, Value input) {
151151

152152
/// Drops the first element from all the tile sizes list. The first element is
153153
/// for the batch dimension.
154-
static IREE::Codegen::LoweringConfigAttrInterface
155-
dropBatchTileSize(IREE::Codegen::LoweringConfigAttrInterface config) {
156-
std::unique_ptr<TilingConfig> tilingConfig = TilingConfig::create(config);
154+
static IREE::CPU::LoweringConfigAttr
155+
dropBatchTileSize(IREE::CPU::LoweringConfigAttr config) {
157156
SmallVector<IREE::CPU::LoweringConfigLevelInfo> tilingInfo =
158-
tilingConfig->getTilingLevelInfo();
157+
config.getAvailableTilingInfo();
159158
SmallVector<NamedAttribute> newItems;
160159
for (auto [level, tileSizes, scalableTileFlags] : tilingInfo) {
161160
tileSizes.erase(tileSizes.begin());
@@ -204,8 +203,7 @@ struct ConvertBatchMmt4DtoMmt4DPattern
204203
.result();
205204

206205
auto loweringConfig =
207-
getLoweringConfig<IREE::Codegen::LoweringConfigAttrInterface>(
208-
oldFillOp);
206+
getLoweringConfig<IREE::CPU::LoweringConfigAttr>(oldFillOp);
209207
if (loweringConfig) {
210208
auto config = dropBatchTileSize(loweringConfig);
211209
setLoweringConfig(reducedOut.getDefiningOp(), config);
@@ -240,8 +238,7 @@ struct ConvertBatchMmt4DtoMmt4DPattern
240238
loc, reducedOut.getType(), ValueRange{reducedLhs, reducedRhs},
241239
ValueRange{reducedOut});
242240

243-
auto loweringConfig =
244-
getLoweringConfig<IREE::Codegen::LoweringConfigAttrInterface>(op);
241+
auto loweringConfig = getLoweringConfig<IREE::CPU::LoweringConfigAttr>(op);
245242
if (loweringConfig) {
246243
auto config = dropBatchTileSize(loweringConfig);
247244
setLoweringConfig(mmt4DOp, config);

compiler/src/iree/compiler/Codegen/Common/TileSizeSelection.cpp

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -95,24 +95,6 @@ void TilingConfig::initFromCPULoweringConfig(IREE::CPU::LoweringConfigAttr lc) {
9595
}
9696
}
9797

98-
SmallVector<IREE::CPU::LoweringConfigLevelInfo>
99-
TilingConfig::getTilingLevelInfo() {
100-
SmallVector<IREE::CPU::LoweringConfigLevelInfo> result;
101-
TileSizesListType tileSizesList = getTileSizes();
102-
ScalableTileFlagsListType scalableFlagsList = getScalableTileFlags();
103-
int64_t mappedIdx = 0;
104-
for (auto [idx, actualLevel] : llvm::enumerate(tilingLevelToActualLevelMap)) {
105-
if (actualLevel == IREE::CPU::TilingLevel::InvalidLevel) {
106-
continue;
107-
}
108-
result.push_back(IREE::CPU::LoweringConfigLevelInfo{
109-
static_cast<IREE::CPU::TilingLevel>(idx), tileSizesList[mappedIdx],
110-
scalableFlagsList[mappedIdx]});
111-
mappedIdx++;
112-
}
113-
return result;
114-
}
115-
11698
bool TilingConfig::isValidLevel(IREE::CPU::TilingLevel level) {
11799
return tilingLevelToActualLevelMap[static_cast<int64_t>(level)] !=
118100
IREE::CPU::TilingLevel::InvalidLevel;

compiler/src/iree/compiler/Codegen/Common/TileSizeSelection.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -69,12 +69,6 @@ class TilingConfig {
6969
[](int64_t tileSize) { return tileSize != 0; });
7070
}
7171

72-
/// Returns a list of tiling information for each level. Each value is a valid
73-
/// level in the TilingConfig.
74-
/// Different from attribute variant, the method materialize the attribute
75-
/// content to the `IREE::CPU::LoweringConfigLevelInfo` contrainer.
76-
SmallVector<IREE::CPU::LoweringConfigLevelInfo> getTilingLevelInfo();
77-
7872
/// Returns all the tile sizes of all the levels of the configuration.
7973
TileSizesListType getTileSizes() const {
8074
TileSizesListType result;

compiler/src/iree/compiler/Codegen/Dialect/CPU/IR/IREECPUAttrs.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,24 @@ Attribute LoweringConfigAttr::getTilingLevelAttr(MLIRContext *ctx,
158158
ctx, tileSizes, /*interchange=*/{}, scalableFlags);
159159
}
160160

161+
SmallVector<LoweringConfigLevelInfo>
162+
LoweringConfigAttr::getAvailableTilingInfo() {
163+
SmallVector<LoweringConfigLevelInfo> result;
164+
for (unsigned i = 0, e = TilingLevel::MaxNumTileLevels; i < e; ++i) {
165+
if (!hasTilingLevel(i)) {
166+
continue;
167+
}
168+
auto attr = cast<IREE::Codegen::LoweringConfigTilingLevelAttr>(
169+
getTilingLevelAttr(i));
170+
LoweringConfigLevelInfo item;
171+
item.level = static_cast<TilingLevel>(i);
172+
llvm::append_range(item.sizes, attr.getSizes());
173+
llvm::append_range(item.scalableFlags, attr.getScalableFlags());
174+
result.push_back(item);
175+
}
176+
return result;
177+
}
178+
161179
SmallVector<int64_t> LoweringConfigAttr::getWorkgroupTileSizes() const {
162180
return getTileSizes(getConfig(), DistributionTiles);
163181
}

compiler/src/iree/compiler/Codegen/Dialect/CPU/IR/IREECPUAttrs.td

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ def IREECPU_LoweringConfigAttr :
7171
static Attribute getTilingLevelAttr(MLIRContext *ctx,
7272
ArrayRef<int64_t> tileSizes,
7373
ArrayRef<bool> scalableFlags);
74+
75+
/// Returns a vector that contains all the tiling information in the config.
76+
SmallVector<LoweringConfigLevelInfo> getAvailableTilingInfo();
7477
}];
7578
let hasCustomAssemblyFormat = 1;
7679
let genVerifyDecl = 1;

compiler/src/iree/compiler/Codegen/Dialect/CPU/IR/IREECPUTypes.h

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,6 @@
1111
#include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenInterfaces.h"
1212
#include "iree/compiler/Codegen/Dialect/Codegen/Utils/Utils.h"
1313

14-
// clang-format off
15-
#define GET_ATTRDEF_CLASSES
16-
#include "iree/compiler/Codegen/Dialect/CPU/IR/IREECPUAttrs.h.inc"
17-
#undef GET_ATTRDEF_CLASSES
18-
// clang-format on
19-
2014
namespace mlir::iree_compiler::IREE::CPU {
2115

2216
/// Representation for all the supported tiling levels. All or just a subset of
@@ -43,4 +37,8 @@ StringRef getTilingLevelName(TilingLevel level);
4337

4438
} // namespace mlir::iree_compiler::IREE::CPU
4539

40+
// clang-format off
41+
#define GET_ATTRDEF_CLASSES
42+
#include "iree/compiler/Codegen/Dialect/CPU/IR/IREECPUAttrs.h.inc"
43+
// clang-format on
4644
#endif // IREE_COMPILER_CODEGEN_DIALECT_CPU_IREECPUTYPES_H_

compiler/src/iree/compiler/Codegen/LLVMCPU/KernelDispatch.cpp

Lines changed: 42 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1063,30 +1063,12 @@ class LoweringConfigGenerator {
10631063
};
10641064

10651065
/// Returns the same lowering_config attribute with the updated tile sizes and
1066-
/// scalable tile flags. The `setDistrubtionConfig` flag is only available when
1067-
/// `origLoweringConfig is a IREE::CPU::LoweringConfigAttr. The distribution
1068-
/// tiling sizes is not set if it is false.
1069-
/// See `Codegen/Common/TileSizeSelection.h` for the convention of mapping
1070-
/// between tiling levels.
1071-
static IREE::Codegen::LoweringConfigAttrInterface getNewLoweringConfig(
1072-
IREE::Codegen::LoweringConfigAttrInterface origLoweringConfig,
1073-
ArrayRef<IREE::CPU::LoweringConfigLevelInfo> tilingInfo,
1074-
bool setDistributionConfig) {
1075-
assert((isa<IREE::Codegen::LoweringConfigAttr, IREE::CPU::LoweringConfigAttr>(
1076-
origLoweringConfig)));
1077-
MLIRContext *ctx = origLoweringConfig.getContext();
1078-
if (isa<IREE::Codegen::LoweringConfigAttr>(origLoweringConfig)) {
1079-
TileSizesListType tileSizesList;
1080-
ScalableTileFlagsListType scalableTileFlagsList;
1081-
for (auto [level, tileSizes, scalableFlags] : tilingInfo) {
1082-
(void)level;
1083-
tileSizesList.push_back(tileSizes);
1084-
scalableTileFlagsList.push_back(scalableFlags);
1085-
}
1086-
return IREE::Codegen::LoweringConfigAttr::get(ctx, tileSizesList,
1087-
scalableTileFlagsList);
1088-
}
1089-
1066+
/// scalable tile flags. The distribution tiling sizes is not set if it is
1067+
/// false.
1068+
static IREE::Codegen::LoweringConfigAttrInterface
1069+
getNewLoweringConfig(MLIRContext *ctx,
1070+
ArrayRef<IREE::CPU::LoweringConfigLevelInfo> tilingInfo,
1071+
bool setDistributionConfig) {
10901072
SmallVector<NamedAttribute> newItems;
10911073
for (auto [level, tileSizes, scalableFlags] : tilingInfo) {
10921074
if (!setDistributionConfig && level == TilingLevel::DistributionTiles) {
@@ -2751,13 +2733,16 @@ static LogicalResult
27512733
adjustTileSizesForUnPackOp(mlir::FunctionOpInterface entryPointFn,
27522734
Operation *rootOp) {
27532735
auto linalgOp = dyn_cast<linalg::LinalgOp>(rootOp);
2754-
if (!linalgOp)
2736+
if (!linalgOp) {
2737+
return success();
2738+
}
2739+
auto loweringConfig =
2740+
getLoweringConfig<IREE::CPU::LoweringConfigAttr>(linalgOp);
2741+
if (!loweringConfig) {
2742+
// Tile size adjustment is only available when the rootOp uses
2743+
// IREE::CPU::LoweringConfigAttr.
27552744
return success();
2756-
IREE::Codegen::LoweringConfigAttrInterface loweringConfig =
2757-
getLoweringConfig(linalgOp);
2758-
std::unique_ptr<TilingConfig> tilingConfig =
2759-
TilingConfig::create(loweringConfig);
2760-
TileSizesListType tileSizesList = tilingConfig->getTileSizes();
2745+
}
27612746

27622747
bool foundUnPackOp = false;
27632748
SmallVector<int64_t> alignedSizes(linalgOp.getNumLoops(), 1);
@@ -2792,7 +2777,7 @@ adjustTileSizesForUnPackOp(mlir::FunctionOpInterface entryPointFn,
27922777

27932778
// Fixup for making tileSizes be multiple of inner_tile_sizes.
27942779
SmallVector<IREE::CPU::LoweringConfigLevelInfo> tilingInfo =
2795-
tilingConfig->getTilingLevelInfo();
2780+
loweringConfig.getAvailableTilingInfo();
27962781
for (IREE::CPU::LoweringConfigLevelInfo &info : tilingInfo) {
27972782
SmallVector<int64_t> &tileSizes = info.sizes;
27982783
for (auto idx : llvm::seq<int64_t>(0, tileSizes.size())) {
@@ -2824,7 +2809,7 @@ adjustTileSizesForUnPackOp(mlir::FunctionOpInterface entryPointFn,
28242809
}
28252810

28262811
IREE::Codegen::LoweringConfigAttrInterface newLoweringConfig =
2827-
getNewLoweringConfig(loweringConfig, tilingInfo,
2812+
getNewLoweringConfig(rootOp->getContext(), tilingInfo,
28282813
/*setDistributionConfig=*/true);
28292814
return setOpConfigAndEntryPointFnTranslation(
28302815
entryPointFn, rootOp, newLoweringConfig, pipeline, /*workgroupSize=*/{},
@@ -2947,7 +2932,13 @@ setLoweringConfigForComputeOps(mlir::FunctionOpInterface entryPointFn,
29472932
return success();
29482933
}
29492934

2950-
auto rootLoweringConfig = getLoweringConfig(rootOperation);
2935+
auto rootLoweringConfig =
2936+
getLoweringConfig<IREE::CPU::LoweringConfigAttr>(rootOperation);
2937+
if (!rootLoweringConfig) {
2938+
// Propagation is only available for IREE::CPU::LoweringConfigAttr.
2939+
return success();
2940+
}
2941+
29512942
SmallVector<int64_t> distTileSizes, parallelVecTileSizes;
29522943
SmallVector<bool> distScalableTileSizes, parallelVecScalableTileSizes;
29532944
assert(rootLoweringConfig.hasWorkgroupTilingLevel());
@@ -3084,19 +3075,17 @@ setLoweringConfigForComputeOps(mlir::FunctionOpInterface entryPointFn,
30843075
}
30853076

30863077
// Set the lowering configs with new tile sizes.
3087-
// TODO(hanchung): Deprecate TilingConfig from the file.
3088-
std::unique_ptr<TilingConfig> tilingConfig =
3089-
TilingConfig::create(rootLoweringConfig);
30903078
for (auto op : computeOps) {
30913079
int numLoops = cast<TilingInterface>(op).getLoopIteratorTypes().size();
30923080
SmallVector<IREE::CPU::LoweringConfigLevelInfo> newTilingInfo;
30933081
// For root op, we patch the adjusted tile sizes on its original tiling
30943082
// config.
30953083
if (op == rootOperation) {
3096-
newTilingInfo = tilingConfig->getTilingLevelInfo();
3084+
newTilingInfo = rootLoweringConfig.getAvailableTilingInfo();
30973085
updateOrAddTilingLevelInfo(newTilingInfo, IREE::CPU::DistributionTiles,
30983086
distTileSizes, distScalableTileSizes);
3099-
if (tilingConfig->getNumTilingLevels() > 1) {
3087+
if (rootLoweringConfig.hasTilingLevel(
3088+
IREE::CPU::VectorCommonParallelTiles)) {
31003089
updateOrAddTilingLevelInfo(
31013090
newTilingInfo, IREE::CPU::VectorCommonParallelTiles,
31023091
commonVecTileSizes, commonVecScalableTileFlags);
@@ -3110,27 +3099,30 @@ setLoweringConfigForComputeOps(mlir::FunctionOpInterface entryPointFn,
31103099
distTileSizes, falseVec);
31113100
// The cache level tiling sizes are not adjusted, so we use the
31123101
// config from the rootOp directly.
3113-
if (tilingConfig->isValidLevel(IREE::CPU::CacheParallelTiles)) {
3114-
updateOrAddTilingLevelInfo(newTilingInfo, IREE::CPU::CacheParallelTiles,
3115-
tilingConfig->getCacheParallelSizes(),
3116-
falseVec);
3102+
if (rootLoweringConfig.hasTilingLevel(IREE::CPU::CacheParallelTiles)) {
3103+
updateOrAddTilingLevelInfo(
3104+
newTilingInfo, IREE::CPU::CacheParallelTiles,
3105+
rootLoweringConfig.getStaticTilingLevelSizes(
3106+
IREE::CPU::CacheParallelTiles, rootOperation),
3107+
falseVec);
31173108
}
3118-
if (tilingConfig->isValidLevel(IREE::CPU::CacheReductionTiles)) {
3109+
if (rootLoweringConfig.hasTilingLevel(IREE::CPU::CacheReductionTiles)) {
31193110
updateOrAddTilingLevelInfo(
31203111
newTilingInfo, IREE::CPU::CacheReductionTiles,
3121-
tilingConfig->getCacheReductionSizes(), falseVec);
3112+
rootLoweringConfig.getStaticTilingLevelSizes(
3113+
IREE::CPU::CacheReductionTiles, rootOperation),
3114+
falseVec);
31223115
}
31233116
updateOrAddTilingLevelInfo(
31243117
newTilingInfo, IREE::CPU::VectorCommonParallelTiles,
31253118
commonVecTileSizes, commonVecScalableTileFlags);
31263119
bool setUpOK =
31273120
TypeSwitch<Operation *, bool>(op)
31283121
.Case<linalg::PackOp>([&](auto packOp) {
3129-
for (ArrayRef<bool> flags :
3130-
tilingConfig->getScalableTileFlags()) {
3131-
// TODO: Handle scalable flags
3132-
if (llvm::any_of(flags, [&](bool flag) { return flag; }))
3133-
return false;
3122+
// TODO: Handle scalable flags
3123+
if (llvm::any_of(rootLoweringConfig.getVectorScalableFlags(),
3124+
[&](bool flag) { return flag; })) {
3125+
return false;
31343126
}
31353127
updateOrAddTilingLevelInfo(newTilingInfo,
31363128
IREE::CPU::VectorReductionTiles,
@@ -3202,7 +3194,7 @@ setLoweringConfigForComputeOps(mlir::FunctionOpInterface entryPointFn,
32023194
return lhs.level < rhs.level;
32033195
});
32043196
IREE::Codegen::LoweringConfigAttrInterface config =
3205-
getNewLoweringConfig(rootLoweringConfig, newTilingInfo,
3197+
getNewLoweringConfig(rootOperation->getContext(), newTilingInfo,
32063198
/*setDistributionConfig=*/op == rootOperation);
32073199
setLoweringConfig(op, config);
32083200
}

compiler/src/iree/compiler/Codegen/LLVMCPU/test/select_x86_64_lowering_strategy.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1917,7 +1917,7 @@ func.func @custom_op(%arg0 : tensor<384x512xf32>, %arg1 : tensor<512x128xf32>,
19171917
} -> tensor<384x128xf32>
19181918
return %1 : tensor<384x128xf32>
19191919
}
1920-
// CHECK-DAG: #[[CONFIG0:.+]] = #iree_codegen.lowering_config<tile_sizes = {{\[}}[48, 64]]>
1920+
// CHECK-DAG: #[[CONFIG0:.+]] = #iree_codegen.lowering_config<tile_sizes = {{\[}}[48, 64, 0]]>
19211921
// CHECK-DAG: #[[CONFIG1:.+]] = #iree_cpu.lowering_config<cache_parallel = [48, 64], vector_common_parallel = [8, 32]>
19221922
// CHECK-DAG: #[[CONFIG2:.+]] = #iree_cpu.lowering_config<cache_parallel = [48, 64, 0], distribution = [48, 64, 0], vector_common_parallel = [8, 32, 0], vector_reduction = [0, 0, 16]>
19231923
// CHECK-DAG: #[[TRANSLATION_INFO:.+]] = #iree_codegen.translation_info<pipeline = CPUDoubleTilingExpert, {enable_loop_peeling}>

0 commit comments

Comments
 (0)