1 //===- tco.cpp - Tilikum Crossing Opt ---------------------------*- C++ -*-===//
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 // This is to be like LLVM's opt program, only for FIR. Such a program is
10 // required for roundtrip testing, etc.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "flang/Optimizer/Dialect/FIRDialect.h"
15 #include "flang/Optimizer/Support/KindMapping.h"
16 #include "mlir/IR/BuiltinOps.h"
17 #include "mlir/IR/MLIRContext.h"
18 #include "mlir/Parser.h"
19 #include "mlir/Pass/Pass.h"
20 #include "mlir/Pass/PassManager.h"
21 #include "mlir/Transforms/Passes.h"
22 #include "llvm/Support/CommandLine.h"
23 #include "llvm/Support/ErrorOr.h"
24 #include "llvm/Support/InitLLVM.h"
25 #include "llvm/Support/MemoryBuffer.h"
26 #include "llvm/Support/SourceMgr.h"
27 #include "llvm/Support/ToolOutputFile.h"
28 #include "llvm/Support/raw_ostream.h"
29
30 using namespace llvm;
31
32 static cl::opt<std::string>
33 inputFilename(cl::Positional, cl::desc("<input file>"), cl::init("-"));
34
35 static cl::opt<std::string> outputFilename("o",
36 cl::desc("Specify output filename"),
37 cl::value_desc("filename"),
38 cl::init("-"));
39
40 static cl::opt<bool> emitFir("emit-fir",
41 cl::desc("Parse and pretty-print the input"),
42 cl::init(false));
43
printModuleBody(mlir::ModuleOp mod,raw_ostream & output)44 static void printModuleBody(mlir::ModuleOp mod, raw_ostream &output) {
45 for (auto &op : mod.getBody()->without_terminator())
46 output << op << '\n';
47 }
48
49 // compile a .fir file
compileFIR()50 static int compileFIR() {
51 // check that there is a file to load
52 ErrorOr<std::unique_ptr<MemoryBuffer>> fileOrErr =
53 MemoryBuffer::getFileOrSTDIN(inputFilename);
54
55 if (std::error_code EC = fileOrErr.getError()) {
56 errs() << "Could not open file: " << EC.message() << '\n';
57 return 1;
58 }
59
60 // load the file into a module
61 SourceMgr sourceMgr;
62 sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
63 mlir::MLIRContext context;
64 fir::registerFIRDialects(context.getDialectRegistry());
65 auto owningRef = mlir::parseSourceFile(sourceMgr, &context);
66
67 if (!owningRef) {
68 errs() << "Error can't load file " << inputFilename << '\n';
69 return 2;
70 }
71 if (mlir::failed(owningRef->verify())) {
72 errs() << "Error verifying FIR module\n";
73 return 4;
74 }
75
76 std::error_code ec;
77 ToolOutputFile out(outputFilename, ec, sys::fs::OF_None);
78
79 // run passes
80 mlir::PassManager pm{&context};
81 mlir::applyPassManagerCLOptions(pm);
82 if (emitFir) {
83 // parse the input and pretty-print it back out
84 // -emit-fir intentionally disables all the passes
85 } else {
86 // TODO: Actually add passes when added to FIR code base
87 // add all the passes
88 // the user can disable them individually
89 }
90
91 // run the pass manager
92 if (mlir::succeeded(pm.run(*owningRef))) {
93 // passes ran successfully, so keep the output
94 if (emitFir)
95 printModuleBody(*owningRef, out.os());
96 out.keep();
97 return 0;
98 }
99
100 // pass manager failed
101 printModuleBody(*owningRef, errs());
102 errs() << "\n\nFAILED: " << inputFilename << '\n';
103 return 8;
104 }
105
main(int argc,char ** argv)106 int main(int argc, char **argv) {
107 fir::registerFIRPasses();
108 [[maybe_unused]] InitLLVM y(argc, argv);
109 mlir::registerPassManagerCLOptions();
110 mlir::PassPipelineCLParser passPipe("", "Compiler passes to run");
111 cl::ParseCommandLineOptions(argc, argv, "Tilikum Crossing Optimizer\n");
112 return compileFIR();
113 }
114