Skip to content

Commit 0b43964

Browse files
committed
Share part of the global_asm!() implementation between cg_ssa and cg_clif
1 parent 3e61e7f commit 0b43964

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
@@ -560,7 +560,10 @@ fn codegen_cgu_content(
560560
}
561561
}
562562
MonoItem::GlobalAsm(item_id) => {
563-
crate::global_asm::codegen_global_asm_item(tcx, &mut cx.global_asm, item_id);
563+
rustc_codegen_ssa::base::codegen_global_asm(
564+
&mut GlobalAsmContext { tcx, global_asm: &mut cx.global_asm },
565+
item_id,
566+
);
564567
}
565568
}
566569
}

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::{
1513
FnAbiError, FnAbiOfHelpers, FnAbiRequest, HasTyCtxt, HasTypingEnv, LayoutError, LayoutOfHelpers,
@@ -91,66 +89,6 @@ impl<'tcx> HasTypingEnv<'tcx> for GlobalAsmContext<'_, 'tcx> {
9189
}
9290
}
9391

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