-
Notifications
You must be signed in to change notification settings - Fork 15.7k
[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
Changes from 2 commits
4d0f1b5
7213313
1c8d7c5
55fd283
5cf9754
41f5cee
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -4671,6 +4671,79 @@ 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(); | ||
| } | ||
|
|
||
| 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(); | ||
| } | ||
|
Contributor
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). |
||
|
|
||
| return mlir::failure(); | ||
| } | ||
|
|
||
| void fir::BoxTotalElementsOp::getCanonicalizationPatterns( | ||
| mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) { | ||
| patterns.add<SimplifyBoxTotalElementsOp>(context); | ||
| } | ||
|
|
||
| //===----------------------------------------------------------------------===// | ||
| // FIROpsDialect | ||
| //===----------------------------------------------------------------------===// | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| //===- 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/BoxValue.h" | ||
| #include "flang/Optimizer/Builder/CUFCommon.h" | ||
| #include "flang/Optimizer/Builder/FIRBuilder.h" | ||
| #include "flang/Optimizer/Builder/LowLevelIntrinsics.h" | ||
| #include "flang/Optimizer/Builder/Todo.h" | ||
| #include "flang/Optimizer/Dialect/FIROps.h" | ||
| #include "flang/Optimizer/Dialect/FIRType.h" | ||
| #include "flang/Optimizer/Dialect/Support/FIRContext.h" | ||
| #include "flang/Optimizer/HLFIR/HLFIRDialect.h" | ||
| #include "flang/Optimizer/Transforms/Passes.h" | ||
| #include "flang/Optimizer/Transforms/Utils.h" | ||
| #include "flang/Runtime/entry-names.h" | ||
| #include "flang/Support/Fortran.h" | ||
| #include "mlir/IR/Matchers.h" | ||
| #include "mlir/IR/Operation.h" | ||
| #include "mlir/Pass/Pass.h" | ||
| #include "mlir/Transforms/DialectConversion.h" | ||
|
|
||
| #include "flang/Optimizer/Builder/Runtime/Inquiry.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.getI8Type(), 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::success(); | ||
| } | ||
|
|
||
| /// 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::success(); | ||
| } | ||
|
|
||
| void SimplifyFIROperationsPass::runOnOperation() { | ||
| mlir::ModuleOp module = getOperation(); | ||
| mlir::MLIRContext &context = getContext(); | ||
| mlir::ConversionTarget target(context); | ||
| target.addIllegalOp<fir::IsContiguousBoxOp>(); | ||
| target.addIllegalOp<fir::BoxTotalElementsOp>(); | ||
| mlir::RewritePatternSet patterns(&context); | ||
| fir::populateSimplifyFIROperationsPatterns(patterns, | ||
| preferInlineImplementation); | ||
| if (mlir::failed( | ||
| mlir::applyPartialConversion(module, target, std::move(patterns)))) { | ||
|
||
| mlir::emitError(module.getLoc(), DEBUG_TYPE " pass failed"); | ||
| signalPassFailure(); | ||
| } | ||
| } | ||
|
|
||
| void fir::populateSimplifyFIROperationsPatterns( | ||
| mlir::RewritePatternSet &patterns, bool preferInlineImplementation) { | ||
| patterns.insert<IsContiguousBoxCoversion, BoxTotalElementsConversion>( | ||
| patterns.getContext(), preferInlineImplementation); | ||
| } | ||
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>>