Repository navigation
[AsmPrinter] Add generic support for verifying instruction sizes #187703
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 1 commit
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Some comments aren't visible on the classic Files Changed page.
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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -2147,6 +2147,11 @@ void AsmPrinter::emitFunctionBody() { | |||||
| if (isVerbose()) | ||||||
| emitComments(MI, STI, OutStreamer->getCommentOS()); | ||||||
|
|
||||||
| #ifndef NDEBUG | ||||||
| MCFragment *OldFragment = OutStreamer->getCurrentFragment(); | ||||||
| size_t OldFragSize = OldFragment->getFixedSize(); | ||||||
| #endif | ||||||
|
|
||||||
| switch (MI.getOpcode()) { | ||||||
| case TargetOpcode::CFI_INSTRUCTION: | ||||||
| emitCFIInstruction(MI); | ||||||
|
|
@@ -2265,6 +2270,29 @@ void AsmPrinter::emitFunctionBody() { | |||||
| break; | ||||||
| } | ||||||
|
|
||||||
| #ifndef NDEBUG | ||||||
| // Verify that the instruction size reported by InstrInfo matches the | ||||||
| // actually emitted size. Many backends performing branch relaxation | ||||||
| // on the MIR level rely on this for correctness. | ||||||
| if (OutStreamer->isObj()) { | ||||||
| const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); | ||||||
| MCFragment *NewFragment = OutStreamer->getCurrentFragment(); | ||||||
| // Don't try to handle fragment splitting cases. | ||||||
| if (NewFragment == OldFragment && TII->shouldVerifyInstSize(MI)) { | ||||||
| unsigned ExpectedSize = TII->getInstSizeInBytes(MI); | ||||||
| unsigned ActualSize = NewFragment->getFixedSize() - OldFragSize; | ||||||
| // FIXME: InstrInfo currently over-estimates the size of STACKMAP. | ||||||
| if (ActualSize != ExpectedSize && | ||||||
| MI.getOpcode() != TargetOpcode::STACKMAP) { | ||||||
| dbgs() << "Size mismatch for: " << MI << "\n"; | ||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Consider also print the section name , sth like
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
This already includes the newline |
||||||
| dbgs() << "Expected size: " << ExpectedSize << "\n"; | ||||||
| dbgs() << "Actual size: " << ActualSize << "\n"; | ||||||
| abort(); | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
| #endif | ||||||
|
|
||||||
| if (MI.isCall()) { | ||||||
| if (MF->getTarget().Options.BBAddrMap) | ||||||
| OutStreamer->emitLabel(createCallsiteEndSymbol(MBB)); | ||||||
|
|
||||||
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.
getInstSizeInBytes should always return a conservative overestimate. It cannot be expected to give an exact match in all cases
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.
That's at least true with pseudos. It's fuzzier at this point in the emission. For AMDGPU have some cases where we should be able to reduce the instruction size after relaxation determines the exact constants, we just happen to not perform that optimization today
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.
True, but for the purposes of this verification, I do think being able to check the exact size is quite useful. After all, for most instructions (even most pseudos) you can determine the exact size, and it's useful to check that.
I've updated the hook to return a InstSizeVerifyMode instead that allows picking between NoVerify, ExactSize and AllowOverEstimate. In the future, I'd like to enable AllowOverEstimate by default, but a backend can use ExactSize to get stricter validation (e.g. for PPC it holds for everything apart from stackmap).