forked from iree-org/iree
-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
088c000
commit 5852224
Showing
5 changed files
with
87 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
compiler/src/iree/compiler/Codegen/LLVMCPU/ExpandF16MaxFToF32Pass.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Copyright 2023 The IREE Authors | ||
// | ||
// Licensed under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
|
||
#include "iree/compiler/Codegen/LLVMCPU/LLVMCPUPasses.h" | ||
#include "iree/compiler/Codegen/PassDetail.h" | ||
#include "mlir/Dialect/Vector/Transforms/LoweringPatterns.h" | ||
#include "mlir/Dialect/Vector/Transforms/VectorRewritePatterns.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
#include "mlir/Dialect/Arith/IR/Arith.h" | ||
|
||
namespace mlir { | ||
namespace iree_compiler { | ||
|
||
namespace { | ||
|
||
struct ExpandF16MaxFToF32Pattern : public OpRewritePattern<arith::MaxFOp> { | ||
public: | ||
using OpRewritePattern<arith::MaxFOp>::OpRewritePattern; | ||
|
||
LogicalResult matchAndRewrite(arith::MaxFOp op, | ||
PatternRewriter &rewriter) const override{ | ||
Type resultType = op.getLhs().getType(); | ||
if (getElementTypeOrSelf(resultType).getIntOrFloatBitWidth() != 16) { | ||
return failure(); | ||
} | ||
|
||
Location loc = op.getLoc(); | ||
|
||
Type wideType = rewriter.getF32Type(); | ||
if(auto vecTy = resultType.dyn_cast<VectorType>()) { | ||
wideType = VectorType::get(vecTy.getShape(), wideType); | ||
} | ||
|
||
Value lhsExt = rewriter.create<arith::ExtFOp>(loc, wideType, op.getLhs()); | ||
Value rhsExt = rewriter.create<arith::ExtFOp>(loc, wideType, op.getRhs()); | ||
Value maxExt = | ||
rewriter.create<arith::MaxFOp>(loc, wideType, lhsExt, rhsExt); | ||
Value result = rewriter.create<arith::TruncFOp>(loc, resultType, maxExt); | ||
|
||
rewriter.replaceOp(op, result); | ||
return success(); | ||
} | ||
}; | ||
|
||
struct ExpandF16MaxFToF32Pass | ||
: public ExpandF16MaxFToF32Base< | ||
ExpandF16MaxFToF32Pass> { | ||
void getDependentDialects(DialectRegistry ®istry) const override { | ||
registry.insert<linalg::LinalgDialect>(); | ||
} | ||
|
||
void runOnOperation() override { | ||
MLIRContext *context = &getContext(); | ||
RewritePatternSet patterns(&getContext()); | ||
patterns.insert<ExpandF16MaxFToF32Pattern>( | ||
context); | ||
if (failed(applyPatternsAndFoldGreedily(getOperation(), | ||
std::move(patterns)))) { | ||
return signalPassFailure(); | ||
} | ||
} | ||
}; | ||
} // namespace | ||
|
||
std::unique_ptr<Pass> createExpandF16MaxFToF32Pass() { | ||
return std::make_unique<ExpandF16MaxFToF32Pass>(); | ||
} | ||
|
||
} // namespace iree_compiler | ||
} // namespace mlir |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters