Skip to content

Commit 6c7b8ec

Browse files
committed
Add hotpatch flag. Proof of concept only working on x86/x64.
1 parent 03ae680 commit 6c7b8ec

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
@@ -370,6 +370,15 @@ pub fn from_fn_attrs<'ll, 'tcx>(
370370
to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
371371
}
372372

373+
// patchable-function is only implemented on x86 on LLVM
374+
if cx.sess().opts.unstable_opts.hotpatch && cx.sess().target.is_x86() {
375+
to_add.push(llvm::CreateAttrStringValue(
376+
cx.llcx,
377+
"patchable-function",
378+
"prologue-short-redirect",
379+
));
380+
}
381+
373382
// FIXME: none of these functions interact with source level attributes.
374383
to_add.extend(frame_pointer_type_attr(cx));
375384
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
@@ -36,6 +36,7 @@ impl OwnedTargetMachine {
3636
emit_stack_size_section: bool,
3737
relax_elf_relocations: bool,
3838
use_init_array: bool,
39+
use_hotpatch: bool,
3940
split_dwarf_file: &CStr,
4041
output_obj_file: &CStr,
4142
debug_info_compression: &CStr,
@@ -68,6 +69,7 @@ impl OwnedTargetMachine {
6869
emit_stack_size_section,
6970
relax_elf_relocations,
7071
use_init_array,
72+
use_hotpatch,
7173
split_dwarf_file.as_ptr(),
7274
output_obj_file.as_ptr(),
7375
debug_info_compression.as_ptr(),

compiler/rustc_codegen_llvm/src/back/write.rs

+5
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,10 @@ pub fn target_machine_factory(
221221
let use_init_array =
222222
!sess.opts.unstable_opts.use_ctors_section.unwrap_or(sess.target.use_ctors_section);
223223

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

226230
let use_emulated_tls = matches!(sess.tls_model(), TlsModel::Emulated);
@@ -293,6 +297,7 @@ pub fn target_machine_factory(
293297
emit_stack_size_section,
294298
relax_elf_relocations,
295299
use_init_array,
300+
use_hotpatch,
296301
&split_dwarf_file,
297302
&output_obj_file,
298303
&debuginfo_compression,

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+1
Original file line numberDiff line numberDiff line change
@@ -2189,6 +2189,7 @@ extern "C" {
21892189
EmitStackSizeSection: bool,
21902190
RelaxELFRelocations: bool,
21912191
UseInitArray: bool,
2192+
UseHotpatch: bool,
21922193
SplitDwarfFile: *const c_char,
21932194
OutputObjFile: *const c_char,
21942195
DebugInfoCompression: *const c_char,

compiler/rustc_interface/src/tests.rs

+1
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,7 @@ fn test_unstable_options_tracking_hash() {
787787
tracked!(fuel, Some(("abc".to_string(), 99)));
788788
tracked!(function_return, FunctionReturn::ThunkExtern);
789789
tracked!(function_sections, Some(false));
790+
tracked!(hotpatch, true);
790791
tracked!(human_readable_cgu_names, true);
791792
tracked!(incremental_ignore_spans, true);
792793
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 AsmComments,
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) {
@@ -438,6 +438,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
438438
Options.MCOptions.AsmVerbose = AsmComments;
439439
Options.MCOptions.PreserveAsmComments = AsmComments;
440440
Options.MCOptions.ABIName = ABIStr;
441+
Options.Hotpatch = UseHotpatch;
441442
if (SplitDwarfFile) {
442443
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
443444
}

compiler/rustc_session/src/options.rs

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

compiler/rustc_target/src/spec/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1963,6 +1963,9 @@ impl Target {
19631963

19641964
Ok(dl)
19651965
}
1966+
pub fn is_x86(&self) -> bool {
1967+
["x86", "x86_64"].contains(&&self.arch[..])
1968+
}
19661969
}
19671970

19681971
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)