Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mention Register Size in #[warn(asm_sub_register)] #121940

Merged
merged 4 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions compiler/rustc_hir_analysis/src/check/intrinsicck.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ use rustc_session::lint;
use rustc_span::def_id::LocalDefId;
use rustc_span::Symbol;
use rustc_target::abi::FieldIdx;
use rustc_target::asm::{InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType};
use rustc_target::asm::{
InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType, ModifierInfo,
};

pub struct InlineAsmCtxt<'a, 'tcx> {
tcx: TyCtxt<'tcx>,
Expand Down Expand Up @@ -253,8 +255,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
}

// Check whether a modifier is suggested for using this type.
if let Some((suggested_modifier, suggested_result)) =
reg_class.suggest_modifier(asm_arch, asm_ty)
if let Some(ModifierInfo {
modifier: suggested_modifier,
result: suggested_result,
size: suggested_size,
}) = reg_class.suggest_modifier(asm_arch, asm_ty)
{
// Search for any use of this operand without a modifier and emit
// the suggestion for them.
Expand All @@ -268,8 +273,11 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
}
}
if !spans.is_empty() {
let (default_modifier, default_result) =
reg_class.default_modifier(asm_arch).unwrap();
let ModifierInfo {
modifier: default_modifier,
result: default_result,
size: default_size,
} = reg_class.default_modifier(asm_arch).unwrap();
self.tcx.node_span_lint(
lint::builtin::ASM_SUB_REGISTER,
expr.hir_id,
Expand All @@ -278,10 +286,10 @@ impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
|lint| {
lint.span_label(expr.span, "for this argument");
lint.help(format!(
"use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}`",
"use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}` (for {suggested_size}-bit values)",
));
lint.help(format!(
"or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}`",
"or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}` (for {default_size}-bit values)",
));
},
);
Expand Down
26 changes: 11 additions & 15 deletions compiler/rustc_target/src/asm/aarch64.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use crate::spec::{RelocModel, Target};
use rustc_data_structures::fx::FxIndexSet;
use rustc_macros::HashStable_Generic;
Expand Down Expand Up @@ -27,32 +27,28 @@ impl AArch64InlineAsmRegClass {
None
}

pub fn suggest_modifier(
self,
_arch: InlineAsmArch,
ty: InlineAsmType,
) -> Option<(char, &'static str)> {
pub fn suggest_modifier(self, _arch: InlineAsmArch, ty: InlineAsmType) -> Option<ModifierInfo> {
match self {
Self::reg => match ty.size().bits() {
64 => None,
_ => Some(('w', "w0")),
_ => Some(('w', "w0", 32).into()),
},
fmease marked this conversation as resolved.
Show resolved Hide resolved
Self::vreg | Self::vreg_low16 => match ty.size().bits() {
8 => Some(('b', "b0")),
16 => Some(('h', "h0")),
32 => Some(('s', "s0")),
64 => Some(('d', "d0")),
128 => Some(('q', "q0")),
8 => Some(('b', "b0", 8).into()),
16 => Some(('h', "h0", 16).into()),
32 => Some(('s', "s0", 32).into()),
64 => Some(('d', "d0", 64).into()),
128 => Some(('q', "q0", 128).into()),
_ => None,
},
Self::preg => None,
}
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
match self {
Self::reg => Some(('x', "x0")),
Self::vreg | Self::vreg_low16 => Some(('v', "v0")),
Self::reg => Some(('x', "x0", 64).into()),
Self::vreg | Self::vreg_low16 => Some(('v', "v0", 128).into()),
Self::preg => None,
}
}
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/arm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use crate::spec::{RelocModel, Target};
use rustc_data_structures::fx::FxIndexSet;
use rustc_macros::HashStable_Generic;
Expand Down Expand Up @@ -35,11 +35,11 @@ impl ArmInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/avr.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand Down Expand Up @@ -29,11 +29,11 @@ impl AvrInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/bpf.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -23,11 +23,11 @@ impl BpfInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/csky.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -23,11 +23,11 @@ impl CSKYInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/hexagon.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -22,11 +22,11 @@ impl HexagonInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/loongarch.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -23,11 +23,11 @@ impl LoongArchInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/m68k.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -24,11 +24,11 @@ impl M68kInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/mips.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -23,11 +23,11 @@ impl MipsInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
20 changes: 14 additions & 6 deletions compiler/rustc_target/src/asm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,18 @@ use rustc_span::Symbol;
use std::fmt;
use std::str::FromStr;

pub struct ModifierInfo {
pub modifier: char,
pub result: &'static str,
pub size: u64,
}

impl From<(char, &'static str, u64)> for ModifierInfo {
fn from((modifier, result, size): (char, &'static str, u64)) -> Self {
Self { modifier, result, size }
}
}

macro_rules! def_reg_class {
($arch:ident $arch_regclass:ident {
$(
Expand Down Expand Up @@ -512,11 +524,7 @@ impl InlineAsmRegClass {
/// Such suggestions are useful if a type smaller than the full register
/// size is used and a modifier can be used to point to the subregister of
/// the correct size.
pub fn suggest_modifier(
self,
arch: InlineAsmArch,
ty: InlineAsmType,
) -> Option<(char, &'static str)> {
pub fn suggest_modifier(self, arch: InlineAsmArch, ty: InlineAsmType) -> Option<ModifierInfo> {
match self {
Self::X86(r) => r.suggest_modifier(arch, ty),
Self::Arm(r) => r.suggest_modifier(arch, ty),
Expand Down Expand Up @@ -545,7 +553,7 @@ impl InlineAsmRegClass {
/// This is only needed when the register class can suggest a modifier, so
/// that the user can be shown how to get the default behavior without a
/// warning.
pub fn default_modifier(self, arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, arch: InlineAsmArch) -> Option<ModifierInfo> {
match self {
Self::X86(r) => r.default_modifier(arch),
Self::Arm(r) => r.default_modifier(arch),
Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/msp430.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand All @@ -22,11 +22,11 @@ impl Msp430InlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/nvptx.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;

Expand All @@ -23,11 +23,11 @@ impl NvptxInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/powerpc.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use rustc_macros::HashStable_Generic;
use rustc_span::Symbol;
use std::fmt;
Expand Down Expand Up @@ -26,11 +26,11 @@ impl PowerPCInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
6 changes: 3 additions & 3 deletions compiler/rustc_target/src/asm/riscv.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use super::{InlineAsmArch, InlineAsmType};
use super::{InlineAsmArch, InlineAsmType, ModifierInfo};
use crate::spec::{RelocModel, Target};
use rustc_data_structures::fx::FxIndexSet;
use rustc_macros::HashStable_Generic;
Expand Down Expand Up @@ -26,11 +26,11 @@ impl RiscVInlineAsmRegClass {
self,
_arch: InlineAsmArch,
_ty: InlineAsmType,
) -> Option<(char, &'static str)> {
) -> Option<ModifierInfo> {
None
}

pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<(char, &'static str)> {
pub fn default_modifier(self, _arch: InlineAsmArch) -> Option<ModifierInfo> {
None
}

Expand Down
Loading
Loading