[flang-commits] [flang] a9dacb1 - [flang] Add -O flag to tco (#151869)
via flang-commits
flang-commits at lists.llvm.org
Wed Aug 6 08:04:37 PDT 2025
Author: parabola94
Date: 2025-08-06T16:04:32+01:00
New Revision: a9dacb10fe7db908f1305757820497de65626a46
URL: https://github.com/llvm/llvm-project/commit/a9dacb10fe7db908f1305757820497de65626a46
DIFF: https://github.com/llvm/llvm-project/commit/a9dacb10fe7db908f1305757820497de65626a46.diff
LOG: [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.
Added:
Modified:
flang/tools/tco/tco.cpp
Removed:
################################################################################
diff --git a/flang/tools/tco/tco.cpp b/flang/tools/tco/tco.cpp
index d8daf8769cb21..36939802f55a6 100644
--- a/flang/tools/tco/tco.cpp
+++ b/flang/tools/tco/tco.cpp
@@ -51,6 +51,12 @@ static cl::opt<bool> emitFir("emit-fir",
cl::desc("Parse and pretty-print the input"),
cl::init(false));
+static cl::opt<unsigned>
+ OptLevel("O",
+ cl::desc("Optimization level. [-O0, -O1, -O2, or -O3] "
+ "(default = '-O2')"),
+ cl::Prefix, cl::init(2));
+
static cl::opt<std::string> targetTriple("target",
cl::desc("specify a target triple"),
cl::init("native"));
@@ -96,6 +102,22 @@ static void printModule(mlir::ModuleOp mod, raw_ostream &output) {
output << mod << '\n';
}
+static std::optional<llvm::OptimizationLevel>
+getOptimizationLevel(unsigned level) {
+ switch (level) {
+ default:
+ return std::nullopt;
+ case 0:
+ return llvm::OptimizationLevel::O0;
+ case 1:
+ return llvm::OptimizationLevel::O1;
+ case 2:
+ return llvm::OptimizationLevel::O2;
+ case 3:
+ return llvm::OptimizationLevel::O3;
+ }
+}
+
// compile a .fir file
static llvm::LogicalResult
compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
@@ -157,9 +179,17 @@ compileFIR(const mlir::PassPipelineCLParser &passPipeline) {
if (mlir::failed(passPipeline.addToPipeline(pm, errorHandler)))
return mlir::failure();
} else {
- MLIRToLLVMPassPipelineConfig config(llvm::OptimizationLevel::O2);
+ std::optional<llvm::OptimizationLevel> level =
+ getOptimizationLevel(OptLevel);
+ if (!level) {
+ errs() << "Error invalid optimization level\n";
+ return mlir::failure();
+ }
+ MLIRToLLVMPassPipelineConfig config(*level);
+ // TODO: config.StackArrays should be set here?
config.EnableOpenMP = true; // assume the input contains OpenMP
config.AliasAnalysis = enableAliasAnalysis && !testGeneratorMode;
+ config.LoopVersioning = OptLevel > 2;
if (codeGenLLVM) {
// Run only CodeGen passes.
fir::createDefaultFIRCodeGenPassPipeline(pm, config);
More information about the flang-commits
mailing list