Skip to content

Commit 42ae211

Browse files
committed
Share part of the global_asm!() implementation between cg_ssa and cg_clif
1 parent 0dd4292 commit 42ae211

File tree

4 files changed

+73
-135
lines changed

4 files changed

+73
-135
lines changed

compiler/rustc_codegen_cranelift/src/driver/aot.rs

+4-1
Original file line numberDiff line numberDiff line change
@@ -553,7 +553,10 @@ fn codegen_cgu_content(
553553
}
554554
}
555555
MonoItem::GlobalAsm(item_id) => {
556-
crate::global_asm::codegen_global_asm_item(tcx, &mut cx.global_asm, item_id);
556+
rustc_codegen_ssa::base::codegen_global_asm(
557+
&mut GlobalAsmContext { tcx, global_asm: &mut cx.global_asm },
558+
item_id,
559+
);
557560
}
558561
}
559562
}

compiler/rustc_codegen_cranelift/src/global_asm.rs

-62
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ use std::sync::Arc;
88

99
use rustc_ast::{InlineAsmOptions, InlineAsmTemplatePiece};
1010
use rustc_codegen_ssa::traits::{AsmCodegenMethods, GlobalAsmOperandRef};
11-
use rustc_hir::{InlineAsmOperand, ItemId};
12-
use rustc_middle::mir::interpret::ErrorHandled;
1311
use rustc_middle::ty::TyCtxt;
1412
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutError, LayoutOfHelpers};
1513
use rustc_session::config::{OutputFilenames, OutputType};
@@ -77,66 +75,6 @@ impl<'tcx> HasTypingEnv<'tcx> for GlobalAsmContext<'_, 'tcx> {
7775
}
7876
}
7977

80-
pub(crate) fn codegen_global_asm_item(tcx: TyCtxt<'_>, global_asm: &mut String, item_id: ItemId) {
81-
let item = tcx.hir().item(item_id);
82-
let rustc_hir::ItemKind::GlobalAsm(asm) = item.kind else {
83-
bug!("Expected GlobalAsm found {:?}", item);
84-
};
85-
86-
// Adapted from rustc_codegen_ssa::mono_items::MonoItem::define
87-
let operands: Vec<_> = asm
88-
.operands
89-
.iter()
90-
.map(|(op, op_sp)| match *op {
91-
InlineAsmOperand::Const { ref anon_const } => {
92-
match tcx.const_eval_poly(anon_const.def_id.to_def_id()) {
93-
Ok(const_value) => {
94-
let ty = tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
95-
let string = rustc_codegen_ssa::common::asm_const_to_str(
96-
tcx,
97-
*op_sp,
98-
const_value,
99-
FullyMonomorphizedLayoutCx(tcx).layout_of(ty),
100-
);
101-
GlobalAsmOperandRef::Const { string }
102-
}
103-
Err(ErrorHandled::Reported { .. }) => {
104-
// An error has already been reported and
105-
// compilation is guaranteed to fail if execution
106-
// hits this path. So an empty string instead of
107-
// a stringified constant value will suffice.
108-
GlobalAsmOperandRef::Const { string: String::new() }
109-
}
110-
Err(ErrorHandled::TooGeneric(_)) => {
111-
span_bug!(*op_sp, "asm const cannot be resolved; too generic")
112-
}
113-
}
114-
}
115-
InlineAsmOperand::SymFn { ref anon_const } => {
116-
let ty = tcx.typeck_body(anon_const.body).node_type(anon_const.hir_id);
117-
let instance = match ty.kind() {
118-
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
119-
_ => span_bug!(*op_sp, "asm sym is not a function"),
120-
};
121-
122-
GlobalAsmOperandRef::SymFn { instance }
123-
}
124-
InlineAsmOperand::SymStatic { path: _, def_id } => {
125-
GlobalAsmOperandRef::SymStatic { def_id }
126-
}
127-
InlineAsmOperand::In { .. }
128-
| InlineAsmOperand::Out { .. }
129-
| InlineAsmOperand::InOut { .. }
130-
| InlineAsmOperand::SplitInOut { .. }
131-
| InlineAsmOperand::Label { .. } => {
132-
span_bug!(*op_sp, "invalid operand type for global_asm!")
133-
}
134-
})
135-
.collect();
136-
137-
codegen_global_asm_inner(tcx, global_asm, asm.template, &operands, asm.options, asm.line_spans);
138-
}
139-
14078
fn codegen_global_asm_inner<'tcx>(
14179
tcx: TyCtxt<'tcx>,
14280
global_asm: &mut String,

compiler/rustc_codegen_ssa/src/base.rs

+66-1
Original file line numberDiff line numberDiff line change
@@ -10,19 +10,21 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
1010
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
1111
use rustc_data_structures::sync::{Lrc, par_map};
1212
use rustc_data_structures::unord::UnordMap;
13+
use rustc_hir::ItemId;
1314
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1415
use rustc_hir::lang_items::LangItem;
1516
use rustc_metadata::EncodedMetadata;
16-
use rustc_middle::bug;
1717
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1818
use rustc_middle::middle::debugger_visualizer::{DebuggerVisualizerFile, DebuggerVisualizerType};
1919
use rustc_middle::middle::exported_symbols::SymbolExportKind;
2020
use rustc_middle::middle::{exported_symbols, lang_items};
2121
use rustc_middle::mir::BinOp;
22+
use rustc_middle::mir::interpret::ErrorHandled;
2223
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
2324
use rustc_middle::query::Providers;
2425
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
2526
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
27+
use rustc_middle::{bug, span_bug};
2628
use rustc_session::Session;
2729
use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
2830
use rustc_span::symbol::sym;
@@ -420,6 +422,69 @@ pub(crate) fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
420422
mir::codegen_mir::<Bx>(cx, instance);
421423
}
422424

425+
pub fn codegen_global_asm<'tcx, Cx>(cx: &mut Cx, item_id: ItemId)
426+
where
427+
Cx: LayoutOf<'tcx, LayoutOfResult = TyAndLayout<'tcx>> + AsmCodegenMethods<'tcx>,
428+
{
429+
let item = cx.tcx().hir().item(item_id);
430+
if let rustc_hir::ItemKind::GlobalAsm(asm) = item.kind {
431+
let operands: Vec<_> = asm
432+
.operands
433+
.iter()
434+
.map(|(op, op_sp)| match *op {
435+
rustc_hir::InlineAsmOperand::Const { ref anon_const } => {
436+
match cx.tcx().const_eval_poly(anon_const.def_id.to_def_id()) {
437+
Ok(const_value) => {
438+
let ty =
439+
cx.tcx().typeck_body(anon_const.body).node_type(anon_const.hir_id);
440+
let string = common::asm_const_to_str(
441+
cx.tcx(),
442+
*op_sp,
443+
const_value,
444+
cx.layout_of(ty),
445+
);
446+
GlobalAsmOperandRef::Const { string }
447+
}
448+
Err(ErrorHandled::Reported { .. }) => {
449+
// An error has already been reported and
450+
// compilation is guaranteed to fail if execution
451+
// hits this path. So an empty string instead of
452+
// a stringified constant value will suffice.
453+
GlobalAsmOperandRef::Const { string: String::new() }
454+
}
455+
Err(ErrorHandled::TooGeneric(_)) => {
456+
span_bug!(*op_sp, "asm const cannot be resolved; too generic")
457+
}
458+
}
459+
}
460+
rustc_hir::InlineAsmOperand::SymFn { ref anon_const } => {
461+
let ty = cx.tcx().typeck_body(anon_const.body).node_type(anon_const.hir_id);
462+
let instance = match ty.kind() {
463+
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
464+
_ => span_bug!(*op_sp, "asm sym is not a function"),
465+
};
466+
467+
GlobalAsmOperandRef::SymFn { instance }
468+
}
469+
rustc_hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
470+
GlobalAsmOperandRef::SymStatic { def_id }
471+
}
472+
rustc_hir::InlineAsmOperand::In { .. }
473+
| rustc_hir::InlineAsmOperand::Out { .. }
474+
| rustc_hir::InlineAsmOperand::InOut { .. }
475+
| rustc_hir::InlineAsmOperand::SplitInOut { .. }
476+
| rustc_hir::InlineAsmOperand::Label { .. } => {
477+
span_bug!(*op_sp, "invalid operand type for global_asm!")
478+
}
479+
})
480+
.collect();
481+
482+
cx.codegen_global_asm(asm.template, &operands, asm.options, asm.line_spans);
483+
} else {
484+
span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
485+
}
486+
}
487+
423488
/// Creates the `main` function which will initialize the rust runtime and call
424489
/// users main function.
425490
pub fn maybe_create_entry_wrapper<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(

compiler/rustc_codegen_ssa/src/mono_item.rs

+3-71
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
use rustc_hir as hir;
21
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
3-
use rustc_middle::mir::interpret::ErrorHandled;
42
use rustc_middle::mir::mono::{Linkage, MonoItem, MonoItemData, Visibility};
5-
use rustc_middle::ty::Instance;
6-
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
7-
use rustc_middle::{span_bug, ty};
3+
use rustc_middle::ty::layout::HasTyCtxt;
84
use tracing::debug;
95

6+
use crate::base;
107
use crate::mir::naked_asm;
118
use crate::traits::*;
12-
use crate::{base, common};
139

1410
pub trait MonoItemExt<'a, 'tcx> {
1511
fn define<Bx: BuilderMethods<'a, 'tcx>>(
@@ -44,71 +40,7 @@ impl<'a, 'tcx: 'a> MonoItemExt<'a, 'tcx> for MonoItem<'tcx> {
4440
cx.codegen_static(def_id);
4541
}
4642
MonoItem::GlobalAsm(item_id) => {
47-
let item = cx.tcx().hir().item(item_id);
48-
if let hir::ItemKind::GlobalAsm(asm) = item.kind {
49-
let operands: Vec<_> = asm
50-
.operands
51-
.iter()
52-
.map(|(op, op_sp)| match *op {
53-
hir::InlineAsmOperand::Const { ref anon_const } => {
54-
match cx.tcx().const_eval_poly(anon_const.def_id.to_def_id()) {
55-
Ok(const_value) => {
56-
let ty = cx
57-
.tcx()
58-
.typeck_body(anon_const.body)
59-
.node_type(anon_const.hir_id);
60-
let string = common::asm_const_to_str(
61-
cx.tcx(),
62-
*op_sp,
63-
const_value,
64-
cx.layout_of(ty),
65-
);
66-
GlobalAsmOperandRef::Const { string }
67-
}
68-
Err(ErrorHandled::Reported { .. }) => {
69-
// An error has already been reported and
70-
// compilation is guaranteed to fail if execution
71-
// hits this path. So an empty string instead of
72-
// a stringified constant value will suffice.
73-
GlobalAsmOperandRef::Const { string: String::new() }
74-
}
75-
Err(ErrorHandled::TooGeneric(_)) => {
76-
span_bug!(
77-
*op_sp,
78-
"asm const cannot be resolved; too generic"
79-
)
80-
}
81-
}
82-
}
83-
hir::InlineAsmOperand::SymFn { ref anon_const } => {
84-
let ty = cx
85-
.tcx()
86-
.typeck_body(anon_const.body)
87-
.node_type(anon_const.hir_id);
88-
let instance = match ty.kind() {
89-
&ty::FnDef(def_id, args) => Instance::new(def_id, args),
90-
_ => span_bug!(*op_sp, "asm sym is not a function"),
91-
};
92-
93-
GlobalAsmOperandRef::SymFn { instance }
94-
}
95-
hir::InlineAsmOperand::SymStatic { path: _, def_id } => {
96-
GlobalAsmOperandRef::SymStatic { def_id }
97-
}
98-
hir::InlineAsmOperand::In { .. }
99-
| hir::InlineAsmOperand::Out { .. }
100-
| hir::InlineAsmOperand::InOut { .. }
101-
| hir::InlineAsmOperand::SplitInOut { .. }
102-
| hir::InlineAsmOperand::Label { .. } => {
103-
span_bug!(*op_sp, "invalid operand type for global_asm!")
104-
}
105-
})
106-
.collect();
107-
108-
cx.codegen_global_asm(asm.template, &operands, asm.options, asm.line_spans);
109-
} else {
110-
span_bug!(item.span, "Mismatch between hir::Item type and MonoItem type")
111-
}
43+
base::codegen_global_asm(cx, item_id);
11244
}
11345
MonoItem::Fn(instance) => {
11446
if cx

0 commit comments

Comments
 (0)