Skip to content

Commit

Permalink
cranelift: Group labels by FuncId instead of Name
Browse files Browse the repository at this point in the history
This prevents confusing label locations when there are multiple functions
with the same Name.
  • Loading branch information
afonso360 committed Oct 7, 2023
1 parent fef8a90 commit 0d0ae66
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
4 changes: 2 additions & 2 deletions cranelift/jit/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ impl Module for JITModule {
.buffer
.relocs()
.iter()
.map(|reloc| ModuleReloc::from_mach_reloc(reloc, &ctx.func))
.map(|reloc| ModuleReloc::from_mach_reloc(reloc, &ctx.func, id))
.collect();

self.record_function_for_perf(ptr, size, &decl.linkage_name(id));
Expand Down Expand Up @@ -797,7 +797,7 @@ impl Module for JITModule {
size,
relocs: relocs
.iter()
.map(|reloc| ModuleReloc::from_mach_reloc(reloc, func))
.map(|reloc| ModuleReloc::from_mach_reloc(reloc, func, id))
.collect(),
});

Expand Down
12 changes: 8 additions & 4 deletions cranelift/module/src/module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ use core::fmt::Display;
use cranelift_codegen::binemit::{CodeOffset, Reloc};
use cranelift_codegen::entity::{entity_impl, PrimaryMap};
use cranelift_codegen::ir::function::{Function, VersionMarker};
use cranelift_codegen::ir::{ExternalName, UserFuncName};
use cranelift_codegen::ir::ExternalName;
use cranelift_codegen::settings::SetError;
use cranelift_codegen::{
ir, isa, CodegenError, CompileError, Context, FinalizedMachReloc, FinalizedRelocTarget,
Expand All @@ -36,7 +36,11 @@ pub struct ModuleReloc {

impl ModuleReloc {
/// Converts a `FinalizedMachReloc` produced from a `Function` into a `ModuleReloc`.
pub fn from_mach_reloc(mach_reloc: &FinalizedMachReloc, func: &Function) -> Self {
pub fn from_mach_reloc(
mach_reloc: &FinalizedMachReloc,
func: &Function,
func_id: FuncId,
) -> Self {
let name = match mach_reloc.target {
FinalizedRelocTarget::ExternalName(ExternalName::User(reff)) => {
let name = &func.params.user_named_funcs()[reff];
Expand All @@ -50,7 +54,7 @@ impl ModuleReloc {
ModuleRelocTarget::KnownSymbol(ks)
}
FinalizedRelocTarget::Func(offset) => {
ModuleRelocTarget::FunctionOffset(func.name.clone(), offset)
ModuleRelocTarget::FunctionOffset(func_id, offset)
}
};
Self {
Expand Down Expand Up @@ -430,7 +434,7 @@ pub enum ModuleRelocTarget {
/// Symbols known to the linker.
KnownSymbol(ir::KnownSymbol),
/// A offset inside a function
FunctionOffset(UserFuncName, CodeOffset),
FunctionOffset(FuncId, CodeOffset),
}

impl ModuleRelocTarget {
Expand Down
16 changes: 6 additions & 10 deletions cranelift/object/src/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use anyhow::anyhow;
use cranelift_codegen::binemit::{Addend, CodeOffset, Reloc};
use cranelift_codegen::entity::SecondaryMap;
use cranelift_codegen::ir::UserFuncName;
use cranelift_codegen::isa::{OwnedTargetIsa, TargetIsa};
use cranelift_codegen::{self, ir, FinalizedMachReloc};
use cranelift_control::ControlPlane;
Expand Down Expand Up @@ -132,7 +131,7 @@ pub struct ObjectModule {
libcalls: HashMap<ir::LibCall, SymbolId>,
libcall_names: Box<dyn Fn(ir::LibCall) -> String + Send + Sync>,
known_symbols: HashMap<ir::KnownSymbol, SymbolId>,
known_labels: HashMap<(UserFuncName, CodeOffset), SymbolId>,
known_labels: HashMap<(FuncId, CodeOffset), SymbolId>,
per_function_section: bool,
}

Expand Down Expand Up @@ -372,7 +371,9 @@ impl Module for ObjectModule {
if !relocs.is_empty() {
let relocs = relocs
.iter()
.map(|record| self.process_reloc(&ModuleReloc::from_mach_reloc(&record, func)))
.map(|record| {
self.process_reloc(&ModuleReloc::from_mach_reloc(&record, func, func_id))
})
.collect();
self.relocs.push(SymbolRelocs {
section,
Expand Down Expand Up @@ -594,15 +595,10 @@ impl ObjectModule {
}
}

ModuleRelocTarget::FunctionOffset(ref fname, offset) => {
match self.known_labels.entry((fname.clone(), offset)) {
ModuleRelocTarget::FunctionOffset(func_id, offset) => {
match self.known_labels.entry((func_id, offset)) {
Entry::Occupied(o) => *o.get(),
Entry::Vacant(v) => {
let func_user_name = fname.get_user().unwrap();
let func_id = FuncId::from_name(&ModuleRelocTarget::user(
func_user_name.namespace,
func_user_name.index,
));
let func_symbol_id = self.functions[func_id].unwrap().0;
let func_symbol = self.object.symbol(func_symbol_id);

Expand Down

0 comments on commit 0d0ae66

Please sign in to comment.