Skip to content

Commit 56d22cd

Browse files
committed
Use context methods instead of directly calling FFI
1 parent b9baf63 commit 56d22cd

File tree

4 files changed

+9
-25
lines changed

4 files changed

+9
-25
lines changed

compiler/rustc_codegen_llvm/src/builder.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,7 @@ impl<'a, 'll, 'tcx> BuilderMethods<'a, 'tcx> for Builder<'a, 'll, 'tcx> {
303303
}
304304

305305
let id_str = "branch_weights";
306-
let id = unsafe {
307-
llvm::LLVMMDStringInContext2(self.cx.llcx, id_str.as_ptr().cast(), id_str.len())
308-
};
306+
let id = self.cx.create_metadata(id_str.into());
309307

310308
// For switch instructions with 2 targets, the `llvm.expect` intrinsic is used.
311309
// This function handles switch instructions with more than 2 targets and it needs to

compiler/rustc_codegen_llvm/src/consts.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -477,13 +477,7 @@ impl<'ll> CodegenCx<'ll, '_> {
477477
.unwrap_or(true)
478478
{
479479
if let Some(section) = attrs.link_section {
480-
let section = unsafe {
481-
llvm::LLVMMDStringInContext2(
482-
self.llcx,
483-
section.as_str().as_c_char_ptr(),
484-
section.as_str().len(),
485-
)
486-
};
480+
let section = self.create_metadata(section.as_str().into());
487481
assert!(alloc.provenance().ptrs().is_empty());
488482

489483
// The `inspect` method is okay here because we checked for provenance, and

compiler/rustc_codegen_llvm/src/context.rs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ use smallvec::SmallVec;
3434

3535
use crate::back::write::to_llvm_code_model;
3636
use crate::callee::get_fn;
37-
use crate::common::AsCCharPtr;
3837
use crate::debuginfo::metadata::apply_vcall_visibility_metadata;
3938
use crate::llvm::Metadata;
4039
use crate::type_::Type;
@@ -169,6 +168,8 @@ pub(crate) unsafe fn create_module<'ll>(
169168
let mod_name = SmallCStr::new(mod_name);
170169
let llmod = unsafe { llvm::LLVMModuleCreateWithNameInContext(mod_name.as_ptr(), llcx) };
171170

171+
let cx = SimpleCx::new(llmod, llcx, tcx.data_layout.pointer_size());
172+
172173
let mut target_data_layout = sess.target.data_layout.to_string();
173174
let llvm_version = llvm_util::get_version();
174175

@@ -474,18 +475,13 @@ pub(crate) unsafe fn create_module<'ll>(
474475
let rustc_producer =
475476
format!("rustc version {}", option_env!("CFG_VERSION").expect("CFG_VERSION"));
476477

477-
let name_metadata = unsafe {
478-
llvm::LLVMMDStringInContext2(
479-
llcx,
480-
rustc_producer.as_c_char_ptr(),
481-
rustc_producer.as_bytes().len(),
482-
)
483-
};
478+
let name_metadata = cx.create_metadata(rustc_producer);
479+
484480
unsafe {
485481
llvm::LLVMAddNamedMetadataOperand(
486482
llmod,
487483
c"llvm.ident".as_ptr(),
488-
&llvm::LLVMMetadataAsValue(llcx, llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
484+
&cx.get_metadata_value(llvm::LLVMMDNodeInContext2(llcx, &name_metadata, 1)),
489485
);
490486
}
491487

compiler/rustc_codegen_llvm/src/debuginfo/metadata.rs

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::path::{Path, PathBuf};
55
use std::sync::Arc;
66
use std::{iter, ptr};
77

8-
use libc::{c_char, c_longlong, c_uint};
8+
use libc::{c_longlong, c_uint};
99
use rustc_abi::{Align, Size};
1010
use rustc_codegen_ssa::debuginfo::type_names::{VTableNameKind, cpp_like_debuginfo};
1111
use rustc_codegen_ssa::traits::*;
@@ -1582,13 +1582,9 @@ 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);
15851586

15861587
unsafe {
1587-
let typeid = llvm::LLVMMDStringInContext2(
1588-
cx.llcx,
1589-
trait_ref_typeid.as_ptr() as *const c_char,
1590-
trait_ref_typeid.as_bytes().len(),
1591-
);
15921588
let v = [llvm::LLVMValueAsMetadata(cx.const_usize(0)), typeid];
15931589
llvm::LLVMRustGlobalAddMetadata(
15941590
vtable,

0 commit comments

Comments
 (0)