Files
carbon-lang/toolchain/lower/options.h
T
aa69a484eb Add support for running LLVM optimizer. (#6225)
Adds a flag `--optimize=<mode>` that specifies what to optimize for:

* `--optimize=none` turns off the optimizer as much as possible, but
still respects always_inline.
* `--optimize=debug` aims to be the equivalent of `-Og` / `-O1`, and
provides optimizations that don't affect the ability to debug the
program. This is the default.
* `--optimize=size` optimizes for the size of the produced program, and
aims to be the equivalent of `-Oz`.
* `--optimize=speed` optimizes for the execution time of the produced
program, and aims to be the equivalent of `-O3`.

Following the approach taken by Clang, the optimization level feeds into
both the configuration of the LLVM pass pipeline and the attributes
added to function definitions generated by the frontend.

Optimization is performed in a new phase, `optimize`, which runs between
`lower` and `codegen`.

---------

Co-authored-by: Dana Jansens <danakj@orodu.net>
Co-authored-by: Chandler Carruth <chandlerc@gmail.com>
2025-11-05 00:15:14 +00:00

45 lines
1.4 KiB
C++

// Part of the Carbon Language project, under the Apache License v2.0 with LLVM
// Exceptions. See /LICENSE for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
#ifndef CARBON_TOOLCHAIN_LOWER_OPTIONS_H_
#define CARBON_TOOLCHAIN_LOWER_OPTIONS_H_
#include "llvm/Support/raw_ostream.h"
namespace Carbon::Lower {
enum class OptimizationLevel {
// No optimizations beyond necessary ones like inlining always-inline
// functions. Corresponds to Clang -O0.
None,
// Perform optimizations that make the build faster and don't degrade
// debugging. Corresponds to Clang -O1 or -Og.
Debug,
// Optimize for binary size. Corresponds to Clang -Oz.
Size,
// Optimize for program execution speed. Corresponds to Clang -O3.
Speed,
};
struct LowerToLLVMOptions {
// Options must be set individually, not through initialization.
explicit LowerToLLVMOptions() = default;
// If set, enables LLVM IR verification.
llvm::raw_ostream* llvm_verifier_stream = nullptr;
// Whether to include debug info in lowered output.
bool want_debug_info = false;
// If set, enables verbose output.
llvm::raw_ostream* vlog_stream = nullptr;
// The optimization level to set on lowered functions by default.
OptimizationLevel opt_level = OptimizationLevel::Debug;
};
} // namespace Carbon::Lower
#endif // CARBON_TOOLCHAIN_LOWER_OPTIONS_H_