Skip to content
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

[BOLT] Fix NOP instruction emission on x86 #72186

Merged
merged 1 commit into from
Nov 14, 2023
Merged

Conversation

maksfb
Copy link
Contributor

@maksfb maksfb commented Nov 14, 2023

Use MCAsmBackend::writeNopData() interface to emit NOP instructions on x86. There are multiple forms of NOP instruction on x86 with different sizes. Currently, LLVM's assembly/disassembly does not support all forms correctly which can lead to a breakage of input code semantics, e.g. if the program relies on NOP instructions for reserving a patch space.

Add "--keep-nops" option to preserve NOP instructions.

Use MCAsmBackend::writeNopData() interface to emit NOP instructions on
x86. There are multiple forms of NOP instruction on x86 with different
sizes. Currently, LLVM's assembly/disassembly does not support all forms
correctly which can lead to a breakage of input code semantics, e.g. if
the program relies on NOP instructions for reserving a patch space.

Add "--keep-nops" option to preserve NOP instructions.
Copy link
Contributor

@aaupov aaupov left a comment

Choose a reason for hiding this comment

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

LGTM. What's the context of this change, is it Linux kernel-related?

@maksfb
Copy link
Contributor Author

maksfb commented Nov 14, 2023

Yes, it's needed for the Linux kernel support, but should be applicable to other use cases as well.

@maksfb
Copy link
Contributor Author

maksfb commented Nov 14, 2023

Note that with this change, NOP instruction encoding could be different. E.g., 4-byte NOP is now encoded as:

0f 1f 40 00

compared to:

0f 1f 04 00

previously.

This is also required to match Linux kernel expectations for NOP encoding.

@maksfb maksfb merged commit f633f32 into llvm:main Nov 14, 2023
4 checks passed
@@ -4366,6 +4367,10 @@ MCInst *BinaryFunction::getInstructionAtOffset(uint64_t Offset) {
}
}

bool BinaryFunction::shouldPreserveNops() const {
return PreserveNops || opts::KeepNops;
Copy link
Member

Choose a reason for hiding this comment

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

Small nit: Maybe add option check to RemoveNops::runOnFunctions too/instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What's the rationale behind that extra check?

Copy link
Member

Choose a reason for hiding this comment

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

Not to loop though the functions in pass. It is minor thing

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Ah - we can skip the pass all over: #72228.

@maksfb maksfb deleted the gh-keep-nop branch November 15, 2023 20:28
zahiraam pushed a commit to zahiraam/llvm-project that referenced this pull request Nov 20, 2023
Use MCAsmBackend::writeNopData() interface to emit NOP instructions on
x86. There are multiple forms of NOP instruction on x86 with different
sizes. Currently, LLVM's assembly/disassembly does not support all forms
correctly which can lead to a breakage of input code semantics, e.g. if
the program relies on NOP instructions for reserving a patch space.

Add "--keep-nops" option to preserve NOP instructions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants