authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-10-25 19:15:17-04:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-10-25 19:15:17-04:00
log30d01c8fea68baab396081da040f49defa494088
tree410ad5e004d75dfb7cf29c077ab163e8853f8748
parent97dc5f6eb531c91e8bd23a5589cae64e0a4561e8
parentb5be01a5972d2288658eecbf6a52c3ea8331c151
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #9874 from leecannon/frame_pointer

Make omiting frame pointer independent of build mode

17 files changed, 102 insertions(+), 12 deletions(-)

lib/std/build.zig+9
......@@ -1515,6 +1515,8 @@ pub const LibExeObjStep = struct {
15151515
15161516 red_zone: ?bool = null,
15171517
1518 omit_frame_pointer: ?bool = null,
1519
15181520 subsystem: ?std.Target.SubSystem = null,
15191521
15201522 /// Overrides the default stack size
......@@ -2406,6 +2408,13 @@ pub const LibExeObjStep = struct {
24062408 try zig_args.append("-mno-red-zone");
24072409 }
24082410 }
2411 if (self.omit_frame_pointer) |omit_frame_pointer| {
2412 if (omit_frame_pointer) {
2413 try zig_args.append("-fomit-frame-pointer");
2414 } else {
2415 try zig_args.append("-fno-omit-frame-pointer");
2416 }
2417 }
24092418 if (self.disable_sanitize_c) {
24102419 try zig_args.append("-fno-sanitize-c");
24112420 }
src/Compilation.zig+13
......@@ -688,6 +688,7 @@ pub const InitOptions = struct {
688688 want_sanitize_c: ?bool = null,
689689 want_stack_check: ?bool = null,
690690 want_red_zone: ?bool = null,
691 omit_frame_pointer: ?bool = null,
691692 want_valgrind: ?bool = null,
692693 want_tsan: ?bool = null,
693694 want_compiler_rt: ?bool = null,
......@@ -1123,6 +1124,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11231124
11241125 const strip = options.strip or !target_util.hasDebugInfo(options.target);
11251126 const red_zone = options.want_red_zone orelse target_util.hasRedZone(options.target);
1127 const omit_frame_pointer = options.omit_frame_pointer orelse (options.optimize_mode != .Debug);
11261128
11271129 // We put everything into the cache hash that *cannot be modified during an incremental update*.
11281130 // For example, one cannot change the target between updates, but one can change source files,
......@@ -1156,6 +1158,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
11561158 cache.hash.add(tsan);
11571159 cache.hash.add(stack_check);
11581160 cache.hash.add(red_zone);
1161 cache.hash.add(omit_frame_pointer);
11591162 cache.hash.add(link_mode);
11601163 cache.hash.add(options.function_sections);
11611164 cache.hash.add(strip);
......@@ -1426,6 +1429,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
14261429 .tsan = tsan,
14271430 .stack_check = stack_check,
14281431 .red_zone = red_zone,
1432 .omit_frame_pointer = omit_frame_pointer,
14291433 .single_threaded = single_threaded,
14301434 .verbose_link = options.verbose_link,
14311435 .machine_code_model = options.machine_code_model,
......@@ -3292,6 +3296,12 @@ pub fn addCCArgs(
32923296 try argv.append("-mno-red-zone");
32933297 }
32943298
3299 if (comp.bin_file.options.omit_frame_pointer) {
3300 try argv.append("-fomit-frame-pointer");
3301 } else {
3302 try argv.append("-fno-omit-frame-pointer");
3303 }
3304
32953305 switch (comp.bin_file.options.optimize_mode) {
32963306 .Debug => {
32973307 // windows c runtime requires -D_DEBUG if using debug libraries
......@@ -4122,6 +4132,7 @@ fn buildOutputFromZig(
41224132 .want_sanitize_c = false,
41234133 .want_stack_check = false,
41244134 .want_red_zone = comp.bin_file.options.red_zone,
4135 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
41254136 .want_valgrind = false,
41264137 .want_tsan = false,
41274138 .want_pic = comp.bin_file.options.pic,
......@@ -4405,6 +4416,7 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
44054416 .include_compiler_rt = include_compiler_rt,
44064417 .enable_stack_probing = comp.bin_file.options.stack_check,
44074418 .red_zone = comp.bin_file.options.red_zone,
4419 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
44084420 .enable_time_report = comp.time_report,
44094421 .enable_stack_report = comp.stack_report,
44104422 .test_is_evented = comp.test_evented_io,
......@@ -4554,6 +4566,7 @@ pub fn build_crt_file(
45544566 .want_sanitize_c = false,
45554567 .want_stack_check = false,
45564568 .want_red_zone = comp.bin_file.options.red_zone,
4569 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
45574570 .want_valgrind = false,
45584571 .want_tsan = false,
45594572 .want_pic = comp.bin_file.options.pic,
src/clang_options_data.zig+16-2
......@@ -3014,7 +3014,14 @@ flagpd1("fno-objc-infer-related-result-type"),
30143014flagpd1("fno-objc-legacy-dispatch"),
30153015flagpd1("fno-objc-nonfragile-abi"),
30163016flagpd1("fno-objc-weak"),
3017flagpd1("fno-omit-frame-pointer"),
3017.{
3018 .name = "fno-omit-frame-pointer",
3019 .syntax = .flag,
3020 .zig_equivalent = .no_omit_frame_pointer,
3021 .pd1 = true,
3022 .pd2 = false,
3023 .psl = false,
3024},
30183025flagpd1("fno-openmp"),
30193026flagpd1("fno-openmp-cuda-force-full-runtime"),
30203027flagpd1("fno-openmp-cuda-mode"),
......@@ -3235,7 +3242,14 @@ flagpd1("fobjc-runtime-has-weak"),
32353242flagpd1("fobjc-sender-dependent-dispatch"),
32363243flagpd1("fobjc-subscripting-legacy-runtime"),
32373244flagpd1("fobjc-weak"),
3238flagpd1("fomit-frame-pointer"),
3245.{
3246 .name = "fomit-frame-pointer",
3247 .syntax = .flag,
3248 .zig_equivalent = .omit_frame_pointer,
3249 .pd1 = true,
3250 .pd2 = false,
3251 .psl = false,
3252},
32393253flagpd1("fopenmp"),
32403254flagpd1("fopenmp-cuda-force-full-runtime"),
32413255flagpd1("fopenmp-cuda-mode"),
src/codegen/llvm.zig+25
......@@ -668,6 +668,11 @@ pub const DeclGen = struct {
668668 if (!dg.module.comp.bin_file.options.red_zone) {
669669 dg.addFnAttr(llvm_fn, "noredzone");
670670 }
671 if (dg.module.comp.bin_file.options.omit_frame_pointer) {
672 dg.addFnAttrString(llvm_fn, "frame-pointer", "none");
673 } else {
674 dg.addFnAttrString(llvm_fn, "frame-pointer", "all");
675 }
671676 dg.addFnAttr(llvm_fn, "nounwind");
672677 if (dg.module.comp.unwind_tables) {
673678 dg.addFnAttr(llvm_fn, "uwtable");
......@@ -1541,10 +1546,30 @@ pub const DeclGen = struct {
15411546 val.addAttributeAtIndex(index, llvm_attr);
15421547 }
15431548
1549 fn addAttrString(
1550 dg: *DeclGen,
1551 val: *const llvm.Value,
1552 index: llvm.AttributeIndex,
1553 name: []const u8,
1554 value: []const u8,
1555 ) void {
1556 const llvm_attr = dg.context.createStringAttribute(
1557 name.ptr,
1558 @intCast(c_uint, name.len),
1559 value.ptr,
1560 @intCast(c_uint, value.len),
1561 );
1562 val.addAttributeAtIndex(index, llvm_attr);
1563 }
1564
15441565 fn addFnAttr(dg: DeclGen, val: *const llvm.Value, name: []const u8) void {
15451566 dg.addAttr(val, std.math.maxInt(llvm.AttributeIndex), name);
15461567 }
15471568
1569 fn addFnAttrString(dg: *DeclGen, val: *const llvm.Value, name: []const u8, value: []const u8) void {
1570 dg.addAttrString(val, std.math.maxInt(llvm.AttributeIndex), name, value);
1571 }
1572
15481573 fn removeFnAttr(fn_val: *const llvm.Value, name: []const u8) void {
15491574 removeAttr(fn_val, std.math.maxInt(llvm.AttributeIndex), name);
15501575 }
src/codegen/llvm/bindings.zig+3
......@@ -28,6 +28,9 @@ pub const Context = opaque {
2828 pub const createEnumAttribute = LLVMCreateEnumAttribute;
2929 extern fn LLVMCreateEnumAttribute(*const Context, KindID: c_uint, Val: u64) *const Attribute;
3030
31 pub const createStringAttribute = LLVMCreateStringAttribute;
32 extern fn LLVMCreateStringAttribute(*const Context, Key: [*]const u8, Key_Len: c_uint, Value: [*]const u8, Value_Len: c_uint) *const Attribute;
33
3134 pub const intType = LLVMIntTypeInContext;
3235 extern fn LLVMIntTypeInContext(C: *const Context, NumBits: c_uint) *const Type;
3336
src/glibc.zig+1
......@@ -953,6 +953,7 @@ fn buildSharedLib(
953953 .want_sanitize_c = false,
954954 .want_stack_check = false,
955955 .want_red_zone = comp.bin_file.options.red_zone,
956 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
956957 .want_valgrind = false,
957958 .want_tsan = false,
958959 .emit_h = null,
src/libcxx.zig+2
......@@ -189,6 +189,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {
189189 .want_sanitize_c = false,
190190 .want_stack_check = false,
191191 .want_red_zone = comp.bin_file.options.red_zone,
192 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
192193 .want_valgrind = false,
193194 .want_tsan = comp.bin_file.options.tsan,
194195 .want_pic = comp.bin_file.options.pic,
......@@ -321,6 +322,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
321322 .want_sanitize_c = false,
322323 .want_stack_check = false,
323324 .want_red_zone = comp.bin_file.options.red_zone,
325 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
324326 .want_valgrind = false,
325327 .want_tsan = comp.bin_file.options.tsan,
326328 .want_pic = comp.bin_file.options.pic,
src/libunwind.zig+1
......@@ -113,6 +113,7 @@ pub fn buildStaticLib(comp: *Compilation) !void {
113113 .want_sanitize_c = false,
114114 .want_stack_check = false,
115115 .want_red_zone = comp.bin_file.options.red_zone,
116 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
116117 .want_valgrind = false,
117118 .want_tsan = false,
118119 .want_pic = comp.bin_file.options.pic,
src/link.zig+1
......@@ -90,6 +90,7 @@ pub const Options = struct {
9090 tsan: bool,
9191 stack_check: bool,
9292 red_zone: bool,
93 omit_frame_pointer: bool,
9394 single_threaded: bool,
9495 verbose_link: bool,
9596 dll_export_fns: bool,
src/main.zig+12
......@@ -323,6 +323,8 @@ const usage_build_generic =
323323 \\ medium|large]
324324 \\ -mred-zone Force-enable the "red-zone"
325325 \\ -mno-red-zone Force-disable the "red-zone"
326 \\ -fomit-frame-pointer Omit the stack frame pointer
327 \\ -fno-omit-frame-pointer Store the stack frame pointer
326328 \\ -mexec-model=[value] Execution model (WASI only)
327329 \\ --name [name] Override root name (not a file path)
328330 \\ -O [mode] Choose what to optimize for
......@@ -581,6 +583,7 @@ fn buildOutputType(
581583 var want_sanitize_c: ?bool = null;
582584 var want_stack_check: ?bool = null;
583585 var want_red_zone: ?bool = null;
586 var omit_frame_pointer: ?bool = null;
584587 var want_valgrind: ?bool = null;
585588 var want_tsan: ?bool = null;
586589 var want_compiler_rt: ?bool = null;
......@@ -976,6 +979,10 @@ fn buildOutputType(
976979 want_red_zone = true;
977980 } else if (mem.eql(u8, arg, "-mno-red-zone")) {
978981 want_red_zone = false;
982 } else if (mem.eql(u8, arg, "-fomit-frame-pointer")) {
983 omit_frame_pointer = true;
984 } else if (mem.eql(u8, arg, "-fno-omit-frame-pointer")) {
985 omit_frame_pointer = false;
979986 } else if (mem.eql(u8, arg, "-fsanitize-c")) {
980987 want_sanitize_c = true;
981988 } else if (mem.eql(u8, arg, "-fno-sanitize-c")) {
......@@ -1217,6 +1224,8 @@ fn buildOutputType(
12171224 .no_lto => want_lto = false,
12181225 .red_zone => want_red_zone = true,
12191226 .no_red_zone => want_red_zone = false,
1227 .omit_frame_pointer => omit_frame_pointer = true,
1228 .no_omit_frame_pointer => omit_frame_pointer = false,
12201229 .unwind_tables => want_unwind_tables = true,
12211230 .no_unwind_tables => want_unwind_tables = false,
12221231 .nostdlib => ensure_libc_on_non_freestanding = false,
......@@ -2082,6 +2091,7 @@ fn buildOutputType(
20822091 .want_sanitize_c = want_sanitize_c,
20832092 .want_stack_check = want_stack_check,
20842093 .want_red_zone = want_red_zone,
2094 .omit_frame_pointer = omit_frame_pointer,
20852095 .want_valgrind = want_valgrind,
20862096 .want_tsan = want_tsan,
20872097 .want_compiler_rt = want_compiler_rt,
......@@ -3708,6 +3718,8 @@ pub const ClangArgIterator = struct {
37083718 nostdlibinc,
37093719 red_zone,
37103720 no_red_zone,
3721 omit_frame_pointer,
3722 no_omit_frame_pointer,
37113723 strip,
37123724 exec_model,
37133725 emit_llvm,
src/musl.zig+1
......@@ -207,6 +207,7 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
207207 .want_sanitize_c = false,
208208 .want_stack_check = false,
209209 .want_red_zone = comp.bin_file.options.red_zone,
210 .omit_frame_pointer = comp.bin_file.options.omit_frame_pointer,
210211 .want_valgrind = false,
211212 .want_tsan = false,
212213 .emit_h = null,
src/stage1.zig+1
......@@ -128,6 +128,7 @@ pub const Module = extern struct {
128128 include_compiler_rt: bool,
129129 enable_stack_probing: bool,
130130 red_zone: bool,
131 omit_frame_pointer: bool,
131132 enable_time_report: bool,
132133 enable_stack_report: bool,
133134 test_is_evented: bool,
src/stage1/all_types.hpp+1
......@@ -2175,6 +2175,7 @@ struct CodeGen {
21752175 bool dll_export_fns;
21762176 bool have_stack_probing;
21772177 bool red_zone;
2178 bool omit_frame_pointer;
21782179 bool function_sections;
21792180 bool include_compiler_rt;
21802181 bool test_is_evented;
src/stage1/codegen.cpp+6-10
......@@ -363,10 +363,6 @@ static bool cc_want_sret_attr(CallingConvention cc) {
363363 zig_unreachable();
364364}
365365
366static bool codegen_have_frame_pointer(CodeGen *g) {
367 return g->build_mode == BuildModeDebug;
368}
369
370366static void add_common_fn_attributes(CodeGen *g, LLVMValueRef llvm_fn) {
371367 if (!g->red_zone) {
372368 addLLVMFnAttr(llvm_fn, "noredzone");
......@@ -603,7 +599,7 @@ static LLVMValueRef make_fn_llvm_value(CodeGen *g, ZigFn *fn) {
603599 addLLVMFnAttrInt(llvm_fn, "alignstack", fn->alignstack_value);
604600 }
605601
606 if (codegen_have_frame_pointer(g) && cc != CallingConventionInline) {
602 if (!g->omit_frame_pointer && cc != CallingConventionInline) {
607603 ZigLLVMAddFunctionAttr(llvm_fn, "frame-pointer", "all");
608604 }
609605 if (fn->section_name) {
......@@ -1223,7 +1219,7 @@ static LLVMValueRef get_add_error_return_trace_addr_fn(CodeGen *g) {
12231219 // Error return trace memory is in the stack, which is impossible to be at address 0
12241220 // on any architecture.
12251221 addLLVMArgAttr(fn_val, (unsigned)0, "nonnull");
1226 if (codegen_have_frame_pointer(g)) {
1222 if (!g->omit_frame_pointer) {
12271223 ZigLLVMAddFunctionAttr(fn_val, "frame-pointer", "all");
12281224 }
12291225
......@@ -1299,7 +1295,7 @@ static LLVMValueRef get_return_err_fn(CodeGen *g) {
12991295 LLVMSetLinkage(fn_val, LLVMInternalLinkage);
13001296 ZigLLVMFunctionSetCallingConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified));
13011297 add_common_fn_attributes(g, fn_val);
1302 if (codegen_have_frame_pointer(g)) {
1298 if (!g->omit_frame_pointer) {
13031299 ZigLLVMAddFunctionAttr(fn_val, "frame-pointer", "all");
13041300 }
13051301
......@@ -1381,7 +1377,7 @@ static LLVMValueRef get_safety_crash_err_fn(CodeGen *g) {
13811377 LLVMSetLinkage(fn_val, LLVMInternalLinkage);
13821378 ZigLLVMFunctionSetCallingConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified));
13831379 add_common_fn_attributes(g, fn_val);
1384 if (codegen_have_frame_pointer(g)) {
1380 if (!g->omit_frame_pointer) {
13851381 ZigLLVMAddFunctionAttr(fn_val, "frame-pointer", "all");
13861382 }
13871383 // Not setting alignment here. See the comment above about
......@@ -2389,7 +2385,7 @@ static LLVMValueRef get_merge_err_ret_traces_fn_val(CodeGen *g) {
23892385
23902386 addLLVMArgAttr(fn_val, (unsigned)1, "noalias");
23912387 addLLVMArgAttr(fn_val, (unsigned)1, "readonly");
2392 if (codegen_have_frame_pointer(g)) {
2388 if (!g->omit_frame_pointer) {
23932389 ZigLLVMAddFunctionAttr(fn_val, "frame-pointer", "all");
23942390 }
23952391
......@@ -5418,7 +5414,7 @@ static LLVMValueRef get_enum_tag_name_function(CodeGen *g, ZigType *enum_type) {
54185414 LLVMSetLinkage(fn_val, LLVMInternalLinkage);
54195415 ZigLLVMFunctionSetCallingConv(fn_val, get_llvm_cc(g, CallingConventionUnspecified));
54205416 add_common_fn_attributes(g, fn_val);
5421 if (codegen_have_frame_pointer(g)) {
5417 if (!g->omit_frame_pointer) {
54225418 ZigLLVMAddFunctionAttr(fn_val, "frame-pointer", "all");
54235419 }
54245420
src/stage1/stage1.cpp+1
......@@ -95,6 +95,7 @@ void zig_stage1_build_object(struct ZigStage1 *stage1) {
9595 g->unwind_tables = stage1->unwind_tables;
9696 g->have_stack_probing = stage1->enable_stack_probing;
9797 g->red_zone = stage1->red_zone;
98 g->omit_frame_pointer = stage1->omit_frame_pointer;
9899 g->is_single_threaded = stage1->is_single_threaded;
99100 g->valgrind_enabled = stage1->valgrind_enabled;
100101 g->tsan_enabled = stage1->tsan_enabled;
src/stage1/stage1.h+1
......@@ -199,6 +199,7 @@ struct ZigStage1 {
199199 bool include_compiler_rt;
200200 bool enable_stack_probing;
201201 bool red_zone;
202 bool omit_frame_pointer;
202203 bool enable_time_report;
203204 bool enable_stack_report;
204205 bool test_is_evented;
tools/update_clang_options.zig+8
......@@ -300,6 +300,14 @@ const known_options = [_]KnownOpt{
300300 .name = "mno-red-zone",
301301 .ident = "no_red_zone",
302302 },
303 .{
304 .name = "fomit-frame-pointer",
305 .ident = "omit_frame_pointer",
306 },
307 .{
308 .name = "fno-omit-frame-pointer",
309 .ident = "no_omit_frame_pointer",
310 },
303311 .{
304312 .name = "MD",
305313 .ident = "dep_file",