Skip to content

Binary format code section offset tracking#2515

Merged
kripken merged 14 commits intomasterfrom
binloc
Dec 19, 2019
Merged

Binary format code section offset tracking#2515
kripken merged 14 commits intomasterfrom
binloc

Conversation

@kripken
Copy link
Copy Markdown
Member

@kripken kripken commented Dec 7, 2019

Optionally track the binary format code section offsets,
that is, when loading a binary, remember where each IR
node was read from. This is necessary for DWARF
debug info, as these are the offsets DWARF refers to.

(Note that eventually we may want to do something
else, like first read the DWARF and only then add
debug info annotations into the IR in a more LLVM-like
manner, but this is more straightforward and should be
enough to update debug lines and ranges).

This tracking adds noticeable overhead - every single
IR node adds an entry in a map - so avoid it unless
actually necessary. Specifically, if the user passes in
-g and there are actually DWARF sections in the
binary, and we are not about to remove those sections,
then we need it.

Print binary format code section offsets in text, when
printing with -g. This will help debug and test dwarf
support. It looks like

;; code offset: 0x7

as an annotation right before each node.

Also add support for -g in wasm-opt tests (unlike
a pass, it has just one - as a prefix).

Helps #2400

@kripken kripken requested a review from dschuff December 7, 2019 21:12
@dschuff
Copy link
Copy Markdown
Member

dschuff commented Dec 17, 2019

How is tracking the input location/offset of each IR node any different than tracking the input source position? why does it need to be done differently?

@kripken
Copy link
Copy Markdown
Member Author

kripken commented Dec 17, 2019

By input source position do you mean the source maps tracking? That tracks expression => file, line, col. This tracking is for expression => original binary location.

If there is also a tracking of original binary location => file, line, col then the two become related, however, we need the general mapping of expression => original binary location in order to handle things that are not file, line, col (like stuff with locals, etc.).

Long-term, I think we should remove the source maps tracking logic, once DWARF is stable. We can emit a source map using DWARF at the end of the pipeline if that is still useful.

@dschuff
Copy link
Copy Markdown
Member

dschuff commented Dec 17, 2019

Right, but i mean to track expression -> file/line/col we added a field to every expression to contain that info (distributing it across the whole expression tree), rather than having a single map object on the module to keep all the information together. I'm just wondering why this is different. I agree that it makes sense to eventually remove the file/line/col information.

@dschuff
Copy link
Copy Markdown
Member

dschuff commented Dec 17, 2019

Wait nevermind, I just looked at the code and I'm completely wrong about that. I could have sworn we had done it that way. So I guess the question is, why not just have the info on each node to avoid the map insertion cost? For source maps, the information is stored separately so we want the O(1) lookup but since this information is available at node creation time I think we can store it on the node instead.

Comment thread src/wasm/wasm-binary.cpp
currFunction->debugLocations[curr] = *currDebugLocation.begin();
}
if (DWARF && currFunction) {
currFunction->binaryLocations[curr] = startPos - codeSectionLocation;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

so concretely, could this be curr->binaryLocation = startPos - codeSectionLocation?

Comment thread src/passes/Print.cpp
}
// show a binary position, if there is one
if (debugInfo) {
auto iter = currFunction->binaryLocations.find(curr);
Copy link
Copy Markdown
Member

@dschuff dschuff Dec 17, 2019

Choose a reason for hiding this comment

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

and this could be something like o << "code offset " << curr->binaryLocation

@kripken
Copy link
Copy Markdown
Member Author

kripken commented Dec 17, 2019

why not just have the info on each node

Efficiency in the non-debug case, so the nodes are as small as possible.

@kripken
Copy link
Copy Markdown
Member Author

kripken commented Dec 17, 2019

Also for the debug case, many nodes may not have any debug info, so we save extra size on them too.

@dschuff
Copy link
Copy Markdown
Member

dschuff commented Dec 18, 2019

That's a good point; although since the offset is just a 32-bit number, and the cost of a map entry is at least the pointer-sized key plus the value (plus any overhead; bucket chain?), there would have to be info for fewer than 1/3 of the nodes for that to be an improvement.

Comment thread src/wasm-binary.h
Comment thread src/wasm/wasm-binary.cpp Outdated
@dschuff
Copy link
Copy Markdown
Member

dschuff commented Dec 18, 2019

(by the way I don't have a strong opinion about the map vs extra node space, we could go either way on that unless the cost starts getting noticeable).

@kripken
Copy link
Copy Markdown
Member Author

kripken commented Dec 18, 2019

Yeah, size overhead might be a tradeoff. I lean towards a separate map, because it keeps the non-debug build faster and leaner, and that's important for iteration speed, and there's a chance it'll even be faster for debug builds (as you said, 1/3 or so may be the cutoff point, and at least in an opt build with debug info I'd bet its even fewer).

@dschuff
Copy link
Copy Markdown
Member

dschuff commented Dec 18, 2019

I think in debug builds it will always be a little slower to use a map. In the code here we are doing a map lookup in several places where we could just be loading from a structure (one that's likely already hot in the cache). But you are right about the non-debug builds. Anyway this is fine for now, it wouldn't be a big deal to change yet if we wanted to later.

kripken added a commit that referenced this pull request Dec 19, 2019
This imports LLVM code for DWARF handling. That code has the
Apache 2 license like us. It's also the same code used to
emit DWARF in the common toolchain, so it seems like a safe choice.

This adds two passes: --dwarfdump which runs the same code LLVM
runs for llvm-dwarfdump. This shows we can parse it ok, and will
be useful for debugging. And --dwarfupdate writes out the DWARF
sections (unchanged from what we read, so it just roundtrips - for
updating we need #2515).

This puts LLVM in thirdparty which is added here.

All the LLVM code is behind USE_LLVM_DWARF, which is on
by default, but off in JS for now, as it increases code size by 20%.

This current approach imports the LLVM files directly. This is not
how they are intended to be used, so it required a bunch of
local changes - more than I expected actually, for the platform-specific
stuff. For now this seems to work, so it may be good enough, but
in the long term we may want to switch to linking against libllvm.
A downside to doing that is that binaryen users would need to
have an LLVM build, and even in the waterfall builds we'd have a
problem - while we ship LLVM there anyhow, we constantly update
it, which means that binaryen would need to be on latest llvm all
the time too (which otherwise, given DWARF is quite stable, we
might not need to constantly update).

An even larger issue is that as I did this work I learned about how
DWARF works in LLVM, and while the reading code is easy to
reuse, the writing code is trickier. The main code path is heavily
integrated with the MC layer, which we don't have - we might want
to create a "fake MC layer" for that, but it sounds hard. Instead,
there is the YAML path which is used mostly for testing, and which
can convert DWARF to and from YAML and from binary. Using
the non-YAML parts there, we can convert binary DWARF to
the YAML layer's nice Info data, then convert that to binary. This
works, however, this is not the path LLVM uses normally, and it
supports only some basic DWARF sections - I had to add ranges
support, in fact. So if we need more complex things, we may end
up needing to use the MC layer approach, or consider some other
DWARF library. However, hopefully that should not affect the core
binaryen code which just calls a library for DWARF stuff.

Helps #2400
Comment thread src/wasm/wasm-binary.cpp
getInt32(); // magic
getInt32(); // version
bool has = false;
while (more()) {
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

It seems kind of crazy that we don't already have some kind of general section-iteration mechanism. But I guess that's not the kind of thing Binaryen is usually used for...
This is ok for now (assuming it doesn't duplicate something we do have elsewehere) but let's try not to write this code again in the future.

Comment thread src/wasm.h

std::vector<UserSection> userSections;

// Source maps debug info.
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Maybe a separate change but we should probably rename these fields to make it more clear.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I hope to remove the source maps code in the not far future, which would remove any confusion between these fields. (Once dwarf line numbers works, we can use that for source maps.)

@kripken kripken merged commit 05d785e into master Dec 19, 2019
@kripken kripken deleted the binloc branch December 19, 2019 23:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants