Skip to content

Commit fa3155a

Browse files
committed
Auto merge of #147197 - matthiaskrgr:rollup-du5e4pv, r=matthiaskrgr
Rollup of 6 pull requests Successful merges: - #142506 (Add `Path::has_trailing_sep` and related methods) - #146886 (Add repr(align(2)) to RcInner and ArcInner) - #147166 (several small `proc_macro` cleanups) - #147172 (bootstrap: build bootstrap docs with in-tree rustdoc) - #147181 (cg_llvm: Replace enum `MetadataType` with a list of `MetadataKindId` constants) - #147187 (remove unnecessary test directives) r? `@ghost` `@rustbot` modify labels: rollup
2 parents 42d009c + 561e02c commit fa3155a

File tree

19 files changed

+409
-142
lines changed

19 files changed

+409
-142
lines changed

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ use smallvec::SmallVec;
3434
use crate::back::write::to_llvm_code_model;
3535
use crate::callee::get_fn;
3636
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
37-
use crate::llvm::Metadata;
37+
use crate::llvm::{Metadata, MetadataKindId};
3838
use crate::type_::Type;
3939
use crate::value::Value;
4040
use crate::{attributes, common, coverageinfo, debuginfo, llvm, llvm_util};
@@ -1006,11 +1006,11 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
10061006
pub(crate) fn set_metadata<'a>(
10071007
&self,
10081008
val: &'a Value,
1009-
kind_id: impl Into<llvm::MetadataKindId>,
1009+
kind_id: MetadataKindId,
10101010
md: &'ll Metadata,
10111011
) {
10121012
let node = self.get_metadata_value(md);
1013-
llvm::LLVMSetMetadata(val, kind_id.into(), node);
1013+
llvm::LLVMSetMetadata(val, kind_id, node);
10141014
}
10151015
}
10161016

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1611,16 +1611,12 @@ pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
16111611
let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
16121612
llvm::LLVMRustGlobalAddMetadata(
16131613
vtable,
1614-
llvm::MD_type as c_uint,
1614+
llvm::MD_type,
16151615
llvm::LLVMMDNodeInContext2(cx.llcx, v.as_ptr(), v.len()),
16161616
);
16171617
let vcall_visibility = llvm::LLVMValueAsMetadata(cx.const_u64(vcall_visibility as u64));
16181618
let vcall_visibility_metadata = llvm::LLVMMDNodeInContext2(cx.llcx, &vcall_visibility, 1);
1619-
llvm::LLVMGlobalSetMetadata(
1620-
vtable,
1621-
llvm::MetadataType::MD_vcall_visibility as c_uint,
1622-
vcall_visibility_metadata,
1623-
);
1619+
llvm::LLVMGlobalSetMetadata(vtable, llvm::MD_vcall_visibility, vcall_visibility_metadata);
16241620
}
16251621
}
16261622

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

Lines changed: 7 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ use super::debuginfo::{
2929
DITemplateTypeParameter, DIType, DebugEmissionKind, DebugNameTableKind,
3030
};
3131
use crate::llvm;
32+
use crate::llvm::MetadataKindId;
3233

3334
/// In the LLVM-C API, boolean values are passed as `typedef int LLVMBool`,
3435
/// which has a different ABI from Rust or C++ `bool`.
@@ -513,31 +514,6 @@ pub(crate) enum FileType {
513514
ObjectFile,
514515
}
515516

516-
/// LLVMMetadataType
517-
#[derive(Copy, Clone)]
518-
#[repr(C)]
519-
#[expect(dead_code, reason = "Some variants are unused, but are kept to match LLVM-C")]
520-
pub(crate) enum MetadataType {
521-
MD_dbg = 0,
522-
MD_tbaa = 1,
523-
MD_prof = 2,
524-
MD_fpmath = 3,
525-
MD_range = 4,
526-
MD_tbaa_struct = 5,
527-
MD_invariant_load = 6,
528-
MD_alias_scope = 7,
529-
MD_noalias = 8,
530-
MD_nontemporal = 9,
531-
MD_mem_parallel_loop_access = 10,
532-
MD_nonnull = 11,
533-
MD_unpredictable = 15,
534-
MD_align = 17,
535-
MD_type = 19,
536-
MD_vcall_visibility = 28,
537-
MD_noundef = 29,
538-
MD_kcfi_type = 36,
539-
}
540-
541517
/// Must match the layout of `LLVMInlineAsmDialect`.
542518
#[derive(Copy, Clone, PartialEq)]
543519
#[repr(C)]
@@ -1035,16 +1011,6 @@ pub(crate) type GetSymbolsCallback =
10351011
unsafe extern "C" fn(*mut c_void, *const c_char) -> *mut c_void;
10361012
pub(crate) type GetSymbolsErrorCallback = unsafe extern "C" fn(*const c_char) -> *mut c_void;
10371013

1038-
#[derive(Copy, Clone)]
1039-
#[repr(transparent)]
1040-
pub(crate) struct MetadataKindId(c_uint);
1041-
1042-
impl From<MetadataType> for MetadataKindId {
1043-
fn from(value: MetadataType) -> Self {
1044-
Self(value as c_uint)
1045-
}
1046-
}
1047-
10481014
unsafe extern "C" {
10491015
// Create and destroy contexts.
10501016
pub(crate) fn LLVMContextDispose(C: &'static mut Context);
@@ -1139,7 +1105,11 @@ unsafe extern "C" {
11391105
pub(crate) fn LLVMSetValueName2(Val: &Value, Name: *const c_char, NameLen: size_t);
11401106
pub(crate) fn LLVMReplaceAllUsesWith<'a>(OldVal: &'a Value, NewVal: &'a Value);
11411107
pub(crate) safe fn LLVMSetMetadata<'a>(Val: &'a Value, KindID: MetadataKindId, Node: &'a Value);
1142-
pub(crate) fn LLVMGlobalSetMetadata<'a>(Val: &'a Value, KindID: c_uint, Metadata: &'a Metadata);
1108+
pub(crate) fn LLVMGlobalSetMetadata<'a>(
1109+
Val: &'a Value,
1110+
KindID: MetadataKindId,
1111+
Metadata: &'a Metadata,
1112+
);
11431113
pub(crate) safe fn LLVMValueAsMetadata(Node: &Value) -> &Metadata;
11441114

11451115
// Operations on constants of any type
@@ -2059,7 +2029,7 @@ unsafe extern "C" {
20592029
// Operations on all values
20602030
pub(crate) fn LLVMRustGlobalAddMetadata<'a>(
20612031
Val: &'a Value,
2062-
KindID: c_uint,
2032+
KindID: MetadataKindId,
20632033
Metadata: &'a Metadata,
20642034
);
20652035
pub(crate) fn LLVMRustIsNonGVFunctionPointerTy(Val: &Value) -> bool;
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
use libc::c_uint;
2+
3+
pub(crate) use self::fixed_kinds::*;
4+
5+
#[derive(Copy, Clone)]
6+
#[repr(transparent)]
7+
pub(crate) struct MetadataKindId(c_uint);
8+
9+
macro_rules! declare_fixed_metadata_kinds {
10+
(
11+
$(
12+
FIXED_MD_KIND($variant:ident, $value:literal)
13+
)*
14+
) => {
15+
// Use a submodule to group all declarations into one `#[expect(..)]`.
16+
#[expect(dead_code)]
17+
mod fixed_kinds {
18+
use super::MetadataKindId;
19+
$(
20+
#[expect(non_upper_case_globals)]
21+
pub(crate) const $variant: MetadataKindId = MetadataKindId($value);
22+
)*
23+
}
24+
};
25+
}
26+
27+
// Must be kept in sync with the corresponding static assertions in `RustWrapper.cpp`.
28+
declare_fixed_metadata_kinds! {
29+
FIXED_MD_KIND(MD_dbg, 0)
30+
FIXED_MD_KIND(MD_tbaa, 1)
31+
FIXED_MD_KIND(MD_prof, 2)
32+
FIXED_MD_KIND(MD_fpmath, 3)
33+
FIXED_MD_KIND(MD_range, 4)
34+
FIXED_MD_KIND(MD_tbaa_struct, 5)
35+
FIXED_MD_KIND(MD_invariant_load, 6)
36+
FIXED_MD_KIND(MD_alias_scope, 7)
37+
FIXED_MD_KIND(MD_noalias, 8)
38+
FIXED_MD_KIND(MD_nontemporal, 9)
39+
FIXED_MD_KIND(MD_mem_parallel_loop_access, 10)
40+
FIXED_MD_KIND(MD_nonnull, 11)
41+
FIXED_MD_KIND(MD_dereferenceable, 12)
42+
FIXED_MD_KIND(MD_dereferenceable_or_null, 13)
43+
FIXED_MD_KIND(MD_make_implicit, 14)
44+
FIXED_MD_KIND(MD_unpredictable, 15)
45+
FIXED_MD_KIND(MD_invariant_group, 16)
46+
FIXED_MD_KIND(MD_align, 17)
47+
FIXED_MD_KIND(MD_loop, 18)
48+
FIXED_MD_KIND(MD_type, 19)
49+
FIXED_MD_KIND(MD_section_prefix, 20)
50+
FIXED_MD_KIND(MD_absolute_symbol, 21)
51+
FIXED_MD_KIND(MD_associated, 22)
52+
FIXED_MD_KIND(MD_callees, 23)
53+
FIXED_MD_KIND(MD_irr_loop, 24)
54+
FIXED_MD_KIND(MD_access_group, 25)
55+
FIXED_MD_KIND(MD_callback, 26)
56+
FIXED_MD_KIND(MD_preserve_access_index, 27)
57+
FIXED_MD_KIND(MD_vcall_visibility, 28)
58+
FIXED_MD_KIND(MD_noundef, 29)
59+
FIXED_MD_KIND(MD_annotation, 30)
60+
FIXED_MD_KIND(MD_nosanitize, 31)
61+
FIXED_MD_KIND(MD_func_sanitize, 32)
62+
FIXED_MD_KIND(MD_exclude, 33)
63+
FIXED_MD_KIND(MD_memprof, 34)
64+
FIXED_MD_KIND(MD_callsite, 35)
65+
FIXED_MD_KIND(MD_kcfi_type, 36)
66+
FIXED_MD_KIND(MD_pcsections, 37)
67+
FIXED_MD_KIND(MD_DIAssignID, 38)
68+
FIXED_MD_KIND(MD_coro_outside_frame, 39)
69+
FIXED_MD_KIND(MD_mmra, 40)
70+
FIXED_MD_KIND(MD_noalias_addrspace, 41)
71+
}

compiler/rustc_codegen_llvm/src/llvm/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,14 @@ use rustc_llvm::RustString;
1111

1212
pub(crate) use self::CallConv::*;
1313
pub(crate) use self::CodeGenOptSize::*;
14-
pub(crate) use self::MetadataType::*;
1514
pub(crate) use self::ffi::*;
15+
pub(crate) use self::metadata_kind::*;
1616
use crate::common::AsCCharPtr;
1717

1818
pub(crate) mod diagnostic;
1919
pub(crate) mod enzyme_ffi;
2020
mod ffi;
21+
mod metadata_kind;
2122

2223
pub(crate) use self::enzyme_ffi::*;
2324

compiler/rustc_codegen_llvm/src/type_.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
306306
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
307307
llvm::LLVMRustGlobalAddMetadata(
308308
function,
309-
llvm::MD_type as c_uint,
309+
llvm::MD_type,
310310
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
311311
)
312312
}
@@ -318,7 +318,7 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
318318
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
319319
llvm::LLVMGlobalSetMetadata(
320320
function,
321-
llvm::MD_type as c_uint,
321+
llvm::MD_type,
322322
llvm::LLVMMDNodeInContext2(self.llcx, v.as_ptr(), v.len()),
323323
)
324324
}
@@ -333,7 +333,7 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
333333
unsafe {
334334
llvm::LLVMRustGlobalAddMetadata(
335335
function,
336-
llvm::MD_kcfi_type as c_uint,
336+
llvm::MD_kcfi_type,
337337
llvm::LLVMMDNodeInContext2(
338338
self.llcx,
339339
&llvm::LLVMValueAsMetadata(kcfi_type_metadata),
@@ -348,7 +348,7 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
348348
unsafe {
349349
llvm::LLVMGlobalSetMetadata(
350350
function,
351-
llvm::MD_kcfi_type as c_uint,
351+
llvm::MD_kcfi_type,
352352
llvm::LLVMMDNodeInContext2(
353353
self.llcx,
354354
&llvm::LLVMValueAsMetadata(kcfi_type_metadata),

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1824,3 +1824,55 @@ extern "C" size_t LLVMRustEnzymeGetMaxTypeDepth() {
18241824
return 6; // Default fallback depth
18251825
}
18261826
#endif
1827+
1828+
// Statically assert that the fixed metadata kind IDs declared in
1829+
// `metadata_kind.rs` match the ones actually used by LLVM.
1830+
#define FIXED_MD_KIND(VARIANT, VALUE) \
1831+
static_assert(::llvm::LLVMContext::VARIANT == VALUE);
1832+
// Must be kept in sync with the corresponding list in `metadata_kind.rs`.
1833+
FIXED_MD_KIND(MD_dbg, 0)
1834+
FIXED_MD_KIND(MD_tbaa, 1)
1835+
FIXED_MD_KIND(MD_prof, 2)
1836+
FIXED_MD_KIND(MD_fpmath, 3)
1837+
FIXED_MD_KIND(MD_range, 4)
1838+
FIXED_MD_KIND(MD_tbaa_struct, 5)
1839+
FIXED_MD_KIND(MD_invariant_load, 6)
1840+
FIXED_MD_KIND(MD_alias_scope, 7)
1841+
FIXED_MD_KIND(MD_noalias, 8)
1842+
FIXED_MD_KIND(MD_nontemporal, 9)
1843+
FIXED_MD_KIND(MD_mem_parallel_loop_access, 10)
1844+
FIXED_MD_KIND(MD_nonnull, 11)
1845+
FIXED_MD_KIND(MD_dereferenceable, 12)
1846+
FIXED_MD_KIND(MD_dereferenceable_or_null, 13)
1847+
FIXED_MD_KIND(MD_make_implicit, 14)
1848+
FIXED_MD_KIND(MD_unpredictable, 15)
1849+
FIXED_MD_KIND(MD_invariant_group, 16)
1850+
FIXED_MD_KIND(MD_align, 17)
1851+
FIXED_MD_KIND(MD_loop, 18)
1852+
FIXED_MD_KIND(MD_type, 19)
1853+
FIXED_MD_KIND(MD_section_prefix, 20)
1854+
FIXED_MD_KIND(MD_absolute_symbol, 21)
1855+
FIXED_MD_KIND(MD_associated, 22)
1856+
FIXED_MD_KIND(MD_callees, 23)
1857+
FIXED_MD_KIND(MD_irr_loop, 24)
1858+
FIXED_MD_KIND(MD_access_group, 25)
1859+
FIXED_MD_KIND(MD_callback, 26)
1860+
FIXED_MD_KIND(MD_preserve_access_index, 27)
1861+
FIXED_MD_KIND(MD_vcall_visibility, 28)
1862+
FIXED_MD_KIND(MD_noundef, 29)
1863+
FIXED_MD_KIND(MD_annotation, 30)
1864+
FIXED_MD_KIND(MD_nosanitize, 31)
1865+
FIXED_MD_KIND(MD_func_sanitize, 32)
1866+
FIXED_MD_KIND(MD_exclude, 33)
1867+
FIXED_MD_KIND(MD_memprof, 34)
1868+
FIXED_MD_KIND(MD_callsite, 35)
1869+
FIXED_MD_KIND(MD_kcfi_type, 36)
1870+
FIXED_MD_KIND(MD_pcsections, 37)
1871+
FIXED_MD_KIND(MD_DIAssignID, 38)
1872+
FIXED_MD_KIND(MD_coro_outside_frame, 39)
1873+
FIXED_MD_KIND(MD_mmra, 40)
1874+
FIXED_MD_KIND(MD_noalias_addrspace, 41)
1875+
// If some fixed metadata kinds are not present and consistent in all supported
1876+
// LLVM versions, it's fine to omit them from this list; in that case Rust-side
1877+
// code cannot declare them as fixed IDs and must look them up by name instead.
1878+
#undef FIXED_MD_KIND

library/alloc/src/rc.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -277,7 +277,9 @@ use crate::vec::Vec;
277277
// This is repr(C) to future-proof against possible field-reordering, which
278278
// would interfere with otherwise safe [into|from]_raw() of transmutable
279279
// inner types.
280-
#[repr(C)]
280+
// repr(align(2)) (forcing alignment to at least 2) is required because usize
281+
// has 1-byte alignment on AVR.
282+
#[repr(C, align(2))]
281283
struct RcInner<T: ?Sized> {
282284
strong: Cell<usize>,
283285
weak: Cell<usize>,

library/alloc/src/sync.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -341,7 +341,7 @@ pub struct Weak<
341341
// but it is not necessarily a valid pointer.
342342
// `Weak::new` sets this to `usize::MAX` so that it doesn’t need
343343
// to allocate space on the heap. That's not a value a real pointer
344-
// will ever have because RcInner has alignment at least 2.
344+
// will ever have because ArcInner has alignment at least 2.
345345
ptr: NonNull<ArcInner<T>>,
346346
alloc: A,
347347
}
@@ -366,7 +366,9 @@ impl<T: ?Sized, A: Allocator> fmt::Debug for Weak<T, A> {
366366
// This is repr(C) to future-proof against possible field-reordering, which
367367
// would interfere with otherwise safe [into|from]_raw() of transmutable
368368
// inner types.
369-
#[repr(C)]
369+
// Unlike RcInner, repr(align(2)) is not strictly required because atomic types
370+
// have the alignment same as its size, but we use it for consistency and clarity.
371+
#[repr(C, align(2))]
370372
struct ArcInner<T: ?Sized> {
371373
strong: Atomic<usize>,
372374

@@ -1613,9 +1615,9 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
16131615
pub fn as_ptr(this: &Self) -> *const T {
16141616
let ptr: *mut ArcInner<T> = NonNull::as_ptr(this.ptr);
16151617

1616-
// SAFETY: This cannot go through Deref::deref or RcInnerPtr::inner because
1618+
// SAFETY: This cannot go through Deref::deref or ArcInnerPtr::inner because
16171619
// this is required to retain raw/mut provenance such that e.g. `get_mut` can
1618-
// write through the pointer after the Rc is recovered through `from_raw`.
1620+
// write through the pointer after the Arc is recovered through `from_raw`.
16191621
unsafe { &raw mut (*ptr).data }
16201622
}
16211623

@@ -2450,7 +2452,7 @@ impl<T: ?Sized, A: Allocator> Arc<T, A> {
24502452
/// If any other `Arc` or [`Weak`] pointers to the same allocation exist, then
24512453
/// they must not be dereferenced or have active borrows for the duration
24522454
/// of the returned borrow, and their inner type must be exactly the same as the
2453-
/// inner type of this Rc (including lifetimes). This is trivially the case if no
2455+
/// inner type of this Arc (including lifetimes). This is trivially the case if no
24542456
/// such pointers exist, for example immediately after `Arc::new`.
24552457
///
24562458
/// # Examples
@@ -3022,7 +3024,7 @@ impl<T: ?Sized, A: Allocator> Weak<T, A> {
30223024
// Otherwise, we're guaranteed the pointer came from a nondangling Weak.
30233025
// SAFETY: data_offset is safe to call, as ptr references a real (potentially dropped) T.
30243026
let offset = unsafe { data_offset(ptr) };
3025-
// Thus, we reverse the offset to get the whole RcInner.
3027+
// Thus, we reverse the offset to get the whole ArcInner.
30263028
// SAFETY: the pointer originated from a Weak, so this offset is safe.
30273029
unsafe { ptr.byte_sub(offset) as *mut ArcInner<T> }
30283030
};
@@ -4015,7 +4017,7 @@ impl<T: ?Sized, A: Allocator> Unpin for Arc<T, A> {}
40154017
/// valid instance of T, but the T is allowed to be dropped.
40164018
unsafe fn data_offset<T: ?Sized>(ptr: *const T) -> usize {
40174019
// Align the unsized value to the end of the ArcInner.
4018-
// Because RcInner is repr(C), it will always be the last field in memory.
4020+
// Because ArcInner is repr(C), it will always be the last field in memory.
40194021
// SAFETY: since the only unsized types possible are slices, trait objects,
40204022
// and extern types, the input safety requirement is currently enough to
40214023
// satisfy the requirements of align_of_val_raw; this is an implementation

0 commit comments

Comments
 (0)