Skip to content

Cross-module-optimization: Mark stored class properties as usableFromInline if they are used in a ref_element_addr instruction. #28560

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 2 commits into from
Dec 4, 2019
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
2 changes: 1 addition & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,7 @@ static AccessLevel getAdjustedFormalAccess(const ValueDecl *VD,
return getMaximallyOpenAccessFor(VD);

if (treatUsableFromInlineAsPublic &&
(access == AccessLevel::Internal || access == AccessLevel::Private) &&
access <= AccessLevel::Internal &&
VD->isUsableFromInline()) {
return AccessLevel::Public;
}
Expand Down
29 changes: 16 additions & 13 deletions lib/SILOptimizer/IPO/CrossModuleSerializationSetup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,25 +137,25 @@ class InstructionVisitor : public SILCloner<InstructionVisitor> {
};

/// Make a nominal type, including it's context, usable from inline.
static void makeNominalUsableFromInline(NominalTypeDecl *NT, SILModule &M) {
if (NT->getEffectiveAccess() >= AccessLevel::Public)
static void makeDeclUsableFromInline(ValueDecl *decl, SILModule &M) {
if (decl->getEffectiveAccess() >= AccessLevel::Public)
return;

if (!NT->isUsableFromInline()) {
if (!decl->isUsableFromInline()) {
// Mark the nominal type as "usableFromInline".
// TODO: find a way to do this without modifying the AST. The AST should be
// immutable at this point.
auto &ctx = NT->getASTContext();
auto &ctx = decl->getASTContext();
auto *attr = new (ctx) UsableFromInlineAttr(/*implicit=*/true);
NT->getAttrs().add(attr);
decl->getAttrs().add(attr);
}
if (auto *enclosingNominal = dyn_cast<NominalTypeDecl>(NT->getDeclContext())) {
makeNominalUsableFromInline(enclosingNominal, M);
} else if (auto *enclosingExt = dyn_cast<ExtensionDecl>(NT->getDeclContext())) {
if (auto *extendedNominal = enclosingExt->getExtendedNominal()) {
makeNominalUsableFromInline(extendedNominal, M);
if (auto *nominalCtx = dyn_cast<NominalTypeDecl>(decl->getDeclContext())) {
makeDeclUsableFromInline(nominalCtx, M);
} else if (auto *extCtx = dyn_cast<ExtensionDecl>(decl->getDeclContext())) {
if (auto *extendedNominal = extCtx->getExtendedNominal()) {
makeDeclUsableFromInline(extendedNominal, M);
}
} else if (NT->getDeclContext()->isLocalContext()) {
} else if (decl->getDeclContext()->isLocalContext()) {
// TODO
}
}
Expand All @@ -166,15 +166,15 @@ void CrossModuleSerializationSetup::makeTypeUsableFromInline(CanType type) {
return;

if (NominalTypeDecl *NT = type->getNominalOrBoundGenericNominal()) {
makeNominalUsableFromInline(NT, M);
makeDeclUsableFromInline(NT, M);
}

// Also make all sub-types usable from inline.
type.visit([this](Type rawSubType) {
CanType subType = rawSubType->getCanonicalType();
if (typesHandled.insert(subType.getPointer()).second) {
if (NominalTypeDecl *subNT = subType->getNominalOrBoundGenericNominal()) {
makeNominalUsableFromInline(subNT, M);
makeDeclUsableFromInline(subNT, M);
}
}
});
Expand Down Expand Up @@ -235,6 +235,9 @@ prepareInstructionForSerialization(SILInstruction *inst) {
[this](SILDeclRef method) { handleReferencedMethod(method); });
return;
}
if (auto *REAI = dyn_cast<RefElementAddrInst>(inst)) {
makeDeclUsableFromInline(REAI->getField(), M);
}
}

void CrossModuleSerializationSetup::handleReferencedFunction(SILFunction *func) {
Expand Down
17 changes: 17 additions & 0 deletions test/SILOptimizer/Inputs/cross-module-objc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Foundation

final class ObjcClass : NSObject {
fileprivate var ii: Int = 127
}

@inline(never)
func returnObjcClassMember<T>(_ c: ObjcClass, _ t: T) -> Int {
return c.ii
}

@inline(never)
public func callObjcClassMember<T>(_ t: T) -> Int {
let c = ObjcClass()
return returnObjcClassMember(c, t)
}

32 changes: 32 additions & 0 deletions test/SILOptimizer/cross-module-optimization-objc.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// First test: functional correctness

// RUN: %empty-directory(%t)
// RUN: %target-build-swift -O -wmo -parse-as-library -cross-module-optimization -emit-module -emit-module-path=%t/Test.swiftmodule -module-name=Test -I%t %S/Inputs/cross-module-objc.swift -c -o %t/test.o
// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %s -c -o %t/main.o
// RUN: %target-swiftc_driver %t/main.o %t/test.o -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT

// Check if it also works if the main module is compiled with -Onone:

// RUN: %target-build-swift -Onone -wmo -module-name=Main -I%t %s -c -o %t/main-onone.o
// RUN: %target-swiftc_driver %t/main-onone.o %t/test.o -o %t/a.out
// RUN: %target-run %t/a.out | %FileCheck %s -check-prefix=CHECK-OUTPUT

// REQUIRES: executable_test
// REQUIRES: objc_interop

// Second test: check if CMO really imports the SIL of functions in other modules.

// RUN: %target-build-swift -O -wmo -module-name=Main -I%t %s -Xllvm -sil-disable-pass=FunctionSignatureOpts -emit-sil | %FileCheck %s -check-prefix=CHECK-SIL


import Test

func testClass() {
// CHECK-OUTPUT: 127
// CHECK-SIL-DAG: sil shared [noinline] @$s4Test21returnObjcClassMemberySiAA0cD0C_xtlFSi_Tg5
print(callObjcClassMember(0))
}

testClass()