Skip to content

Commit c9db233

Browse files
cfallinbjorn3
andauthored
Cranelift: move exception-handler metadata into callsites. (#10609)
* Rework MachBuffer interface for exception_handlers * Rework MachBuffer to store exception handler records in flattened vector. This commit updates the call-site metadata to refer to a range in a flattened vector containing tuples of handler tags and labels (before finalization) or code offsets (after finalization). It also provides an iterator accessor `.call_sites()` on the finalized buffer that yields this information in a safe way. --------- Co-authored-by: bjorn3 <17426603+bjorn3@users.noreply.github.com>
1 parent 930d354 commit c9db233

6 files changed

Lines changed: 109 additions & 100 deletions

File tree

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

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2942,14 +2942,11 @@ impl MachInstEmit for Inst {
29422942
let offset = sink.cur_offset();
29432943
sink.push_user_stack_map(state, offset, s);
29442944
}
2945-
sink.add_call_site();
29462945

2947-
// Add exception info, if any, at this point (which will
2948-
// be the return address on stack).
29492946
if let Some(try_call) = info.try_call_info.as_ref() {
2950-
for &(tag, label) in &try_call.exception_dests {
2951-
sink.add_exception_handler(tag, label);
2952-
}
2947+
sink.add_call_site(&try_call.exception_dests);
2948+
} else {
2949+
sink.add_call_site(&[]);
29532950
}
29542951

29552952
if info.callee_pop_size > 0 {
@@ -2989,14 +2986,11 @@ impl MachInstEmit for Inst {
29892986
let offset = sink.cur_offset();
29902987
sink.push_user_stack_map(state, offset, s);
29912988
}
2992-
sink.add_call_site();
29932989

2994-
// Add exception info, if any, at this point (which will
2995-
// be the return address on stack).
29962990
if let Some(try_call) = info.try_call_info.as_ref() {
2997-
for &(tag, label) in &try_call.exception_dests {
2998-
sink.add_exception_handler(tag, label);
2999-
}
2991+
sink.add_call_site(&try_call.exception_dests);
2992+
} else {
2993+
sink.add_call_site(&[]);
30002994
}
30012995

30022996
if info.callee_pop_size > 0 {
@@ -3035,7 +3029,7 @@ impl MachInstEmit for Inst {
30353029
// for the target, but rather a function relocation.
30363030
sink.add_reloc(Reloc::Arm64Call, &info.dest, 0);
30373031
sink.put4(enc_jump26(0b000101, 0));
3038-
sink.add_call_site();
3032+
sink.add_call_site(&[]);
30393033

30403034
// `emit_return_call_common_sequence` emits an island if
30413035
// necessary, so we can safely disable the worst-case-size check
@@ -3050,7 +3044,7 @@ impl MachInstEmit for Inst {
30503044
targets: vec![],
30513045
}
30523046
.emit(sink, emit_info, state);
3053-
sink.add_call_site();
3047+
sink.add_call_site(&[]);
30543048

30553049
// `emit_return_call_common_sequence` emits an island if
30563050
// necessary, so we can safely disable the worst-case-size check

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -182,14 +182,11 @@ fn pulley_emit<P>(
182182
let offset = sink.cur_offset();
183183
sink.push_user_stack_map(state, offset, s);
184184
}
185-
sink.add_call_site();
186185

187-
// Add exception info, if any, at this point (which will
188-
// be the return address on stack).
189186
if let Some(try_call) = info.try_call_info.as_ref() {
190-
for &(tag, label) in &try_call.exception_dests {
191-
sink.add_exception_handler(tag, label);
192-
}
187+
sink.add_call_site(&try_call.exception_dests);
188+
} else {
189+
sink.add_call_site(&[]);
193190
}
194191

195192
let adjust = -i32::try_from(info.callee_pop_size).unwrap();
@@ -226,14 +223,10 @@ fn pulley_emit<P>(
226223
sink.push_user_stack_map(state, offset, s);
227224
}
228225

229-
sink.add_call_site();
230-
231-
// Add exception info, if any, at this point (which will
232-
// be the return address on stack).
233226
if let Some(try_call) = info.try_call_info.as_ref() {
234-
for &(tag, label) in &try_call.exception_dests {
235-
sink.add_exception_handler(tag, label);
236-
}
227+
sink.add_call_site(&try_call.exception_dests);
228+
} else {
229+
sink.add_call_site(&[]);
237230
}
238231

239232
let adjust = -i32::try_from(info.callee_pop_size).unwrap();
@@ -295,7 +288,7 @@ fn pulley_emit<P>(
295288
let offset = sink.cur_offset();
296289
sink.push_user_stack_map(state, offset, s);
297290
}
298-
sink.add_call_site();
291+
sink.add_call_site(&[]);
299292

300293
// If a callee pop is happening here that means that something has
301294
// messed up, these are expected to be "very simple" signatures.

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

Lines changed: 7 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1123,7 +1123,6 @@ impl Inst {
11231123
}
11241124

11251125
&Inst::Call { ref info } => {
1126-
sink.add_call_site();
11271126
sink.add_reloc(Reloc::RiscvCallPlt, &info.dest, 0);
11281127

11291128
Inst::construct_auipc_and_jalr(Some(writable_link_reg()), writable_link_reg(), 0)
@@ -1135,12 +1134,10 @@ impl Inst {
11351134
sink.push_user_stack_map(state, offset, s);
11361135
}
11371136

1138-
// Add exception info, if any, at this point (which will
1139-
// be the return address on stack).
11401137
if let Some(try_call) = info.try_call_info.as_ref() {
1141-
for &(tag, label) in &try_call.exception_dests {
1142-
sink.add_exception_handler(tag, label);
1143-
}
1138+
sink.add_call_site(&try_call.exception_dests);
1139+
} else {
1140+
sink.add_call_site(&[]);
11441141
}
11451142

11461143
let callee_pop_size = i32::try_from(info.callee_pop_size).unwrap();
@@ -1181,14 +1178,10 @@ impl Inst {
11811178
sink.push_user_stack_map(state, offset, s);
11821179
}
11831180

1184-
sink.add_call_site();
1185-
1186-
// Add exception info, if any, at this point (which will
1187-
// be the return address on stack).
11881181
if let Some(try_call) = info.try_call_info.as_ref() {
1189-
for &(tag, label) in &try_call.exception_dests {
1190-
sink.add_exception_handler(tag, label);
1191-
}
1182+
sink.add_call_site(&try_call.exception_dests);
1183+
} else {
1184+
sink.add_call_site(&[]);
11921185
}
11931186

11941187
let callee_pop_size = i32::try_from(info.callee_pop_size).unwrap();
@@ -1220,7 +1213,7 @@ impl Inst {
12201213
&Inst::ReturnCall { ref info } => {
12211214
emit_return_call_common_sequence(sink, emit_info, state, info);
12221215

1223-
sink.add_call_site();
1216+
sink.add_call_site(&[]);
12241217
sink.add_reloc(Reloc::RiscvCallPlt, &info.dest, 0);
12251218
Inst::construct_auipc_and_jalr(None, writable_spilltmp_reg(), 0)
12261219
.into_iter()

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3216,14 +3216,11 @@ impl Inst {
32163216
sink.push_user_stack_map(state, offset, s);
32173217
}
32183218
put(sink, enc);
3219-
sink.add_call_site();
32203219

3221-
// Add exception info, if any, at this point (which will
3222-
// be the return address on stack).
32233220
if let Some(try_call) = info.try_call_info.as_ref() {
3224-
for &(tag, label) in &try_call.exception_dests {
3225-
sink.add_exception_handler(tag, label);
3226-
}
3221+
sink.add_call_site(&try_call.exception_dests);
3222+
} else {
3223+
sink.add_call_site(&[]);
32273224
}
32283225

32293226
state.nominal_sp_offset -= info.callee_pop_size;
@@ -3264,7 +3261,7 @@ impl Inst {
32643261
}
32653262
};
32663263
put(sink, enc);
3267-
sink.add_call_site();
3264+
sink.add_call_site(&[]);
32683265
}
32693266
&Inst::ElfTlsGetOffset { ref symbol, .. } => {
32703267
let opcode = 0xc05; // BRASL
@@ -3281,7 +3278,7 @@ impl Inst {
32813278
}
32823279

32833280
put(sink, &enc_ril_b(opcode, gpr(14), 0));
3284-
sink.add_call_site();
3281+
sink.add_call_site(&[]);
32853282
}
32863283
&Inst::Args { .. } => {}
32873284
&Inst::Rets { .. } => {}

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

Lines changed: 8 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,14 +1611,11 @@ pub(crate) fn emit(
16111611
// beginning of the immediate field.
16121612
emit_reloc(sink, Reloc::X86CallPCRel4, &call_info.dest, -4);
16131613
sink.put4(0);
1614-
sink.add_call_site();
16151614

1616-
// Add exception info, if any, at this point (which will
1617-
// be the return address on stack).
16181615
if let Some(try_call) = call_info.try_call_info.as_ref() {
1619-
for &(tag, label) in &try_call.exception_dests {
1620-
sink.add_exception_handler(tag, label);
1621-
}
1616+
sink.add_call_site(&try_call.exception_dests);
1617+
} else {
1618+
sink.add_call_site(&[]);
16221619
}
16231620

16241621
// Reclaim the outgoing argument area that was released by the callee, to ensure that
@@ -1663,7 +1660,7 @@ pub(crate) fn emit(
16631660
// beginning of the immediate field.
16641661
emit_reloc(sink, Reloc::X86CallPCRel4, &call_info.dest, -4);
16651662
sink.put4(0);
1666-
sink.add_call_site();
1663+
sink.add_call_site(&[]);
16671664
}
16681665

16691666
Inst::ReturnCallUnknown { info: call_info } => {
@@ -1675,7 +1672,7 @@ pub(crate) fn emit(
16751672
target: RegMem::reg(callee),
16761673
}
16771674
.emit(sink, info, state);
1678-
sink.add_call_site();
1675+
sink.add_call_site(&[]);
16791676
}
16801677

16811678
Inst::CallUnknown {
@@ -1717,14 +1714,10 @@ pub(crate) fn emit(
17171714
sink.push_user_stack_map(state, offset, s);
17181715
}
17191716

1720-
sink.add_call_site();
1721-
1722-
// Add exception info, if any, at this point (which will
1723-
// be the return address on stack).
17241717
if let Some(try_call) = call_info.try_call_info.as_ref() {
1725-
for &(tag, label) in &try_call.exception_dests {
1726-
sink.add_exception_handler(tag, label);
1727-
}
1718+
sink.add_call_site(&try_call.exception_dests);
1719+
} else {
1720+
sink.add_call_site(&[]);
17281721
}
17291722

17301723
// Reclaim the outgoing argument area that was released by the callee, to ensure that

0 commit comments

Comments
 (0)