diff --git a/flang/include/flang/Frontend/CompilerInvocation.h b/flang/include/flang/Frontend/CompilerInvocation.h --- a/flang/include/flang/Frontend/CompilerInvocation.h +++ b/flang/include/flang/Frontend/CompilerInvocation.h @@ -17,6 +17,7 @@ #include "flang/Frontend/FrontendOptions.h" #include "flang/Frontend/PreprocessorOptions.h" #include "flang/Frontend/TargetOptions.h" +#include "flang/Lower/LoweringOptions.h" #include "flang/Parser/parsing.h" #include "flang/Semantics/semantics.h" #include "clang/Basic/Diagnostic.h" @@ -68,6 +69,9 @@ // of options. Fortran::parser::Options parserOpts; + /// Options controlling lowering. + Fortran::lower::LoweringOptions loweringOpts; + /// Options controlling the target. Fortran::frontend::TargetOptions targetOpts; @@ -136,6 +140,11 @@ CodeGenOptions &getCodeGenOpts() { return codeGenOpts; } const CodeGenOptions &getCodeGenOpts() const { return codeGenOpts; } + Fortran::lower::LoweringOptions &getLoweringOpts() { return loweringOpts; } + const Fortran::lower::LoweringOptions &getLoweringOpts() const { + return loweringOpts; + } + Fortran::semantics::SemanticsContext &getSemanticsContext() { return *semanticsContext; } @@ -226,6 +235,10 @@ /// Set the Semantic Options void setSemanticsOpts(Fortran::parser::AllCookedSources &); + + /// Set \p loweringOptions controlling lowering behavior based + /// on the \p optimizationLevel. + void setLoweringOptions(); }; } // end namespace Fortran::frontend diff --git a/flang/include/flang/Lower/AbstractConverter.h b/flang/include/flang/Lower/AbstractConverter.h --- a/flang/include/flang/Lower/AbstractConverter.h +++ b/flang/include/flang/Lower/AbstractConverter.h @@ -14,6 +14,7 @@ #define FORTRAN_LOWER_ABSTRACTCONVERTER_H #include "flang/Common/Fortran.h" +#include "flang/Lower/LoweringOptions.h" #include "flang/Lower/PFTDefs.h" #include "flang/Optimizer/Builder/BoxValue.h" #include "flang/Semantics/symbol.h" @@ -225,7 +226,22 @@ /// Get the KindMap. virtual const fir::KindMapping &getKindMap() = 0; + AbstractConverter(const Fortran::lower::LoweringOptions &loweringOptions) + : loweringOptions(loweringOptions) {} virtual ~AbstractConverter() = default; + + //===--------------------------------------------------------------------===// + // Miscellaneous + //===--------------------------------------------------------------------===// + + /// Return options controlling lowering behavior. + const Fortran::lower::LoweringOptions &getLoweringOptions() const { + return loweringOptions; + } + +private: + /// Options controlling lowering behavior. + const Fortran::lower::LoweringOptions &loweringOptions; }; } // namespace lower diff --git a/flang/include/flang/Lower/Bridge.h b/flang/include/flang/Lower/Bridge.h --- a/flang/include/flang/Lower/Bridge.h +++ b/flang/include/flang/Lower/Bridge.h @@ -15,6 +15,7 @@ #include "flang/Common/Fortran.h" #include "flang/Lower/AbstractConverter.h" +#include "flang/Lower/LoweringOptions.h" #include "flang/Optimizer/Builder/FIRBuilder.h" #include "flang/Optimizer/Support/KindMapping.h" #include "mlir/IR/BuiltinOps.h" @@ -52,9 +53,10 @@ const Fortran::evaluate::IntrinsicProcTable &intrinsics, const Fortran::evaluate::TargetCharacteristics &targetCharacteristics, const Fortran::parser::AllCookedSources &allCooked, - llvm::StringRef triple, fir::KindMapping &kindMap) { + llvm::StringRef triple, fir::KindMapping &kindMap, + const Fortran::lower::LoweringOptions &loweringOptions) { return LoweringBridge(ctx, defaultKinds, intrinsics, targetCharacteristics, - allCooked, triple, kindMap); + allCooked, triple, kindMap, loweringOptions); } //===--------------------------------------------------------------------===// @@ -83,6 +85,10 @@ /// Get the kind map. const fir::KindMapping &getKindMap() const { return kindMap; } + const Fortran::lower::LoweringOptions &getLoweringOptions() const { + return loweringOptions; + } + /// Create a folding context. Careful: this is very expensive. Fortran::evaluate::FoldingContext createFoldingContext() const; @@ -107,7 +113,8 @@ const Fortran::evaluate::IntrinsicProcTable &intrinsics, const Fortran::evaluate::TargetCharacteristics &targetCharacteristics, const Fortran::parser::AllCookedSources &cooked, llvm::StringRef triple, - fir::KindMapping &kindMap); + fir::KindMapping &kindMap, + const Fortran::lower::LoweringOptions &loweringOptions); LoweringBridge() = delete; LoweringBridge(const LoweringBridge &) = delete; @@ -118,6 +125,7 @@ mlir::MLIRContext &context; std::unique_ptr module; fir::KindMapping &kindMap; + const Fortran::lower::LoweringOptions &loweringOptions; }; } // namespace lower diff --git a/flang/include/flang/Lower/LoweringOptions.h b/flang/include/flang/Lower/LoweringOptions.h new file mode 100644 --- /dev/null +++ b/flang/include/flang/Lower/LoweringOptions.h @@ -0,0 +1,36 @@ +//===- LoweringOptions.h ----------------------------------------*- C++ -*-===// +// +// 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 +/// Options controlling lowering of front-end fragments to the FIR dialect +/// of MLIR +/// +//===----------------------------------------------------------------------===// + +#ifndef FLANG_LOWER_LOWERINGOPTIONS_H +#define FLANG_LOWER_LOWERINGOPTIONS_H + +namespace Fortran::lower { + +class LoweringOptions { + /// If true, lower transpose without a runtime call. + unsigned optimizeTranspose : 1; + +public: + LoweringOptions() : optimizeTranspose(true) {} + + bool getOptimizeTranspose() const { return optimizeTranspose; } + LoweringOptions &setOptimizeTranspose(bool v) { + optimizeTranspose = v; + return *this; + } +}; + +} // namespace Fortran::lower + +#endif // FLANG_LOWER_LOWERINGOPTIONS_H diff --git a/flang/lib/Frontend/CompilerInstance.cpp b/flang/lib/Frontend/CompilerInstance.cpp --- a/flang/lib/Frontend/CompilerInstance.cpp +++ b/flang/lib/Frontend/CompilerInstance.cpp @@ -151,6 +151,8 @@ allSources->set_encoding(invoc.getFortranOpts().encoding); // Create the semantics context and set semantic options. invoc.setSemanticsOpts(*this->allCookedSources); + // Set options controlling lowering to FIR. + invoc.setLoweringOptions(); // Run the frontend action `act` for every input file. for (const FrontendInputFile &fif : getFrontendOpts().inputs) { diff --git a/flang/lib/Frontend/CompilerInvocation.cpp b/flang/lib/Frontend/CompilerInvocation.cpp --- a/flang/lib/Frontend/CompilerInvocation.cpp +++ b/flang/lib/Frontend/CompilerInvocation.cpp @@ -794,3 +794,12 @@ .set_warningsAreErrors(getWarnAsErr()) .set_moduleFileSuffix(getModuleFileSuffix()); } + +/// Set \p loweringOptions controlling lowering behavior based +/// on the \p optimizationLevel. +void CompilerInvocation::setLoweringOptions() { + const auto &codegenOpts = getCodeGenOpts(); + + // Lower TRANSPOSE as a runtime call under -O0. + loweringOpts.setOptimizeTranspose(codegenOpts.OptimizationLevel > 0); +} diff --git a/flang/lib/Frontend/FrontendActions.cpp b/flang/lib/Frontend/FrontendActions.cpp --- a/flang/lib/Frontend/FrontendActions.cpp +++ b/flang/lib/Frontend/FrontendActions.cpp @@ -148,7 +148,7 @@ *mlirCtx, defKinds, ci.getInvocation().getSemanticsContext().intrinsics(), ci.getInvocation().getSemanticsContext().targetCharacteristics(), ci.getParsing().allCooked(), ci.getInvocation().getTargetOpts().triple, - kindMap); + kindMap, ci.getInvocation().getLoweringOpts()); // Create a parse tree and lower it to FIR Fortran::parser::Program &parseTree{*ci.getParsing().parseTree()}; diff --git a/flang/lib/Lower/Bridge.cpp b/flang/lib/Lower/Bridge.cpp --- a/flang/lib/Lower/Bridge.cpp +++ b/flang/lib/Lower/Bridge.cpp @@ -179,7 +179,8 @@ class FirConverter : public Fortran::lower::AbstractConverter { public: explicit FirConverter(Fortran::lower::LoweringBridge &bridge) - : bridge{bridge}, foldingContext{bridge.createFoldingContext()} {} + : Fortran::lower::AbstractConverter(bridge.getLoweringOptions()), + bridge{bridge}, foldingContext{bridge.createFoldingContext()} {} virtual ~FirConverter() = default; /// Convert the PFT to FIR. @@ -3243,10 +3244,11 @@ const Fortran::evaluate::IntrinsicProcTable &intrinsics, const Fortran::evaluate::TargetCharacteristics &targetCharacteristics, const Fortran::parser::AllCookedSources &cooked, llvm::StringRef triple, - fir::KindMapping &kindMap) + fir::KindMapping &kindMap, + const Fortran::lower::LoweringOptions &loweringOptions) : defaultKinds{defaultKinds}, intrinsics{intrinsics}, targetCharacteristics{targetCharacteristics}, cooked{&cooked}, - context{context}, kindMap{kindMap} { + context{context}, kindMap{kindMap}, loweringOptions{loweringOptions} { // Register the diagnostic handler. context.getDiagEngine().registerHandler([](mlir::Diagnostic &diag) { llvm::raw_ostream &os = llvm::errs(); diff --git a/flang/lib/Lower/ConvertExpr.cpp b/flang/lib/Lower/ConvertExpr.cpp --- a/flang/lib/Lower/ConvertExpr.cpp +++ b/flang/lib/Lower/ConvertExpr.cpp @@ -88,8 +88,12 @@ // from the "inline" FIR, e.g. it may diagnose out-of-memory conditions // during the temporary allocation whereas the inline implementation // relies on AllocMemOp that will silently return null in case -// there is not enough memory. So it may be a good idea to set -// this option to false for -O0. +// there is not enough memory. +// +// If it is set to false, then TRANSPOSE will be lowered using +// a runtime call. If it is set to true, then the lowering is controlled +// by LoweringOptions::optimizeTranspose bit (see isTransposeOptEnabled +// function in this file). static llvm::cl::opt optimizeTranspose( "opt-transpose", llvm::cl::desc("lower transpose without using a runtime call"), @@ -595,36 +599,50 @@ module->name().ToString().find("omp_lib") == std::string::npos; } +// Return true if TRANSPOSE should be lowered without a runtime call. +static bool +isTransposeOptEnabled(const Fortran::lower::AbstractConverter &converter) { + return optimizeTranspose && + converter.getLoweringOptions().getOptimizeTranspose(); +} + // A set of visitors to detect if the given expression // is a TRANSPOSE call that should be lowered without using // runtime TRANSPOSE implementation. template -static bool isOptimizableTranspose(const T &) { +static bool isOptimizableTranspose(const T &, + const Fortran::lower::AbstractConverter &) { return false; } static bool -isOptimizableTranspose(const Fortran::evaluate::ProcedureRef &procRef) { +isOptimizableTranspose(const Fortran::evaluate::ProcedureRef &procRef, + const Fortran::lower::AbstractConverter &converter) { const Fortran::evaluate::SpecificIntrinsic *intrin = procRef.proc().GetSpecificIntrinsic(); - return optimizeTranspose && intrin && intrin->name == "transpose"; + return isTransposeOptEnabled(converter) && intrin && + intrin->name == "transpose"; } template static bool -isOptimizableTranspose(const Fortran::evaluate::FunctionRef &funcRef) { +isOptimizableTranspose(const Fortran::evaluate::FunctionRef &funcRef, + const Fortran::lower::AbstractConverter &converter) { return isOptimizableTranspose( - static_cast(funcRef)); + static_cast(funcRef), converter); } template -static bool isOptimizableTranspose(Fortran::evaluate::Expr expr) { +static bool +isOptimizableTranspose(Fortran::evaluate::Expr expr, + const Fortran::lower::AbstractConverter &converter) { // If optimizeTranspose is not enabled, return false right away. - if (!optimizeTranspose) + if (!isTransposeOptEnabled(converter)) return false; - return std::visit([&](const auto &e) { return isOptimizableTranspose(e); }, - expr.u); + return std::visit( + [&](const auto &e) { return isOptimizableTranspose(e, converter); }, + expr.u); } namespace { @@ -3289,7 +3307,7 @@ // is used to not create a new temporary storage. if (isScalar(x) || Fortran::evaluate::UnwrapWholeSymbolOrComponentDataRef(x) || - (isTransformationalRef(x) && !isOptimizableTranspose(x))) + (isTransformationalRef(x) && !isOptimizableTranspose(x, converter))) return std::visit([&](const auto &e) { return genref(e); }, x.u); if (useBoxArg) return asArrayArg(x); @@ -5139,7 +5157,7 @@ llvm::Optional retTy) { mlir::Location loc = getLoc(); - if (isOptimizableTranspose(procRef)) + if (isOptimizableTranspose(procRef, converter)) return genTransposeProcRef(procRef); if (procRef.IsElemental()) { diff --git a/flang/test/Lower/Intrinsics/transpose.f90 b/flang/test/Lower/Intrinsics/transpose.f90 --- a/flang/test/Lower/Intrinsics/transpose.f90 +++ b/flang/test/Lower/Intrinsics/transpose.f90 @@ -1,4 +1,5 @@ ! RUN: bbc -emit-fir %s -opt-transpose=false -o - | FileCheck %s +! RUN: %flang_fc1 -emit-fir -O0 %s -o - | FileCheck %s ! CHECK-LABEL: func @_QPtranspose_test( ! CHECK-SAME: %[[source:.*]]: !fir.ref>{{.*}}) { diff --git a/flang/test/Lower/Intrinsics/transpose_opt.f90 b/flang/test/Lower/Intrinsics/transpose_opt.f90 --- a/flang/test/Lower/Intrinsics/transpose_opt.f90 +++ b/flang/test/Lower/Intrinsics/transpose_opt.f90 @@ -1,4 +1,8 @@ ! RUN: bbc -emit-fir %s -opt-transpose=true -o - | FileCheck %s +! RUN: bbc -emit-fir %s -o - | FileCheck %s +! RUN: %flang_fc1 -emit-fir -O1 %s -o - | FileCheck %s +! RUN: %flang_fc1 -emit-fir -O2 %s -o - | FileCheck %s +! RUN: %flang_fc1 -emit-fir -O3 %s -o - | FileCheck %s ! CHECK-LABEL: func.func @_QPtranspose_test( ! CHECK-SAME: %[[VAL_0:.*]]: !fir.ref> {fir.bindc_name = "mat"}) { diff --git a/flang/tools/bbc/bbc.cpp b/flang/tools/bbc/bbc.cpp --- a/flang/tools/bbc/bbc.cpp +++ b/flang/tools/bbc/bbc.cpp @@ -217,10 +217,12 @@ auto &defKinds = semanticsContext.defaultKinds(); fir::KindMapping kindMap( &ctx, llvm::ArrayRef{fir::fromDefaultKinds(defKinds)}); + // Use default lowering options for bbc. + Fortran::lower::LoweringOptions loweringOptions{}; auto burnside = Fortran::lower::LoweringBridge::create( ctx, defKinds, semanticsContext.intrinsics(), semanticsContext.targetCharacteristics(), parsing.allCooked(), "", - kindMap); + kindMap, loweringOptions); burnside.lower(parseTree, semanticsContext); mlir::ModuleOp mlirModule = burnside.getModule(); std::error_code ec;