Skip to content

Commit

Permalink
[aievec] Generalize vector passes
Browse files Browse the repository at this point in the history
Right now, vectorization passes hook to FuncOp, which prevents
conversion to AIEVec within other top level operations, like AIE.device
ops.

This patch makes all passes generic and allows for conversion within
AIE.device.
  • Loading branch information
jsetoain committed Sep 15, 2023
1 parent f21d029 commit f90379d
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 39 deletions.
2 changes: 1 addition & 1 deletion include/aie/Dialect/AIEVec/Analysis/Passes.td
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@

include "mlir/Pass/PassBase.td"

def AIEVecConvAnalysis : Pass<"aievec-convolution-analysis", "mlir::func::FuncOp"> {
def AIEVecConvAnalysis : Pass<"aievec-convolution-analysis"> {
let summary = "Find MAC chains that can be replaced by convolution ops in "
"AIE-ML";
let constructor = "xilinx::aievec::createAIEVecConvolutionAnalysisPass()";
Expand Down
18 changes: 8 additions & 10 deletions lib/Dialect/AIEVec/Transforms/AIEVecOptimizations.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,8 +234,7 @@ configureAIEVecV2TransformationLegalizations(ConversionTarget &target) {
// Lowering passes
//===----------------------------------------------------------------------===//
struct AIEVecTransformationPass
: public PassWrapper<AIEVecTransformationPass,
OperationPass<func::FuncOp>> {
: public PassWrapper<AIEVecTransformationPass, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(AIEVecTransformationPass)

AIEVecTransformationPass() = default;
Expand Down Expand Up @@ -267,7 +266,7 @@ struct AIEVecTransformationPass
llvm::cl::init("aie")};

void runOnOperation() override {
auto func = getOperation();
auto op = getOperation();
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);
ConversionTarget target(*context);
Expand All @@ -277,7 +276,7 @@ struct AIEVecTransformationPass
if (target == "aieml") {
aieVersion = AIEArch::AIE_ML;
} else if (target != "aie") {
func.emitError() << "unknown AIE target '" << aieTarget << "'";
op->emitError() << "unknown AIE target '" << aieTarget << "'";
signalPassFailure();
return;
}
Expand All @@ -290,7 +289,7 @@ struct AIEVecTransformationPass
configureAIEVecV2TransformationLegalizations(target);
}

if (failed(applyPartialConversion(func, target, std::move(patterns)))) {
if (failed(applyPartialConversion(op, target, std::move(patterns)))) {
signalPassFailure();
}
}
Expand All @@ -302,8 +301,7 @@ createAIEVecTransformationPass(const OptimizeAIEVecOptions &options) {
}

struct AIEVecConvOpTransformationPass
: public PassWrapper<AIEVecConvOpTransformationPass,
OperationPass<func::FuncOp>> {
: public PassWrapper<AIEVecConvOpTransformationPass, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(AIEVecConvOpTransformationPass)

AIEVecConvOpTransformationPass() = default;
Expand Down Expand Up @@ -343,7 +341,7 @@ struct AIEVecConvOpTransformationPass
llvm::cl::init(0)};

void runOnOperation() override {
auto func = getOperation();
auto op = getOperation();
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);
ConversionTarget target(*context);
Expand All @@ -353,7 +351,7 @@ struct AIEVecConvOpTransformationPass
if (target == "aieml") {
aieVersion = AIEArch::AIE_ML;
} else if (target != "aie") {
func.emitError() << "unknown AIE target '" << aieTarget << "'";
op->emitError() << "unknown AIE target '" << aieTarget << "'";
signalPassFailure();
return;
}
Expand All @@ -365,7 +363,7 @@ struct AIEVecConvOpTransformationPass
configureAIEVecConvOpTransformationLegalizations(target, am);
}

if (failed(applyPartialConversion(func, target, std::move(patterns)))) {
if (failed(applyPartialConversion(op, target, std::move(patterns)))) {
signalPassFailure();
}
}
Expand Down
10 changes: 5 additions & 5 deletions lib/Dialect/AIEVec/Transforms/ConvertVectorToAIEVec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,19 @@ using SetInboundsToWriteOp = SetInboundsToReadStoreOpPattern<TransferWriteOp>;
//===----------------------------------------------------------------------===//

struct RedundantLoadStoreOptimizationPass
: public PassWrapper<RedundantLoadStoreOptimizationPass,
OperationPass<func::FuncOp>> {
: public PassWrapper<RedundantLoadStoreOptimizationPass, OperationPass<>> {

void runOnOperation() override {
func::FuncOp funcOp = getOperation();
auto op = getOperation();
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);

patterns.add<SetInboundsToReadOp, SetInboundsToWriteOp>(
patterns.getContext());

(void)applyPatternsAndFoldGreedily(funcOp, std::move(patterns));
(void)applyPatternsAndFoldGreedily(op, std::move(patterns));
IRRewriter rewriter(&getContext());
vector::transferOpflowOpt(rewriter, funcOp);
vector::transferOpflowOpt(rewriter, op);
}
};

Expand Down
8 changes: 4 additions & 4 deletions lib/Dialect/AIEVec/Transforms/FoldMulAddChainToConvOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -490,16 +490,16 @@ struct AIEVecConvAnalysis : public AIEVecConvAnalysisBase<AIEVecConvAnalysis> {
markAllAnalysesPreserved();
AnalysisManager am = getAnalysisManager();
LongestConvMACChainAnalysis::am = &am;
func::FuncOp func = getOperation();
Operation *op = getOperation();

// Compute all the chains
func.walk([&](arith::AddIOp addOp) {
op->walk([&](arith::AddIOp addOp) {
if (isa<VectorType>(addOp.getResult().getType()))
am.getChildAnalysis<LongestConvMACChainAnalysis>(addOp);
});

// Sort the chains, ready to split by group
func.walk([&](arith::AddIOp addOp) {
op->walk([&](arith::AddIOp addOp) {
if (isa<VectorType>(addOp.getResult().getType())) {
auto &analysis =
am.getChildAnalysis<LongestConvMACChainAnalysis>(addOp);
Expand All @@ -509,7 +509,7 @@ struct AIEVecConvAnalysis : public AIEVecConvAnalysisBase<AIEVecConvAnalysis> {
});

if (printResult) {
func.walk([&](arith::AddIOp addOp) {
op->walk([&](arith::AddIOp addOp) {
if (isa<VectorType>(addOp.getResult().getType())) {
auto &macChainAnalysis =
am.getChildAnalysis<LongestConvMACChainAnalysis>(addOp);
Expand Down
22 changes: 12 additions & 10 deletions lib/Dialect/AIEVec/Transforms/VectorToAIEVecConversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2183,7 +2183,7 @@ static void configureAIEVecV2Legalizations(ConversionTarget &target,
/// Lower incoming vector operations into their corresponding AIE vector
/// intrinsics.
struct LowerVectorToAIEVec
: public PassWrapper<LowerVectorToAIEVec, OperationPass<func::FuncOp>> {
: public PassWrapper<LowerVectorToAIEVec, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(LowerVectorToAIEVec)

LowerVectorToAIEVec() = default;
Expand Down Expand Up @@ -2213,7 +2213,7 @@ struct LowerVectorToAIEVec
llvm::cl::init("aie")};

void runOnOperation() override {
auto func = getOperation();
auto op = getOperation();
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);
ConversionTarget target(*context);
Expand All @@ -2223,7 +2223,7 @@ struct LowerVectorToAIEVec
if (target == "aieml") {
aieVersion = AIEArch::AIE_ML;
} else if (target != "aie") {
func.emitError() << "unknown AIE target '" << aieTarget << "'";
op->emitError() << "unknown AIE target '" << aieTarget << "'";
signalPassFailure();
return;
}
Expand All @@ -2239,7 +2239,7 @@ struct LowerVectorToAIEVec
configureAIEVecV2Legalizations(target, am);
}

if (failed(applyPartialConversion(func, target, std::move(patterns)))) {
if (failed(applyPartialConversion(op, target, std::move(patterns)))) {
signalPassFailure();
}
}
Expand All @@ -2259,7 +2259,8 @@ createLowerVectorToAIEVec(const LowerVectorToAIEVecOptions &options) {
// additional common subexpressions with UPDs generated from unaligned
// `transfer_read` ops.
struct ExtendUPDOpsPass
: public PassWrapper<ExtendUPDOpsPass, OperationPass<func::FuncOp>> {
: public PassWrapper<ExtendUPDOpsPass, OperationPass<>> {

void runOnOperation() override {
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);
Expand All @@ -2272,8 +2273,8 @@ struct ExtendUPDOpsPass
llvm::all_of(op->getUsers(),
[](Operation *op) { return isa<aievec::ExtOp>(op); });
});
auto func = getOperation();
if (failed(applyPartialConversion(func, target, std::move(patterns)))) {
auto op = getOperation();
if (failed(applyPartialConversion(op, target, std::move(patterns)))) {
signalPassFailure();
}
}
Expand All @@ -2285,7 +2286,8 @@ struct ExtendUPDOpsPass
// TODO: a single ext op of the top half, which might be a good opportunity to
// TODO: further optimize wide UPDs.
struct SimplifyUPDOpsPass
: public PassWrapper<SimplifyUPDOpsPass, OperationPass<func::FuncOp>> {
: public PassWrapper<SimplifyUPDOpsPass, OperationPass<>> {

void runOnOperation() override {
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);
Expand All @@ -2297,8 +2299,8 @@ struct SimplifyUPDOpsPass
return !defOp || !isa<aievec::UPDOp>(defOp) || !defOp->hasOneUse() ||
op.getIndex() != 0;
});
auto func = getOperation();
if (failed(applyPartialConversion(func, target, std::move(patterns)))) {
auto op = getOperation();
if (failed(applyPartialConversion(op, target, std::move(patterns)))) {
signalPassFailure();
}
}
Expand Down
17 changes: 8 additions & 9 deletions lib/Dialect/AIEVec/Transforms/VectorToVectorConversions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,7 @@ populateAIEMLCanonicalizeConversionPatterns(RewritePatternSet &patterns) {
// 2) Split unaligned transfer reads into a wider aligned transfer read
// followed by a `vector.extract_strided_slice` operation.
struct CanonicalizeVectorForAIEVecPass
: public PassWrapper<CanonicalizeVectorForAIEVecPass,
OperationPass<func::FuncOp>> {
: public PassWrapper<CanonicalizeVectorForAIEVecPass, OperationPass<>> {
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(CanonicalizeVectorForAIEVecPass)

CanonicalizeVectorForAIEVecPass() = default;
Expand Down Expand Up @@ -364,7 +363,7 @@ struct CanonicalizeVectorForAIEVecPass
llvm::cl::init("aie")};

void runOnOperation() override {
func::FuncOp funcOp = getOperation();
auto op = getOperation();
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);
ConversionTarget target(*context);
Expand All @@ -375,7 +374,7 @@ struct CanonicalizeVectorForAIEVecPass
if (target == "aieml") {
aieVersion = AIEArch::AIE_ML;
} else if (target != "aie") {
funcOp.emitError() << "unknown AIE target '" << aieTarget << "'";
op->emitError() << "unknown AIE target '" << aieTarget << "'";
signalPassFailure();
return;
}
Expand All @@ -391,7 +390,7 @@ struct CanonicalizeVectorForAIEVecPass
configureAIEMLCanonicalizeLegalizations(target);
}

if (failed(applyPartialConversion(funcOp, target, std::move(patterns)))) {
if (failed(applyPartialConversion(op, target, std::move(patterns)))) {
signalPassFailure();
}
}
Expand All @@ -403,16 +402,16 @@ static std::unique_ptr<::mlir::Pass> createCanonicalizeVectorForAIEVecPass(
}

struct HoistCastOpToDataSourcePass
: public PassWrapper<HoistCastOpToDataSourcePass,
OperationPass<func::FuncOp>> {
: public PassWrapper<HoistCastOpToDataSourcePass, OperationPass<>> {

void runOnOperation() override {
func::FuncOp funcOp = getOperation();
auto op = getOperation();
MLIRContext *context = &getContext();
RewritePatternSet patterns(context);

patterns.add<HoistCastOpToDataSourcePattern>(patterns.getContext());

(void)applyPatternsAndFoldGreedily(funcOp, std::move(patterns));
(void)applyPatternsAndFoldGreedily(op, std::move(patterns));
}
};

Expand Down

0 comments on commit f90379d

Please sign in to comment.