[llvm] 3493e5d - [ThinLTO] Clear read/write only flags when referencing function summary (#178761)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Jan 30 11:08:26 PST 2026
Author: Teresa Johnson
Date: 2026-01-30T11:08:20-08:00
New Revision: 3493e5d9247f1aeb0603409a6f0af67e89fc4c0f
URL: https://github.com/llvm/llvm-project/commit/3493e5d9247f1aeb0603409a6f0af67e89fc4c0f
DIFF: https://github.com/llvm/llvm-project/commit/3493e5d9247f1aeb0603409a6f0af67e89fc4c0f.diff
LOG: [ThinLTO] Clear read/write only flags when referencing function summary (#178761)
Conservatively clear read/write only flags during attribute propagation
across all of a ValueInfo's summaries when one is a function summary.
While function summaries will not be read/write-only, we need to ensure
that
any global variable summary with the same GUID/ValueInfo is also not
marked
read or write only. This case can occur with same named locals in
different
modules compiled without enough path.
Enhance the current test to confirm this works correctly for a couple
cases.
This change enables a follow on simplification and compile time
improvement
during importing of global variables.
Added:
llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3.ll
llvm/test/ThinLTO/X86/Inputs/local_name_conflict_var3b.ll
Modified:
llvm/lib/IR/ModuleSummaryIndex.cpp
llvm/test/ThinLTO/X86/local_name_conflict_var.ll
Removed:
################################################################################
diff --git a/llvm/lib/IR/ModuleSummaryIndex.cpp b/llvm/lib/IR/ModuleSummaryIndex.cpp
index 33947542bcf16..9a3bb970494a2 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
diff erence 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);
+ }
}
}
@@ -251,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
new file mode 100644
index 0000000000000..b2030e82aeccb
--- /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 @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
new file mode 100644
index 0000000000000..6811f01dfc56b
--- /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 @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 ed5c6e4fd4f98..b57d3d31f75f3 100644
--- a/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
+++ b/llvm/test/ThinLTO/X86/local_name_conflict_var.ll
@@ -1,12 +1,12 @@
-; 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).
; 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,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,
+;; 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.
+; 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_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
+
; 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"
@@ -25,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