-
Notifications
You must be signed in to change notification settings - Fork 13.6k
[CodeGen][X86] Improving robustness of PATCHABLE_OP wrapper instruction #59039
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
Comments
@llvm/issue-subscribers-backend-x86 |
Turns out that this is not only windows related. This happens for all X86 platforms when |
Ping @aganea |
Ping! |
Maybe @phoebewang or @RKSimon can help out here since it's very much a complicated X86 backend issue. |
I'm not familiar with hotpatch. Is the patch https://reviews.llvm.org/D137642 will solve the problem here too? |
That patch fixes the selection of the function's instruction to make patchable (guarantee it's at least 2 bytes long or is preceded by a 2-byte nop). |
This patch fixes crashes related with how PatchableFunction selects the instruction to make patchable: - Ensure PatchableFunction skips all instructions that don't generate actual machine instructions. - Handle the case where the first MachineBasicBlock is empty - Removed support for 16 bit x86 architectures. Note: another issue remains related with PatchableFunction, in the lowering part. See #59039 Differential Revision: https://reviews.llvm.org/D137642
Ping! I'm currently stuck, none of my attempts seem sustainable. Note that MSVC seems to do this differently, as in MSVC blog: "most of the time, the compiler can juggle things so that you don’t even notice that it arranged for the first instruction of a function to be a multi-byte instruction. ". But it seems that doing that in clang could be pretty involved. |
cc @KanRobert |
Ping on this - anyone know if someone that can help us progress on this? |
Pinging this once again since it was linked into an issue with tailcall optimizations today (#76958). It would be great if somebody could look into this, since using Since most developers/companies probably don't use the I'd therefore like to propose the following compromise as a fix for this issue:
This would only add an unnecessary 2-byte NOP for functions which are only one byte in size, which should be very, very few. However, I'm not familiar with code generation and intermediate passes in LLVM, so the proposed solution might not make sense in the context of LLVM. |
Also see: #76879 |
I've posted a fix in #77245 |
…77245) Previously, tail jump pseudo-opcodes were skipped by the `encodeInstruction()` call inside `X86AsmPrinter::LowerPATCHABLE_OP`. This caused emission of a 2-byte NOP and dropping of the tail jump. With this PR, we change `PATCHABLE_OP` to not wrap the first `MachineInstr` anymore, but inserting itself before, leaving the instruction unaltered. At lowering time in `X86AsmPrinter`, we now "look ahead" for the next non-pseudo `MachineInstr` and lower+encode it, to inspect its size. If the size is below what `PATCHABLE_OP` expects, it inserts NOPs; otherwise it does nothing. That way, now the first `MachineInstr` is always lowered as usual even if `"patchable-function"="prologue-short-redirect"` is used. Fixes #76879, #76958 and #59039
When activating
-fms-hotpatch
in clang, the"patchable-function"
pass replaces the first machine instruction with a wrapperPATCHABLE_OP
.This wrapping loses some information about the wrapped instruction: when a
PATCHABLE_OP
instruction is handled byX86AsmPrinter::emitInstruction
, the wrapped instruction simply gets lowered without going throughX86AsmPrinter::emitInstruction
itself.Here is an example in C/C++, where a tail call doesn't get lowered properly: https://godbolt.org/z/1Pjcbx87n
The source of the issue seems to be the loss of information in
PatchableFunction::runOnMachineFunction
when replacing aMachineInstr
with thePATCHABLE_OP
one: It only keeps the OpCode and operands of the wrapped instruction, andX86AsmPrinter::emitInstruction
can't be called from it.We are looking for a clean way to achieve this. Any suggestions?
The text was updated successfully, but these errors were encountered: