Add MergeDataSegments pass#8647
Draft
LegionMammal978 wants to merge 4 commits intoWebAssembly:mainfrom
Draft
Conversation
4a120d6 to
64952b5
Compare
Contributor
|
It looks like all of this was created using an LLM? You should add tests to demonstrate how this data segments merges + check edge cases using lit tests (test/lit/passes/ |
Author
|
No, I wrote everything in this PR myself, I'm not a big fan of LLMs' coding style. (Indeed, I asked an LLM to review my merge functions, and it kept wanting to add all sorts of extraneous steps.) I think I see how to write the lit tests (create the file with the inputs, run |
Contributor
run from root dir: ./scripts/fuzz_opt.py |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Recently, I was writing a WASM module by hand, and I used a number of individual small data segments to store strings. I tried seeing if wasm-opt could combine these adjacent data segments, but found that it did not have any such pass. Thus, I've implemented a new MergeDataSegments pass to merge active data segments that are overlapping, adjacent, or near-adjacent, in order to save the space of storing multiple data-segment headers, and the time of processing them during instantiation.
It is designed to be as aggressive as possible, fully supporting multiple memories and accounting for non-constant-offset segments. Meanwhile, unless TNH is enabled, it is also designed to carefully replicate the original module's behavior w.r.t. out-of-bounds traps during instantiation: the goal is that there should be no observable difference in the output, short of unreliable tricks like reading a partially-instantiated
SharedArrayBuffer.In principle, this functionality might have been included in the existing MemoryPacking pass, but I believe that it makes sense to separate the primary functionalities of splitting vs. merging data segments, which require different forms of tracking. In that sense, I see MergeDataSegments as complementing the MemoryPacking pass. For instance, MemoryPacking requires that its input has no overlapping data segments, a property that MergeDataSegments is often able to ensure in its output.
Some implementation notes:
--ignore-implicit-traps, following MemoryPacking.unreachable, etc., but this is a small edge case.ReFinalize()is not needed, since the instructions and stack arguments are unchanged, only their indices are modified. Similarly, I markrequiresNonNullableLocalFixups()asfalse. In principle, the behavior of the memory instructions on active data segments can be simplified, but I figure that it's better to leave that to the more extensive modifications of MemoryPacking.thresholdof MemoryPacking does not always match the size heuristic of MergeDataSegments, so if run in alternation in certain cases, they could fight over splitting vs. merging the same two segments.test/passes/?test/lit/passes/?), nor how exactly they are formatted. I'm especially unsure how to test behavior around theMAX_SEG_SIZE.