Skip to content

Commit a44085d

Browse files
authored
add -fopt-bisect-limit
1 parent 93bdd04 commit a44085d

8 files changed

Lines changed: 35 additions & 0 deletions

File tree

src/Compilation.zig

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,7 @@ pub const InitOptions = struct {
968968
linker_print_gc_sections: bool = false,
969969
linker_print_icf_sections: bool = false,
970970
linker_print_map: bool = false,
971+
linker_opt_bisect_limit: i32 = -1,
971972
each_lib_rpath: ?bool = null,
972973
build_id: ?bool = null,
973974
disable_c_depfile: bool = false,
@@ -1826,6 +1827,7 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
18261827
.print_gc_sections = options.linker_print_gc_sections,
18271828
.print_icf_sections = options.linker_print_icf_sections,
18281829
.print_map = options.linker_print_map,
1830+
.opt_bisect_limit = options.linker_opt_bisect_limit,
18291831
.z_nodelete = options.linker_z_nodelete,
18301832
.z_notext = options.linker_z_notext,
18311833
.z_defs = options.linker_z_defs,

src/codegen/llvm.zig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -520,6 +520,10 @@ pub const Object = struct {
520520
if (options.pie) llvm_module.setModulePIELevel();
521521
if (code_model != .Default) llvm_module.setModuleCodeModel(code_model);
522522

523+
if (options.opt_bisect_limit >= 0) {
524+
context.setOptBisectLimit(std.math.lossyCast(c_int, options.opt_bisect_limit));
525+
}
526+
523527
return Object{
524528
.gpa = gpa,
525529
.module = options.module.?,

src/codegen/llvm/bindings.zig

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,9 @@ pub const Context = opaque {
8585

8686
pub const createBuilder = LLVMCreateBuilderInContext;
8787
extern fn LLVMCreateBuilderInContext(C: *Context) *Builder;
88+
89+
pub const setOptBisectLimit = ZigLLVMSetOptBisectLimit;
90+
extern fn ZigLLVMSetOptBisectLimit(C: *Context, limit: c_int) void;
8891
};
8992

9093
pub const Value = opaque {

src/link.zig

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ pub const Options = struct {
169169
print_gc_sections: bool,
170170
print_icf_sections: bool,
171171
print_map: bool,
172+
opt_bisect_limit: i32,
172173

173174
objects: []Compilation.LinkObject,
174175
framework_dirs: []const []const u8,

src/main.zig

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ const usage_build_generic =
536536
\\ --test-runner [path] Specify a custom test runner
537537
\\
538538
\\Debug Options (Zig Compiler Development):
539+
\\ -fopt-bisect-limit [limit] Only run [limit] first LLVM optimization passes
539540
\\ -ftime-report Print timing diagnostics
540541
\\ -fstack-report Print stack size diagnostics
541542
\\ --verbose-link Display linker invocations
@@ -729,6 +730,7 @@ fn buildOutputType(
729730
var linker_print_gc_sections: bool = false;
730731
var linker_print_icf_sections: bool = false;
731732
var linker_print_map: bool = false;
733+
var linker_opt_bisect_limit: i32 = -1;
732734
var linker_z_nocopyreloc = false;
733735
var linker_z_nodelete = false;
734736
var linker_z_notext = false;
@@ -1285,6 +1287,8 @@ fn buildOutputType(
12851287
no_builtin = false;
12861288
} else if (mem.eql(u8, arg, "-fno-builtin")) {
12871289
no_builtin = true;
1290+
} else if (mem.startsWith(u8, arg, "-fopt-bisect-limit=")) {
1291+
linker_opt_bisect_limit = std.math.lossyCast(i32, parseIntSuffix(arg, "-fopt-bisect-limit=".len));
12881292
} else if (mem.eql(u8, arg, "--eh-frame-hdr")) {
12891293
link_eh_frame_hdr = true;
12901294
} else if (mem.eql(u8, arg, "--emit-relocs")) {
@@ -2996,6 +3000,7 @@ fn buildOutputType(
29963000
.linker_print_gc_sections = linker_print_gc_sections,
29973001
.linker_print_icf_sections = linker_print_icf_sections,
29983002
.linker_print_map = linker_print_map,
3003+
.linker_opt_bisect_limit = linker_opt_bisect_limit,
29993004
.linker_global_base = linker_global_base,
30003005
.linker_export_symbol_names = linker_export_symbol_names.items,
30013006
.linker_z_nocopyreloc = linker_z_nocopyreloc,

src/zig_llvm.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
#include <llvm/IR/Instructions.h>
3232
#include <llvm/IR/LegacyPassManager.h>
3333
#include <llvm/IR/Module.h>
34+
#include <llvm/IR/OptBisect.h>
3435
#include <llvm/IR/PassManager.h>
3536
#include <llvm/IR/Verifier.h>
3637
#include <llvm/InitializePasses.h>
@@ -412,6 +413,18 @@ ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref) {
412413
return wrap(Type::getTokenTy(*unwrap(context_ref)));
413414
}
414415

416+
417+
ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit) {
418+
// In LLVM15 we just have an OptBisect singleton we can edit.
419+
OptBisect& bisect = getOptBisector();
420+
bisect.setLimit(limit);
421+
422+
// In LLVM16 OptBisect will be wrapped in OptPassGate, and will need to be set per context.
423+
// static OptBisect _opt_bisector;
424+
// _opt_bisector.setLimit(limit);
425+
// unwrap(context_ref)->setOptPassGate(_opt_bisector);
426+
}
427+
415428
LLVMValueRef ZigLLVMAddFunctionInAddressSpace(LLVMModuleRef M, const char *Name, LLVMTypeRef FunctionTy, unsigned AddressSpace) {
416429
Function* func = Function::Create(unwrap<FunctionType>(FunctionTy), GlobalValue::ExternalLinkage, AddressSpace, Name, unwrap(M));
417430
return wrap(func);

src/zig_llvm.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ ZIG_EXTERN_C LLVMTargetMachineRef ZigLLVMCreateTargetMachine(LLVMTargetRef T, co
6767

6868
ZIG_EXTERN_C LLVMTypeRef ZigLLVMTokenTypeInContext(LLVMContextRef context_ref);
6969

70+
ZIG_EXTERN_C void ZigLLVMSetOptBisectLimit(LLVMContextRef context_ref, int limit);
71+
7072
ZIG_EXTERN_C LLVMValueRef ZigLLVMAddFunctionInAddressSpace(LLVMModuleRef M, const char *Name,
7173
LLVMTypeRef FunctionTy, unsigned AddressSpace);
7274

tools/stage2_gdb_pretty_printers.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,13 @@
22
# put "source /path/to/stage2_gdb_pretty_printers.py" in ~/.gdbinit to load it automatically.
33
import re
44
import gdb.printing
5+
6+
import sys
7+
from pathlib import Path
8+
sys.path.insert(0, str(Path(__file__).parent))
59
import stage2_pretty_printers_common as common
610

11+
712
class TypePrinter:
813
def __init__(self, val):
914
self.val = val

0 commit comments

Comments
 (0)