Skip to content

Commit 75fd78f

Browse files
authored
Unrolled build for #147181
Rollup merge of #147181 - Zalathar:fixed-metadata, r=petrochenkov cg_llvm: Replace enum `MetadataType` with a list of `MetadataKindId` constants The metadata kind ID values declared in `MetadataType` are not part of the LLVM-C API, and are not machine-checked. If a value that we use ever goes out of sync with LLVM, the resulting bugs could be difficult to track down. And the existing values lack any clear indication of what LLVM declarations they correspond to. On top of that, we currently have another way of expressing metadata kind IDs in the form of `MetadataKindId`, which creates confusing inconsistency in LLVM bindings. This PR therefore consolidates all usage of “fixed” metadata kind IDs into one list of `MetadataKindId` constants, which is backed by static assertions in our C++ code that match them up with named anonymous-enum variants in `llvm::LLVMContext`.
2 parents 42d009c + cc6329a commit 75fd78f

File tree

7 files changed

+141
-51
lines changed

7 files changed

+141
-51
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

0 commit comments

Comments
 (0)