Skip to content

Commit 660cf92

Browse files
authored
[CodeGen] Clean up the IRs within the MaterializeEncoding pass. (#20625)
The pass already populates some canonicalization patterns, but the CPU tests still require running the canonicalizer. The revision explicitly populates needed patterns, and prevent redundant op creation in the pass. Since it already cleans the IR, it makes sense to run CSE within the pass. The revision slightly improves the compilation time because it does not create redundant copy operations in the first place. Signed-off-by: hanhanW <[email protected]>
1 parent 8069d7b commit 660cf92

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

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

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "mlir/Dialect/Tensor/IR/Tensor.h"
2525
#include "mlir/Interfaces/FunctionInterfaces.h"
2626
#include "mlir/Pass/PassManager.h"
27+
#include "mlir/Transforms/CSE.h"
2728
#include "mlir/Transforms/DialectConversion.h"
2829
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
2930

@@ -132,20 +133,26 @@ materializeFuncOpEncodings(FunctionOpInterface funcOp,
132133
}
133134
}
134135

135-
// Add patterns to fold pack/unpack ops with pad/extract_slice ops and
136-
// resolve dims ops.
136+
// Run patterns to fold pack/unpack ops with pad/extract_slice ops, resolve
137+
// dims ops, and eliminate common sub-expressions.
137138
{
138139
RewritePatternSet patterns(ctx);
139140
populateReshapeToInterfaceTensorPatterns(patterns);
140141
tensor::CastOp::getCanonicalizationPatterns(patterns, ctx);
141142
tensor::populateFoldTensorEmptyPatterns(patterns);
142143
linalg::FillOp::getCanonicalizationPatterns(patterns, ctx);
144+
linalg::PackOp::getCanonicalizationPatterns(patterns, ctx);
145+
linalg::UnPackOp::getCanonicalizationPatterns(patterns, ctx);
143146
linalg::populateFoldIntoPackAndUnpackPatterns(patterns);
144147
memref::populateResolveRankedShapedTypeResultDimsPatterns(patterns);
145148
if (failed(applyPatternsGreedily(funcOp, std::move(patterns)))) {
146149
funcOp.emitOpError("folding patterns failed");
147150
return failure();
148151
}
152+
153+
IRRewriter rewriter(ctx);
154+
DominanceInfo domInfo;
155+
mlir::eliminateCommonSubExpressions(rewriter, domInfo, funcOp);
149156
}
150157

151158
return success();

compiler/src/iree/compiler/Codegen/Common/test/llvmcpu_materialize_encoding.mlir

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: iree-opt --pass-pipeline="builtin.module(func.func(iree-codegen-materialize-device-encoding),canonicalize,cse)" --split-input-file %s | FileCheck %s
1+
// RUN: iree-opt --pass-pipeline="builtin.module(func.func(iree-codegen-materialize-device-encoding))" --split-input-file %s | FileCheck %s
22

33
#pipeline_layout = #hal.pipeline.layout<bindings = [
44
#hal.pipeline.binding<storage_buffer>,

compiler/src/iree/compiler/Codegen/ExternalInterfaces/CPUEncodingExternalModels.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,10 +97,13 @@ getExpandedType(RankedTensorType type, bool isBatched, bool isTransposed,
9797

9898
/// Given an input Value and a desired output element type, create and return
9999
/// an element-wise linalg::GenericOp that extends the input Value to the
100-
/// output element type.
100+
/// output element type. Returns `input` if casting is not needed.
101101
static Value createElementWiseExtUIOp(OpBuilder &builder, Value input,
102102
Location loc, Type outElemType) {
103103
auto inputType = cast<RankedTensorType>(input.getType());
104+
if (inputType.getElementType() == outElemType) {
105+
return input;
106+
}
104107
SmallVector<AffineMap> maps(
105108
2, builder.getMultiDimIdentityMap(inputType.getRank()));
106109
SmallVector<utils::IteratorType> iteratorTypes(inputType.getRank(),

0 commit comments

Comments
 (0)