[PATCH] D122958: [clang] Corrections for target_clones multiversion functions.

Tom Honermann via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Sat Apr 2 14:12:09 PDT 2022


tahonermann created this revision.
tahonermann added reviewers: erichkeane, aaron.ballman.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
tahonermann updated this revision to Diff 419919.
tahonermann added a comment.
tahonermann updated this revision to Diff 420008.
tahonermann published this revision for review.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Removed the in-class declaration of CodeGenModule::EmitTargetClonesResolver().


tahonermann added a comment.

Simplified code to select a resolver function.



================
Comment at: clang/lib/CodeGen/CodeGenModule.cpp:3492
 
-    if (TI.supportsIFunc() || FD->isTargetMultiVersion()) {
-      ResolverFunc = cast<llvm::Function>(
-          GetGlobalValue((getMangledName(GD) + ".resolver").str()));
-      ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
-    } else {
-      ResolverFunc = cast<llvm::Function>(GetGlobalValue(getMangledName(GD)));
-    }
+    ResolverFunc->setLinkage(getMultiversionLinkage(*this, GD));
 
----------------
`setLinkage()` was previously only called in the ifunc case. I don't know why that would be, so I "fixed" it here.


================
Comment at: clang/lib/CodeGen/CodeGenModule.cpp:3323
       EmitGlobalFunctionDefinition(GD.getWithMultiVersionIndex(I), nullptr);
-    // Requires multiple emits.
   } else if (FD->isTargetClonesMultiVersion()) {
----------------
This comment seemed oddly placed and didn't seem to convey used information, so I gave it the boot.


================
Comment at: clang/lib/CodeGen/CodeGenModule.cpp:3411-3461
-void CodeGenModule::EmitTargetClonesResolver(GlobalDecl GD) {
-  const auto *FD = cast<FunctionDecl>(GD.getDecl());
-  assert(FD && "Not a FunctionDecl?");
-  const auto *TC = FD->getAttr<TargetClonesAttr>();
-  assert(TC && "Not a target_clones Function?");
-
-  llvm::Function *ResolverFunc;
----------------
This functionality was all merged into `CodeGenModule::emitMultiVersionFunctions()` where much of it was already present.


================
Comment at: clang/lib/CodeGen/CodeGenModule.cpp:3488-3493
+    if (getTarget().supportsIFunc()) {
+      auto *IFunc =
+          cast<llvm::GlobalIFunc>(GetOrCreateMultiVersionResolver(GD));
+      ResolverFunc = cast<llvm::Function>(IFunc->getResolver());
+    } else
+      ResolverFunc = cast<llvm::Function>(GetOrCreateMultiVersionResolver(GD));
----------------
Use of `GetOrCreateMultiVersionResolver()` here avoids duplication of code to determine how resolver functions are named for each multiversion function kind.


================
Comment at: clang/lib/CodeGen/CodeGenModule.cpp:3670-3673
+  // The resolver needs to be created. For target and target_clones, defer
+  // creation until the end of the TU.
+  if (FD->isTargetMultiVersion() || FD->isTargetClonesMultiVersion())
     MultiVersionFuncs.push_back(GD);
----------------
This is the change that effectively ensures that a `target_clones` function resolver is emitted.


================
Comment at: clang/test/CodeGen/attr-target-clones.c:26
 // LINUX: define {{.*}}i32 @foo.default.1()
-// LINUX: define i32 ()* @foo.resolver() comdat
+// LINUX: define weak_odr i32 ()* @foo.resolver() comdat
 // LINUX: ret i32 ()* @foo.sse4.2.0
----------------
`target_clones` resolvers are now emitted consistently with `target` resolvers.


================
Comment at: clang/test/CodeGen/attr-target-clones.c:152-156
+// LINUX: define linkonce i32 @foo_inline2.arch_sandybridge.0() #[[SB]]
 // LINUX: define linkonce i32 @foo_inline2.default.2() #[[DEF]]
 // LINUX: define linkonce i32 @foo_inline2.sse4.2.1() #[[SSE42]]
 
+// WINDOWS: define linkonce_odr dso_local i32 @foo_inline2.arch_sandybridge.0() #[[SB]]
----------------
Some checks were simply missing previously.


================
Comment at: clang/test/CodeGen/attr-target-clones.c:161-165
+// LINUX: declare i32 @foo_used_no_defn.default.1()
+// LINUX: declare i32 @foo_used_no_defn.sse4.2.0()
+
+// WINDOWS: declare dso_local i32 @foo_used_no_defn.default.1()
+// WINDOWS: declare dso_local i32 @foo_used_no_defn.sse4.2.0()
----------------
Declarations emitted cause the function is no defined!


This change merges code for emit of target and target_clones multiversion
resolver functions and, in doing so, corrects handling of target_clones
functions that are declared but not defined. Previously, a use of such
a target_clones function would result in an attempted emit of an ifunc
that referenced an undefined resolver function. Ifunc references to
undefined resolver functions are not allowed and, when the LLVM verifier
is not disabled (via '-disable-llvm-verifier'), resulted in the verifier
issuing a "IFunc resolver must be a definition" error and aborting the
compilation. With this change, ifuncs and resolver function definitions
are always emitted for used target_clones functions regardless of whether
the target_clones function is defined (if the function is defined, then
the ifunc and resolver are emitted regardless of whether the function is
used).

This change has the side effect of causing target_clones variants and
resolver functions to be emitted in a different order than they were
previously. This is harmless and is reflected in the updated tests.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D122958

Files:
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/test/CodeGen/attr-target-clones.c
  clang/test/CodeGenCXX/attr-target-clones.cpp

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D122958.420008.patch
Type: text/x-patch
Size: 20285 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20220402/0a42dd5b/attachment-0001.bin>


More information about the cfe-commits mailing list