Skip to content

Commit 3b85d83

Browse files
authored
feat: add granular tail call detection infrastructure to MachInst (#11599)
* feat: add granular tail call detection infrastructure to machinst Adds core infrastructure for distinguishing between regular calls and tail calls at the instruction level. * feat: implement call_type() method for all ISA backends * refactor: pass around function_calls enum instead of boolean * feat: add function_calls.update() logic
1 parent 3c3fb35 commit 3b85d83

File tree

13 files changed

+122
-61
lines changed

13 files changed

+122
-61
lines changed

cranelift/codegen/src/isa/aarch64/abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1120,7 +1120,7 @@ impl ABIMachineSpec for AArch64MachineDeps {
11201120
flags: &settings::Flags,
11211121
sig: &Signature,
11221122
regs: &[Writable<RealReg>],
1123-
is_leaf: bool,
1123+
function_calls: FunctionCalls,
11241124
incoming_args_size: u32,
11251125
tail_args_size: u32,
11261126
stackslots_size: u32,
@@ -1144,7 +1144,7 @@ impl ABIMachineSpec for AArch64MachineDeps {
11441144

11451145
// Compute linkage frame size.
11461146
let setup_area_size = if flags.preserve_frame_pointers()
1147-
|| !is_leaf
1147+
|| function_calls != FunctionCalls::None
11481148
// The function arguments that are passed on the stack are addressed
11491149
// relative to the Frame Pointer.
11501150
|| incoming_args_size > 0
@@ -1167,7 +1167,7 @@ impl ABIMachineSpec for AArch64MachineDeps {
11671167
stackslots_size,
11681168
outgoing_args_size,
11691169
clobbered_callee_saves: regs,
1170-
is_leaf,
1170+
function_calls,
11711171
}
11721172
}
11731173

cranelift/codegen/src/isa/aarch64/inst/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1000,15 +1000,16 @@ impl MachInst for Inst {
10001000
}
10011001
}
10021002

1003-
fn is_call(&self) -> bool {
1003+
fn call_type(&self) -> CallType {
10041004
match self {
10051005
Inst::Call { .. }
10061006
| Inst::CallInd { .. }
1007-
| Inst::ReturnCall { .. }
1008-
| Inst::ReturnCallInd { .. }
10091007
| Inst::ElfTlsGetAddr { .. }
1010-
| Inst::MachOTlsGetAddr { .. } => true,
1011-
_ => false,
1008+
| Inst::MachOTlsGetAddr { .. } => CallType::Regular,
1009+
1010+
Inst::ReturnCall { .. } | Inst::ReturnCallInd { .. } => CallType::TailCall,
1011+
1012+
_ => CallType::None,
10121013
}
10131014
}
10141015

cranelift/codegen/src/isa/pulley_shared/abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ where
495495
flags: &settings::Flags,
496496
_sig: &Signature,
497497
regs: &[Writable<RealReg>],
498-
is_leaf: bool,
498+
function_calls: FunctionCalls,
499499
incoming_args_size: u32,
500500
tail_args_size: u32,
501501
stackslots_size: u32,
@@ -515,7 +515,7 @@ where
515515

516516
// Compute linkage frame size.
517517
let setup_area_size = if flags.preserve_frame_pointers()
518-
|| !is_leaf
518+
|| function_calls != FunctionCalls::None
519519
// The function arguments that are passed on the stack are addressed
520520
// relative to the Frame Pointer.
521521
|| incoming_args_size > 0
@@ -537,7 +537,7 @@ where
537537
stackslots_size,
538538
outgoing_args_size,
539539
clobbered_callee_saves: regs,
540-
is_leaf,
540+
function_calls,
541541
}
542542
}
543543

cranelift/codegen/src/isa/pulley_shared/inst/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -490,14 +490,15 @@ where
490490
todo!()
491491
}
492492

493-
fn is_call(&self) -> bool {
493+
fn call_type(&self) -> CallType {
494494
match &self.inst {
495-
Inst::Call { .. }
496-
| Inst::IndirectCall { .. }
497-
| Inst::IndirectCallHost { .. }
498-
| Inst::ReturnCall { .. }
499-
| Inst::ReturnIndirectCall { .. } => true,
500-
_ => false,
495+
Inst::Call { .. } | Inst::IndirectCall { .. } | Inst::IndirectCallHost { .. } => {
496+
CallType::Regular
497+
}
498+
499+
Inst::ReturnCall { .. } | Inst::ReturnIndirectCall { .. } => CallType::TailCall,
500+
501+
_ => CallType::None,
501502
}
502503
}
503504

cranelift/codegen/src/isa/riscv64/abi.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -628,7 +628,7 @@ impl ABIMachineSpec for Riscv64MachineDeps {
628628
flags: &settings::Flags,
629629
_sig: &Signature,
630630
regs: &[Writable<RealReg>],
631-
is_leaf: bool,
631+
function_calls: FunctionCalls,
632632
incoming_args_size: u32,
633633
tail_args_size: u32,
634634
stackslots_size: u32,
@@ -648,7 +648,7 @@ impl ABIMachineSpec for Riscv64MachineDeps {
648648

649649
// Compute linkage frame size.
650650
let setup_area_size = if flags.preserve_frame_pointers()
651-
|| !is_leaf
651+
|| function_calls != FunctionCalls::None
652652
// The function arguments that are passed on the stack are addressed
653653
// relative to the Frame Pointer.
654654
|| incoming_args_size > 0
@@ -671,7 +671,7 @@ impl ABIMachineSpec for Riscv64MachineDeps {
671671
stackslots_size,
672672
outgoing_args_size,
673673
clobbered_callee_saves: regs,
674-
is_leaf,
674+
function_calls,
675675
}
676676
}
677677

cranelift/codegen/src/isa/riscv64/inst/mod.rs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -758,14 +758,15 @@ impl MachInst for Inst {
758758
}
759759
}
760760

761-
fn is_call(&self) -> bool {
761+
fn call_type(&self) -> CallType {
762762
match self {
763-
Inst::Call { .. }
764-
| Inst::CallInd { .. }
765-
| Inst::ReturnCall { .. }
766-
| Inst::ReturnCallInd { .. }
767-
| Inst::ElfTlsGetAddr { .. } => true,
768-
_ => false,
763+
Inst::Call { .. } | Inst::CallInd { .. } | Inst::ElfTlsGetAddr { .. } => {
764+
CallType::Regular
765+
}
766+
767+
Inst::ReturnCall { .. } | Inst::ReturnCallInd { .. } => CallType::TailCall,
768+
769+
_ => CallType::None,
769770
}
770771
}
771772

cranelift/codegen/src/isa/s390x/abi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -896,7 +896,7 @@ impl ABIMachineSpec for S390xMachineDeps {
896896
flags: &settings::Flags,
897897
_sig: &Signature,
898898
regs: &[Writable<RealReg>],
899-
is_leaf: bool,
899+
function_calls: FunctionCalls,
900900
incoming_args_size: u32,
901901
tail_args_size: u32,
902902
stackslots_size: u32,
@@ -975,7 +975,7 @@ impl ABIMachineSpec for S390xMachineDeps {
975975
stackslots_size,
976976
outgoing_args_size,
977977
clobbered_callee_saves: regs,
978-
is_leaf,
978+
function_calls,
979979
}
980980
}
981981

cranelift/codegen/src/isa/s390x/inst/mod.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1122,10 +1122,13 @@ impl MachInst for Inst {
11221122
}
11231123
}
11241124

1125-
fn is_call(&self) -> bool {
1125+
fn call_type(&self) -> CallType {
11261126
match self {
1127-
Inst::Call { .. } | Inst::ReturnCall { .. } | Inst::ElfTlsGetOffset { .. } => true,
1128-
_ => false,
1127+
Inst::Call { .. } | Inst::ElfTlsGetOffset { .. } => CallType::Regular,
1128+
1129+
Inst::ReturnCall { .. } => CallType::TailCall,
1130+
1131+
_ => CallType::None,
11291132
}
11301133
}
11311134

cranelift/codegen/src/isa/x64/abi.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@ impl ABIMachineSpec for X64ABIMachineSpec {
900900
flags: &settings::Flags,
901901
_sig: &Signature,
902902
regs: &[Writable<RealReg>],
903-
is_leaf: bool,
903+
function_calls: FunctionCalls,
904904
incoming_args_size: u32,
905905
tail_args_size: u32,
906906
stackslots_size: u32,
@@ -947,7 +947,7 @@ impl ABIMachineSpec for X64ABIMachineSpec {
947947
stackslots_size,
948948
outgoing_args_size,
949949
clobbered_callee_saves: regs,
950-
is_leaf,
950+
function_calls,
951951
}
952952
}
953953

cranelift/codegen/src/isa/x64/inst/mod.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,15 +1281,16 @@ impl MachInst for Inst {
12811281
}
12821282
}
12831283

1284-
fn is_call(&self) -> bool {
1284+
fn call_type(&self) -> CallType {
12851285
match self {
12861286
Inst::CallKnown { .. }
12871287
| Inst::CallUnknown { .. }
1288-
| Inst::ReturnCallKnown { .. }
1289-
| Inst::ReturnCallUnknown { .. }
12901288
| Inst::ElfTlsGetAddr { .. }
1291-
| Inst::MachOTlsGetAddr { .. } => true,
1292-
_ => false,
1289+
| Inst::MachOTlsGetAddr { .. } => CallType::Regular,
1290+
1291+
Inst::ReturnCallKnown { .. } | Inst::ReturnCallUnknown { .. } => CallType::TailCall,
1292+
1293+
_ => CallType::None,
12931294
}
12941295
}
12951296

0 commit comments

Comments
 (0)