Skip to content

Commit 35f97b5

Browse files
committed
Add hotpatch flag. Proof of concept only working on x86/x64.
1 parent 9afe713 commit 35f97b5

File tree

9 files changed

+39
-1
lines changed

9 files changed

+39
-1
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+9
Original file line numberDiff line numberDiff line change
@@ -379,6 +379,15 @@ pub(crate) fn llfn_attrs_from_instance<'ll, 'tcx>(
379379
to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
380380
}
381381

382+
// patchable-function is only implemented on x86 on LLVM
383+
if cx.sess().opts.unstable_opts.hotpatch && cx.sess().target.is_x86() {
384+
to_add.push(llvm::CreateAttrStringValue(
385+
cx.llcx,
386+
"patchable-function",
387+
"prologue-short-redirect",
388+
));
389+
}
390+
382391
// FIXME: none of these functions interact with source level attributes.
383392
to_add.extend(frame_pointer_type_attr(cx));
384393
to_add.extend(function_return_attr(cx));

compiler/rustc_codegen_llvm/src/back/owned_target_machine.rs

+2
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ impl OwnedTargetMachine {
3535
emit_stack_size_section: bool,
3636
relax_elf_relocations: bool,
3737
use_init_array: bool,
38+
use_hotpatch: bool,
3839
split_dwarf_file: &CStr,
3940
output_obj_file: &CStr,
4041
debug_info_compression: &CStr,
@@ -67,6 +68,7 @@ impl OwnedTargetMachine {
6768
emit_stack_size_section,
6869
relax_elf_relocations,
6970
use_init_array,
71+
use_hotpatch,
7072
split_dwarf_file.as_ptr(),
7173
output_obj_file.as_ptr(),
7274
debug_info_compression.as_ptr(),

compiler/rustc_codegen_llvm/src/back/write.rs

+5
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,10 @@ pub(crate) fn target_machine_factory(
218218
let use_init_array =
219219
!sess.opts.unstable_opts.use_ctors_section.unwrap_or(sess.target.use_ctors_section);
220220

221+
// this annotes in the debug file that code is hotpatchable. In addtion without it -functionpadmin will be ignored.
222+
// See: https://github.com/llvm/llvm-project/blob/d703b922961e0d02a5effdd4bfbb23ad50a3cc9f/lld/COFF/Writer.cpp#L1298
223+
let use_hotpatch = sess.opts.unstable_opts.hotpatch && sess.target.is_x86();
224+
221225
let path_mapping = sess.source_map().path_mapping().clone();
222226

223227
let use_emulated_tls = matches!(sess.tls_model(), TlsModel::Emulated);
@@ -290,6 +294,7 @@ pub(crate) fn target_machine_factory(
290294
emit_stack_size_section,
291295
relax_elf_relocations,
292296
use_init_array,
297+
use_hotpatch,
293298
&split_dwarf_file,
294299
&output_obj_file,
295300
&debuginfo_compression,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2196,6 +2196,7 @@ unsafe extern "C" {
21962196
EmitStackSizeSection: bool,
21972197
RelaxELFRelocations: bool,
21982198
UseInitArray: bool,
2199+
UseHotpatch: bool,
21992200
SplitDwarfFile: *const c_char,
22002201
OutputObjFile: *const c_char,
22012202
DebugInfoCompression: *const c_char,

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -786,6 +786,7 @@ fn test_unstable_options_tracking_hash() {
786786
tracked!(fuel, Some(("abc".to_string(), 99)));
787787
tracked!(function_return, FunctionReturn::ThunkExtern);
788788
tracked!(function_sections, Some(false));
789+
tracked!(hotpatch, true);
789790
tracked!(human_readable_cgu_names, true);
790791
tracked!(incremental_ignore_spans, true);
791792
tracked!(inline_in_all_cgus, Some(true));

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -408,7 +408,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
408408
LLVMRustCodeGenOptLevel RustOptLevel, bool UseSoftFloat,
409409
bool FunctionSections, bool DataSections, bool UniqueSectionNames,
410410
bool TrapUnreachable, bool Singlethread, bool VerboseAsm,
411-
bool EmitStackSizeSection, bool RelaxELFRelocations, bool UseInitArray,
411+
bool EmitStackSizeSection, bool RelaxELFRelocations, bool UseInitArray, bool UseHotpatch,
412412
const char *SplitDwarfFile, const char *OutputObjFile,
413413
const char *DebugInfoCompression, bool UseEmulatedTls,
414414
const char *ArgsCstrBuff, size_t ArgsCstrBuffLen) {
@@ -439,6 +439,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
439439
// Always preserve comments that were written by the user
440440
Options.MCOptions.PreserveAsmComments = true;
441441
Options.MCOptions.ABIName = ABIStr;
442+
Options.Hotpatch = UseHotpatch;
442443
if (SplitDwarfFile) {
443444
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
444445
}

compiler/rustc_session/src/options.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1763,6 +1763,10 @@ options! {
17631763
"explicitly enable the `cfg(target_thread_local)` directive"),
17641764
hir_stats: bool = (false, parse_bool, [UNTRACKED],
17651765
"print some statistics about AST and HIR (default: no)"),
1766+
hotpatch: bool = (false, parse_bool, [TRACKED],
1767+
"ensures hotpatching is always possible by ensuring that the first instruction of \
1768+
each function is at least two bytes, and no jump within the function goes to the first instruction. \
1769+
Should be combined with link-arg passing -functionpadmin to the linker. Currently only supported for x86 (default: false)"),
17661770
human_readable_cgu_names: bool = (false, parse_bool, [TRACKED],
17671771
"generate human-readable, predictable names for codegen units (default: no)"),
17681772
identify_regions: bool = (false, parse_bool, [UNTRACKED],

compiler/rustc_target/src/spec/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1986,6 +1986,9 @@ impl Target {
19861986

19871987
Ok(dl)
19881988
}
1989+
pub fn is_x86(&self) -> bool {
1990+
["x86", "x86_64"].contains(&&self.arch[..])
1991+
}
19891992
}
19901993

19911994
pub trait HasTargetSpec {

tests/codegen/hotpatch.rs

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
//@ revisions: x32 x64
2+
//@[x32] only-x86
3+
//@[x64] only-x86_64
4+
//@ compile-flags: -Z hotpatch
5+
6+
#![crate_type = "lib"]
7+
8+
#[no_mangle]
9+
pub fn foo() {}
10+
11+
// CHECK: @foo() unnamed_addr #0
12+
// CHECK: attributes #0 = { {{.*}} "patchable-function"="prologue-short-redirect" {{.*}}}

0 commit comments

Comments
 (0)