Skip to content

Commit

Permalink
Pass value and valueLen to create a StringRef
Browse files Browse the repository at this point in the history
Instead of creating a cstring.

Co-authored-by: LoveSy <shana@zju.edu.cn>
  • Loading branch information
kxxt and yujincheng08 committed Apr 9, 2024
1 parent adec1a2 commit 33db209
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 10 deletions.
12 changes: 5 additions & 7 deletions compiler/rustc_codegen_llvm/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ use libc::c_uint;
use std::borrow::Borrow;
use std::cell::{Cell, RefCell};
use std::ffi::CStr;
use std::ffi::CString;
use std::str;

/// There is one `CodegenCx` per compilation unit. Each one has its own LLVM
Expand Down Expand Up @@ -332,15 +331,14 @@ pub unsafe fn create_module<'ll>(
// correctly setting target-abi for the LTO object
// FIXME: https://github.com/llvm/llvm-project/issues/50591
// If llvm_abiname is empty, emit nothing.
if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64")
&& !sess.target.options.llvm_abiname.is_empty()
{
let llvm_abiname = CString::new(sess.target.options.llvm_abiname.as_ref()).unwrap();
let llvm_abiname = &sess.target.options.llvm_abiname;
if matches!(sess.target.arch.as_ref(), "riscv32" | "riscv64") && !llvm_abiname.is_empty() {
llvm::LLVMRustAddModuleFlagString(
llmod,
llvm::LLVMModFlagBehavior::Error,
c"target-abi".as_ptr() as *const _,
llvm_abiname.as_ptr() as *const _,
c"target-abi".as_ptr(),
llvm_abiname.as_ptr().cast(),
llvm_abiname.len(),
);
}

Expand Down
1 change: 1 addition & 0 deletions compiler/rustc_codegen_llvm/src/llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1813,6 +1813,7 @@ extern "C" {
merge_behavior: LLVMModFlagBehavior,
name: *const c_char,
value: *const c_char,
value_len: size_t,
);

pub fn LLVMRustHasModuleFlag(M: &Module, name: *const c_char, len: size_t) -> bool;
Expand Down
7 changes: 4 additions & 3 deletions compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -829,9 +829,10 @@ extern "C" void LLVMRustAddModuleFlagString(
LLVMModuleRef M,
Module::ModFlagBehavior MergeBehavior,
const char *Name,
const char *Value) {
llvm::LLVMContext &Ctx = unwrap(M)->getContext();
unwrap(M)->addModuleFlag(MergeBehavior, Name, llvm::MDString::get(Ctx, Value));
const char *Value,
size_t ValueLen) {
unwrap(M)->addModuleFlag(MergeBehavior, Name,
MDString::get(unwrap(M)->getContext(), StringRef(Value, ValueLen)));
}

extern "C" bool LLVMRustHasModuleFlag(LLVMModuleRef M, const char *Name,
Expand Down

0 comments on commit 33db209

Please sign in to comment.