|
| 1 | +//===- SimplifyFIROperations.cpp -- simplify complex FIR operations ------===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | + |
| 9 | +//===----------------------------------------------------------------------===// |
| 10 | +/// \file |
| 11 | +/// This pass transforms some FIR operations into their equivalent |
| 12 | +/// implementations using other FIR operations. The transformation |
| 13 | +/// can legally use SCF dialect and generate Fortran runtime calls. |
| 14 | +//===----------------------------------------------------------------------===// |
| 15 | + |
| 16 | +#include "flang/Optimizer/Builder/FIRBuilder.h" |
| 17 | +#include "flang/Optimizer/Builder/Runtime/Inquiry.h" |
| 18 | +#include "flang/Optimizer/Builder/Todo.h" |
| 19 | +#include "flang/Optimizer/Dialect/FIROps.h" |
| 20 | +#include "flang/Optimizer/Transforms/Passes.h" |
| 21 | +#include "mlir/Pass/Pass.h" |
| 22 | +#include "mlir/Transforms/GreedyPatternRewriteDriver.h" |
| 23 | + |
| 24 | +namespace fir { |
| 25 | +#define GEN_PASS_DEF_SIMPLIFYFIROPERATIONS |
| 26 | +#include "flang/Optimizer/Transforms/Passes.h.inc" |
| 27 | +} // namespace fir |
| 28 | + |
| 29 | +#define DEBUG_TYPE "flang-simplify-fir-operations" |
| 30 | + |
| 31 | +namespace { |
| 32 | +/// Pass runner. |
| 33 | +class SimplifyFIROperationsPass |
| 34 | + : public fir::impl::SimplifyFIROperationsBase<SimplifyFIROperationsPass> { |
| 35 | +public: |
| 36 | + using fir::impl::SimplifyFIROperationsBase< |
| 37 | + SimplifyFIROperationsPass>::SimplifyFIROperationsBase; |
| 38 | + |
| 39 | + void runOnOperation() override final; |
| 40 | +}; |
| 41 | + |
| 42 | +/// Base class for all conversions holding the pass options. |
| 43 | +template <typename Op> |
| 44 | +class ConversionBase : public mlir::OpRewritePattern<Op> { |
| 45 | +public: |
| 46 | + using mlir::OpRewritePattern<Op>::OpRewritePattern; |
| 47 | + |
| 48 | + template <typename... Args> |
| 49 | + ConversionBase(mlir::MLIRContext *context, Args &&...args) |
| 50 | + : mlir::OpRewritePattern<Op>(context), |
| 51 | + options{std::forward<Args>(args)...} {} |
| 52 | + |
| 53 | + mlir::LogicalResult matchAndRewrite(Op, |
| 54 | + mlir::PatternRewriter &) const override; |
| 55 | + |
| 56 | +protected: |
| 57 | + fir::SimplifyFIROperationsOptions options; |
| 58 | +}; |
| 59 | + |
| 60 | +/// fir::IsContiguousBoxOp converter. |
| 61 | +using IsContiguousBoxCoversion = ConversionBase<fir::IsContiguousBoxOp>; |
| 62 | + |
| 63 | +/// fir::BoxTotalElementsOp converter. |
| 64 | +using BoxTotalElementsConversion = ConversionBase<fir::BoxTotalElementsOp>; |
| 65 | +} // namespace |
| 66 | + |
| 67 | +/// Generate a call to IsContiguous/IsContiguousUpTo function or an inline |
| 68 | +/// sequence reading extents/strides from the box and checking them. |
| 69 | +/// This conversion may produce fir.box_elesize and a loop (for assumed |
| 70 | +/// rank). |
| 71 | +template <> |
| 72 | +mlir::LogicalResult IsContiguousBoxCoversion::matchAndRewrite( |
| 73 | + fir::IsContiguousBoxOp op, mlir::PatternRewriter &rewriter) const { |
| 74 | + mlir::Location loc = op.getLoc(); |
| 75 | + fir::FirOpBuilder builder(rewriter, op.getOperation()); |
| 76 | + // TODO: support preferInlineImplementation. |
| 77 | + bool doInline = options.preferInlineImplementation && false; |
| 78 | + if (!doInline) { |
| 79 | + // Generate Fortran runtime call. |
| 80 | + mlir::Value result; |
| 81 | + if (op.getInnermost()) { |
| 82 | + mlir::Value one = |
| 83 | + builder.createIntegerConstant(loc, builder.getI32Type(), 1); |
| 84 | + result = |
| 85 | + fir::runtime::genIsContiguousUpTo(builder, loc, op.getBox(), one); |
| 86 | + } else { |
| 87 | + result = fir::runtime::genIsContiguous(builder, loc, op.getBox()); |
| 88 | + } |
| 89 | + result = builder.createConvert(loc, op.getType(), result); |
| 90 | + rewriter.replaceOp(op, result); |
| 91 | + return mlir::success(); |
| 92 | + } |
| 93 | + |
| 94 | + // Generate inline implementation. |
| 95 | + TODO(loc, "inline IsContiguousBoxOp"); |
| 96 | + return mlir::failure(); |
| 97 | +} |
| 98 | + |
| 99 | +/// Generate a call to Size runtime function or an inline |
| 100 | +/// sequence reading extents from the box an multiplying them. |
| 101 | +/// This conversion may produce a loop (for assumed rank). |
| 102 | +template <> |
| 103 | +mlir::LogicalResult BoxTotalElementsConversion::matchAndRewrite( |
| 104 | + fir::BoxTotalElementsOp op, mlir::PatternRewriter &rewriter) const { |
| 105 | + mlir::Location loc = op.getLoc(); |
| 106 | + fir::FirOpBuilder builder(rewriter, op.getOperation()); |
| 107 | + // TODO: support preferInlineImplementation. |
| 108 | + // Reading the extent from the box for 1D arrays probably |
| 109 | + // results in less code than the call, so we can always |
| 110 | + // inline it. |
| 111 | + bool doInline = options.preferInlineImplementation && false; |
| 112 | + if (!doInline) { |
| 113 | + // Generate Fortran runtime call. |
| 114 | + mlir::Value result = fir::runtime::genSize(builder, loc, op.getBox()); |
| 115 | + result = builder.createConvert(loc, op.getType(), result); |
| 116 | + rewriter.replaceOp(op, result); |
| 117 | + return mlir::success(); |
| 118 | + } |
| 119 | + |
| 120 | + // Generate inline implementation. |
| 121 | + TODO(loc, "inline BoxTotalElementsOp"); |
| 122 | + return mlir::failure(); |
| 123 | +} |
| 124 | + |
| 125 | +void SimplifyFIROperationsPass::runOnOperation() { |
| 126 | + mlir::ModuleOp module = getOperation(); |
| 127 | + mlir::MLIRContext &context = getContext(); |
| 128 | + mlir::RewritePatternSet patterns(&context); |
| 129 | + fir::populateSimplifyFIROperationsPatterns(patterns, |
| 130 | + preferInlineImplementation); |
| 131 | + mlir::GreedyRewriteConfig config; |
| 132 | + config.enableRegionSimplification = mlir::GreedySimplifyRegionLevel::Disabled; |
| 133 | + |
| 134 | + if (mlir::failed( |
| 135 | + mlir::applyPatternsGreedily(module, std::move(patterns), config))) { |
| 136 | + mlir::emitError(module.getLoc(), DEBUG_TYPE " pass failed"); |
| 137 | + signalPassFailure(); |
| 138 | + } |
| 139 | +} |
| 140 | + |
| 141 | +void fir::populateSimplifyFIROperationsPatterns( |
| 142 | + mlir::RewritePatternSet &patterns, bool preferInlineImplementation) { |
| 143 | + patterns.insert<IsContiguousBoxCoversion, BoxTotalElementsConversion>( |
| 144 | + patterns.getContext(), preferInlineImplementation); |
| 145 | +} |
0 commit comments