Skip to content

Commit 4f54c75

Browse files
committed
Use Clang's logic for adding the default IR attributes to a function.
A lot of attributes are essentially default target configuration, and we should only differ when there's a good reason to. For the attributes we were already setting: - the ptrauth and target CPU/feature attributes are taken care of by Clang - I've updated the optsize/minsize attributes to the apparent intent - I've left the frame-pointer override in place for now Fixes rdar://63289339, which was caused by Swift's ptrauth IR attributes getting out of sync with Clang's.
1 parent 3939786 commit 4f54c75

File tree

6 files changed

+15
-35
lines changed

6 files changed

+15
-35
lines changed

lib/IRGen/IRGenModule.cpp

Lines changed: 10 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,42 +1001,22 @@ void IRGenModule::setHasFramePointer(llvm::Function *F,
10011001
/// Construct initial function attributes from options.
10021002
void IRGenModule::constructInitialFnAttributes(llvm::AttrBuilder &Attrs,
10031003
OptimizationMode FuncOptMode) {
1004+
// Add the default attributes for the Clang configuration.
1005+
clang::CodeGen::addDefaultFunctionDefinitionAttributes(getClangCGM(), Attrs);
1006+
10041007
// Add frame pointer attributes.
1008+
// FIXME: why are we doing this?
10051009
setHasFramePointer(Attrs, IRGen.Opts.DisableFPElim);
10061010

1007-
// Add target-cpu and target-features if they are non-null.
1008-
auto *Clang = static_cast<ClangImporter *>(Context.getClangModuleLoader());
1009-
clang::TargetOptions &ClangOpts = Clang->getTargetInfo().getTargetOpts();
1010-
1011-
std::string &CPU = ClangOpts.CPU;
1012-
if (CPU != "")
1013-
Attrs.addAttribute("target-cpu", CPU);
1014-
1015-
std::vector<std::string> Features;
1016-
for (auto &F : ClangOpts.Features)
1017-
if (!shouldRemoveTargetFeature(F))
1018-
Features.push_back(F);
1019-
1020-
if (!Features.empty()) {
1021-
SmallString<64> allFeatures;
1022-
// Sort so that the target features string is canonical.
1023-
std::sort(Features.begin(), Features.end());
1024-
llvm::interleave(Features, [&](const std::string &s) {
1025-
allFeatures.append(s);
1026-
}, [&]{
1027-
allFeatures.push_back(',');
1028-
});
1029-
Attrs.addAttribute("target-features", allFeatures);
1030-
}
1011+
// Add/remove MinSize based on the appropriate setting.
10311012
if (FuncOptMode == OptimizationMode::NotSet)
10321013
FuncOptMode = IRGen.Opts.OptMode;
1033-
if (FuncOptMode == OptimizationMode::ForSize)
1014+
if (FuncOptMode == OptimizationMode::ForSize) {
1015+
Attrs.addAttribute(llvm::Attribute::OptimizeForSize);
10341016
Attrs.addAttribute(llvm::Attribute::MinSize);
1035-
1036-
auto triple = llvm::Triple(ClangOpts.Triple);
1037-
if (triple.getArchName() == "arm64e") {
1038-
Attrs.addAttribute("ptrauth-returns");
1039-
Attrs.addAttribute("ptrauth-calls");
1017+
} else {
1018+
Attrs.removeAttribute(llvm::Attribute::MinSize);
1019+
Attrs.removeAttribute(llvm::Attribute::OptimizeForSize);
10401020
}
10411021
}
10421022

test/IRGen/c_globals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,4 @@ public func testCaptureGlobal() {
3232
}
3333

3434
// CHECK-DAG: attributes [[CLANG_FUNC_ATTR]] = { noinline nounwind {{.*}}"frame-pointer"="all"{{.*}}
35-
// CHECK-DAG: attributes [[SWIFT_FUNC_ATTR]] = { "frame-pointer"="all" {{.*}}"target-cpu"
35+
// CHECK-DAG: attributes [[SWIFT_FUNC_ATTR]] = { {{.*}}"frame-pointer"="all" {{.*}}"target-cpu"

test/IRGen/generic_metatypes.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -146,4 +146,4 @@ func makeGenericMetatypes() {
146146
// CHECK: ret %swift.metadata_response
147147

148148
// CHECK-DAG: attributes [[NOUNWIND_READNONE]] = { nounwind readnone }
149-
// CHECK-DAG: attributes [[NOUNWIND_OPT]] = { noinline nounwind "frame-pointer"="none" "target-cpu"
149+
// CHECK-DAG: attributes [[NOUNWIND_OPT]] = { noinline nounwind "

test/IRGen/generic_metatypes_future.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,4 +176,4 @@ func makeGenericMetatypes() {
176176
// CHECK: ret %swift.metadata_response
177177

178178
// CHECK-DAG: attributes [[NOUNWIND_READNONE]] = { nounwind readnone }
179-
// CHECK-DAG: attributes [[NOUNWIND_OPT]] = { noinline nounwind "frame-pointer"="none" "target-cpu"
179+
// CHECK-DAG: attributes [[NOUNWIND_OPT]] = { noinline nounwind "

test/IRGen/ptrauth-functions.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -87,4 +87,4 @@ bb0(%0 : $T):
8787

8888
sil_vtable F {
8989
}
90-
// CHECK: #[[ATTRS]] = {{{.*}} "ptrauth-calls" "ptrauth-returns"
90+
// CHECK: #[[ATTRS]] = {{{.*}} "ptrauth-auth-traps" "ptrauth-calls" "ptrauth-indirect-gotos" "ptrauth-returns"

test/SILOptimizer/opt_mode.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,5 +47,5 @@ func test_ospeed(_ a: A) -> Int {
4747
}
4848

4949

50-
// CHECK-IR: attributes [[SIZE_ATTR]] = { minsize "
50+
// CHECK-IR: attributes [[SIZE_ATTR]] = { minsize optsize "
5151
// CHECK-IR: attributes [[NOSIZE_ATTR]] = { "

0 commit comments

Comments
 (0)