Skip to content

[IRGen] Replace duplicate code with new method #13720

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 3 additions & 1 deletion include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -471,7 +471,7 @@ class SILModule {
/// Look for a global variable by name.
///
/// \return null if this module has no such global variable
SILGlobalVariable *lookUpGlobalVariable(StringRef name) const {
SILGlobalVariable *lookUpGlobalVariable(StringRef name) const {
return GlobalVariableMap.lookup(name);
}

Expand Down Expand Up @@ -712,6 +712,8 @@ class SILModule {
}
};

bool isDeclVisibleExternally(const ValueDecl *decl, const SILModule *module);

inline llvm::raw_ostream &operator<<(llvm::raw_ostream &OS, const SILModule &M){
M.print(OS);
return OS;
Expand Down
15 changes: 15 additions & 0 deletions lib/SIL/SIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,3 +115,18 @@ swift::getLinkageForProtocolConformance(const NormalProtocolConformance *C,
return (definition ? SILLinkage::Public : SILLinkage::PublicExternal);
}
}

bool swift::isDeclVisibleExternally(const ValueDecl *decl, const SILModule *module) {
AccessLevel access = decl->getEffectiveAccess();
switch (access) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
return false;
case AccessLevel::Internal:
return !module->isWholeModule();
case AccessLevel::Public:
case AccessLevel::Open:
return true;
}
llvm_unreachable("Unhandled access level in switch");
}
17 changes: 1 addition & 16 deletions lib/SILOptimizer/IPO/DeadFunctionElimination.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -407,22 +407,7 @@ class FunctionLivenessComputation {

/// Retrieve the visibility information from the AST.
bool isVisibleExternally(const ValueDecl *decl) {
AccessLevel access = decl->getEffectiveAccess();
SILLinkage linkage;
switch (access) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
linkage = SILLinkage::Private;
break;
case AccessLevel::Internal:
linkage = SILLinkage::Hidden;
break;
case AccessLevel::Public:
case AccessLevel::Open:
linkage = SILLinkage::Public;
break;
}
if (isPossiblyUsedExternally(linkage, Module->isWholeModule()))
if (isDeclVisibleExternally(decl, Module))
return true;

// If a vtable or witness table (method) is only visible in another module
Expand Down
11 changes: 1 addition & 10 deletions lib/SILOptimizer/IPO/GlobalOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -911,16 +911,7 @@ static bool canBeChangedExternally(SILGlobalVariable *SILG) {
// Use access specifiers from the declarations,
// if possible.
if (auto *Decl = SILG->getDecl()) {
switch (Decl->getEffectiveAccess()) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
return false;
case AccessLevel::Internal:
return !SILG->getModule().isWholeModule();
case AccessLevel::Public:
case AccessLevel::Open:
return true;
}
return isDeclVisibleExternally(Decl, &SILG->getModule());
}

if (SILG->getLinkage() == SILLinkage::Private)
Expand Down
21 changes: 1 addition & 20 deletions lib/SILOptimizer/IPO/GlobalPropertyOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -127,25 +127,6 @@ class GlobalPropertyOpt {
}
return false;
}

bool isVisibleExternally(VarDecl *decl) {
AccessLevel access = decl->getEffectiveAccess();
SILLinkage linkage;
switch (access) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
linkage = SILLinkage::Private;
break;
case AccessLevel::Internal:
linkage = SILLinkage::Hidden;
break;
case AccessLevel::Public:
case AccessLevel::Open:
linkage = SILLinkage::Public;
break;
}
return isPossiblyUsedExternally(linkage, M.isWholeModule());
}

static bool canAddressEscape(SILValue V, bool acceptStore);

Expand All @@ -154,7 +135,7 @@ class GlobalPropertyOpt {
Entry * &entry = FieldEntries[Field];
if (!entry) {
entry = new (EntryAllocator.Allocate()) Entry(SILValue(), Field);
if (isVisibleExternally(Field))
if (isDeclVisibleExternally(Field, &M))
setAddressEscapes(entry);
}
return entry;
Expand Down
36 changes: 3 additions & 33 deletions lib/SILOptimizer/IPO/LetPropertiesOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,18 +158,12 @@ void LetPropertiesOpt::optimizeLetPropertyAccess(VarDecl *Property,
DEBUG(llvm::dbgs() << "Replacing access to property '" << *Property
<< "' by its constant initializer\n");

auto PropertyAccess = Property->getEffectiveAccess();
auto TypeAccess = Ty->getEffectiveAccess();
auto CanRemove = false;

// Check if a given let property can be removed, because it
// is not accessible elsewhere. This can happen if this property
// is private or if it is internal and WMO mode is used.
if (TypeAccess <= AccessLevel::FilePrivate ||
PropertyAccess <= AccessLevel::FilePrivate
|| ((TypeAccess <= AccessLevel::Internal ||
PropertyAccess <= AccessLevel::Internal) &&
Module->isWholeModule())) {
if (!isDeclVisibleExternally(Ty, Module) || !isDeclVisibleExternally(Property, Module)) {
CanRemove = true;
DEBUG(llvm::dbgs() << "Storage for property '" << *Property
<< "' can be eliminated\n");
Expand Down Expand Up @@ -289,28 +283,7 @@ static bool isSameInitSequence(const InitSequence &LHS,

/// Check if a given let property can be assigned externally.
static bool isAssignableExternally(VarDecl *Property, SILModule *Module) {
AccessLevel access = Property->getEffectiveAccess();
SILLinkage linkage;
switch (access) {
case AccessLevel::Private:
case AccessLevel::FilePrivate:
linkage = SILLinkage::Private;
DEBUG(llvm::dbgs() << "Property " << *Property << " has private access\n");
break;
case AccessLevel::Internal:
linkage = SILLinkage::Hidden;
DEBUG(llvm::dbgs() << "Property " << *Property << " has internal access\n");
break;
case AccessLevel::Public:
case AccessLevel::Open:
linkage = SILLinkage::Public;
DEBUG(llvm::dbgs() << "Property " << *Property << " has public access\n");
break;
}

DEBUG(llvm::dbgs() << "Module of " << *Property << " WMO mode is: " << Module->isWholeModule() << "\n");

if (isPossiblyUsedExternally(linkage, Module->isWholeModule())) {
if (isDeclVisibleExternally(Property, Module)) {
// If at least one of the properties of the enclosing type cannot be
// used externally, then no initializer can be implemented externally as
// it wouldn't be able to initialize such a property.
Expand All @@ -331,10 +304,7 @@ static bool isAssignableExternally(VarDecl *Property, SILModule *Module) {
// it is a whole module compilation. In this case, no external initializer
// may exist.
for (auto SP : Ty->getStoredProperties()) {
auto storedPropertyAccess = SP->getEffectiveAccess();
if (storedPropertyAccess <= AccessLevel::FilePrivate ||
(storedPropertyAccess <= AccessLevel::Internal &&
Module->isWholeModule())) {
if (!isDeclVisibleExternally(SP, Module)) {
DEBUG(llvm::dbgs() << "Property " << *Property
<< " cannot be set externally\n");
return false;
Expand Down
12 changes: 1 addition & 11 deletions lib/SILOptimizer/Transforms/SpeculativeDevirtualizer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -257,18 +257,8 @@ static bool isDefaultCaseKnown(ClassHierarchyAnalysis *CHA,
return false;

// Only consider 'private' members, unless we are in whole-module compilation.
switch (CD->getEffectiveAccess()) {
case AccessLevel::Open:
if (isDeclVisibleExternally(CD, &AI.getModule()))
return false;
case AccessLevel::Public:
Copy link
Author

@dtweston dtweston Jan 10, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The comment says we should only consider private members, but it treats public members the same as internal members, which makes me think I'm possibly misunderstanding this.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably just an oversight.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok. I think something in this later change broke a SILOptimizer test (global_property_opt.sil), but I wasn't yet able to figure out what it meant.

Here's a gist with the difference: https://gist.github.com/dtweston/1ce13a14a0cadbfe2088aa647ee7a891

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This test breakage has gone away, so I think everything's fine now.

case AccessLevel::Internal:
if (!AI.getModule().isWholeModule())
return false;
break;
case AccessLevel::FilePrivate:
case AccessLevel::Private:
break;
}

// This is a private or a module internal class.
//
Expand Down
25 changes: 1 addition & 24 deletions lib/SILOptimizer/Utils/Devirtualize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,18 +171,8 @@ static bool isKnownFinalClass(ClassDecl *CD, SILModule &M,
return false;

// Only consider 'private' members, unless we are in whole-module compilation.
switch (CD->getEffectiveAccess()) {
case AccessLevel::Open:
if (isDeclVisibleExternally(CD, &M))
return false;
case AccessLevel::Public:
case AccessLevel::Internal:
if (!M.isWholeModule())
return false;
break;
case AccessLevel::FilePrivate:
case AccessLevel::Private:
break;
}

// Take the ClassHierarchyAnalysis into account.
// If a given class has no subclasses and
Expand All @@ -192,19 +182,6 @@ static bool isKnownFinalClass(ClassDecl *CD, SILModule &M,
// of devirtualization.
if (CHA) {
if (!CHA->hasKnownDirectSubclasses(CD)) {
switch (CD->getEffectiveAccess()) {
case AccessLevel::Open:
return false;
case AccessLevel::Public:
case AccessLevel::Internal:
if (!M.isWholeModule())
return false;
break;
case AccessLevel::FilePrivate:
case AccessLevel::Private:
break;
}

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The above code also appeared on lines 174 to 185, so it seems redundant to duplicate them here.

return true;
}
}
Expand Down