Skip to content

Commit 3f37e32

Browse files
hanhanWkeshavvinayak01
authored andcommitted
[CPU] Switch CPUDoubleTilingExpert pipeline to use IREE::CPU::LoweringConfigAttr. (iree-org#21354)
The revision switches all the dispatches that use `CPUDoubleTilingExpert` to `IREE::CPU::LoweringConfigAttr`, which is a root-based tiling approach. There are two commits in the revision: - [The switch for `linalg.matmul` dispatches](iree-org@e885e86): it mainly focuses on the lowering config changes. - [The switch for `linalg.generic` dispatches](iree-org@48a527f): - Changes for lowering configs. - Update the pipeline that enumerates all the tiling levels. - Update `LLVMCPU2DScalableTo1DScalable` pass to use `IREE::CPU::LoweringConfigAttr`. The pipeline and lowering config verification only applies on the root op because CPUDoubleTilingExpert expects exactly four levels of tiling. After the switch, only the root op has four levels of tiling. We will need to refresh the verification logic anyway, as they are legacy code and it is easier to have better implementation today. So it will be refreshed in a follow-up. New known issues: - Trailing vector unit dims are not folded away in the root-op based pipeline, which results in larger binary sizes. Because they are unrolled: iree-org#21420 --------- Signed-off-by: hanhanW <[email protected]> Signed-off-by: keshavvinayak01 <[email protected]>
1 parent 5d90a81 commit 3f37e32

22 files changed

+331
-438
lines changed

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

Lines changed: 42 additions & 71 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,10 @@
88
#include "iree/compiler/Codegen/Dialect/CPU/IR/IREECPUTypes.h"
99
#include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenAttrs.h"
1010
#include "iree/compiler/Codegen/Dialect/Codegen/IR/IREECodegenInterfaces.h"
11+
#include "llvm/ADT/STLExtras.h"
1112
#include "llvm/Support/Casting.h"
1213
#include "llvm/Support/Debug.h"
14+
#include "mlir/IR/Attributes.h"
1315
#include "mlir/IR/BuiltinAttributes.h"
1416

1517
#define DEBUG_TYPE "tiling-config"
@@ -169,88 +171,57 @@ SizesAndScalableFlags TilingConfig::getVectorTileSizes() {
169171
return std::make_pair(vectorSizes, scalableFlags);
170172
}
171173

172-
/// Returns a new `LoweringConfigAttr`, with the tile sizes of vector
173-
/// dimensions, set to `sizes`, and the corresponding scalability set to
174-
/// `scalableFlags`.
175-
IREE::Codegen::LoweringConfigAttr
176-
TilingConfig::getLoweringConfigWithNewVectorSizes(
174+
IREE::CPU::LoweringConfigAttr TilingConfig::getLoweringConfigWithNewVectorSizes(
177175
ArrayRef<int64_t> sizes, ArrayRef<bool> scalableFlags) {
178176
unsigned numDims = getNumDimensions();
177+
(void)numDims;
179178
assert(sizes.size() == numDims &&
180179
"expected `sizes` to match number of dimensions");
181180
assert((scalableFlags.empty() || scalableFlags.size() == numDims) &&
182181
"expected `scalableFlags` to match "
183182
"number of dimensions (or be empty)");
184183

185-
// Make a map from tiling levels to vector dims at that level.
186-
std::array<SmallVector<unsigned, 4>, TilingLevel::MaxNumTileLevels>
187-
tilingLevelToDimsMap;
188-
for (unsigned dimPos = 0; dimPos < numDims; ++dimPos) {
189-
auto tilingLevelIndex = getTilingLevelForVectorDimPosition(dimPos);
190-
assert((tilingLevelIndex.has_value() || sizes[dimPos] == 0) &&
191-
"attempting to set vector size for dim with underspecified tiling "
192-
"level (zero is the only valid tile size)");
193-
if (tilingLevelIndex.has_value())
194-
tilingLevelToDimsMap[*tilingLevelIndex].push_back(dimPos);
195-
}
196-
197-
MLIRContext *context = loweringConfig.getContext();
198-
SmallVector<IREE::Codegen::LoweringConfigTilingLevelAttr> tilingLevels;
199-
for (unsigned i = 0, e = getNumTilingLevels(); i < e; ++i) {
200-
tilingLevels.push_back(cast<IREE::Codegen::LoweringConfigTilingLevelAttr>(
201-
loweringConfig.getTilingLevelAttr(i)));
202-
}
203-
SmallVector<IREE::Codegen::LoweringConfigTilingLevelAttr> newTilingLevelsList(
204-
tilingLevels.begin(), tilingLevels.end());
205-
206-
// For each vector tiling level:
207-
for (auto [tilingLevelIndex, tilingLevelDims] :
208-
llvm::enumerate(tilingLevelToDimsMap)) {
209-
if (tilingLevelDims.empty())
184+
MLIRContext *ctx = loweringConfig.getContext();
185+
SmallVector<NamedAttribute> items;
186+
for (unsigned i = 0, e = TilingLevel::MaxNumTileLevels; i < e; ++i) {
187+
auto level = static_cast<TilingLevel>(i);
188+
if (!isValidLevel(level)) {
210189
continue;
211-
auto level = tilingLevels[tilingLevelIndex];
212-
SmallVector<int64_t> newSizes(level.getSizes());
213-
SmallVector<bool> newScalableFlags(level.getScalableFlags());
214-
newScalableFlags.resize(numDims);
215-
// 1. Update all the vector sizes within that tiling level.
216-
for (unsigned dimPos : tilingLevelDims) {
217-
std::tie(newSizes[dimPos], newScalableFlags[dimPos]) =
218-
getTileSizeAtIndex(sizes, scalableFlags, dimPos);
219190
}
220-
// 2. Then create a new tiling level attribute for that level.
221-
auto newLevel = IREE::Codegen::LoweringConfigTilingLevelAttr::get(
222-
context, newSizes, level.getInterchange(), newScalableFlags);
223-
newTilingLevelsList[tilingLevelIndex] = newLevel;
224-
}
225-
226-
// Create a new `lowering_config` attribute.
227-
auto newTilingLevels = IREE::Codegen::LoweringConfigTilingLevelsAttr::get(
228-
context, newTilingLevelsList);
229-
return IREE::Codegen::LoweringConfigAttr::get(context, newTilingLevels);
230-
}
231-
232-
/// Returns a list with the tiling levels that can be fused for this
233-
/// configuration.
234-
SmallVector<int64_t> TilingConfig::getFusableLevels() {
235-
switch (getNumTilingLevels()) {
236-
case 0:
237-
return {};
238-
case 1:
239-
// Only distribution level.
240-
return {0};
241-
case 3:
242-
// Only distribution level + vector common parallel levels.
243-
return {0, 1};
244-
case 4:
245-
// Distribution + vector common parallel levels + vector inner parallel
246-
// levels.
247-
return {0, 1, 3};
248-
case 6:
249-
// Distribution + cache parallel levels.
250-
return {0, 1, 3, 5};
251-
default:
252-
llvm_unreachable("Unexpected number of tiling levels");
191+
switch (level) {
192+
case TilingLevel::DistributionTiles:
193+
case TilingLevel::CacheParallelTiles:
194+
case TilingLevel::CacheReductionTiles: {
195+
items.emplace_back(IREE::CPU::getTilingLevelName(level),
196+
getTilingLevelAttr(i));
197+
break;
198+
}
199+
case TilingLevel::VectorCommonParallelTiles:
200+
case TilingLevel::VectorReductionTiles:
201+
case TilingLevel::VectorInnerParallelTiles: {
202+
auto attr = cast<IREE::Codegen::LoweringConfigTilingLevelAttr>(
203+
loweringConfig.getTilingLevelAttr(i));
204+
SmallVector<int64_t> newSizes(attr.getSizes());
205+
SmallVector<bool> newScalableFlags(attr.getScalableFlags());
206+
newScalableFlags.resize(newSizes.size(), false);
207+
for (auto [idx, size] : llvm::enumerate(newSizes)) {
208+
if (size == 0) {
209+
continue;
210+
}
211+
newSizes[idx] = sizes[idx];
212+
newScalableFlags[idx] = scalableFlags[idx];
213+
}
214+
auto newLevel = IREE::Codegen::LoweringConfigTilingLevelAttr::get(
215+
ctx, newSizes, attr.getInterchange(), newScalableFlags);
216+
items.emplace_back(IREE::CPU::getTilingLevelName(level), newLevel);
217+
break;
218+
}
219+
case TilingLevel::MaxNumTileLevels:
220+
case TilingLevel::InvalidLevel:
221+
break;
222+
};
253223
}
224+
return IREE::CPU::LoweringConfigAttr::get(ctx, items);
254225
}
255226

256227
/// Returns the actual level in the configuration for this level of tiling.

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

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -181,19 +181,26 @@ class TilingConfig {
181181
/// Returns a new `LoweringConfigAttr`, with the tile sizes of vector
182182
/// dimensions, set to `sizes`, and the corresponding scalability set to
183183
/// `scalableFlags`.
184-
IREE::Codegen::LoweringConfigAttr
184+
IREE::CPU::LoweringConfigAttr
185185
getLoweringConfigWithNewVectorSizes(ArrayRef<int64_t> sizes,
186186
ArrayRef<bool> scalableFlags = {});
187187

188-
/// Returns a list with the tiling levels that can be fused for this
189-
/// configuration.
190-
SmallVector<int64_t> getFusableLevels();
191-
192-
// TODO(dcaballe): Revisit if these features are ever used.
193-
SmallVector<int64_t> getTileInterchangeSizes(unsigned level) {
194-
auto attr = cast<IREE::Codegen::LoweringConfigTilingLevelAttr>(
195-
loweringConfig.getTilingLevelAttr(level));
196-
return SmallVector<int64_t>(attr.getInterchange());
188+
/// Returns the `level`-th valid tiling attribute. Returns an empty vector if
189+
/// it does not exist.
190+
IREE::Codegen::LoweringConfigTilingLevelAttr
191+
getTilingLevelAttr(int64_t level) {
192+
for (auto [idx, mappedLevel] :
193+
llvm::enumerate(tilingLevelToActualLevelMap)) {
194+
if (mappedLevel == TilingLevel::InvalidLevel) {
195+
continue;
196+
}
197+
if (--level >= 0) {
198+
continue;
199+
}
200+
return cast<IREE::Codegen::LoweringConfigTilingLevelAttr>(
201+
loweringConfig.getTilingLevelAttr(mappedLevel));
202+
}
203+
return {};
197204
}
198205

199206
private:

compiler/src/iree/compiler/Codegen/LLVMCPU/BUILD.bazel

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,7 @@ iree_compiler_cc_library(
166166
"@llvm-project//mlir:SCFUtils",
167167
"@llvm-project//mlir:TensorDialect",
168168
"@llvm-project//mlir:TensorTransforms",
169+
"@llvm-project//mlir:TilingInterface",
169170
"@llvm-project//mlir:TosaDialect",
170171
"@llvm-project//mlir:TosaToArith",
171172
"@llvm-project//mlir:TransformDialect",

compiler/src/iree/compiler/Codegen/LLVMCPU/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,7 @@ iree_cc_library(
135135
MLIRSCFUtils
136136
MLIRTensorDialect
137137
MLIRTensorTransforms
138+
MLIRTilingInterface
138139
MLIRTosaDialect
139140
MLIRTosaToArith
140141
MLIRTransformDialect

0 commit comments

Comments
 (0)