-
Notifications
You must be signed in to change notification settings - Fork 14.3k
[flang] Added fir.is_contiguous_box and fir.box_total_elements ops. #131047
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
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
4d0f1b5
[flang] Added fir.is_contiguous_box and fir.box_total_elements ops.
vzakhari 7213313
[flang] Expand new FIR box operations into runtime calls.
vzakhari 1c8d7c5
Changed `IsContiguousUpTo` to accept int dim argument.
vzakhari 55fd283
Clarified that the input box cannot be absent.
vzakhari 5cf9754
Use greedy rewriter.
vzakhari 41f5cee
Added TODOs for more canonicalization.
vzakhari 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
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
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 |
---|---|---|
|
@@ -4671,6 +4671,83 @@ void fir::UnpackArrayOp::getEffects( | |
mlir::SideEffects::DefaultResource::get()); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// IsContiguousBoxOp | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace { | ||
struct SimplifyIsContiguousBoxOp | ||
: public mlir::OpRewritePattern<fir::IsContiguousBoxOp> { | ||
using mlir::OpRewritePattern<fir::IsContiguousBoxOp>::OpRewritePattern; | ||
mlir::LogicalResult | ||
matchAndRewrite(fir::IsContiguousBoxOp op, | ||
mlir::PatternRewriter &rewriter) const override; | ||
}; | ||
} // namespace | ||
|
||
mlir::LogicalResult SimplifyIsContiguousBoxOp::matchAndRewrite( | ||
fir::IsContiguousBoxOp op, mlir::PatternRewriter &rewriter) const { | ||
auto boxType = mlir::cast<fir::BaseBoxType>(op.getBox().getType()); | ||
// Nothing to do for assumed-rank arrays and !fir.box<none>. | ||
if (boxType.isAssumedRank() || fir::isBoxNone(boxType)) | ||
return mlir::failure(); | ||
|
||
if (fir::getBoxRank(boxType) == 0) { | ||
// Scalars are always contiguous. | ||
mlir::Type i1Type = rewriter.getI1Type(); | ||
rewriter.replaceOpWithNewOp<mlir::arith::ConstantOp>( | ||
op, i1Type, rewriter.getIntegerAttr(i1Type, 1)); | ||
return mlir::success(); | ||
} | ||
|
||
// TODO: support more patterns, e.g. a result of fir.embox without | ||
// the slice is contiguous. We can add fir::isSimplyContiguous(box) | ||
// that walks def-use to figure it out. | ||
return mlir::failure(); | ||
} | ||
|
||
void fir::IsContiguousBoxOp::getCanonicalizationPatterns( | ||
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) { | ||
patterns.add<SimplifyIsContiguousBoxOp>(context); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// BoxTotalElementsOp | ||
//===----------------------------------------------------------------------===// | ||
|
||
namespace { | ||
struct SimplifyBoxTotalElementsOp | ||
: public mlir::OpRewritePattern<fir::BoxTotalElementsOp> { | ||
using mlir::OpRewritePattern<fir::BoxTotalElementsOp>::OpRewritePattern; | ||
mlir::LogicalResult | ||
matchAndRewrite(fir::BoxTotalElementsOp op, | ||
mlir::PatternRewriter &rewriter) const override; | ||
}; | ||
} // namespace | ||
|
||
mlir::LogicalResult SimplifyBoxTotalElementsOp::matchAndRewrite( | ||
fir::BoxTotalElementsOp op, mlir::PatternRewriter &rewriter) const { | ||
auto boxType = mlir::cast<fir::BaseBoxType>(op.getBox().getType()); | ||
// Nothing to do for assumed-rank arrays and !fir.box<none>. | ||
if (boxType.isAssumedRank() || fir::isBoxNone(boxType)) | ||
return mlir::failure(); | ||
|
||
if (fir::getBoxRank(boxType) == 0) { | ||
// Scalar: 1 element. | ||
rewriter.replaceOpWithNewOp<mlir::arith::ConstantOp>( | ||
op, op.getType(), rewriter.getIntegerAttr(op.getType(), 1)); | ||
return mlir::success(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Result of fir.embox without slices can also fold to true (there are other cases, but it is a good start and some fir::isSimplyConintiguous(box) helper could be added later to try to walk back more, which could be helpful after inlining). |
||
|
||
// TODO: support more cases, e.g. !fir.box<!fir.array<10xi32>>. | ||
return mlir::failure(); | ||
} | ||
|
||
void fir::BoxTotalElementsOp::getCanonicalizationPatterns( | ||
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) { | ||
patterns.add<SimplifyBoxTotalElementsOp>(context); | ||
} | ||
|
||
//===----------------------------------------------------------------------===// | ||
// FIROpsDialect | ||
//===----------------------------------------------------------------------===// | ||
|
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
145 changes: 145 additions & 0 deletions
145
flang/lib/Optimizer/Transforms/SimplifyFIROperations.cpp
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 |
---|---|---|
@@ -0,0 +1,145 @@ | ||
//===- SimplifyFIROperations.cpp -- simplify complex FIR operations ------===// | ||
// | ||
// Part of the LLVM Project, 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 | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
//===----------------------------------------------------------------------===// | ||
/// \file | ||
/// This pass transforms some FIR operations into their equivalent | ||
/// implementations using other FIR operations. The transformation | ||
/// can legally use SCF dialect and generate Fortran runtime calls. | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "flang/Optimizer/Builder/FIRBuilder.h" | ||
#include "flang/Optimizer/Builder/Runtime/Inquiry.h" | ||
#include "flang/Optimizer/Builder/Todo.h" | ||
#include "flang/Optimizer/Dialect/FIROps.h" | ||
#include "flang/Optimizer/Transforms/Passes.h" | ||
#include "mlir/Pass/Pass.h" | ||
#include "mlir/Transforms/GreedyPatternRewriteDriver.h" | ||
|
||
namespace fir { | ||
#define GEN_PASS_DEF_SIMPLIFYFIROPERATIONS | ||
#include "flang/Optimizer/Transforms/Passes.h.inc" | ||
} // namespace fir | ||
|
||
#define DEBUG_TYPE "flang-simplify-fir-operations" | ||
|
||
namespace { | ||
/// Pass runner. | ||
class SimplifyFIROperationsPass | ||
: public fir::impl::SimplifyFIROperationsBase<SimplifyFIROperationsPass> { | ||
public: | ||
using fir::impl::SimplifyFIROperationsBase< | ||
SimplifyFIROperationsPass>::SimplifyFIROperationsBase; | ||
|
||
void runOnOperation() override final; | ||
}; | ||
|
||
/// Base class for all conversions holding the pass options. | ||
template <typename Op> | ||
class ConversionBase : public mlir::OpRewritePattern<Op> { | ||
public: | ||
using mlir::OpRewritePattern<Op>::OpRewritePattern; | ||
|
||
template <typename... Args> | ||
ConversionBase(mlir::MLIRContext *context, Args &&...args) | ||
: mlir::OpRewritePattern<Op>(context), | ||
options{std::forward<Args>(args)...} {} | ||
|
||
mlir::LogicalResult matchAndRewrite(Op, | ||
mlir::PatternRewriter &) const override; | ||
|
||
protected: | ||
fir::SimplifyFIROperationsOptions options; | ||
}; | ||
|
||
/// fir::IsContiguousBoxOp converter. | ||
using IsContiguousBoxCoversion = ConversionBase<fir::IsContiguousBoxOp>; | ||
|
||
/// fir::BoxTotalElementsOp converter. | ||
using BoxTotalElementsConversion = ConversionBase<fir::BoxTotalElementsOp>; | ||
} // namespace | ||
|
||
/// Generate a call to IsContiguous/IsContiguousUpTo function or an inline | ||
/// sequence reading extents/strides from the box and checking them. | ||
/// This conversion may produce fir.box_elesize and a loop (for assumed | ||
/// rank). | ||
template <> | ||
mlir::LogicalResult IsContiguousBoxCoversion::matchAndRewrite( | ||
fir::IsContiguousBoxOp op, mlir::PatternRewriter &rewriter) const { | ||
mlir::Location loc = op.getLoc(); | ||
fir::FirOpBuilder builder(rewriter, op.getOperation()); | ||
// TODO: support preferInlineImplementation. | ||
bool doInline = options.preferInlineImplementation && false; | ||
if (!doInline) { | ||
// Generate Fortran runtime call. | ||
mlir::Value result; | ||
if (op.getInnermost()) { | ||
mlir::Value one = | ||
builder.createIntegerConstant(loc, builder.getI32Type(), 1); | ||
result = | ||
fir::runtime::genIsContiguousUpTo(builder, loc, op.getBox(), one); | ||
} else { | ||
result = fir::runtime::genIsContiguous(builder, loc, op.getBox()); | ||
} | ||
result = builder.createConvert(loc, op.getType(), result); | ||
rewriter.replaceOp(op, result); | ||
return mlir::success(); | ||
} | ||
|
||
// Generate inline implementation. | ||
TODO(loc, "inline IsContiguousBoxOp"); | ||
return mlir::failure(); | ||
} | ||
|
||
/// Generate a call to Size runtime function or an inline | ||
/// sequence reading extents from the box an multiplying them. | ||
/// This conversion may produce a loop (for assumed rank). | ||
template <> | ||
mlir::LogicalResult BoxTotalElementsConversion::matchAndRewrite( | ||
fir::BoxTotalElementsOp op, mlir::PatternRewriter &rewriter) const { | ||
mlir::Location loc = op.getLoc(); | ||
fir::FirOpBuilder builder(rewriter, op.getOperation()); | ||
// TODO: support preferInlineImplementation. | ||
// Reading the extent from the box for 1D arrays probably | ||
// results in less code than the call, so we can always | ||
// inline it. | ||
bool doInline = options.preferInlineImplementation && false; | ||
if (!doInline) { | ||
// Generate Fortran runtime call. | ||
mlir::Value result = fir::runtime::genSize(builder, loc, op.getBox()); | ||
result = builder.createConvert(loc, op.getType(), result); | ||
rewriter.replaceOp(op, result); | ||
return mlir::success(); | ||
} | ||
|
||
// Generate inline implementation. | ||
TODO(loc, "inline BoxTotalElementsOp"); | ||
return mlir::failure(); | ||
} | ||
|
||
void SimplifyFIROperationsPass::runOnOperation() { | ||
mlir::ModuleOp module = getOperation(); | ||
mlir::MLIRContext &context = getContext(); | ||
mlir::RewritePatternSet patterns(&context); | ||
fir::populateSimplifyFIROperationsPatterns(patterns, | ||
preferInlineImplementation); | ||
mlir::GreedyRewriteConfig config; | ||
config.enableRegionSimplification = mlir::GreedySimplifyRegionLevel::Disabled; | ||
|
||
if (mlir::failed( | ||
mlir::applyPatternsGreedily(module, std::move(patterns), config))) { | ||
mlir::emitError(module.getLoc(), DEBUG_TYPE " pass failed"); | ||
signalPassFailure(); | ||
} | ||
} | ||
|
||
void fir::populateSimplifyFIROperationsPatterns( | ||
mlir::RewritePatternSet &patterns, bool preferInlineImplementation) { | ||
patterns.insert<IsContiguousBoxCoversion, BoxTotalElementsConversion>( | ||
patterns.getContext(), preferInlineImplementation); | ||
} |
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
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.
I think we could also canonicalize this for cases where the number of elements is known at compile time inside of the type e.g. !fir.box<!fir.array<10xi32>>