authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-13 02:03:42+02:00
committergravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-08-15 10:56:33+02:00
log2bf244c0e92a02dbd260057332663aae61852712
tree86b4a0c3f33fd490616a6b12db86bb9f17513f9c
parentab9628dd2c7441e2cc648874583cc38a357998a1
signaturebadge-check Signed by SSH key SHA256:7B/LJ7bpR1eX8aCXSr4mtd5M45VMPKcx9zY8e95b5QM

llvm: Always omit the frame pointer for naked functions.


1 files changed, 16 insertions(+), 5 deletions(-)

src/codegen/llvm.zig+16-5
...@@ -2909,7 +2909,17 @@ pub const Object = struct {...@@ -2909,7 +2909,17 @@ pub const Object = struct {
2909 function_index.setAlignment(resolved.alignment.toLlvm(), &o.builder);2909 function_index.setAlignment(resolved.alignment.toLlvm(), &o.builder);
29102910
2911 // Function attributes that are independent of analysis results of the function body.2911 // Function attributes that are independent of analysis results of the function body.
2912 try o.addCommonFnAttributes(&attributes, owner_mod);2912 try o.addCommonFnAttributes(
2913 &attributes,
2914 owner_mod,
2915 // Some backends don't respect the `naked` attribute in `TargetFrameLowering::hasFP()`,
2916 // so for these backends, LLVM will happily emit code that accesses the stack through
2917 // the frame pointer. This is nonsensical since what the `naked` attribute does is
2918 // suppress generation of the prologue and epilogue, and the prologue is where the
2919 // frame pointer normally gets set up. At time of writing, this is the case for at
2920 // least x86 and RISC-V.
2921 owner_mod.omit_frame_pointer or fn_info.cc == .Naked,
2922 );
29132923
2914 if (fn_info.return_type == .noreturn_type) try attributes.addFnAttr(.noreturn, &o.builder);2924 if (fn_info.return_type == .noreturn_type) try attributes.addFnAttr(.noreturn, &o.builder);
29152925
...@@ -2956,13 +2966,14 @@ pub const Object = struct {...@@ -2956,13 +2966,14 @@ pub const Object = struct {
2956 o: *Object,2966 o: *Object,
2957 attributes: *Builder.FunctionAttributes.Wip,2967 attributes: *Builder.FunctionAttributes.Wip,
2958 owner_mod: *Package.Module,2968 owner_mod: *Package.Module,
2969 omit_frame_pointer: bool,
2959 ) Allocator.Error!void {2970 ) Allocator.Error!void {
2960 const comp = o.pt.zcu.comp;2971 const comp = o.pt.zcu.comp;
29612972
2962 if (!owner_mod.red_zone) {2973 if (!owner_mod.red_zone) {
2963 try attributes.addFnAttr(.noredzone, &o.builder);2974 try attributes.addFnAttr(.noredzone, &o.builder);
2964 }2975 }
2965 if (owner_mod.omit_frame_pointer) {2976 if (omit_frame_pointer) {
2966 try attributes.addFnAttr(.{ .string = .{2977 try attributes.addFnAttr(.{ .string = .{
2967 .kind = try o.builder.string("frame-pointer"),2978 .kind = try o.builder.string("frame-pointer"),
2968 .value = try o.builder.string("none"),2979 .value = try o.builder.string("none"),
...@@ -4528,7 +4539,7 @@ pub const Object = struct {...@@ -4528,7 +4539,7 @@ pub const Object = struct {
45284539
4529 var attributes: Builder.FunctionAttributes.Wip = .{};4540 var attributes: Builder.FunctionAttributes.Wip = .{};
4530 defer attributes.deinit(&o.builder);4541 defer attributes.deinit(&o.builder);
4531 try o.addCommonFnAttributes(&attributes, zcu.root_mod);4542 try o.addCommonFnAttributes(&attributes, zcu.root_mod, zcu.root_mod.omit_frame_pointer);
45324543
4533 function_index.setLinkage(.internal, &o.builder);4544 function_index.setLinkage(.internal, &o.builder);
4534 function_index.setCallConv(.fastcc, &o.builder);4545 function_index.setCallConv(.fastcc, &o.builder);
...@@ -4557,7 +4568,7 @@ pub const Object = struct {...@@ -4557,7 +4568,7 @@ pub const Object = struct {
45574568
4558 var attributes: Builder.FunctionAttributes.Wip = .{};4569 var attributes: Builder.FunctionAttributes.Wip = .{};
4559 defer attributes.deinit(&o.builder);4570 defer attributes.deinit(&o.builder);
4560 try o.addCommonFnAttributes(&attributes, zcu.root_mod);4571 try o.addCommonFnAttributes(&attributes, zcu.root_mod, zcu.root_mod.omit_frame_pointer);
45614572
4562 function_index.setLinkage(.internal, &o.builder);4573 function_index.setLinkage(.internal, &o.builder);
4563 function_index.setCallConv(.fastcc, &o.builder);4574 function_index.setCallConv(.fastcc, &o.builder);
...@@ -9676,7 +9687,7 @@ pub const FuncGen = struct {...@@ -9676,7 +9687,7 @@ pub const FuncGen = struct {
96769687
9677 var attributes: Builder.FunctionAttributes.Wip = .{};9688 var attributes: Builder.FunctionAttributes.Wip = .{};
9678 defer attributes.deinit(&o.builder);9689 defer attributes.deinit(&o.builder);
9679 try o.addCommonFnAttributes(&attributes, zcu.root_mod);9690 try o.addCommonFnAttributes(&attributes, zcu.root_mod, zcu.root_mod.omit_frame_pointer);
96809691
9681 function_index.setLinkage(.internal, &o.builder);9692 function_index.setLinkage(.internal, &o.builder);
9682 function_index.setCallConv(.fastcc, &o.builder);9693 function_index.setCallConv(.fastcc, &o.builder);