Skip to content

Commit 62adb03

Browse files
allightcopybara-github
authored andcommitted
Add --llvm_opt_level option to aot_compiler_main
This can be useful if the llvm optimizer is crashing to aid in debugging. Bug: #1776 PiperOrigin-RevId: 707988660
1 parent dee91dc commit 62adb03

File tree

4 files changed

+32
-19
lines changed

4 files changed

+32
-19
lines changed

xls/jit/BUILD

+1
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ cc_binary(
9999
":function_base_jit",
100100
":function_jit",
101101
":jit_proc_runtime",
102+
":llvm_compiler",
102103
":llvm_type_converter",
103104
":observer",
104105
":type_layout_cc_proto",

xls/jit/aot_compiler_main.cc

+15-10
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
#include "xls/jit/function_base_jit.h"
5353
#include "xls/jit/function_jit.h"
5454
#include "xls/jit/jit_proc_runtime.h"
55+
#include "xls/jit/llvm_compiler.h"
5556
#include "xls/jit/llvm_type_converter.h"
5657
#include "xls/jit/observer.h"
5758
#include "xls/jit/type_layout.pb.h"
@@ -75,6 +76,9 @@ ABSL_FLAG(std::optional<std::string>, output_llvm_opt_ir, std::nullopt,
7576
"Path at which to write the output optimized llvm file.");
7677
ABSL_FLAG(std::optional<std::string>, output_asm, std::nullopt,
7778
"Path at which to write the output optimized llvm file.");
79+
ABSL_FLAG(int64_t, llvm_opt_level, xls::LlvmCompiler::kDefaultOptLevel,
80+
"The optimization level to use for the LLVM optimizer.");
81+
7882
#ifdef ABSL_HAVE_MEMORY_SANITIZER
7983
static constexpr bool kHasMsan = true;
8084
#else
@@ -227,7 +231,7 @@ absl::Status RealMain(const std::string& input_ir_path,
227231
const std::optional<std::string>& top,
228232
const std::optional<std::string>& output_object_path,
229233
const std::optional<std::string>& output_proto_path,
230-
bool include_msan,
234+
bool include_msan, int64_t llvm_opt_level,
231235
const std::optional<std::string>& output_textproto_path,
232236
const std::optional<std::string>& output_llvm_ir_path,
233237
const std::optional<std::string>& output_llvm_opt_ir_path,
@@ -258,26 +262,26 @@ absl::Status RealMain(const std::string& input_ir_path,
258262

259263
std::optional<JitObjectCode> object_code;
260264
if (f->IsFunction()) {
261-
XLS_ASSIGN_OR_RETURN(
262-
object_code,
263-
FunctionJit::CreateObjectCode(f->AsFunctionOrDie(),
264-
/*opt_level = */ 3, include_msan, &obs));
265+
XLS_ASSIGN_OR_RETURN(object_code, FunctionJit::CreateObjectCode(
266+
f->AsFunctionOrDie(), llvm_opt_level,
267+
include_msan, &obs));
265268
} else if (f->IsProc()) {
266269
if (f->AsProcOrDie()->is_new_style_proc()) {
267270
XLS_ASSIGN_OR_RETURN(
268-
object_code,
269-
CreateProcAotObjectCode(f->AsProcOrDie(), include_msan, &obs));
271+
object_code, CreateProcAotObjectCode(f->AsProcOrDie(), llvm_opt_level,
272+
include_msan, &obs));
270273
} else {
271274
// all procs
272-
XLS_ASSIGN_OR_RETURN(object_code, CreateProcAotObjectCode(
273-
package.get(), include_msan, &obs));
275+
XLS_ASSIGN_OR_RETURN(
276+
object_code, CreateProcAotObjectCode(package.get(), llvm_opt_level,
277+
include_msan, &obs));
274278
}
275279
} else {
276280
XLS_ASSIGN_OR_RETURN(BlockElaboration elab,
277281
BlockElaboration::Elaborate(f->AsBlockOrDie()));
278282
XLS_ASSIGN_OR_RETURN(
279283
object_code,
280-
BlockJit::CreateObjectCode(elab, /*opt_level=*/3, include_msan, &obs));
284+
BlockJit::CreateObjectCode(elab, llvm_opt_level, include_msan, &obs));
281285
}
282286
AotPackageEntrypointsProto all_entrypoints;
283287
if (output_object_path) {
@@ -340,6 +344,7 @@ int main(int argc, char** argv) {
340344
bool include_msan = absl::GetFlag(FLAGS_include_msan);
341345
absl::Status status = xls::RealMain(
342346
input_ir_path, top, output_object_path, output_proto_path, include_msan,
347+
absl::GetFlag(FLAGS_llvm_opt_level),
343348
absl::GetFlag(FLAGS_output_textproto),
344349
absl::GetFlag(FLAGS_output_llvm_ir),
345350
absl::GetFlag(FLAGS_output_llvm_opt_ir), absl::GetFlag(FLAGS_output_asm));

xls/jit/jit_proc_runtime.cc

+11-7
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,11 @@ class SharedCompiler final : public LlvmCompiler {
124124
};
125125

126126
absl::StatusOr<JitObjectCode> GetAotObjectCode(ProcElaboration elaboration,
127+
int64_t opt_level,
127128
bool with_msan,
128129
JitObserver* observer) {
129-
XLS_ASSIGN_OR_RETURN(
130-
std::unique_ptr<AotCompiler> compiler,
131-
AotCompiler::Create(
132-
with_msan, /*opt_level=*/LlvmCompiler::kDefaultOptLevel, observer));
130+
XLS_ASSIGN_OR_RETURN(std::unique_ptr<AotCompiler> compiler,
131+
AotCompiler::Create(with_msan, opt_level, observer));
133132
XLS_ASSIGN_OR_RETURN(std::unique_ptr<llvm::TargetMachine> target,
134133
compiler->CreateTargetMachine());
135134
llvm::DataLayout layout = target->createDataLayout();
@@ -288,17 +287,22 @@ absl::StatusOr<std::unique_ptr<SerialProcRuntime>> CreateJitSerialProcRuntime(
288287
}
289288

290289
absl::StatusOr<JitObjectCode> CreateProcAotObjectCode(Package* package,
290+
int64_t opt_level,
291291
bool with_msan,
292292
JitObserver* observer) {
293293
XLS_ASSIGN_OR_RETURN(ProcElaboration elaboration,
294294
ProcElaboration::ElaborateOldStylePackage(package));
295-
return GetAotObjectCode(std::move(elaboration), with_msan, observer);
295+
return GetAotObjectCode(std::move(elaboration), opt_level, with_msan,
296+
observer);
296297
}
297-
absl::StatusOr<JitObjectCode> CreateProcAotObjectCode(Proc* top, bool with_msan,
298+
absl::StatusOr<JitObjectCode> CreateProcAotObjectCode(Proc* top,
299+
int64_t opt_level,
300+
bool with_msan,
298301
JitObserver* observer) {
299302
XLS_ASSIGN_OR_RETURN(ProcElaboration elaboration,
300303
ProcElaboration::Elaborate(top));
301-
return GetAotObjectCode(std::move(elaboration), with_msan, observer);
304+
return GetAotObjectCode(std::move(elaboration), opt_level, with_msan,
305+
observer);
302306
}
303307

304308
// Create a SerialProcRuntime composed of ProcJits. Constructed from the

xls/jit/jit_proc_runtime.h

+5-2
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
#ifndef XLS_JIT_JIT_PROC_RUNTIME_H_
1616
#define XLS_JIT_JIT_PROC_RUNTIME_H_
1717

18+
#include <cstdint>
1819
#include <memory>
1920
#include <optional>
2021

@@ -71,10 +72,12 @@ absl::StatusOr<std::unique_ptr<SerialProcRuntime>> CreateAotSerialProcRuntime(
7172

7273
// Generate AOT code for the given proc elaboration.
7374
absl::StatusOr<JitObjectCode> CreateProcAotObjectCode(
74-
Package* package, bool with_msan, JitObserver* observer = nullptr);
75+
Package* package, int64_t opt_level, bool with_msan,
76+
JitObserver* observer = nullptr);
7577
// Generate AOT code for the given proc elaboration.
7678
absl::StatusOr<JitObjectCode> CreateProcAotObjectCode(
77-
Proc* top, bool with_msan, JitObserver* observer = nullptr);
79+
Proc* top, int64_t opt_level, bool with_msan,
80+
JitObserver* observer = nullptr);
7881

7982
} // namespace xls
8083

0 commit comments

Comments
 (0)