From 8717cbaa84ce740c987c055da765b83c46401a51 Mon Sep 17 00:00:00 2001 From: mejrs <59372212+mejrs@users.noreply.github.com> Date: Mon, 31 Aug 2026 20:48:05 +0200 Subject: [PATCH] Deduplicate `InstrumentFnAttr` --- Cargo.lock | 2 ++ compiler/rustc_attr_ir/src/data_structures.rs | 7 ++++-- compiler/rustc_codegen_llvm/Cargo.toml | 1 + compiler/rustc_codegen_llvm/src/attributes.rs | 20 ++++++++-------- compiler/rustc_codegen_ssa/Cargo.toml | 1 + .../rustc_codegen_ssa/src/codegen_attrs.rs | 15 +++++------- .../src/middle/codegen_fn_attrs.rs | 24 ++++--------------- 7 files changed, 30 insertions(+), 40 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2c9dc442663f8..2880a1c137192 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3798,6 +3798,7 @@ dependencies = [ "rustc-demangle", "rustc_abi", "rustc_ast", + "rustc_attr_ir", "rustc_codegen_ssa", "rustc_crate_store", "rustc_data_structures", @@ -3837,6 +3838,7 @@ dependencies = [ "rustc_abi", "rustc_arena", "rustc_ast", + "rustc_attr_ir", "rustc_attr_parsing", "rustc_crate_store", "rustc_data_structures", diff --git a/compiler/rustc_attr_ir/src/data_structures.rs b/compiler/rustc_attr_ir/src/data_structures.rs index beb4ba8fabef7..54d083a094883 100644 --- a/compiler/rustc_attr_ir/src/data_structures.rs +++ b/compiler/rustc_attr_ir/src/data_structures.rs @@ -12,7 +12,9 @@ use rustc_ast::token::DocFragmentKind; use rustc_ast::{AttrStyle, Path, ast}; use rustc_data_structures::fx::FxIndexMap; use rustc_error_messages::{DiagArgValue, IntoDiagArg}; -use rustc_macros::{Decodable, Encodable, PrintAttribute, StableHash}; +use rustc_macros::{ + Decodable, Decodable_NoContext, Encodable, Encodable_NoContext, PrintAttribute, StableHash, +}; use rustc_span::def_id::DefId; use rustc_span::hygiene::Transparency; use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol}; @@ -108,7 +110,8 @@ pub enum InstructionSetAttr { ArmT32, } -#[derive(Copy, Clone, PartialEq, Encodable, Decodable, Debug, Eq, StableHash, PrintAttribute)] +#[derive(Copy, Clone, Debug)] +#[derive(Encodable_NoContext, Decodable_NoContext, StableHash, PrintAttribute)] pub enum InstrumentFnAttr { /// `#[instrument_fn = "on"]` On, diff --git a/compiler/rustc_codegen_llvm/Cargo.toml b/compiler/rustc_codegen_llvm/Cargo.toml index 27c4062ddd941..ce6731d4ef701 100644 --- a/compiler/rustc_codegen_llvm/Cargo.toml +++ b/compiler/rustc_codegen_llvm/Cargo.toml @@ -20,6 +20,7 @@ object = { version = "0.39.1", default-features = false, features = ["std", "rea rustc-demangle = "0.1.28" rustc_abi = { path = "../rustc_abi" } rustc_ast = { path = "../rustc_ast" } +rustc_attr_ir = { path = "../rustc_attr_ir" } rustc_codegen_ssa = { path = "../rustc_codegen_ssa" } rustc_crate_store = { path = "../rustc_crate_store" } rustc_data_structures = { path = "../rustc_data_structures" } diff --git a/compiler/rustc_codegen_llvm/src/attributes.rs b/compiler/rustc_codegen_llvm/src/attributes.rs index 9415c2ecb9d10..b1c10a85b4dff 100644 --- a/compiler/rustc_codegen_llvm/src/attributes.rs +++ b/compiler/rustc_codegen_llvm/src/attributes.rs @@ -1,10 +1,10 @@ //! Set and unset common attributes on LLVM values. -use rustc_hir::attrs::{InlineAttr, InstructionSetAttr, OptimizeAttr, RtsanSetting}; +use rustc_attr_ir::{ + InlineAttr, InstructionSetAttr, InstrumentFnAttr, OptimizeAttr, RtsanSetting, find_attr, +}; use rustc_hir::def_id::DefId; -use rustc_hir::find_attr; use rustc_middle::middle::codegen_fn_attrs::{ - CodegenFnAttrFlags, CodegenFnAttrs, InstrumentFnAttr, PatchableFunctionEntry, SanitizerFnAttrs, - TargetFeature, + CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs, TargetFeature, }; use rustc_middle::ty::{self, Instance, TyCtxt}; use rustc_session::config::{ @@ -236,7 +236,7 @@ fn function_return_attr<'ll>(cx: &SimpleCx<'ll>, sess: &Session) -> Option<&'ll fn instrument_function_attr<'ll>( cx: &SimpleCx<'ll>, sess: &Session, - instrument_fn: InstrumentFnAttr, + instrument_fn: Option, ) -> SmallVec<[&'ll Attribute; 4]> { let mut attrs = SmallVec::new(); if sess.opts.unstable_opts.instrument_mcount != InstrumentMcount::Disabled { @@ -244,8 +244,8 @@ fn instrument_function_attr<'ll>( // `post-inline-ee-instrument` LLVM pass. let instrument_entry = match instrument_fn { - InstrumentFnAttr::Default | InstrumentFnAttr::On => true, - InstrumentFnAttr::Off => false, + Some(InstrumentFnAttr::On) | None => true, + Some(InstrumentFnAttr::Off) => false, }; if instrument_entry { @@ -290,13 +290,13 @@ fn instrument_function_attr<'ll>( // always and never may be overridden by the #[instrument_fn = ...] attribute. match instrument_fn { - InstrumentFnAttr::Default => {} - InstrumentFnAttr::On => { + Some(InstrumentFnAttr::On) => { always = true; } - InstrumentFnAttr::Off => { + Some(InstrumentFnAttr::Off) => { never = true; } + None => {} } if never { diff --git a/compiler/rustc_codegen_ssa/Cargo.toml b/compiler/rustc_codegen_ssa/Cargo.toml index a1306143a680a..55305815b05f0 100644 --- a/compiler/rustc_codegen_ssa/Cargo.toml +++ b/compiler/rustc_codegen_ssa/Cargo.toml @@ -15,6 +15,7 @@ regex = "1.4" rustc_abi = { path = "../rustc_abi" } rustc_arena = { path = "../rustc_arena" } rustc_ast = { path = "../rustc_ast" } +rustc_attr_ir = { path = "../rustc_attr_ir" } rustc_attr_parsing = { path = "../rustc_attr_parsing" } rustc_crate_store = { path = "../rustc_crate_store" } rustc_data_structures = { path = "../rustc_data_structures" } diff --git a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs index aae300d2f9ed5..2d1e1decce82c 100644 --- a/compiler/rustc_codegen_ssa/src/codegen_attrs.rs +++ b/compiler/rustc_codegen_ssa/src/codegen_attrs.rs @@ -1,16 +1,16 @@ use rustc_abi::{Align, ExternAbi}; -use rustc_hir::attrs::{ - AttributeKind, EiiImplResolution, InlineAttr, InstrumentFnAttr as HirInstrumentFnAttr, Linkage, - OptimizeAttr, RtsanSetting, UsedBy, +use rustc_attr_ir::{ + Attribute, AttributeKind, EiiImplResolution, InlineAttr, Linkage, OptimizeAttr, RtsanSetting, + UsedBy, find_attr, }; +use rustc_hir as hir; use rustc_hir::def::DefKind; use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId}; -use rustc_hir::{self as hir, Attribute, find_attr}; use rustc_lint_defs::builtin::{INLINE_NO_SANITIZE, RTSAN_NONBLOCKING_ASYNC}; use rustc_macros::Diagnostic; use rustc_middle::bug; use rustc_middle::middle::codegen_fn_attrs::{ - CodegenFnAttrFlags, CodegenFnAttrs, InstrumentFnAttr, PatchableFunctionEntry, SanitizerFnAttrs, + CodegenFnAttrFlags, CodegenFnAttrs, PatchableFunctionEntry, SanitizerFnAttrs, }; use rustc_middle::mono::Visibility; use rustc_middle::query::Providers; @@ -305,10 +305,7 @@ fn process_builtin_attrs( )); } AttributeKind::InstrumentFn(instrument_fn) => { - codegen_fn_attrs.instrument_fn = match instrument_fn { - HirInstrumentFnAttr::On => InstrumentFnAttr::On, - HirInstrumentFnAttr::Off => InstrumentFnAttr::Off, - }; + codegen_fn_attrs.instrument_fn = Some(*instrument_fn); } _ => {} } diff --git a/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs b/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs index ef9043101dbbb..f15dc1001fd95 100644 --- a/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs +++ b/compiler/rustc_middle/src/middle/codegen_fn_attrs.rs @@ -1,7 +1,9 @@ use std::borrow::Cow; use rustc_abi::Align; -use rustc_hir::attrs::{InlineAttr, InstructionSetAttr, Linkage, OptimizeAttr, RtsanSetting}; +use rustc_attr_ir::{ + InlineAttr, InstructionSetAttr, InstrumentFnAttr, Linkage, OptimizeAttr, RtsanSetting, +}; use rustc_hir::def_id::DefId; use rustc_macros::{StableHash, TyDecodable, TyEncodable}; use rustc_span::Symbol; @@ -121,23 +123,7 @@ pub struct CodegenFnAttrs { /// The `#[rustc_objc_selector = "..."]` attribute. pub objc_selector: Option, /// The `#[instrument_fn]` attribute. - pub instrument_fn: InstrumentFnAttr, -} - -#[derive(Copy, Clone, TyEncodable, TyDecodable, StableHash, Debug)] -pub enum InstrumentFnAttr { - /// Always instrument function - On, - /// Never instrument function - Off, - /// Instrument based on command line options, if any. - Default, -} - -const impl Default for InstrumentFnAttr { - fn default() -> Self { - InstrumentFnAttr::Default - } + pub instrument_fn: Option, } #[derive(Copy, Clone, Debug, TyEncodable, TyDecodable, StableHash, PartialEq, Eq)] @@ -274,7 +260,7 @@ impl CodegenFnAttrs { patchable_function_entry: None, objc_class: None, objc_selector: None, - instrument_fn: InstrumentFnAttr::default(), + instrument_fn: None, } }