Skip to content

Commit aa69a48

Browse files
zygoloiddanakjchandlerc
authored
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 <[email protected]> Co-authored-by: Chandler Carruth <[email protected]>
1 parent db150ff commit aa69a48

File tree

202 files changed

+3378
-1255
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

202 files changed

+3378
-1255
lines changed

toolchain/autoupdate_testdata.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,10 +51,10 @@ def main() -> None:
5151
args = parser.parse_args()
5252

5353
if args.non_fatal_checks:
54-
if build_mode == "opt":
54+
if build_mode == "optimize":
5555
exit(
5656
"`--non-fatal-checks` is incompatible with inferred "
57-
"`-c opt` build mode"
57+
"`-c optimize` build mode"
5858
)
5959
configs.append("--config=non-fatal-checks")
6060

toolchain/check/testdata/interop/cpp/function/thunk_ast.carbon

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ auto foo(short a) -> void;
6464
// CHECK:STDOUT: | |-ParmVarDecl {{0x[a-f0-9]+}} <<invalid sloc>> <invalid sloc> implicit 'std::align_val_t'
6565
// CHECK:STDOUT: | `-VisibilityAttr {{0x[a-f0-9]+}} <<invalid sloc>> Implicit Default
6666
// CHECK:STDOUT: |-FunctionDecl {{0x[a-f0-9]+}} <<carbon-internal>:8:1, line:14:1> line:8:7 operator new 'void *(unsigned long, void *) noexcept'
67-
// CHECK:STDOUT: | |-ParmVarDecl {{0x[a-f0-9]+}} <<built-in>:173:23, col:37> <carbon-internal>:8:33 'unsigned long'
67+
// CHECK:STDOUT: | |-ParmVarDecl {{0x[a-f0-9]+}} <<built-in>:174:23, col:37> <carbon-internal>:8:33 'unsigned long'
6868
// CHECK:STDOUT: | `-ParmVarDecl {{0x[a-f0-9]+}} <col:35, col:39> col:40 'void *'
6969
// CHECK:STDOUT: `-FunctionDecl {{0x[a-f0-9]+}} <./thunk_required.h:[[@LINE-51]]:6> col:6 foo__carbon_thunk 'void (short * _Nonnull)' extern
7070
// CHECK:STDOUT: |-ParmVarDecl {{0x[a-f0-9]+}} <col:6> col:6 used a 'short * _Nonnull':'short *'

toolchain/codegen/codegen.cpp

Lines changed: 0 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -17,27 +17,6 @@
1717

1818
namespace Carbon {
1919

20-
auto CodeGen::Make(llvm::Module* module, llvm::StringRef target_triple_str,
21-
Diagnostics::Consumer* consumer) -> std::optional<CodeGen> {
22-
llvm::Triple target_triple(target_triple_str);
23-
std::string error;
24-
const llvm::Target* target =
25-
llvm::TargetRegistry::lookupTarget(target_triple, error);
26-
CARBON_CHECK(target, "Target should be validated before codegen: {0}", error);
27-
28-
module->setTargetTriple(target_triple);
29-
30-
constexpr llvm::StringLiteral CPU = "generic";
31-
constexpr llvm::StringLiteral Features = "";
32-
33-
llvm::TargetOptions target_opts;
34-
CodeGen codegen(module,
35-
consumer ? consumer : &Diagnostics::ConsoleConsumer());
36-
codegen.target_machine_.reset(target->createTargetMachine(
37-
target_triple, CPU, Features, target_opts, llvm::Reloc::PIC_));
38-
return codegen;
39-
}
40-
4120
auto CodeGen::EmitAssembly(llvm::raw_pwrite_stream& out) -> bool {
4221
return EmitCode(out, llvm::CodeGenFileType::AssemblyFile);
4322
}

toolchain/codegen/codegen.h

Lines changed: 6 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,10 @@ namespace Carbon {
1414

1515
class CodeGen {
1616
public:
17-
// `module` and `errors` must not be null. `consumer` may be null, in which
18-
// case diagnostics go to stderr.
19-
static auto Make(llvm::Module* module, llvm::StringRef target_triple_str,
20-
Diagnostics::Consumer* consumer = nullptr)
21-
-> std::optional<CodeGen>;
17+
// `module`, `target_machine`, and `consumer` must not be null.
18+
explicit CodeGen(llvm::Module* module, llvm::TargetMachine* target_machine,
19+
Diagnostics::Consumer* consumer)
20+
: module_(module), target_machine_(target_machine), emitter_(consumer) {}
2221

2322
// Generates the object code file.
2423
// Returns false in case of failure, and any information about the failure is
@@ -35,10 +34,6 @@ class CodeGen {
3534
auto EmitAssembly(llvm::raw_pwrite_stream& out) -> bool;
3635

3736
private:
38-
// `module` and `consumer` must not be null.
39-
explicit CodeGen(llvm::Module* module, Diagnostics::Consumer* consumer)
40-
: module_(module), emitter_(consumer) {}
41-
4237
// Using the llvm pass emits either assembly or object code to dest.
4338
// Returns false in case of failure, and any information about the failure is
4439
// printed to the error stream.
@@ -47,10 +42,10 @@ class CodeGen {
4742

4843
llvm::Module* module_;
4944

45+
llvm::TargetMachine* target_machine_;
46+
5047
// The emitter for diagnostics.
5148
Diagnostics::FileEmitter emitter_;
52-
53-
std::unique_ptr<llvm::TargetMachine> target_machine_;
5449
};
5550

5651
} // namespace Carbon

toolchain/driver/BUILD

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,15 @@ cc_library(
155155
"//toolchain/language_server",
156156
"//toolchain/lex",
157157
"//toolchain/lower",
158+
"//toolchain/lower:options",
158159
"//toolchain/parse",
159160
"//toolchain/parse:tree",
160161
"//toolchain/sem_ir:file",
161162
"//toolchain/sem_ir:typed_insts",
162163
"//toolchain/source:source_buffer",
163164
"@llvm-project//llvm:Core",
164165
"@llvm-project//llvm:MC",
166+
"@llvm-project//llvm:Passes",
165167
"@llvm-project//llvm:Support",
166168
"@llvm-project//llvm:TargetParser",
167169
],

0 commit comments

Comments
 (0)