Skip to content

Use Clang's logic for adding the default IR attributes to a function #31906

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 21, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 10 additions & 30 deletions lib/IRGen/IRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1001,42 +1001,22 @@ void IRGenModule::setHasFramePointer(llvm::Function *F,
/// Construct initial function attributes from options.
void IRGenModule::constructInitialFnAttributes(llvm::AttrBuilder &Attrs,
OptimizationMode FuncOptMode) {
// Add the default attributes for the Clang configuration.
clang::CodeGen::addDefaultFunctionDefinitionAttributes(getClangCGM(), Attrs);

// Add frame pointer attributes.
// FIXME: why are we doing this?
setHasFramePointer(Attrs, IRGen.Opts.DisableFPElim);

// Add target-cpu and target-features if they are non-null.
auto *Clang = static_cast<ClangImporter *>(Context.getClangModuleLoader());
clang::TargetOptions &ClangOpts = Clang->getTargetInfo().getTargetOpts();

std::string &CPU = ClangOpts.CPU;
if (CPU != "")
Attrs.addAttribute("target-cpu", CPU);

std::vector<std::string> Features;
for (auto &F : ClangOpts.Features)
if (!shouldRemoveTargetFeature(F))
Features.push_back(F);

if (!Features.empty()) {
SmallString<64> allFeatures;
// Sort so that the target features string is canonical.
std::sort(Features.begin(), Features.end());
llvm::interleave(Features, [&](const std::string &s) {
allFeatures.append(s);
}, [&]{
allFeatures.push_back(',');
});
Attrs.addAttribute("target-features", allFeatures);
}
// Add/remove MinSize based on the appropriate setting.
if (FuncOptMode == OptimizationMode::NotSet)
FuncOptMode = IRGen.Opts.OptMode;
if (FuncOptMode == OptimizationMode::ForSize)
if (FuncOptMode == OptimizationMode::ForSize) {
Attrs.addAttribute(llvm::Attribute::OptimizeForSize);
Attrs.addAttribute(llvm::Attribute::MinSize);

auto triple = llvm::Triple(ClangOpts.Triple);
if (triple.getArchName() == "arm64e") {
Attrs.addAttribute("ptrauth-returns");
Attrs.addAttribute("ptrauth-calls");
} else {
Attrs.removeAttribute(llvm::Attribute::MinSize);
Attrs.removeAttribute(llvm::Attribute::OptimizeForSize);
}
}

Expand Down
2 changes: 1 addition & 1 deletion test/IRGen/c_globals.swift
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,4 @@ public func testCaptureGlobal() {
}

// CHECK-DAG: attributes [[CLANG_FUNC_ATTR]] = { noinline nounwind {{.*}}"frame-pointer"="all"{{.*}}
// CHECK-DAG: attributes [[SWIFT_FUNC_ATTR]] = { "frame-pointer"="all" {{.*}}"target-cpu"
// CHECK-DAG: attributes [[SWIFT_FUNC_ATTR]] = { {{.*}}"frame-pointer"="all" {{.*}}"target-cpu"
2 changes: 1 addition & 1 deletion test/IRGen/generic_metatypes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,4 @@ func makeGenericMetatypes() {
// CHECK: ret %swift.metadata_response

// CHECK-DAG: attributes [[NOUNWIND_READNONE]] = { nounwind readnone }
// CHECK-DAG: attributes [[NOUNWIND_OPT]] = { noinline nounwind "frame-pointer"="none" "target-cpu"
// CHECK-DAG: attributes [[NOUNWIND_OPT]] = { noinline nounwind "
2 changes: 1 addition & 1 deletion test/IRGen/generic_metatypes_future.swift
Original file line number Diff line number Diff line change
Expand Up @@ -176,4 +176,4 @@ func makeGenericMetatypes() {
// CHECK: ret %swift.metadata_response

// CHECK-DAG: attributes [[NOUNWIND_READNONE]] = { nounwind readnone }
// CHECK-DAG: attributes [[NOUNWIND_OPT]] = { noinline nounwind "frame-pointer"="none" "target-cpu"
// CHECK-DAG: attributes [[NOUNWIND_OPT]] = { noinline nounwind "
2 changes: 1 addition & 1 deletion test/IRGen/ptrauth-functions.sil
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,4 @@ bb0(%0 : $T):

sil_vtable F {
}
// CHECK: #[[ATTRS]] = {{{.*}} "ptrauth-calls" "ptrauth-returns"
// CHECK: #[[ATTRS]] = {{{.*}} "ptrauth-auth-traps" "ptrauth-calls" "ptrauth-indirect-gotos" "ptrauth-returns"
2 changes: 1 addition & 1 deletion test/SILOptimizer/opt_mode.swift
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,5 @@ func test_ospeed(_ a: A) -> Int {
}


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