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/Support/InitFIR.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
50 static mlir::LogicalResult
compileFIR(const mlir::PassPipelineCLParser & passPipeline)51 compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
52   // check that there is a file to load
53   ErrorOr<std::unique_ptr<MemoryBuffer>> fileOrErr =
54       MemoryBuffer::getFileOrSTDIN(inputFilename);
55 
56   if (std::error_code EC = fileOrErr.getError()) {
57     errs() << "Could not open file: " << EC.message() << '\n';
58     return mlir::failure();
59   }
60 
61   // load the file into a module
62   SourceMgr sourceMgr;
63   sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc());
64   mlir::DialectRegistry registry;
65   fir::support::registerDialects(registry);
66   mlir::MLIRContext context(registry);
67   auto owningRef = mlir::parseSourceFile(sourceMgr, &context);
68 
69   if (!owningRef) {
70     errs() << "Error can't load file " << inputFilename << '\n';
71     return mlir::failure();
72   }
73   if (mlir::failed(owningRef->verify())) {
74     errs() << "Error verifying FIR module\n";
75     return mlir::failure();
76   }
77 
78   std::error_code ec;
79   ToolOutputFile out(outputFilename, ec, sys::fs::OF_None);
80 
81   // run passes
82   mlir::PassManager pm{&context};
83   mlir::applyPassManagerCLOptions(pm);
84   if (emitFir) {
85     // parse the input and pretty-print it back out
86     // -emit-fir intentionally disables all the passes
87   } else {
88     // TODO: Actually add passes when added to FIR code base
89     // add all the passes
90     // the user can disable them individually
91   }
92 
93   // run the pass manager
94   if (mlir::succeeded(pm.run(*owningRef))) {
95     // passes ran successfully, so keep the output
96     if (emitFir)
97       printModuleBody(*owningRef, out.os());
98     out.keep();
99     return mlir::success();
100   }
101 
102   // pass manager failed
103   printModuleBody(*owningRef, errs());
104   errs() << "\n\nFAILED: " << inputFilename << '\n';
105   return mlir::failure();
106 }
107 
main(int argc,char ** argv)108 int main(int argc, char **argv) {
109   fir::support::registerMLIRPassesForFortranTools();
110   [[maybe_unused]] InitLLVM y(argc, argv);
111   mlir::registerPassManagerCLOptions();
112   mlir::PassPipelineCLParser passPipe("", "Compiler passes to run");
113   cl::ParseCommandLineOptions(argc, argv, "Tilikum Crossing Optimizer\n");
114   return mlir::failed(compileFIR(passPipe));
115 }
116