[llvm] [ThinLTO] Clear read/write only flags when referencing function summary (PR #178761)

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Fri Jan 30 10:38:42 PST 2026


https://github.com/teresajohnson updated https://github.com/llvm/llvm-project/pull/178761

>From 92a3dab7c785b8e678627932228508fd87a91060 Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson at google.com>
Date: Thu, 29 Jan 2026 13:41:50 -0800
Subject: [PATCH 1/5] [ThinLTO] Expand test to cover same-named local var and
 func

Enhance the test of importing of read-only (loaded from only) same named
local variables to test another case. In the new case, there is another
local of the same name in another module that is a function instead of a
variable. This test illustrates that we correctly conservatively mark
all of the summaries of that GUID as non-read only (because a function
cannot be loaded from).

This is helpful to confirm since it illustrates that once we see a
non-global variable summary on a ValueInfo (for a GUID), we can assume
that none of the other summaries are read/write-only marked global
variables (which must be imported on reference).
---
 .../X86/Inputs/local_name_conflict_var3.ll      | 14 ++++++++++++++
 .../test/ThinLTO/X86/local_name_conflict_var.ll | 17 ++++++++++++++---
 2 files changed, 28 insertions(+), 3 deletions(-)
 create mode 100644 llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll

diff --git a/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll b/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll
new file mode 100644
index 0000000000000..979b64ec7d77f
--- /dev/null
+++ b/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll
@@ -0,0 +1,14 @@
+; ModuleID = 'local_name_conflict_var.o'
+source_filename = "local_name_conflict_var.c"
+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"
+
+define internal i32 @baz() {
+  ret i32 0
+}
+
+define i32 @b() {
+entry:
+  %is_null = icmp eq i32 ()* @baz, null
+  ret i32 0
+}
diff --git a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
index ed5c6e4fd4f98..0a7ab051d250b 100644
--- a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
+++ b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
@@ -4,9 +4,9 @@
 
 ; Do setup work for all below tests: generate bitcode and combined index
 ; RUN: opt -module-summary -module-hash %s -o %t.bc
-; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var1.ll -o %t2.bc
-; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var2.ll -o %t3.bc
-; RUN: llvm-lto -thinlto-action=thinlink -o %t4.bc %t.bc %t2.bc %t3.bc
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var1.ll -o %t1.bc
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var2.ll -o %t2.bc
+; RUN: llvm-lto -thinlto-action=thinlink -o %t4.bc %t.bc %t1.bc %t2.bc
 
 ; This module will import a() and b() which should cause the read only copy
 ; of baz from each of those modules to be imported. Check that the both are
@@ -15,6 +15,17 @@
 ; IMPORT: @baz.llvm.{{.*}} = internal global i32 10
 ; IMPORT: @baz.llvm.{{.*}} = internal global i32 10
 
+;; Now do the same but linking in a 3rd module, in which baz is a local function.
+;; We should correctly mark all summaries for baz as non-read only (which doesn't
+;; make sense for a function, as you can't load from it). This should cause the
+;; linkage type of the imported copies of baz variables to be available_externally
+;; instead of internal.
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3.ll -o %t3.bc
+; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3.bc
+; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t.bc -thinlto-index=%t5.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORTNOREADONLY
+; IMPORTNOREADONLY: @baz.llvm.{{.*}} = available_externally hidden global i32 10
+; IMPORTNOREADONLY: @baz.llvm.{{.*}} = available_externally hidden global i32 10
+
 ; ModuleID = 'local_name_conflict_var_main.o'
 source_filename = "local_name_conflict_var_main.c"
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

>From 0c7673788aae3d62b79d3ed4ef1e8a4303a66980 Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson at google.com>
Date: Thu, 29 Jan 2026 19:47:36 -0800
Subject: [PATCH 2/5] Make sure we clear the read/write-only flags on all
 referenced summaries if any is a function. Add another test that would fail
 without the new change.

---
 llvm/lib/IR/ModuleSummaryIndex.cpp            | 25 ++++++++++++++++++-
 .../X86/Inputs/local_name_conflict_var3b.ll   | 14 +++++++++++
 .../ThinLTO/X86/local_name_conflict_var.ll    | 19 ++++++++------
 3 files changed, 50 insertions(+), 8 deletions(-)
 create mode 100644 llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll

diff --git a/llvm/lib/IR/ModuleSummaryIndex.cpp b/llvm/lib/IR/ModuleSummaryIndex.cpp
index 33947542bcf16..070d0a9ef5d7f 100644
--- a/llvm/lib/IR/ModuleSummaryIndex.cpp
+++ b/llvm/lib/IR/ModuleSummaryIndex.cpp
@@ -219,7 +219,8 @@ propagateAttributesToRefs(GlobalValueSummary *S,
         continue;
     } else if (MarkedNonReadWriteOnly.contains(VI))
       continue;
-    for (auto &Ref : VI.getSummaryList())
+    bool HasNonGVar = false;
+    for (auto &Ref : VI.getSummaryList()) {
       // If references to alias is not read/writeonly then aliasee
       // is not read/writeonly
       if (auto *GVS = dyn_cast<GlobalVarSummary>(Ref->getBaseObject())) {
@@ -227,7 +228,29 @@ propagateAttributesToRefs(GlobalValueSummary *S,
           GVS->setReadOnly(false);
         if (!VI.isWriteOnly())
           GVS->setWriteOnly(false);
+      } else {
+        // Note that this needs special processing.
+        HasNonGVar = true;
+        break;
       }
+    }
+    // In the case where we have a reference to a VI that is a function not a
+    // variable, conservatively mark all summaries as non-read or write only.
+    // In most cases that would have happened in the above loop. However,
+    // this will make a difference in a few rare cases where there are same
+    // named locals in modules without enough distinguishing path, which end up
+    // with the same GUID. If these are a mix of variables and functions we want
+    // to handle the variables conservatively.
+    if (HasNonGVar) {
+      for (auto &Ref : VI.getSummaryList()) {
+        auto *GVS = dyn_cast<GlobalVarSummary>(Ref->getBaseObject());
+        if (!GVS)
+          continue;
+        GVS->setReadOnly(false);
+        GVS->setWriteOnly(false);
+      }
+      MarkedNonReadWriteOnly.insert(VI);
+    }
   }
 }
 
diff --git a/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll b/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll
new file mode 100644
index 0000000000000..7898e12cb5907
--- /dev/null
+++ b/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll
@@ -0,0 +1,14 @@
+; ModuleID = 'local_name_conflict_var.o'
+source_filename = "local_name_conflict_var.c"
+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"
+
+define internal i32 @baz() {
+  ret i32 0
+}
+
+define i32 @b() {
+entry:
+  %call1 = call i32 (...) @baz()
+  ret i32 0
+}
diff --git a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
index 0a7ab051d250b..6a4d1352e4e2c 100644
--- a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
+++ b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
@@ -15,17 +15,22 @@
 ; IMPORT: @baz.llvm.{{.*}} = internal global i32 10
 ; IMPORT: @baz.llvm.{{.*}} = internal global i32 10
 
-;; Now do the same but linking in a 3rd module, in which baz is a local function.
-;; We should correctly mark all summaries for baz as non-read only (which doesn't
-;; make sense for a function, as you can't load from it). This should cause the
-;; linkage type of the imported copies of baz variables to be available_externally
-;; instead of internal.
-; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3.ll -o %t3.bc
-; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3.bc
+;; Now do the same but linking in a 3rd module, in which baz is a local function,
+;; which has a non-call reference to it in b(). We should correctly mark all
+;; summaries for baz as non-read only, which should cause the linkage type of
+;; the imported copies of baz variables to be available_externally instead of
+;; internal.
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3b.ll -o %t3b.bc
+; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3b.bc
 ; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t.bc -thinlto-index=%t5.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORTNOREADONLY
 ; IMPORTNOREADONLY: @baz.llvm.{{.*}} = available_externally hidden global i32 10
 ; IMPORTNOREADONLY: @baz.llvm.{{.*}} = available_externally hidden global i32 10
 
+;; Do this again but where the 3rd module calls local function baz.
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3.ll -o %t3.bc
+; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3.bc
+; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t.bc -thinlto-index=%t5.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORTNOREADONLY
+
 ; ModuleID = 'local_name_conflict_var_main.o'
 source_filename = "local_name_conflict_var_main.c"
 target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"

>From 37cd49ab150fced2698780cf5bac7aaca777a7fb Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson at google.com>
Date: Fri, 30 Jan 2026 08:46:45 -0800
Subject: [PATCH 3/5] Minor fix to test to match the order of the comments

---
 llvm/test/ThinLTO/X86/local_name_conflict_var.ll | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)

diff --git a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
index 6a4d1352e4e2c..eecd01ec7fa09 100644
--- a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
+++ b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
@@ -20,15 +20,15 @@
 ;; summaries for baz as non-read only, which should cause the linkage type of
 ;; the imported copies of baz variables to be available_externally instead of
 ;; internal.
-; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3b.ll -o %t3b.bc
-; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3b.bc
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3b.ll -o %t3.bc
+; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3.bc
 ; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t.bc -thinlto-index=%t5.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORTNOREADONLY
 ; IMPORTNOREADONLY: @baz.llvm.{{.*}} = available_externally hidden global i32 10
 ; IMPORTNOREADONLY: @baz.llvm.{{.*}} = available_externally hidden global i32 10
 
 ;; Do this again but where the 3rd module calls local function baz.
-; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3.ll -o %t3.bc
-; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3.bc
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3.ll -o %t3b.bc
+; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3b.bc
 ; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t.bc -thinlto-index=%t5.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORTNOREADONLY
 
 ; ModuleID = 'local_name_conflict_var_main.o'

>From 6e139393751c71eeccc6c1f0fe0c022d6fbdfe0d Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson at google.com>
Date: Fri, 30 Jan 2026 09:11:18 -0800
Subject: [PATCH 4/5] Complete the minor test fix from the last commit

---
 llvm/test/ThinLTO/X86/local_name_conflict_var.ll | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)

diff --git a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
index eecd01ec7fa09..af7f9a483fd2d 100644
--- a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
+++ b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
@@ -20,14 +20,14 @@
 ;; summaries for baz as non-read only, which should cause the linkage type of
 ;; the imported copies of baz variables to be available_externally instead of
 ;; internal.
-; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3b.ll -o %t3.bc
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3.ll -o %t3.bc
 ; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3.bc
 ; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t.bc -thinlto-index=%t5.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORTNOREADONLY
 ; IMPORTNOREADONLY: @baz.llvm.{{.*}} = available_externally hidden global i32 10
 ; IMPORTNOREADONLY: @baz.llvm.{{.*}} = available_externally hidden global i32 10
 
 ;; Do this again but where the 3rd module calls local function baz.
-; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3.ll -o %t3b.bc
+; RUN: opt -module-summary -module-hash %p/Inputs/local_name_conflict_var3b.ll -o %t3b.bc
 ; RUN: llvm-lto -thinlto-action=thinlink -o %t5.bc %t.bc %t1.bc %t2.bc %t3b.bc
 ; RUN: llvm-lto -thinlto-action=import -exported-symbol=main %t.bc -thinlto-index=%t5.bc -o - | llvm-dis -o - | FileCheck %s --check-prefix=IMPORTNOREADONLY
 

>From b89a2c679166569dfaba5d25f95e38a769a587f9 Mon Sep 17 00:00:00 2001
From: Teresa Johnson <tejohnson at google.com>
Date: Fri, 30 Jan 2026 10:38:19 -0800
Subject: [PATCH 5/5] Address comments and more test cleanup

---
 llvm/lib/IR/ModuleSummaryIndex.cpp                        | 4 +++-
 llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll  | 2 +-
 llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll | 2 +-
 llvm/test/ThinLTO/X86/local_name_conflict_var.ll          | 6 ++++--
 4 files changed, 9 insertions(+), 5 deletions(-)

diff --git a/llvm/lib/IR/ModuleSummaryIndex.cpp b/llvm/lib/IR/ModuleSummaryIndex.cpp
index 070d0a9ef5d7f..9a3bb970494a2 100644
--- a/llvm/lib/IR/ModuleSummaryIndex.cpp
+++ b/llvm/lib/IR/ModuleSummaryIndex.cpp
@@ -274,7 +274,9 @@ propagateAttributesToRefs(GlobalValueSummary *S,
 //      reference is not readonly
 //   d. clear WO attribute from variable referenced by a function when
 //      reference is not writeonly
-//   e. clear IsDSOLocal flag in every summary if any of them is false.
+//   e. clear RO and WO attributes from variables with the same GUID as
+//      a non-variable.
+//   f. clear IsDSOLocal flag in every summary if any of them is false.
 //
 //   Because of (c, d) we don't internalize variables read by function A
 //   and modified by function B.
diff --git a/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll b/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll
index 979b64ec7d77f..b2030e82aeccb 100644
--- a/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll
+++ b/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll
@@ -7,7 +7,7 @@ define internal i32 @baz() {
   ret i32 0
 }
 
-define i32 @b() {
+define i32 @c() {
 entry:
   %is_null = icmp eq i32 ()* @baz, null
   ret i32 0
diff --git a/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll b/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll
index 7898e12cb5907..6811f01dfc56b 100644
--- a/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll
+++ b/llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll
@@ -7,7 +7,7 @@ define internal i32 @baz() {
   ret i32 0
 }
 
-define i32 @b() {
+define i32 @c() {
 entry:
   %call1 = call i32 (...) @baz()
   ret i32 0
diff --git a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
index af7f9a483fd2d..b57d3d31f75f3 100644
--- a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
+++ b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
@@ -1,4 +1,4 @@
-; Test handling when two files with the same source file name contain
+; Test handling when multiple files with the same source file name contain
 ; static read only variables with the same name (which will have the same GUID
 ; in the combined index).
 
@@ -16,7 +16,7 @@
 ; IMPORT: @baz.llvm.{{.*}} = internal global i32 10
 
 ;; Now do the same but linking in a 3rd module, in which baz is a local function,
-;; which has a non-call reference to it in b(). We should correctly mark all
+;; which has a non-call reference to it in c(). We should correctly mark all
 ;; summaries for baz as non-read only, which should cause the linkage type of
 ;; the imported copies of baz variables to be available_externally instead of
 ;; internal.
@@ -41,8 +41,10 @@ define i32 @main() {
 entry:
   %call1 = call i32 (...) @a()
   %call2 = call i32 (...) @b()
+  %call3 = call i32 (...) @c()
   ret i32 0
 }
 
 declare i32 @a(...)
 declare i32 @b(...)
+declare i32 @c(...)



More information about the llvm-commits mailing list