rustc_hir_typeck/
inline_asm.rs

1use rustc_abi::FieldIdx;
2use rustc_ast::InlineAsmTemplatePiece;
3use rustc_data_structures::fx::FxIndexSet;
4use rustc_hir::def_id::DefId;
5use rustc_hir::{self as hir, LangItem};
6use rustc_middle::bug;
7use rustc_middle::ty::{self, Article, FloatTy, IntTy, Ty, TyCtxt, TypeVisitableExt, UintTy};
8use rustc_session::lint;
9use rustc_span::def_id::LocalDefId;
10use rustc_span::{ErrorGuaranteed, Span, Symbol, sym};
11use rustc_target::asm::{
12    InlineAsmReg, InlineAsmRegClass, InlineAsmRegOrRegClass, InlineAsmType, ModifierInfo,
13};
14use rustc_trait_selection::infer::InferCtxtExt;
15
16use crate::FnCtxt;
17use crate::errors::RegisterTypeUnstable;
18
19pub(crate) struct InlineAsmCtxt<'a, 'tcx> {
20    target_features: &'tcx FxIndexSet<Symbol>,
21    fcx: &'a FnCtxt<'a, 'tcx>,
22}
23
24enum NonAsmTypeReason<'tcx> {
25    UnevaluatedSIMDArrayLength(DefId, ty::Const<'tcx>),
26    Invalid(Ty<'tcx>),
27    InvalidElement(DefId, Ty<'tcx>),
28    NotSizedPtr(Ty<'tcx>),
29    EmptySIMDArray(Ty<'tcx>),
30    Tainted(ErrorGuaranteed),
31}
32
33impl<'a, 'tcx> InlineAsmCtxt<'a, 'tcx> {
34    pub(crate) fn new(fcx: &'a FnCtxt<'a, 'tcx>, def_id: LocalDefId) -> Self {
35        InlineAsmCtxt { target_features: fcx.tcx.asm_target_features(def_id), fcx }
36    }
37
38    fn tcx(&self) -> TyCtxt<'tcx> {
39        self.fcx.tcx
40    }
41
42    fn expr_ty(&self, expr: &hir::Expr<'tcx>) -> Ty<'tcx> {
43        let ty = self.fcx.typeck_results.borrow().expr_ty_adjusted(expr);
44        let ty = self.fcx.try_structurally_resolve_type(expr.span, ty);
45        if ty.has_non_region_infer() {
46            Ty::new_misc_error(self.tcx())
47        } else {
48            self.tcx().erase_and_anonymize_regions(ty)
49        }
50    }
51
52    // FIXME(compiler-errors): This could use `<$ty as Pointee>::Metadata == ()`
53    fn is_thin_ptr_ty(&self, span: Span, ty: Ty<'tcx>) -> bool {
54        // Type still may have region variables, but `Sized` does not depend
55        // on those, so just erase them before querying.
56        if self.fcx.type_is_sized_modulo_regions(self.fcx.param_env, ty) {
57            return true;
58        }
59        if let ty::Foreign(..) = self.fcx.try_structurally_resolve_type(span, ty).kind() {
60            return true;
61        }
62        false
63    }
64
65    fn get_asm_ty(
66        &self,
67        span: Span,
68        ty: Ty<'tcx>,
69    ) -> Result<InlineAsmType, NonAsmTypeReason<'tcx>> {
70        let asm_ty_isize = match self.tcx().sess.target.pointer_width {
71            16 => InlineAsmType::I16,
72            32 => InlineAsmType::I32,
73            64 => InlineAsmType::I64,
74            width => bug!("unsupported pointer width: {width}"),
75        };
76
77        match *ty.kind() {
78            ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Ok(InlineAsmType::I8),
79            ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Ok(InlineAsmType::I16),
80            ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Ok(InlineAsmType::I32),
81            ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Ok(InlineAsmType::I64),
82            ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => Ok(InlineAsmType::I128),
83            ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => Ok(asm_ty_isize),
84            ty::Float(FloatTy::F16) => Ok(InlineAsmType::F16),
85            ty::Float(FloatTy::F32) => Ok(InlineAsmType::F32),
86            ty::Float(FloatTy::F64) => Ok(InlineAsmType::F64),
87            ty::Float(FloatTy::F128) => Ok(InlineAsmType::F128),
88            ty::FnPtr(..) => Ok(asm_ty_isize),
89            ty::RawPtr(elem_ty, _) => {
90                if self.is_thin_ptr_ty(span, elem_ty) {
91                    Ok(asm_ty_isize)
92                } else {
93                    Err(NonAsmTypeReason::NotSizedPtr(ty))
94                }
95            }
96            ty::Adt(adt, args) if adt.repr().simd() => {
97                if !adt.is_struct() {
98                    let guar = self.fcx.dcx().span_delayed_bug(
99                        span,
100                        format!("repr(simd) should only be used on structs, got {}", adt.descr()),
101                    );
102                    return Err(NonAsmTypeReason::Tainted(guar));
103                }
104
105                let fields = &adt.non_enum_variant().fields;
106                if fields.is_empty() {
107                    return Err(NonAsmTypeReason::EmptySIMDArray(ty));
108                }
109                let field = &fields[FieldIdx::ZERO];
110                let elem_ty = field.ty(self.tcx(), args);
111
112                let (size, ty) = match *elem_ty.kind() {
113                    ty::Array(ty, len) => {
114                        // FIXME: `try_structurally_resolve_const` doesn't eval consts
115                        // in the old solver.
116                        let len = if self.fcx.next_trait_solver() {
117                            self.fcx.try_structurally_resolve_const(span, len)
118                        } else {
119                            self.fcx.tcx.normalize_erasing_regions(
120                                self.fcx.typing_env(self.fcx.param_env),
121                                len,
122                            )
123                        };
124                        let Some(len) = len.try_to_target_usize(self.tcx()) else {
125                            return Err(NonAsmTypeReason::UnevaluatedSIMDArrayLength(
126                                field.did, len,
127                            ));
128                        };
129                        (len, ty)
130                    }
131                    _ => (fields.len() as u64, elem_ty),
132                };
133
134                match ty.kind() {
135                    ty::Int(IntTy::I8) | ty::Uint(UintTy::U8) => Ok(InlineAsmType::VecI8(size)),
136                    ty::Int(IntTy::I16) | ty::Uint(UintTy::U16) => Ok(InlineAsmType::VecI16(size)),
137                    ty::Int(IntTy::I32) | ty::Uint(UintTy::U32) => Ok(InlineAsmType::VecI32(size)),
138                    ty::Int(IntTy::I64) | ty::Uint(UintTy::U64) => Ok(InlineAsmType::VecI64(size)),
139                    ty::Int(IntTy::I128) | ty::Uint(UintTy::U128) => {
140                        Ok(InlineAsmType::VecI128(size))
141                    }
142                    ty::Int(IntTy::Isize) | ty::Uint(UintTy::Usize) => {
143                        Ok(match self.tcx().sess.target.pointer_width {
144                            16 => InlineAsmType::VecI16(size),
145                            32 => InlineAsmType::VecI32(size),
146                            64 => InlineAsmType::VecI64(size),
147                            width => bug!("unsupported pointer width: {width}"),
148                        })
149                    }
150                    ty::Float(FloatTy::F16) => Ok(InlineAsmType::VecF16(size)),
151                    ty::Float(FloatTy::F32) => Ok(InlineAsmType::VecF32(size)),
152                    ty::Float(FloatTy::F64) => Ok(InlineAsmType::VecF64(size)),
153                    ty::Float(FloatTy::F128) => Ok(InlineAsmType::VecF128(size)),
154                    _ => Err(NonAsmTypeReason::InvalidElement(field.did, ty)),
155                }
156            }
157            ty::Infer(_) => bug!("unexpected infer ty in asm operand"),
158            _ => Err(NonAsmTypeReason::Invalid(ty)),
159        }
160    }
161
162    fn check_asm_operand_type(
163        &self,
164        idx: usize,
165        reg: InlineAsmRegOrRegClass,
166        expr: &'tcx hir::Expr<'tcx>,
167        template: &[InlineAsmTemplatePiece],
168        is_input: bool,
169        tied_input: Option<(&'tcx hir::Expr<'tcx>, Option<InlineAsmType>)>,
170    ) -> Option<InlineAsmType> {
171        let ty = self.expr_ty(expr);
172        if ty.has_non_region_infer() {
173            bug!("inference variable in asm operand ty: {:?} {:?}", expr, ty);
174        }
175
176        let asm_ty = match *ty.kind() {
177            // `!` is allowed for input but not for output (issue #87802)
178            ty::Never if is_input => return None,
179            _ if ty.references_error() => return None,
180            ty::Adt(adt, args) if self.tcx().is_lang_item(adt.did(), LangItem::MaybeUninit) => {
181                let fields = &adt.non_enum_variant().fields;
182                let ty = fields[FieldIdx::ONE].ty(self.tcx(), args);
183                // FIXME: Are we just trying to map to the `T` in `MaybeUninit<T>`?
184                // If so, just get it from the args.
185                let ty::Adt(ty, args) = ty.kind() else {
186                    unreachable!("expected first field of `MaybeUninit` to be an ADT")
187                };
188                assert!(
189                    ty.is_manually_drop(),
190                    "expected first field of `MaybeUninit` to be `ManuallyDrop`"
191                );
192                let fields = &ty.non_enum_variant().fields;
193                let ty = fields[FieldIdx::ZERO].ty(self.tcx(), args);
194                self.get_asm_ty(expr.span, ty)
195            }
196            _ => self.get_asm_ty(expr.span, ty),
197        };
198        let asm_ty = match asm_ty {
199            Ok(asm_ty) => asm_ty,
200            Err(reason) => {
201                match reason {
202                    NonAsmTypeReason::UnevaluatedSIMDArrayLength(did, len) => {
203                        let msg = format!("cannot evaluate SIMD vector length `{len}`");
204                        self.fcx
205                            .dcx()
206                            .struct_span_err(self.tcx().def_span(did), msg)
207                            .with_span_note(
208                                expr.span,
209                                "SIMD vector length needs to be known statically for use in `asm!`",
210                            )
211                            .emit();
212                    }
213                    NonAsmTypeReason::Invalid(ty) => {
214                        let msg = format!("cannot use value of type `{ty}` for inline assembly");
215                        self.fcx.dcx().struct_span_err(expr.span, msg).with_note(
216                            "only integers, floats, SIMD vectors, pointers and function pointers \
217                            can be used as arguments for inline assembly",
218                        ).emit();
219                    }
220                    NonAsmTypeReason::NotSizedPtr(ty) => {
221                        let msg = format!(
222                            "cannot use value of unsized pointer type `{ty}` for inline assembly"
223                        );
224                        self.fcx
225                            .dcx()
226                            .struct_span_err(expr.span, msg)
227                            .with_note("only sized pointers can be used in inline assembly")
228                            .emit();
229                    }
230                    NonAsmTypeReason::InvalidElement(did, ty) => {
231                        let msg = format!(
232                            "cannot use SIMD vector with element type `{ty}` for inline assembly"
233                        );
234                        self.fcx.dcx()
235                        .struct_span_err(self.tcx().def_span(did), msg).with_span_note(
236                            expr.span,
237                            "only integers, floats, SIMD vectors, pointers and function pointers \
238                            can be used as arguments for inline assembly",
239                        ).emit();
240                    }
241                    NonAsmTypeReason::EmptySIMDArray(ty) => {
242                        let msg = format!("use of empty SIMD vector `{ty}`");
243                        self.fcx.dcx().struct_span_err(expr.span, msg).emit();
244                    }
245                    NonAsmTypeReason::Tainted(_error_guard) => {
246                        // An error has already been reported.
247                    }
248                }
249                return None;
250            }
251        };
252
253        // Check that the type implements Copy. The only case where this can
254        // possibly fail is for SIMD types which don't #[derive(Copy)].
255        if !self.fcx.type_is_copy_modulo_regions(self.fcx.param_env, ty) {
256            let msg = "arguments for inline assembly must be copyable";
257            self.fcx
258                .dcx()
259                .struct_span_err(expr.span, msg)
260                .with_note(format!("`{ty}` does not implement the Copy trait"))
261                .emit();
262        }
263
264        // Ideally we wouldn't need to do this, but LLVM's register allocator
265        // really doesn't like it when tied operands have different types.
266        //
267        // This is purely an LLVM limitation, but we have to live with it since
268        // there is no way to hide this with implicit conversions.
269        //
270        // For the purposes of this check we only look at the `InlineAsmType`,
271        // which means that pointers and integers are treated as identical (modulo
272        // size).
273        if let Some((in_expr, Some(in_asm_ty))) = tied_input {
274            if in_asm_ty != asm_ty {
275                let msg = "incompatible types for asm inout argument";
276                let in_expr_ty = self.expr_ty(in_expr);
277                self.fcx
278                    .dcx()
279                    .struct_span_err(vec![in_expr.span, expr.span], msg)
280                    .with_span_label(in_expr.span, format!("type `{in_expr_ty}`"))
281                    .with_span_label(expr.span, format!("type `{ty}`"))
282                    .with_note(
283                        "asm inout arguments must have the same type, \
284                        unless they are both pointers or integers of the same size",
285                    )
286                    .emit();
287            }
288
289            // All of the later checks have already been done on the input, so
290            // let's not emit errors and warnings twice.
291            return Some(asm_ty);
292        }
293
294        // Check the type against the list of types supported by the selected
295        // register class.
296        let asm_arch = self.tcx().sess.asm_arch.unwrap();
297        let allow_experimental_reg = self.tcx().features().asm_experimental_reg();
298        let reg_class = reg.reg_class();
299        let supported_tys = reg_class.supported_types(asm_arch, allow_experimental_reg);
300        let Some((_, feature)) = supported_tys.iter().find(|&&(t, _)| t == asm_ty) else {
301            let mut err = if !allow_experimental_reg
302                && reg_class.supported_types(asm_arch, true).iter().any(|&(t, _)| t == asm_ty)
303            {
304                self.tcx().sess.create_feature_err(
305                    RegisterTypeUnstable { span: expr.span, ty },
306                    sym::asm_experimental_reg,
307                )
308            } else {
309                let msg = format!("type `{ty}` cannot be used with this register class");
310                let mut err = self.fcx.dcx().struct_span_err(expr.span, msg);
311                let supported_tys: Vec<_> =
312                    supported_tys.iter().map(|(t, _)| t.to_string()).collect();
313                err.note(format!(
314                    "register class `{}` supports these types: {}",
315                    reg_class.name(),
316                    supported_tys.join(", "),
317                ));
318                err
319            };
320            if let Some(suggest) = reg_class.suggest_class(asm_arch, asm_ty) {
321                err.help(format!("consider using the `{}` register class instead", suggest.name()));
322            }
323            err.emit();
324            return Some(asm_ty);
325        };
326
327        // Check whether the selected type requires a target feature. Note that
328        // this is different from the feature check we did earlier. While the
329        // previous check checked that this register class is usable at all
330        // with the currently enabled features, some types may only be usable
331        // with a register class when a certain feature is enabled. We check
332        // this here since it depends on the results of typeck.
333        //
334        // Also note that this check isn't run when the operand type is never
335        // (!). In that case we still need the earlier check to verify that the
336        // register class is usable at all.
337        if let Some(feature) = feature {
338            if !self.target_features.contains(feature) {
339                let msg = format!("`{feature}` target feature is not enabled");
340                self.fcx
341                    .dcx()
342                    .struct_span_err(expr.span, msg)
343                    .with_note(format!(
344                        "this is required to use type `{}` with register class `{}`",
345                        ty,
346                        reg_class.name(),
347                    ))
348                    .emit();
349                return Some(asm_ty);
350            }
351        }
352
353        // Check whether a modifier is suggested for using this type.
354        if let Some(ModifierInfo {
355            modifier: suggested_modifier,
356            result: suggested_result,
357            size: suggested_size,
358        }) = reg_class.suggest_modifier(asm_arch, asm_ty)
359        {
360            // Search for any use of this operand without a modifier and emit
361            // the suggestion for them.
362            let mut spans = vec![];
363            for piece in template {
364                if let &InlineAsmTemplatePiece::Placeholder { operand_idx, modifier, span } = piece
365                {
366                    if operand_idx == idx && modifier.is_none() {
367                        spans.push(span);
368                    }
369                }
370            }
371            if !spans.is_empty() {
372                let ModifierInfo {
373                    modifier: default_modifier,
374                    result: default_result,
375                    size: default_size,
376                } = reg_class.default_modifier(asm_arch).unwrap();
377                self.tcx().node_span_lint(
378                    lint::builtin::ASM_SUB_REGISTER,
379                    expr.hir_id,
380                    spans,
381                    |lint| {
382                        lint.primary_message("formatting may not be suitable for sub-register argument");
383                        lint.span_label(expr.span, "for this argument");
384                        lint.help(format!(
385                            "use `{{{idx}:{suggested_modifier}}}` to have the register formatted as `{suggested_result}` (for {suggested_size}-bit values)",
386                        ));
387                        lint.help(format!(
388                            "or use `{{{idx}:{default_modifier}}}` to keep the default formatting of `{default_result}` (for {default_size}-bit values)",
389                        ));
390                    },
391                );
392            }
393        }
394
395        Some(asm_ty)
396    }
397
398    pub(crate) fn check_asm(&self, asm: &hir::InlineAsm<'tcx>) {
399        let Some(asm_arch) = self.tcx().sess.asm_arch else {
400            self.fcx.dcx().delayed_bug("target architecture does not support asm");
401            return;
402        };
403        let allow_experimental_reg = self.tcx().features().asm_experimental_reg();
404        for (idx, &(op, op_sp)) in asm.operands.iter().enumerate() {
405            // Validate register classes against currently enabled target
406            // features. We check that at least one type is available for
407            // the enabled features.
408            //
409            // We ignore target feature requirements for clobbers: if the
410            // feature is disabled then the compiler doesn't care what we
411            // do with the registers.
412            //
413            // Note that this is only possible for explicit register
414            // operands, which cannot be used in the asm string.
415            if let Some(reg) = op.reg() {
416                // Some explicit registers cannot be used depending on the
417                // target. Reject those here.
418                if let InlineAsmRegOrRegClass::Reg(reg) = reg {
419                    if let InlineAsmReg::Err = reg {
420                        // `validate` will panic on `Err`, as an error must
421                        // already have been reported.
422                        continue;
423                    }
424                    if let Err(msg) = reg.validate(
425                        asm_arch,
426                        self.tcx().sess.relocation_model(),
427                        self.target_features,
428                        &self.tcx().sess.target,
429                        op.is_clobber(),
430                    ) {
431                        let msg = format!("cannot use register `{}`: {}", reg.name(), msg);
432                        self.fcx.dcx().span_err(op_sp, msg);
433                        continue;
434                    }
435                }
436
437                if !op.is_clobber() {
438                    let mut missing_required_features = vec![];
439                    let reg_class = reg.reg_class();
440                    if let InlineAsmRegClass::Err = reg_class {
441                        continue;
442                    }
443                    for &(_, feature) in reg_class.supported_types(asm_arch, allow_experimental_reg)
444                    {
445                        match feature {
446                            Some(feature) => {
447                                if self.target_features.contains(&feature) {
448                                    missing_required_features.clear();
449                                    break;
450                                } else {
451                                    missing_required_features.push(feature);
452                                }
453                            }
454                            None => {
455                                missing_required_features.clear();
456                                break;
457                            }
458                        }
459                    }
460
461                    // We are sorting primitive strs here and can use unstable sort here
462                    missing_required_features.sort_unstable();
463                    missing_required_features.dedup();
464                    match &missing_required_features[..] {
465                        [] => {}
466                        [feature] => {
467                            let msg = format!(
468                                "register class `{}` requires the `{}` target feature",
469                                reg_class.name(),
470                                feature
471                            );
472                            self.fcx.dcx().span_err(op_sp, msg);
473                            // register isn't enabled, don't do more checks
474                            continue;
475                        }
476                        features => {
477                            let msg = format!(
478                                "register class `{}` requires at least one of the following target features: {}",
479                                reg_class.name(),
480                                features
481                                    .iter()
482                                    .map(|f| f.as_str())
483                                    .intersperse(", ")
484                                    .collect::<String>(),
485                            );
486                            self.fcx.dcx().span_err(op_sp, msg);
487                            // register isn't enabled, don't do more checks
488                            continue;
489                        }
490                    }
491                }
492            }
493
494            match op {
495                hir::InlineAsmOperand::In { reg, expr } => {
496                    self.check_asm_operand_type(idx, reg, expr, asm.template, true, None);
497                }
498                hir::InlineAsmOperand::Out { reg, late: _, expr } => {
499                    if let Some(expr) = expr {
500                        self.check_asm_operand_type(idx, reg, expr, asm.template, false, None);
501                    }
502                }
503                hir::InlineAsmOperand::InOut { reg, late: _, expr } => {
504                    self.check_asm_operand_type(idx, reg, expr, asm.template, false, None);
505                }
506                hir::InlineAsmOperand::SplitInOut { reg, late: _, in_expr, out_expr } => {
507                    let in_ty =
508                        self.check_asm_operand_type(idx, reg, in_expr, asm.template, true, None);
509                    if let Some(out_expr) = out_expr {
510                        self.check_asm_operand_type(
511                            idx,
512                            reg,
513                            out_expr,
514                            asm.template,
515                            false,
516                            Some((in_expr, in_ty)),
517                        );
518                    }
519                }
520                hir::InlineAsmOperand::Const { anon_const } => {
521                    let ty = self.expr_ty(self.tcx().hir_body(anon_const.body).value);
522                    match ty.kind() {
523                        ty::Error(_) => {}
524                        _ if ty.is_integral() => {}
525                        _ => {
526                            self.fcx
527                                .dcx()
528                                .struct_span_err(op_sp, "invalid type for `const` operand")
529                                .with_span_label(
530                                    self.tcx().def_span(anon_const.def_id),
531                                    format!("is {} `{}`", ty.kind().article(), ty),
532                                )
533                                .with_help("`const` operands must be of an integer type")
534                                .emit();
535                        }
536                    }
537                }
538                // Typeck has checked that SymFn refers to a function.
539                hir::InlineAsmOperand::SymFn { expr } => {
540                    let ty = self.expr_ty(expr);
541                    match ty.kind() {
542                        ty::FnDef(..) => {}
543                        ty::Error(_) => {}
544                        _ => {
545                            self.fcx
546                                .dcx()
547                                .struct_span_err(op_sp, "invalid `sym` operand")
548                                .with_span_label(
549                                    expr.span,
550                                    format!("is {} `{}`", ty.kind().article(), ty),
551                                )
552                                .with_help(
553                                    "`sym` operands must refer to either a function or a static",
554                                )
555                                .emit();
556                        }
557                    }
558                }
559                // AST lowering guarantees that SymStatic points to a static.
560                hir::InlineAsmOperand::SymStatic { .. } => {}
561                // No special checking is needed for labels.
562                hir::InlineAsmOperand::Label { .. } => {}
563            }
564        }
565    }
566}