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>
This commit is contained in:
Richard Smith
2025-11-05 00:15:14 +00:00
committed by GitHub
co-authored by Dana Jansens Chandler Carruth
parent db150ffc5c
commit aa69a484eb
202 changed files with 3376 additions and 1253 deletions
+25 -21
View File
@@ -92,6 +92,11 @@ class ToolchainFileTest : public FileTestBase {
// Sets different default flags based on the component being tested.
auto GetDefaultArgs() const -> llvm::SmallVector<std::string> override;
// Returns string replacements to implement `%{key}` -> `value` in arguments.
auto GetArgReplacements() const -> llvm::StringMap<std::string> override {
return {{"core", data_->installation.core_package().native()}};
}
// Generally uses the parent implementation, with special handling for lex.
auto GetDefaultFileRE(llvm::ArrayRef<llvm::StringRef> filenames) const
-> std::optional<RE2> override;
@@ -296,6 +301,15 @@ static auto DoClangASTCheckReplacements(std::string& check_line) -> void {
auto ToolchainFileTest::DoExtraCheckReplacements(std::string& check_line) const
-> void {
// The path to the core package appears in various places, such as some check
// diagnostics and debug information produced by lowering, and will differ
// between testing environments, so don't test it.
// TODO: Consider adding a content keyword to name the core package, and
// replace with that instead. Alternatively, consider adding the core
// package to the VFS with a fixed name.
absl::StrReplaceAll({{data_->installation.core_package().native(), "{{.*}}"}},
&check_line);
if (component_ == "driver") {
// TODO: Disable token output, it's not interesting for these tests.
if (llvm::StringRef(check_line).starts_with("// CHECK:STDOUT: {")) {
@@ -308,29 +322,19 @@ auto ToolchainFileTest::DoExtraCheckReplacements(std::string& check_line) const
// The column happens to be right for FileStart, but the line is wrong.
static RE2 file_token_re(R"((FileEnd.*column: |FileStart.*line: )( *\d+))");
RE2::Replace(&check_line, file_token_re, R"(\1{{ *\\d+}})");
} else if (component_ == "check" || component_ == "lower") {
// The path to the core package appears in some check diagnostics and in
// debug information produced by lowering, and will differ between testing
// environments, so don't test it.
// TODO: Consider adding a content keyword to name the core package, and
// replace with that instead. Alternatively, consider adding the core
// package to the VFS with a fixed name.
absl::StrReplaceAll(
{{data_->installation.core_package().native(), "{{.*}}"}}, &check_line);
if (component_ == "check") {
DoClangASTCheckReplacements(check_line);
} else if (component_ == "check") {
DoClangASTCheckReplacements(check_line);
// Reduce instruction numbering sensitivity; this is brittle for
// instruction edits including adding/removing singleton instructions.
static RE2 inst_re(
R"(((?:import_ref [^,]*, |<unexpected>\.)inst)[0-9A-F]+)");
RE2::Replace(&check_line, inst_re, R"(\1{{[0-9A-F]+}})");
// Reduce instruction numbering sensitivity; this is brittle for
// instruction edits including adding/removing singleton instructions.
static RE2 inst_re(
R"(((?:import_ref [^,]*, |<unexpected>\.)inst)[0-9A-F]+)");
RE2::Replace(&check_line, inst_re, R"(\1{{[0-9A-F]+}})");
// Reduce location sensitivity in imports referring to `Core`; this is
// brittle for small edits, including comment changes.
static RE2 core_loc_re(R"((import_ref Core//[^,]*, loc)\d+_\d+)");
RE2::Replace(&check_line, core_loc_re, R"(\1{{\\d+_\\d+}})");
}
// Reduce location sensitivity in imports referring to `Core`; this is
// brittle for small edits, including comment changes.
static RE2 core_loc_re(R"((import_ref Core//[^,]*, loc)\d+_\d+)");
RE2::Replace(&check_line, core_loc_re, R"(\1{{\\d+_\\d+}})");
} else {
FileTestBase::DoExtraCheckReplacements(check_line);
}