Skip to content

Customize the limit of MaxDataSegments #7285

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 20, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 10 additions & 8 deletions src/ir/memory-utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,14 @@ inline void ensureExists(Module* wasm) {
// Try to merge segments until they fit into web limitations.
// Return true if successful.
// Does not yet support multimemory
inline bool ensureLimitedSegments(Module& module) {
inline bool
ensureLimitedSegments(Module& module,
Index maxDataSegments = WebLimitations::MaxDataSegments) {
if (module.memories.size() > 1) {
return false;
}
auto& dataSegments = module.dataSegments;
if (dataSegments.size() <= WebLimitations::MaxDataSegments) {
if (dataSegments.size() <= maxDataSegments) {
return true;
}

Expand Down Expand Up @@ -86,19 +88,19 @@ inline bool ensureLimitedSegments(Module& module) {

// check if we have too many dynamic data segments, which we can do nothing
// about
if (numDynamic + 1 >= WebLimitations::MaxDataSegments) {
if (numDynamic + 1 >= maxDataSegments) {
return false;
}

// we'll merge constant segments if we must
if (numConstant + numDynamic >= WebLimitations::MaxDataSegments) {
numConstant = WebLimitations::MaxDataSegments - numDynamic - 1;
if (numConstant + numDynamic >= maxDataSegments) {
numConstant = maxDataSegments - numDynamic - 1;
[[maybe_unused]] auto num = numConstant + numDynamic;
assert(num == WebLimitations::MaxDataSegments - 1);
assert(num == maxDataSegments - 1);
}

std::vector<std::unique_ptr<wasm::DataSegment>> mergedSegments;
mergedSegments.reserve(WebLimitations::MaxDataSegments);
mergedSegments.reserve(maxDataSegments);

// drop empty segments and pass through dynamic-offset segments
for (auto& segment : dataSegments) {
Expand All @@ -121,7 +123,7 @@ inline bool ensureLimitedSegments(Module& module) {
if (!isRelevant(*segment)) {
continue;
}
if (mergedSegments.size() + 2 < WebLimitations::MaxDataSegments) {
if (mergedSegments.size() + 2 < maxDataSegments) {
mergedSegments.push_back(std::move(segment));
continue;
}
Expand Down
19 changes: 18 additions & 1 deletion src/passes/LimitSegments.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,28 @@
#include "pass.h"
#include "wasm.h"

//
// Attempt to merge segments to fit within a specified limit.
//
// By default this limit is equal to the one commonly used by wasm VMs
// (see wasm-limits.h), but it can be changed with the option below:
//
// --pass-arg=limit-segments@max-data-segments
//
// Specify a custom maximum number of data segments.
//

namespace wasm {

struct LimitSegments : public Pass {
void run(Module* module) override {
if (!MemoryUtils::ensureLimitedSegments(*module)) {
Index maxDataSegments;
if (hasArgument("limit-segments")) {
maxDataSegments = std::stoul(getArgument("limit-segments", ""));
} else {
maxDataSegments = WebLimitations::MaxDataSegments;
}
if (!MemoryUtils::ensureLimitedSegments(*module, maxDataSegments)) {
std::cerr << "Unable to merge segments. "
<< "wasm VMs may not accept this binary" << std::endl;
}
Expand Down
13 changes: 13 additions & 0 deletions test/lit/passes/custom-max-data-segments.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
;; RUN: wasm-opt %s --limit-segments --pass-arg=limit-segments@3 -S -o - | filecheck %s

;; Test that the data segments custom limit is respected.
(module
(memory 256 256)
;; CHECK: (data $0 (i32.const 0) "A")
(data (i32.const 0) "A")
;; CHECK: (data $"" (i32.const 1) "AAAA")
(data (i32.const 1) "A")
(data (i32.const 2) "A")
(data (i32.const 3) "A")
(data (i32.const 4) "A")
)
Loading