Skip to content

Commit e7d7c62

Browse files
committed
Add ms-hotpatch flag. Proof of concept only working on x86/x64.
1 parent a6e87c5 commit e7d7c62

File tree

9 files changed

+39
-0
lines changed

9 files changed

+39
-0
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+9
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,15 @@ pub fn from_fn_attrs<'ll, 'tcx>(
341341
to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
342342
}
343343

344+
// patchable-function is only implemented on x86 on LLVM
345+
if cx.sess().opts.unstable_opts.ms_hotpatch && cx.sess().target.is_x86() {
346+
to_add.push(llvm::CreateAttrStringValue(
347+
cx.llcx,
348+
"patchable-function",
349+
"prologue-short-redirect",
350+
));
351+
}
352+
344353
// FIXME: none of these functions interact with source level attributes.
345354
to_add.extend(frame_pointer_type_attr(cx));
346355
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
@@ -220,6 +220,10 @@ pub fn target_machine_factory(
220220
let use_init_array =
221221
!sess.opts.unstable_opts.use_ctors_section.unwrap_or(sess.target.use_ctors_section);
222222

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

225229
let use_emulated_tls = matches!(sess.tls_model(), TlsModel::Emulated);
@@ -292,6 +296,7 @@ pub fn target_machine_factory(
292296
emit_stack_size_section,
293297
relax_elf_relocations,
294298
use_init_array,
299+
use_hotpatch,
295300
&split_dwarf_file,
296301
&output_obj_file,
297302
&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
@@ -798,6 +798,7 @@ fn test_unstable_options_tracking_hash() {
798798
tracked!(mir_keep_place_mention, true);
799799
tracked!(mir_opt_level, Some(4));
800800
tracked!(move_size_limit, Some(4096));
801+
tracked!(ms_hotpatch, true);
801802
tracked!(mutable_noalias, false);
802803
tracked!(
803804
next_solver,

compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp

+2
Original file line numberDiff line numberDiff line change
@@ -405,6 +405,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
405405
bool EmitStackSizeSection,
406406
bool RelaxELFRelocations,
407407
bool UseInitArray,
408+
bool UseHotpatch,
408409
const char *SplitDwarfFile,
409410
const char *OutputObjFile,
410411
const char *DebugInfoCompression,
@@ -436,6 +437,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
436437
Options.MCOptions.AsmVerbose = AsmComments;
437438
Options.MCOptions.PreserveAsmComments = AsmComments;
438439
Options.MCOptions.ABIName = ABIStr;
440+
Options.Hotpatch = UseHotpatch;
439441
if (SplitDwarfFile) {
440442
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
441443
}

compiler/rustc_session/src/options.rs

+4
Original file line numberDiff line numberDiff line change
@@ -1813,6 +1813,10 @@ options! {
18131813
"MIR optimization level (0-4; default: 1 in non optimized builds and 2 in optimized builds)"),
18141814
move_size_limit: Option<usize> = (None, parse_opt_number, [TRACKED],
18151815
"the size at which the `large_assignments` lint starts to be emitted"),
1816+
ms_hotpatch: bool = (false, parse_bool, [TRACKED],
1817+
"ensures hotpatching is always possible by ensuring that the first instruction of \
1818+
each function is at least two bytes, and no jump within the function goes to the first instruction. \
1819+
Should be combined with link-arg passing -functionpadmin to the linker. Currently only supported for x86 (default: false)"),
18161820
mutable_noalias: bool = (true, parse_bool, [TRACKED],
18171821
"emit noalias metadata for mutable references (default: yes)"),
18181822
next_solver: Option<NextSolverConfig> = (None, parse_next_solver_config, [TRACKED],

compiler/rustc_target/src/spec/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -1910,6 +1910,9 @@ impl Target {
19101910

19111911
Ok(dl)
19121912
}
1913+
pub fn is_x86(&self) -> bool {
1914+
["x86", "x86_64"].contains(&&self.arch[..])
1915+
}
19131916
}
19141917

19151918
pub trait HasTargetSpec {

tests/codegen/ms-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 ms-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)