-
Notifications
You must be signed in to change notification settings - Fork 15.3k
[BOLT][BTI] Add MCPlusBuilder::addBTItoBBStart #167329
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
Open
bgergely0
wants to merge
1
commit into
main
Choose a base branch
from
users/bgergely0/bolt-add-bti-to-bb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2808,6 +2808,81 @@ class AArch64MCPlusBuilder : public MCPlusBuilder { | |
| Inst.addOperand(MCOperand::createImm(HintNum)); | ||
| } | ||
|
|
||
| bool isBTIVariantCoveringCall(MCInst &Call, MCInst &Pad) const override { | ||
| assert((isIndirectCall(Call) || isIndirectBranch(Call)) && | ||
| "Not an indirect call or branch."); | ||
|
|
||
| // A BLR can be accepted by a BTI c. | ||
| if (isIndirectCall(Call)) | ||
| return isBTILandingPad(Pad, true, false) || | ||
| isBTILandingPad(Pad, true, true); | ||
|
|
||
| // A BR can be accepted by a BTI j or BTI c (and BTI jc) IF the operand is | ||
| // x16 or x17. If the operand is not x16 or x17, it can be accepted by a BTI | ||
| // j or BTI jc (and not BTI c). | ||
| if (isIndirectBranch(Call)) { | ||
| assert(Call.getNumOperands() == 1 && | ||
| "Indirect branch needs to have 1 operand."); | ||
| assert(Call.getOperand(0).isReg() && | ||
| "Indirect branch does not have a register operand."); | ||
| MCPhysReg Reg = Call.getOperand(0).getReg(); | ||
| if (Reg == AArch64::X16 || Reg == AArch64::X17) | ||
| return isBTILandingPad(Pad, true, false) || | ||
| isBTILandingPad(Pad, false, true) || | ||
| isBTILandingPad(Pad, true, true); | ||
| return isBTILandingPad(Pad, false, true) || | ||
| isBTILandingPad(Pad, true, true); | ||
| } | ||
| return false; | ||
| } | ||
|
|
||
| void addBTItoBBStart(BinaryBasicBlock &BB, MCInst &Call) const override { | ||
| auto II = BB.getFirstNonPseudo(); | ||
| if (II != BB.end()) { | ||
|
Contributor
Author
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. I think I have previously encountered empty BasicBlocks - that was around function splitting I believe. Not sure if empty BasicBlocks should be covered here. Currently, we silently skip them - that does not seem ideal. |
||
| if (isBTIVariantCoveringCall(Call, *II)) | ||
| return; | ||
| // A BLR can be accepted by a BTI c. | ||
| if (isIndirectCall(Call)) { | ||
| // if we have a BTI j at the start, extend it to a BTI jc, | ||
| // otherwise insert a new BTI c. | ||
| if (isBTILandingPad(*II, false, true)) { | ||
| updateBTIVariant(*II, true, true); | ||
| } else { | ||
| MCInst BTIInst; | ||
| createBTI(BTIInst, true, false); | ||
| BB.insertInstruction(II, BTIInst); | ||
| } | ||
| } | ||
|
|
||
| // A BR can be accepted by a BTI j or BTI c (and BTI jc) IF the operand is | ||
| // x16 or x17. If the operand is not x16 or x17, it can be accepted by a | ||
| // BTI j or BTI jc (and not BTI c). | ||
| if (isIndirectBranch(Call)) { | ||
| assert(Call.getNumOperands() == 1 && | ||
| "Indirect branch needs to have 1 operand."); | ||
| assert(Call.getOperand(0).isReg() && | ||
| "Indirect branch does not have a register operand."); | ||
| MCPhysReg Reg = Call.getOperand(0).getReg(); | ||
| if (Reg == AArch64::X16 || Reg == AArch64::X17) { | ||
| // Add a new BTI c | ||
| MCInst BTIInst; | ||
| createBTI(BTIInst, true, false); | ||
| BB.insertInstruction(II, BTIInst); | ||
| } else { | ||
| // If BB starts with a BTI c, extend it to BTI jc, | ||
| // otherwise insert a new BTI j. | ||
| if (isBTILandingPad(*II, true, false)) { | ||
| updateBTIVariant(*II, true, true); | ||
| } else { | ||
| MCInst BTIInst; | ||
| createBTI(BTIInst, false, true); | ||
| BB.insertInstruction(II, BTIInst); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| InstructionListType materializeAddress(const MCSymbol *Target, MCContext *Ctx, | ||
| MCPhysReg RegName, | ||
| int64_t Addend = 0) const override { | ||
|
|
||
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.
Nit: should it be addBTIToBBStart?
If we're entertaining a rename, how about
insertrather than add - since normally I think of putting something at the beginning of a list as 'insertion' rather than 'addition'.insertBTIwould be a clear verb for a function name (and 'ToBBStart would be a bit redundant since presumably we won't expect to insert a BTI anywhere else?)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.
insert is probably better.
agree.
The naming should also imply that we don't necessarily insert a new instruction, only if the instruction already at the BBStart is not a BTI (or not compatible with a BTI).
WDYT?
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.
ensureBTITarget?
makeBTITarget?
On the other hand
insertBTII think should be reasonably clear. If it begins withif (alreadyBTITarget) return;that shouldn't be too surprising to a developer given the purpose of BTI. At least I can't think of a good reason to need to double them up.