Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ThinLTO]Mark referencers of local ifunc not eligible for import #92431

Merged
merged 6 commits into from
May 16, 2024

Conversation

minglotus-6
Copy link
Contributor

@minglotus-6 minglotus-6 commented May 16, 2024

If an ifunc has local linkage, do not add it into ref edges and mark its referencer (a function or global variable) not eligible for import. An ifunc doesn't have summary and ThinLTO cannot promote it. Importing the referencer may cause linkage errors.

To reference a similar fix, https://reviews.llvm.org/D158961 marks callers of local ifunc not eligible for import to fix #58740

@minglotus-6 minglotus-6 requested a review from MaskRay May 16, 2024 17:11
@minglotus-6 minglotus-6 marked this pull request as ready for review May 16, 2024 17:19
@llvmbot llvmbot added LTO Link time optimization (regular/full LTO or ThinLTO) llvm:analysis labels May 16, 2024
@llvmbot
Copy link
Collaborator

llvmbot commented May 16, 2024

@llvm/pr-subscribers-llvm-analysis

@llvm/pr-subscribers-lto

Author: Mingming Liu (minglotus-6)

Changes

If an ifunc has local linkage, do not add it into ref edges and mark its referencer (a function or global variable) not eligible for import. An ifunc doesn't have summary and ThinLTO cannot promote it. Importing the referencer may cause linkage errors.

To reference a similar fix, https://reviews.llvm.org/D158961 marks callers of local ifunc not eligible for import to fix #58740


Full diff: https://github.com/llvm/llvm-project/pull/92431.diff

2 Files Affected:

  • (modified) llvm/lib/Analysis/ModuleSummaryAnalysis.cpp (+35-17)
  • (added) llvm/test/ThinLTO/X86/ref-ifunc.ll (+46)
diff --git a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
index c3d15afe6d9cb..36cff096f33aa 100644
--- a/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
+++ b/llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
@@ -88,16 +88,22 @@ extern cl::opt<unsigned> MaxNumVTableAnnotations;
 // Walk through the operands of a given User via worklist iteration and populate
 // the set of GlobalValue references encountered. Invoked either on an
 // Instruction or a GlobalVariable (which walks its initializer).
-// Return true if any of the operands contains blockaddress. This is important
-// to know when computing summary for global var, because if global variable
-// references basic block address we can't import it separately from function
-// containing that basic block. For simplicity we currently don't import such
-// global vars at all. When importing function we aren't interested if any
-// instruction in it takes an address of any basic block, because instruction
-// can only take an address of basic block located in the same function.
+//
+// Return true if any user makes the analyzed instruction or global variable not
+// eligible to import.
+// - If a local-linkage ifunc is referenced, mark the analyzed value as not
+// eligible for import.
+// - Additionally, global vars are marked as not eligible to import if they
+// references references basic block address, because we can't import it
+// separately from function containing that basic block. For simplicity we
+// currently don't import such global vars at all. When importing functions we
+// aren't interested if any instruction in it takes an address of any basic
+// block, because instruction can only take an address of basic block located in
+// the same function.
 static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
                          SetVector<ValueInfo, std::vector<ValueInfo>> &RefEdges,
-                         SmallPtrSet<const User *, 8> &Visited) {
+                         SmallPtrSet<const User *, 8> &Visited,
+                         bool &RefLocalLinkageIFunc) {
   bool HasBlockAddress = false;
   SmallVector<const User *, 32> Worklist;
   if (Visited.insert(CurUser).second)
@@ -119,8 +125,18 @@ static bool findRefEdges(ModuleSummaryIndex &Index, const User *CurUser,
         // We have a reference to a global value. This should be added to
         // the reference set unless it is a callee. Callees are handled
         // specially by WriteFunction and are added to a separate list.
-        if (!(CB && CB->isCallee(&OI)))
+        if (!(CB && CB->isCallee(&OI))) {
+          // If an ifunc has local linkage, do not add it into ref edges and
+          // mark its referencer not eligible for import. An ifunc doesn't have
+          // summary and ThinLTO cannot promote it; importing the referencer may
+          // cause linkage errors.
+          if (auto *GI = dyn_cast_if_present<GlobalIFunc>(GV);
+              GI && GI->hasLocalLinkage()) {
+            RefLocalLinkageIFunc = true;
+            continue;
+          }
           RefEdges.insert(Index.getOrInsertValueInfo(GV));
+        }
         continue;
       }
       if (Visited.insert(Operand).second)
@@ -313,7 +329,8 @@ static void computeFunctionSummary(
 
   // Add personality function, prefix data and prologue data to function's ref
   // list.
-  findRefEdges(Index, &F, RefEdges, Visited);
+  bool HasIFunc = false;
+  findRefEdges(Index, &F, RefEdges, Visited, HasIFunc);
   std::vector<const Instruction *> NonVolatileLoads;
   std::vector<const Instruction *> NonVolatileStores;
 
@@ -326,7 +343,7 @@ static void computeFunctionSummary(
 
   bool HasInlineAsmMaybeReferencingInternal = false;
   bool HasIndirBranchToBlockAddress = false;
-  bool HasIFuncCall = false;
+
   bool HasUnknownCall = false;
   bool MayThrow = false;
   for (const BasicBlock &BB : F) {
@@ -372,11 +389,11 @@ static void computeFunctionSummary(
             // of calling it we should add GV to RefEdges directly.
             RefEdges.insert(Index.getOrInsertValueInfo(GV));
           else if (auto *U = dyn_cast<User>(Stored))
-            findRefEdges(Index, U, RefEdges, Visited);
+            findRefEdges(Index, U, RefEdges, Visited, HasIFunc);
           continue;
         }
       }
-      findRefEdges(Index, &I, RefEdges, Visited);
+      findRefEdges(Index, &I, RefEdges, Visited, HasIFunc);
       const auto *CB = dyn_cast<CallBase>(&I);
       if (!CB) {
         if (I.mayThrow())
@@ -450,7 +467,7 @@ static void computeFunctionSummary(
         // Non-local ifunc is not cloned and does not have the issue.
         if (auto *GI = dyn_cast_if_present<GlobalIFunc>(CalledValue))
           if (GI->hasLocalLinkage())
-            HasIFuncCall = true;
+            HasIFunc = true;
         // Skip inline assembly calls.
         if (CI && CI->isInlineAsm())
           continue;
@@ -555,7 +572,7 @@ static void computeFunctionSummary(
                            SmallPtrSet<const User *, 8> &Cache) {
       for (const auto *I : Instrs) {
         Cache.erase(I);
-        findRefEdges(Index, I, Edges, Cache);
+        findRefEdges(Index, I, Edges, Cache, HasIFunc);
       }
     };
 
@@ -633,7 +650,7 @@ static void computeFunctionSummary(
   bool NonRenamableLocal = isNonRenamableLocal(F);
   bool NotEligibleForImport = NonRenamableLocal ||
                               HasInlineAsmMaybeReferencingInternal ||
-                              HasIndirBranchToBlockAddress || HasIFuncCall;
+                              HasIndirBranchToBlockAddress || HasIFunc;
   GlobalValueSummary::GVFlags Flags(
       F.getLinkage(), F.getVisibility(), NotEligibleForImport,
       /* Live = */ false, F.isDSOLocal(), F.canBeOmittedFromSymbolTable(),
@@ -787,7 +804,8 @@ static void computeVariableSummary(ModuleSummaryIndex &Index,
                                    SmallVectorImpl<MDNode *> &Types) {
   SetVector<ValueInfo, std::vector<ValueInfo>> RefEdges;
   SmallPtrSet<const User *, 8> Visited;
-  bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited);
+  bool Unused = false;
+  bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited, Unused);
   bool NonRenamableLocal = isNonRenamableLocal(V);
   GlobalValueSummary::GVFlags Flags(
       V.getLinkage(), V.getVisibility(), NonRenamableLocal,
diff --git a/llvm/test/ThinLTO/X86/ref-ifunc.ll b/llvm/test/ThinLTO/X86/ref-ifunc.ll
new file mode 100644
index 0000000000000..714ae83cb1abb
--- /dev/null
+++ b/llvm/test/ThinLTO/X86/ref-ifunc.ll
@@ -0,0 +1,46 @@
+; RUN: opt -module-summary %s -o %t.bc
+
+; RUN: llvm-dis %t.bc -o - | FileCheck %s
+
+; Tests that caller is not eligible to import and it doesn't have refs to ifunc 'callee'
+
+; CHECK: gv: (name: "caller", summaries: (function: ({{.*}}, flags: ({{.*}}notEligibleToImport: 1
+; CHECK-NOT: refs
+; CHECK-SAME: guid = 16677772384402303968
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+@__cpu_model = external global { i32, i32, i32, [1 x i32] }
+
+@callee = internal ifunc void(), ptr @callee.resolver
+
+define void @dispatch(ptr %func) {
+    tail call void %func()
+    ret void
+}
+
+define void @caller() {
+  tail call void @dispatch(ptr @callee)
+  ret void
+}
+
+define internal ptr @callee.resolver() {
+resolver_entry:
+  tail call void @__cpu_indicator_init()
+  %0 = load i32, ptr getelementptr inbounds ({ i32, i32, i32, [1 x i32] }, ptr @__cpu_model, i64 0, i32 3, i64 0)
+  %1 = and i32 %0, 1024
+  %.not = icmp eq i32 %1, 0
+  %func_sel = select i1 %.not, ptr @callee.default.1, ptr @callee.avx2.0
+  ret ptr %func_sel
+}
+
+define internal void @callee.default.1(i32 %a) {
+  ret void
+}
+
+define internal void @callee.avx2.0(i32 %a) {
+  ret void
+}
+
+declare void @__cpu_indicator_init()

// - If a local-linkage ifunc is referenced, the analyzed value is not eligible
// for import.
// - Additionally, global vars will be marked as not eligible to import if they
// references references basic block address, because we can't import it
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

s/references references/reference a/

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore if you put the old comment block back as I suggest above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -787,7 +804,9 @@ static void computeVariableSummary(ModuleSummaryIndex &Index,
SmallVectorImpl<MDNode *> &Types) {
SetVector<ValueInfo, std::vector<ValueInfo>> RefEdges;
SmallPtrSet<const User *, 8> Visited;
bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited);
bool RefIFunc = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest "RefLocalIFunc"

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, can you add this case to your new test? I think it is only testing the call case.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed and added @var = constant { [1 x ptr] } { [1 x ptr] [ptr @callee]} in the test.

@@ -326,7 +343,7 @@ static void computeFunctionSummary(

bool HasInlineAsmMaybeReferencingInternal = false;
bool HasIndirBranchToBlockAddress = false;
bool HasIFuncCall = false;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggest "HasLocalIFuncCallOrRef" to be more explicit

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

// can only take an address of basic block located in the same function.
//
// Return true if any user references a block address, and sets
// `RefLocalLinkageIFunc` to true if the analyzed value references local ifunc.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since the RefLocalLinkageIFunc doesn't affect the return value, I suggest leaving the original comment as is, but add a separate blurb below it about the new output parameter.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

Copy link
Contributor Author

@minglotus-6 minglotus-6 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ptal, thanks!

// - If a local-linkage ifunc is referenced, the analyzed value is not eligible
// for import.
// - Additionally, global vars will be marked as not eligible to import if they
// references references basic block address, because we can't import it
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

// can only take an address of basic block located in the same function.
//
// Return true if any user references a block address, and sets
// `RefLocalLinkageIFunc` to true if the analyzed value references local ifunc.
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -326,7 +343,7 @@ static void computeFunctionSummary(

bool HasInlineAsmMaybeReferencingInternal = false;
bool HasIndirBranchToBlockAddress = false;
bool HasIFuncCall = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done.

@@ -787,7 +804,9 @@ static void computeVariableSummary(ModuleSummaryIndex &Index,
SmallVectorImpl<MDNode *> &Types) {
SetVector<ValueInfo, std::vector<ValueInfo>> RefEdges;
SmallPtrSet<const User *, 8> Visited;
bool HasBlockAddress = findRefEdges(Index, &V, RefEdges, Visited);
bool RefIFunc = false;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Renamed and added @var = constant { [1 x ptr] } { [1 x ptr] [ptr @callee]} in the test.

@minglotus-6
Copy link
Contributor Author

The failed test is irrelevant

# After downloading logs from https://buildkite.com/llvm-project/github-pull-requests/builds/64747#018f8296-6cf1-497e-94ab-8a53cd351a8f

cat github-pull-requests_build_64747_linux-linux-x64.log | grep -B 10 -A 10 "Failed Tests"
bk;t=1715887562543[0.70s,0.80s) :: [                                        ] :: [  1/419]
bk;t=1715887562543[0.60s,0.70s) :: [                                        ] :: [  1/419]
bk;t=1715887562543[0.50s,0.60s) :: [                                        ] :: [  6/419]
bk;t=1715887562543[0.40s,0.50s) :: [                                        ] :: [  9/419]
bk;t=1715887562543[0.30s,0.40s) :: [**                                      ] :: [ 23/419]
bk;t=1715887562543[0.20s,0.30s) :: [*************                           ] :: [142/419]
bk;t=1715887562543[0.10s,0.20s) :: [***************                         ] :: [167/419]
bk;t=1715887562543[0.00s,0.10s) :: [******                                  ] :: [ 69/419]
bk;t=1715887562543--------------------------------------------------------------------------
bk;t=1715887562543********************
bk;t=1715887562543Failed Tests (1):
bk;t=1715887562543  BOLT :: RISCV/unnamed-sym-no-entry.c
bk;t=1715887562543
bk;t=1715887562543
bk;t=1715887562543Testing Time: 2.56s
bk;t=1715887562543
bk;t=1715887562543Total Discovered Tests: 431
bk;t=1715887562543  Skipped          :   7 (1.62%)
bk;t=1715887562543  Unsupported      :  13 (3.02%)
bk;t=1715887562543  Passed           : 409 (94.90%)
bk;t=1715887562543  Expectedly Failed:   1 (0.23%)

@minglotus-6 minglotus-6 merged commit fa9b1be into llvm:main May 16, 2024
3 of 4 checks passed
qiaojbao pushed a commit to GPUOpen-Drivers/llvm-project that referenced this pull request Jun 5, 2024
…52d673f5b

Local branch amd-gfx fb552d6 Merged main:e578314c049bb9ae6dc3983db5cf27513e29517b into amd-gfx:f40f282b2831
Remote branch main fa9b1be [ThinLTO]Mark referencers of local ifunc not eligible for import (llvm#92431)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
llvm:analysis LTO Link time optimization (regular/full LTO or ThinLTO)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

LTO + target_clones leads to undefined references
3 participants