rustc_codegen_ssa/mir/operand.rs
1use std::fmt;
2
3use itertools::Either;
4use rustc_abi as abi;
5use rustc_abi::{
6 Align, BackendRepr, FIRST_VARIANT, FieldIdx, Primitive, Size, TagEncoding, VariantIdx, Variants,
7};
8use rustc_hir::LangItem;
9use rustc_middle::mir::interpret::{Pointer, Scalar, alloc_range};
10use rustc_middle::mir::{self, ConstValue};
11use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
12use rustc_middle::ty::{self, Ty};
13use rustc_middle::{bug, span_bug};
14use rustc_session::config::{AnnotateMoves, DebugInfo, OptLevel};
15use tracing::{debug, instrument};
16
17use super::place::{PlaceRef, PlaceValue};
18use super::rvalue::transmute_scalar;
19use super::{FunctionCx, LocalRef};
20use crate::MemFlags;
21use crate::common::IntPredicate;
22use crate::traits::*;
23
24/// The representation of a Rust value. The enum variant is in fact
25/// uniquely determined by the value's type, but is kept as a
26/// safety check.
27#[derive(Copy, Clone, Debug)]
28pub enum OperandValue<V> {
29 /// A reference to the actual operand. The data is guaranteed
30 /// to be valid for the operand's lifetime.
31 /// The second value, if any, is the extra data (vtable or length)
32 /// which indicates that it refers to an unsized rvalue.
33 ///
34 /// An `OperandValue` *must* be this variant for any type for which
35 /// [`LayoutTypeCodegenMethods::is_backend_ref`] returns `true`.
36 /// (That basically amounts to "isn't one of the other variants".)
37 ///
38 /// This holds a [`PlaceValue`] (like a [`PlaceRef`] does) with a pointer
39 /// to the location holding the value. The type behind that pointer is the
40 /// one returned by [`LayoutTypeCodegenMethods::backend_type`].
41 Ref(PlaceValue<V>),
42 /// A single LLVM immediate value.
43 ///
44 /// An `OperandValue` *must* be this variant for any type for which
45 /// [`LayoutTypeCodegenMethods::is_backend_immediate`] returns `true`.
46 /// The backend value in this variant must be the *immediate* backend type,
47 /// as returned by [`LayoutTypeCodegenMethods::immediate_backend_type`].
48 Immediate(V),
49 /// A pair of immediate LLVM values. Used by wide pointers too.
50 ///
51 /// # Invariants
52 /// - For `Pair(a, b)`, `a` is always at offset 0, but may have `FieldIdx(1..)`
53 /// - `b` is not at offset 0, because `V` is not a 1ZST type.
54 /// - `a` and `b` will have a different FieldIdx, but otherwise `b`'s may be lower
55 /// or they may not be adjacent, due to arbitrary numbers of 1ZST fields that
56 /// will not affect the shape of the data which determines if `Pair` will be used.
57 /// - An `OperandValue` *must* be this variant for any type for which
58 /// [`LayoutTypeCodegenMethods::is_backend_scalar_pair`] returns `true`.
59 /// - The backend values in this variant must be the *immediate* backend types,
60 /// as returned by [`LayoutTypeCodegenMethods::scalar_pair_element_backend_type`]
61 /// with `immediate: true`.
62 Pair(V, V),
63 /// A value taking no bytes, and which therefore needs no LLVM value at all.
64 ///
65 /// If you ever need a `V` to pass to something, get a fresh poison value
66 /// from [`ConstCodegenMethods::const_poison`].
67 ///
68 /// An `OperandValue` *must* be this variant for any type for which
69 /// `is_zst` on its `Layout` returns `true`. Note however that
70 /// these values can still require alignment.
71 ZeroSized,
72}
73
74impl<V: CodegenObject> OperandValue<V> {
75 /// Return the data pointer and optional metadata as backend values
76 /// if this value can be treat as a pointer.
77 pub(crate) fn try_pointer_parts(self) -> Option<(V, Option<V>)> {
78 match self {
79 OperandValue::Immediate(llptr) => Some((llptr, None)),
80 OperandValue::Pair(llptr, llextra) => Some((llptr, Some(llextra))),
81 OperandValue::Ref(_) | OperandValue::ZeroSized => None,
82 }
83 }
84
85 /// Treat this value as a pointer and return the data pointer and
86 /// optional metadata as backend values.
87 ///
88 /// If you're making a place, use [`Self::deref`] instead.
89 pub(crate) fn pointer_parts(self) -> (V, Option<V>) {
90 self.try_pointer_parts()
91 .unwrap_or_else(|| bug!("OperandValue cannot be a pointer: {self:?}"))
92 }
93
94 /// Treat this value as a pointer and return the place to which it points.
95 ///
96 /// The pointer immediate doesn't inherently know its alignment,
97 /// so you need to pass it in. If you want to get it from a type's ABI
98 /// alignment, then maybe you want [`OperandRef::deref`] instead.
99 ///
100 /// This is the inverse of [`PlaceValue::address`].
101 pub(crate) fn deref(self, align: Align) -> PlaceValue<V> {
102 let (llval, llextra) = self.pointer_parts();
103 PlaceValue { llval, llextra, align }
104 }
105
106 pub(crate) fn is_expected_variant_for_type<'tcx, Cx: LayoutTypeCodegenMethods<'tcx>>(
107 &self,
108 cx: &Cx,
109 ty: TyAndLayout<'tcx>,
110 ) -> bool {
111 match self {
112 OperandValue::ZeroSized => ty.is_zst(),
113 OperandValue::Immediate(_) => cx.is_backend_immediate(ty),
114 OperandValue::Pair(_, _) => cx.is_backend_scalar_pair(ty),
115 OperandValue::Ref(_) => cx.is_backend_ref(ty),
116 }
117 }
118}
119
120/// An `OperandRef` is an "SSA" reference to a Rust value, along with
121/// its type.
122///
123/// NOTE: unless you know a value's type exactly, you should not
124/// generate LLVM opcodes acting on it and instead act via methods,
125/// to avoid nasty edge cases. In particular, using `Builder::store`
126/// directly is sure to cause problems -- use `OperandRef::store`
127/// instead.
128#[derive(Copy, Clone)]
129pub struct OperandRef<'tcx, V> {
130 /// The value.
131 pub val: OperandValue<V>,
132
133 /// The layout of value, based on its Rust type.
134 pub layout: TyAndLayout<'tcx>,
135
136 /// Annotation for profiler visibility of move/copy operations.
137 /// When set, the store operation should appear as an inlined call to this function.
138 pub move_annotation: Option<ty::Instance<'tcx>>,
139}
140
141impl<V: CodegenObject> fmt::Debug for OperandRef<'_, V> {
142 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
143 write!(f, "OperandRef({:?} @ {:?})", self.val, self.layout)
144 }
145}
146
147impl<'a, 'tcx, V: CodegenObject> OperandRef<'tcx, V> {
148 pub fn zero_sized(layout: TyAndLayout<'tcx>) -> OperandRef<'tcx, V> {
149 assert!(layout.is_zst());
150 OperandRef { val: OperandValue::ZeroSized, layout, move_annotation: None }
151 }
152
153 pub(crate) fn from_const<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
154 bx: &mut Bx,
155 val: mir::ConstValue,
156 ty: Ty<'tcx>,
157 ) -> Self {
158 let layout = bx.layout_of(ty);
159
160 let val = match val {
161 ConstValue::Scalar(x) => {
162 let BackendRepr::Scalar(scalar) = layout.backend_repr else {
163 bug!("from_const: invalid ByVal layout: {:#?}", layout);
164 };
165 let llval = bx.scalar_to_backend(x, scalar, bx.immediate_backend_type(layout));
166 OperandValue::Immediate(llval)
167 }
168 ConstValue::ZeroSized => return OperandRef::zero_sized(layout),
169 ConstValue::Slice { alloc_id, meta } => {
170 let BackendRepr::ScalarPair(a_scalar, _) = layout.backend_repr else {
171 bug!("from_const: invalid ScalarPair layout: {:#?}", layout);
172 };
173 let a = Scalar::from_pointer(Pointer::new(alloc_id.into(), Size::ZERO), &bx.tcx());
174 let a_llval = bx.scalar_to_backend(
175 a,
176 a_scalar,
177 bx.scalar_pair_element_backend_type(layout, 0, true),
178 );
179 let b_llval = bx.const_usize(meta);
180 OperandValue::Pair(a_llval, b_llval)
181 }
182 ConstValue::Indirect { alloc_id, offset } => {
183 let alloc = bx.tcx().global_alloc(alloc_id).unwrap_memory();
184 return Self::from_const_alloc(bx, layout, alloc, offset);
185 }
186 };
187
188 OperandRef { val, layout, move_annotation: None }
189 }
190
191 fn from_const_alloc<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
192 bx: &mut Bx,
193 layout: TyAndLayout<'tcx>,
194 alloc: rustc_middle::mir::interpret::ConstAllocation<'tcx>,
195 offset: Size,
196 ) -> Self {
197 let alloc_align = alloc.inner().align;
198 assert!(alloc_align >= layout.align.abi, "{alloc_align:?} < {:?}", layout.align.abi);
199
200 let read_scalar = |start, size, s: abi::Scalar, ty| {
201 match alloc.0.read_scalar(
202 bx,
203 alloc_range(start, size),
204 /*read_provenance*/ matches!(s.primitive(), abi::Primitive::Pointer(_)),
205 ) {
206 Ok(val) => bx.scalar_to_backend(val, s, ty),
207 Err(_) => bx.const_poison(ty),
208 }
209 };
210
211 // It may seem like all types with `Scalar` or `ScalarPair` ABI are fair game at this point.
212 // However, `MaybeUninit<u64>` is considered a `Scalar` as far as its layout is concerned --
213 // and yet cannot be represented by an interpreter `Scalar`, since we have to handle the
214 // case where some of the bytes are initialized and others are not. So, we need an extra
215 // check that walks over the type of `mplace` to make sure it is truly correct to treat this
216 // like a `Scalar` (or `ScalarPair`).
217 match layout.backend_repr {
218 BackendRepr::Scalar(s @ abi::Scalar::Initialized { .. }) => {
219 let size = s.size(bx);
220 assert_eq!(size, layout.size, "abi::Scalar size does not match layout size");
221 let val = read_scalar(offset, size, s, bx.immediate_backend_type(layout));
222 OperandRef { val: OperandValue::Immediate(val), layout, move_annotation: None }
223 }
224 BackendRepr::ScalarPair(
225 a @ abi::Scalar::Initialized { .. },
226 b @ abi::Scalar::Initialized { .. },
227 ) => {
228 let (a_size, b_size) = (a.size(bx), b.size(bx));
229 let b_offset = (offset + a_size).align_to(b.align(bx).abi);
230 assert!(b_offset.bytes() > 0);
231 let a_val = read_scalar(
232 offset,
233 a_size,
234 a,
235 bx.scalar_pair_element_backend_type(layout, 0, true),
236 );
237 let b_val = read_scalar(
238 b_offset,
239 b_size,
240 b,
241 bx.scalar_pair_element_backend_type(layout, 1, true),
242 );
243 OperandRef { val: OperandValue::Pair(a_val, b_val), layout, move_annotation: None }
244 }
245 _ if layout.is_zst() => OperandRef::zero_sized(layout),
246 _ => {
247 // Neither a scalar nor scalar pair. Load from a place
248 // FIXME: should we cache `const_data_from_alloc` to avoid repeating this for the
249 // same `ConstAllocation`?
250 let init = bx.const_data_from_alloc(alloc);
251 let base_addr = bx.static_addr_of(init, alloc_align, None);
252
253 let llval = bx.const_ptr_byte_offset(base_addr, offset);
254 bx.load_operand(PlaceRef::new_sized(llval, layout))
255 }
256 }
257 }
258
259 /// Asserts that this operand refers to a scalar and returns
260 /// a reference to its value.
261 pub fn immediate(self) -> V {
262 match self.val {
263 OperandValue::Immediate(s) => s,
264 _ => bug!("not immediate: {:?}", self),
265 }
266 }
267
268 /// Asserts that this operand is a pointer (or reference) and returns
269 /// the place to which it points. (This requires no code to be emitted
270 /// as we represent places using the pointer to the place.)
271 ///
272 /// This uses [`Ty::builtin_deref`] to include the type of the place and
273 /// assumes the place is aligned to the pointee's usual ABI alignment.
274 ///
275 /// If you don't need the type, see [`OperandValue::pointer_parts`]
276 /// or [`OperandValue::deref`].
277 pub fn deref<Cx: CodegenMethods<'tcx>>(self, cx: &Cx) -> PlaceRef<'tcx, V> {
278 if self.layout.ty.is_box() {
279 // Derefer should have removed all Box derefs
280 bug!("dereferencing {:?} in codegen", self.layout.ty);
281 }
282
283 let projected_ty = self
284 .layout
285 .ty
286 .builtin_deref(true)
287 .unwrap_or_else(|| bug!("deref of non-pointer {:?}", self));
288
289 let layout = cx.layout_of(projected_ty);
290 self.val.deref(layout.align.abi).with_type(layout)
291 }
292
293 /// Store this operand into a place, applying move/copy annotation if present.
294 ///
295 /// This is the preferred method for storing operands, as it automatically
296 /// applies profiler annotations for tracked move/copy operations.
297 pub fn store_with_annotation<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
298 self,
299 bx: &mut Bx,
300 dest: PlaceRef<'tcx, V>,
301 ) {
302 if let Some(instance) = self.move_annotation {
303 bx.with_move_annotation(instance, |bx| self.val.store(bx, dest))
304 } else {
305 self.val.store(bx, dest)
306 }
307 }
308
309 /// If this operand is a `Pair`, we return an aggregate with the two values.
310 /// For other cases, see `immediate`.
311 pub fn immediate_or_packed_pair<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
312 self,
313 bx: &mut Bx,
314 ) -> V {
315 if let OperandValue::Pair(a, b) = self.val {
316 let llty = bx.cx().immediate_backend_type(self.layout);
317 debug!("Operand::immediate_or_packed_pair: packing {:?} into {:?}", self, llty);
318 // Reconstruct the immediate aggregate.
319 let mut llpair = bx.cx().const_poison(llty);
320 llpair = bx.insert_value(llpair, a, 0);
321 llpair = bx.insert_value(llpair, b, 1);
322 llpair
323 } else {
324 self.immediate()
325 }
326 }
327
328 /// If the type is a pair, we return a `Pair`, otherwise, an `Immediate`.
329 pub fn from_immediate_or_packed_pair<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
330 bx: &mut Bx,
331 llval: V,
332 layout: TyAndLayout<'tcx>,
333 ) -> Self {
334 let val = if let BackendRepr::ScalarPair(..) = layout.backend_repr {
335 debug!("Operand::from_immediate_or_packed_pair: unpacking {:?} @ {:?}", llval, layout);
336
337 // Deconstruct the immediate aggregate.
338 let a_llval = bx.extract_value(llval, 0);
339 let b_llval = bx.extract_value(llval, 1);
340 OperandValue::Pair(a_llval, b_llval)
341 } else {
342 OperandValue::Immediate(llval)
343 };
344 OperandRef { val, layout, move_annotation: None }
345 }
346
347 pub(crate) fn extract_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
348 &self,
349 fx: &mut FunctionCx<'a, 'tcx, Bx>,
350 bx: &mut Bx,
351 i: usize,
352 ) -> Self {
353 let field = self.layout.field(bx.cx(), i);
354 let offset = self.layout.fields.offset(i);
355
356 if !bx.is_backend_ref(self.layout) && bx.is_backend_ref(field) {
357 // Part of https://github.com/rust-lang/compiler-team/issues/838
358 span_bug!(
359 fx.mir.span,
360 "Non-ref type {self:?} cannot project to ref field type {field:?}",
361 );
362 }
363
364 let val = if field.is_zst() {
365 OperandValue::ZeroSized
366 } else if field.size == self.layout.size {
367 assert_eq!(offset.bytes(), 0);
368 fx.codegen_transmute_operand(bx, *self, field)
369 } else {
370 let (in_scalar, imm) = match (self.val, self.layout.backend_repr) {
371 // Extract a scalar component from a pair.
372 (OperandValue::Pair(a_llval, b_llval), BackendRepr::ScalarPair(a, b)) => {
373 if offset.bytes() == 0 {
374 assert_eq!(field.size, a.size(bx.cx()));
375 (Some(a), a_llval)
376 } else {
377 assert_eq!(offset, a.size(bx.cx()).align_to(b.align(bx.cx()).abi));
378 assert_eq!(field.size, b.size(bx.cx()));
379 (Some(b), b_llval)
380 }
381 }
382
383 _ => {
384 span_bug!(fx.mir.span, "OperandRef::extract_field({:?}): not applicable", self)
385 }
386 };
387 OperandValue::Immediate(match field.backend_repr {
388 BackendRepr::SimdVector { .. } => imm,
389 BackendRepr::Scalar(out_scalar) => {
390 let Some(in_scalar) = in_scalar else {
391 span_bug!(
392 fx.mir.span,
393 "OperandRef::extract_field({:?}): missing input scalar for output scalar",
394 self
395 )
396 };
397 if in_scalar != out_scalar {
398 // If the backend and backend_immediate types might differ,
399 // flip back to the backend type then to the new immediate.
400 // This avoids nop truncations, but still handles things like
401 // Bools in union fields needs to be truncated.
402 let backend = bx.from_immediate(imm);
403 bx.to_immediate_scalar(backend, out_scalar)
404 } else {
405 imm
406 }
407 }
408 BackendRepr::ScalarPair(_, _)
409 | BackendRepr::Memory { .. }
410 | BackendRepr::ScalableVector { .. } => bug!(),
411 })
412 };
413
414 OperandRef { val, layout: field, move_annotation: None }
415 }
416
417 /// Obtain the actual discriminant of a value.
418 #[instrument(level = "trace", skip(fx, bx))]
419 pub fn codegen_get_discr<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
420 self,
421 fx: &mut FunctionCx<'a, 'tcx, Bx>,
422 bx: &mut Bx,
423 cast_to: Ty<'tcx>,
424 ) -> V {
425 let dl = &bx.tcx().data_layout;
426 let cast_to_layout = bx.cx().layout_of(cast_to);
427 let cast_to = bx.cx().immediate_backend_type(cast_to_layout);
428
429 // We check uninhabitedness separately because a type like
430 // `enum Foo { Bar(i32, !) }` is still reported as `Variants::Single`,
431 // *not* as `Variants::Empty`.
432 if self.layout.is_uninhabited() {
433 return bx.cx().const_poison(cast_to);
434 }
435
436 let (tag_scalar, tag_encoding, tag_field) = match self.layout.variants {
437 Variants::Empty => unreachable!("we already handled uninhabited types"),
438 Variants::Single { index } => {
439 let discr_val =
440 if let Some(discr) = self.layout.ty.discriminant_for_variant(bx.tcx(), index) {
441 discr.val
442 } else {
443 // This arm is for types which are neither enums nor coroutines,
444 // and thus for which the only possible "variant" should be the first one.
445 assert_eq!(index, FIRST_VARIANT);
446 // There's thus no actual discriminant to return, so we return
447 // what it would have been if this was a single-variant enum.
448 0
449 };
450 return bx.cx().const_uint_big(cast_to, discr_val);
451 }
452 Variants::Multiple { tag, ref tag_encoding, tag_field, .. } => {
453 (tag, tag_encoding, tag_field)
454 }
455 };
456
457 // Read the tag/niche-encoded discriminant from memory.
458 let tag_op = match self.val {
459 OperandValue::ZeroSized => bug!(),
460 OperandValue::Immediate(_) | OperandValue::Pair(_, _) => {
461 self.extract_field(fx, bx, tag_field.as_usize())
462 }
463 OperandValue::Ref(place) => {
464 let tag = place.with_type(self.layout).project_field(bx, tag_field.as_usize());
465 bx.load_operand(tag)
466 }
467 };
468 let tag_imm = tag_op.immediate();
469
470 // Decode the discriminant (specifically if it's niche-encoded).
471 match *tag_encoding {
472 TagEncoding::Direct => {
473 let signed = match tag_scalar.primitive() {
474 // We use `i1` for bytes that are always `0` or `1`,
475 // e.g., `#[repr(i8)] enum E { A, B }`, but we can't
476 // let LLVM interpret the `i1` as signed, because
477 // then `i1 1` (i.e., `E::B`) is effectively `i8 -1`.
478 Primitive::Int(_, signed) => !tag_scalar.is_bool() && signed,
479 _ => false,
480 };
481 bx.intcast(tag_imm, cast_to, signed)
482 }
483 TagEncoding::Niche { untagged_variant, ref niche_variants, niche_start } => {
484 // Cast to an integer so we don't have to treat a pointer as a
485 // special case.
486 let (tag, tag_llty) = match tag_scalar.primitive() {
487 // FIXME(erikdesjardins): handle non-default addrspace ptr sizes
488 Primitive::Pointer(_) => {
489 let t = bx.type_from_integer(dl.ptr_sized_integer());
490 let tag = bx.ptrtoint(tag_imm, t);
491 (tag, t)
492 }
493 _ => (tag_imm, bx.cx().immediate_backend_type(tag_op.layout)),
494 };
495
496 // `layout_sanity_check` ensures that we only get here for cases where the discriminant
497 // value and the variant index match, since that's all `Niche` can encode.
498
499 let relative_max = niche_variants.end().as_u32() - niche_variants.start().as_u32();
500 let niche_start_const = bx.cx().const_uint_big(tag_llty, niche_start);
501
502 // We have a subrange `niche_start..=niche_end` inside `range`.
503 // If the value of the tag is inside this subrange, it's a
504 // "niche value", an increment of the discriminant. Otherwise it
505 // indicates the untagged variant.
506 // A general algorithm to extract the discriminant from the tag
507 // is:
508 // relative_tag = tag - niche_start
509 // is_niche = relative_tag <= (ule) relative_max
510 // discr = if is_niche {
511 // cast(relative_tag) + niche_variants.start()
512 // } else {
513 // untagged_variant
514 // }
515 // However, we will likely be able to emit simpler code.
516 let (is_niche, tagged_discr, delta) = if relative_max == 0 {
517 // Best case scenario: only one tagged variant. This will
518 // likely become just a comparison and a jump.
519 // The algorithm is:
520 // is_niche = tag == niche_start
521 // discr = if is_niche {
522 // niche_start
523 // } else {
524 // untagged_variant
525 // }
526 let is_niche = bx.icmp(IntPredicate::IntEQ, tag, niche_start_const);
527 let tagged_discr =
528 bx.cx().const_uint(cast_to, niche_variants.start().as_u32() as u64);
529 (is_niche, tagged_discr, 0)
530 } else {
531 // Thanks to parameter attributes and load metadata, LLVM already knows
532 // the general valid range of the tag. It's possible, though, for there
533 // to be an impossible value *in the middle*, which those ranges don't
534 // communicate, so it's worth an `assume` to let the optimizer know.
535 // Most importantly, this means when optimizing a variant test like
536 // `SELECT(is_niche, complex, CONST) == CONST` it's ok to simplify that
537 // to `!is_niche` because the `complex` part can't possibly match.
538 //
539 // This was previously asserted on `tagged_discr` below, where the
540 // impossible value is more obvious, but that caused an intermediate
541 // value to become multi-use and thus not optimize, so instead this
542 // assumes on the original input which is always multi-use. See
543 // <https://github.com/llvm/llvm-project/issues/134024#issuecomment-3131782555>
544 //
545 // FIXME: If we ever get range assume operand bundles in LLVM (so we
546 // don't need the `icmp`s in the instruction stream any more), it
547 // might be worth moving this back to being on the switch argument
548 // where it's more obviously applicable.
549 if niche_variants.contains(&untagged_variant)
550 && bx.cx().sess().opts.optimize != OptLevel::No
551 {
552 let impossible = niche_start
553 .wrapping_add(u128::from(untagged_variant.as_u32()))
554 .wrapping_sub(u128::from(niche_variants.start().as_u32()));
555 let impossible = bx.cx().const_uint_big(tag_llty, impossible);
556 let ne = bx.icmp(IntPredicate::IntNE, tag, impossible);
557 bx.assume(ne);
558 }
559
560 // With multiple niched variants we'll have to actually compute
561 // the variant index from the stored tag.
562 //
563 // However, there's still one small optimization we can often do for
564 // determining *whether* a tag value is a natural value or a niched
565 // variant. The general algorithm involves a subtraction that often
566 // wraps in practice, making it tricky to analyse. However, in cases
567 // where there are few enough possible values of the tag that it doesn't
568 // need to wrap around, we can instead just look for the contiguous
569 // tag values on the end of the range with a single comparison.
570 //
571 // For example, take the type `enum Demo { A, B, Untagged(bool) }`.
572 // The `bool` is {0, 1}, and the two other variants are given the
573 // tags {2, 3} respectively. That means the `tag_range` is
574 // `[0, 3]`, which doesn't wrap as unsigned (nor as signed), so
575 // we can test for the niched variants with just `>= 2`.
576 //
577 // That means we're looking either for the niche values *above*
578 // the natural values of the untagged variant:
579 //
580 // niche_start niche_end
581 // | |
582 // v v
583 // MIN -------------+---------------------------+---------- MAX
584 // ^ | is niche |
585 // | +---------------------------+
586 // | |
587 // tag_range.start tag_range.end
588 //
589 // Or *below* the natural values:
590 //
591 // niche_start niche_end
592 // | |
593 // v v
594 // MIN ----+-----------------------+---------------------- MAX
595 // | is niche | ^
596 // +-----------------------+ |
597 // | |
598 // tag_range.start tag_range.end
599 //
600 // With those two options and having the flexibility to choose
601 // between a signed or unsigned comparison on the tag, that
602 // covers most realistic scenarios. The tests have a (contrived)
603 // example of a 1-byte enum with over 128 niched variants which
604 // wraps both as signed as unsigned, though, and for something
605 // like that we're stuck with the general algorithm.
606
607 let tag_range = tag_scalar.valid_range(&dl);
608 let tag_size = tag_scalar.size(&dl);
609 let niche_end = u128::from(relative_max).wrapping_add(niche_start);
610 let niche_end = tag_size.truncate(niche_end);
611
612 let relative_discr = bx.sub(tag, niche_start_const);
613 let cast_tag = bx.intcast(relative_discr, cast_to, false);
614 let is_niche = if tag_range.no_unsigned_wraparound(tag_size) == Ok(true) {
615 if niche_start == tag_range.start {
616 let niche_end_const = bx.cx().const_uint_big(tag_llty, niche_end);
617 bx.icmp(IntPredicate::IntULE, tag, niche_end_const)
618 } else {
619 assert_eq!(niche_end, tag_range.end);
620 bx.icmp(IntPredicate::IntUGE, tag, niche_start_const)
621 }
622 } else if tag_range.no_signed_wraparound(tag_size) == Ok(true) {
623 if niche_start == tag_range.start {
624 let niche_end_const = bx.cx().const_uint_big(tag_llty, niche_end);
625 bx.icmp(IntPredicate::IntSLE, tag, niche_end_const)
626 } else {
627 assert_eq!(niche_end, tag_range.end);
628 bx.icmp(IntPredicate::IntSGE, tag, niche_start_const)
629 }
630 } else {
631 bx.icmp(
632 IntPredicate::IntULE,
633 relative_discr,
634 bx.cx().const_uint(tag_llty, relative_max as u64),
635 )
636 };
637
638 (is_niche, cast_tag, niche_variants.start().as_u32() as u128)
639 };
640
641 let tagged_discr = if delta == 0 {
642 tagged_discr
643 } else {
644 bx.add(tagged_discr, bx.cx().const_uint_big(cast_to, delta))
645 };
646
647 let untagged_variant_const =
648 bx.cx().const_uint(cast_to, u64::from(untagged_variant.as_u32()));
649
650 let discr = bx.select(is_niche, tagged_discr, untagged_variant_const);
651
652 // In principle we could insert assumes on the possible range of `discr`, but
653 // currently in LLVM this isn't worth it because the original `tag` will
654 // have either a `range` parameter attribute or `!range` metadata,
655 // or come from a `transmute` that already `assume`d it.
656
657 discr
658 }
659 }
660 }
661}
662
663/// Each of these variants starts out as `Either::Right` when it's uninitialized,
664/// then setting the field changes that to `Either::Left` with the backend value.
665#[derive(Debug, Copy, Clone)]
666enum OperandValueBuilder<V> {
667 ZeroSized,
668 Immediate(Either<V, abi::Scalar>),
669 Pair(Either<V, abi::Scalar>, Either<V, abi::Scalar>),
670 /// `repr(simd)` types need special handling because they each have a non-empty
671 /// array field (which uses [`OperandValue::Ref`]) despite the SIMD type itself
672 /// using [`OperandValue::Immediate`] which for any other kind of type would
673 /// mean that its one non-ZST field would also be [`OperandValue::Immediate`].
674 Vector(Either<V, ()>),
675}
676
677/// Allows building up an `OperandRef` by setting fields one at a time.
678#[derive(Debug, Copy, Clone)]
679pub(super) struct OperandRefBuilder<'tcx, V> {
680 val: OperandValueBuilder<V>,
681 layout: TyAndLayout<'tcx>,
682}
683
684impl<'a, 'tcx, V: CodegenObject> OperandRefBuilder<'tcx, V> {
685 /// Creates an uninitialized builder for an instance of the `layout`.
686 ///
687 /// ICEs for [`BackendRepr::Memory`] types (other than ZSTs), which should
688 /// be built up inside a [`PlaceRef`] instead as they need an allocated place
689 /// into which to write the values of the fields.
690 pub(super) fn new(layout: TyAndLayout<'tcx>) -> Self {
691 let val = match layout.backend_repr {
692 BackendRepr::Memory { .. } if layout.is_zst() => OperandValueBuilder::ZeroSized,
693 BackendRepr::Scalar(s) => OperandValueBuilder::Immediate(Either::Right(s)),
694 BackendRepr::ScalarPair(a, b) => {
695 OperandValueBuilder::Pair(Either::Right(a), Either::Right(b))
696 }
697 BackendRepr::SimdVector { .. } | BackendRepr::ScalableVector { .. } => {
698 OperandValueBuilder::Vector(Either::Right(()))
699 }
700 BackendRepr::Memory { .. } => {
701 bug!("Cannot use non-ZST Memory-ABI type in operand builder: {layout:?}");
702 }
703 };
704 OperandRefBuilder { val, layout }
705 }
706
707 pub(super) fn insert_field<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
708 &mut self,
709 bx: &mut Bx,
710 variant: VariantIdx,
711 field: FieldIdx,
712 field_operand: OperandRef<'tcx, V>,
713 ) {
714 if let OperandValue::ZeroSized = field_operand.val {
715 // A ZST never adds any state, so just ignore it.
716 // This special-casing is worth it because of things like
717 // `Result<!, !>` where `Ok(never)` is legal to write,
718 // but the type shows as FieldShape::Primitive so we can't
719 // actually look at the layout for the field being set.
720 return;
721 }
722
723 let is_zero_offset = if let abi::FieldsShape::Primitive = self.layout.fields {
724 // The other branch looking at field layouts ICEs for primitives,
725 // so we need to handle them separately.
726 // Because we handled ZSTs above (like the metadata in a thin pointer),
727 // the only possibility is that we're setting the one-and-only field.
728 assert!(!self.layout.is_zst());
729 assert_eq!(variant, FIRST_VARIANT);
730 assert_eq!(field, FieldIdx::ZERO);
731 true
732 } else {
733 let variant_layout = self.layout.for_variant(bx.cx(), variant);
734 let field_offset = variant_layout.fields.offset(field.as_usize());
735 field_offset == Size::ZERO
736 };
737
738 let mut update = |tgt: &mut Either<V, abi::Scalar>, src, from_scalar| {
739 let to_scalar = tgt.unwrap_right();
740 // We transmute here (rather than just `from_immediate`) because in
741 // `Result<usize, *const ()>` the field of the `Ok` is an integer,
742 // but the corresponding scalar in the enum is a pointer.
743 let imm = transmute_scalar(bx, src, from_scalar, to_scalar);
744 *tgt = Either::Left(imm);
745 };
746
747 match (field_operand.val, field_operand.layout.backend_repr) {
748 (OperandValue::ZeroSized, _) => unreachable!("Handled above"),
749 (OperandValue::Immediate(v), BackendRepr::Scalar(from_scalar)) => match &mut self.val {
750 OperandValueBuilder::Immediate(val @ Either::Right(_)) if is_zero_offset => {
751 update(val, v, from_scalar);
752 }
753 OperandValueBuilder::Pair(fst @ Either::Right(_), _) if is_zero_offset => {
754 update(fst, v, from_scalar);
755 }
756 OperandValueBuilder::Pair(_, snd @ Either::Right(_)) if !is_zero_offset => {
757 update(snd, v, from_scalar);
758 }
759 _ => {
760 bug!("Tried to insert {field_operand:?} into {variant:?}.{field:?} of {self:?}")
761 }
762 },
763 (OperandValue::Immediate(v), BackendRepr::SimdVector { .. }) => match &mut self.val {
764 OperandValueBuilder::Vector(val @ Either::Right(())) if is_zero_offset => {
765 *val = Either::Left(v);
766 }
767 _ => {
768 bug!("Tried to insert {field_operand:?} into {variant:?}.{field:?} of {self:?}")
769 }
770 },
771 (OperandValue::Pair(a, b), BackendRepr::ScalarPair(from_sa, from_sb)) => {
772 match &mut self.val {
773 OperandValueBuilder::Pair(fst @ Either::Right(_), snd @ Either::Right(_)) => {
774 update(fst, a, from_sa);
775 update(snd, b, from_sb);
776 }
777 _ => bug!(
778 "Tried to insert {field_operand:?} into {variant:?}.{field:?} of {self:?}"
779 ),
780 }
781 }
782 (OperandValue::Ref(place), BackendRepr::Memory { .. }) => match &mut self.val {
783 OperandValueBuilder::Vector(val @ Either::Right(())) => {
784 let ibty = bx.cx().immediate_backend_type(self.layout);
785 let simd = bx.load_from_place(ibty, place);
786 *val = Either::Left(simd);
787 }
788 _ => {
789 bug!("Tried to insert {field_operand:?} into {variant:?}.{field:?} of {self:?}")
790 }
791 },
792 _ => bug!("Operand cannot be used with `insert_field`: {field_operand:?}"),
793 }
794 }
795
796 /// Insert the immediate value `imm` for field `f` in the *type itself*,
797 /// rather than into one of the variants.
798 ///
799 /// Most things want [`Self::insert_field`] instead, but this one is
800 /// necessary for writing things like enum tags that aren't in any variant.
801 pub(super) fn insert_imm(&mut self, f: FieldIdx, imm: V) {
802 let field_offset = self.layout.fields.offset(f.as_usize());
803 let is_zero_offset = field_offset == Size::ZERO;
804 match &mut self.val {
805 OperandValueBuilder::Immediate(val @ Either::Right(_)) if is_zero_offset => {
806 *val = Either::Left(imm);
807 }
808 OperandValueBuilder::Pair(fst @ Either::Right(_), _) if is_zero_offset => {
809 *fst = Either::Left(imm);
810 }
811 OperandValueBuilder::Pair(_, snd @ Either::Right(_)) if !is_zero_offset => {
812 *snd = Either::Left(imm);
813 }
814 _ => bug!("Tried to insert {imm:?} into field {f:?} of {self:?}"),
815 }
816 }
817
818 /// After having set all necessary fields, this converts the builder back
819 /// to the normal `OperandRef`.
820 ///
821 /// ICEs if any required fields were not set.
822 pub(super) fn build(&self, cx: &impl CodegenMethods<'tcx, Value = V>) -> OperandRef<'tcx, V> {
823 let OperandRefBuilder { val, layout } = *self;
824
825 // For something like `Option::<u32>::None`, it's expected that the
826 // payload scalar will not actually have been set, so this converts
827 // unset scalars to corresponding `undef` values so long as the scalar
828 // from the layout allows uninit.
829 let unwrap = |r: Either<V, abi::Scalar>| match r {
830 Either::Left(v) => v,
831 Either::Right(s) if s.is_uninit_valid() => {
832 let bty = cx.type_from_scalar(s);
833 cx.const_undef(bty)
834 }
835 Either::Right(_) => bug!("OperandRef::build called while fields are missing {self:?}"),
836 };
837
838 let val = match val {
839 OperandValueBuilder::ZeroSized => OperandValue::ZeroSized,
840 OperandValueBuilder::Immediate(v) => OperandValue::Immediate(unwrap(v)),
841 OperandValueBuilder::Pair(a, b) => OperandValue::Pair(unwrap(a), unwrap(b)),
842 OperandValueBuilder::Vector(v) => match v {
843 Either::Left(v) => OperandValue::Immediate(v),
844 Either::Right(())
845 if let BackendRepr::SimdVector { element, .. } = layout.backend_repr
846 && element.is_uninit_valid() =>
847 {
848 let bty = cx.immediate_backend_type(layout);
849 OperandValue::Immediate(cx.const_undef(bty))
850 }
851 Either::Right(()) => {
852 bug!("OperandRef::build called while fields are missing {self:?}")
853 }
854 },
855 };
856 OperandRef { val, layout, move_annotation: None }
857 }
858}
859
860/// Default size limit for move/copy annotations (in bytes). 64 bytes is a common size of a cache
861/// line, and the assumption is that anything this size or below is very cheap to move/copy, so only
862/// annotate copies larger than this.
863const MOVE_ANNOTATION_DEFAULT_LIMIT: u64 = 65;
864
865impl<'a, 'tcx, V: CodegenObject> OperandValue<V> {
866 /// Returns an `OperandValue` that's generally UB to use in any way.
867 ///
868 /// Depending on the `layout`, returns `ZeroSized` for ZSTs, an `Immediate` or
869 /// `Pair` containing poison value(s), or a `Ref` containing a poison pointer.
870 ///
871 /// Supports sized types only.
872 pub fn poison<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
873 bx: &mut Bx,
874 layout: TyAndLayout<'tcx>,
875 ) -> OperandValue<V> {
876 assert!(layout.is_sized());
877 if layout.is_zst() {
878 OperandValue::ZeroSized
879 } else if bx.cx().is_backend_immediate(layout) {
880 let ibty = bx.cx().immediate_backend_type(layout);
881 OperandValue::Immediate(bx.const_poison(ibty))
882 } else if bx.cx().is_backend_scalar_pair(layout) {
883 let ibty0 = bx.cx().scalar_pair_element_backend_type(layout, 0, true);
884 let ibty1 = bx.cx().scalar_pair_element_backend_type(layout, 1, true);
885 OperandValue::Pair(bx.const_poison(ibty0), bx.const_poison(ibty1))
886 } else {
887 let ptr = bx.cx().type_ptr();
888 OperandValue::Ref(PlaceValue::new_sized(bx.const_poison(ptr), layout.align.abi))
889 }
890 }
891
892 pub fn store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
893 self,
894 bx: &mut Bx,
895 dest: PlaceRef<'tcx, V>,
896 ) {
897 self.store_with_flags(bx, dest, MemFlags::empty());
898 }
899
900 pub fn volatile_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
901 self,
902 bx: &mut Bx,
903 dest: PlaceRef<'tcx, V>,
904 ) {
905 self.store_with_flags(bx, dest, MemFlags::VOLATILE);
906 }
907
908 pub fn unaligned_volatile_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
909 self,
910 bx: &mut Bx,
911 dest: PlaceRef<'tcx, V>,
912 ) {
913 self.store_with_flags(bx, dest, MemFlags::VOLATILE | MemFlags::UNALIGNED);
914 }
915
916 pub fn nontemporal_store<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
917 self,
918 bx: &mut Bx,
919 dest: PlaceRef<'tcx, V>,
920 ) {
921 self.store_with_flags(bx, dest, MemFlags::NONTEMPORAL);
922 }
923
924 pub(crate) fn store_with_flags<Bx: BuilderMethods<'a, 'tcx, Value = V>>(
925 self,
926 bx: &mut Bx,
927 dest: PlaceRef<'tcx, V>,
928 flags: MemFlags,
929 ) {
930 debug!("OperandRef::store: operand={:?}, dest={:?}", self, dest);
931 match self {
932 OperandValue::ZeroSized => {
933 // Avoid generating stores of zero-sized values, because the only way to have a
934 // zero-sized value is through `undef`/`poison`, and the store itself is useless.
935 }
936 OperandValue::Ref(val) => {
937 assert!(dest.layout.is_sized(), "cannot directly store unsized values");
938 if val.llextra.is_some() {
939 bug!("cannot directly store unsized values");
940 }
941 bx.typed_place_copy_with_flags(dest.val, val, dest.layout, flags);
942 }
943 OperandValue::Immediate(s) => {
944 let val = bx.from_immediate(s);
945 bx.store_with_flags(val, dest.val.llval, dest.val.align, flags);
946 }
947 OperandValue::Pair(a, b) => {
948 let BackendRepr::ScalarPair(a_scalar, b_scalar) = dest.layout.backend_repr else {
949 bug!("store_with_flags: invalid ScalarPair layout: {:#?}", dest.layout);
950 };
951 let b_offset = a_scalar.size(bx).align_to(b_scalar.align(bx).abi);
952
953 let val = bx.from_immediate(a);
954 let align = dest.val.align;
955 bx.store_with_flags(val, dest.val.llval, align, flags);
956
957 let llptr = bx.inbounds_ptradd(dest.val.llval, bx.const_usize(b_offset.bytes()));
958 let val = bx.from_immediate(b);
959 let align = dest.val.align.restrict_for_offset(b_offset);
960 bx.store_with_flags(val, llptr, align, flags);
961 }
962 }
963 }
964}
965
966impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
967 fn maybe_codegen_consume_direct(
968 &mut self,
969 bx: &mut Bx,
970 place_ref: mir::PlaceRef<'tcx>,
971 ) -> Option<OperandRef<'tcx, Bx::Value>> {
972 debug!("maybe_codegen_consume_direct(place_ref={:?})", place_ref);
973
974 match self.locals[place_ref.local] {
975 LocalRef::Operand(mut o) => {
976 // We only need to handle the projections that
977 // `LocalAnalyzer::process_place` let make it here.
978 for elem in place_ref.projection {
979 match *elem {
980 mir::ProjectionElem::Field(f, _) => {
981 assert!(
982 !o.layout.ty.is_any_ptr(),
983 "Bad PlaceRef: destructing pointers should use cast/PtrMetadata, \
984 but tried to access field {f:?} of pointer {o:?}",
985 );
986 o = o.extract_field(self, bx, f.index());
987 }
988 mir::PlaceElem::Downcast(_, vidx) => {
989 debug_assert_eq!(
990 o.layout.variants,
991 abi::Variants::Single { index: vidx },
992 );
993 let layout = o.layout.for_variant(bx.cx(), vidx);
994 o = OperandRef { layout, ..o }
995 }
996 _ => return None,
997 }
998 }
999
1000 Some(o)
1001 }
1002 LocalRef::PendingOperand => {
1003 bug!("use of {:?} before def", place_ref);
1004 }
1005 LocalRef::Place(..) | LocalRef::UnsizedPlace(..) => {
1006 // watch out for locals that do not have an
1007 // alloca; they are handled somewhat differently
1008 None
1009 }
1010 }
1011 }
1012
1013 pub fn codegen_consume(
1014 &mut self,
1015 bx: &mut Bx,
1016 place_ref: mir::PlaceRef<'tcx>,
1017 ) -> OperandRef<'tcx, Bx::Value> {
1018 debug!("codegen_consume(place_ref={:?})", place_ref);
1019
1020 let ty = self.monomorphized_place_ty(place_ref);
1021 let layout = bx.cx().layout_of(ty);
1022
1023 // ZSTs don't require any actual memory access.
1024 if layout.is_zst() {
1025 return OperandRef::zero_sized(layout);
1026 }
1027
1028 if let Some(o) = self.maybe_codegen_consume_direct(bx, place_ref) {
1029 return o;
1030 }
1031
1032 // for most places, to consume them we just load them
1033 // out from their home
1034 let place = self.codegen_place(bx, place_ref);
1035 bx.load_operand(place)
1036 }
1037
1038 pub fn codegen_operand(
1039 &mut self,
1040 bx: &mut Bx,
1041 operand: &mir::Operand<'tcx>,
1042 ) -> OperandRef<'tcx, Bx::Value> {
1043 debug!("codegen_operand(operand={:?})", operand);
1044
1045 match *operand {
1046 mir::Operand::Copy(ref place) | mir::Operand::Move(ref place) => {
1047 let kind = match operand {
1048 mir::Operand::Move(_) => LangItem::CompilerMove,
1049 mir::Operand::Copy(_) => LangItem::CompilerCopy,
1050 _ => unreachable!(),
1051 };
1052
1053 // Check if we should annotate this move/copy for profiling
1054 let move_annotation = self.move_copy_annotation_instance(bx, place.as_ref(), kind);
1055
1056 OperandRef { move_annotation, ..self.codegen_consume(bx, place.as_ref()) }
1057 }
1058
1059 mir::Operand::Constant(ref constant) => {
1060 let constant_ty = self.monomorphize(constant.ty());
1061 // Most SIMD vector constants should be passed as immediates.
1062 // (In particular, some intrinsics really rely on this.)
1063 if constant_ty.is_simd() {
1064 // However, some SIMD types do not actually use the vector ABI
1065 // (in particular, packed SIMD types do not). Ensure we exclude those.
1066 let layout = bx.layout_of(constant_ty);
1067 if let BackendRepr::SimdVector { .. } = layout.backend_repr {
1068 let (llval, ty) = self.immediate_const_vector(bx, constant);
1069 return OperandRef {
1070 val: OperandValue::Immediate(llval),
1071 layout: bx.layout_of(ty),
1072 move_annotation: None,
1073 };
1074 }
1075 }
1076 self.eval_mir_constant_to_operand(bx, constant)
1077 }
1078 }
1079 }
1080
1081 /// Creates an `Instance` for annotating a move/copy operation at codegen time.
1082 ///
1083 /// Returns `Some(instance)` if the operation should be annotated with debug info, `None`
1084 /// otherwise. The instance represents a monomorphized `compiler_move<T, SIZE>` or
1085 /// `compiler_copy<T, SIZE>` function that can be used to create debug scopes.
1086 ///
1087 /// There are a number of conditions that must be met for an annotation to be created, but aside
1088 /// from the basics (annotation is enabled, we're generating debuginfo), the primary concern is
1089 /// moves/copies which could result in a real `memcpy`. So we check for the size limit, but also
1090 /// that the underlying representation of the type is in memory.
1091 fn move_copy_annotation_instance(
1092 &self,
1093 bx: &Bx,
1094 place: mir::PlaceRef<'tcx>,
1095 kind: LangItem,
1096 ) -> Option<ty::Instance<'tcx>> {
1097 let tcx = bx.tcx();
1098 let sess = tcx.sess;
1099
1100 // Skip if we're not generating debuginfo
1101 if sess.opts.debuginfo == DebugInfo::None {
1102 return None;
1103 }
1104
1105 // Check if annotation is enabled and get size limit (otherwise skip)
1106 let size_limit = match sess.opts.unstable_opts.annotate_moves {
1107 AnnotateMoves::Disabled => return None,
1108 AnnotateMoves::Enabled(None) => MOVE_ANNOTATION_DEFAULT_LIMIT,
1109 AnnotateMoves::Enabled(Some(limit)) => limit,
1110 };
1111
1112 let ty = self.monomorphized_place_ty(place);
1113 let layout = bx.cx().layout_of(ty);
1114 let ty_size = layout.size.bytes();
1115
1116 // Only annotate if type has a memory representation and exceeds size limit (and has a
1117 // non-zero size)
1118 if layout.is_zst()
1119 || ty_size < size_limit
1120 || !matches!(layout.backend_repr, BackendRepr::Memory { .. })
1121 {
1122 return None;
1123 }
1124
1125 // Look up the DefId for compiler_move or compiler_copy lang item
1126 let def_id = tcx.lang_items().get(kind)?;
1127
1128 // Create generic args: compiler_move<T, SIZE> or compiler_copy<T, SIZE>
1129 let size_const = ty::Const::from_target_usize(tcx, ty_size);
1130 let generic_args = tcx.mk_args(&[ty.into(), size_const.into()]);
1131
1132 // Create the Instance
1133 let typing_env = self.mir.typing_env(tcx);
1134 let instance = ty::Instance::expect_resolve(
1135 tcx,
1136 typing_env,
1137 def_id,
1138 generic_args,
1139 rustc_span::DUMMY_SP, // span only used for error messages
1140 );
1141
1142 Some(instance)
1143 }
1144}