[PATCH] D129709: [clang][CodeGen] add fn_ret_thunk_extern to synthetic fns

Nick Desaulniers via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Wed Jul 13 17:30:52 PDT 2022


nickdesaulniers created this revision.
nickdesaulniers added reviewers: MaskRay, void.
Herald added subscribers: jsji, StephenFan, jdoerfert, pengfei, hiraditya.
Herald added a project: All.
nickdesaulniers requested review of this revision.
Herald added projects: clang, LLVM.
Herald added subscribers: llvm-commits, cfe-commits.

Follow up fix to
commit 2240d72f15f3 <https://reviews.llvm.org/rG2240d72f15f3b7b9d9fb65450f9bf635fd310f6f> ("[X86] initial -mfunction-return=thunk-extern
support")
https://reviews.llvm.org/D129572

@nathanchance reported that -mfunction-return=thunk-extern was failing
to annotate the asan and tsan contructors.
https://lore.kernel.org/llvm/Ys7pLq+tQk5xEa%2FB@dev-arch.thelio-3990X/

I then noticed the same occurring for gcov synthetic functions.

Similar to
commit 2786e67 <https://reviews.llvm.org/rG2786e673c7d67ffca531ef38d679620ee3048a1e> ("[IR][sanitizer] Add module flag "frame-pointer" and set
it for cc1 -mframe-pointer={non-leaf,all}")
define a new module level MetaData, "fn_ret_thunk_extern", then when set
adds the fn_ret_thunk_extern IR Fn Attr to synthetically created
Functions.

https://github.com/llvm/llvm-project/issues/56514

Fixes #56514


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D129709

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/test/CodeGen/attr-function-return.c
  llvm/docs/LangRef.rst
  llvm/lib/IR/Function.cpp


Index: llvm/lib/IR/Function.cpp
===================================================================
--- llvm/lib/IR/Function.cpp
+++ llvm/lib/IR/Function.cpp
@@ -354,6 +354,8 @@
     B.addAttribute("frame-pointer", "all");
     break;
   }
+  if (M->getModuleFlag("fn_return_thunk_extern"))
+    B.addAttribute(Attribute::FnRetThunkExtern);
   F->addFnAttrs(B);
   return F;
 }
Index: llvm/docs/LangRef.rst
===================================================================
--- llvm/docs/LangRef.rst
+++ llvm/docs/LangRef.rst
@@ -7367,6 +7367,8 @@
 - "uwtable": **Max**. The value can be 0, 1, or 2. If the value is 1, a synthesized
   function will get the ``uwtable(sync)`` function attribute, if the value is 2,
   a synthesized function will get the ``uwtable(async)`` function attribute.
+- "fn_return_thunk_extern": The synthesized function will get the
+  ``fn_return_thunk_extern`` function attribute.
 
 Objective-C Garbage Collection Module Flags Metadata
 ----------------------------------------------------
Index: clang/test/CodeGen/attr-function-return.c
===================================================================
--- clang/test/CodeGen/attr-function-return.c
+++ clang/test/CodeGen/attr-function-return.c
@@ -6,6 +6,15 @@
 // RUN: %clang_cc1 -std=gnu2x -triple x86_64-linux-gnu %s -emit-llvm -o - \
 // RUN:  -mfunction-return=thunk-extern | FileCheck %s \
 // RUN:  --check-prefixes=CHECK,CHECK-EXTERN
+// RUN: %clang_cc1 -std=gnu2x -triple x86_64-linux-gnu %s -emit-llvm -o - \
+// RUN:  -mfunction-return=thunk-extern -fprofile-arcs \
+// RUN:   | FileCheck %s --check-prefix=CHECK-GCOV
+// RUN: %clang_cc1 -std=gnu2x -triple x86_64-linux-gnu %s -emit-llvm -o - \
+// RUN:  -mfunction-return=thunk-extern -fsanitize=address \
+// RUN:   | FileCheck %s --check-prefix=CHECK-ASAN
+// RUN: %clang_cc1 -std=gnu2x -triple x86_64-linux-gnu %s -emit-llvm -o - \
+// RUN:  -mfunction-return=thunk-extern -fsanitize=thread \
+// RUN:   | FileCheck %s --check-prefix=CHECK-TSAN
 
 #if !__has_attribute(function_return)
 #error "missing attribute support for function_return"
@@ -91,6 +100,16 @@
 // CHECK-EXTERN: @no_attrs() [[EXTERN]]
 void no_attrs(void) {}
 
+// Test synthetic functions.
+// CHECK-GCOV: @__llvm_gcov_writeout() unnamed_addr [[EXTERNGCOV:#[0-9]+]]
+// CHECK-GCOV: @__llvm_gcov_reset() unnamed_addr [[EXTERNGCOV]]
+// CHECK-GCOV: @__llvm_gcov_init() unnamed_addr [[EXTERNGCOV]]
+// CHECK-ASAN: @asan.module_ctor() [[EXTERNASAN:#[0-9]+]]
+// CHECK-TSAN: @tsan.module_ctor() [[EXTERNTSAN:#[0-9]+]]
+
 // CHECK-NOM-NOT:  [[NOATTR]] = {{.*}}fn_ret_thunk_extern
 // CHECK-KEEP-NOT: [[NOATTR]] = {{.*}}fn_ret_thunk_extern
 // CHECK: [[EXTERN]] = {{.*}}fn_ret_thunk_extern
+// CHECK-GCOV: [[EXTERNGCOV]] = {{.*}}fn_ret_thunk_extern
+// CHECK-ASAN: [[EXTERNASAN]] = {{.*}}fn_ret_thunk_extern
+// CHECK-TSAN: [[EXTERNTSAN]] = {{.*}}fn_ret_thunk_extern
Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -756,6 +756,9 @@
   if (CodeGenOpts.IBTSeal)
     getModule().addModuleFlag(llvm::Module::Override, "ibt-seal", 1);
 
+  if (CodeGenOpts.FunctionReturnThunks)
+    getModule().addModuleFlag(llvm::Module::Override, "fn_return_thunk_extern", 1);
+
   // Add module metadata for return address signing (ignoring
   // non-leaf/all) and stack tagging. These are actually turned on by function
   // attributes, but we use module metadata to emit build attributes. This is


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D129709.444470.patch
Type: text/x-patch
Size: 3542 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220714/86434ba0/attachment.bin>


More information about the cfe-commits mailing list