Index: lld/wasm/Writer.cpp =================================================================== --- lld/wasm/Writer.cpp +++ lld/wasm/Writer.cpp @@ -841,7 +841,7 @@ void Writer::createInitMemoryFunction() { LLVM_DEBUG(dbgs() << "createInitMemoryFunction\n"); assert(WasmSym::initMemoryFlag); - uint32_t flagAddress = WasmSym::initMemoryFlag->getVirtualAddress(); + uint64_t flagAddress = WasmSym::initMemoryFlag->getVirtualAddress(); std::string bodyContent; { raw_string_ostream os(bodyContent); @@ -890,8 +890,10 @@ // ( ... drop data segments ... ) // ) + bool is64 = config->is64.getValueOr(false); + // Atomically check whether this is the main thread. - writeI32Const(os, flagAddress, "flag address"); + writePtrConst(os, flagAddress, is64, "flag address"); writeI32Const(os, 0, "expected flag value"); writeI32Const(os, 1, "flag value"); writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); @@ -901,9 +903,10 @@ writeU8(os, WASM_TYPE_NORESULT, "blocktype"); // Did not increment 0, so wait for main thread to initialize memory - writeI32Const(os, flagAddress, "flag address"); + writePtrConst(os, flagAddress, is64, "flag address"); writeI32Const(os, 1, "expected flag value"); writeI64Const(os, -1, "timeout"); + writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); writeUleb128(os, WASM_OPCODE_I32_ATOMIC_WAIT, "i32.atomic.wait"); writeMemArg(os, 2, 0); @@ -915,12 +918,7 @@ for (const OutputSegment *s : segments) { if (needsPassiveInitialization(s)) { // destination address - if (config->is64.getValueOr(false)) { - writeI64Const(os, s->startVA, "destination address"); - } else { - writeI32Const(os, static_cast(s->startVA), - "destination address"); - } + writePtrConst(os, s->startVA, is64, "destination address"); // source segment offset writeI32Const(os, 0, "segment offset"); // memory region size @@ -934,14 +932,14 @@ } // Set flag to 2 to mark end of initialization - writeI32Const(os, flagAddress, "flag address"); + writePtrConst(os, flagAddress, is64, "flag address"); writeI32Const(os, 2, "flag value"); writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); writeUleb128(os, WASM_OPCODE_I32_ATOMIC_STORE, "i32.atomic.store"); writeMemArg(os, 2, 0); // Notify any waiters that memory initialization is complete - writeI32Const(os, flagAddress, "flag address"); + writePtrConst(os, flagAddress, is64, "flag address"); writeI32Const(os, -1, "number of waiters"); writeU8(os, WASM_OPCODE_ATOMICS_PREFIX, "atomics prefix"); writeUleb128(os, WASM_OPCODE_ATOMIC_NOTIFY, "atomic.notify"); Index: lld/wasm/WriterUtils.h =================================================================== --- lld/wasm/WriterUtils.h +++ lld/wasm/WriterUtils.h @@ -40,6 +40,9 @@ void writeI64Const(raw_ostream &os, int64_t number, const Twine &msg); +void writePtrConst(raw_ostream &os, int64_t number, bool is64, + const Twine &msg); + void writeMemArg(raw_ostream &os, uint32_t alignment, uint64_t offset); void writeInitExpr(raw_ostream &os, const llvm::wasm::WasmInitExpr &initExpr); Index: lld/wasm/WriterUtils.cpp =================================================================== --- lld/wasm/WriterUtils.cpp +++ lld/wasm/WriterUtils.cpp @@ -136,6 +136,14 @@ writeSleb128(os, number, msg); } +void writePtrConst(raw_ostream &os, int64_t number, bool is64, + const Twine &msg) { + if (is64) + writeI64Const(os, number, msg); + else + writeI32Const(os, static_cast(number), msg); +} + void writeMemArg(raw_ostream &os, uint32_t alignment, uint64_t offset) { writeUleb128(os, alignment, "alignment"); writeUleb128(os, offset, "offset");