[PATCH] D72220: [X86] Support function attribute "patchable-function-entry"

Fangrui Song via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 7 00:08:14 PST 2020


MaskRay updated this revision to Diff 236529.
MaskRay added a comment.

Implement `.section __patchable_function_entries,"aw", at progbits`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D72220/new/

https://reviews.llvm.org/D72220

Files:
  llvm/lib/Target/X86/X86MCInstLower.cpp
  llvm/test/CodeGen/X86/patchable-function-entry.ll


Index: llvm/test/CodeGen/X86/patchable-function-entry.ll
===================================================================
--- /dev/null
+++ llvm/test/CodeGen/X86/patchable-function-entry.ll
@@ -0,0 +1,44 @@
+; RUN: llc -mtriple=i386 %s -o - | FileCheck --check-prefixes=CHECK,32 %s
+; RUN: llc -mtriple=x86_64 %s -o - | FileCheck --check-prefixes=CHECK,64 %s
+
+define void @f0() "patchable-function-entry"="0" {
+; CHECK-LABEL: f0:
+; CHECK-NOT:   nop
+; CHECK:       ret
+  ret void
+}
+
+define void @f1() "patchable-function-entry"="1" {
+; CHECK-LABEL: f1:
+; CHECK:       nop
+; CHECK-NEXT:  ret
+  ret void
+}
+
+define void @f3() "patchable-function-entry"="3" {
+; CHECK-LABEL: f3:
+; 32-COUNT-3:  nop
+; 64:          nopl (%rax)
+; CHECK:       ret
+  ret void
+}
+
+define void @f5() "patchable-function-entry"="5" {
+; CHECK-LABEL: f5:
+; 32-COUNT-5:  nop
+; 64:          nopl 8(%rax,%rax)
+; CHECK-NEXT:  ret
+  ret void
+}
+
+; CHECK: .section __patchable_function_entries,"aw", at progbits
+; 32: .p2align 2
+; 32-NEXT: .long f0
+; 32-NEXT: .long f1
+; 32-NEXT: .long f3
+; 32-NEXT: .long f5
+; 64: .p2align 3
+; 64-NEXT: .quad f0
+; 64-NEXT: .quad f1
+; 64-NEXT: .quad f3
+; 64-NEXT: .quad f5
Index: llvm/lib/Target/X86/X86MCInstLower.cpp
===================================================================
--- llvm/lib/Target/X86/X86MCInstLower.cpp
+++ llvm/lib/Target/X86/X86MCInstLower.cpp
@@ -1035,9 +1035,11 @@
 /// bytes.  Return the size of nop emitted.
 static unsigned EmitNop(MCStreamer &OS, unsigned NumBytes, bool Is64Bit,
                         const MCSubtargetInfo &STI) {
-  // This works only for 64bit. For 32bit we have to do additional checking if
-  // the CPU supports multi-byte nops.
-  assert(Is64Bit && "EmitNops only supports X86-64");
+  if (!Is64Bit) {
+    // TODO Do additional checking if the CPU supports multi-byte nops.
+    OS.EmitInstruction(MCInstBuilder(X86::NOOP), STI);
+    return 1;
+  }
 
   unsigned NopSize;
   unsigned Opc, BaseReg, ScaleVal, IndexReg, Displacement, SegmentReg;
@@ -1559,6 +1561,17 @@
 
 void X86AsmPrinter::LowerPATCHABLE_FUNCTION_ENTER(const MachineInstr &MI,
                                                   X86MCInstLower &MCIL) {
+  const Function &F = MF->getFunction();
+  if (F.hasFnAttribute("patchable-function-entry")) {
+    recordPatchableFunctionEntry();
+    unsigned Num;
+    if (F.getFnAttribute("patchable-function-entry")
+            .getValueAsString()
+            .getAsInteger(10, Num))
+      return;
+    EmitNops(*OutStreamer, Num, Subtarget->is64Bit(), getSubtargetInfo());
+    return;
+  }
   // We want to emit the following pattern:
   //
   //   .p2align 1, ...


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72220.236529.patch
Type: text/x-patch
Size: 2680 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200107/68ead627/attachment.bin>


More information about the llvm-commits mailing list