Skip to content
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

Two fixes for the -g (debug info) option #6931

Merged
merged 7 commits into from
Sep 1, 2023

Conversation

SingleAccretion
Copy link
Contributor

Two changes:

  1. Fix a small bug where a constant DWARF "block" wasn't handled in transformation.
  2. Fix up instruction offsets after code emission to account for branch removal.

See also the commit messages for a bit more detail.

With these two changes, I am able to step through the code generated by the compiler we are working on.

Fixes #3999.

"Block"s in DWARF are arbitrary-sized constants.

They are used for e. g. __int128 constants in C/C++, which is how this was hit.
This is generated by VS when using its "open folder feature".
Comment on lines 1101 to 1102
let value_labels_ranges =
self.compute_value_labels_ranges(regalloc, &inst_offsets[..], func_body_len);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is interesting to note that optimize_branches is invoked after this code one last time and can nominally modify the current buffer pointer, make func_body_len stale, etc. At the same time, for it to remove a branch, that branch needs to point past the end of the function, which doesn't seem possible?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm -- it's theoretically possible, if it were a block that had just a jump instruction, no fallthrough (previous block ended in an uncond branch), and we rewrote branches to this last block to go directly to the jump dest instead.

Perhaps you could pull out the result of buffer.finish(...) (which is where the last optimization call occurs) to before this line?

@SingleAccretion SingleAccretion changed the title Two fixes for the -g (DWARF debug info) option Two fixes for the -g (debug info) option Aug 30, 2023
Turns out that this is easier then fixing them in-place when
removing the branch because a bunch of ambient state needs to
be passed through to the MachBuffer for that to become possible.

Testing: tested to fix the issue on both one of the reported
samples as well as my own.
@SingleAccretion SingleAccretion marked this pull request as ready for review August 30, 2023 12:57
@SingleAccretion SingleAccretion requested review from a team as code owners August 30, 2023 12:57
@SingleAccretion SingleAccretion requested review from fitzgen and removed request for a team August 30, 2023 12:57
@github-actions github-actions bot added cranelift Issues related to the Cranelift code generator cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. labels Aug 30, 2023
Copy link
Member

@fitzgen fitzgen left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me!

Would it be possible to construct a somewhat minimal test case where the branch lowering messes up the offsets and we need to monotize them and add it to our debugging tests? Eg https://github.com/bytecodealliance/wasmtime/blob/main/tests/all/debug/lldb.rs#L62

cranelift/codegen/src/machinst/vcode.rs Outdated Show resolved Hide resolved
Comment on lines 1101 to 1102
let value_labels_ranges =
self.compute_value_labels_ranges(regalloc, &inst_offsets[..], func_body_len);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm -- it's theoretically possible, if it were a block that had just a jump instruction, no fallthrough (previous block ended in an uncond branch), and we rewrote branches to this last block to go directly to the jump dest instead.

Perhaps you could pull out the result of buffer.finish(...) (which is where the last optimization call occurs) to before this line?

@SingleAccretion
Copy link
Contributor Author

Would it be possible to construct a somewhat minimal test case where the branch lowering messes up the offsets and we need to monotize them and add it to our debugging tests?

Should definitely be possible; will do.

It is more obviously correct this way that the code which
is using buffer.cur_offset() is not reading stale values.
Also delete EmitResult::inst_offsets, it was not used.
If either is unknown, err on the safe side.
Copy link
Member

@cfallin cfallin left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Latest changes LGTM to me -- thanks very much for this!

@cfallin cfallin added this pull request to the merge queue Sep 1, 2023
Merged via the queue into bytecodealliance:main with commit d4a4d4f Sep 1, 2023
18 checks passed
eduardomourar pushed a commit to eduardomourar/wasmtime that referenced this pull request Sep 6, 2023
* Fix up a debug info transform bug

"Block"s in DWARF are arbitrary-sized constants.

They are used for e. g. __int128 constants in C/C++, which is how this was hit.

* Add the ".vs" folder to ".gitignore"

This is generated by VS when using its "open folder feature".

* Monotonize instructon offsets after code emission

Turns out that this is easier then fixing them in-place when
removing the branch because a bunch of ambient state needs to
be passed through to the MachBuffer for that to become possible.

Testing: tested to fix the issue on both one of the reported
samples as well as my own.

* Move the last optimize_branches earlier

It is more obviously correct this way that the code which
is using buffer.cur_offset() is not reading stale values.

* Fix the zero-offset problem with NO_INST_OFFSET

Also delete EmitResult::inst_offsets, it was not used.

* Add a test

* Check both sides of the range for NO_INST_OFFSET

If either is unknown, err on the safe side.
@SingleAccretion SingleAccretion deleted the DI-Fixes branch October 15, 2024 21:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
cranelift:area:machinst Issues related to instruction selection and the new MachInst backend. cranelift Issues related to the Cranelift code generator
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Assertion in debugger mode (left < right)
3 participants