authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2023-03-16 01:32:43-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-03-17 01:57:14-04:00
logcfcd6698cd9385938df27a2052c2309229171e4f
tree3ac0ce351109462a9f0e96c5f06fe94737be48a8
parent4ec299007a6a183edddfcb0505a9c1501da7ad0c

main: add debug option to dump unoptimized llvm ir


12 files changed, 75 insertions(+), 19 deletions(-)

lib/build_runner.zig+7-2
......@@ -204,7 +204,11 @@ pub fn main() !void {
204204 } else if (mem.eql(u8, arg, "--verbose-air")) {
205205 builder.verbose_air = true;
206206 } else if (mem.eql(u8, arg, "--verbose-llvm-ir")) {
207 builder.verbose_llvm_ir = true;
207 builder.verbose_llvm_ir = "-";
208 } else if (mem.startsWith(u8, arg, "--verbose-llvm-ir=")) {
209 builder.verbose_llvm_ir = arg["--verbose-llvm-ir=".len..];
210 } else if (mem.eql(u8, arg, "--verbose-llvm-bc=")) {
211 builder.verbose_llvm_bc = arg["--verbose-llvm-bc=".len..];
208212 } else if (mem.eql(u8, arg, "--verbose-cimport")) {
209213 builder.verbose_cimport = true;
210214 } else if (mem.eql(u8, arg, "--verbose-cc")) {
......@@ -990,7 +994,8 @@ fn usage(builder: *std.Build, already_ran_build: bool, out_stream: anytype) !voi
990994 \\ --debug-pkg-config Fail if unknown pkg-config flags encountered
991995 \\ --verbose-link Enable compiler debug output for linking
992996 \\ --verbose-air Enable compiler debug output for Zig AIR
993 \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR
997 \\ --verbose-llvm-ir[=file] Enable compiler debug output for LLVM IR
998 \\ --verbose-llvm-bc=[file] Enable compiler debug output for LLVM BC
994999 \\ --verbose-cimport Enable compiler debug output for C imports
9951000 \\ --verbose-cc Enable compiler debug output for C compilation
9961001 \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features
lib/std/Build.zig+5-2
......@@ -54,7 +54,8 @@ verbose: bool,
5454verbose_link: bool,
5555verbose_cc: bool,
5656verbose_air: bool,
57verbose_llvm_ir: bool,
57verbose_llvm_ir: ?[]const u8,
58verbose_llvm_bc: ?[]const u8,
5859verbose_cimport: bool,
5960verbose_llvm_cpu_features: bool,
6061reference_trace: ?u32 = null,
......@@ -204,7 +205,8 @@ pub fn create(
204205 .verbose_link = false,
205206 .verbose_cc = false,
206207 .verbose_air = false,
207 .verbose_llvm_ir = false,
208 .verbose_llvm_ir = null,
209 .verbose_llvm_bc = null,
208210 .verbose_cimport = false,
209211 .verbose_llvm_cpu_features = false,
210212 .invalid_user_input = false,
......@@ -292,6 +294,7 @@ fn createChildOnly(parent: *Build, dep_name: []const u8, build_root: Cache.Direc
292294 .verbose_cc = parent.verbose_cc,
293295 .verbose_air = parent.verbose_air,
294296 .verbose_llvm_ir = parent.verbose_llvm_ir,
297 .verbose_llvm_bc = parent.verbose_llvm_bc,
295298 .verbose_cimport = parent.verbose_cimport,
296299 .verbose_llvm_cpu_features = parent.verbose_llvm_cpu_features,
297300 .reference_trace = parent.reference_trace,
lib/std/Build/CompileStep.zig+2-1
......@@ -1438,7 +1438,8 @@ fn make(step: *Step, prog_node: *std.Progress.Node) !void {
14381438
14391439 if (b.verbose_cimport) try zig_args.append("--verbose-cimport");
14401440 if (b.verbose_air) try zig_args.append("--verbose-air");
1441 if (b.verbose_llvm_ir) try zig_args.append("--verbose-llvm-ir");
1441 if (b.verbose_llvm_ir) |path| try zig_args.append(b.fmt("--verbose-llvm-ir={s}", .{path}));
1442 if (b.verbose_llvm_bc) |path| try zig_args.append(b.fmt("--verbose-llvm-bc={s}", .{path}));
14421443 if (b.verbose_link or self.verbose_link) try zig_args.append("--verbose-link");
14431444 if (b.verbose_cc or self.verbose_cc) try zig_args.append("--verbose-cc");
14441445 if (b.verbose_llvm_cpu_features) try zig_args.append("--verbose-llvm-cpu-features");
src/Compilation.zig+7-2
......@@ -86,7 +86,8 @@ clang_preprocessor_mode: ClangPreprocessorMode,
8686/// Whether to print clang argvs to stdout.
8787verbose_cc: bool,
8888verbose_air: bool,
89verbose_llvm_ir: bool,
89verbose_llvm_ir: ?[]const u8,
90verbose_llvm_bc: ?[]const u8,
9091verbose_cimport: bool,
9192verbose_llvm_cpu_features: bool,
9293disable_c_depfile: bool,
......@@ -585,7 +586,8 @@ pub const InitOptions = struct {
585586 verbose_cc: bool = false,
586587 verbose_link: bool = false,
587588 verbose_air: bool = false,
588 verbose_llvm_ir: bool = false,
589 verbose_llvm_ir: ?[]const u8 = null,
590 verbose_llvm_bc: ?[]const u8 = null,
589591 verbose_cimport: bool = false,
590592 verbose_llvm_cpu_features: bool = false,
591593 is_test: bool = false,
......@@ -1559,6 +1561,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
15591561 .verbose_cc = options.verbose_cc,
15601562 .verbose_air = options.verbose_air,
15611563 .verbose_llvm_ir = options.verbose_llvm_ir,
1564 .verbose_llvm_bc = options.verbose_llvm_bc,
15621565 .verbose_cimport = options.verbose_cimport,
15631566 .verbose_llvm_cpu_features = options.verbose_llvm_cpu_features,
15641567 .disable_c_depfile = options.disable_c_depfile,
......@@ -5342,6 +5345,7 @@ fn buildOutputFromZig(
53425345 .verbose_link = comp.bin_file.options.verbose_link,
53435346 .verbose_air = comp.verbose_air,
53445347 .verbose_llvm_ir = comp.verbose_llvm_ir,
5348 .verbose_llvm_bc = comp.verbose_llvm_bc,
53455349 .verbose_cimport = comp.verbose_cimport,
53465350 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
53475351 .clang_passthrough_mode = comp.clang_passthrough_mode,
......@@ -5419,6 +5423,7 @@ pub fn build_crt_file(
54195423 .verbose_link = comp.bin_file.options.verbose_link,
54205424 .verbose_air = comp.verbose_air,
54215425 .verbose_llvm_ir = comp.verbose_llvm_ir,
5426 .verbose_llvm_bc = comp.verbose_llvm_bc,
54225427 .verbose_cimport = comp.verbose_cimport,
54235428 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
54245429 .clang_passthrough_mode = comp.clang_passthrough_mode,
src/Module.zig+2-2
......@@ -4263,7 +4263,7 @@ pub fn ensureFuncBodyAnalyzed(mod: *Module, func: *Fn) SemaError!void {
42634263 comp.emit_llvm_bc == null);
42644264
42654265 const dump_air = builtin.mode == .Debug and comp.verbose_air;
4266 const dump_llvm_ir = builtin.mode == .Debug and comp.verbose_llvm_ir;
4266 const dump_llvm_ir = builtin.mode == .Debug and (comp.verbose_llvm_ir != null or comp.verbose_llvm_bc != null);
42674267
42684268 if (no_bin_file and !dump_air and !dump_llvm_ir) return;
42694269
......@@ -6395,7 +6395,7 @@ pub fn linkerUpdateDecl(mod: *Module, decl_index: Decl.Index) !void {
63956395 comp.emit_llvm_ir == null and
63966396 comp.emit_llvm_bc == null);
63976397
6398 const dump_llvm_ir = builtin.mode == .Debug and comp.verbose_llvm_ir;
6398 const dump_llvm_ir = builtin.mode == .Debug and (comp.verbose_llvm_ir != null or comp.verbose_llvm_bc != null);
63996399
64006400 if (no_bin_file and !dump_llvm_ir) return;
64016401
src/codegen/llvm.zig+29-2
......@@ -751,8 +751,35 @@ pub const Object = struct {
751751 dib.finalize();
752752 }
753753
754 if (comp.verbose_llvm_ir) {
755 self.llvm_module.dump();
754 if (comp.verbose_llvm_ir) |path| {
755 if (std.mem.eql(u8, path, "-")) {
756 self.llvm_module.dump();
757 } else {
758 const path_z = try comp.gpa.dupeZ(u8, path);
759 defer comp.gpa.free(path_z);
760
761 var error_message: [*:0]const u8 = undefined;
762
763 if (self.llvm_module.printModuleToFile(path_z, &error_message).toBool()) {
764 defer llvm.disposeMessage(error_message);
765
766 log.err("dump LLVM module failed ir={s}: {s}", .{
767 path, error_message,
768 });
769 }
770 }
771 }
772
773 if (comp.verbose_llvm_bc) |path| {
774 const path_z = try comp.gpa.dupeZ(u8, path);
775 defer comp.gpa.free(path_z);
776
777 const error_code = self.llvm_module.writeBitcodeToFile(path_z);
778 if (error_code != 0) {
779 log.err("dump LLVM module failed bc={s}: {d}", .{
780 path, error_code,
781 });
782 }
756783 }
757784
758785 var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa);
src/codegen/llvm/bindings.zig+3
......@@ -422,6 +422,9 @@ pub const Module = opaque {
422422
423423 pub const printModuleToFile = LLVMPrintModuleToFile;
424424 extern fn LLVMPrintModuleToFile(M: *Module, Filename: [*:0]const u8, ErrorMessage: *[*:0]const u8) Bool;
425
426 pub const writeBitcodeToFile = LLVMWriteBitcodeToFile;
427 extern fn LLVMWriteBitcodeToFile(M: *Module, Path: [*:0]const u8) c_int;
425428};
426429
427430pub const lookupIntrinsicID = LLVMLookupIntrinsicID;
src/glibc.zig+1
......@@ -1097,6 +1097,7 @@ fn buildSharedLib(
10971097 .verbose_link = comp.bin_file.options.verbose_link,
10981098 .verbose_air = comp.verbose_air,
10991099 .verbose_llvm_ir = comp.verbose_llvm_ir,
1100 .verbose_llvm_bc = comp.verbose_llvm_bc,
11001101 .verbose_cimport = comp.verbose_cimport,
11011102 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
11021103 .clang_passthrough_mode = comp.clang_passthrough_mode,
src/libcxx.zig+2
......@@ -250,6 +250,7 @@ pub fn buildLibCXX(comp: *Compilation, prog_node: *std.Progress.Node) !void {
250250 .verbose_link = comp.bin_file.options.verbose_link,
251251 .verbose_air = comp.verbose_air,
252252 .verbose_llvm_ir = comp.verbose_llvm_ir,
253 .verbose_llvm_bc = comp.verbose_llvm_bc,
253254 .verbose_cimport = comp.verbose_cimport,
254255 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
255256 .clang_passthrough_mode = comp.clang_passthrough_mode,
......@@ -410,6 +411,7 @@ pub fn buildLibCXXABI(comp: *Compilation, prog_node: *std.Progress.Node) !void {
410411 .verbose_link = comp.bin_file.options.verbose_link,
411412 .verbose_air = comp.verbose_air,
412413 .verbose_llvm_ir = comp.verbose_llvm_ir,
414 .verbose_llvm_bc = comp.verbose_llvm_bc,
413415 .verbose_cimport = comp.verbose_cimport,
414416 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
415417 .clang_passthrough_mode = comp.clang_passthrough_mode,
src/libtsan.zig+1
......@@ -226,6 +226,7 @@ pub fn buildTsan(comp: *Compilation, prog_node: *std.Progress.Node) !void {
226226 .verbose_link = comp.bin_file.options.verbose_link,
227227 .verbose_air = comp.verbose_air,
228228 .verbose_llvm_ir = comp.verbose_llvm_ir,
229 .verbose_llvm_bc = comp.verbose_llvm_bc,
229230 .verbose_cimport = comp.verbose_cimport,
230231 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
231232 .clang_passthrough_mode = comp.clang_passthrough_mode,
src/libunwind.zig+1
......@@ -122,6 +122,7 @@ pub fn buildStaticLib(comp: *Compilation, prog_node: *std.Progress.Node) !void {
122122 .verbose_link = comp.bin_file.options.verbose_link,
123123 .verbose_air = comp.verbose_air,
124124 .verbose_llvm_ir = comp.verbose_llvm_ir,
125 .verbose_llvm_bc = comp.verbose_llvm_bc,
125126 .verbose_cimport = comp.verbose_cimport,
126127 .verbose_llvm_cpu_features = comp.verbose_llvm_cpu_features,
127128 .clang_passthrough_mode = comp.clang_passthrough_mode,
src/main.zig+15-8
......@@ -370,10 +370,10 @@ const usage_build_generic =
370370 \\ -fno-emit-bin Do not output machine code
371371 \\ -femit-asm[=path] Output .s (assembly code)
372372 \\ -fno-emit-asm (default) Do not output .s (assembly code)
373 \\ -femit-llvm-ir[=path] Produce a .ll file with LLVM IR (requires LLVM extensions)
374 \\ -fno-emit-llvm-ir (default) Do not produce a .ll file with LLVM IR
375 \\ -femit-llvm-bc[=path] Produce a LLVM module as a .bc file (requires LLVM extensions)
376 \\ -fno-emit-llvm-bc (default) Do not produce a LLVM module as a .bc file
373 \\ -femit-llvm-ir[=path] Produce a .ll file with optimized LLVM IR (requires LLVM extensions)
374 \\ -fno-emit-llvm-ir (default) Do not produce a .ll file with optimized LLVM IR
375 \\ -femit-llvm-bc[=path] Produce an optimized LLVM module as a .bc file (requires LLVM extensions)
376 \\ -fno-emit-llvm-bc (default) Do not produce an optimized LLVM module as a .bc file
377377 \\ -femit-h[=path] Generate a C header file (.h)
378378 \\ -fno-emit-h (default) Do not generate a C header file (.h)
379379 \\ -femit-docs[=path] Create a docs/ dir with html documentation
......@@ -555,13 +555,14 @@ const usage_build_generic =
555555 \\ --test-runner [path] Specify a custom test runner
556556 \\
557557 \\Debug Options (Zig Compiler Development):
558 \\ -fopt-bisect-limit [limit] Only run [limit] first LLVM optimization passes
558 \\ -fopt-bisect-limit=[limit] Only run [limit] first LLVM optimization passes
559559 \\ -ftime-report Print timing diagnostics
560560 \\ -fstack-report Print stack size diagnostics
561561 \\ --verbose-link Display linker invocations
562562 \\ --verbose-cc Display C compiler invocations
563563 \\ --verbose-air Enable compiler debug output for Zig AIR
564 \\ --verbose-llvm-ir Enable compiler debug output for LLVM IR
564 \\ --verbose-llvm-ir[=path] Enable compiler debug output for unoptimized LLVM IR
565 \\ --verbose-llvm-bc=[path] Enable compiler debug output for unoptimized LLVM BC
565566 \\ --verbose-cimport Enable compiler debug output for C imports
566567 \\ --verbose-llvm-cpu-features Enable compiler debug output for LLVM CPU features
567568 \\ --debug-log [scope] Enable printing debug/info log messages for scope
......@@ -704,7 +705,8 @@ fn buildOutputType(
704705 var verbose_link = (builtin.os.tag != .wasi or builtin.link_libc) and std.process.hasEnvVarConstant("ZIG_VERBOSE_LINK");
705706 var verbose_cc = (builtin.os.tag != .wasi or builtin.link_libc) and std.process.hasEnvVarConstant("ZIG_VERBOSE_CC");
706707 var verbose_air = false;
707 var verbose_llvm_ir = false;
708 var verbose_llvm_ir: ?[]const u8 = null;
709 var verbose_llvm_bc: ?[]const u8 = null;
708710 var verbose_cimport = false;
709711 var verbose_llvm_cpu_features = false;
710712 var time_report = false;
......@@ -1441,7 +1443,11 @@ fn buildOutputType(
14411443 } else if (mem.eql(u8, arg, "--verbose-air")) {
14421444 verbose_air = true;
14431445 } else if (mem.eql(u8, arg, "--verbose-llvm-ir")) {
1444 verbose_llvm_ir = true;
1446 verbose_llvm_ir = "-";
1447 } else if (mem.startsWith(u8, arg, "--verbose-llvm-ir=")) {
1448 verbose_llvm_ir = arg["--verbose-llvm-ir=".len..];
1449 } else if (mem.startsWith(u8, arg, "--verbose-llvm-bc=")) {
1450 verbose_llvm_bc = arg["--verbose-llvm-bc=".len..];
14451451 } else if (mem.eql(u8, arg, "--verbose-cimport")) {
14461452 verbose_cimport = true;
14471453 } else if (mem.eql(u8, arg, "--verbose-llvm-cpu-features")) {
......@@ -3226,6 +3232,7 @@ fn buildOutputType(
32263232 .verbose_link = verbose_link,
32273233 .verbose_air = verbose_air,
32283234 .verbose_llvm_ir = verbose_llvm_ir,
3235 .verbose_llvm_bc = verbose_llvm_bc,
32293236 .verbose_cimport = verbose_cimport,
32303237 .verbose_llvm_cpu_features = verbose_llvm_cpu_features,
32313238 .machine_code_model = machine_code_model,