Skip to content

Commit 41a1b9c

Browse files
authored
Merge pull request #3500 from swiftwasm/main
[pull] swiftwasm from main
2 parents cd19d16 + 510afdf commit 41a1b9c

30 files changed

+661
-66
lines changed

include/swift/AST/ClangModuleLoader.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -238,6 +238,9 @@ class ClangModuleLoader : public ModuleLoader {
238238
SubstitutionMap subst) = 0;
239239

240240
virtual bool isCXXMethodMutating(const clang::CXXMethodDecl *method) = 0;
241+
242+
virtual Type importFunctionReturnType(const clang::FunctionDecl *clangDecl,
243+
DeclContext *dc) = 0;
241244
};
242245

243246
/// Describes a C++ template instantiation error.

include/swift/ClangImporter/ClangImporter.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,9 @@ class ClangImporter final : public ClangModuleLoader {
458458
DeclName importName(const clang::NamedDecl *D,
459459
clang::DeclarationName givenName);
460460

461+
Type importFunctionReturnType(const clang::FunctionDecl *clangDecl,
462+
DeclContext *dc) override;
463+
461464
Optional<std::string>
462465
getOrCreatePCH(const ClangImporterOptions &ImporterOptions,
463466
StringRef SwiftPCHHash);

include/swift/Option/Options.td

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -595,8 +595,13 @@ def experimental_cxx_stdlib :
595595

596596
def experimental_emit_module_separately:
597597
Flag<["-"], "experimental-emit-module-separately">,
598-
Flags<[FrontendOption, NoInteractiveOption, HelpHidden]>,
599-
HelpText<"Schedule a swift module emission job instead of a merge-modules job (new Driver only)">;
598+
Flags<[NoInteractiveOption, HelpHidden]>,
599+
HelpText<"Emit module files as a distinct job (new Driver only)">;
600+
601+
def no_emit_module_separately:
602+
Flag<["-"], "no-emit-module-separately">,
603+
Flags<[NoInteractiveOption, HelpHidden]>,
604+
HelpText<"Force using merge-module as the incremental build mode (new Driver only)">;
600605

601606
def requirement_machine_EQ : Joined<["-"], "requirement-machine=">,
602607
Flags<[FrontendOption, ModuleInterfaceOption]>,

include/swift/SIL/MemoryLocations.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ void dumpBits(const SmallBitVector &bits);
4343
///
4444
/// Memory locations are limited to addresses which are guaranteed to
4545
/// be not aliased, like @in/inout parameters and alloc_stack.
46-
/// Currently only a certain set of address instructions are supported:
47-
/// Specifically those instructions which are going to be included when SIL
48-
/// supports opaque values.
46+
/// Currently only a certain set of address instructions are supported, for
47+
/// details see `MemoryLocations::analyzeLocationUsesRecursively` and
48+
/// `MemoryLocations::analyzeAddrProjection`.
4949
class MemoryLocations {
5050
public:
5151

@@ -288,7 +288,7 @@ class MemoryLocations {
288288
/// not covered by sub-fields.
289289
const Bits &getNonTrivialLocations();
290290

291-
/// Debug dump the MemoryLifetime internals.
291+
/// Debug dump the MemoryLocations internals.
292292
void dump() const;
293293

294294
private:

include/swift/SIL/SILType.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -294,6 +294,10 @@ class SILType {
294294
/// even though they are technically trivial.
295295
bool isTrivial(const SILFunction &F) const;
296296

297+
/// True if the type is an empty tuple or an empty struct or a tuple or
298+
/// struct containing only empty types.
299+
bool isEmpty(const SILFunction &F) const;
300+
297301
/// True if the type, or the referenced type of an address type, is known to
298302
/// be a scalar reference-counted type such as a class, box, or thick function
299303
/// type. Returns false for non-trivial aggregates.

lib/AST/Decl.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7827,7 +7827,7 @@ FuncDecl *FuncDecl::createImported(ASTContext &Context, SourceLoc FuncLoc,
78277827
Type FnRetType,
78287828
GenericParamList *GenericParams,
78297829
DeclContext *Parent, ClangNode ClangN) {
7830-
assert(ClangN && FnRetType);
7830+
assert(ClangN);
78317831
auto *const FD = FuncDecl::createImpl(
78327832
Context, SourceLoc(), StaticSpellingKind::None, FuncLoc, Name, NameLoc,
78337833
Async, SourceLoc(), Throws, SourceLoc(), GenericParams, Parent, ClangN);

lib/ClangImporter/ClangImporter.cpp

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4277,6 +4277,19 @@ importName(const clang::NamedDecl *D,
42774277
getDeclName();
42784278
}
42794279

4280+
Type ClangImporter::importFunctionReturnType(
4281+
const clang::FunctionDecl *clangDecl, DeclContext *dc) {
4282+
bool isInSystemModule =
4283+
cast<ClangModuleUnit>(dc->getModuleScopeContext())->isSystemModule();
4284+
bool allowNSUIntegerAsInt =
4285+
Impl.shouldAllowNSUIntegerAsInt(isInSystemModule, clangDecl);
4286+
if (auto imported =
4287+
Impl.importFunctionReturnType(dc, clangDecl, allowNSUIntegerAsInt)
4288+
.getType())
4289+
return imported;
4290+
return dc->getASTContext().getNeverType();
4291+
}
4292+
42804293
bool ClangImporter::isInOverlayModuleForImportedModule(
42814294
const DeclContext *overlayDC,
42824295
const DeclContext *importedDC) {

lib/ClangImporter/ImportDecl.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4167,8 +4167,9 @@ namespace {
41674167
if (!bodyParams)
41684168
return nullptr;
41694169

4170-
importedType =
4171-
Impl.importFunctionReturnType(dc, decl, allowNSUIntegerAsInt);
4170+
if (decl->getReturnType()->isScalarType())
4171+
importedType =
4172+
Impl.importFunctionReturnType(dc, decl, allowNSUIntegerAsInt);
41724173
} else {
41734174
// Import the function type. If we have parameters, make sure their
41744175
// names get into the resulting function type.
@@ -4204,7 +4205,7 @@ namespace {
42044205
name = DeclName(Impl.SwiftContext, name.getBaseName(), bodyParams);
42054206
}
42064207

4207-
if (!importedType)
4208+
if (!bodyParams)
42084209
return nullptr;
42094210

42104211
auto loc = Impl.importSourceLoc(decl->getLocation());

lib/ClangImporter/ImportType.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1827,12 +1827,17 @@ ImportedType ClangImporter::Implementation::importFunctionParamsAndReturnType(
18271827
bool allowNSUIntegerAsInt =
18281828
shouldAllowNSUIntegerAsInt(isFromSystemModule, clangDecl);
18291829

1830+
// Only eagerly import the return type if it's not too expensive (the current
1831+
// huristic for that is if it's not a record type).
18301832
ImportedType importedType;
18311833
if (auto templateType =
18321834
dyn_cast<clang::TemplateTypeParmType>(clangDecl->getReturnType())) {
18331835
importedType = {findGenericTypeInGenericDecls(templateType, genericParams),
18341836
false};
1835-
} else {
1837+
} else if (!isa<clang::RecordType>(clangDecl->getReturnType()) ||
1838+
// TODO: we currently don't lazily load operator return types, but
1839+
// this should be trivial to add.
1840+
clangDecl->isOverloadedOperator()) {
18361841
importedType =
18371842
importFunctionReturnType(dc, clangDecl, allowNSUIntegerAsInt);
18381843
if (!importedType)

lib/SIL/IR/SILType.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,30 @@ bool SILType::isTrivial(const SILFunction &F) const {
104104
return F.getTypeLowering(contextType).isTrivial();
105105
}
106106

107+
bool SILType::isEmpty(const SILFunction &F) const {
108+
if (auto tupleTy = getAs<TupleType>()) {
109+
// A tuple is empty if it either has no elements or if all elements are
110+
// empty.
111+
for (unsigned idx = 0, num = tupleTy->getNumElements(); idx < num; ++idx) {
112+
if (!getTupleElementType(idx).isEmpty(F))
113+
return false;
114+
}
115+
return true;
116+
}
117+
if (StructDecl *structDecl = getStructOrBoundGenericStruct()) {
118+
// Also, a struct is empty if it either has no fields or if all fields are
119+
// empty.
120+
SILModule &module = F.getModule();
121+
TypeExpansionContext typeEx = F.getTypeExpansionContext();
122+
for (VarDecl *field : structDecl->getStoredProperties()) {
123+
if (!getFieldType(field, module, typeEx).isEmpty(F))
124+
return false;
125+
}
126+
return true;
127+
}
128+
return false;
129+
}
130+
107131
bool SILType::isReferenceCounted(SILModule &M) const {
108132
return M.Types.getTypeLowering(*this,
109133
TypeExpansionContext::minimal())

0 commit comments

Comments
 (0)