Skip to content

Commit 530470c

Browse files
Maqrkkyuri91
authored andcommitted
Module is only returned when thread setup is done
Since the thread setup is asynchronous, we have to create a custom promise to return the module, and only resolve this when the utility thread has been set up.
1 parent 8510653 commit 530470c

File tree

5 files changed

+33
-1
lines changed

5 files changed

+33
-1
lines changed

clang/include/clang/Basic/BuiltinsCheerp.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ BUILTIN(__builtin_cheerp_set_thread_pointer, "vUi", "")
4343
BUILTIN(__builtin_cheerp_get_thread_pointer, "Ui", "")
4444
BUILTIN(__builtin_cheerp_atomic_wait, "iv*iLLi", "")
4545
BUILTIN(__builtin_cheerp_atomic_notify, "iv*i", "")
46+
BUILTIN(__builtin_cheerp_thread_setup_resolve, "v", "")
4647

4748
// SIMD builtins
4849
TARGET_BUILTIN(__builtin_wasm_swizzle_i8x16, "V16ScV16ScV16Sc", "nc", "simd128")

llvm/include/llvm/Cheerp/NameGenerator.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,8 @@ class NameGenerator
8282
THREADINGOBJECT,
8383
THREADINGOBJECTWORKER,
8484
BLOBNAME,
85+
THREADSETUPPROMISE,
86+
THREADSETUPRESOLVE,
8587
MEMORY,
8688
HEAP8,
8789
HEAP16,

llvm/include/llvm/IR/IntrinsicsCheerp.td

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,10 @@ def int_cheerp_atomic_notify : Intrinsic<[llvm_i32_ty],
116116
[llvm_ptr_ty, llvm_i32_ty]>,
117117
ClangBuiltin<"__builtin_cheerp_atomic_notify">;
118118

119+
def int_cheerp_thread_setup_resolve : Intrinsic<[],
120+
[]>,
121+
ClangBuiltin<"__builtin_cheerp_thread_setup_resolve">;
122+
119123
def int_cheerp_environ : Intrinsic<[llvm_anyptr_ty]>;
120124

121125
def int_cheerp_argv : Intrinsic<[llvm_anyptr_ty]>;

llvm/lib/CheerpWriter/CheerpWriter.cpp

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -977,6 +977,12 @@ CheerpWriter::COMPILE_INSTRUCTION_FEEDBACK CheerpWriter::handleBuiltinCall(const
977977
stream << "new Blob([" << threadingObject << "." << blobText << "])";
978978
return COMPILE_OK;
979979
}
980+
else if(intrinsicId==Intrinsic::cheerp_thread_setup_resolve)
981+
{
982+
StringRef threadSetupResolve = namegen.getBuiltinName(NameGenerator::Builtin::THREADSETUPRESOLVE);
983+
stream << threadSetupResolve << "();" << NewLine;
984+
return COMPILE_OK;
985+
}
980986
else if(intrinsicId==Intrinsic::abs)
981987
{
982988
//Implementing ( X >= 0 ) ? X : -X
@@ -6848,7 +6854,7 @@ void CheerpWriter::compileWasmLoader()
68486854
stream << "module.exports=" << NewLine;
68496855
else if (makeModule == MODULE_TYPE::ES6)
68506856
stream << "return ";
6851-
stream << dummyName << ".promise.then(" << shortestName << "=>{" << NewLine;
6857+
stream << dummyName << ".promise=" << dummyName << ".promise.then(" << shortestName << "=>{" << NewLine;
68526858
stream << threadObject << ".module=" << shortestName << ".module;" << NewLine;
68536859
stream << "__asm=" << shortestName << ".instance.exports;" << NewLine;
68546860
}
@@ -7131,6 +7137,13 @@ void CheerpWriter::compileEntryPoint()
71317137

71327138
void CheerpWriter::compileThreadingObject()
71337139
{
7140+
// First create the promise and resolve variables for threading setup.
7141+
StringRef threadPromise = namegen.getBuiltinName(NameGenerator::Builtin::THREADSETUPPROMISE);
7142+
StringRef threadResolve = namegen.getBuiltinName(NameGenerator::Builtin::THREADSETUPRESOLVE);
7143+
stream << "var " << threadResolve << "=null;" << NewLine;
7144+
stream << "var " << threadPromise << "=new Promise((r)=>{" << threadResolve << "=r;});" << NewLine;
7145+
7146+
// Then create the rest of the threading variables.
71347147
StringRef threadObject = namegen.getBuiltinName(NameGenerator::Builtin::THREADINGOBJECT);
71357148
StringRef workerThreadObject = namegen.getBuiltinName(NameGenerator::Builtin::THREADINGOBJECTWORKER);
71367149
StringRef blobName = namegen.getBuiltinName(NameGenerator::Builtin::BLOBNAME);
@@ -7266,6 +7279,16 @@ void CheerpWriter::makeJS()
72667279

72677280
compileDefineExports();
72687281
compileEntryPoint();
7282+
7283+
// If -pthread is linked in, we want to return the thread setup promise here.
7284+
if (!LowerAtomics)
7285+
{
7286+
stream << "return " << namegen.getBuiltinName(NameGenerator::Builtin::THREADSETUPPROMISE) << ";" << NewLine;
7287+
// If we are in ES6 or CommonJS module mode, chain the thread setup promise here.
7288+
if (makeModule == MODULE_TYPE::ES6 || makeModule == MODULE_TYPE::COMMONJS)
7289+
stream << "}).then(()=>{" << NewLine;
7290+
}
7291+
72697292
if (makeModule == MODULE_TYPE::COMMONJS || makeModule == MODULE_TYPE::ES6)
72707293
compileCommonJSExports();
72717294

llvm/lib/CheerpWriter/NameGenerator.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -695,6 +695,8 @@ void NameGenerator::generateReadableNames(const Module& M, const GlobalDepsAnaly
695695
builtins[THREADINGOBJECT] = "threadingObject";
696696
builtins[THREADINGOBJECTWORKER] = "threadingObjectWorker";
697697
builtins[BLOBNAME] = "blobText";
698+
builtins[THREADSETUPPROMISE] = "__threadSetupPromise";
699+
builtins[THREADSETUPRESOLVE] = "__threadSetupResolve";
698700
builtins[HEAP8] = "HEAP8";
699701
builtins[HEAP16] = "HEAP16";
700702
builtins[HEAP32] = "HEAP32";

0 commit comments

Comments
 (0)