Index: flang/include/flang/Optimizer/Dialect/FIRDialect.h =================================================================== --- flang/include/flang/Optimizer/Dialect/FIRDialect.h +++ flang/include/flang/Optimizer/Dialect/FIRDialect.h @@ -10,12 +10,10 @@ // //===----------------------------------------------------------------------===// -#ifndef OPTIMIZER_DIALECT_FIRDIALECT_H -#define OPTIMIZER_DIALECT_FIRDIALECT_H +#ifndef FORTRAN_OPTIMIZER_DIALECT_FIRDIALECT_H +#define FORTRAN_OPTIMIZER_DIALECT_FIRDIALECT_H #include "mlir/IR/Dialect.h" -#include "mlir/InitAllDialects.h" -#include "mlir/InitAllPasses.h" namespace fir { @@ -36,47 +34,16 @@ mlir::DialectAsmPrinter &p) const override; }; -/// Register the dialect with the provided registry. -inline void registerFIRDialects(mlir::DialectRegistry ®istry) { - // clang-format off - registry.insert(); - // clang-format on -} - -/// Register the standard passes we use. This comes from registerAllPasses(), -/// but is a smaller set since we aren't using many of the passes found there. -inline void registerGeneralPasses() { - mlir::createCanonicalizerPass(); - mlir::createCSEPass(); - mlir::createSuperVectorizePass({}); - mlir::createLoopUnrollPass(); - mlir::createLoopUnrollAndJamPass(); - mlir::createSimplifyAffineStructuresPass(); - mlir::createLoopFusionPass(); - mlir::createLoopInvariantCodeMotionPass(); - mlir::createAffineLoopInvariantCodeMotionPass(); - mlir::createPipelineDataTransferPass(); - mlir::createLowerAffinePass(); - mlir::createLoopTilingPass(0); - mlir::createLoopCoalescingPass(); - mlir::createAffineDataCopyGenerationPass(0, 0); - mlir::createMemRefDataFlowOptPass(); - mlir::createStripDebugInfoPass(); - mlir::createPrintOpStatsPass(); - mlir::createInlinerPass(); - mlir::createSymbolDCEPass(); - mlir::createLocationSnapshotPass({}); -} +/// The FIR codegen dialect is a dialect containing a small set of transient +/// operations used exclusively during code generation. +class FIRCodeGenDialect final : public mlir::Dialect { +public: + explicit FIRCodeGenDialect(mlir::MLIRContext *ctx); + virtual ~FIRCodeGenDialect(); -inline void registerFIRPasses() { registerGeneralPasses(); } + static llvm::StringRef getDialectNamespace() { return "fircg"; } +}; } // namespace fir -#endif // OPTIMIZER_DIALECT_FIRDIALECT_H +#endif // FORTRAN_OPTIMIZER_DIALECT_FIRDIALECT_H Index: flang/include/flang/Optimizer/Support/InitFIR.h =================================================================== --- /dev/null +++ flang/include/flang/Optimizer/Support/InitFIR.h @@ -0,0 +1,76 @@ +//===-- Optimizer/Support/InitFIR.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 +// +//===----------------------------------------------------------------------===// +// +// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/ +// +//===----------------------------------------------------------------------===// + +#ifndef FORTRAN_OPTIMIZER_SUPPORT_INITFIR_H +#define FORTRAN_OPTIMIZER_SUPPORT_INITFIR_H + +#include "flang/Optimizer/Dialect/FIRDialect.h" +#include "mlir/Conversion/Passes.h" +#include "mlir/Dialect/Affine/Passes.h" +#include "mlir/InitAllDialects.h" +#include "mlir/Pass/Pass.h" +#include "mlir/Pass/PassRegistry.h" +#include "mlir/Transforms/LocationSnapshot.h" +#include "mlir/Transforms/Passes.h" + +namespace fir::support { + +// The definitive list of dialects used by flang. +#define FLANG_DIALECT_LIST \ + mlir::AffineDialect, FIROpsDialect, mlir::LLVM::LLVMDialect, \ + mlir::acc::OpenACCDialect, mlir::omp::OpenMPDialect, \ + mlir::scf::SCFDialect, mlir::StandardOpsDialect, \ + mlir::vector::VectorDialect + +/// Register all the dialects used by flang. +inline void registerDialects(mlir::DialectRegistry ®istry) { + registry.insert(); +} + +/// Forced load of all the dialects used by flang. Lowering is not an MLIR +/// pass, but a producer of FIR and MLIR. It is therefore a requirement that the +/// dialects be preloaded to be able to build the IR. +inline void loadDialects(mlir::MLIRContext &context) { + context.loadDialect(); +} + +/// Register the standard passes we use. This comes from registerAllPasses(), +/// but is a smaller set since we aren't using many of the passes found there. +inline void registerFIRPasses() { + mlir::registerCanonicalizerPass(); + mlir::registerCSEPass(); + mlir::registerAffineLoopFusionPass(); + mlir::registerLoopInvariantCodeMotionPass(); + mlir::registerLoopCoalescingPass(); + mlir::registerStripDebugInfoPass(); + mlir::registerPrintOpStatsPass(); + mlir::registerInlinerPass(); + mlir::registerSCCPPass(); + mlir::registerMemRefDataFlowOptPass(); + mlir::registerSymbolDCEPass(); + mlir::registerLocationSnapshotPass(); + mlir::registerAffinePipelineDataTransferPass(); + + mlir::registerAffineVectorizePass(); + mlir::registerAffineLoopUnrollPass(); + mlir::registerAffineLoopUnrollAndJamPass(); + mlir::registerSimplifyAffineStructuresPass(); + mlir::registerAffineLoopInvariantCodeMotionPass(); + mlir::registerAffineLoopTilingPass(); + mlir::registerAffineDataCopyGenerationPass(); + + mlir::registerConvertAffineToStandardPass(); +} + +} // namespace fir::support + +#endif // FORTRAN_OPTIMIZER_SUPPORT_INITFIR_H Index: flang/tools/tco/tco.cpp =================================================================== --- flang/tools/tco/tco.cpp +++ flang/tools/tco/tco.cpp @@ -11,7 +11,7 @@ // //===----------------------------------------------------------------------===// -#include "flang/Optimizer/Dialect/FIRDialect.h" +#include "flang/Optimizer/Support/InitFIR.h" #include "flang/Optimizer/Support/KindMapping.h" #include "mlir/IR/BuiltinOps.h" #include "mlir/IR/MLIRContext.h" @@ -62,7 +62,7 @@ SourceMgr sourceMgr; sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc()); mlir::DialectRegistry registry; - fir::registerFIRDialects(registry); + fir::support::registerDialects(registry); mlir::MLIRContext context(registry); auto owningRef = mlir::parseSourceFile(sourceMgr, &context); @@ -106,7 +106,7 @@ } int main(int argc, char **argv) { - fir::registerFIRPasses(); + fir::support::registerFIRPasses(); [[maybe_unused]] InitLLVM y(argc, argv); mlir::registerPassManagerCLOptions(); mlir::PassPipelineCLParser passPipe("", "Compiler passes to run");