Skip to content

Commit f3f2d2f

Browse files
authored
Merge pull request #4044 from swiftwasm/main
2 parents 0a80fcc + 871c6b9 commit f3f2d2f

36 files changed

+541
-440
lines changed

cmake/modules/StandaloneOverlay.cmake

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,10 @@
1+
# In some configurations (e.g. back deploy concurrency) we
2+
# configure the build from the root of the Swift repo but we skip
3+
# stdlib/CMakeLists.txt, with the risk of missing important parameters.
4+
# To account for this scenario, we include the stdlib options
5+
# before the guard
6+
include(${CMAKE_CURRENT_LIST_DIR}/../../stdlib/cmake/modules/StdlibOptions.cmake)
7+
18
# CMAKE_SOURCE_DIR is the directory that cmake got initially invoked on.
29
# CMAKE_CURRENT_SOURCE_DIR is the current directory. If these are equal, it's
310
# a top-level build of the CMAKE_SOURCE_DIR. Otherwise, define a guard variable
@@ -85,10 +92,6 @@ set(SWIFT_DARWIN_MODULE_ARCHS "" CACHE STRING
8592
targets on Darwin platforms. These targets are in addition to the full \
8693
library targets.")
8794

88-
option(SWIFT_STDLIB_SHORT_MANGLING_LOOKUPS
89-
"Build stdlib with fast-path context descriptor lookups based on well-known short manglings."
90-
TRUE)
91-
9295
# -----------------------------------------------------------------------------
9396
# Constants
9497

include/swift/AST/TBDGenRequests.h

Lines changed: 27 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,13 @@ class TBDGenDescriptor final {
5050
FileOrModule Input;
5151
TBDGenOptions Opts;
5252

53-
TBDGenDescriptor(FileOrModule input, const TBDGenOptions &opts)
54-
: Input(input), Opts(opts) {
53+
/// Symbols (e.g. function names) which are made public by the
54+
/// CrossModuleOptimization pass and therefore must be included in the TBD file.
55+
TBDSymbolSetPtr publicCMOSymbols;
56+
57+
TBDGenDescriptor(FileOrModule input, const TBDGenOptions &opts,
58+
TBDSymbolSetPtr publicCMOSymbols)
59+
: Input(input), Opts(opts), publicCMOSymbols(publicCMOSymbols) {
5560
assert(input);
5661
}
5762

@@ -73,17 +78,21 @@ class TBDGenDescriptor final {
7378
const StringRef getDataLayoutString() const;
7479
const llvm::Triple &getTarget() const;
7580

81+
TBDSymbolSetPtr getPublicCMOSymbols() const { return publicCMOSymbols; }
82+
7683
bool operator==(const TBDGenDescriptor &other) const;
7784
bool operator!=(const TBDGenDescriptor &other) const {
7885
return !(*this == other);
7986
}
8087

81-
static TBDGenDescriptor forFile(FileUnit *file, const TBDGenOptions &opts) {
82-
return TBDGenDescriptor(file, opts);
88+
static TBDGenDescriptor forFile(FileUnit *file, const TBDGenOptions &opts,
89+
TBDSymbolSetPtr publicCMOSymbols) {
90+
return TBDGenDescriptor(file, opts, publicCMOSymbols);
8391
}
8492

85-
static TBDGenDescriptor forModule(ModuleDecl *M, const TBDGenOptions &opts) {
86-
return TBDGenDescriptor(M, opts);
93+
static TBDGenDescriptor forModule(ModuleDecl *M, const TBDGenOptions &opts,
94+
TBDSymbolSetPtr publicCMOSymbols) {
95+
return TBDGenDescriptor(M, opts, publicCMOSymbols);
8796
}
8897
};
8998

@@ -154,6 +163,9 @@ class SymbolSource {
154163
/// A symbol used to customize linker behavior, introduced by TBDGen.
155164
LinkerDirective,
156165

166+
/// A symbol which was made public by the CrossModuleOptimization pass.
167+
CrossModuleOptimization,
168+
157169
/// A symbol with an unknown origin.
158170
// FIXME: This should be eliminated.
159171
Unknown
@@ -173,7 +185,8 @@ class SymbolSource {
173185
irEntity = entity;
174186
}
175187
explicit SymbolSource(Kind kind) : kind(kind) {
176-
assert(kind == Kind::LinkerDirective || kind == Kind::Unknown);
188+
assert(kind == Kind::LinkerDirective || kind == Kind::Unknown ||
189+
kind == Kind::CrossModuleOptimization);
177190
}
178191

179192
public:
@@ -186,6 +199,9 @@ class SymbolSource {
186199
static SymbolSource forLinkerDirective() {
187200
return SymbolSource{Kind::LinkerDirective};
188201
}
202+
static SymbolSource forCrossModuleOptimization() {
203+
return SymbolSource{Kind::CrossModuleOptimization};
204+
}
189205
static SymbolSource forUnknown() {
190206
return SymbolSource{Kind::Unknown};
191207
}
@@ -194,6 +210,10 @@ class SymbolSource {
194210
return kind == Kind::LinkerDirective;
195211
}
196212

213+
bool isFromCrossModuleOptimization() const {
214+
return kind == Kind::CrossModuleOptimization;
215+
}
216+
197217
SILDeclRef getSILDeclRef() const {
198218
assert(kind == Kind::SIL);
199219
return silDeclRef;

include/swift/Frontend/Frontend.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,7 @@ class CompilerInstance {
432432
std::unique_ptr<ASTContext> Context;
433433
std::unique_ptr<Lowering::TypeConverter> TheSILTypes;
434434
std::unique_ptr<DiagnosticVerifier> DiagVerifier;
435+
TBDSymbolSetPtr publicCMOSymbols;
435436

436437
/// A cache describing the set of inter-module dependencies that have been queried.
437438
/// Null if not present.
@@ -583,6 +584,10 @@ class CompilerInstance {
583584
/// file.
584585
SourceFile *getCodeCompletionFile() const;
585586

587+
/// Return the symbols (e.g. function names) which are made public by the
588+
/// CrossModuleOptimization pass and therefore must be included in the TBD file.
589+
TBDSymbolSetPtr getPublicCMOSymbols() const { return publicCMOSymbols; }
590+
586591
private:
587592
/// Set up the file system by loading and validating all VFS overlay YAML
588593
/// files. If the process of validating VFS files failed, or the overlay

include/swift/Reflection/TypeRef.h

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,22 +38,13 @@ enum class TypeRefKind {
3838
#undef TYPEREF
3939
};
4040

41-
// MSVC reports an error if we use "template"
42-
// Clang reports an error if we don't use "template"
43-
#if defined(__clang__) || defined(__GNUC__)
44-
# define DEPENDENT_TEMPLATE template
45-
#else
46-
# define DEPENDENT_TEMPLATE
47-
#endif
48-
4941
#define FIND_OR_CREATE_TYPEREF(Allocator, TypeRefTy, ...) \
5042
auto ID = Profile(__VA_ARGS__); \
51-
const auto Entry = Allocator.TypeRefTy##s.find(ID); \
52-
if (Entry != Allocator.TypeRefTy##s.end()) \
43+
const auto Entry = Allocator.TypeRefTy##s.find(ID); \
44+
if (Entry != Allocator.TypeRefTy##s.end()) \
5345
return Entry->second; \
54-
const auto TR = \
55-
Allocator.DEPENDENT_TEMPLATE makeTypeRef<TypeRefTy>(__VA_ARGS__); \
56-
Allocator.TypeRefTy##s.insert({ID, TR}); \
46+
const auto TR = Allocator.template makeTypeRef<TypeRefTy>(__VA_ARGS__); \
47+
Allocator.TypeRefTy##s.insert({ID, TR}); \
5748
return TR;
5849

5950
/// An identifier containing the unique bit pattern made up of all of the

include/swift/SIL/SILBridging.h

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,10 @@
1515

1616
#include "BridgedSwiftObject.h"
1717
#include <stddef.h>
18-
#include <string>
18+
19+
#ifdef __cplusplus
20+
extern "C" {
21+
#endif
1922

2023
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2124

@@ -161,19 +164,19 @@ BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,
161164
BridgedSlab slab);
162165

163166
BridgedStringRef SILFunction_getName(BridgedFunction function);
164-
std::string SILFunction_debugDescription(BridgedFunction function);
167+
BridgedStringRef SILFunction_debugDescription(BridgedFunction function);
165168
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
166169
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
167170
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
168171
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function);
169172

170173
BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
171-
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
174+
BridgedStringRef SILGlobalVariable_debugDescription(BridgedGlobalVar global);
172175

173176
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
174177
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
175178
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
176-
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
179+
BridgedStringRef SILBasicBlock_debugDescription(BridgedBasicBlock block);
177180
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
178181
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
179182
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
@@ -188,7 +191,7 @@ OptionalBridgedOperand Operand_nextUse(BridgedOperand);
188191
BridgedInstruction Operand_getUser(BridgedOperand);
189192
SwiftInt Operand_isTypeDependent(BridgedOperand);
190193

191-
std::string SILNode_debugDescription(BridgedNode node);
194+
BridgedStringRef SILNode_debugDescription(BridgedNode node);
192195
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
193196
BridgedType SILValue_getType(BridgedValue value);
194197

@@ -242,4 +245,8 @@ BridgedInstruction SILBuilder_createIntegerLiteral(BridgedInstruction insertionP
242245

243246
SWIFT_END_NULLABILITY_ANNOTATIONS
244247

248+
#ifdef __cplusplus
249+
} // extern "C"
250+
#endif
251+
245252
#endif

include/swift/SIL/SILModule.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
#include "swift/SIL/SILVTable.h"
3939
#include "swift/SIL/SILWitnessTable.h"
4040
#include "swift/SIL/TypeLowering.h"
41+
#include "swift/TBDGen/TBDGen.h"
4142
#include "llvm/ADT/ArrayRef.h"
4243
#include "llvm/ADT/FoldingSet.h"
4344
#include "llvm/ADT/MapVector.h"
@@ -367,6 +368,10 @@ class SILModule {
367368

368369
/// Folding set for key path patterns.
369370
llvm::FoldingSet<KeyPathPattern> KeyPathPatterns;
371+
372+
/// Symbols (e.g. function names) which are made public by the
373+
/// CrossModuleOptimization pass and therefore must be included in the TBD file.
374+
TBDSymbolSetPtr publicCMOSymbols;
370375

371376
public:
372377
~SILModule();
@@ -506,6 +511,12 @@ class SILModule {
506511

507512
const SILOptions &getOptions() const { return Options; }
508513

514+
/// Return the symbols (e.g. function names) which are made public by the
515+
/// CrossModuleOptimization pass and therefore must be included in the TBD file.
516+
TBDSymbolSetPtr getPublicCMOSymbols() { return publicCMOSymbols; }
517+
518+
void addPublicCMOSymbol(StringRef symbol);
519+
509520
using iterator = FunctionListType::iterator;
510521
using const_iterator = FunctionListType::const_iterator;
511522
FunctionListType &getFunctionList() { return functions; }

include/swift/SILOptimizer/PassManager/Passes.def

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -110,8 +110,8 @@ PASS(AccessEnforcementSelection, "access-enforcement-selection",
110110
"Access Enforcement Selection")
111111
PASS(AccessEnforcementWMO, "access-enforcement-wmo",
112112
"Access Enforcement Whole Module Optimization")
113-
PASS(CrossModuleSerializationSetup, "cross-module-serialization-setup",
114-
"Setup serialization flags for cross-module optimization")
113+
PASS(CrossModuleOptimization, "cmo",
114+
"Perform cross-module optimization")
115115
PASS(AccessSummaryDumper, "access-summary-dump",
116116
"Dump Address Parameter Access Summary")
117117
PASS(AccessStorageAnalysisDumper, "access-storage-analysis-dump",
@@ -405,8 +405,6 @@ PASS(SimplifyUnreachableContainingBlocks, "simplify-unreachable-containing-block
405405
"Utility pass. Removes all non-term insts from blocks with unreachable terms")
406406
PASS(SerializeSILPass, "serialize-sil",
407407
"Utility pass. Serializes the current SILModule")
408-
PASS(CMOSerializeSILPass, "cmo-serialize-sil",
409-
"Utility pass. Serializes the current SILModule for cross-module-optimization")
410408
PASS(YieldOnceCheck, "yield-once-check",
411409
"Check correct usage of yields in yield-once coroutines")
412410
PASS(OSLogOptimization, "os-log-optimization", "Optimize os log calls")

include/swift/TBDGen/TBDGen.h

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
#include "llvm/ADT/StringSet.h"
1717
#include "swift/Basic/Version.h"
1818
#include <vector>
19+
#include <set>
20+
#include <string>
21+
#include <memory>
1922

2023
namespace llvm {
2124
class raw_ostream;
@@ -104,12 +107,26 @@ struct TBDGenOptions {
104107
}
105108
};
106109

110+
/// Used for symbols which are made public by the CrossModuleOptimization pass
111+
/// and therefore must be included in the TBD file.
112+
///
113+
/// We cannot use llvm::StringSet, because we need deterministic iteration order.
114+
using TBDSymbolSet = std::set<std::string>;
115+
116+
/// A pointer to TBDSymbolSet.
117+
///
118+
/// Do reference counting for memory management.
119+
/// This set is created in the optimizer and primarily stored in the SILModule.
120+
/// But then they need to be kept alive beyond the lifetime of the SILModule.
121+
using TBDSymbolSetPtr = std::shared_ptr<TBDSymbolSet>;
122+
107123
std::vector<std::string> getPublicSymbols(TBDGenDescriptor desc);
108124

109125
void writeTBDFile(ModuleDecl *M, llvm::raw_ostream &os,
110-
const TBDGenOptions &opts);
126+
const TBDGenOptions &opts, TBDSymbolSetPtr publicCMOSymbols);
111127

112-
void writeAPIJSONFile(ModuleDecl *M, llvm::raw_ostream &os, bool PrettyPrint);
128+
void writeAPIJSONFile(ModuleDecl *M, llvm::raw_ostream &os, bool PrettyPrint,
129+
TBDSymbolSetPtr publicCMOSymbols);
113130

114131
} // end namespace swift
115132

lib/AST/Module.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2374,17 +2374,18 @@ canBeUsedForCrossModuleOptimization(DeclContext *ctxt) const {
23742374
// See if context is imported in a "regular" way, i.e. not with
23752375
// @_implementationOnly or @_spi.
23762376
ModuleDecl::ImportFilter filter = {
2377-
ModuleDecl::ImportFilterKind::Exported,
2378-
ModuleDecl::ImportFilterKind::Default};
2377+
ModuleDecl::ImportFilterKind::ImplementationOnly,
2378+
ModuleDecl::ImportFilterKind::SPIAccessControl
2379+
};
23792380
SmallVector<ImportedModule, 4> results;
23802381
getImportedModules(results, filter);
23812382

23822383
auto &imports = getASTContext().getImportCache();
23832384
for (auto &desc : results) {
23842385
if (imports.isImportedBy(moduleOfCtxt, desc.importedModule))
2385-
return true;
2386+
return false;
23862387
}
2387-
return false;
2388+
return true;
23882389
}
23892390

23902391
void SourceFile::lookupImportedSPIGroups(

lib/AST/RequirementMachine/PropertyUnification.cpp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,13 @@ RewriteSystem::TypeWitness::TypeWitness(
650650
getConcreteConformance().getProtocol());
651651
}
652652

653-
bool swift::rewriting::operator==(
654-
const RewriteSystem::TypeWitness &lhs,
655-
const RewriteSystem::TypeWitness &rhs) {
656-
return (lhs.LHS == rhs.LHS &&
657-
lhs.RHS == rhs.RHS);
653+
namespace swift {
654+
namespace rewriting {
655+
bool operator==(const RewriteSystem::TypeWitness &lhs,
656+
const RewriteSystem::TypeWitness &rhs) {
657+
return lhs.LHS == rhs.LHS && lhs.RHS == rhs.RHS;
658+
}
659+
}
658660
}
659661

660662
void RewriteSystem::TypeWitness::dump(llvm::raw_ostream &out) const {
@@ -906,4 +908,4 @@ void PropertyMap::recordConcreteConformanceRule(
906908
path.invert();
907909

908910
inducedRules.emplace_back(std::move(lhs), std::move(rhs), std::move(path));
909-
}
911+
}

lib/DriverTool/swift_api_extract_main.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ class SwiftAPIExtractInvocation {
215215
return 1;
216216

217217
if (OutputFilename == "-") {
218-
writeAPIJSONFile(M, llvm::outs(), PrettyPrint);
218+
writeAPIJSONFile(M, llvm::outs(), PrettyPrint, nullptr);
219219
return 0;
220220
}
221221

@@ -227,7 +227,7 @@ class SwiftAPIExtractInvocation {
227227
return 1;
228228
}
229229

230-
writeAPIJSONFile(M, OS, PrettyPrint);
230+
writeAPIJSONFile(M, OS, PrettyPrint, nullptr);
231231
return 0;
232232
}
233233
};

lib/Frontend/Frontend.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1336,6 +1336,8 @@ bool CompilerInstance::performSILProcessing(SILModule *silModule) {
13361336

13371337
performSILOptimizations(Invocation, silModule);
13381338

1339+
publicCMOSymbols = silModule->getPublicCMOSymbols();
1340+
13391341
if (auto *stats = getStatsReporter())
13401342
countStatsPostSILOpt(*stats, *silModule);
13411343

0 commit comments

Comments
 (0)