Skip to content

Commit 52fbefd

Browse files
Merge pull request #4501 from swiftwasm/main
[pull] swiftwasm from main
2 parents 7f7062f + 04226b3 commit 52fbefd

File tree

21 files changed

+106
-138
lines changed

21 files changed

+106
-138
lines changed

CHANGELOG.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ _**Note:** This is in reverse chronological order, so newer entries are added to
9393
in places that would previously fail because `any` types do not conform
9494
to their protocols. For example:
9595

96-
```
96+
```swift
9797
protocol P {
9898
associatedtype A
9999
func getA() -> A

include/swift/AST/SILOptions.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,12 @@ enum class DestroyHoistingOption : uint8_t {
6666
On = 1
6767
};
6868

69+
enum class CrossModuleOptimizationMode : uint8_t {
70+
Off = 0,
71+
Default = 1,
72+
Aggressive = 2
73+
};
74+
6975
class SILModule;
7076

7177
class SILOptions {
@@ -117,7 +123,7 @@ class SILOptions {
117123
bool DisableSILPerfOptimizations = false;
118124

119125
/// Controls whether cross module optimization is enabled.
120-
bool CrossModuleOptimization = false;
126+
CrossModuleOptimizationMode CMOMode = CrossModuleOptimizationMode::Off;
121127

122128
/// Enables experimental performance annotations.
123129
bool EnablePerformanceAnnotations = false;

include/swift/AST/TBDGenRequests.h

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

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) {
53+
TBDGenDescriptor(FileOrModule input, const TBDGenOptions &opts)
54+
: Input(input), Opts(opts) {
6055
assert(input);
6156
}
6257

@@ -78,21 +73,17 @@ class TBDGenDescriptor final {
7873
const StringRef getDataLayoutString() const;
7974
const llvm::Triple &getTarget() const;
8075

81-
TBDSymbolSetPtr getPublicCMOSymbols() const { return publicCMOSymbols; }
82-
8376
bool operator==(const TBDGenDescriptor &other) const;
8477
bool operator!=(const TBDGenDescriptor &other) const {
8578
return !(*this == other);
8679
}
8780

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

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

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

166-
/// A symbol which was made public by the CrossModuleOptimization pass.
167-
CrossModuleOptimization,
168-
169157
/// A symbol with an unknown origin.
170158
// FIXME: This should be eliminated.
171159
Unknown
@@ -185,8 +173,7 @@ class SymbolSource {
185173
irEntity = entity;
186174
}
187175
explicit SymbolSource(Kind kind) : kind(kind) {
188-
assert(kind == Kind::LinkerDirective || kind == Kind::Unknown ||
189-
kind == Kind::CrossModuleOptimization);
176+
assert(kind == Kind::LinkerDirective || kind == Kind::Unknown);
190177
}
191178

192179
public:
@@ -199,9 +186,6 @@ class SymbolSource {
199186
static SymbolSource forLinkerDirective() {
200187
return SymbolSource{Kind::LinkerDirective};
201188
}
202-
static SymbolSource forCrossModuleOptimization() {
203-
return SymbolSource{Kind::CrossModuleOptimization};
204-
}
205189
static SymbolSource forUnknown() {
206190
return SymbolSource{Kind::Unknown};
207191
}
@@ -210,10 +194,6 @@ class SymbolSource {
210194
return kind == Kind::LinkerDirective;
211195
}
212196

213-
bool isFromCrossModuleOptimization() const {
214-
return kind == Kind::CrossModuleOptimization;
215-
}
216-
217197
SILDeclRef getSILDeclRef() const {
218198
assert(kind == Kind::SIL);
219199
return silDeclRef;

include/swift/Frontend/Frontend.h

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,6 @@ class CompilerInstance {
436436
std::unique_ptr<ASTContext> Context;
437437
std::unique_ptr<Lowering::TypeConverter> TheSILTypes;
438438
std::unique_ptr<DiagnosticVerifier> DiagVerifier;
439-
TBDSymbolSetPtr publicCMOSymbols;
440439

441440
/// A cache describing the set of inter-module dependencies that have been queried.
442441
/// Null if not present.
@@ -588,10 +587,6 @@ class CompilerInstance {
588587
/// file.
589588
SourceFile *getCodeCompletionFile() const;
590589

591-
/// Return the symbols (e.g. function names) which are made public by the
592-
/// CrossModuleOptimization pass and therefore must be included in the TBD file.
593-
TBDSymbolSetPtr getPublicCMOSymbols() const { return publicCMOSymbols; }
594-
595590
private:
596591
/// Set up the file system by loading and validating all VFS overlay YAML
597592
/// files. If the process of validating VFS files failed, or the overlay

include/swift/Option/Options.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,6 +812,10 @@ def Oplayground : Flag<["-"], "Oplayground">, Group<O_Group>,
812812
Flags<[HelpHidden, FrontendOption, ModuleInterfaceOption]>,
813813
HelpText<"Compile with optimizations appropriate for a playground">;
814814

815+
def EnbaleDefaultCMO : Flag<["-"], "enable-default-cmo">,
816+
Flags<[HelpHidden, FrontendOption]>,
817+
HelpText<"Perform conservative cross-module optimization">;
818+
815819
def CrossModuleOptimization : Flag<["-"], "cross-module-optimization">,
816820
Flags<[HelpHidden, FrontendOption]>,
817821
HelpText<"Perform cross-module optimization">;

include/swift/SIL/SILModule.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include "swift/SIL/SILVTable.h"
3939
#include "swift/SIL/SILWitnessTable.h"
4040
#include "swift/SIL/TypeLowering.h"
41-
#include "swift/TBDGen/TBDGen.h"
4241
#include "llvm/ADT/ArrayRef.h"
4342
#include "llvm/ADT/FoldingSet.h"
4443
#include "llvm/ADT/MapVector.h"
@@ -387,10 +386,6 @@ class SILModule {
387386

388387
/// Folding set for key path patterns.
389388
llvm::FoldingSet<KeyPathPattern> KeyPathPatterns;
390-
391-
/// Symbols (e.g. function names) which are made public by the
392-
/// CrossModuleOptimization pass and therefore must be included in the TBD file.
393-
TBDSymbolSetPtr publicCMOSymbols;
394389

395390
public:
396391
~SILModule();
@@ -560,12 +555,6 @@ class SILModule {
560555

561556
const SILOptions &getOptions() const { return Options; }
562557

563-
/// Return the symbols (e.g. function names) which are made public by the
564-
/// CrossModuleOptimization pass and therefore must be included in the TBD file.
565-
TBDSymbolSetPtr getPublicCMOSymbols() { return publicCMOSymbols; }
566-
567-
void addPublicCMOSymbol(StringRef symbol);
568-
569558
using iterator = FunctionListType::iterator;
570559
using const_iterator = FunctionListType::const_iterator;
571560
FunctionListType &getFunctionList() { return functions; }

include/swift/TBDGen/TBDGen.h

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

2320
namespace llvm {
2421
class raw_ostream;
@@ -107,26 +104,12 @@ struct TBDGenOptions {
107104
}
108105
};
109106

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-
123107
std::vector<std::string> getPublicSymbols(TBDGenDescriptor desc);
124108

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

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

131114
} // end namespace swift
132115

lib/AST/Type.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4115,7 +4115,6 @@ CanType ProtocolCompositionType::getMinimalCanonicalType(
41154115
return Reqs.front().getSecondType()->getCanonicalType();
41164116
}
41174117

4118-
Type superclass;
41194118
llvm::SmallVector<Type, 2> MinimalMembers;
41204119
bool MinimalHasExplicitAnyObject = false;
41214120
auto ifaceTy = Sig.getGenericParams().back();
@@ -4126,10 +4125,6 @@ CanType ProtocolCompositionType::getMinimalCanonicalType(
41264125

41274126
switch (Req.getKind()) {
41284127
case RequirementKind::Superclass:
4129-
assert((!superclass || superclass->isEqual(Req.getSecondType()))
4130-
&& "Multiple distinct superclass constraints!");
4131-
superclass = Req.getSecondType();
4132-
break;
41334128
case RequirementKind::Conformance:
41344129
MinimalMembers.push_back(Req.getSecondType());
41354130
break;
@@ -4141,10 +4136,16 @@ CanType ProtocolCompositionType::getMinimalCanonicalType(
41414136
}
41424137
}
41434138

4144-
// Ensure superclass bounds appear first regardless of their order among
4145-
// the signature's requirements.
4146-
if (superclass)
4147-
MinimalMembers.insert(MinimalMembers.begin(), superclass->getCanonicalType());
4139+
// A superclass constraint is always retained and must appear first in the
4140+
// members list.
4141+
assert(Composition->getMembers().front()->getClassOrBoundGenericClass() ==
4142+
MinimalMembers.front()->getClassOrBoundGenericClass());
4143+
4144+
// If we are left with a single member and no layout constraint, the member
4145+
// is the minimal type. Also, note that a protocol composition cannot be
4146+
// constructed with a single member unless there is a layout constraint.
4147+
if (MinimalMembers.size() == 1 && !MinimalHasExplicitAnyObject)
4148+
return CanType(MinimalMembers.front());
41484149

41494150
// The resulting composition is necessarily canonical.
41504151
return CanType(build(Ctx, MinimalMembers, MinimalHasExplicitAnyObject));

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, nullptr);
218+
writeAPIJSONFile(M, llvm::outs(), PrettyPrint);
219219
return 0;
220220
}
221221

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

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

lib/Frontend/CompilerInvocation.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1745,7 +1745,11 @@ static bool ParseSILArgs(SILOptions &Opts, ArgList &Args,
17451745
OPT_enable_actor_data_race_checks,
17461746
OPT_disable_actor_data_race_checks, /*default=*/false);
17471747
Opts.DisableSILPerfOptimizations |= Args.hasArg(OPT_disable_sil_perf_optzns);
1748-
Opts.CrossModuleOptimization |= Args.hasArg(OPT_CrossModuleOptimization);
1748+
if (Args.hasArg(OPT_CrossModuleOptimization)) {
1749+
Opts.CMOMode = CrossModuleOptimizationMode::Aggressive;
1750+
} else if (Args.hasArg(OPT_EnbaleDefaultCMO)) {
1751+
Opts.CMOMode = CrossModuleOptimizationMode::Default;
1752+
}
17491753
Opts.EnablePerformanceAnnotations |=
17501754
Args.hasArg(OPT_ExperimentalPerformanceAnnotations);
17511755
Opts.VerifyAll |= Args.hasArg(OPT_sil_verify_all);

lib/Frontend/Frontend.cpp

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

13311331
performSILOptimizations(Invocation, silModule);
13321332

1333-
publicCMOSymbols = silModule->getPublicCMOSymbols();
1334-
13351333
if (auto *stats = getStatsReporter())
13361334
countStatsPostSILOpt(*stats, *silModule);
13371335

lib/FrontendTool/FrontendTool.cpp

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -702,10 +702,16 @@ static bool writeTBDIfNeeded(CompilerInstance &Instance) {
702702
return false;
703703
}
704704

705+
if (Invocation.getSILOptions().CMOMode ==
706+
CrossModuleOptimizationMode::Aggressive) {
707+
Instance.getDiags().diagnose(SourceLoc(),
708+
diag::tbd_not_supported_with_cmo);
709+
return false;
710+
}
711+
705712
const std::string &TBDPath = Invocation.getTBDPathForWholeModule();
706713

707-
return writeTBD(Instance.getMainModule(), TBDPath, tbdOpts,
708-
Instance.getPublicCMOSymbols());
714+
return writeTBD(Instance.getMainModule(), TBDPath, tbdOpts);
709715
}
710716

711717
static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
@@ -1392,19 +1398,19 @@ static bool processCommandLineAndRunImmediately(CompilerInstance &Instance,
13921398

13931399
static bool validateTBDIfNeeded(const CompilerInvocation &Invocation,
13941400
ModuleOrSourceFile MSF,
1395-
const llvm::Module &IRModule,
1396-
TBDSymbolSetPtr publicCMOSymbols) {
1397-
auto mode = Invocation.getFrontendOptions().ValidateTBDAgainstIR;
1398-
if (mode == FrontendOptions::TBDValidationMode::All &&
1399-
Invocation.getSILOptions().CrossModuleOptimization)
1400-
mode = FrontendOptions::TBDValidationMode::MissingFromTBD;
1401-
1401+
const llvm::Module &IRModule) {
1402+
const auto mode = Invocation.getFrontendOptions().ValidateTBDAgainstIR;
14021403
const bool canPerformTBDValidation = [&]() {
14031404
// If the user has requested we skip validation, honor it.
14041405
if (mode == FrontendOptions::TBDValidationMode::None) {
14051406
return false;
14061407
}
14071408

1409+
// Cross-module optimization does not support TBD.
1410+
if (Invocation.getSILOptions().CMOMode == CrossModuleOptimizationMode::Aggressive) {
1411+
return false;
1412+
}
1413+
14081414
// If we can't validate the given input file, bail early. This covers cases
14091415
// like passing raw SIL as a primary file.
14101416
const auto &IO = Invocation.getFrontendOptions().InputsAndOutputs;
@@ -1465,10 +1471,9 @@ static bool validateTBDIfNeeded(const CompilerInvocation &Invocation,
14651471
// noise from e.g. statically-linked libraries.
14661472
Opts.embedSymbolsFromModules.clear();
14671473
if (auto *SF = MSF.dyn_cast<SourceFile *>()) {
1468-
return validateTBD(SF, IRModule, Opts, publicCMOSymbols,
1469-
diagnoseExtraSymbolsInTBD);
1474+
return validateTBD(SF, IRModule, Opts, diagnoseExtraSymbolsInTBD);
14701475
} else {
1471-
return validateTBD(MSF.get<ModuleDecl *>(), IRModule, Opts, publicCMOSymbols,
1476+
return validateTBD(MSF.get<ModuleDecl *>(), IRModule, Opts,
14721477
diagnoseExtraSymbolsInTBD);
14731478
}
14741479
}
@@ -1681,8 +1686,6 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
16811686
return processCommandLineAndRunImmediately(
16821687
Instance, std::move(SM), MSF, observer, ReturnValue);
16831688

1684-
TBDSymbolSetPtr publicCMOSymbols = SM->getPublicCMOSymbols();
1685-
16861689
StringRef OutputFilename = PSPs.OutputFilename;
16871690
std::vector<std::string> ParallelOutputFilenames =
16881691
opts.InputsAndOutputs.copyOutputFilenames();
@@ -1701,8 +1704,7 @@ static bool performCompileStepsPostSILGen(CompilerInstance &Instance,
17011704
if (!IRModule)
17021705
return Instance.getDiags().hadAnyError();
17031706

1704-
if (validateTBDIfNeeded(Invocation, MSF, *IRModule.getModule(),
1705-
publicCMOSymbols))
1707+
if (validateTBDIfNeeded(Invocation, MSF, *IRModule.getModule()))
17061708
return true;
17071709

17081710
return generateCode(Instance, OutputFilename, IRModule.getModule(),

0 commit comments

Comments
 (0)