Skip to content
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

[pull] swiftwasm from main #5515

Merged
merged 15 commits into from
Jun 12, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
19 changes: 10 additions & 9 deletions include/swift/AST/Decl.h
Original file line number Diff line number Diff line change
Expand Up @@ -659,7 +659,7 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {
HasAnyUnavailableValues : 1
);

SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
SWIFT_INLINE_BITFIELD(ModuleDecl, TypeDecl, 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1,
/// If the module is compiled as static library.
StaticLibrary : 1,

Expand Down Expand Up @@ -709,7 +709,10 @@ class alignas(1 << DeclAlignInBits) Decl : public ASTAllocated<Decl> {

/// If the map from @objc provided name to top level swift::Decl in this
/// module is populated
ObjCNameLookupCachePopulated : 1
ObjCNameLookupCachePopulated : 1,

/// Whether this module has been built with C++ interoperability enabled.
HasCxxInteroperability : 1
);

SWIFT_INLINE_BITFIELD(PrecedenceGroupDecl, Decl, 1+2,
Expand Down Expand Up @@ -5293,10 +5296,10 @@ class AbstractStorageDecl : public ValueDecl {

/// Overwrite the registered implementation-info. This should be
/// used carefully.
void setImplInfo(StorageImplInfo implInfo) {
LazySemanticInfo.ImplInfoComputed = 1;
ImplInfo = implInfo;
}
void setImplInfo(StorageImplInfo implInfo);

/// Cache the implementation-info, for use by the request-evaluator.
void cacheImplInfo(StorageImplInfo implInfo);

ReadImplKind getReadImpl() const {
return getImplInfo().getReadImpl();
Expand All @@ -5311,9 +5314,7 @@ class AbstractStorageDecl : public ValueDecl {

/// Return true if this is a VarDecl that has storage associated with
/// it.
bool hasStorage() const {
return getImplInfo().hasStorage();
}
bool hasStorage() const;

/// Return true if this storage has the basic accessors/capability
/// to be mutated. This is generally constant after the accessors are
Expand Down
13 changes: 13 additions & 0 deletions include/swift/AST/DiagnosticsSema.def
Original file line number Diff line number Diff line change
Expand Up @@ -874,6 +874,12 @@ ERROR(need_hermetic_seal_to_import_module,none,
"module %0 was built with -experimental-hermetic-seal-at-link, but "
"current compilation does not have -experimental-hermetic-seal-at-link",
(Identifier))
ERROR(need_cxx_interop_to_import_module,none,
"module %0 was built with C++ interoperability enabled, but "
"current compilation does not enable C++ interoperability",
(Identifier))
NOTE(enable_cxx_interop_docs,none,
"visit https://www.swift.org/documentation/cxx-interop/project-build-setup to learn how to enable C++ interoperability", ())

ERROR(modularization_issue_decl_moved,Fatal,
"reference to %select{top-level declaration|type}0 %1 broken by a context change; "
Expand Down Expand Up @@ -7181,6 +7187,13 @@ ERROR(invalid_macro_role_for_macro_syntax,none,
(unsigned))
ERROR(macro_cannot_introduce_names,none,
"'%0' macros are not allowed to introduce names", (StringRef))
ERROR(macro_accessor_missing_from_expansion,none,
"expansion of macro %0 did not produce a %select{non-|}1observing "
"accessor",
(DeclName, bool))
ERROR(macro_init_accessor_not_documented,none,
"expansion of macro %0 produced an unexpected 'init' accessor",
(DeclName))

ERROR(macro_resolve_circular_reference, none,
"circular reference resolving %select{freestanding|attached}0 macro %1",
Expand Down
6 changes: 6 additions & 0 deletions include/swift/AST/Evaluator.h
Original file line number Diff line number Diff line change
Expand Up @@ -316,6 +316,12 @@ class Evaluator {
cache.insert<Request>(request, std::move(output));
}

template<typename Request,
typename std::enable_if<!Request::hasExternalCache>::type* = nullptr>
bool hasCachedResult(const Request &request) {
return cache.find_as(request) != cache.end<Request>();
}

/// Do not introduce new callers of this function.
template<typename Request,
typename std::enable_if<!Request::hasExternalCache>::type* = nullptr>
Expand Down
8 changes: 8 additions & 0 deletions include/swift/AST/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -654,6 +654,14 @@ class ModuleDecl
Bits.ModuleDecl.HasHermeticSealAtLink = enabled;
}

/// Returns true if this module was built with C++ interoperability enabled.
bool hasCxxInteroperability() const {
return Bits.ModuleDecl.HasCxxInteroperability;
}
void setHasCxxInteroperability(bool enabled = true) {
Bits.ModuleDecl.HasCxxInteroperability = enabled;
}

/// \returns true if this module is a system module; note that the StdLib is
/// considered a system module.
bool isSystemModule() const {
Expand Down
7 changes: 7 additions & 0 deletions include/swift/AST/NameLookup.h
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,13 @@ void forEachPotentialResolvedMacro(
llvm::function_ref<void(MacroDecl *, const MacroRoleAttr *)> body
);

/// For each macro with the given role that might be attached to the given
/// declaration, call the body.
void forEachPotentialAttachedMacro(
Decl *decl, MacroRole role,
llvm::function_ref<void(MacroDecl *macro, const MacroRoleAttr *)> body
);

} // end namespace namelookup

/// Describes an inherited nominal entry.
Expand Down
20 changes: 20 additions & 0 deletions include/swift/AST/TypeCheckRequests.h
Original file line number Diff line number Diff line change
Expand Up @@ -1686,6 +1686,26 @@ class InitAccessorPropertiesRequest :
ArrayRef<VarDecl *>
evaluate(Evaluator &evaluator, NominalTypeDecl *decl) const;

// Evaluation.
bool evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;

public:
bool isCached() const { return true; }
};

class HasStorageRequest :
public SimpleRequest<HasStorageRequest,
bool(AbstractStorageDecl *),
RequestFlags::Cached> {
public:
using SimpleRequest::SimpleRequest;

private:
friend SimpleRequest;

// Evaluation.
bool evaluate(Evaluator &evaluator, AbstractStorageDecl *decl) const;

public:
bool isCached() const { return true; }
};
Expand Down
3 changes: 3 additions & 0 deletions include/swift/AST/TypeCheckerTypeIDZone.def
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,9 @@ SWIFT_REQUEST(TypeChecker, SelfAccessKindRequest, SelfAccessKind(FuncDecl *),
SWIFT_REQUEST(TypeChecker, StorageImplInfoRequest,
StorageImplInfo(AbstractStorageDecl *), SeparatelyCached,
NoLocationInfo)
SWIFT_REQUEST(TypeChecker, HasStorageRequest,
bool(AbstractStorageDecl *), Cached,
NoLocationInfo)
SWIFT_REQUEST(TypeChecker, StoredPropertiesAndMissingMembersRequest,
ArrayRef<Decl *>(NominalTypeDecl *), Cached, NoLocationInfo)
SWIFT_REQUEST(TypeChecker, StoredPropertiesRequest,
Expand Down
4 changes: 4 additions & 0 deletions include/swift/Basic/LangOptions.h
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,10 @@ namespace swift {
/// Imports getters and setters as computed properties.
bool CxxInteropGettersSettersAsProperties = false;

/// Should the compiler require C++ interoperability to be enabled
/// when importing Swift modules that enable C++ interoperability.
bool RequireCxxInteropToImportCxxInteropModule = true;

/// On Darwin platforms, use the pre-stable ABI's mark bit for Swift
/// classes instead of the stable ABI's bit. This is needed when
/// targeting OSes prior to macOS 10.14.4 and iOS 12.2, where
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Option/FrontendOptions.td
Original file line number Diff line number Diff line change
Expand Up @@ -865,6 +865,11 @@ def cxx_interop_getters_setters_as_properties :
HelpText<"Import getters and setters as computed properties in Swift">,
Flags<[FrontendOption, HelpHidden]>;

def cxx_interop_disable_requirement_at_import :
Flag<["-"], "disable-cxx-interop-requirement-at-import">,
HelpText<"Do not require C++ interoperability to be enabled when importing a Swift module that enables C++ interoperability">,
Flags<[FrontendOption, HelpHidden]>;

def use_malloc : Flag<["-"], "use-malloc">,
HelpText<"Allocate internal data structures using malloc "
"(for memory debugging)">;
Expand Down
2 changes: 0 additions & 2 deletions include/swift/SIL/SILModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -395,9 +395,7 @@ class SILModule {
/// Action to be executed for serializing the SILModule.
ActionCallback SerializeSILAction;

#ifndef NDEBUG
BasicBlockNameMapType basicBlockNames;
#endif

SILModule(llvm::PointerUnion<FileUnit *, ModuleDecl *> context,
Lowering::TypeConverter &TC, const SILOptions &Options,
Expand Down
5 changes: 5 additions & 0 deletions include/swift/Serialization/Validation.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ class ExtendedValidationInfo {
unsigned IsBuiltFromInterface : 1;
unsigned IsAllowModuleWithCompilerErrorsEnabled : 1;
unsigned IsConcurrencyChecked : 1;
unsigned HasCxxInteroperability : 1;
} Bits;
public:
ExtendedValidationInfo() : Bits() {}
Expand Down Expand Up @@ -232,6 +233,10 @@ class ExtendedValidationInfo {
void setIsConcurrencyChecked(bool val = true) {
Bits.IsConcurrencyChecked = val;
}
bool hasCxxInteroperability() const { return Bits.HasCxxInteroperability; }
void setHasCxxInteroperability(bool val) {
Bits.HasCxxInteroperability = val;
}
};

struct SearchPath {
Expand Down
29 changes: 28 additions & 1 deletion lib/AST/Decl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2525,7 +2525,7 @@ AbstractStorageDecl::getAccessStrategy(AccessSemantics semantics,
ResilienceExpansion expansion) const {
switch (semantics) {
case AccessSemantics::DirectToStorage:
assert(hasStorage());
assert(hasStorage() || getASTContext().Diags.hadAnyError());
return AccessStrategy::getStorage();

case AccessSemantics::DistributedThunk:
Expand Down Expand Up @@ -6393,13 +6393,40 @@ bool ProtocolDecl::hasCircularInheritedProtocols() const {
ctx.evaluator, HasCircularInheritedProtocolsRequest{mutableThis}, true);
}

bool AbstractStorageDecl::hasStorage() const {
ASTContext &ctx = getASTContext();
return evaluateOrDefault(ctx.evaluator,
HasStorageRequest{const_cast<AbstractStorageDecl *>(this)},
false);
}

StorageImplInfo AbstractStorageDecl::getImplInfo() const {
ASTContext &ctx = getASTContext();
return evaluateOrDefault(ctx.evaluator,
StorageImplInfoRequest{const_cast<AbstractStorageDecl *>(this)},
StorageImplInfo::getSimpleStored(StorageIsMutable));
}

void AbstractStorageDecl::cacheImplInfo(StorageImplInfo implInfo) {
LazySemanticInfo.ImplInfoComputed = 1;
ImplInfo = implInfo;
}

void AbstractStorageDecl::setImplInfo(StorageImplInfo implInfo) {
cacheImplInfo(implInfo);

if (isImplicit()) {
auto &evaluator = getASTContext().evaluator;
HasStorageRequest request{this};
if (!evaluator.hasCachedResult(request))
evaluator.cacheOutput(request, implInfo.hasStorage());
else {
assert(
evaluateOrDefault(evaluator, request, false) == implInfo.hasStorage());
}
}
}

bool AbstractStorageDecl::hasPrivateAccessor() const {
for (auto accessor : getAllAccessors()) {
if (hasPrivateOrFilePrivateFormalAccess(accessor))
Expand Down
1 change: 1 addition & 0 deletions lib/AST/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -646,6 +646,7 @@ ModuleDecl::ModuleDecl(Identifier name, ASTContext &ctx,
Bits.ModuleDecl.HasHermeticSealAtLink = 0;
Bits.ModuleDecl.IsConcurrencyChecked = 0;
Bits.ModuleDecl.ObjCNameLookupCachePopulated = 0;
Bits.ModuleDecl.HasCxxInteroperability = 0;
}

void ModuleDecl::setIsSystemModule(bool flag) {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/NameLookup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1627,7 +1627,7 @@ void namelookup::forEachPotentialResolvedMacro(

/// For each macro with the given role that might be attached to the given
/// declaration, call the body.
static void forEachPotentialAttachedMacro(
void namelookup::forEachPotentialAttachedMacro(
Decl *decl, MacroRole role,
llvm::function_ref<void(MacroDecl *macro, const MacroRoleAttr *)> body
) {
Expand Down
2 changes: 1 addition & 1 deletion lib/AST/TypeCheckRequests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ StorageImplInfoRequest::getCachedResult() const {

void StorageImplInfoRequest::cacheResult(StorageImplInfo value) const {
auto *storage = std::get<0>(getStorage());
storage->setImplInfo(value);
storage->cacheImplInfo(value);
}

//----------------------------------------------------------------------------//
Expand Down
2 changes: 2 additions & 0 deletions lib/ClangImporter/ClangImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
#include "swift/AST/NameLookupRequests.h"
#include "swift/AST/PrettyStackTrace.h"
#include "swift/AST/SourceFile.h"
#include "swift/AST/TypeCheckRequests.h"
#include "swift/AST/Types.h"
#include "swift/Basic/Defer.h"
#include "swift/Basic/Platform.h"
Expand Down Expand Up @@ -5056,6 +5057,7 @@ cloneBaseMemberDecl(ValueDecl *decl, DeclContext *newContext) {
out->setIsObjC(var->isObjC());
out->setIsDynamic(var->isDynamic());
out->copyFormalAccessFrom(var);
out->getASTContext().evaluator.cacheOutput(HasStorageRequest{out}, false);
out->setAccessors(SourceLoc(),
makeBaseClassMemberAccessors(newContext, out, var),
SourceLoc());
Expand Down
1 change: 1 addition & 0 deletions lib/ClangImporter/ImportDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ void ClangImporter::Implementation::makeComputed(AbstractStorageDecl *storage,
AccessorDecl *getter,
AccessorDecl *setter) {
assert(getter);
storage->getASTContext().evaluator.cacheOutput(HasStorageRequest{storage}, false);
if (setter) {
storage->setImplInfo(StorageImplInfo::getMutableComputed());
storage->setAccessors(SourceLoc(), {getter, setter}, SourceLoc());
Expand Down
2 changes: 2 additions & 0 deletions lib/Frontend/CompilerInvocation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1043,6 +1043,8 @@ static bool ParseLangArgs(LangOptions &Opts, ArgList &Args,
Args.hasArg(OPT_experimental_c_foreign_reference_types);

Opts.CxxInteropGettersSettersAsProperties = Args.hasArg(OPT_cxx_interop_getters_setters_as_properties);
Opts.RequireCxxInteropToImportCxxInteropModule =
!Args.hasArg(OPT_cxx_interop_disable_requirement_at_import);

Opts.VerifyAllSubstitutionMaps |= Args.hasArg(OPT_verify_all_substitution_maps);

Expand Down
3 changes: 3 additions & 0 deletions lib/Frontend/Frontend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1319,6 +1319,9 @@ ModuleDecl *CompilerInstance::getMainModule() const {
MainModule->setResilienceStrategy(ResilienceStrategy::Resilient);
if (Invocation.getLangOptions().isSwiftVersionAtLeast(6))
MainModule->setIsConcurrencyChecked(true);
if (Invocation.getLangOptions().EnableCXXInterop &&
Invocation.getLangOptions().RequireCxxInteropToImportCxxInteropModule)
MainModule->setHasCxxInteroperability();

// Register the main module with the AST context.
Context->addLoadedModule(MainModule);
Expand Down
15 changes: 11 additions & 4 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2942,10 +2942,9 @@ void IRGenSILFunction::visitAllocGlobalInst(AllocGlobalInst *i) {

void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
SILGlobalVariable *var = i->getReferencedGlobal();
SILType loweredTy = var->getLoweredTypeInContext(getExpansionContext());
assert(loweredTy == i->getType().getObjectType());
SILType loweredTy = var->getLoweredType();
auto &ti = getTypeInfo(loweredTy);

auto expansion = IGM.getResilienceExpansionForLayout(var);

// If the variable is empty in all resilience domains that can see it,
Expand All @@ -2968,7 +2967,15 @@ void IRGenSILFunction::visitGlobalAddrInst(GlobalAddrInst *i) {
// Otherwise, the static storage for the global consists of a fixed-size
// buffer; project it.
addr = emitProjectValueInBuffer(*this, loweredTy, addr);



// Get the address of the type in context.
SILType loweredTyInContext = var->getLoweredTypeInContext(getExpansionContext());
auto &tiInContext = getTypeInfo(loweredTyInContext);
auto ptr = Builder.CreateBitOrPointerCast(addr.getAddress(),
tiInContext.getStorageType()->getPointerTo());
addr = Address(ptr, tiInContext.getStorageType(),
tiInContext.getBestKnownAlignment());
setLoweredAddress(i, addr);
}

Expand Down