Skip to content

SILGen: Closures within @inline(__always) functions should be always inlined, too #64210

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
Mar 9, 2023
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
14 changes: 12 additions & 2 deletions lib/SIL/IR/SILDeclRef.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -931,10 +931,20 @@ bool SILDeclRef::isNoinline() const {

/// True if the function has the @inline(__always) attribute.
bool SILDeclRef::isAlwaysInline() const {
if (!hasDecl())
swift::Decl *decl = nullptr;
if (hasDecl()) {
decl = getDecl();
} else if (auto *ce = getAbstractClosureExpr()) {
// Closures within @inline(__always) functions should be always inlined, too.
// Note that this is different from @inline(never), because closures inside
// @inline(never) _can_ be inlined within the inline-never function.
decl = ce->getParent()->getInnermostDeclarationDeclContext();
if (!decl)
return false;
} else {
return false;
}

auto *decl = getDecl();
if (auto attr = decl->getAttrs().getAttribute<InlineAttr>())
if (attr->getKind() == InlineKind::Always)
return true;
Expand Down
7 changes: 7 additions & 0 deletions test/SILGen/inline_always.swift
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
@inline(__always)
func always_inline_callee() {}

// CHECK-LABEL: sil hidden [always_inline] [ossa] @$s13inline_always11testClosureyyF
@inline(__always)
func testClosure() {
// CHECK-LABEL: sil private [always_inline] [ossa] @$s13inline_always11testClosureyyFyycfU_
_ = { }
}

protocol AlwaysInline {
func alwaysInlined()
}
Expand Down
26 changes: 26 additions & 0 deletions test/SILOptimizer/inlinealways_and_closure.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
// RUN: %target-swift-frontend %s -O -module-name=test -emit-sil | %FileCheck %s

// REQUIRES: swift_stdlib_no_asserts,optimized_stdlib

public protocol P {
func takePointer<T>(_ p: UnsafePointer<T>)
}

extension P {
@inline(__always)
func genericFunc<T>(_ x: inout T) {
withUnsafePointer(to: &x) {
takePointer($0)
}
}
}

// CHECK-LABEL: sil {{.*}}@$s4test6testityyAA1P_pF
// CHECK-NOT: function_ref
// CHECK: witness_method
// CHECK: } // end sil function '$s4test6testityyAA1P_pF'
public func testit(_ p: P) {
var x = 0
p.genericFunc(&x)
}