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

[Codegen][X86] Fix /hotpatch with clang-cl and inline asm #87639

Merged
merged 2 commits into from
Apr 9, 2024

Conversation

aganea
Copy link
Member

@aganea aganea commented Apr 4, 2024

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 2 NOPs (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

This fixes an edge case where functions starting with inline assembly would
assert while trying to lower that inline asm instruction.

After this commit, for now we always add 2 NOPs 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 llvm#56234
@llvmbot
Copy link
Collaborator

llvmbot commented Apr 4, 2024

@llvm/pr-subscribers-backend-x86

Author: Alexandre Ganea (aganea)

Changes

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 2 NOPs 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


Full diff: https://github.com/llvm/llvm-project/pull/87639.diff

2 Files Affected:

  • (modified) llvm/lib/Target/X86/X86MCInstLower.cpp (+3-1)
  • (modified) llvm/test/CodeGen/X86/patchable-prologue.ll (+17)
diff --git a/llvm/lib/Target/X86/X86MCInstLower.cpp b/llvm/lib/Target/X86/X86MCInstLower.cpp
index e2330ff34c1753..e6510be6b9afd0 100644
--- a/llvm/lib/Target/X86/X86MCInstLower.cpp
+++ b/llvm/lib/Target/X86/X86MCInstLower.cpp
@@ -980,8 +980,10 @@ void X86AsmPrinter::LowerPATCHABLE_OP(const MachineInstr &MI,
   SmallString<256> Code;
   unsigned MinSize = MI.getOperand(0).getImm();
 
-  if (NextMI != MI.getParent()->end()) {
+  if (NextMI != MI.getParent()->end() && !NextMI->isInlineAsm()) {
     // Lower the next MachineInstr to find its byte size.
+    // If the next instruction is inline assembly, we skip lowering it for now,
+    // and assume we should always generate NOPs.
     MCInst MCI;
     MCIL.Lower(&*NextMI, MCI);
 
diff --git a/llvm/test/CodeGen/X86/patchable-prologue.ll b/llvm/test/CodeGen/X86/patchable-prologue.ll
index 71a392845fdea3..901da41d5b6cac 100644
--- a/llvm/test/CodeGen/X86/patchable-prologue.ll
+++ b/llvm/test/CodeGen/X86/patchable-prologue.ll
@@ -193,3 +193,20 @@ do.body:                                          ; preds = %do.body, %entry
 do.end:                                           ; preds = %do.body
   ret void
 }
+
+
+; Test that inline asm is properly hotpatched. We currently don't examine the
+; asm instruction when printing it, thus we always emit patching NOPs.
+
+; 64: inline_asm:
+; 64-NEXT: # %bb.0:
+; 64-NEXT: xchgw   %ax, %ax                        # encoding: [0x66,0x90]
+; 64-NEXT: #APP
+; 64-NEXT: nop                                     # encoding: [0x90]
+; 64-NEXT: #NO_APP
+
+define dso_local void @inline_asm() "patchable-function"="prologue-short-redirect" {
+entry:
+  call void asm sideeffect "nop", "~{dirflag},~{fpsr},~{flags}"()
+  ret void
+}
\ No newline at end of file

Copy link
Contributor

@KanRobert KanRobert left a comment

Choose a reason for hiding this comment

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

LGTM in general

@sylvain-audi
Copy link
Contributor

LGTM too

@aganea aganea merged commit ec1af63 into llvm:main Apr 9, 2024
4 checks passed
@aganea aganea deleted the fix_hotpatch_clangcl_ue branch April 11, 2024 12:38
llvmbot pushed a commit to llvmbot/llvm-project that referenced this pull request Apr 11, 2024
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 llvm#56234

(cherry picked from commit ec1af63)
tstellar pushed a commit to llvmbot/llvm-project that referenced this pull request Apr 15, 2024
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 llvm#56234

(cherry picked from commit ec1af63)
@pointhex pointhex mentioned this pull request May 7, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Adding hot-patch compiler flag when compiling UE5 from source fails on multiple files.
4 participants