Skip to content

Commit c42d846

Browse files
committed
Auto merge of rust-lang#94229 - erikdesjardins:rem2, r=nikic
Remove LLVM attribute removal This was necessary before, because `declare_raw_fn` would always apply the default optimization attributes to every declared function. Then `attributes::from_fn_attrs` would have to remove the default attributes in the case of, e.g. `#[optimize(speed)]` in a `-Os` build. (see [`src/test/codegen/optimize-attr-1.rs`](https://github.com/rust-lang/rust/blob/03a8cc7df1d65554a4d40825b0490c93ac0f0236/src/test/codegen/optimize-attr-1.rs#L33)) However, every relevant callsite of `declare_raw_fn` (i.e. where we actually generate code for the function, and not e.g. a call to an intrinsic, where optimization attributes don't [?] matter) calls `from_fn_attrs`, so we can remove the attribute setting from `declare_raw_fn`, and rely on `from_fn_attrs` to apply the correct attributes all at once. r? `@ghost` (blocked on rust-lang#94221) `@rustbot` label S-blocked
2 parents 2a280de + dce14cf commit c42d846

File tree

5 files changed

+13
-87
lines changed

5 files changed

+13
-87
lines changed

compiler/rustc_codegen_llvm/src/attributes.rs

+9-39
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,6 @@ pub fn apply_to_llfn(llfn: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
2828
}
2929
}
3030

31-
pub fn remove_from_llfn(llfn: &Value, idx: AttributePlace, attrs: &[AttributeKind]) {
32-
if !attrs.is_empty() {
33-
llvm::RemoveFunctionAttributes(llfn, idx, attrs);
34-
}
35-
}
36-
3731
pub fn apply_to_callsite(callsite: &Value, idx: AttributePlace, attrs: &[&Attribute]) {
3832
if !attrs.is_empty() {
3933
llvm::AddCallSiteAttributes(callsite, idx, attrs);
@@ -215,38 +209,23 @@ pub fn non_lazy_bind_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute
215209
}
216210
}
217211

218-
/// Returns attributes to remove and to add, respectively,
219-
/// to set the default optimizations attrs on a function.
212+
/// Get the default optimizations attrs for a function.
220213
#[inline]
221214
pub(crate) fn default_optimisation_attrs<'ll>(
222215
cx: &CodegenCx<'ll, '_>,
223-
) -> (
224-
// Attributes to remove
225-
SmallVec<[AttributeKind; 3]>,
226-
// Attributes to add
227-
SmallVec<[&'ll Attribute; 2]>,
228-
) {
229-
let mut to_remove = SmallVec::new();
230-
let mut to_add = SmallVec::new();
216+
) -> SmallVec<[&'ll Attribute; 2]> {
217+
let mut attrs = SmallVec::new();
231218
match cx.sess().opts.optimize {
232219
OptLevel::Size => {
233-
to_remove.push(llvm::AttributeKind::MinSize);
234-
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
235-
to_remove.push(llvm::AttributeKind::OptimizeNone);
220+
attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
236221
}
237222
OptLevel::SizeMin => {
238-
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
239-
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
240-
to_remove.push(llvm::AttributeKind::OptimizeNone);
241-
}
242-
OptLevel::No => {
243-
to_remove.push(llvm::AttributeKind::MinSize);
244-
to_remove.push(llvm::AttributeKind::OptimizeForSize);
245-
to_remove.push(llvm::AttributeKind::OptimizeNone);
223+
attrs.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
224+
attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
246225
}
247226
_ => {}
248227
}
249-
(to_remove, to_add)
228+
attrs
250229
}
251230

252231
/// Composite function which sets LLVM attributes for function depending on its AST (`#[attribute]`)
@@ -258,25 +237,17 @@ pub fn from_fn_attrs<'ll, 'tcx>(
258237
) {
259238
let codegen_fn_attrs = cx.tcx.codegen_fn_attrs(instance.def_id());
260239

261-
let mut to_remove = SmallVec::<[_; 4]>::new();
262240
let mut to_add = SmallVec::<[_; 16]>::new();
263241

264242
match codegen_fn_attrs.optimize {
265243
OptimizeAttr::None => {
266-
let (to_remove_opt, to_add_opt) = default_optimisation_attrs(cx);
267-
to_remove.extend(to_remove_opt);
268-
to_add.extend(to_add_opt);
269-
}
270-
OptimizeAttr::Speed => {
271-
to_remove.push(llvm::AttributeKind::MinSize);
272-
to_remove.push(llvm::AttributeKind::OptimizeForSize);
273-
to_remove.push(llvm::AttributeKind::OptimizeNone);
244+
to_add.extend(default_optimisation_attrs(cx));
274245
}
275246
OptimizeAttr::Size => {
276247
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
277248
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
278-
to_remove.push(llvm::AttributeKind::OptimizeNone);
279249
}
250+
OptimizeAttr::Speed => {}
280251
}
281252

282253
let inline = if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
@@ -421,7 +392,6 @@ pub fn from_fn_attrs<'ll, 'tcx>(
421392
to_add.push(llvm::CreateAttrStringValue(cx.llcx, cstr!("target-features"), &val));
422393
}
423394

424-
attributes::remove_from_llfn(llfn, Function, &to_remove);
425395
attributes::apply_to_llfn(llfn, Function, &to_add);
426396
}
427397

compiler/rustc_codegen_llvm/src/declare.rs

+4-10
Original file line numberDiff line numberDiff line change
@@ -41,21 +41,15 @@ fn declare_raw_fn<'ll>(
4141
llvm::SetFunctionCallConv(llfn, callconv);
4242
llvm::SetUnnamedAddress(llfn, unnamed);
4343

44-
let mut attrs_to_remove = SmallVec::<[_; 4]>::new();
45-
let mut attrs_to_add = SmallVec::<[_; 4]>::new();
44+
let mut attrs = SmallVec::<[_; 4]>::new();
4645

4746
if cx.tcx.sess.opts.cg.no_redzone.unwrap_or(cx.tcx.sess.target.disable_redzone) {
48-
attrs_to_add.push(llvm::AttributeKind::NoRedZone.create_attr(cx.llcx));
47+
attrs.push(llvm::AttributeKind::NoRedZone.create_attr(cx.llcx));
4948
}
5049

51-
let (to_remove, to_add) = attributes::default_optimisation_attrs(cx);
52-
attrs_to_remove.extend(to_remove);
53-
attrs_to_add.extend(to_add);
50+
attrs.extend(attributes::non_lazy_bind_attr(cx));
5451

55-
attrs_to_add.extend(attributes::non_lazy_bind_attr(cx));
56-
57-
attributes::remove_from_llfn(llfn, Function, &attrs_to_remove);
58-
attributes::apply_to_llfn(llfn, Function, &attrs_to_add);
52+
attributes::apply_to_llfn(llfn, Function, &attrs);
5953

6054
llfn
6155
}

compiler/rustc_codegen_llvm/src/llvm/ffi.rs

-6
Original file line numberDiff line numberDiff line change
@@ -1202,12 +1202,6 @@ extern "C" {
12021202
Attrs: *const &'a Attribute,
12031203
AttrsLen: size_t,
12041204
);
1205-
pub fn LLVMRustRemoveFunctionAttributes(
1206-
Fn: &Value,
1207-
index: c_uint,
1208-
Attrs: *const AttributeKind,
1209-
AttrsLen: size_t,
1210-
);
12111205

12121206
// Operations on parameters
12131207
pub fn LLVMIsAArgument(Val: &Value) -> Option<&Value>;

compiler/rustc_codegen_llvm/src/llvm/mod.rs

-6
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,6 @@ pub fn AddFunctionAttributes<'ll>(llfn: &'ll Value, idx: AttributePlace, attrs:
3737
}
3838
}
3939

40-
pub fn RemoveFunctionAttributes(llfn: &Value, idx: AttributePlace, attrs: &[AttributeKind]) {
41-
unsafe {
42-
LLVMRustRemoveFunctionAttributes(llfn, idx.as_uint(), attrs.as_ptr(), attrs.len());
43-
}
44-
}
45-
4640
pub fn AddCallSiteAttributes<'ll>(
4741
callsite: &'ll Value,
4842
idx: AttributePlace,

compiler/rustc_llvm/llvm-wrapper/RustWrapper.cpp

-26
Original file line numberDiff line numberDiff line change
@@ -250,38 +250,12 @@ template<typename T> static inline void AddAttributes(T *t, unsigned Index,
250250
t->setAttributes(PALNew);
251251
}
252252

253-
template<typename T> static inline void RemoveAttributes(T *t, unsigned Index,
254-
LLVMRustAttribute *RustAttrs,
255-
size_t RustAttrsLen) {
256-
AttributeList PAL = t->getAttributes();
257-
AttributeList PALNew;
258-
#if LLVM_VERSION_LT(14, 0)
259-
AttrBuilder B;
260-
for (LLVMRustAttribute RustAttr : makeArrayRef(RustAttrs, RustAttrsLen))
261-
B.addAttribute(fromRust(RustAttr));
262-
PALNew = PAL.removeAttributes(t->getContext(), Index, B);
263-
#else
264-
AttributeMask Mask;
265-
for (LLVMRustAttribute RustAttr : makeArrayRef(RustAttrs, RustAttrsLen))
266-
Mask.addAttribute(fromRust(RustAttr));
267-
PALNew = PAL.removeAttributesAtIndex(t->getContext(), Index, Mask);
268-
#endif
269-
t->setAttributes(PALNew);
270-
}
271-
272253
extern "C" void LLVMRustAddFunctionAttributes(LLVMValueRef Fn, unsigned Index,
273254
LLVMAttributeRef *Attrs, size_t AttrsLen) {
274255
Function *F = unwrap<Function>(Fn);
275256
AddAttributes(F, Index, Attrs, AttrsLen);
276257
}
277258

278-
extern "C" void LLVMRustRemoveFunctionAttributes(LLVMValueRef Fn, unsigned Index,
279-
LLVMRustAttribute *RustAttrs,
280-
size_t RustAttrsLen) {
281-
Function *F = unwrap<Function>(Fn);
282-
RemoveAttributes(F, Index, RustAttrs, RustAttrsLen);
283-
}
284-
285259
extern "C" void LLVMRustAddCallSiteAttributes(LLVMValueRef Instr, unsigned Index,
286260
LLVMAttributeRef *Attrs, size_t AttrsLen) {
287261
CallBase *Call = unwrap<CallBase>(Instr);

0 commit comments

Comments
 (0)