[AsmPrinter] Fix placement of function entry comments
authorFangrui Song <i@maskray.me>
Tue, 18 Apr 2023 22:01:36 +0000 (15:01 -0700)
committerFangrui Song <i@maskray.me>
Tue, 18 Apr 2023 22:01:36 +0000 (15:01 -0700)
The placement is currently wrong in the presence of function entry related
instrumentations (prefixdata, -fpatchable-function-entry=, -fsanitize=kcfi,
etc).

llvm/lib/CodeGen/AsmPrinter/AsmPrinter.cpp
llvm/test/CodeGen/AArch64/patchable-function-entry.ll
llvm/test/CodeGen/AArch64/prefixdata.ll
llvm/test/CodeGen/LoongArch/patchable-function-entry.ll
llvm/test/CodeGen/RISCV/patchable-function-entry.ll
llvm/test/CodeGen/X86/patchable-function-entry.ll
llvm/test/CodeGen/X86/prefixdata.ll

index 10b6e52..0a803d8 100644 (file)
@@ -927,13 +927,6 @@ void AsmPrinter::emitFunctionHeader() {
   if (F.hasFnAttribute(Attribute::Cold))
     OutStreamer->emitSymbolAttribute(CurrentFnSym, MCSA_Cold);
 
-  if (isVerbose()) {
-    F.printAsOperand(OutStreamer->getCommentOS(),
-                     /*PrintType=*/false, F.getParent());
-    emitFunctionHeaderComment();
-    OutStreamer->getCommentOS() << '\n';
-  }
-
   // Emit the prefix data.
   if (F.hasPrefixData()) {
     if (MAI->hasSubsectionsViaSymbols()) {
@@ -977,6 +970,13 @@ void AsmPrinter::emitFunctionHeader() {
     CurrentPatchableFunctionEntrySym = CurrentFnBegin;
   }
 
+  if (isVerbose()) {
+    F.printAsOperand(OutStreamer->getCommentOS(),
+                     /*PrintType=*/false, F.getParent());
+    emitFunctionHeaderComment();
+    OutStreamer->getCommentOS() << '\n';
+  }
+
   // Emit the function descriptor. This is a virtual function to allow targets
   // to emit their specific function descriptor. Right now it is only used by
   // the AIX target. The PowerPC 64-bit V1 ELF target also uses function
index 7768906..5750c1f 100644 (file)
@@ -71,10 +71,10 @@ define void @f5() "patchable-function-entry"="5" comdat {
 ;; "patchable-function-prefix" emits data before the function entry label.
 define void @f3_2() "patchable-function-entry"="1" "patchable-function-prefix"="2" {
 ; CHECK-LABEL: .type f3_2,@function
-; CHECK-NEXT: .Ltmp1: // @f3_2
+; CHECK-NEXT: .Ltmp1:
 ; CHECK-NEXT:  nop
 ; CHECK-NEXT:  nop
-; CHECK-NEXT: f3_2:
+; CHECK-NEXT: f3_2:  // @f3_2
 ; CHECK:      // %bb.0:
 ; CHECK-NEXT:  nop
 ; CHECK-NEXT:  ret
@@ -90,10 +90,10 @@ define void @f3_2() "patchable-function-entry"="1" "patchable-function-prefix"="
 ;; When prefix data is used, arbitrarily place NOPs after prefix data.
 define void @prefix() "patchable-function-entry"="0" "patchable-function-prefix"="1" prefix i32 1 {
 ; CHECK-LABEL: .type prefix,@function
-; CHECK-NEXT: .word 1 // @prefix
+; CHECK-NEXT: .word 1  // 0x1
 ; CHECK:      .Ltmp2:
 ; CHECK:       nop
-; CHECK-NEXT: prefix:
+; CHECK-NEXT: prefix:  // @prefix
 ;; Emit a __patchable_function_entries entry even if "patchable-function-entry" is 0.
 ; CHECK:      .section __patchable_function_entries,"awo",@progbits,prefix{{$}}
 ; CHECK:      .p2align 3
index 37d4cc0..eeeeb7b 100644 (file)
@@ -8,9 +8,8 @@
 ; MACHO-NEXT: .alt_entry _f
 ; MACHO-NEXT: _f:
 ; ELF: .type f,@function
-; ELF-NEXT: .word      1
-; ELF-NEXT: // 0x1
-; ELF-NEXT: f:
+; ELF-NEXT: .word      1  // 0x1
+; ELF-NEXT: f:  // @f
 define void @f() prefix i32 1 {
   ret void
 }
index 12d4bfb..aaa3fda 100644 (file)
@@ -43,9 +43,9 @@ define void @f5() "patchable-function-entry"="5" comdat {
 ;; "patchable-function-prefix" emits data before the function entry label.
 define void @f3_2() "patchable-function-entry"="1" "patchable-function-prefix"="2" {
 ; CHECK-LABEL:   .type f3_2,@function
-; CHECK-NEXT:    .Ltmp0: # @f3_2
+; CHECK-NEXT:    .Ltmp0:
 ; CHECK-COUNT-2:   nop
-; CHECK-NEXT:    f3_2:
+; CHECK-NEXT:    f3_2:  # @f3_2
 ; CHECK:         # %bb.0:
 ; CHECK-NEXT:      nop
 ; LA32-NEXT:       addi.w $sp, $sp, -16
index 9c4eb2c..2804fdf 100644 (file)
@@ -49,10 +49,10 @@ define void @f5() "patchable-function-entry"="5" comdat {
 ;; "patchable-function-prefix" emits data before the function entry label.
 define void @f3_2() "patchable-function-entry"="1" "patchable-function-prefix"="2" {
 ; CHECK-LABEL: .type f3_2,@function
-; CHECK-NEXT:  .Ltmp0: # @f3_2
+; CHECK-NEXT:  .Ltmp0:
 ; NORVC-COUNT-2: addi zero, zero, 0
 ; RVC-COUNT-2:   c.nop
-; CHECK-NEXT:  f3_2:
+; CHECK-NEXT:  f3_2:  # @f3_2
 ; CHECK:       # %bb.0:
 ; NORVC-NEXT:    addi zero, zero, 0
 ; NORVC-NEXT:    addi sp, sp, -16
index 8d09843..124f5c5 100644 (file)
@@ -80,10 +80,10 @@ define void @f5() "patchable-function-entry"="5" comdat {
 ;; the remaining instructions do not need to be modified.
 define void @f3_2() "patchable-function-entry"="1" "patchable-function-prefix"="2" {
 ; CHECK-LABEL: .type f3_2,@function
-; CHECK-NEXT: .Ltmp0: # @f3_2
+; CHECK-NEXT: .Ltmp0:
 ; CHECK-NEXT:  nop
 ; CHECK-NEXT:  nop
-; CHECK-NEXT: f3_2:
+; CHECK-NEXT: f3_2: # @f3_2
 ; CHECK:      # %bb.0:
 ; CHECK-NEXT:  nop
 ; CHECK-NEXT:  ret
index 2c3f277..20a558c 100644 (file)
@@ -4,13 +4,12 @@
 @i = linkonce_odr global i32 1
 
 ; MACHO: ltmp0:
-; MACHO-NEXT: .long 1
+; MACHO-NEXT: .long 1  ## 0x1
 ; MACHO-NEXT: .alt_entry _f
-; MACHO-NEXT: _f:
+; MACHO-NEXT: _f:  ## @f
 ; ELF: .type f,@function
-; ELF-NEXT: .long      1
-; ELF-NEXT: # 0x1
-; ELF-NEXT: f:
+; ELF-NEXT: .long      1  # 0x1
+; ELF-NEXT: f:  # @f
 define void @f() prefix i32 1 {
   ret void
 }