Skip to content

Commit cd19d16

Browse files
authored
Merge pull request #3496 from swiftwasm/main
[pull] swiftwasm from main
2 parents 80af02d + 8c43800 commit cd19d16

File tree

7 files changed

+95
-36
lines changed

7 files changed

+95
-36
lines changed

lib/AST/ASTMangler.cpp

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2173,8 +2173,14 @@ void ASTMangler::appendModule(const ModuleDecl *module,
21732173

21742174
// Try the special 'swift' substitution.
21752175
if (ModName == STDLIB_NAME) {
2176-
assert(useModuleName.empty());
2177-
return appendOperator("s");
2176+
if (useModuleName.empty()) {
2177+
appendOperator("s");
2178+
} else if (DWARFMangling) {
2179+
appendOperator("s");
2180+
} else {
2181+
appendIdentifier(useModuleName);
2182+
}
2183+
return;
21782184
}
21792185

21802186
if (ModName == MANGLING_MODULE_OBJC) {

lib/Serialization/SerializeSIL.cpp

Lines changed: 41 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -227,7 +227,9 @@ namespace {
227227
DifferentiabilityWitnessesToEmit;
228228

229229
/// Additional functions we might need to serialize.
230-
llvm::SmallVector<const SILFunction *, 16> Worklist;
230+
llvm::SmallVector<const SILFunction *, 16> functionWorklist;
231+
232+
llvm::SmallVector<const SILGlobalVariable *, 16> globalWorklist;
231233

232234
/// String storage for temporarily created strings which are referenced from
233235
/// the tables.
@@ -250,7 +252,9 @@ namespace {
250252
bool emitDeclarationsForOnoneSupport);
251253
void addReferencedSILFunction(const SILFunction *F,
252254
bool DeclOnly = false);
253-
void processSILFunctionWorklist();
255+
void addReferencedGlobalVariable(const SILGlobalVariable *gl);
256+
257+
void processWorklists();
254258

255259
/// Helper function to update ListOfValues for MethodInst. Format:
256260
/// Attr, SILDeclRef (DeclID, Kind, uncurryLevel), and an operand.
@@ -340,7 +344,7 @@ void SILSerializer::addMandatorySILFunction(const SILFunction *F,
340344
// Function body should be serialized unless it is a KeepAsPublic function
341345
// (which is typically a pre-specialization).
342346
if (!emitDeclarationsForOnoneSupport)
343-
Worklist.push_back(F);
347+
functionWorklist.push_back(F);
344348
}
345349

346350
void SILSerializer::addReferencedSILFunction(const SILFunction *F,
@@ -354,7 +358,7 @@ void SILSerializer::addReferencedSILFunction(const SILFunction *F,
354358
// serialize the body or just the declaration.
355359
if (shouldEmitFunctionBody(F)) {
356360
FuncsToEmit[F] = false;
357-
Worklist.push_back(F);
361+
functionWorklist.push_back(F);
358362
return;
359363
}
360364

@@ -363,23 +367,35 @@ void SILSerializer::addReferencedSILFunction(const SILFunction *F,
363367
F->hasForeignBody());
364368

365369
FuncsToEmit[F] = false;
366-
Worklist.push_back(F);
370+
functionWorklist.push_back(F);
367371
return;
368372
}
369373

370374
// Ok, we just need to emit a declaration.
371375
FuncsToEmit[F] = true;
372376
}
373377

374-
void SILSerializer::processSILFunctionWorklist() {
375-
while (!Worklist.empty()) {
376-
const SILFunction *F = Worklist.back();
377-
Worklist.pop_back();
378-
assert(F != nullptr);
378+
void SILSerializer::addReferencedGlobalVariable(const SILGlobalVariable *gl) {
379+
if (GlobalsToEmit.insert(gl).second)
380+
globalWorklist.push_back(gl);
381+
}
379382

380-
assert(FuncsToEmit.count(F) > 0);
381-
writeSILFunction(*F, FuncsToEmit[F]);
382-
}
383+
384+
void SILSerializer::processWorklists() {
385+
do {
386+
while (!functionWorklist.empty()) {
387+
const SILFunction *F = functionWorklist.pop_back_val();
388+
assert(F != nullptr);
389+
390+
assert(FuncsToEmit.count(F) > 0);
391+
writeSILFunction(*F, FuncsToEmit[F]);
392+
}
393+
while (!globalWorklist.empty()) {
394+
const SILGlobalVariable *gl = globalWorklist.pop_back_val();
395+
assert(GlobalsToEmit.count(gl) > 0);
396+
writeSILGlobalVar(*gl);
397+
}
398+
} while (!functionWorklist.empty());
383399
}
384400

385401
/// We enumerate all values in a SILFunction beforehand to correctly
@@ -1121,7 +1137,7 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
11211137
// Format: Name and type. Use SILOneOperandLayout.
11221138
const AllocGlobalInst *AGI = cast<AllocGlobalInst>(&SI);
11231139
auto *G = AGI->getReferencedGlobal();
1124-
GlobalsToEmit.insert(G);
1140+
addReferencedGlobalVariable(G);
11251141
SILOneOperandLayout::emitRecord(Out, ScratchRecord,
11261142
SILAbbrCodes[SILOneOperandLayout::Code],
11271143
(unsigned)SI.getKind(), 0, 0, 0,
@@ -1133,7 +1149,7 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
11331149
// Format: Name and type. Use SILOneOperandLayout.
11341150
const GlobalAccessInst *GI = cast<GlobalAccessInst>(&SI);
11351151
auto *G = GI->getReferencedGlobal();
1136-
GlobalsToEmit.insert(G);
1152+
addReferencedGlobalVariable(G);
11371153
SILOneOperandLayout::emitRecord(Out, ScratchRecord,
11381154
SILAbbrCodes[SILOneOperandLayout::Code],
11391155
(unsigned)SI.getKind(), 0,
@@ -2841,6 +2857,12 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
28412857
writeSILDefaultWitnessTable(wt);
28422858
}
28432859

2860+
// Add global variables that must be emitted to the list.
2861+
for (const SILGlobalVariable &g : SILMod->getSILGlobals()) {
2862+
if (g.isSerialized() || ShouldSerializeAll)
2863+
addReferencedGlobalVariable(&g);
2864+
}
2865+
28442866
// Emit only declarations if it is a module with pre-specializations.
28452867
// And only do it in optimized builds.
28462868
bool emitDeclarationsForOnoneSupport =
@@ -2860,7 +2882,7 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
28602882
}
28612883

28622884
addMandatorySILFunction(&F, emitDeclarationsForOnoneSupport);
2863-
processSILFunctionWorklist();
2885+
processWorklists();
28642886
}
28652887

28662888
// Write out differentiability witnesses.
@@ -2879,7 +2901,7 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
28792901
// Process SIL functions referenced by differentiability witnesses.
28802902
// Note: this is necessary despite processing `FuncsToEmit` below because
28812903
// `Worklist` is processed separately.
2882-
processSILFunctionWorklist();
2904+
processWorklists();
28832905

28842906
// Now write function declarations for every function we've
28852907
// emitted a reference to without emitting a function body for.
@@ -2893,16 +2915,8 @@ void SILSerializer::writeSILBlock(const SILModule *SILMod) {
28932915
}
28942916
}
28952917

2896-
// Add global variables that must be emitted to the list.
2897-
for (const SILGlobalVariable &g : SILMod->getSILGlobals())
2898-
if (g.isSerialized() || ShouldSerializeAll)
2899-
GlobalsToEmit.insert(&g);
2900-
2901-
// Now write out all referenced global variables.
2902-
for (auto *g : GlobalsToEmit)
2903-
writeSILGlobalVar(*g);
2904-
2905-
assert(Worklist.empty() && "Did not emit everything in worklist");
2918+
assert(functionWorklist.empty() && globalWorklist.empty() &&
2919+
"Did not emit everything in worklists");
29062920
}
29072921

29082922
void SILSerializer::writeSILModule(const SILModule *SILMod) {

stdlib/public/Concurrency/Actor.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1625,7 +1625,7 @@ void DefaultActorImpl::enqueue(Job *job) {
16251625
auto oldStatus = oldState.Flags.getStatus();
16261626
bool wasIdle = oldStatus == Status::Idle;
16271627

1628-
// Update the priority: the prriority of the job we're adding
1628+
// Update the priority: the priority of the job we're adding
16291629
// if the actor was idle, or the max if not. Only the running
16301630
// thread can decrease the actor's priority once it's non-idle.
16311631
// (But note that the job we enqueue can still observe a
@@ -1988,4 +1988,4 @@ bool swift::swift_distributed_actor_is_remote(DefaultActor *_actor) {
19881988
bool DefaultActorImpl::isDistributedRemote() {
19891989
auto state = CurrentState.load(std::memory_order_relaxed);
19901990
return state.Flags.isDistributedRemote();
1991-
}
1991+
}

test/Interpreter/llvm_link_time_opt.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@
44
// REQUIRES: lld_lto
55

66
// RUN: %empty-directory(%t)
7-
// RUN: %target-swiftc_driver -emit-library -static -lto=llvm-full -emit-module %S/Inputs/lto/module1.swift -working-directory %t
8-
// RUN: %target-swiftc_driver -lto=llvm-full %s -I%t -L%t -lmodule1 -module-name main -o %t/main
7+
// RUN: %target-swiftc_driver -emit-library -static -lto=llvm-full %lto_flags -emit-module %S/Inputs/lto/module1.swift -working-directory %t
8+
// RUN: %target-swiftc_driver -lto=llvm-full %lto_flags %s -I%t -L%t -lmodule1 -module-name main -o %t/main
99
// RUN: %llvm-nm --defined-only %t/main | %FileCheck %s
1010

1111
// CHECK-NOT: _$s7module120unusedPublicFunctionyyF
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %target-sil-opt %s -serialize -o /dev/null
2+
3+
sil_stage canonical
4+
5+
import Builtin
6+
7+
// Check that we don't crash if a serialiable function is referenced from the
8+
// initializer of a global variable.
9+
10+
sil_global [serialized] @globalFuncPtr : $@callee_guaranteed () -> () = {
11+
%0 = function_ref @calledFromFuncPtr : $@convention(thin) () -> ()
12+
%initval = thin_to_thick_function %0 : $@convention(thin) () -> () to $@callee_guaranteed () -> ()
13+
}
14+
15+
sil @caller : $@convention(thin) () -> () {
16+
bb0:
17+
%0 = global_addr @globalFuncPtr : $*@callee_guaranteed () -> ()
18+
%7 = tuple ()
19+
return %7 : $()
20+
}
21+
22+
sil shared [serializable] @calledFromFuncPtr : $@convention(thin) () -> () {
23+
bb0:
24+
%6 = tuple ()
25+
return %6 : $()
26+
}

test/lit.cfg

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,12 +219,22 @@ lit_config.note('Using cmake: ' + config.cmake)
219219
config.llvm_src_root = getattr(config, 'llvm_src_root', None)
220220
config.llvm_obj_root = getattr(config, 'llvm_obj_root', None)
221221

222+
lto_flags = ""
223+
222224
if platform.system() == 'OpenBSD':
223225
llvm_libs_dir = getattr(config, 'llvm_libs_dir', None)
224226
if not llvm_libs_dir:
225227
lit_config.fatal('No LLVM libs dir set.')
226228
config.environment['LD_LIBRARY_PATH'] = llvm_libs_dir
227229

230+
elif platform.system() == 'Darwin':
231+
llvm_libs_dir = getattr(config, 'llvm_libs_dir', None)
232+
if not llvm_libs_dir:
233+
lit_config.fatal('No LLVM libs dir set.')
234+
lto_flags = "-Xlinker -lto_library -Xlinker %s/libLTO.dylib" % llvm_libs_dir
235+
236+
config.substitutions.append( ('%lto_flags', lto_flags) )
237+
228238
def append_to_env_path(directory):
229239
config.environment['PATH'] = \
230240
os.path.pathsep.join((directory, config.environment['PATH']))

tools/sil-opt/SILOpt.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -241,6 +241,9 @@ EmitVerboseSIL("emit-verbose-sil",
241241
static llvm::cl::opt<bool>
242242
EmitSIB("emit-sib", llvm::cl::desc("Emit serialized AST + SIL file(s)"));
243243

244+
static llvm::cl::opt<bool>
245+
Serialize("serialize", llvm::cl::desc("Emit serialized AST + SIL file(s)"));
246+
244247
static llvm::cl::opt<std::string>
245248
ModuleCachePath("module-cache-path", llvm::cl::desc("Clang module cache path"));
246249

@@ -605,7 +608,7 @@ int main(int argc, char **argv) {
605608
}
606609
}
607610

608-
if (EmitSIB) {
611+
if (EmitSIB || Serialize) {
609612
llvm::SmallString<128> OutputFile;
610613
if (OutputFilename.size()) {
611614
OutputFile = OutputFilename;
@@ -621,8 +624,8 @@ int main(int argc, char **argv) {
621624

622625
SerializationOptions serializationOpts;
623626
serializationOpts.OutputPath = OutputFile.c_str();
624-
serializationOpts.SerializeAllSIL = true;
625-
serializationOpts.IsSIB = true;
627+
serializationOpts.SerializeAllSIL = EmitSIB;
628+
serializationOpts.IsSIB = EmitSIB;
626629

627630
serialize(CI.getMainModule(), serializationOpts, SILMod.get());
628631
} else {

0 commit comments

Comments
 (0)