llvm-project/flang/tools/bbc/bbc.cpp

254 lines
9.6 KiB
C++

//===- bbc.cpp - Burnside Bridge Compiler -----------------------*- 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/
//
//===----------------------------------------------------------------------===//
///
/// This is a tool for translating Fortran sources to the FIR dialect of MLIR.
///
//===----------------------------------------------------------------------===//
#include "flang/Common/Fortran-features.h"
#include "flang/Common/default-kinds.h"
#include "flang/Lower/Bridge.h"
#include "flang/Lower/PFTBuilder.h"
#include "flang/Lower/Support/Verifier.h"
#include "flang/Optimizer/Support/FIRContext.h"
#include "flang/Optimizer/Support/InitFIR.h"
#include "flang/Optimizer/Support/InternalNames.h"
#include "flang/Optimizer/Support/KindMapping.h"
#include "flang/Optimizer/Support/Utils.h"
#include "flang/Optimizer/Transforms/Passes.h"
#include "flang/Parser/characters.h"
#include "flang/Parser/dump-parse-tree.h"
#include "flang/Parser/message.h"
#include "flang/Parser/parse-tree-visitor.h"
#include "flang/Parser/parse-tree.h"
#include "flang/Parser/parsing.h"
#include "flang/Parser/provenance.h"
#include "flang/Parser/unparse.h"
#include "flang/Semantics/expression.h"
#include "flang/Semantics/runtime-type-info.h"
#include "flang/Semantics/semantics.h"
#include "flang/Semantics/unparse-with-symbols.h"
#include "flang/Version.inc"
#include "mlir/Conversion/SCFToStandard/SCFToStandard.h"
#include "mlir/IR/AsmState.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/MLIRContext.h"
#include "mlir/Parser.h"
#include "mlir/Pass/Pass.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/PassRegistry.h"
#include "mlir/Transforms/GreedyPatternRewriteDriver.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/ErrorOr.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/InitLLVM.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/Support/Path.h"
#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/TargetSelect.h"
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/raw_ostream.h"
//===----------------------------------------------------------------------===//
// Some basic command-line options
//===----------------------------------------------------------------------===//
static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
llvm::cl::Required,
llvm::cl::desc("<input file>"));
static llvm::cl::opt<std::string>
outputFilename("o", llvm::cl::desc("Specify the output filename"),
llvm::cl::value_desc("filename"));
static llvm::cl::opt<bool>
emitFIR("emit-fir",
llvm::cl::desc("Dump the FIR created by lowering and exit"),
llvm::cl::init(false));
static llvm::cl::opt<bool> pftDumpTest(
"pft-test",
llvm::cl::desc("parse the input, create a PFT, dump it, and exit"),
llvm::cl::init(false));
#define FLANG_EXCLUDE_CODEGEN
#include "flang/Tools/CLOptions.inc"
//===----------------------------------------------------------------------===//
using ProgramName = std::string;
// Print the module without the "module { ... }" wrapper.
static void printModule(mlir::ModuleOp mlirModule, llvm::raw_ostream &out) {
for (auto &op : *mlirModule.getBody())
out << op << '\n';
out << '\n';
}
static void registerAllPasses() {
fir::support::registerMLIRPassesForFortranTools();
fir::registerOptTransformPasses();
}
//===----------------------------------------------------------------------===//
// Translate Fortran input to FIR, a dialect of MLIR.
//===----------------------------------------------------------------------===//
static mlir::LogicalResult convertFortranSourceToMLIR(
std::string path, Fortran::parser::Options options,
const ProgramName &programPrefix,
Fortran::semantics::SemanticsContext &semanticsContext,
const mlir::PassPipelineCLParser &passPipeline) {
// prep for prescan and parse
Fortran::parser::Parsing parsing{semanticsContext.allCookedSources()};
parsing.Prescan(path, options);
if (!parsing.messages().empty() && (parsing.messages().AnyFatalError())) {
llvm::errs() << programPrefix << "could not scan " << path << '\n';
parsing.messages().Emit(llvm::errs(), parsing.allCooked());
return mlir::failure();
}
// parse the input Fortran
parsing.Parse(llvm::outs());
parsing.messages().Emit(llvm::errs(), parsing.allCooked());
if (!parsing.consumedWholeFile()) {
parsing.EmitMessage(llvm::errs(), parsing.finalRestingPlace(),
"parser FAIL (final position)");
return mlir::failure();
}
if ((!parsing.messages().empty() && (parsing.messages().AnyFatalError())) ||
!parsing.parseTree().has_value()) {
llvm::errs() << programPrefix << "could not parse " << path << '\n';
return mlir::failure();
}
// run semantics
auto &parseTree = *parsing.parseTree();
Fortran::semantics::Semantics semantics(semanticsContext, parseTree);
semantics.Perform();
semantics.EmitMessages(llvm::errs());
if (semantics.AnyFatalError()) {
llvm::errs() << programPrefix << "semantic errors in " << path << '\n';
return mlir::failure();
}
Fortran::semantics::RuntimeDerivedTypeTables tables;
if (!semantics.AnyFatalError()) {
tables =
Fortran::semantics::BuildRuntimeDerivedTypeTables(semanticsContext);
if (!tables.schemata)
llvm::errs() << programPrefix
<< "could not find module file for __fortran_type_info\n";
}
if (pftDumpTest) {
if (auto ast = Fortran::lower::createPFT(parseTree, semanticsContext)) {
Fortran::lower::dumpPFT(llvm::outs(), *ast);
return mlir::success();
}
llvm::errs() << "Pre FIR Tree is NULL.\n";
return mlir::failure();
}
// translate to FIR dialect of MLIR
mlir::DialectRegistry registry;
fir::support::registerNonCodegenDialects(registry);
mlir::MLIRContext ctx(registry);
fir::support::loadNonCodegenDialects(ctx);
auto &defKinds = semanticsContext.defaultKinds();
fir::KindMapping kindMap(
&ctx, llvm::ArrayRef<fir::KindTy>{fir::fromDefaultKinds(defKinds)});
auto burnside = Fortran::lower::LoweringBridge::create(
ctx, defKinds, semanticsContext.intrinsics(), parsing.allCooked(), "",
kindMap);
burnside.lower(parseTree, semanticsContext);
mlir::ModuleOp mlirModule = burnside.getModule();
std::error_code ec;
std::string outputName = outputFilename;
if (!outputName.size())
outputName = llvm::sys::path::stem(inputFilename).str().append(".mlir");
llvm::raw_fd_ostream out(outputName, ec);
if (ec)
return mlir::emitError(mlir::UnknownLoc::get(&ctx),
"could not open output file ")
<< outputName;
// Otherwise run the default passes.
mlir::PassManager pm(&ctx, mlir::OpPassManager::Nesting::Implicit);
pm.enableVerifier(/*verifyPasses=*/true);
mlir::applyPassManagerCLOptions(pm);
if (passPipeline.hasAnyOccurrences()) {
// run the command-line specified pipeline
(void)passPipeline.addToPipeline(pm, [&](const llvm::Twine &msg) {
mlir::emitError(mlir::UnknownLoc::get(&ctx)) << msg;
return mlir::failure();
});
} else if (emitFIR) {
// --emit-fir: Build the IR, verify it, and dump the IR if the IR passes
// verification. Use --dump-module-on-failure to dump invalid IR.
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>());
if (mlir::failed(pm.run(mlirModule))) {
llvm::errs() << "FATAL: verification of lowering to FIR failed";
return mlir::failure();
}
printModule(mlirModule, out);
return mlir::success();
} else {
// run the default canned pipeline
pm.addPass(std::make_unique<Fortran::lower::VerifierPass>());
// Add default optimizer pass pipeline.
fir::createDefaultFIROptimizerPassPipeline(pm);
}
if (mlir::succeeded(pm.run(mlirModule))) {
// Emit MLIR and do not lower to LLVM IR.
printModule(mlirModule, out);
return mlir::success();
}
// Something went wrong. Try to dump the MLIR module.
llvm::errs() << "oops, pass manager reported failure\n";
return mlir::failure();
}
int main(int argc, char **argv) {
[[maybe_unused]] llvm::InitLLVM y(argc, argv);
registerAllPasses();
mlir::registerMLIRContextCLOptions();
mlir::registerPassManagerCLOptions();
mlir::PassPipelineCLParser passPipe("", "Compiler passes to run");
llvm::cl::ParseCommandLineOptions(argc, argv, "Burnside Bridge Compiler\n");
ProgramName programPrefix;
programPrefix = argv[0] + ": "s;
Fortran::parser::Options options;
options.predefinitions.emplace_back("__flang__", "1");
options.predefinitions.emplace_back("__flang_major__",
FLANG_VERSION_MAJOR_STRING);
options.predefinitions.emplace_back("__flang_minor__",
FLANG_VERSION_MINOR_STRING);
options.predefinitions.emplace_back("__flang_patchlevel__",
FLANG_VERSION_PATCHLEVEL_STRING);
Fortran::common::IntrinsicTypeDefaultKinds defaultKinds;
Fortran::parser::AllSources allSources;
Fortran::parser::AllCookedSources allCookedSources(allSources);
Fortran::semantics::SemanticsContext semanticsContext{
defaultKinds, options.features, allCookedSources};
return mlir::failed(convertFortranSourceToMLIR(
inputFilename, options, programPrefix, semanticsContext, passPipe));
}