Skip to content

Commit

Permalink
Use hints when generating fresh labels in IRBuilder
Browse files Browse the repository at this point in the history
IRBuilder often has to generate new label names for blocks and other
scopes. Previously it would generate each new name by starting with
"block" or "label" and incrementing a suffix until finding a fresh name,
but this made name generation quadratic in the number of names to
generate.

To spend less time generating names, track a hint index at which to
start looking for a fresh name and increment it every time a name is
generated. This speeds up a version of the binary parser that uses
IRBuilder by about 15%.
  • Loading branch information
tlively committed Nov 18, 2024
1 parent 3981163 commit 5b2c76f
Show file tree
Hide file tree
Showing 8 changed files with 87 additions and 82 deletions.
7 changes: 5 additions & 2 deletions src/wasm-ir-builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -507,16 +507,19 @@ class IRBuilder : public UnifiedExpressionVisitor<IRBuilder, Result<>> {
// its stack.
std::unordered_map<Name, std::vector<Index>> labelDepths;

Name makeFresh(Name label) {
Name makeFresh(Name label, Index hint = 0) {
return Names::getValidName(
label,
[&](Name candidate) {
return labelDepths.insert({candidate, {}}).second;
},
0,
hint,
"");
}

Index blockHint = 0;
Index labelHint = 0;

void pushScope(ScopeCtx scope) {
if (auto label = scope.getOriginalLabel()) {
// Assign a fresh label to the scope, if necessary.
Expand Down
6 changes: 4 additions & 2 deletions src/wasm/wasm-ir-builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -987,6 +987,8 @@ Result<> IRBuilder::visitEnd() {
EHUtils::handleBlockNestedPops(func, wasm);
}
this->func = nullptr;
blockHint = 0;
labelHint = 0;
} else if (auto* block = scope.getBlock()) {
assert(*expr == block);
block->name = scope.label;
Expand Down Expand Up @@ -1073,9 +1075,9 @@ Result<Name> IRBuilder::getLabelName(Index label, bool forDelegate) {
if (!scopeLabel) {
// The scope does not already have a name, so we need to create one.
if ((*scope)->getBlock()) {
scopeLabel = makeFresh("block");
scopeLabel = makeFresh("block", blockHint++);
} else {
scopeLabel = makeFresh("label");
scopeLabel = makeFresh("label", labelHint++);
}
}
if (!forDelegate) {
Expand Down
38 changes: 19 additions & 19 deletions test/lit/basic/reference-types.wast
Original file line number Diff line number Diff line change
Expand Up @@ -361,88 +361,88 @@
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block0 (result eqref)
;; CHECK-TEXT-NEXT: (br_if $block0
;; CHECK-TEXT-NEXT: (global.get $global_eqref)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block1 (result eqref)
;; CHECK-TEXT-NEXT: (br_if $block1
;; CHECK-TEXT-NEXT: (ref.null none)
;; CHECK-TEXT-NEXT: (global.get $global_eqref)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block2 (result funcref)
;; CHECK-TEXT-NEXT: (block $block2 (result eqref)
;; CHECK-TEXT-NEXT: (br_if $block2
;; CHECK-TEXT-NEXT: (local.get $local_funcref)
;; CHECK-TEXT-NEXT: (ref.null none)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block3 (result funcref)
;; CHECK-TEXT-NEXT: (br_if $block3
;; CHECK-TEXT-NEXT: (global.get $global_funcref)
;; CHECK-TEXT-NEXT: (local.get $local_funcref)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block4 (result funcref)
;; CHECK-TEXT-NEXT: (br_if $block4
;; CHECK-TEXT-NEXT: (ref.null nofunc)
;; CHECK-TEXT-NEXT: (global.get $global_funcref)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block5 (result funcref)
;; CHECK-TEXT-NEXT: (br_if $block5
;; CHECK-TEXT-NEXT: (ref.func $foo)
;; CHECK-TEXT-NEXT: (ref.null nofunc)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block6 (result anyref)
;; CHECK-TEXT-NEXT: (block $block6 (result funcref)
;; CHECK-TEXT-NEXT: (br_if $block6
;; CHECK-TEXT-NEXT: (local.get $local_anyref)
;; CHECK-TEXT-NEXT: (ref.func $foo)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block7 (result anyref)
;; CHECK-TEXT-NEXT: (br_if $block7
;; CHECK-TEXT-NEXT: (global.get $global_anyref)
;; CHECK-TEXT-NEXT: (local.get $local_anyref)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block8 (result anyref)
;; CHECK-TEXT-NEXT: (br_if $block8
;; CHECK-TEXT-NEXT: (ref.null none)
;; CHECK-TEXT-NEXT: (global.get $global_anyref)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block9 (result anyref)
;; CHECK-TEXT-NEXT: (br_if $block9
;; CHECK-TEXT-NEXT: (local.get $local_eqref)
;; CHECK-TEXT-NEXT: (ref.null none)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block10 (result anyref)
;; CHECK-TEXT-NEXT: (br_if $block10
;; CHECK-TEXT-NEXT: (local.get $local_eqref)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: )
;; CHECK-TEXT-NEXT: (drop
;; CHECK-TEXT-NEXT: (block $block11 (result anyref)
;; CHECK-TEXT-NEXT: (br_if $block11
;; CHECK-TEXT-NEXT: (ref.null none)
;; CHECK-TEXT-NEXT: (i32.const 1)
;; CHECK-TEXT-NEXT: )
Expand Down
4 changes: 2 additions & 2 deletions test/lit/passes/outlining.wast
Original file line number Diff line number Diff line change
Expand Up @@ -675,8 +675,8 @@
;; CHECK: (func $a (type $1) (param $0 i32) (result i32)
;; CHECK-NEXT: (call $outline$)
;; CHECK-NEXT: (block $block
;; CHECK-NEXT: (block $block0
;; CHECK-NEXT: (br_table $block $block0
;; CHECK-NEXT: (block $block1
;; CHECK-NEXT: (br_table $block $block1
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: )
;; CHECK-NEXT: (return
Expand Down
18 changes: 9 additions & 9 deletions test/lit/wat-kitchen-sink.wast
Original file line number Diff line number Diff line change
Expand Up @@ -2570,14 +2570,14 @@
)

;; CHECK: (func $label-index (type $0)
;; CHECK-NEXT: (block $block1
;; CHECK-NEXT: (block $block2
;; CHECK-NEXT: (block $block
;; CHECK-NEXT: (block $block0
;; CHECK-NEXT: (block $block1
;; CHECK-NEXT: (block $l
;; CHECK-NEXT: (br $block)
;; CHECK-NEXT: (br $block0)
;; CHECK-NEXT: (br $l)
;; CHECK-NEXT: (br $block1)
;; CHECK-NEXT: (br $l)
;; CHECK-NEXT: (br $block2)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand Down Expand Up @@ -2844,9 +2844,9 @@
;; CHECK: (func $br-table-index (type $0)
;; CHECK-NEXT: (block $block
;; CHECK-NEXT: (block $l
;; CHECK-NEXT: (block $block1
;; CHECK-NEXT: (block $block0
;; CHECK-NEXT: (br_table $block $l $block0 $block1
;; CHECK-NEXT: (block $block2
;; CHECK-NEXT: (block $block1
;; CHECK-NEXT: (br_table $block $l $block1 $block2
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: )
;; CHECK-NEXT: )
Expand Down Expand Up @@ -4821,9 +4821,9 @@
;; CHECK-NEXT: (drop
;; CHECK-NEXT: (block $block (result (ref $to-f32-cont))
;; CHECK-NEXT: (tuple.drop 3
;; CHECK-NEXT: (block $block0 (type $34) (result i32 i64 (ref null $simple-cont))
;; CHECK-NEXT: (block $block1 (type $34) (result i32 i64 (ref null $simple-cont))
;; CHECK-NEXT: (local.set $f
;; CHECK-NEXT: (resume $simple-cont (on $empty $block) (on $tag-pair-to-pair $block0)
;; CHECK-NEXT: (resume $simple-cont (on $empty $block) (on $tag-pair-to-pair $block1)
;; CHECK-NEXT: (i32.const 0)
;; CHECK-NEXT: (i64.const 1)
;; CHECK-NEXT: (local.get $ct)
Expand Down
Loading

0 comments on commit 5b2c76f

Please sign in to comment.