2020-03-12 12:47:22 +08:00
|
|
|
//===- tco.cpp - Tilikum Crossing Opt ---------------------------*- 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
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is to be like LLVM's opt program, only for FIR. Such a program is
|
|
|
|
// required for roundtrip testing, etc.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2022-01-24 21:14:49 +08:00
|
|
|
#include "flang/Optimizer/CodeGen/CodeGen.h"
|
|
|
|
#include "flang/Optimizer/Support/FIRContext.h"
|
2021-02-13 05:09:10 +08:00
|
|
|
#include "flang/Optimizer/Support/InitFIR.h"
|
2022-01-24 21:14:49 +08:00
|
|
|
#include "flang/Optimizer/Support/InternalNames.h"
|
2020-03-12 12:47:22 +08:00
|
|
|
#include "flang/Optimizer/Support/KindMapping.h"
|
2022-01-24 21:14:49 +08:00
|
|
|
#include "flang/Optimizer/Transforms/Passes.h"
|
|
|
|
#include "mlir/IR/AsmState.h"
|
2020-11-20 02:43:12 +08:00
|
|
|
#include "mlir/IR/BuiltinOps.h"
|
2020-03-12 12:47:22 +08:00
|
|
|
#include "mlir/IR/MLIRContext.h"
|
2022-03-05 04:53:22 +08:00
|
|
|
#include "mlir/Parser/Parser.h"
|
2020-03-12 12:47:22 +08:00
|
|
|
#include "mlir/Pass/Pass.h"
|
|
|
|
#include "mlir/Pass/PassManager.h"
|
|
|
|
#include "mlir/Transforms/Passes.h"
|
|
|
|
#include "llvm/Support/CommandLine.h"
|
|
|
|
#include "llvm/Support/ErrorOr.h"
|
2021-08-04 21:11:27 +08:00
|
|
|
#include "llvm/Support/FileSystem.h"
|
2020-03-12 12:47:22 +08:00
|
|
|
#include "llvm/Support/InitLLVM.h"
|
|
|
|
#include "llvm/Support/MemoryBuffer.h"
|
|
|
|
#include "llvm/Support/SourceMgr.h"
|
2022-01-24 21:14:49 +08:00
|
|
|
#include "llvm/Support/TargetSelect.h"
|
2020-03-12 12:47:22 +08:00
|
|
|
#include "llvm/Support/ToolOutputFile.h"
|
|
|
|
#include "llvm/Support/raw_ostream.h"
|
|
|
|
|
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
static cl::opt<std::string>
|
|
|
|
inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<std::string> outputFilename("o",
|
|
|
|
cl::desc("Specify output filename"),
|
|
|
|
cl::value_desc("filename"),
|
|
|
|
cl::init("-"));
|
|
|
|
|
|
|
|
static cl::opt<bool> emitFir("emit-fir",
|
|
|
|
cl::desc("Parse and pretty-print the input"),
|
|
|
|
cl::init(false));
|
|
|
|
|
2022-01-24 21:14:49 +08:00
|
|
|
static cl::opt<std::string> targetTriple("target",
|
|
|
|
cl::desc("specify a target triple"),
|
|
|
|
cl::init("native"));
|
|
|
|
|
|
|
|
#include "flang/Tools/CLOptions.inc"
|
|
|
|
|
2020-03-12 12:47:22 +08:00
|
|
|
static void printModuleBody(mlir::ModuleOp mod, raw_ostream &output) {
|
2022-01-24 21:14:49 +08:00
|
|
|
for (auto &op : *mod.getBody())
|
2020-03-12 12:47:22 +08:00
|
|
|
output << op << '\n';
|
|
|
|
}
|
|
|
|
|
|
|
|
// compile a .fir file
|
2021-02-05 03:25:22 +08:00
|
|
|
static mlir::LogicalResult
|
|
|
|
compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
|
2020-03-12 12:47:22 +08:00
|
|
|
// check that there is a file to load
|
|
|
|
ErrorOr<std::unique_ptr<MemoryBuffer>> fileOrErr =
|
|
|
|
MemoryBuffer::getFileOrSTDIN(inputFilename);
|
|
|
|
|
|
|
|
if (std::error_code EC = fileOrErr.getError()) {
|
|
|
|
errs() << "Could not open file: " << EC.message() << '\n';
|
2021-02-05 03:25:22 +08:00
|
|
|
return mlir::failure();
|
2020-03-12 12:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// load the file into a module
|
|
|
|
SourceMgr sourceMgr;
|
|
|
|
sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
|
2021-02-10 17:11:50 +08:00
|
|
|
mlir::DialectRegistry registry;
|
2021-02-13 05:09:10 +08:00
|
|
|
fir::support::registerDialects(registry);
|
2021-02-10 17:11:50 +08:00
|
|
|
mlir::MLIRContext context(registry);
|
2022-01-24 21:14:49 +08:00
|
|
|
fir::support::loadDialects(context);
|
|
|
|
fir::support::registerLLVMTranslation(context);
|
2022-03-07 20:53:49 +08:00
|
|
|
auto owningRef = mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &context);
|
2020-03-12 12:47:22 +08:00
|
|
|
|
|
|
|
if (!owningRef) {
|
|
|
|
errs() << "Error can't load file " << inputFilename << '\n';
|
2021-02-05 03:25:22 +08:00
|
|
|
return mlir::failure();
|
2020-03-12 12:47:22 +08:00
|
|
|
}
|
2022-02-02 07:01:30 +08:00
|
|
|
if (mlir::failed(owningRef->verifyInvariants())) {
|
2020-03-12 12:47:22 +08:00
|
|
|
errs() << "Error verifying FIR module\n";
|
2021-02-05 03:25:22 +08:00
|
|
|
return mlir::failure();
|
2020-03-12 12:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
std::error_code ec;
|
|
|
|
ToolOutputFile out(outputFilename, ec, sys::fs::OF_None);
|
|
|
|
|
|
|
|
// run passes
|
2022-01-24 21:14:49 +08:00
|
|
|
fir::KindMapping kindMap{&context};
|
|
|
|
fir::setTargetTriple(*owningRef, targetTriple);
|
|
|
|
fir::setKindMapping(*owningRef, kindMap);
|
|
|
|
mlir::PassManager pm(&context, mlir::OpPassManager::Nesting::Implicit);
|
|
|
|
pm.enableVerifier(/*verifyPasses=*/true);
|
2020-03-12 12:47:22 +08:00
|
|
|
mlir::applyPassManagerCLOptions(pm);
|
|
|
|
if (emitFir) {
|
|
|
|
// parse the input and pretty-print it back out
|
|
|
|
// -emit-fir intentionally disables all the passes
|
2022-01-24 21:14:49 +08:00
|
|
|
} else if (passPipeline.hasAnyOccurrences()) {
|
|
|
|
auto errorHandler = [&](const Twine &msg) {
|
|
|
|
mlir::emitError(mlir::UnknownLoc::get(pm.getContext())) << msg;
|
|
|
|
return mlir::failure();
|
|
|
|
};
|
|
|
|
if (mlir::failed(passPipeline.addToPipeline(pm, errorHandler)))
|
|
|
|
return mlir::failure();
|
2020-03-12 12:47:22 +08:00
|
|
|
} else {
|
2022-01-24 21:14:49 +08:00
|
|
|
fir::createMLIRToLLVMPassPipeline(pm);
|
|
|
|
fir::addLLVMDialectToLLVMPass(pm, out.os());
|
2020-03-12 12:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// run the pass manager
|
|
|
|
if (mlir::succeeded(pm.run(*owningRef))) {
|
|
|
|
// passes ran successfully, so keep the output
|
2022-01-24 21:14:49 +08:00
|
|
|
if (emitFir || passPipeline.hasAnyOccurrences())
|
2020-03-12 12:47:22 +08:00
|
|
|
printModuleBody(*owningRef, out.os());
|
|
|
|
out.keep();
|
2021-02-05 03:25:22 +08:00
|
|
|
return mlir::success();
|
2020-03-12 12:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// pass manager failed
|
|
|
|
printModuleBody(*owningRef, errs());
|
|
|
|
errs() << "\n\nFAILED: " << inputFilename << '\n';
|
2021-02-05 03:25:22 +08:00
|
|
|
return mlir::failure();
|
2020-03-12 12:47:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char **argv) {
|
2022-01-22 03:34:17 +08:00
|
|
|
[[maybe_unused]] InitLLVM y(argc, argv);
|
2022-01-24 21:14:49 +08:00
|
|
|
fir::support::registerMLIRPassesForFortranTools();
|
|
|
|
fir::registerOptCodeGenPasses();
|
|
|
|
fir::registerOptTransformPasses();
|
|
|
|
mlir::registerMLIRContextCLOptions();
|
2020-03-12 12:47:22 +08:00
|
|
|
mlir::registerPassManagerCLOptions();
|
|
|
|
mlir::PassPipelineCLParser passPipe("", "Compiler passes to run");
|
|
|
|
cl::ParseCommandLineOptions(argc, argv, "Tilikum Crossing Optimizer\n");
|
2021-02-05 03:25:22 +08:00
|
|
|
return mlir::failed(compileFIR(passPipe));
|
2020-03-12 12:47:22 +08:00
|
|
|
}
|