jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 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 | |
Eric Schweitz | f800a9b | 2021-02-12 21:09:10 | [diff] [blame] | 14 | #include "flang/Optimizer/Support/InitFIR.h" |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 15 | #include "flang/Optimizer/Support/KindMapping.h" |
River Riddle | 65fcddf | 2020-11-19 18:43:12 | [diff] [blame] | 16 | #include "mlir/IR/BuiltinOps.h" |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 17 | #include "mlir/IR/MLIRContext.h" |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 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 | |
| 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 |
Sourabh Singh Tomar | bca0619 | 2021-02-04 19:25:22 | [diff] [blame] | 50 | static mlir::LogicalResult |
| 51 | compileFIR(const mlir::PassPipelineCLParser &passPipeline) { |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 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'; |
Sourabh Singh Tomar | bca0619 | 2021-02-04 19:25:22 | [diff] [blame] | 58 | return mlir::failure(); |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 59 | } |
| 60 | |
| 61 | // load the file into a module |
| 62 | SourceMgr sourceMgr; |
| 63 | sourceMgr.AddNewSourceBuffer(std::move(*fileOrErr), SMLoc()); |
Alex Zinenko | 2996a8d | 2021-02-10 09:11:50 | [diff] [blame] | 64 | mlir::DialectRegistry registry; |
Eric Schweitz | f800a9b | 2021-02-12 21:09:10 | [diff] [blame] | 65 | fir::support::registerDialects(registry); |
Alex Zinenko | 2996a8d | 2021-02-10 09:11:50 | [diff] [blame] | 66 | mlir::MLIRContext context(registry); |
Mehdi Amini | f6615b2 | 2020-08-23 21:20:49 | [diff] [blame] | 67 | auto owningRef = mlir::parseSourceFile(sourceMgr, &context); |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 68 | |
| 69 | if (!owningRef) { |
| 70 | errs() << "Error can't load file " << inputFilename << '\n'; |
Sourabh Singh Tomar | bca0619 | 2021-02-04 19:25:22 | [diff] [blame] | 71 | return mlir::failure(); |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 72 | } |
| 73 | if (mlir::failed(owningRef->verify())) { |
| 74 | errs() << "Error verifying FIR module\n"; |
Sourabh Singh Tomar | bca0619 | 2021-02-04 19:25:22 | [diff] [blame] | 75 | return mlir::failure(); |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | std::error_code ec; |
| 79 | ToolOutputFile out(outputFilename, ec, sys::fs::OF_None); |
| 80 | |
| 81 | // run passes |
Mehdi Amini | f6615b2 | 2020-08-23 21:20:49 | [diff] [blame] | 82 | mlir::PassManager pm{&context}; |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 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(); |
Sourabh Singh Tomar | bca0619 | 2021-02-04 19:25:22 | [diff] [blame] | 99 | return mlir::success(); |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | // pass manager failed |
| 103 | printModuleBody(*owningRef, errs()); |
| 104 | errs() << "\n\nFAILED: " << inputFilename << '\n'; |
Sourabh Singh Tomar | bca0619 | 2021-02-04 19:25:22 | [diff] [blame] | 105 | return mlir::failure(); |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | int main(int argc, char **argv) { |
Eric Schweitz | f800a9b | 2021-02-12 21:09:10 | [diff] [blame] | 109 | fir::support::registerFIRPasses(); |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 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"); |
Sourabh Singh Tomar | bca0619 | 2021-02-04 19:25:22 | [diff] [blame] | 114 | return mlir::failed(compileFIR(passPipe)); |
jeanPerier | 57f536a | 2020-03-12 04:47:22 | [diff] [blame] | 115 | } |