Skip to content

Commit 7f95f04

Browse files
committed
Eliminate all direct uses of LLVMMDStringInContext2
1 parent 56d22cd commit 7f95f04

File tree

9 files changed

+26
-28
lines changed

9 files changed

+26
-28
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -302,8 +302,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
302302
return;
303303
}
304304

305-
let id_str = "branch_weights";
306-
let id = self.cx.create_metadata(id_str.into());
305+
let id = self.cx.create_metadata(b"branch_weights");
307306

308307
// For switch instructions with 2 targets, the `llvm.expect` intrinsic is used.
309308
// This function handles switch instructions with more than 2 targets and it needs to
@@ -1718,7 +1717,7 @@ impl<'a, 'll, 'tcx> Builder<'a, 'll, 'tcx> {
17181717
} else {
17191718
cfi::typeid_for_fnabi(self.tcx, fn_abi, options)
17201719
};
1721-
let typeid_metadata = self.cx.create_metadata(typeid);
1720+
let typeid_metadata = self.cx.create_metadata(typeid.as_bytes());
17221721
let dbg_loc = self.get_dbg_loc();
17231722

17241723
// Test whether the function pointer is associated with the type identifier using the

compiler/rustc_codegen_llvm/src/builder/autodiff.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,12 @@ fn match_args_from_caller_to_enzyme<'ll>(
7676
outer_pos = 1;
7777
}
7878

79-
let enzyme_const = cx.create_metadata("enzyme_const".to_string());
80-
let enzyme_out = cx.create_metadata("enzyme_out".to_string());
81-
let enzyme_dup = cx.create_metadata("enzyme_dup".to_string());
82-
let enzyme_dupv = cx.create_metadata("enzyme_dupv".to_string());
83-
let enzyme_dupnoneed = cx.create_metadata("enzyme_dupnoneed".to_string());
84-
let enzyme_dupnoneedv = cx.create_metadata("enzyme_dupnoneedv".to_string());
79+
let enzyme_const = cx.create_metadata(b"enzyme_const");
80+
let enzyme_out = cx.create_metadata(b"enzyme_out");
81+
let enzyme_dup = cx.create_metadata(b"enzyme_dup");
82+
let enzyme_dupv = cx.create_metadata(b"enzyme_dupv");
83+
let enzyme_dupnoneed = cx.create_metadata(b"enzyme_dupnoneed");
84+
let enzyme_dupnoneedv = cx.create_metadata(b"enzyme_dupnoneedv");
8585

8686
while activity_pos < inputs.len() {
8787
let diff_activity = inputs[activity_pos as usize];
@@ -378,12 +378,12 @@ fn generate_enzyme_call<'ll>(
378378
let mut args = Vec::with_capacity(num_args as usize + 1);
379379
args.push(fn_to_diff);
380380

381-
let enzyme_primal_ret = cx.create_metadata("enzyme_primal_return".to_string());
381+
let enzyme_primal_ret = cx.create_metadata(b"enzyme_primal_return");
382382
if matches!(attrs.ret_activity, DiffActivity::Dual | DiffActivity::Active) {
383383
args.push(cx.get_metadata_value(enzyme_primal_ret));
384384
}
385385
if attrs.width > 1 {
386-
let enzyme_width = cx.create_metadata("enzyme_width".to_string());
386+
let enzyme_width = cx.create_metadata(b"enzyme_width");
387387
args.push(cx.get_metadata_value(enzyme_width));
388388
args.push(cx.get_const_int(cx.type_i64(), attrs.width as u64));
389389
}

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use rustc_middle::ty::{self, Instance};
1717
use rustc_middle::{bug, span_bug};
1818
use tracing::{debug, instrument, trace};
1919

20-
use crate::common::{AsCCharPtr, CodegenCx};
20+
use crate::common::CodegenCx;
2121
use crate::errors::SymbolAlreadyDefined;
2222
use crate::type_::Type;
2323
use crate::type_of::LayoutLlvmExt;
@@ -477,16 +477,14 @@ impl<'ll> CodegenCx<'ll, '_> {
477477
.unwrap_or(true)
478478
{
479479
if let Some(section) = attrs.link_section {
480-
let section = self.create_metadata(section.as_str().into());
480+
let section = self.create_metadata(section.as_str().as_bytes());
481481
assert!(alloc.provenance().ptrs().is_empty());
482482

483483
// The `inspect` method is okay here because we checked for provenance, and
484484
// because we are doing this access to inspect the final interpreter state (not
485485
// as part of the interpreter execution).
486486
let bytes = alloc.inspect_with_uninit_and_ptr_outside_interpreter(0..alloc.len());
487-
let alloc = unsafe {
488-
llvm::LLVMMDStringInContext2(self.llcx, bytes.as_c_char_ptr(), bytes.len())
489-
};
487+
let alloc = self.create_metadata(bytes);
490488
let data = [section, alloc];
491489
let meta =
492490
unsafe { llvm::LLVMMDNodeInContext2(self.llcx, data.as_ptr(), data.len()) };

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,7 @@ pub(crate) unsafe fn create_module<'ll>(
475475
let rustc_producer =
476476
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
477477

478-
let name_metadata = cx.create_metadata(rustc_producer);
478+
let name_metadata = cx.create_metadata(rustc_producer.as_bytes());
479479

480480
unsafe {
481481
llvm::LLVMAddNamedMetadataOperand(
@@ -695,7 +695,7 @@ impl<'ll, CX: Borrow<SCx<'ll>>> GenericCx<'ll, CX> {
695695
}
696696
}
697697

698-
pub(crate) fn create_metadata(&self, name: String) -> &'ll Metadata {
698+
pub(crate) fn create_metadata(&self, name: &[u8]) -> &'ll Metadata {
699699
unsafe {
700700
llvm::LLVMMDStringInContext2(self.llcx(), name.as_ptr() as *const c_char, name.len())
701701
}

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1582,7 +1582,7 @@ pub(crate) fn apply_vcall_visibility_metadata<'ll, 'tcx>(
15821582
};
15831583

15841584
let trait_ref_typeid = typeid_for_trait_ref(cx.tcx, trait_ref);
1585-
let typeid = cx.create_metadata(trait_ref_typeid);
1585+
let typeid = cx.create_metadata(trait_ref_typeid.as_bytes());
15861586

15871587
unsafe {
15881588
let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];

compiler/rustc_codegen_llvm/src/declare.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
176176
{
177177
let typeid = cfi::typeid_for_instance(self.tcx, instance, options);
178178
if typeids.insert(typeid.clone()) {
179-
self.add_type_metadata(llfn, typeid);
179+
self.add_type_metadata(llfn, typeid.as_bytes());
180180
}
181181
}
182182
} else {
@@ -189,7 +189,7 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
189189
.map(cfi::TypeIdOptions::from_iter)
190190
{
191191
let typeid = cfi::typeid_for_fnabi(self.tcx, fn_abi, options);
192-
self.add_type_metadata(llfn, typeid);
192+
self.add_type_metadata(llfn, typeid.as_bytes());
193193
}
194194
}
195195
}

compiler/rustc_codegen_llvm/src/type_.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ impl<'ll, 'tcx> LayoutTypeCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
298298
}
299299

300300
impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
301-
fn add_type_metadata(&self, function: &'ll Value, typeid: String) {
301+
fn add_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
302302
let typeid_metadata = self.create_metadata(typeid);
303303
unsafe {
304304
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
@@ -310,7 +310,7 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
310310
}
311311
}
312312

313-
fn set_type_metadata(&self, function: &'ll Value, typeid: String) {
313+
fn set_type_metadata(&self, function: &'ll Value, typeid: &[u8]) {
314314
let typeid_metadata = self.create_metadata(typeid);
315315
unsafe {
316316
let v = [llvm::LLVMValueAsMetadata(self.const_usize(0)), typeid_metadata];
@@ -322,7 +322,7 @@ impl<'ll, 'tcx> TypeMembershipCodegenMethods<'tcx> for CodegenCx<'ll, 'tcx> {
322322
}
323323
}
324324

325-
fn typeid_metadata(&self, typeid: String) -> Option<&'ll Metadata> {
325+
fn typeid_metadata(&self, typeid: &[u8]) -> Option<&'ll Metadata> {
326326
Some(self.create_metadata(typeid))
327327
}
328328

compiler/rustc_codegen_ssa/src/meth.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,8 @@ pub(crate) fn load_vtable<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>>(
139139
&& bx.cx().sess().lto() == Lto::Fat
140140
{
141141
if let Some(trait_ref) = dyn_trait_in_self(bx.tcx(), ty) {
142-
let typeid = bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), trait_ref)).unwrap();
142+
let typeid =
143+
bx.typeid_metadata(typeid_for_trait_ref(bx.tcx(), trait_ref).as_bytes()).unwrap();
143144
let func = bx.type_checked_load(llvtable, vtable_byte_offset, typeid);
144145
return func;
145146
} else if nonnull {

compiler/rustc_codegen_ssa/src/traits/type_.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -154,9 +154,9 @@ pub trait LayoutTypeCodegenMethods<'tcx>: BackendTypes {
154154
// For backends that support CFI using type membership (i.e., testing whether a given pointer is
155155
// associated with a type identifier).
156156
pub trait TypeMembershipCodegenMethods<'tcx>: BackendTypes {
157-
fn add_type_metadata(&self, _function: Self::Function, _typeid: String) {}
158-
fn set_type_metadata(&self, _function: Self::Function, _typeid: String) {}
159-
fn typeid_metadata(&self, _typeid: String) -> Option<Self::Metadata> {
157+
fn add_type_metadata(&self, _function: Self::Function, _typeid: &[u8]) {}
158+
fn set_type_metadata(&self, _function: Self::Function, _typeid: &[u8]) {}
159+
fn typeid_metadata(&self, _typeid: &[u8]) -> Option<Self::Metadata> {
160160
None
161161
}
162162
fn add_kcfi_type_metadata(&self, _function: Self::Function, _typeid: u32) {}

0 commit comments

Comments
 (0)