Skip to content

Commit 926bf1a

Browse files
committed
Pass LLVM string attributes as string slices
1 parent 08504c6 commit 926bf1a

File tree

5 files changed

+47
-43
lines changed

5 files changed

+47
-43
lines changed

Diff for: compiler/rustc_codegen_llvm/src/abi.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -561,8 +561,7 @@ impl<'ll, 'tcx> FnAbiLlvmExt<'ll, 'tcx> for FnAbi<'tcx, Ty<'tcx>> {
561561
if self.conv == Conv::CCmseNonSecureCall {
562562
// This will probably get ignored on all targets but those supporting the TrustZone-M
563563
// extension (thumbv8m targets).
564-
let cmse_nonsecure_call =
565-
llvm::CreateAttrString(bx.cx.llcx, cstr::cstr!("cmse_nonsecure_call"));
564+
let cmse_nonsecure_call = llvm::CreateAttrString(bx.cx.llcx, "cmse_nonsecure_call");
566565
attributes::apply_to_callsite(
567566
callsite,
568567
llvm::AttributePlace::Function,

Diff for: compiler/rustc_codegen_llvm/src/attributes.rs

+23-29
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
//! Set and unset common attributes on LLVM values.
22
3-
use std::ffi::CString;
4-
5-
use cstr::cstr;
63
use rustc_codegen_ssa::traits::*;
7-
use rustc_data_structures::small_c_str::SmallCStr;
84
use rustc_hir::def_id::DefId;
95
use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
106
use rustc_middle::ty::{self, TyCtxt};
@@ -103,11 +99,11 @@ pub fn frame_pointer_type_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attr
10399
fp = FramePointer::Always;
104100
}
105101
let attr_value = match fp {
106-
FramePointer::Always => cstr!("all"),
107-
FramePointer::NonLeaf => cstr!("non-leaf"),
102+
FramePointer::Always => "all",
103+
FramePointer::NonLeaf => "non-leaf",
108104
FramePointer::MayOmit => return None,
109105
};
110-
Some(llvm::CreateAttrStringValue(cx.llcx, cstr!("frame-pointer"), attr_value))
106+
Some(llvm::CreateAttrStringValue(cx.llcx, "frame-pointer", attr_value))
111107
}
112108

113109
/// Tell LLVM what instrument function to insert.
@@ -119,11 +115,11 @@ fn instrument_function_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribu
119115

120116
// The function name varies on platforms.
121117
// See test/CodeGen/mcount.c in clang.
122-
let mcount_name = CString::new(cx.sess().target.mcount.as_str().as_bytes()).unwrap();
118+
let mcount_name = cx.sess().target.mcount.as_str();
123119

124120
Some(llvm::CreateAttrStringValue(
125121
cx.llcx,
126-
cstr!("instrument-function-entry-inlined"),
122+
"instrument-function-entry-inlined",
127123
&mcount_name,
128124
))
129125
} else {
@@ -159,20 +155,20 @@ fn probestack_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
159155
StackProbeType::None => return None,
160156
// Request LLVM to generate the probes inline. If the given LLVM version does not support
161157
// this, no probe is generated at all (even if the attribute is specified).
162-
StackProbeType::Inline => cstr!("inline-asm"),
158+
StackProbeType::Inline => "inline-asm",
163159
// Flag our internal `__rust_probestack` function as the stack probe symbol.
164160
// This is defined in the `compiler-builtins` crate for each architecture.
165-
StackProbeType::Call => cstr!("__rust_probestack"),
161+
StackProbeType::Call => "__rust_probestack",
166162
// Pick from the two above based on the LLVM version.
167163
StackProbeType::InlineOrCall { min_llvm_version_for_inline } => {
168164
if llvm_util::get_version() < min_llvm_version_for_inline {
169-
cstr!("__rust_probestack")
165+
"__rust_probestack"
170166
} else {
171-
cstr!("inline-asm")
167+
"inline-asm"
172168
}
173169
}
174170
};
175-
Some(llvm::CreateAttrStringValue(cx.llcx, cstr!("probe-stack"), attr_value))
171+
Some(llvm::CreateAttrStringValue(cx.llcx, "probe-stack", attr_value))
176172
}
177173

178174
fn stackprotector_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
@@ -187,15 +183,13 @@ fn stackprotector_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
187183
}
188184

189185
pub fn target_cpu_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> &'ll Attribute {
190-
let target_cpu = SmallCStr::new(llvm_util::target_cpu(cx.tcx.sess));
191-
llvm::CreateAttrStringValue(cx.llcx, cstr!("target-cpu"), target_cpu.as_c_str())
186+
let target_cpu = llvm_util::target_cpu(cx.tcx.sess);
187+
llvm::CreateAttrStringValue(cx.llcx, "target-cpu", target_cpu)
192188
}
193189

194190
pub fn tune_cpu_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute> {
195-
llvm_util::tune_cpu(cx.tcx.sess).map(|tune| {
196-
let tune_cpu = SmallCStr::new(tune);
197-
llvm::CreateAttrStringValue(cx.llcx, cstr!("tune-cpu"), tune_cpu.as_c_str())
198-
})
191+
llvm_util::tune_cpu(cx.tcx.sess)
192+
.map(|tune_cpu| llvm::CreateAttrStringValue(cx.llcx, "tune-cpu", tune_cpu))
199193
}
200194

201195
/// Get the `NonLazyBind` LLVM attribute,
@@ -280,7 +274,7 @@ pub fn from_fn_attrs<'ll, 'tcx>(
280274
}
281275

282276
if cx.sess().opts.debugging_opts.profile_sample_use.is_some() {
283-
to_add.push(llvm::CreateAttrString(cx.llcx, cstr!("use-sample-profile")));
277+
to_add.push(llvm::CreateAttrString(cx.llcx, "use-sample-profile"));
284278
}
285279

286280
// FIXME: none of these three functions interact with source level attributes.
@@ -310,7 +304,7 @@ pub fn from_fn_attrs<'ll, 'tcx>(
310304
attributes::apply_to_llfn(llfn, AttributePlace::ReturnValue, &[no_alias]);
311305
}
312306
if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::CMSE_NONSECURE_ENTRY) {
313-
to_add.push(llvm::CreateAttrString(cx.llcx, cstr!("cmse_nonsecure_entry")));
307+
to_add.push(llvm::CreateAttrString(cx.llcx, "cmse_nonsecure_entry"));
314308
}
315309
if let Some(align) = codegen_fn_attrs.alignment {
316310
llvm::set_alignment(llfn, align as usize);
@@ -363,12 +357,12 @@ pub fn from_fn_attrs<'ll, 'tcx>(
363357
// If this function is an import from the environment but the wasm
364358
// import has a specific module/name, apply them here.
365359
if let Some(module) = wasm_import_module(cx.tcx, instance.def_id()) {
366-
to_add.push(llvm::CreateAttrStringValue(cx.llcx, cstr!("wasm-import-module"), &module));
360+
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-module", &module));
367361

368362
let name =
369363
codegen_fn_attrs.link_name.unwrap_or_else(|| cx.tcx.item_name(instance.def_id()));
370-
let name = CString::new(name.as_str()).unwrap();
371-
to_add.push(llvm::CreateAttrStringValue(cx.llcx, cstr!("wasm-import-name"), &name));
364+
let name = name.as_str();
365+
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "wasm-import-name", name));
372366
}
373367

374368
// The `"wasm"` abi on wasm targets automatically enables the
@@ -388,13 +382,13 @@ pub fn from_fn_attrs<'ll, 'tcx>(
388382
let val = global_features
389383
.chain(function_features.iter().map(|s| &s[..]))
390384
.intersperse(",")
391-
.collect::<SmallCStr>();
392-
to_add.push(llvm::CreateAttrStringValue(cx.llcx, cstr!("target-features"), &val));
385+
.collect::<String>();
386+
to_add.push(llvm::CreateAttrStringValue(cx.llcx, "target-features", &val));
393387
}
394388

395389
attributes::apply_to_llfn(llfn, Function, &to_add);
396390
}
397391

398-
fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<CString> {
399-
tcx.wasm_import_module_map(id.krate).get(&id).map(|s| CString::new(&s[..]).unwrap())
392+
fn wasm_import_module(tcx: TyCtxt<'_>, id: DefId) -> Option<&String> {
393+
tcx.wasm_import_module_map(id.krate).get(&id)
400394
}

Diff for: compiler/rustc_codegen_llvm/src/llvm/ffi.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -1175,11 +1175,12 @@ extern "C" {
11751175

11761176
// Operations on attributes
11771177
pub fn LLVMRustCreateAttrNoValue(C: &Context, attr: AttributeKind) -> &Attribute;
1178-
pub fn LLVMRustCreateAttrString(C: &Context, Name: *const c_char) -> &Attribute;
1179-
pub fn LLVMRustCreateAttrStringValue(
1178+
pub fn LLVMCreateStringAttribute(
11801179
C: &Context,
11811180
Name: *const c_char,
1181+
NameLen: c_uint,
11821182
Value: *const c_char,
1183+
ValueLen: c_uint,
11831184
) -> &Attribute;
11841185
pub fn LLVMRustCreateAlignmentAttr(C: &Context, bytes: u64) -> &Attribute;
11851186
pub fn LLVMRustCreateDereferenceableAttr(C: &Context, bytes: u64) -> &Attribute;

Diff for: compiler/rustc_codegen_llvm/src/llvm/mod.rs

+20-4
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,28 @@ pub fn AddCallSiteAttributes<'ll>(
4747
}
4848
}
4949

50-
pub fn CreateAttrStringValue<'ll>(llcx: &'ll Context, attr: &CStr, value: &CStr) -> &'ll Attribute {
51-
unsafe { LLVMRustCreateAttrStringValue(llcx, attr.as_ptr(), value.as_ptr()) }
50+
pub fn CreateAttrStringValue<'ll>(llcx: &'ll Context, attr: &str, value: &str) -> &'ll Attribute {
51+
unsafe {
52+
LLVMCreateStringAttribute(
53+
llcx,
54+
attr.as_ptr().cast(),
55+
attr.len().try_into().unwrap(),
56+
value.as_ptr().cast(),
57+
value.len().try_into().unwrap(),
58+
)
59+
}
5260
}
5361

54-
pub fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &CStr) -> &'ll Attribute {
55-
unsafe { LLVMRustCreateAttrStringValue(llcx, attr.as_ptr(), std::ptr::null()) }
62+
pub fn CreateAttrString<'ll>(llcx: &'ll Context, attr: &str) -> &'ll Attribute {
63+
unsafe {
64+
LLVMCreateStringAttribute(
65+
llcx,
66+
attr.as_ptr().cast(),
67+
attr.len().try_into().unwrap(),
68+
std::ptr::null(),
69+
0,
70+
)
71+
}
5672
}
5773

5874
pub fn CreateAlignmentAttr(llcx: &Context, bytes: u64) -> &Attribute {

Diff for: compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

-6
Original file line numberDiff line numberDiff line change
@@ -267,12 +267,6 @@ extern "C" LLVMAttributeRef LLVMRustCreateAttrNoValue(LLVMContextRef C,
267267
return wrap(Attribute::get(*unwrap(C), fromRust(RustAttr)));
268268
}
269269

270-
extern "C" LLVMAttributeRef LLVMRustCreateAttrStringValue(LLVMContextRef C,
271-
const char *Name,
272-
const char *Value) {
273-
return wrap(Attribute::get(*unwrap(C), StringRef(Name), StringRef(Value)));
274-
}
275-
276270
extern "C" LLVMAttributeRef LLVMRustCreateAlignmentAttr(LLVMContextRef C,
277271
uint64_t Bytes) {
278272
return wrap(Attribute::getWithAlignment(*unwrap(C), llvm::Align(Bytes)));

0 commit comments

Comments
 (0)