diff options
author | Thomas Lively <tlively@google.com> | 2024-11-14 16:29:37 -0500 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-11-14 13:29:37 -0800 |
commit | 6efd41779272d2ac7eb75705c21c9f437a1409c9 (patch) | |
tree | 9890e82b1e35f483ea76394b5dcc3cfb1bb59e79 | |
parent | d4e7fee97c9fa7ae2c6f069d4428964cc1ea1795 (diff) | |
download | binaryen-6efd41779272d2ac7eb75705c21c9f437a1409c9.tar.gz binaryen-6efd41779272d2ac7eb75705c21c9f437a1409c9.tar.bz2 binaryen-6efd41779272d2ac7eb75705c21c9f437a1409c9.zip |
Record binary locations for nested blocks (#7078)
The binary reader has special handling for blocks immediately nested
inside other blocks to eliminate recursion while parsing very deep
stacks of blocks. This special handling did not record binary locations
for the nested blocks, though.
Add logic to record binary locations for nested blocks. This binary
reading code is about to be replaced with completely different code that
uses IRBuilder instead, but this change will eliminate some test
differences that we would otherwise see when we make that change.
-rw-r--r-- | src/wasm/wasm-binary.cpp | 20 | ||||
-rw-r--r-- | test/passes/dwarf-local-order.bin.txt | 1 | ||||
-rw-r--r-- | test/passes/fannkuch0_dwarf.bin.txt | 5 | ||||
-rw-r--r-- | test/passes/fannkuch3_dwarf.bin.txt | 7 | ||||
-rw-r--r-- | test/passes/fannkuch3_manyopts_dwarf.bin.txt | 4 | ||||
-rw-r--r-- | test/passes/reverse_dwarf_abbrevs.bin.txt | 4 |
6 files changed, 41 insertions, 0 deletions
diff --git a/src/wasm/wasm-binary.cpp b/src/wasm/wasm-binary.cpp index f5bf11206..73718e636 100644 --- a/src/wasm/wasm-binary.cpp +++ b/src/wasm/wasm-binary.cpp @@ -4453,13 +4453,21 @@ void WasmBinaryReader::visitBlock(Block* curr) { // special-case Block and de-recurse nested blocks in their first position, as // that is a common pattern that can be very highly nested. std::vector<Block*> stack; + // Track start positions for all blocks except for the outermost block, which + // is already handled in the caller. + std::vector<size_t> startPosStack; + size_t startPos = -1; while (1) { curr->type = getType(); curr->name = getNextLabel(); breakStack.push_back({curr->name, curr->type}); stack.push_back(curr); + if (startPos != size_t(-1)) { + startPosStack.push_back(startPos); + } if (more() && input[pos] == BinaryConsts::Block) { // a recursion + startPos = pos; readNextDebugLocation(); curr = allocator.alloc<Block>(); startControlFlow(curr); @@ -4478,6 +4486,12 @@ void WasmBinaryReader::visitBlock(Block* curr) { while (stack.size() > 0) { curr = stack.back(); stack.pop_back(); + if (startPosStack.empty()) { + startPos = -1; + } else { + startPos = startPosStack.back(); + startPosStack.pop_back(); + } // everything after this, that is left when we see the marker, is ours size_t start = expressionStack.size(); if (last) { @@ -4497,6 +4511,12 @@ void WasmBinaryReader::visitBlock(Block* curr) { : Block::NoBreak); breakStack.pop_back(); breakTargetNames.erase(curr->name); + + if (DWARF && currFunction && startPos != size_t(-1)) { + currFunction->expressionLocations[curr] = + BinaryLocations::Span{BinaryLocation(startPos - codeSectionLocation), + BinaryLocation(pos - codeSectionLocation)}; + } } } diff --git a/test/passes/dwarf-local-order.bin.txt b/test/passes/dwarf-local-order.bin.txt index f6b827702..509db04b1 100644 --- a/test/passes/dwarf-local-order.bin.txt +++ b/test/passes/dwarf-local-order.bin.txt @@ -341,6 +341,7 @@ ) ;; code offset: 0xa9 (block $label$1 + ;; code offset: 0xab (block $label$2 ;; code offset: 0xaf (br_if $label$2 diff --git a/test/passes/fannkuch0_dwarf.bin.txt b/test/passes/fannkuch0_dwarf.bin.txt index e761baf70..bee23dc18 100644 --- a/test/passes/fannkuch0_dwarf.bin.txt +++ b/test/passes/fannkuch0_dwarf.bin.txt @@ -7740,6 +7740,7 @@ file_names[ 3]: ) ;; code offset: 0xa1f (block $label$17 + ;; code offset: 0xa21 (block $label$18 ;; code offset: 0xa27 (br_if $label$18 @@ -7927,6 +7928,7 @@ file_names[ 3]: ) ;; code offset: 0xaed (block $label$1 + ;; code offset: 0xaef (block $label$2 ;; code offset: 0xaf4 (br_if $label$2 @@ -8041,6 +8043,7 @@ file_names[ 3]: ) ;; code offset: 0xb4a (block $label$3 + ;; code offset: 0xb4c (block $label$4 ;; code offset: 0xb51 (br_if $label$4 @@ -8914,6 +8917,7 @@ file_names[ 3]: ) ;; code offset: 0xefd (block $label$7 + ;; code offset: 0xeff (block $label$8 ;; code offset: 0xf04 (br_if $label$8 @@ -9818,6 +9822,7 @@ file_names[ 3]: ) ;; code offset: 0x1207 (block $label$17 + ;; code offset: 0x1209 (block $label$18 ;; code offset: 0x120f (br_if $label$18 diff --git a/test/passes/fannkuch3_dwarf.bin.txt b/test/passes/fannkuch3_dwarf.bin.txt index f58068ec3..74b6446a5 100644 --- a/test/passes/fannkuch3_dwarf.bin.txt +++ b/test/passes/fannkuch3_dwarf.bin.txt @@ -4874,7 +4874,9 @@ file_names[ 4]: ) ;; code offset: 0x47 (block $label$1 + ;; code offset: 0x49 (block $label$2 + ;; code offset: 0x4b (block $label$3 ;; code offset: 0x52 (br_if $label$3 @@ -6022,7 +6024,9 @@ file_names[ 4]: ) ;; code offset: 0x3bd (block $label$1 + ;; code offset: 0x3bf (block $label$2 + ;; code offset: 0x3c1 (block $label$3 ;; code offset: 0x3c8 (br_if $label$3 @@ -6192,8 +6196,11 @@ file_names[ 4]: ) ;; code offset: 0x444 (block $label$6 + ;; code offset: 0x446 (block $label$7 + ;; code offset: 0x448 (block $label$8 + ;; code offset: 0x44a (block $label$9 ;; code offset: 0x451 (br_if $label$9 diff --git a/test/passes/fannkuch3_manyopts_dwarf.bin.txt b/test/passes/fannkuch3_manyopts_dwarf.bin.txt index fb6008541..3ffb81efe 100644 --- a/test/passes/fannkuch3_manyopts_dwarf.bin.txt +++ b/test/passes/fannkuch3_manyopts_dwarf.bin.txt @@ -4752,6 +4752,7 @@ file_names[ 4]: ) ;; code offset: 0x43 (block $label$1 + ;; code offset: 0x45 (block $label$2 ;; code offset: 0x4c (if @@ -5886,6 +5887,7 @@ file_names[ 4]: ) ;; code offset: 0x39b (block $label$1 + ;; code offset: 0x39d (block $label$2 ;; code offset: 0x3a4 (if @@ -6050,7 +6052,9 @@ file_names[ 4]: ) ;; code offset: 0x41a (block $label$6 + ;; code offset: 0x41c (block $label$7 + ;; code offset: 0x41e (block $label$8 ;; code offset: 0x425 (if diff --git a/test/passes/reverse_dwarf_abbrevs.bin.txt b/test/passes/reverse_dwarf_abbrevs.bin.txt index 2500b625b..5427acc6c 100644 --- a/test/passes/reverse_dwarf_abbrevs.bin.txt +++ b/test/passes/reverse_dwarf_abbrevs.bin.txt @@ -293,7 +293,9 @@ file_names[ 1]: (local.set $4 ;; code offset: 0x89 (block $label$1 (result i32) + ;; code offset: 0x8b (block $label$2 + ;; code offset: 0x8d (block $label$3 ;; code offset: 0xa5 (if @@ -1387,6 +1389,7 @@ file_names[ 1]: (block $label$1 ;; code offset: 0x43a (if + ;; code offset: 0x412 (block $label$2 (result i32) ;; code offset: 0x41c (if @@ -2102,6 +2105,7 @@ file_names[ 1]: ) ;; code offset: 0x65a (block $label$1 + ;; code offset: 0x65c (block $label$2 ;; code offset: 0x664 (br_if $label$2 |