Skip to content

Commit cca936d

Browse files
committed
Address review comments.
1 parent 4d721d0 commit cca936d

File tree

4 files changed

+38
-15
lines changed

4 files changed

+38
-15
lines changed

cranelift/codegen/src/ir/extfunc.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
use crate::ir::{ArgumentLoc, ExternalName, SigRef, Type};
99
use crate::isa::{CallConv, RegInfo, RegUnit};
10+
use crate::machinst::RelocDistance;
1011
use alloc::vec::Vec;
1112
use core::fmt;
1213
use core::str::FromStr;
@@ -366,6 +367,10 @@ pub struct ExtFuncData {
366367
/// Will this function be defined nearby, such that it will always be a certain distance away,
367368
/// after linking? If so, references to it can avoid going through a GOT or PLT. Note that
368369
/// symbols meant to be preemptible cannot be considered colocated.
370+
///
371+
/// If `true`, some backends may use relocation forms that have limited range: for example,
372+
/// a +/- 2^27-byte range on AArch64. See the documentation for
373+
/// [`RelocDistance`](machinst::RelocDistance) for more details.
369374
pub colocated: bool,
370375
}
371376

@@ -378,6 +383,17 @@ impl fmt::Display for ExtFuncData {
378383
}
379384
}
380385

386+
impl ExtFuncData {
387+
/// Return an estimate of the distance to the referred-to function symbol.
388+
pub fn reloc_distance(&self) -> RelocDistance {
389+
if self.colocated {
390+
RelocDistance::Near
391+
} else {
392+
RelocDistance::Far
393+
}
394+
}
395+
}
396+
381397
#[cfg(test)]
382398
mod tests {
383399
use super::*;

cranelift/codegen/src/ir/globalvalue.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
use crate::ir::immediates::{Imm64, Offset32};
44
use crate::ir::{ExternalName, GlobalValue, Type};
55
use crate::isa::TargetIsa;
6+
use crate::machinst::RelocDistance;
67
use core::fmt;
78

89
/// Information about a global value declaration.
@@ -62,6 +63,10 @@ pub enum GlobalValueData {
6263
/// Will this symbol be defined nearby, such that it will always be a certain distance
6364
/// away, after linking? If so, references to it can avoid going through a GOT. Note that
6465
/// symbols meant to be preemptible cannot be colocated.
66+
///
67+
/// If `true`, some backends may use relocation forms that have limited range: for example,
68+
/// a +/- 2^27-byte range on AArch64. See the documentation for
69+
/// [`RelocDistance`](machinst::RelocDistance) for more details.
6570
colocated: bool,
6671

6772
/// Does this symbol refer to a thread local storage value?
@@ -85,6 +90,20 @@ impl GlobalValueData {
8590
Self::IAddImm { global_type, .. } | Self::Load { global_type, .. } => global_type,
8691
}
8792
}
93+
94+
/// IF this global references a symbol, return an estimate of the relocation distance,
95+
/// based on the `colocated` flag.
96+
pub fn maybe_reloc_distance(&self) -> Option<RelocDistance> {
97+
match self {
98+
&GlobalValueData::Symbol {
99+
colocated: true, ..
100+
} => Some(RelocDistance::Near),
101+
&GlobalValueData::Symbol {
102+
colocated: false, ..
103+
} => Some(RelocDistance::Far),
104+
_ => None,
105+
}
106+
}
88107
}
89108

90109
impl fmt::Display for GlobalValueData {

cranelift/codegen/src/isa/aarch64/lower.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1868,7 +1868,7 @@ fn lower_insn_to_regs<C: LowerCtx<I = Inst>>(ctx: &mut C, insn: IRInst) {
18681868

18691869
Opcode::FuncAddr => {
18701870
let rd = output_to_reg(ctx, outputs[0]);
1871-
let (extname, _) = ctx.call_target(insn).unwrap().clone();
1871+
let (extname, _) = ctx.call_target(insn).unwrap();
18721872
let extname = extname.clone();
18731873
let loc = ctx.srcloc(insn);
18741874
ctx.emit(Inst::LoadExtName {

cranelift/codegen/src/machinst/lower.rs

Lines changed: 2 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -137,17 +137,6 @@ pub enum RelocDistance {
137137
Far,
138138
}
139139

140-
impl RelocDistance {
141-
/// Get a `RelocDistance` from a `colocated` bit in a CLIF instruction's symbol reference.
142-
pub fn from_colocated(colocated: bool) -> RelocDistance {
143-
if colocated {
144-
RelocDistance::Near
145-
} else {
146-
RelocDistance::Far
147-
}
148-
}
149-
}
150-
151140
fn alloc_vreg(
152141
value_regs: &mut SecondaryMap<Value, Reg>,
153142
regclass: RegClass,
@@ -681,7 +670,7 @@ impl<'func, I: VCodeInst> LowerCtx for Lower<'func, I> {
681670
&InstructionData::Call { func_ref, .. }
682671
| &InstructionData::FuncAddr { func_ref, .. } => {
683672
let funcdata = &self.f.dfg.ext_funcs[func_ref];
684-
let dist = RelocDistance::from_colocated(funcdata.colocated);
673+
let dist = funcdata.reloc_distance();
685674
Some((&funcdata.name, dist))
686675
}
687676
_ => None,
@@ -708,11 +697,10 @@ impl<'func, I: VCodeInst> LowerCtx for Lower<'func, I> {
708697
&GlobalValueData::Symbol {
709698
ref name,
710699
ref offset,
711-
colocated,
712700
..
713701
} => {
714702
let offset = offset.bits();
715-
let dist = RelocDistance::from_colocated(colocated);
703+
let dist = gvdata.maybe_reloc_distance().unwrap();
716704
Some((name, dist, offset))
717705
}
718706
_ => None,

0 commit comments

Comments
 (0)