-
Notifications
You must be signed in to change notification settings - Fork 753
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
fix: sourcemap generation for call instruction #5504
fix: sourcemap generation for call instruction #5504
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I took at look at the repo you provided (thanks!), but the testcase is large - looks like it uses all of Binaryen? Instead can you make a small testcase that we can also add to the test suite here?
I made a quick attempt at a lit test now:
;; RUN: wasm-opt %s -o %t.wasm -osm %t.map -q
;; RUN: wasm-opt %t.wasm -ism %t.map -q -o - -S | filecheck %s
(module
(func $foo (param $x i32) (param $y i32)
;;@ src.cpp:10:1
(if
;;@ src.cpp:20:1
(i32.add
;;@ src.cpp:30:1
(local.get $x)
;;@ src.cpp:40:1
(local.get $y)
)
;;@ src.cpp:50:1
(return)
)
;;@ src.cpp:60:1
(call $foo
;;@ src.cpp:70:1
(local.get $x)
;;@ src.cpp:80:1
(local.get $y)
)
)
)
;; CHECK: (module
;; CHECK-NEXT: (func $0 (param $0 i32) (param $1 i32)
;; CHECK-NEXT:
;; CHECK-NEXT: ;;@ src.cpp:40:1
;; CHECK-NEXT: (if
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: ;;@ src.cpp:30:1
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: ;;@ src.cpp:40:1
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
;; CHECK-NEXT: ;;@ src.cpp:50:1
;; CHECK-NEXT: (return)
;; CHECK-NEXT: )
;; CHECK-NEXT: ;;@ src.cpp:80:1
;; CHECK-NEXT: (call $0
;; CHECK-NEXT: ;;@ src.cpp:70:1
;; CHECK-NEXT: (local.get $0)
;; CHECK-NEXT: ;;@ src.cpp:80:1
;; CHECK-NEXT: (local.get $1)
;; CHECK-NEXT: )
Something does indeed look wrong with the call. But also with the add... so I'm not sure what's going on.
src/wasm-stack.h
Outdated
emitDebugLocation(curr); | ||
if (!curr->is<Call>()) { | ||
emitDebugLocation(curr); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add a comment explaining why Call
is special here. That's not obvious to me, sorry if I didn't understand something you already explained in the issue.
In particular, why is Call
different from, say, Binary
? And what about CallIndirect
and CallRef
?
yeah, it seems affact all the misordered instructions, like tree IR:
binary parser:
it is better to combine the debug information generation with binary generation, ie |
99edfa6
to
8f04946
Compare
@kripken source map decode
wasm decode
according to source map position, the latest postion of wasm should be: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nice! This looks much better. I also see the results on the lit
testcase from my comment look better with this patch applied (still not perfect, but that can be left for later, after this PR).
I think it would be good to get that lit test into the test suite. Here is an updated version with your patch:
;; RUN: wasm-opt %s -o %t.wasm -osm %t.map -g -q
;; RUN: wasm-opt %t.wasm -ism %t.map -q -o - -S | filecheck %s
(module
(func $foo (param $x i32) (param $y i32)
;;@ src.cpp:10:1
(if
;;@ src.cpp:20:1
(i32.add
;;@ src.cpp:30:1
(local.get $x)
;;@ src.cpp:40:1
(local.get $y)
)
;;@ src.cpp:50:1
(return)
)
;;@ src.cpp:60:1
(call $foo
;;@ src.cpp:70:1
(local.get $x)
;;@ src.cpp:80:1
(local.get $y)
)
)
)
;; CHECK: (func $foo (param $x i32) (param $y i32)
;; CHECK-NEXT: ;;@ src.cpp:20:1
;; CHECK-NEXT: (if
;; CHECK-NEXT: (i32.add
;; CHECK-NEXT: ;;@ src.cpp:30:1
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: ;;@ src.cpp:40:1
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )
;; CHECK-NEXT: ;;@ src.cpp:50:1
;; CHECK-NEXT: (return)
;; CHECK-NEXT: )
;; CHECK-NEXT: ;;@ src.cpp:60:1
;; CHECK-NEXT: (call $foo
;; CHECK-NEXT: ;;@ src.cpp:70:1
;; CHECK-NEXT: (local.get $x)
;; CHECK-NEXT: ;;@ src.cpp:80:1
;; CHECK-NEXT: (local.get $y)
;; CHECK-NEXT: )
Please add it in tests/lit/source-map.wast
.
src/wasm-binary.h
Outdated
@@ -1430,7 +1430,7 @@ class WasmBinaryBuilder { | |||
MixedArena& allocator; | |||
const std::vector<char>& input; | |||
std::istream* sourceMap; | |||
std::pair<uint32_t, Function::DebugLocation> nextDebugLocation; | |||
std::tuple<uint32_t, uint32_t, Function::DebugLocation> nextDebugLocation; // available pos, previous pos, next debug location |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it would be good to use a struct here. Something like
struct NextDebugLocation {
uint32_t available;
uint32_t previous;
uint32_t next;
};
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Alon,
I updated the fix according to your comments, and for the next step (add lit
test to main test suite), I donot know how to begin the work, can you give some advice? thank you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For the test, just create a new file at test/lit/source-map.wast
, paste the test contents from here into that file, and add it using git add
, then commit it. It should just work. (edit: test
not tests
)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The lit test just passed as well:
/binaryen$ python build/bin/binaryen-lit -vv test/lit/source-map.wast
-- Testing: 1 tests, 1 workers --
PASS: Binaryen lit tests :: source-map.wast (1 of 1)
Testing Time: 0.06s
Passed: 1
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi Alon,
the test has been added, please kindly review, thank you.
I verified that this patch passes the emscripten source map tests as well. |
9f6268c
to
191b42b
Compare
Thanks! I'm still reading the code, but to fix the CI error please apply the style diff from here: https://github.com/WebAssembly/binaryen/actions/runs/4249525050/jobs/7400946814 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lgtm aside from the code style that needs to be fixed.
191b42b
to
d649bee
Compare
fixed it now:) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great, thanks!
With this, the sourcemap testcase outputs the exact same thing as the input. Followup to #5504
The stack logic was incorrect, and led to source locations being emitted on parents instead of children.
…5524) With this, the sourcemap testcase outputs the exact same thing as the input. Followup to WebAssembly#5504
fix: 5503