-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[flang] add simplification for ProductOp intrinsic #169575
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
stomfaig
wants to merge
9
commits into
llvm:main
Choose a base branch
from
stomfaig:issue_169433
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+528
−0
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
f7daa18
add simplification for ProductOp intrinsic
stomfaig e94d558
format
stomfaig 4ffa90b
add builder for crating value
stomfaig f4d60ef
correct initial value for product simplification
stomfaig 13bc9b2
add tests for product simplification
stomfaig 4c5f89e
format
stomfaig 3db9913
remove leftover comment
stomfaig a489b4b
correct docstrings
stomfaig c71fb78
resolving comments
stomfaig File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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 |
|---|---|---|
|
|
@@ -23,6 +23,7 @@ | |
| #include "mlir/IR/Location.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
| #include <type_traits> | ||
|
||
|
|
||
| namespace hlfir { | ||
| #define GEN_PASS_DEF_SIMPLIFYHLFIRINTRINSICS | ||
|
|
@@ -931,6 +932,37 @@ class SumAsElementalConverter | |
| mlir::Value genScalarAdd(mlir::Value value1, mlir::Value value2); | ||
| }; | ||
|
|
||
| /// Reduction converter for Product. | ||
| class ProductAsElementalConverter | ||
| : public NumericReductionAsElementalConverterBase<hlfir::ProductOp> { | ||
| using Base = NumericReductionAsElementalConverterBase; | ||
|
|
||
| public: | ||
| ProductAsElementalConverter(hlfir::ProductOp op, | ||
| mlir::PatternRewriter &rewriter) | ||
| : Base{op, rewriter} {} | ||
|
|
||
| private: | ||
| virtual llvm::SmallVector<mlir::Value> genReductionInitValues( | ||
| [[maybe_unused]] mlir::ValueRange oneBasedIndices, | ||
| [[maybe_unused]] const llvm::SmallVectorImpl<mlir::Value> &extents) | ||
| final { | ||
| return {fir::factory::createOneValue(builder, loc, getResultElementType())}; | ||
| } | ||
| virtual llvm::SmallVector<mlir::Value> | ||
| reduceOneElement(const llvm::SmallVectorImpl<mlir::Value> ¤tValue, | ||
| hlfir::Entity array, | ||
| mlir::ValueRange oneBasedIndices) final { | ||
| checkReductions(currentValue); | ||
| hlfir::Entity elementValue = | ||
| hlfir::loadElementAt(loc, builder, array, oneBasedIndices); | ||
| return {genScalarMult(currentValue[0], elementValue)}; | ||
| } | ||
|
|
||
| // Generate scalar multiplication of the two values (of the same data type). | ||
| mlir::Value genScalarMult(mlir::Value value1, mlir::Value value2); | ||
| }; | ||
|
|
||
| /// Base class for logical reductions like ALL, ANY, COUNT. | ||
| /// They do not have MASK and FastMathFlags. | ||
| template <typename OpT> | ||
|
|
@@ -1194,6 +1226,20 @@ mlir::Value SumAsElementalConverter::genScalarAdd(mlir::Value value1, | |
| llvm_unreachable("unsupported SUM reduction type"); | ||
| } | ||
|
|
||
| mlir::Value ProductAsElementalConverter::genScalarMult(mlir::Value value1, | ||
| mlir::Value value2) { | ||
| mlir::Type ty = value1.getType(); | ||
| assert(ty == value2.getType() && "reduction values' types do not match"); | ||
| if (mlir::isa<mlir::FloatType>(ty)) | ||
| return mlir::arith::MulFOp::create(builder, loc, value1, value2); | ||
| else if (mlir::isa<mlir::ComplexType>(ty)) | ||
| return fir::MulcOp::create(builder, loc, value1, value2); | ||
| else if (mlir::isa<mlir::IntegerType>(ty)) | ||
| return mlir::arith::MulIOp::create(builder, loc, value1, value2); | ||
|
|
||
| llvm_unreachable("unsupported MUL reduction type"); | ||
| } | ||
|
|
||
| mlir::Value ReductionAsElementalConverter::genMaskValue( | ||
| mlir::Value mask, mlir::Value isPresentPred, mlir::ValueRange indices) { | ||
| mlir::OpBuilder::InsertionGuard guard(builder); | ||
|
|
@@ -1265,6 +1311,9 @@ class ReductionConversion : public mlir::OpRewritePattern<Op> { | |
| } else if constexpr (std::is_same_v<Op, hlfir::SumOp>) { | ||
| SumAsElementalConverter converter{op, rewriter}; | ||
| return converter.convert(); | ||
| } else if constexpr (std::is_same_v<Op, hlfir::ProductOp>) { | ||
| ProductAsElementalConverter converter{op, rewriter}; | ||
| return converter.convert(); | ||
| } | ||
| return rewriter.notifyMatchFailure(op, "unexpected reduction operation"); | ||
| } | ||
|
|
@@ -3158,6 +3207,7 @@ class SimplifyHLFIRIntrinsics | |
| mlir::RewritePatternSet patterns(context); | ||
| patterns.insert<TransposeAsElementalConversion>(context); | ||
| patterns.insert<ReductionConversion<hlfir::SumOp>>(context); | ||
| patterns.insert<ReductionConversion<hlfir::ProductOp>>(context); | ||
| patterns.insert<ArrayShiftConversion<hlfir::CShiftOp>>(context); | ||
| patterns.insert<ArrayShiftConversion<hlfir::EOShiftOp>>(context); | ||
| patterns.insert<CmpCharOpConversion>(context); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Imaginary part must be
0.