Skip to content

Commit dce14cf

Browse files
Remove LLVM attribute removal
This was necessary before, because `declare_raw_fn` would always apply the default optimization attributes to every declared function, and then `attributes::from_fn_attrs` would have to remove the default attributes in the case of, e.g. `#[optimize(speed)]` in a `-Os` build. 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 simply remove the attribute setting from `declare_raw_fn`, and rely on `from_fn_attrs` to apply the correct attributes all at once.
1 parent b07d59f commit dce14cf

File tree

5 files changed

+13
-87
lines changed

5 files changed

+13
-87
lines changed

Diff for: 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);
@@ -217,38 +211,23 @@ pub fn non_lazy_bind_attr<'ll>(cx: &CodegenCx<'ll, '_>) -> Option<&'ll Attribute
217211
}
218212
}
219213

220-
/// Returns attributes to remove and to add, respectively,
221-
/// to set the default optimizations attrs on a function.
214+
/// Get the default optimizations attrs for a function.
222215
#[inline]
223216
pub(crate) fn default_optimisation_attrs<'ll>(
224217
cx: &CodegenCx<'ll, '_>,
225-
) -> (
226-
// Attributes to remove
227-
SmallVec<[AttributeKind; 3]>,
228-
// Attributes to add
229-
SmallVec<[&'ll Attribute; 2]>,
230-
) {
231-
let mut to_remove = SmallVec::new();
232-
let mut to_add = SmallVec::new();
218+
) -> SmallVec<[&'ll Attribute; 2]> {
219+
let mut attrs = SmallVec::new();
233220
match cx.sess().opts.optimize {
234221
OptLevel::Size => {
235-
to_remove.push(llvm::AttributeKind::MinSize);
236-
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
237-
to_remove.push(llvm::AttributeKind::OptimizeNone);
222+
attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
238223
}
239224
OptLevel::SizeMin => {
240-
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
241-
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
242-
to_remove.push(llvm::AttributeKind::OptimizeNone);
243-
}
244-
OptLevel::No => {
245-
to_remove.push(llvm::AttributeKind::MinSize);
246-
to_remove.push(llvm::AttributeKind::OptimizeForSize);
247-
to_remove.push(llvm::AttributeKind::OptimizeNone);
225+
attrs.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
226+
attrs.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
248227
}
249228
_ => {}
250229
}
251-
(to_remove, to_add)
230+
attrs
252231
}
253232

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

263-
let mut to_remove = SmallVec::<[_; 4]>::new();
264242
let mut to_add = SmallVec::<[_; 16]>::new();
265243

266244
match codegen_fn_attrs.optimize {
267245
OptimizeAttr::None => {
268-
let (to_remove_opt, to_add_opt) = default_optimisation_attrs(cx);
269-
to_remove.extend(to_remove_opt);
270-
to_add.extend(to_add_opt);
271-
}
272-
OptimizeAttr::Speed => {
273-
to_remove.push(llvm::AttributeKind::MinSize);
274-
to_remove.push(llvm::AttributeKind::OptimizeForSize);
275-
to_remove.push(llvm::AttributeKind::OptimizeNone);
246+
to_add.extend(default_optimisation_attrs(cx));
276247
}
277248
OptimizeAttr::Size => {
278249
to_add.push(llvm::AttributeKind::MinSize.create_attr(cx.llcx));
279250
to_add.push(llvm::AttributeKind::OptimizeForSize.create_attr(cx.llcx));
280-
to_remove.push(llvm::AttributeKind::OptimizeNone);
281251
}
252+
OptimizeAttr::Speed => {}
282253
}
283254

284255
let inline = if codegen_fn_attrs.flags.contains(CodegenFnAttrFlags::NAKED) {
@@ -425,7 +396,6 @@ pub fn from_fn_attrs<'ll, 'tcx>(
425396
to_add.push(llvm::CreateAttrStringValue(cx.llcx, cstr!("target-features"), &val));
426397
}
427398

428-
attributes::remove_from_llfn(llfn, Function, &to_remove);
429399
attributes::apply_to_llfn(llfn, Function, &to_add);
430400
}
431401

Diff for: 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
}

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

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

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

Diff for: 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,

Diff for: 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)