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/CodeGen/CodeGen.h" |
15 | #include "flang/Optimizer/Dialect/Support/FIRContext.h" |
16 | #include "flang/Optimizer/Dialect/Support/KindMapping.h" |
17 | #include "flang/Optimizer/Support/DataLayout.h" |
18 | #include "flang/Optimizer/Support/InitFIR.h" |
19 | #include "flang/Optimizer/Support/InternalNames.h" |
20 | #include "flang/Optimizer/Transforms/Passes.h" |
21 | #include "flang/Tools/CrossToolHelpers.h" |
22 | #include "mlir/IR/AsmState.h" |
23 | #include "mlir/IR/BuiltinOps.h" |
24 | #include "mlir/IR/MLIRContext.h" |
25 | #include "mlir/Parser/Parser.h" |
26 | #include "mlir/Pass/Pass.h" |
27 | #include "mlir/Pass/PassManager.h" |
28 | #include "mlir/Transforms/Passes.h" |
29 | #include "llvm/Passes/OptimizationLevel.h" |
30 | #include "llvm/Support/CommandLine.h" |
31 | #include "llvm/Support/ErrorOr.h" |
32 | #include "llvm/Support/FileSystem.h" |
33 | #include "llvm/Support/InitLLVM.h" |
34 | #include "llvm/Support/MemoryBuffer.h" |
35 | #include "llvm/Support/SourceMgr.h" |
36 | #include "llvm/Support/TargetSelect.h" |
37 | #include "llvm/Support/ToolOutputFile.h" |
38 | #include "llvm/Support/raw_ostream.h" |
39 | |
40 | using namespace llvm; |
41 | |
42 | static cl::opt<std::string> |
43 | inputFilename(cl::Positional, cl::desc("<input file>" ), cl::init(Val: "-" )); |
44 | |
45 | static cl::opt<std::string> outputFilename("o" , |
46 | cl::desc("Specify output filename" ), |
47 | cl::value_desc("filename" ), |
48 | cl::init(Val: "-" )); |
49 | |
50 | static cl::opt<bool> emitFir("emit-fir" , |
51 | cl::desc("Parse and pretty-print the input" ), |
52 | cl::init(Val: false)); |
53 | |
54 | static cl::opt<std::string> targetTriple("target" , |
55 | cl::desc("specify a target triple" ), |
56 | cl::init(Val: "native" )); |
57 | |
58 | static cl::opt<std::string> |
59 | targetCPU("target-cpu" , cl::desc("specify a target CPU" ), cl::init(Val: "" )); |
60 | |
61 | static cl::opt<std::string> |
62 | targetFeatures("target-features" , cl::desc("specify the target features" ), |
63 | cl::init(Val: "" )); |
64 | |
65 | static cl::opt<bool> codeGenLLVM( |
66 | "code-gen-llvm" , |
67 | cl::desc("Run only CodeGen passes and translate FIR to LLVM IR" ), |
68 | cl::init(Val: false)); |
69 | |
70 | #include "flang/Tools/CLOptions.inc" |
71 | |
72 | static void printModule(mlir::ModuleOp mod, raw_ostream &output) { |
73 | output << mod << '\n'; |
74 | } |
75 | |
76 | // compile a .fir file |
77 | static mlir::LogicalResult |
78 | compileFIR(const mlir::PassPipelineCLParser &passPipeline) { |
79 | // check that there is a file to load |
80 | ErrorOr<std::unique_ptr<MemoryBuffer>> fileOrErr = |
81 | MemoryBuffer::getFileOrSTDIN(Filename: inputFilename); |
82 | |
83 | if (std::error_code EC = fileOrErr.getError()) { |
84 | errs() << "Could not open file: " << EC.message() << '\n'; |
85 | return mlir::failure(); |
86 | } |
87 | |
88 | // load the file into a module |
89 | SourceMgr sourceMgr; |
90 | sourceMgr.AddNewSourceBuffer(F: std::move(*fileOrErr), IncludeLoc: SMLoc()); |
91 | mlir::DialectRegistry registry; |
92 | fir::support::registerDialects(registry); |
93 | fir::support::addFIRExtensions(registry); |
94 | mlir::MLIRContext context(registry); |
95 | fir::support::loadDialects(context); |
96 | fir::support::registerLLVMTranslation(context); |
97 | auto owningRef = mlir::parseSourceFile<mlir::ModuleOp>(sourceMgr, &context); |
98 | |
99 | if (!owningRef) { |
100 | errs() << "Error can't load file " << inputFilename << '\n'; |
101 | return mlir::failure(); |
102 | } |
103 | if (mlir::failed(owningRef->verifyInvariants())) { |
104 | errs() << "Error verifying FIR module\n" ; |
105 | return mlir::failure(); |
106 | } |
107 | |
108 | std::error_code ec; |
109 | ToolOutputFile out(outputFilename, ec, sys::fs::OF_None); |
110 | |
111 | // run passes |
112 | fir::KindMapping kindMap{&context}; |
113 | fir::setTargetTriple(*owningRef, targetTriple); |
114 | fir::setKindMapping(*owningRef, kindMap); |
115 | fir::setTargetCPU(*owningRef, targetCPU); |
116 | fir::setTargetFeatures(*owningRef, targetFeatures); |
117 | // tco is a testing tool, so it will happily use the target independent |
118 | // data layout if none is on the module. |
119 | fir::support::setMLIRDataLayoutFromAttributes(*owningRef, |
120 | /*allowDefaultLayout=*/true); |
121 | mlir::PassManager pm((*owningRef)->getName(), |
122 | mlir::OpPassManager::Nesting::Implicit); |
123 | pm.enableVerifier(/*verifyPasses=*/true); |
124 | (void)mlir::applyPassManagerCLOptions(pm); |
125 | if (emitFir) { |
126 | // parse the input and pretty-print it back out |
127 | // -emit-fir intentionally disables all the passes |
128 | } else if (passPipeline.hasAnyOccurrences()) { |
129 | auto errorHandler = [&](const Twine &msg) { |
130 | mlir::emitError(mlir::UnknownLoc::get(pm.getContext())) << msg; |
131 | return mlir::failure(); |
132 | }; |
133 | if (mlir::failed(passPipeline.addToPipeline(pm, errorHandler))) |
134 | return mlir::failure(); |
135 | } else { |
136 | MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2); |
137 | config.AliasAnalysis = true; // enabled when optimizing for speed |
138 | if (codeGenLLVM) { |
139 | // Run only CodeGen passes. |
140 | fir::createDefaultFIRCodeGenPassPipeline(pm, config); |
141 | } else { |
142 | // Run tco with O2 by default. |
143 | fir::createMLIRToLLVMPassPipeline(pm, config); |
144 | } |
145 | fir::addLLVMDialectToLLVMPass(pm, out.os()); |
146 | } |
147 | |
148 | // run the pass manager |
149 | if (mlir::succeeded(pm.run(*owningRef))) { |
150 | // passes ran successfully, so keep the output |
151 | if ((emitFir || passPipeline.hasAnyOccurrences()) && !codeGenLLVM) |
152 | printModule(*owningRef, out.os()); |
153 | out.keep(); |
154 | return mlir::success(); |
155 | } |
156 | |
157 | // pass manager failed |
158 | printModule(*owningRef, errs()); |
159 | errs() << "\n\nFAILED: " << inputFilename << '\n'; |
160 | return mlir::failure(); |
161 | } |
162 | |
163 | int main(int argc, char **argv) { |
164 | // Disable the ExternalNameConversion pass by default until all the tests have |
165 | // been updated to pass with it enabled. |
166 | disableExternalNameConversion = true; |
167 | |
168 | [[maybe_unused]] InitLLVM y(argc, argv); |
169 | fir::support::registerMLIRPassesForFortranTools(); |
170 | fir::registerOptCodeGenPasses(); |
171 | fir::registerOptTransformPasses(); |
172 | mlir::registerMLIRContextCLOptions(); |
173 | mlir::registerPassManagerCLOptions(); |
174 | mlir::PassPipelineCLParser passPipe("" , "Compiler passes to run" ); |
175 | cl::ParseCommandLineOptions(argc, argv, Overview: "Tilikum Crossing Optimizer\n" ); |
176 | return mlir::failed(compileFIR(passPipe)); |
177 | } |
178 | |