[llvm] 639e8db - [ConstantFold] Don't let noipa block global address equality folding (#228186)

via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 5 12:48:18 PDT 2026


Author: David Blaikie
Date: 2026-10-05T19:48:10Z
New Revision: 639e8db90b6ffbfe9937b7e1e10c8a066d736916

URL: https://github.com/llvm/llvm-project/commit/639e8db90b6ffbfe9937b7e1e10c8a066d736916
DIFF: https://github.com/llvm/llvm-project/commit/639e8db90b6ffbfe9937b7e1e10c8a066d736916.diff

LOG: [ConstantFold] Don't let noipa block global address equality folding (#228186)

When deciding whether two globals may have the same address, globals
that may be replaced at link time are treated as unsafe.
`isInterposable()`
also returns true for `noipa` function definitions by default, but
`noipa` doesn't change which definition (and therefore which address)
a function resolves to. Query `isInterposable(/*CheckNoIPA=*/false)` so
comparisons involving `noipa` functions fold like any other function
with the same linkage.

This is one of a few refinements of noipa identified while working on
making optnone imply noipa - without these refinements,
optnone-implies-noipa, might substantially change clang -O0 codegen. I'm
open to discussing whether these refinements are the right direction,
though.

Assisted-By: Claude

Added: 
    llvm/test/Transforms/InstSimplify/ConstProp/icmp-global-noipa.ll

Modified: 
    llvm/lib/IR/ConstantFold.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/IR/ConstantFold.cpp b/llvm/lib/IR/ConstantFold.cpp
index f8abd576f93c4..d19100931d4d9 100644
--- a/llvm/lib/IR/ConstantFold.cpp
+++ b/llvm/lib/IR/ConstantFold.cpp
@@ -996,7 +996,9 @@ Constant *llvm::ConstantFoldBinaryInstruction(unsigned Opcode, Constant *C1,
 static ICmpInst::Predicate areGlobalsPotentiallyEqual(const GlobalValue *GV1,
                                                       const GlobalValue *GV2) {
   auto isGlobalUnsafeForEquality = [](const GlobalValue *GV) {
-    if (GV->isInterposable() || GV->hasGlobalUnnamedAddr())
+    // noipa doesn't affect which definition (and so which address) a global
+    // resolves to, so it's ignored here.
+    if (GV->isInterposable(/*CheckNoIPA=*/false) || GV->hasGlobalUnnamedAddr())
       return true;
     if (const auto *GVar = dyn_cast<GlobalVariable>(GV)) {
       Type *Ty = GVar->getValueType();

diff  --git a/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global-noipa.ll b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global-noipa.ll
new file mode 100644
index 0000000000000..eb6537ecaa8c6
--- /dev/null
+++ b/llvm/test/Transforms/InstSimplify/ConstProp/icmp-global-noipa.ll
@@ -0,0 +1,61 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 6
+; RUN: opt < %s -passes=instsimplify -S | FileCheck %s
+
+; noipa doesn't affect which definition (and so which address) a function
+; resolves to, so comparisons of noipa function addresses fold just like those
+; of other functions with the same linkage.
+
+define void @f() {
+; CHECK-LABEL: define void @f() {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define void @g() {
+; CHECK-LABEL: define void @g() {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define void @noipa_f() noipa {
+; CHECK-LABEL: define void @noipa_f(
+; CHECK-SAME: ) #[[ATTR0:[0-9]+]] {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define weak void @weak_f() {
+; CHECK-LABEL: define weak void @weak_f() {
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define i1 @plain_eq() {
+; CHECK-LABEL: define i1 @plain_eq() {
+; CHECK-NEXT:    ret i1 false
+;
+  %cmp = icmp eq ptr @f, @g
+  ret i1 %cmp
+}
+
+define i1 @noipa_eq() {
+; CHECK-LABEL: define i1 @noipa_eq() {
+; CHECK-NEXT:    ret i1 false
+;
+  %cmp = icmp eq ptr @noipa_f, @g
+  ret i1 %cmp
+}
+
+; Interposable functions may resolve to the same address, so don't fold.
+define i1 @weak_eq() {
+; CHECK-LABEL: define i1 @weak_eq() {
+; CHECK-NEXT:    [[CMP:%.*]] = icmp eq ptr @weak_f, @g
+; CHECK-NEXT:    ret i1 [[CMP]]
+;
+  %cmp = icmp eq ptr @weak_f, @g
+  ret i1 %cmp
+}


        


More information about the llvm-commits mailing list