Skip to content

Commit 1358f53

Browse files
committed
Share part of the global_asm!() implementation between cg_ssa and cg_clif
1 parent f115335 commit 1358f53

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
@@ -9,19 +9,21 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexSet};
99
use rustc_data_structures::profiling::{get_resident_set_size, print_time_passes_entry};
1010
use rustc_data_structures::sync::{Lrc, par_map};
1111
use rustc_data_structures::unord::UnordMap;
12+
use rustc_hir::ItemId;
1213
use rustc_hir::def_id::{DefId, LOCAL_CRATE};
1314
use rustc_hir::lang_items::LangItem;
1415
use rustc_metadata::EncodedMetadata;
15-
use rustc_middle::bug;
1616
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
1717
use rustc_middle::middle::debugger_visualizer::{DebuggerVisualizerFile, DebuggerVisualizerType};
1818
use rustc_middle::middle::exported_symbols::SymbolExportKind;
1919
use rustc_middle::middle::{exported_symbols, lang_items};
2020
use rustc_middle::mir::BinOp;
21+
use rustc_middle::mir::interpret::ErrorHandled;
2122
use rustc_middle::mir::mono::{CodegenUnit, CodegenUnitNameBuilder, MonoItem};
2223
use rustc_middle::query::Providers;
2324
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
2425
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
26+
use rustc_middle::{bug, span_bug};
2527
use rustc_session::Session;
2628
use rustc_session::config::{self, CrateType, EntryFnType, OptLevel, OutputType};
2729
use rustc_span::{DUMMY_SP, Symbol, sym};
@@ -419,6 +421,69 @@ pub(crate) fn codegen_instance<'a, 'tcx: 'a, Bx: BuilderMethods<'a, 'tcx>>(
419421
mir::codegen_mir::<Bx>(cx, instance);
420422
}
421423

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