Skip to content

Commit

Permalink
use tcx.codegen_fn_attrs to get attribute info
Browse files Browse the repository at this point in the history
  • Loading branch information
folkertdev committed Jul 27, 2024
1 parent 678dfa6 commit 297f7d9
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 15 deletions.
5 changes: 1 addition & 4 deletions compiler/rustc_codegen_ssa/src/codegen_attrs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,10 +109,7 @@ fn codegen_fn_attrs(tcx: TyCtxt<'_>, did: LocalDefId) -> CodegenFnAttrs {
sym::rustc_allocator_zeroed => {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::ALLOCATOR_ZEROED
}
sym::naked => {
// this attribute is ignored during codegen, because a function marked as naked is
// turned into a global asm block.
}
sym::naked => codegen_fn_attrs.flags |= CodegenFnAttrFlags::NAKED,
sym::no_mangle => {
if tcx.opt_item_name(did.to_def_id()).is_some() {
codegen_fn_attrs.flags |= CodegenFnAttrFlags::NO_MANGLE
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_codegen_ssa/src/mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ pub fn codegen_mir<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
let fn_abi = cx.fn_abi_of_instance(instance, ty::List::empty());
debug!("fn_abi: {:?}", fn_abi);

if cx.tcx().has_attr(instance.def.def_id(), rustc_span::sym::naked) {
if cx.tcx().codegen_fn_attrs(instance.def_id()).flags.contains(CodegenFnAttrFlags::NAKED) {
let cached_llbbs = IndexVec::new();

let fx: FunctionCx<'_, '_, Bx> = FunctionCx {
Expand Down
15 changes: 5 additions & 10 deletions compiler/rustc_codegen_ssa/src/mir/naked_asm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use rustc_middle::mir::InlineAsmOperand;
use rustc_middle::ty;
use rustc_middle::ty::layout::{HasTyCtxt, LayoutOf};
use rustc_middle::ty::{Instance, TyCtxt};

use rustc_span::sym;
use rustc_target::asm::InlineAsmArch;

impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
Expand Down Expand Up @@ -133,12 +131,9 @@ fn prefix_and_suffix<'tcx>(

let asm_name = format!("{}{}", if mangle { "_" } else { "" }, tcx.symbol_name(instance).name);

let opt_section = tcx
.get_attr(instance.def.def_id(), sym::link_section)
.and_then(|attr| attr.value_str())
.map(|attr| attr.as_str().to_string());

let attrs = tcx.codegen_fn_attrs(instance.def_id());
let link_section = attrs.link_section.map(|symbol| symbol.as_str().to_string());

let (arch_prefix, arch_suffix) = if is_arm {
(
match attrs.instruction_set {
Expand All @@ -162,7 +157,7 @@ fn prefix_and_suffix<'tcx>(
let mut end = String::new();
match AsmBinaryFormat::from_target(&tcx.sess.target) {
AsmBinaryFormat::Elf => {
let section = opt_section.unwrap_or(format!(".text.{asm_name}"));
let section = link_section.unwrap_or(format!(".text.{asm_name}"));

let progbits = match is_arm {
true => "%progbits",
Expand Down Expand Up @@ -199,7 +194,7 @@ fn prefix_and_suffix<'tcx>(
}
}
AsmBinaryFormat::Macho => {
let section = opt_section.unwrap_or("__TEXT,__text".to_string());
let section = link_section.unwrap_or("__TEXT,__text".to_string());
writeln!(begin, ".pushsection {},regular,pure_instructions", section).unwrap();
writeln!(begin, ".balign 4").unwrap();
if let Some(linkage) = linkage_directive(item_data.linkage) {
Expand All @@ -220,7 +215,7 @@ fn prefix_and_suffix<'tcx>(
}
}
AsmBinaryFormat::Coff => {
let section = opt_section.unwrap_or(format!(".text.{asm_name}"));
let section = link_section.unwrap_or(format!(".text.{asm_name}"));
writeln!(begin, ".pushsection {},\"xr\"", section).unwrap();
writeln!(begin, ".balign 4").unwrap();
if let Some(linkage) = linkage_directive(item_data.linkage) {
Expand Down

0 comments on commit 297f7d9

Please sign in to comment.