Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add x86 specific hotpatch flag to rustc #124966

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions compiler/rustc_codegen_llvm/src/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,15 @@ pub fn from_fn_attrs<'ll, 'tcx>(
to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
}

// patchable-function is only implemented on x86 on LLVM
if cx.sess().opts.unstable_opts.hotpatch && cx.sess().target.is_x86() {
to_add.push(llvm::CreateAttrStringValue(
cx.llcx,
"patchable-function",
"prologue-short-redirect",
));
}

// FIXME: none of these functions interact with source level attributes.
to_add.extend(frame_pointer_type_attr(cx));
to_add.extend(function_return_attr(cx));
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_codegen_llvm/src/back/owned_target_machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ impl OwnedTargetMachine {
emit_stack_size_section: bool,
relax_elf_relocations: bool,
use_init_array: bool,
use_hotpatch: bool,
split_dwarf_file: &CStr,
output_obj_file: &CStr,
debug_info_compression: &CStr,
Expand Down Expand Up @@ -68,6 +69,7 @@ impl OwnedTargetMachine {
emit_stack_size_section,
relax_elf_relocations,
use_init_array,
use_hotpatch,
split_dwarf_file.as_ptr(),
output_obj_file.as_ptr(),
debug_info_compression.as_ptr(),
Expand Down
5 changes: 5 additions & 0 deletions compiler/rustc_codegen_llvm/src/back/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,10 @@ pub fn target_machine_factory(
let use_init_array =
!sess.opts.unstable_opts.use_ctors_section.unwrap_or(sess.target.use_ctors_section);

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

let path_mapping = sess.source_map().path_mapping().clone();

let use_emulated_tls = matches!(sess.tls_model(), TlsModel::Emulated);
Expand Down Expand Up @@ -292,6 +296,7 @@ pub fn target_machine_factory(
emit_stack_size_section,
relax_elf_relocations,
use_init_array,
use_hotpatch,
&split_dwarf_file,
&output_obj_file,
&debuginfo_compression,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2189,6 +2189,7 @@ extern "C" {
EmitStackSizeSection: bool,
RelaxELFRelocations: bool,
UseInitArray: bool,
UseHotpatch: bool,
SplitDwarfFile: *const c_char,
OutputObjFile: *const c_char,
DebugInfoCompression: *const c_char,
Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_interface/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ fn test_unstable_options_tracking_hash() {
tracked!(fuel, Some(("abc".to_string(), 99)));
tracked!(function_return, FunctionReturn::ThunkExtern);
tracked!(function_sections, Some(false));
tracked!(hotpatch, true);
tracked!(human_readable_cgu_names, true);
tracked!(incremental_ignore_spans, true);
tracked!(inline_in_all_cgus, Some(true));
Expand Down
2 changes: 2 additions & 0 deletions compiler/rustc_llvm/llvm-wrapper/PassWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -405,6 +405,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
bool EmitStackSizeSection,
bool RelaxELFRelocations,
bool UseInitArray,
bool UseHotpatch,
const char *SplitDwarfFile,
const char *OutputObjFile,
const char *DebugInfoCompression,
Expand Down Expand Up @@ -436,6 +437,7 @@ extern "C" LLVMTargetMachineRef LLVMRustCreateTargetMachine(
Options.MCOptions.AsmVerbose = AsmComments;
Options.MCOptions.PreserveAsmComments = AsmComments;
Options.MCOptions.ABIName = ABIStr;
Options.Hotpatch = UseHotpatch;
if (SplitDwarfFile) {
Options.MCOptions.SplitDwarfFile = SplitDwarfFile;
}
Expand Down
4 changes: 4 additions & 0 deletions compiler/rustc_session/src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1718,6 +1718,10 @@ options! {
"explicitly enable the `cfg(target_thread_local)` directive"),
hir_stats: bool = (false, parse_bool, [UNTRACKED],
"print some statistics about AST and HIR (default: no)"),
hotpatch: bool = (false, parse_bool, [TRACKED],
"ensures hotpatching is always possible by ensuring that the first instruction of \
each function is at least two bytes, and no jump within the function goes to the first instruction. \
Should be combined with link-arg passing -functionpadmin to the linker. Currently only supported for x86 (default: false)"),
human_readable_cgu_names: bool = (false, parse_bool, [TRACKED],
"generate human-readable, predictable names for codegen units (default: no)"),
identify_regions: bool = (false, parse_bool, [UNTRACKED],
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_target/src/spec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1910,6 +1910,9 @@ impl Target {

Ok(dl)
}
pub fn is_x86(&self) -> bool {
["x86", "x86_64"].contains(&&self.arch[..])
}
}

pub trait HasTargetSpec {
Expand Down
12 changes: 12 additions & 0 deletions tests/codegen/hotpatch.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
//@ revisions: x32 x64
//@[x32] only-x86
//@[x64] only-x86_64
//@ compile-flags: -Z hotpatch

#![crate_type = "lib"]

#[no_mangle]
pub fn foo() {}

// CHECK: @foo() unnamed_addr #0
// CHECK: attributes #0 = { {{.*}} "patchable-function"="prologue-short-redirect" {{.*}}}
Loading