[PATCH] D67058: [clang][CodeGen] Add alias for cpu_dispatch function with IFunc

Sr.Zhang via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sun Sep 1 20:10:41 PDT 2019


zsrkmyn created this revision.
zsrkmyn added a reviewer: erichkeane.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Multi-versioned functions defined by cpu_dispatch and implemented with IFunc
can not be called outside the translation units where they are defined due to
lack of symbols. This patch add function aliases for these functions and thus
make them visible outside.


Repository:
  rC Clang

https://reviews.llvm.org/D67058

Files:
  clang/lib/CodeGen/CodeGenModule.cpp


Index: clang/lib/CodeGen/CodeGenModule.cpp
===================================================================
--- clang/lib/CodeGen/CodeGenModule.cpp
+++ clang/lib/CodeGen/CodeGenModule.cpp
@@ -2948,6 +2948,25 @@
 
   CodeGenFunction CGF(*this);
   CGF.EmitMultiVersionResolver(ResolverFunc, Options);
+
+  if (getTarget().supportsIFunc()) {
+    std::string AliasName = getMangledNameImpl(
+        *this, GD, FD, /*OmitMultiVersionMangling=*/true);
+    llvm::Constant *AliasFunc = GetGlobalValue(AliasName);
+    if (!AliasFunc) {
+      auto *IFunc = cast<llvm::GlobalIFunc>(GetOrCreateLLVMFunction(
+          AliasName, DeclTy, GD, /*ForVTable=*/false, /*DontDefer=*/true,
+          /*IsThunk=*/false, llvm::AttributeList(), NotForDefinition));
+      auto *GA = llvm::GlobalAlias::create(
+         DeclTy, 0, getFunctionLinkage(GD), AliasName, IFunc, &getModule());
+      const auto *D = GD.getDecl();
+      if (D->hasAttr<WeakAttr>() || D->hasAttr<WeakRefAttr>() ||
+          D->isWeakImported()) {
+        GA->setLinkage(llvm::Function::WeakAnyLinkage);
+      }
+      SetCommonAttributes(GD, GA);
+    }
+  }
 }
 
 /// If a dispatcher for the specified mangled name is not in the module, create


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D67058.218283.patch
Type: text/x-patch
Size: 1213 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20190902/26312027/attachment.bin>


More information about the cfe-commits mailing list