-
Notifications
You must be signed in to change notification settings - Fork 15.4k
[WebAssembly] Fold TargetGlobalAddress with added offset #145829
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
Merged
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
fe369c1
Precommit test for folding a load of unsigned offset
badumbatish 3dfe71a
[WebAssembly] Fold nuw global add to load offset
badumbatish 0a00fda
[WebAssembly] Fix nit from PR 145829
91b42d5
[WebAssembly] Add guard for global folding
65f68c3
[WebAssembly] Fix nits for PR 145829
3e7f049
Apply suggestions from nit
badumbatish d501edb
Merge branch 'main' of https://github.com/llvm/llvm-project into fold…
6418cf6
Fix whitespace
lukel97 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
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
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
Oops, something went wrong.
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.
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.
It's interesting that this transformation eagerly swaps these 2 constants, even though it's not actually an optimization. I'm a little bit ambivalent about this because the original actually looks more natural to me (i.e. the operand contains the actual address, and the offset field is actually an offset). OTOH adding more complexity to the transform to prevent this doesn't seem great either, considering that it's not really any worse.
It does suggest though that we might be able to do a further optimization of actually combining these constants in the compiler? In an isolated case like this we couldn't actually remove the i32.const entirely (since there still needs to be an operand), and the constant offset field is always there, so it would potentially be of no benefit. But if we combine them into the offset and leave behind an
i32.const 0, it's plausible that some subsequent optimization could remove it where appropriate. And this is a point where we have the information that the add isnuwso it's legal to do, whereas some subsequent pass (or post-link optimizer like Binaryen) might not have it. So it still seems worthwhile.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 suppose if we want to be really galaxy-brained, it's possible to have a situation where splitting an LEB-encoded value into 2 can actually reduce the overall size because of the way the encoding works; e.g. if one value is 128 and the other is 0, it takes 3 bytes to encode the first and one for the second, but if we split them into 2 64s, they would encode in 1 byte each!
... still, that seems like a bit of a stretch and maybe not as good as just leaving the 0 in the const field.
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.
oh wait... actually we can't do this when one of the operands is relocatable anyway. So it doesn't really apply for this PR. And for cases where they are just regular constants, it seems likely they'd have already been combined earlier in the compiler pipeline anyway. So, yeah there's probably nothing there, other than my original observation about eagerly swapping the operands when there's no actual add instruction to eliminate.
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.
FWIW I had the same thought too. I guess we could just swap around the offset and addrs and see if that maintains the original order in this test? But it's probably dependent on the order of the operands in the add node.
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 tried swapping Addr and Offset in WebAssemblyISelDAGToDAG and it seems it generates bad machine code on several test case, for example: cfg-stackify-eh.ll
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'll push the changes to current nits (excluding swapping) but lmk if we'd like to pursue this avenue