diff --git a/docs/changes/2026-11-29-wasm-memory-grow-lazy-compile/BENCHMARK.md b/docs/changes/2026-11-29-wasm-memory-grow-lazy-compile/BENCHMARK.md new file mode 100644 index 000000000..a7d9ece20 --- /dev/null +++ b/docs/changes/2026-11-29-wasm-memory-grow-lazy-compile/BENCHMARK.md @@ -0,0 +1,196 @@ +# Wasm growth and compiler reuse benchmark + +The base is upstream `0595767`. The primary Release binaries were built from +`afecbac` and the three ordered implementation snapshots. The intervening +upstream commit adds tooling with an identical engine and benchmark tree. +The primary configuration uses GCC 14.2, LLVM 15.0.6, CPU traps, Singlepass, +Multipass and WASI. EVM and the virtual stack were disabled for these WASI +measurements. CPU affinity is specified for each cohort on AMD EPYC 9V74. + +The memory variant only changes growth after bucket migration. The object +variant builds on memory and borrows the emitted object buffer. The combined +variant also reuses the FastRA register orders. The final patch caps retained +object storage at 64 KiB. The last two cohorts isolate the cap with matching +Release spec-enabled builds of the previous and final combination. + +Each round shuffles fresh CLI processes with matching inputs and a discarded +warmup. Times include load, compilation, instantiation, execution and shutdown. +Each row verifies guest stdout, stderr and exit. Wasmtime uses cache-disabled +official 45.0.0 and 51.0.0 binaries. Percent changes are paired medians with +pointwise bootstrap 95% intervals. A/A repeats the same executable. + +## Dual-core Lazy + +41 measured rounds, milliseconds. + +| Case | Main | Memory | Memory + object | Combined | 45 Cranelift | 45 Winch | 51 Cranelift | Combined vs main | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| md5 | 20.87 | 20.78 | 20.53 | 20.36 | 27.10 | 9.08 | 27.32 | -0.56% [-2.30%, +3.24%] | -1.56% [-3.37%, +1.06%] | +| echo | 19.09 | 19.31 | 19.18 | 18.74 | 22.88 | 7.16 | 23.47 | -1.22% [-2.47%, +1.22%] | +0.84% [-1.17%, +4.29%] | +| figlet | 39.17 | 39.91 | 38.85 | 38.76 | 52.77 | 13.72 | 56.60 | -0.39% [-2.24%, +0.37%] | -0.13% [-1.17%, +1.57%] | +| qr2text | 40.25 | 40.59 | 40.42 | 40.30 | 63.42 | 19.96 | 66.67 | +0.11% [-1.94%, +2.43%] | -0.41% [-2.09%, +2.27%] | +| base64cli | 89.53 | 90.77 | 90.72 | 90.16 | 231.52 | 54.75 | 250.10 | +0.57% [-1.80%, +2.19%] | +0.31% [-1.23%, +1.90%] | +| bicg | 56.56 | 55.86 | 55.54 | 55.78 | 56.65 | 58.14 | 56.38 | -0.12% [-3.74%, +1.28%] | -0.66% [-1.39%, +0.47%] | +| atax | 46.83 | 45.66 | 45.64 | 45.61 | 48.82 | 60.41 | 50.19 | -1.08% [-3.38%, +0.59%] | -0.92% [-2.45%, +1.47%] | + +| Case | Memory vs main | Object vs memory | FastRA vs memory + object | +| --- | ---: | ---: | ---: | +| md5 | -0.40% [-2.40%, +2.71%] | -0.75% [-4.12%, +0.52%] | -0.24% [-2.29%, +4.10%] | +| echo | +1.22% [-0.87%, +4.14%] | +0.16% [-2.18%, +1.10%] | -2.05% [-4.10%, -0.01%] | +| figlet | +1.53% [-0.08%, +2.73%] | -1.95% [-3.57%, +0.49%] | -0.01% [-1.23%, +1.51%] | +| qr2text | +0.22% [-1.34%, +1.45%] | +0.73% [-0.65%, +1.88%] | -0.23% [-2.15%, +1.12%] | +| base64cli | +0.19% [-0.82%, +2.78%] | -0.09% [-2.28%, +0.44%] | -0.26% [-1.32%, +0.99%] | +| bicg | -0.94% [-4.65%, +0.36%] | -0.01% [-1.44%, +4.07%] | -0.09% [-3.04%, +2.50%] | +| atax | -0.39% [-1.64%, +0.10%] | -1.15% [-2.33%, +0.79%] | -0.17% [-1.28%, +1.40%] | + +## Single-core Lazy + +31 measured rounds, milliseconds. + +| Case | Main | Memory | Memory + object | Combined | 45 Cranelift | 45 Winch | 51 Cranelift | Combined vs main | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| md5 | 19.97 | 20.24 | 19.85 | 20.02 | 36.68 | 10.47 | 38.35 | +1.95% [-1.79%, +2.70%] | -0.38% [-2.90%, +2.92%] | +| echo | 18.74 | 19.18 | 18.97 | 18.81 | 32.18 | 9.90 | 33.86 | -0.14% [-1.52%, +1.73%] | -0.72% [-1.99%, +1.68%] | +| figlet | 38.51 | 38.74 | 38.57 | 38.39 | 96.97 | 20.15 | 100.91 | +0.28% [-2.80%, +2.08%] | +0.20% [-2.00%, +1.84%] | +| base64cli | 87.81 | 87.44 | 87.71 | 87.13 | 435.94 | 88.15 | 459.62 | -1.12% [-1.58%, -0.44%] | -0.26% [-0.74%, +0.11%] | + +| Case | Memory vs main | Object vs memory | FastRA vs memory + object | +| --- | ---: | ---: | ---: | +| md5 | +1.73% [+0.44%, +4.05%] | -1.38% [-2.35%, +0.21%] | +0.83% [-1.52%, +2.03%] | +| echo | +1.54% [-0.08%, +3.33%] | +0.33% [-1.34%, +1.15%] | -0.29% [-2.42%, +0.77%] | +| figlet | +0.17% [-0.39%, +3.10%] | -0.73% [-1.65%, -0.03%] | -0.47% [-2.14%, +1.25%] | +| base64cli | -0.42% [-0.91%, +0.17%] | -0.21% [-0.71%, +0.64%] | -0.54% [-1.41%, +0.02%] | + +## IRB Lazy + +7 measured rounds, milliseconds. + +| Case | Main | Memory | Memory + object | Combined | 45 Cranelift | 45 Winch | 51 Cranelift | Combined vs main | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| irb | 3,264.92 | 1,822.66 | 1,839.94 | 1,785.26 | 4,780.35 | 920.67 | 5,386.46 | -46.71% [-48.10%, -43.96%] | -1.32% [-3.34%, +4.60%] | + +| Case | Memory vs main | Object vs memory | FastRA vs memory + object | +| --- | ---: | ---: | ---: | +| irb | -44.45% [-47.70%, -41.78%] | +1.99% [-8.68%, +9.79%] | -1.45% [-7.20%, +0.01%] | + +## IRB Singlepass + +5 measured rounds, milliseconds. + +| Case | Main | Memory | Combined | 45 Cranelift | 45 Winch | 51 Cranelift | Combined vs main | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| irb | 2,045.32 | 469.41 | 475.17 | 4,838.10 | 941.54 | 5,291.41 | -76.51% [-76.77%, -74.06%] | +5.17% [-16.58%, +13.62%] | + +## Singlepass controls + +15 measured rounds, milliseconds. + +| Case | Main | Combined | 45 Cranelift | 45 Winch | 51 Cranelift | Combined vs main | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| md5 | 5.21 | 5.24 | 24.38 | 8.43 | 24.72 | +1.02% [-6.14%, +7.90%] | +4.65% [-3.62%, +10.63%] | +| figlet | 8.23 | 7.98 | 50.91 | 13.20 | 54.27 | -1.81% [-7.82%, +0.19%] | -0.27% [-8.40%, +0.59%] | +| bicg | 79.09 | 77.15 | 53.80 | 56.66 | 52.83 | +0.20% [-4.35%, +3.16%] | -0.60% [-5.82%, +1.41%] | +| atax | 77.90 | 80.76 | 48.62 | 61.01 | 48.24 | +2.60% [-0.27%, +8.88%] | +0.02% [-1.37%, +1.12%] | + +## Eager controls + +15 measured rounds, milliseconds. + +| Case | Main | Combined | 45 Cranelift | 45 Winch | 51 Cranelift | Combined vs main | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| md5 | 67.01 | 66.22 | 26.21 | 8.08 | 25.43 | -0.75% [-3.23%, +3.45%] | -3.04% [-22.41%, +10.53%] | +| figlet | 117.85 | 116.44 | 51.82 | 13.58 | 56.87 | -0.79% [-4.52%, +1.97%] | -0.19% [-3.27%, +5.54%] | +| bicg | 87.35 | 90.48 | 52.56 | 57.62 | 52.22 | +3.98% [-3.50%, +13.81%] | +0.30% [-2.66%, +19.50%] | +| atax | 84.41 | 87.83 | 42.12 | 58.91 | 42.77 | -1.14% [-10.45%, +15.40%] | -4.05% [-11.74%, +10.19%] | + +## Large PolyBench control + +3 measured rounds, milliseconds. + +| Case | Main | Combined | 45 Cranelift | 45 Winch | 51 Cranelift | Combined vs main | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| syr2k | 2,296.09 | 2,288.94 | 2,782.76 | 5,442.13 | 2,884.34 | -0.31% [-3.54%, +7.18%] | +4.41% [+0.66%, +5.27%] | + +## Final buffer cap controls + +31 measured rounds, milliseconds. + +| Case | Uncapped | Capped | 45 Cranelift | 45 Winch | 51 Cranelift | Capped vs uncapped | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| md5 | 19.75 | 19.70 | 24.64 | 7.94 | 24.78 | +0.56% [-1.36%, +3.05%] | +0.95% [-0.73%, +2.81%] | +| echo | 18.55 | 18.63 | 22.94 | 7.30 | 23.32 | +0.96% [-2.05%, +2.29%] | +1.45% [-1.40%, +5.24%] | +| figlet | 37.26 | 37.28 | 52.38 | 13.71 | 56.29 | -0.08% [-1.31%, +1.16%] | +1.14% [-1.99%, +3.01%] | +| base64cli | 89.40 | 88.17 | 233.47 | 53.71 | 249.35 | -1.77% [-2.60%, +1.36%] | -0.04% [-1.06%, +1.01%] | +| bicg | 55.70 | 54.63 | 55.41 | 57.30 | 55.28 | -0.88% [-2.91%, +2.64%] | -3.31% [-7.34%, +1.93%] | +| atax | 47.11 | 45.92 | 44.82 | 60.00 | 44.74 | -0.57% [-2.12%, +0.32%] | -1.68% [-2.45%, -0.19%] | + +## Final buffer cap on IRB + +3 measured rounds, milliseconds. + +| Case | Uncapped | Capped | 45 Cranelift | 45 Winch | 51 Cranelift | Capped vs uncapped | Baseline A/A | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | ---: | +| irb | 1,737.73 | 1,739.40 | 4,712.11 | 884.77 | 5,266.84 | +1.63% [-2.34%, +2.29%] | +4.02% [-1.23%, +4.22%] | + +## Compiler diagnostics + +Separate single-core processes with `--enable-statistics` measure aggregate +foreground compilation and the execution timer with foreground JIT removed. +The libc allocation cohort counts requests and requested bytes through +`malloc`, `calloc` and `realloc`. Requested capacity is cumulative over the +process. These runs are independent from the wall-time cohorts. + +| Case | Foreground compiles | Main ms | Memory ms | Memory + object ms | Combined ms | Compiler changes vs memory | +| --- | ---: | ---: | ---: | ---: | ---: | ---: | +| md5 | 31 | 14.94 | 14.85 | 14.68 | 14.63 | -1.21% [-2.02%, +0.19%] | +| echo | 30 | 13.59 | 13.45 | 13.54 | 13.40 | -0.54% [-1.29%, -0.05%] | +| figlet | 113 | 30.81 | 30.93 | 30.60 | 30.38 | -2.35% [-2.93%, -1.69%] | +| qr2text | 74 | 33.54 | 33.58 | 33.20 | 32.84 | -2.61% [-4.43%, -1.20%] | +| base64cli | 215 | 79.82 | 79.41 | 79.20 | 79.02 | -0.39% [-1.26%, +0.95%] | +| bicg | 35 | 17.85 | 18.50 | 17.82 | 17.80 | -2.17% [-2.87%, +1.37%] | +| atax | 35 | 17.47 | 17.58 | 17.38 | 17.22 | -2.02% [-4.35%, +0.70%] | +| irb | 1910 | 1,440.54 | 1,454.99 | 1,470.56 | 1,441.13 | -0.95% [-2.23%, +10.74%] | + +| Case | Main calls | Memory calls | Memory + object calls | Combined calls | Requested MiB saved by compiler changes | +| --- | ---: | ---: | ---: | ---: | ---: | +| md5 | 35213 | 35213 | 34906 | 34764 | 0.25 | +| echo | 33022 | 33022 | 32728 | 32591 | 0.23 | +| figlet | 85444 | 85444 | 84248 | 83690 | 0.82 | +| qr2text | 79502 | 79502 | 78721 | 78330 | 0.67 | +| base64cli | 197541 | 197541 | 195227 | 194109 | 1.90 | +| bicg | 42560 | 42560 | 42221 | 42066 | 0.24 | +| atax | 40222 | 40222 | 39883 | 39728 | 0.24 | +| irb | 3206695 | 3206695 | 3183738 | 3172404 | 28.32 | + +## Findings and verification + +The first migration out of a bucket still copies the accessible prefix. The +independent mapping keeps its base through later growth. On `irb`, the +Singlepass trace counted 50 reservations and 49 releases of 8 GiB in main, +and two reservations and one release in both the memory-only and combined +variants. These are virtual reservations. The execution timer after removing +foreground JIT fell from 1,736.67 to 210.41 ms in the diagnostic cohort. + +The compiler reuse changes reduced `irb` allocation requests from 3,206,695 +to 3,172,404 relative to memory alone. Cumulative requested capacity dropped +by 28.32 MiB. Foreground compile medians changed from 1,455.00 to 1,441.13 ms. +The short-process and incremental IRB process intervals limit the precision +of an independent wall-time claim. The cap has its own matched control above. + +The final Release spec-enabled build passed seven CTest targets. Both +bucket tests ran and passed, including existing slot retirement coverage. +The boundary regression uses two live instances. It verifies 32 MiB bucket +exhaustion, one copy on migration, a stable independent base, prefix writes, +zero newly committed pages, zero growth, a 514-page runtime cap, overflowing +growth, and traps past the new size. The standalone crossing fixture passed +in interpreter, Singlepass, Eager and Lazy with both default mapping and +explicit heap/soft checks. The standard `memory_grow` case passed in those +four modes. The first-growth anonymous tail and bucket guards retain their +existing implementation. The runtime's dWASM default cap is checked before +allocation. SGX was reviewed at source level. EVM JIT logging and Linux perf +compiler declarations passed syntax checks. + +The serial FastRA page capture compares five workloads. Each variant has the +same number of executable mappings. The sets of pages with differing hashes +were the same as on a repeat of main, which also changes process addresses. +The timed and traced guests emitted matching output. diff --git a/docs/changes/2026-11-29-wasm-memory-grow-lazy-compile/README.md b/docs/changes/2026-11-29-wasm-memory-grow-lazy-compile/README.md new file mode 100644 index 000000000..be8599538 --- /dev/null +++ b/docs/changes/2026-11-29-wasm-memory-grow-lazy-compile/README.md @@ -0,0 +1,22 @@ +# Reuse Wasm memory reservations and compiler state + +- **Status**: Proposed +- **Date**: 2026-11-29 +- **Tier**: Full + +When Wasm linear memory outgrows a mapped bucket, the allocator moves it +to an independent mapping. Subsequent growth uses that mapping's own +capacity. The first move retains the bucket's existing protection, +copying, and retirement rules. Guest page limits apply before growth. + +Multipass keeps FastRA's register-class orders in each compile context. +It checks target, callee-saved and reserved registers before reuse. +The object loader borrows the emitted bytes during parsing and copying, +then retains up to 64 KiB of capacity in the context. These changes preserve +register selection, relocation processing and the code publication path. + +See the [runtime specification](../../modules/runtime/spec.md) and +[compiler specification](../../modules/compiler/spec.md). + +The [benchmark report](BENCHMARK.md) includes matched process measurements, +incremental compiler experiments and the mapping correctness checks. diff --git a/docs/modules/compiler/spec.md b/docs/modules/compiler/spec.md index ac928ff95..d5237c492 100644 --- a/docs/modules/compiler/spec.md +++ b/docs/modules/compiler/spec.md @@ -245,6 +245,15 @@ and the sole internal function export is `_start` with signature configured allocator. Its stub publishes directly through the serial lazy path. The thread pool remains available for opt-in private promotion. +### Per-context compiler reuse + +FastRA caches target register-class orders in its compile context. A +change in target register information, callee-saved registers or the +current function's reserved register mask invalidates the orders. The +loader reads the emitted object from the context's byte buffer and resets +the buffer size after parsing, relocation extraction and code copying. It +releases backing allocations larger than 64 KiB. + ### Opt-in private loop tiering `EnableLazyPrivateTiering` permits demand-compiled private WASM helpers to diff --git a/docs/modules/runtime/spec.md b/docs/modules/runtime/spec.md index 033ecaacb..007329f83 100644 --- a/docs/modules/runtime/spec.md +++ b/docs/modules/runtime/spec.md @@ -139,4 +139,7 @@ anonymous PROT_NONE pages. Existing mprotect exposes exactly the requested size; later growth reuses the reservation, since Wasm memory never shrinks. Existing contents, neighbouring items and guard permissions stay unchanged. Accounting, protection and bucket retirement operate across both mapping types. Growth past -the bucket limit retains the existing allocate/copy fallback. +the bucket limit uses the allocate/copy fallback. The resulting independent +mapping grows in place within its own reservation. The grow operation checks +the module and runtime page limits before changing the mapping. The reserved +tail beyond the current size stays inaccessible. diff --git a/src/compiler/cgir/pass/cg_register_class_info.cpp b/src/compiler/cgir/pass/cg_register_class_info.cpp index 4162e48c8..28b8c514b 100644 --- a/src/compiler/cgir/pass/cg_register_class_info.cpp +++ b/src/compiler/cgir/pass/cg_register_class_info.cpp @@ -63,20 +63,6 @@ void CgRegisterClassInfo::runOnCgFunction(CgFunction &cg_func) { } CalleeSavedRegs = CSR; - // Even if CSR list is same, we could have had a different allocation order - // if ignoreCSRForAllocationOrder is evaluated differently. - BitVector CSRHintsForAllocOrder(TRI->getNumRegs()); -#if 0 - for (const MCPhysReg *I = CSR; *I; ++I) - for (MCRegAliasIterator AI(*I, TRI, true); AI.isValid(); ++AI) - CSRHintsForAllocOrder[*AI] = STI.ignoreCSRForAllocationOrder(mf, *AI); -#endif - if (IgnoreCSRForAllocOrder.size() != CSRHintsForAllocOrder.size() || - IgnoreCSRForAllocOrder != CSRHintsForAllocOrder) { - Update = true; - IgnoreCSRForAllocOrder = CSRHintsForAllocOrder; - } - RegCosts = COMPILER::getTargetRegisterCosts(TRI); // Different reserved registers? @@ -125,9 +111,7 @@ void CgRegisterClassInfo::compute(const TargetRegisterClass *RC) const { uint8_t Cost = RegCosts[PhysReg]; MinCost = std::min(MinCost, Cost); - if (CalleeSavedAliases[PhysReg] && - // !STI.ignoreCSRForAllocationOrder(*MF, PhysReg)) - !IgnoreCSRForAllocOrder.test(PhysReg)) + if (CalleeSavedAliases[PhysReg]) // PhysReg aliases a CSR, save it for later. CSRAlias.push_back(PhysReg); else { diff --git a/src/compiler/cgir/pass/cg_register_class_info.h b/src/compiler/cgir/pass/cg_register_class_info.h index 4b3a2cb19..f2ff0cf8d 100644 --- a/src/compiler/cgir/pass/cg_register_class_info.h +++ b/src/compiler/cgir/pass/cg_register_class_info.h @@ -57,10 +57,6 @@ class CgRegisterClassInfo : public NonCopyable { // Map register alias to the callee saved Register. SmallVector CalleeSavedAliases; - // Indicate if a specified callee saved register be in the allocation order - // exactly as written in the tablegen descriptions or listed later. - BitVector IgnoreCSRForAllocOrder; - // Reserved registers in the current MF. BitVector Reserved; diff --git a/src/compiler/cgir/pass/fast_ra.cpp b/src/compiler/cgir/pass/fast_ra.cpp index 12be78cb1..2c34fa491 100644 --- a/src/compiler/cgir/pass/fast_ra.cpp +++ b/src/compiler/cgir/pass/fast_ra.cpp @@ -108,7 +108,7 @@ class FastRA::Impl { // Register masks attached to the current instruction. SmallVector RegMasks; - CgRegisterClassInfo RegClassInfo; + CgRegisterClassInfo *RegClassInfo = nullptr; IndexedMap StackSlotForVirtReg; Impl() : StackSlotForVirtReg(-1) {} @@ -122,7 +122,8 @@ class FastRA::Impl { MFI = &(cg_func.getFrameInfo()); _llvm_workaround = &(cg_func.getContext().getLLVMWorkaround()); MRI->freezeReservedRegs(cg_func); - RegClassInfo.runOnCgFunction(cg_func); + RegClassInfo = &cg_func.getContext().getFastRAClassInfo(); + RegClassInfo->runOnCgFunction(cg_func); unsigned NumRegUnits = TRI->getNumRegUnits(); UsedInInstr.resize(NumRegUnits); @@ -588,7 +589,7 @@ void FastRA::Impl::allocVirtReg(CgInstruction &MI, LiveReg &LR, MCPhysReg BestReg = 0; unsigned BestCost = spillImpossible; - ArrayRef CgAllocationOrder = RegClassInfo.getOrder(&RC); + ArrayRef CgAllocationOrder = RegClassInfo->getOrder(&RC); for (MCPhysReg PhysReg : CgAllocationOrder) { // LLVM_DEBUG(dbgs() << "\tRegister: " << printReg(PhysReg, TRI) << ' // '); diff --git a/src/compiler/compiler.cpp b/src/compiler/compiler.cpp index 214415534..6a7e81b15 100644 --- a/src/compiler/compiler.cpp +++ b/src/compiler/compiler.cpp @@ -25,10 +25,11 @@ #include "compiler/target/x86/x86_mc_lowering.h" #include "compiler/target/x86/x86lowering.h" #include "compiler/wasm_frontend/wasm_mir_compiler.h" +#include "llvm/ADT/ScopeExit.h" #include "llvm/Object/ELFObjectFile.h" #include "llvm/Object/ObjectFile.h" #include "llvm/Support/Host.h" -#include "llvm/Support/SmallVectorMemoryBuffer.h" +#include "llvm/Support/MemoryBuffer.h" #include #include #include @@ -175,9 +176,21 @@ void JITCompilerBase::emitObjectBuffer(CompileContext *Ctx) { Ctx->finalize(); - auto ObjectToLoad = std::make_unique( - std::move(Ctx->getObjBuffer()), false); - auto LoadedObject = llvm::object::ObjectFile::createObjectFile(*ObjectToLoad); + auto &Buffer = Ctx->getObjBuffer(); + const auto ClearBuffer = llvm::make_scope_exit([&Buffer] { + constexpr size_t MaxRetainedObjectBytes = 64 * 1024; + if (Buffer.capacity() > MaxRetainedObjectBytes) { + // Release the backing storage of unusually large objects. + llvm::SmallVector Released(std::move(Buffer)); + } else { + Buffer.clear(); + } + }); + // ObjectFile and its section views borrow the bytes until copying finishes. + // Keep the backing capacity in this compilation context for the next object. + const llvm::MemoryBufferRef ObjectToLoad( + llvm::StringRef(Buffer.data(), Buffer.size()), ""); + auto LoadedObject = llvm::object::ObjectFile::createObjectFile(ObjectToLoad); if (!LoadedObject) { throw getError(ErrorCode::ObjectFileCreationFailed); } @@ -357,7 +370,7 @@ void JITCompilerBase::emitObjectBuffer(CompileContext *Ctx) { } #ifdef ZEN_ENABLE_MULTIPASS_JIT_LOGGING - dumpAsm(ObjectToLoad->getBufferStart(), ObjectToLoad->getBufferSize()); + dumpAsm(ObjectToLoad.getBufferStart(), ObjectToLoad.getBufferSize()); #endif Ctx->CodeSize = TextSection->getSize(); @@ -376,7 +389,7 @@ void JITCompilerBase::emitObjectBuffer(CompileContext *Ctx) { #if defined(ZEN_ENABLE_EVM) && defined(ZEN_ENABLE_LINUX_PERF) && \ !defined(ZEN_ENABLE_MULTIPASS_JIT_LOGGING) - dumpAsm(ObjectToLoad->getBufferStart(), ObjectToLoad->getBufferSize(), + dumpAsm(ObjectToLoad.getBufferStart(), ObjectToLoad.getBufferSize(), Ctx->CodePtr); #endif } diff --git a/src/compiler/context.cpp b/src/compiler/context.cpp index d71da8277..3c1f6be83 100644 --- a/src/compiler/context.cpp +++ b/src/compiler/context.cpp @@ -2,6 +2,7 @@ // SPDX-License-Identifier: Apache-2.0 #include "compiler/context.h" +#include "compiler/cgir/pass/cg_register_class_info.h" #include "compiler/mir/function.h" #include "compiler/mir/pointer.h" #include "compiler/target/x86/x86_llvm_workaround.h" @@ -163,6 +164,13 @@ void CompileContext::finalize() { MCL->finalize(); } +CgRegisterClassInfo &CompileContext::getFastRAClassInfo() { + if (!FastRAClassInfo) { + FastRAClassInfo = std::make_unique(); + } + return *FastRAClassInfo; +} + /// \warning only used for lazy compilation void CompileContext::reinitialize() { ZEN_ASSERT(MCL); diff --git a/src/compiler/context.h b/src/compiler/context.h index 8d8c12a67..fb7b65f45 100644 --- a/src/compiler/context.h +++ b/src/compiler/context.h @@ -22,6 +22,7 @@ class MPointerType; class MConstantInt; class MConstantFloat; class LLVMWorkaround; +class CgRegisterClassInfo; struct FunctionTypeKeyInfo; struct PointerTypeKeyInfo; struct DenseMapAPFloatKeyInfo; @@ -75,6 +76,8 @@ class CompileContext { LLVMWorkaround &getLLVMWorkaround() const { return *Workaround; } + CgRegisterClassInfo &getFastRAClassInfo(); + llvm::LLVMTargetMachine &getTargetMachine() const { return *TM; } const llvm::TargetSubtargetInfo &getSubtargetInfo() const { return *STI; } @@ -139,6 +142,7 @@ class CompileContext { LLVMWorkaround *Workaround = nullptr; std::unique_ptr TM; llvm::TargetSubtargetInfo *STI = nullptr; + std::unique_ptr FastRAClassInfo; /// ================ MC Related ================ diff --git a/src/runtime/memory.cpp b/src/runtime/memory.cpp index 3a8fd471b..179501c35 100644 --- a/src/runtime/memory.cpp +++ b/src/runtime/memory.cpp @@ -530,13 +530,11 @@ WasmMemoryAllocator::enlargeWasmMemory(const WasmMemoryData &OldMemoryData, size_t NewMemorySize) { bool NeedFreeOldMmap = false; if (UseMmap) { - // when use bucket with mmap linear-memory, - // memory.grow must re-alloc memory to grow - bool OldUseMmap = (OldMemoryData.Type == WM_MEMORY_DATA_TYPE_BUCKET_MMAP || - OldMemoryData.Type == WM_MEMORY_DATA_TYPE_SINGLE_MMAP); - bool InstanceUseMmap = - OldUseMmap && (NewMemorySize <= MmapMemoryBucketGrowMaxSize); - if (OldUseMmap && InstanceUseMmap) { + const bool IsBucket = OldMemoryData.Type == WM_MEMORY_DATA_TYPE_BUCKET_MMAP; + const bool IsSingle = OldMemoryData.Type == WM_MEMORY_DATA_TYPE_SINGLE_MMAP; + // A mapping moved out of its bucket already owns the full reservation. + if ((IsBucket && NewMemorySize <= MmapMemoryBucketGrowMaxSize) || + (IsSingle && NewMemorySize <= WasmMemoryAllocatorMmapSize)) { // the mmap item space has enough space to grow const auto &NewMemoryData = WasmMemoryData{ .Type = OldMemoryData.Type, @@ -544,7 +542,7 @@ WasmMemoryAllocator::enlargeWasmMemory(const WasmMemoryData &OldMemoryData, .MemorySize = NewMemorySize, .NeedMprotect = false, }; - if (OldMemoryData.Type == WM_MEMORY_DATA_TYPE_BUCKET_MMAP) { + if (IsBucket) { #ifndef ZEN_ENABLE_SGX // SGX's mmap shim does not implement fixed-address replacement. const size_t InitialSize = @@ -576,9 +574,7 @@ WasmMemoryAllocator::enlargeWasmMemory(const WasmMemoryData &OldMemoryData, return NewMemoryData; } - if (OldUseMmap && !InstanceUseMmap) { - NeedFreeOldMmap = true; - } + NeedFreeOldMmap = IsBucket || IsSingle; } auto *OldMemoryAddr = OldMemoryData.MemoryData; auto OldMemorySize = OldMemoryData.MemorySize; diff --git a/src/tests/lazy_jit_greedy_ondemand_tests.cpp b/src/tests/lazy_jit_greedy_ondemand_tests.cpp index a376915fe..568807106 100644 --- a/src/tests/lazy_jit_greedy_ondemand_tests.cpp +++ b/src/tests/lazy_jit_greedy_ondemand_tests.cpp @@ -633,6 +633,7 @@ TEST(WasmBucketMemory, GrowthPreservesPrefixZeroesAndInstanceIsolation) { auto Bytes = loadWasmFile(BUCKET_GROW_WASM_PATH); auto RT = makeRuntime(false, false, true); ASSERT_NE(RT, nullptr); + RT->setVmMaxMemoryPages(514); auto ModRet = RT->loadModule("bucket_growth", Bytes.data(), Bytes.size()); ASSERT_TRUE(ModRet); auto *Mod = *ModRet; @@ -687,6 +688,34 @@ TEST(WasmBucketMemory, GrowthPreservesPrefixZeroesAndInstanceIsolation) { EXPECT_FALSE(RT->callWasmFunction(*A, "read", {"262144"}, Results)); A->clearError(); EXPECT_EQ(Call(A, "read", {"65535"}), 222u); + + // The last bucket page grows in place. Crossing the boundary detaches only + // A, which can then grow within its independent reservation. + ASSERT_EQ(Call(A, "grow", {"508"}), 4u); + EXPECT_EQ(A->getDefaultMemoryInst().MemBase, Base); + Write(A, "33554431", "19"); + ASSERT_EQ(Call(A, "grow", {"1"}), 512u); + ASSERT_EQ(A->getDefaultMemoryInst().Kind, + zen::runtime::WM_MEMORY_DATA_TYPE_SINGLE_MMAP); + auto *DetachedBase = A->getDefaultMemoryInst().MemBase; + EXPECT_NE(DetachedBase, Base); + EXPECT_EQ(Call(A, "read", {"33554431"}), 19u); + EXPECT_EQ(Call(A, "scan", {"33554432", "33619968"}), 0u); + Write(A, "33619967", "23"); + EXPECT_EQ(Call(B, "read", {"65536"}), 177u); + ASSERT_EQ(Call(A, "grow", {"1"}), 513u); + EXPECT_EQ(A->getDefaultMemoryInst().MemBase, DetachedBase); + EXPECT_EQ(Call(A, "read", {"33619967"}), 23u); + EXPECT_EQ(Call(A, "scan", {"33619968", "33685504"}), 0u); + EXPECT_EQ(Call(A, "grow", {"0"}), 514u); + EXPECT_EQ(Call(A, "grow", {"1"}), UINT32_MAX); + EXPECT_EQ(Call(A, "grow", {"4294967295"}), UINT32_MAX); + EXPECT_EQ(A->getDefaultMemoryInst().CurPages, 514u); + Results.clear(); + EXPECT_FALSE(RT->callWasmFunction(*A, "read", {"33685504"}, Results)); + A->clearError(); + EXPECT_EQ(Call(B, "read", {"0"}), 42u); + EXPECT_EQ(Call(A, "read", {"65536"}), 91u); } TEST(WasmBucketMemory, SlotRetirementAndLargerMappingFallbackStayZero) {