Skip to content

Commit ec1af63

Browse files
authored
[Codegen][X86] Fix /HOTPATCH with clang-cl and inline asm (#87639)
This fixes an edge case where functions starting with inline assembly would assert while trying to lower that inline asm instruction. After this PR, for now we always add a no-op (xchgw in this case) without considering the size of the next inline asm instruction. We might want to revisit this in the future. This fixes Unreal Engine 5.3.2 compilation with clang-cl and /HOTPATCH. Should close #56234
1 parent ccdebba commit ec1af63

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

llvm/lib/Target/X86/X86MCInstLower.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -980,8 +980,10 @@ void X86AsmPrinter::LowerPATCHABLE_OP(const MachineInstr &MI,
980980
SmallString<256> Code;
981981
unsigned MinSize = MI.getOperand(0).getImm();
982982

983-
if (NextMI != MI.getParent()->end()) {
983+
if (NextMI != MI.getParent()->end() && !NextMI->isInlineAsm()) {
984984
// Lower the next MachineInstr to find its byte size.
985+
// If the next instruction is inline assembly, we skip lowering it for now,
986+
// and assume we should always generate NOPs.
985987
MCInst MCI;
986988
MCIL.Lower(&*NextMI, MCI);
987989

llvm/test/CodeGen/X86/patchable-prologue.ll

+17
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,20 @@ do.body: ; preds = %do.body, %entry
193193
do.end: ; preds = %do.body
194194
ret void
195195
}
196+
197+
198+
; Test that inline asm is properly hotpatched. We currently don't examine the
199+
; asm instruction when printing it, thus we always emit patching NOPs.
200+
201+
; 64: inline_asm:
202+
; 64-NEXT: # %bb.0:
203+
; 64-NEXT: xchgw %ax, %ax # encoding: [0x66,0x90]
204+
; 64-NEXT: #APP
205+
; 64-NEXT: int3 # encoding: [0xcc]
206+
; 64-NEXT: #NO_APP
207+
208+
define dso_local void @inline_asm() "patchable-function"="prologue-short-redirect" {
209+
entry:
210+
call void asm sideeffect "int3", "~{dirflag},~{fpsr},~{flags}"()
211+
ret void
212+
}

0 commit comments

Comments
 (0)