Skip to content

Commit 1a59b3e

Browse files
authored
AArch64: port tls_value to ISLE. (#4821)
1 parent b033aba commit 1a59b3e

7 files changed

Lines changed: 26 additions & 40 deletions

File tree

cranelift/codegen/src/isa/aarch64/inst.isle

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -825,7 +825,8 @@
825825

826826
;; A call to the `ElfTlsGetAddr` libcall. Returns address of TLS symbol in x0.
827827
(ElfTlsGetAddr
828-
(symbol ExternalName))
828+
(symbol ExternalName)
829+
(rd WritableReg))
829830

830831
;; An unwind pseudo-instruction.
831832
(Unwind
@@ -3068,3 +3069,12 @@
30683069
(if (ty_vec64 ty))
30693070
(let ((src Reg (mov_from_vec src 0 (ScalarSize.Size64))))
30703071
(cmp_imm (OperandSize.Size64) src (u8_into_imm12 0))))
3072+
3073+
;;;; TLS Values ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
3074+
3075+
;; Helper for emitting ElfTlsGetAddr.
3076+
(decl elf_tls_get_addr (ExternalName) Reg)
3077+
(rule (elf_tls_get_addr name)
3078+
(let ((dst WritableReg (temp_writable_reg $I64))
3079+
(_ Unit (emit (MInst.ElfTlsGetAddr name dst))))
3080+
dst))

cranelift/codegen/src/isa/aarch64/inst/emit.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3191,7 +3191,10 @@ impl MachInstEmit for Inst {
31913191
}
31923192
}
31933193

3194-
&Inst::ElfTlsGetAddr { ref symbol } => {
3194+
&Inst::ElfTlsGetAddr { ref symbol, rd } => {
3195+
let rd = allocs.next_writable(rd);
3196+
assert_eq!(xreg(0), rd.to_reg());
3197+
31953198
// This is the instruction sequence that GCC emits for ELF GD TLS Relocations in aarch64
31963199
// See: https://gcc.godbolt.org/z/KhMh5Gvra
31973200

cranelift/codegen/src/isa/aarch64/inst/mod.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1044,8 +1044,8 @@ fn aarch64_get_operands<F: Fn(VReg) -> VReg>(inst: &Inst, collector: &mut Operan
10441044
}
10451045
&Inst::VirtualSPOffsetAdj { .. } => {}
10461046

1047-
&Inst::ElfTlsGetAddr { .. } => {
1048-
collector.reg_def(Writable::from_reg(regs::xreg(0)));
1047+
&Inst::ElfTlsGetAddr { rd, .. } => {
1048+
collector.reg_fixed_def(rd, regs::xreg(0));
10491049
let mut clobbers = AArch64MachineDeps::get_regs_clobbered_by_call(CallConv::SystemV);
10501050
clobbers.remove(regs::xreg_preg(0));
10511051
collector.reg_clobbers(clobbers);
@@ -2710,8 +2710,9 @@ impl Inst {
27102710
}
27112711
&Inst::EmitIsland { needed_space } => format!("emit_island {}", needed_space),
27122712

2713-
&Inst::ElfTlsGetAddr { ref symbol } => {
2714-
format!("x0 = elf_tls_get_addr {}", symbol.display(None))
2713+
&Inst::ElfTlsGetAddr { ref symbol, rd } => {
2714+
let rd = pretty_print_reg(rd.to_reg(), allocs);
2715+
format!("elf_tls_get_addr {}, {}", rd, symbol.display(None))
27152716
}
27162717
&Inst::Unwind { ref inst } => {
27172718
format!("unwind {:?}", inst)

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2364,7 +2364,9 @@
23642364

23652365
;;; Rules for `tls_value` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23662366

2367-
;; TODO.
2367+
(rule (lower (tls_value (symbol_value_data name _ _)))
2368+
(if (tls_model_is_elf_gd))
2369+
(elf_tls_get_addr name))
23682370

23692371
;;; Rules for `fcvt_low_from_sint` ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
23702372

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

Lines changed: 2 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use crate::isa::aarch64::inst::*;
99
use crate::isa::aarch64::settings as aarch64_settings;
1010
use crate::machinst::lower::*;
1111
use crate::machinst::*;
12-
use crate::settings::{Flags, TlsModel};
12+
use crate::settings::Flags;
1313
use crate::{CodegenError, CodegenResult};
1414
use alloc::boxed::Box;
1515
use alloc::vec::Vec;
@@ -459,23 +459,7 @@ pub(crate) fn lower_insn_to_regs(
459459
implemented_in_isle(ctx)
460460
}
461461

462-
Opcode::TlsValue => match flags.tls_model() {
463-
TlsModel::ElfGd => {
464-
let dst = get_output_reg(ctx, outputs[0]).only_reg().unwrap();
465-
let (name, _, _) = ctx.symbol_value(insn).unwrap();
466-
let symbol = name.clone();
467-
ctx.emit(Inst::ElfTlsGetAddr { symbol });
468-
469-
let x0 = xreg(0);
470-
ctx.emit(Inst::gen_move(dst, x0, I64));
471-
}
472-
_ => {
473-
return Err(CodegenError::Unsupported(format!(
474-
"Unimplemented TLS model in AArch64 backend: {:?}",
475-
flags.tls_model()
476-
)));
477-
}
478-
},
462+
Opcode::TlsValue => implemented_in_isle(ctx),
479463

480464
Opcode::SqmulRoundSat => implemented_in_isle(ctx),
481465

cranelift/codegen/src/machinst/lower.rs

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1026,20 +1026,6 @@ impl<'func, I: VCodeInst> Lower<'func, I> {
10261026
&self.f.dfg[ir_inst]
10271027
}
10281028

1029-
/// Get the symbol name, relocation distance estimate, and offset for a
1030-
/// symbol_value instruction.
1031-
pub fn symbol_value<'b>(
1032-
&'b self,
1033-
ir_inst: Inst,
1034-
) -> Option<(&'b ExternalName, RelocDistance, i64)> {
1035-
match &self.f.dfg[ir_inst] {
1036-
&InstructionData::UnaryGlobalValue { global_value, .. } => {
1037-
self.symbol_value_data(global_value)
1038-
}
1039-
_ => None,
1040-
}
1041-
}
1042-
10431029
/// Likewise, but starting with a GlobalValue identifier.
10441030
pub fn symbol_value_data<'b>(
10451031
&'b self,

cranelift/filetests/filetests/isa/aarch64/tls-elf-gd.clif

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ block0(v0: i32):
1919
; stp d8, d9, [sp, #-16]!
2020
; block0:
2121
; mov x25, x0
22-
; x0 = elf_tls_get_addr userextname0
22+
; elf_tls_get_addr x0, userextname0
2323
; mov x1, x0
2424
; mov x0, x25
2525
; ldp d8, d9, [sp], #16

0 commit comments

Comments
 (0)