Skip to content

Commit

Permalink
Fix memory for WASI mode
Browse files Browse the repository at this point in the history
Make sure that in WASI mode, memory is created internally.
  • Loading branch information
Maqrkk authored and alexp-sssup committed Oct 4, 2023
1 parent d781279 commit 04a5980
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 4 deletions.
1 change: 1 addition & 0 deletions llvm/include/llvm/Cheerp/WasmWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,7 @@ class CheerpWasmWriter final : public CheerpBaseWriter
void compileFunctionSection();
void compileImportSection();
void compileTableSection();
void compileMemorySection();
void compileGlobalSection();
void compileExportSection();
void compileElementSection();
Expand Down
46 changes: 42 additions & 4 deletions llvm/lib/CheerpWriter/CheerpWasmWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4427,14 +4427,18 @@ void CheerpWasmWriter::compileImportSection()

numberOfImportedFunctions = importedTotal;

if (!useWasmLoader && importedTotal == 0)
return;

Section section(0x02, "Import", this);

// Encode number of entries in the import section.
// The +1 is for memory that we import unconditionally.
encodeULEB128(importedTotal + 1, section);
// We import the memory in all cases, except when the target is WASI.
encodeULEB128(importedTotal + useWasmLoader, section);

// Import the memory
compileImportMemory(section);
// Import the memory if target is not WASI.
if (useWasmLoader)
compileImportMemory(section);

for (const Function* F : globalDeps.asmJSImports())
{
Expand Down Expand Up @@ -4586,6 +4590,37 @@ CheerpWasmWriter::GLOBAL_CONSTANT_ENCODING CheerpWasmWriter::shouldEncodeConstan
}
}

void CheerpWasmWriter::compileMemorySection()
{
// Define the memory for the module in WasmPage units. The heap size is
// defined in MiB and the wasm page size is 64 KiB. Thus, the wasm heap
// max size parameter is defined as: heapSize << 20 >> 16 = heapSize << 4.
uint32_t maxMemory = heapSize << 4;
uint32_t minMemory = (linearHelper.getHeapStart() + 65535) >> 16;

// TODO use WasmPage variable instead of hardcoded '1>>16'.
assert(WasmPage == 64 * 1024);

if (noGrowMemory)
minMemory = maxMemory;

{
Section section(0x05, "Memory", this);
encodeULEB128(1, section);
// from the spec:
//limits ::= 0x00 n:u32 => {min n, max e, unshared}
// 0x01 n:u32 m:u32 => {min n, max m, unshared}
// 0x03 n:u32 m:u32 => {min n, max m, shared}
// We use 0x01 and 0x03 only for now
int memType = sharedMemory ? 0x03 : 0x01;
encodeULEB128(memType, section);
// Encode minimum and maximum memory parameters.
encodeULEB128(minMemory, section);
encodeULEB128(maxMemory, section);
section.encode();
}
}

void CheerpWasmWriter::compileGlobalSection()
{
// Temporary map for the globalized constants. We update the global one at the end, to avoid
Expand Down Expand Up @@ -5065,6 +5100,9 @@ void CheerpWasmWriter::compileModule()

compileTableSection();

if (!useWasmLoader)
compileMemorySection();

compileGlobalSection();

compileExportSection();
Expand Down

0 comments on commit 04a5980

Please sign in to comment.