Skip to content

Commit a9dacb1

Browse files
authored
[flang] Add -O flag to tco (#151869)
At the moment, there is no established way to emit LLVM IR before optimization in LLVM. tco can partially does, but the optimization level is fixed to 2. This patch adds -O flag to tco. Note that this is not completely equivalent to the frontend option. tco does not accept -O flag without numbers, and the default optimization level is not 0 but 2.
1 parent f092b82 commit a9dacb1

File tree

1 file changed

+31
-1
lines changed

1 file changed

+31
-1
lines changed

flang/tools/tco/tco.cpp

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,12 @@ static cl::opt<bool> emitFir("emit-fir",
5151
cl::desc("Parse and pretty-print the input"),
5252
cl::init(false));
5353

54+
static cl::opt<unsigned>
55+
OptLevel("O",
56+
cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
57+
"(default = '-O2')"),
58+
cl::Prefix, cl::init(2));
59+
5460
static cl::opt<std::string> targetTriple("target",
5561
cl::desc("specify a target triple"),
5662
cl::init("native"));
@@ -96,6 +102,22 @@ static void printModule(mlir::ModuleOp mod, raw_ostream &output) {
96102
output << mod << '\n';
97103
}
98104

105+
static std::optional<llvm::OptimizationLevel>
106+
getOptimizationLevel(unsigned level) {
107+
switch (level) {
108+
default:
109+
return std::nullopt;
110+
case 0:
111+
return llvm::OptimizationLevel::O0;
112+
case 1:
113+
return llvm::OptimizationLevel::O1;
114+
case 2:
115+
return llvm::OptimizationLevel::O2;
116+
case 3:
117+
return llvm::OptimizationLevel::O3;
118+
}
119+
}
120+
99121
// compile a .fir file
100122
static llvm::LogicalResult
101123
compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
@@ -157,9 +179,17 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
157179
if (mlir::failed(passPipeline.addToPipeline(pm, errorHandler)))
158180
return mlir::failure();
159181
} else {
160-
MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2);
182+
std::optional<llvm::OptimizationLevel> level =
183+
getOptimizationLevel(OptLevel);
184+
if (!level) {
185+
errs() << "Error invalid optimization level\n";
186+
return mlir::failure();
187+
}
188+
MLIRToLLVMPassPipelineConfig config(*level);
189+
// TODO: config.StackArrays should be set here?
161190
config.EnableOpenMP = true; // assume the input contains OpenMP
162191
config.AliasAnalysis = enableAliasAnalysis && !testGeneratorMode;
192+
config.LoopVersioning = OptLevel > 2;
163193
if (codeGenLLVM) {
164194
// Run only CodeGen passes.
165195
fir::createDefaultFIRCodeGenPassPipeline(pm, config);

0 commit comments

Comments
 (0)