Skip to content
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
11 changes: 11 additions & 0 deletions src/ir/module-splitting.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@
// from the IR before splitting.
//
#include "ir/module-splitting.h"
#include "ir/effects.h"
#include "ir/find_all.h"
#include "ir/module-utils.h"
#include "ir/names.h"
Expand Down Expand Up @@ -790,6 +791,16 @@ void ModuleSplitter::shareImportableItems() {
primaryUsed.globals.insert(tableManager.activeBase.global);
}

// Trapping globals should stay in the primary module to preserve the trapping
// behavior upon instantiation.
for (auto& global : primary.globals) {
if (global->init &&
EffectAnalyzer(config.passOptions, primary, global->init)
.hasUnremovableSideEffects()) {
primaryUsed.globals.insert(global->name);
}
}

// Compute the transitive closure of globals referenced in other globals'
// initializers. Since globals can reference other globals, we must ensure
// that if a global is used in a module, all its dependencies are also marked
Expand Down
3 changes: 3 additions & 0 deletions src/ir/module-splitting.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,14 @@
#ifndef wasm_ir_module_splitting_h
#define wasm_ir_module_splitting_h

#include "pass.h"
#include "wasm.h"

namespace wasm::ModuleSplitting {

struct Config {
// Pass options to use for effects analysis
PassOptions passOptions;
// A vector of set of functions to split into that secondary. Each function
// set belongs to a single secondary module. All others are kept in the
// primary module. Must not include the start function if it exists. May or
Expand Down
10 changes: 10 additions & 0 deletions src/tools/wasm-split/split-options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,6 +358,16 @@ WasmSplitOptions::WasmSplitOptions()
{Mode::Split, Mode::MultiSplit, Mode::Instrument},
Options::Arguments::Zero,
[&](Options* o, const std::string& arguments) { stripDebug = true; })
.add("--traps-never-happen",
"-tnh",
"Split under the helpful assumption that no trap is reached at "
"runtime (from load, div/mod, etc.)",
WasmSplitOption,
{Mode::Split, Mode::MultiSplit},
Options::Arguments::Zero,
[&](Options* o, const std::string& arguments) {
passOptions.trapsNeverHappen = true;
})
.add("--output",
"-o",
"Output file.",
Expand Down
1 change: 1 addition & 0 deletions src/tools/wasm-split/wasm-split.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@ void writePlaceholderMap(

void setCommonSplitConfigs(ModuleSplitting::Config& config,
const WasmSplitOptions& options) {
config.passOptions = options.passOptions;
config.usePlaceholders = options.usePlaceholders;
config.minimizeNewExportNames = !options.passOptions.debugInfo;
if (options.importNamespace) {
Expand Down
5 changes: 5 additions & 0 deletions test/lit/help/wasm-split.test
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,11 @@
;; CHECK-NEXT: --strip-debug [split, multi-split, instrument] Strip
;; CHECK-NEXT: debug info (including the names section)
;; CHECK-NEXT:
;; CHECK-NEXT: --traps-never-happen,-tnh [split, multi-split] Split under the
;; CHECK-NEXT: helpful assumption that no trap is
;; CHECK-NEXT: reached at runtime (from load, div/mod,
;; CHECK-NEXT: etc.)
;; CHECK-NEXT:
;; CHECK-NEXT: --output,-o [instrument, merge-profiles, multi-split]
;; CHECK-NEXT: Output file.
;; CHECK-NEXT:
Expand Down
24 changes: 24 additions & 0 deletions test/lit/wasm-split/trapping-global.wast
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
;; RUN: wasm-split %s -all -g -o1 %t.1.wasm -o2 %t.2.wasm --split-funcs=split
;; RUN: wasm-dis -all %t.1.wasm | filecheck %s --check-prefix PRIMARY
;; RUN: wasm-split %s -all -g -o1 %t.tnh.1.wasm -o2 %t.tnh.2.wasm --split-funcs=split --traps-never-happen
;; RUN: wasm-dis -all %t.tnh.1.wasm | filecheck %s --check-prefix PRIMARY-TNH

;; This unused global should NOT be removed by wasm-split because its
;; initializer contains a side effect (a trap due to a null descriptor).
;; However, if we pass --traps-never-happen, we assume traps never occur, so the
;; global will be considered to have no side effects and will be removed.
(module
(rec
(type $struct (descriptor $desc) (struct))
(type $desc (describes $struct) (struct))
)
;; PRIMARY: (global $trap (ref $struct)
;; PRIMARY-TNH-NOT: (global $trap (ref $struct)
(global $trap (ref $struct)
(struct.new_desc $struct
(ref.null none)
)
)

(func $split)
)
Loading