[llvm] [Verifier] Allow too big unused sectioned global variables with local linkage (PR #212733)

Steffen Larsen via llvm-commits llvm-commits at lists.llvm.org
Sun Aug 2 22:00:02 PDT 2026


https://github.com/steffenlarsen updated https://github.com/llvm/llvm-project/pull/212733

>From eefb383facd3e6ff190bd4914143698140a04eb4 Mon Sep 17 00:00:00 2001
From: Steffen Holst Larsen <sholstla at amd.com>
Date: Wed, 29 Jul 2026 05:35:45 -0500
Subject: [PATCH 1/2] [Verifier] Allow too big unused sectioned global
 variables with local linkage

This commit adjusts the check added in
https://github.com/llvm/llvm-project/pull/179625 to allow global
variables that are too big for their address space if they are unused,
have local linkage, and have a section. This allows metadata embedded in
global variables, like __clang_ast, to exceed the limits.

Signed-off-by: Steffen Holst Larsen <sholstla at amd.com>
---
 llvm/lib/IR/Verifier.cpp                 |  5 ++++-
 llvm/test/Verifier/global-var-too-big.ll | 17 +++++++++++++++++
 2 files changed, 21 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 90ad2e5bc8f68..f523fa869a27e 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -812,10 +812,13 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
         "Global @" + GV.getName() + " has illegal target extension type",
         GVType);
 
-  // Check that the the address space can hold all bits of the type, recognized
+  // Check that the address space can hold all bits of the type, recognized
   // by an access in the address space being able to reach all bytes of the
   // type.
+  // An exemption to this is sectioned global variables with local linkage
+  // and no uses. These are usually used for metadata.
   Check(!GVType->isSized() ||
+            (GV.use_empty() && GV.hasLocalLinkage() && GV.hasSection()) ||
             isUIntN(DL.getAddressSizeInBits(GV.getAddressSpace()),
                     GV.getGlobalSize(DL)),
         "Global variable is too large to fit into the address space", &GV,
diff --git a/llvm/test/Verifier/global-var-too-big.ll b/llvm/test/Verifier/global-var-too-big.ll
index fecb021145ae1..cd5c4bba7f78c 100644
--- a/llvm/test/Verifier/global-var-too-big.ll
+++ b/llvm/test/Verifier/global-var-too-big.ll
@@ -12,10 +12,27 @@ target datalayout = "e-m:e-p1:16:16-p2:32:32-p3:64:64-i8:8-i32:32-i64:64"
 @G3 = internal addrspace(1) global [65535 x i8] zeroinitializer, align 4
 @G4 = internal addrspace(2) global [2147483647 x i16] zeroinitializer, align 4
 
+; Too large but exempt: unreferenced, local linkage, has a section.
+ at G5 = internal addrspace(1) global [65536 x i8] zeroinitializer, section "some_section_1", align 4
+ at G6 = private addrspace(1) global [65536 x i8] zeroinitializer, section "some_section_2", align 4
+
+; Too large, has a section, but has a use so not exempt.
+ at G7 = internal addrspace(1) global [65536 x i8] zeroinitializer, section "some_section_3", align 4
+ at G7_user = global ptr addrspace(1) @G7
+
+; Too large, unreferenced, has a section, but external linkage so not exempt.
+ at G8 = addrspace(1) global [65536 x i8] zeroinitializer, section "some_section_4", align 4
+
 ; CHECK: Global variable is too large to fit into the address space
 ; CHECK-NEXT: ptr addrspace(1) @G1
 ; CHECK-NEXT: [65536 x i8]
 ; CHECK: Global variable is too large to fit into the address space
 ; CHECK-NEXT: ptr addrspace(2) @G2
 ; CHECK-NEXT: [2147483648 x i16]
+; CHECK: Global variable is too large to fit into the address space
+; CHECK-NEXT: ptr addrspace(1) @G7
+; CHECK-NEXT: [65536 x i8]
+; CHECK: Global variable is too large to fit into the address space
+; CHECK-NEXT: ptr addrspace(1) @G8
+; CHECK-NEXT: [65536 x i8]
 ; CHECK-NOT: Global variable is too large to fit into the address space

>From 2286c5bb02e777bb1b9803ba267668b03f704643 Mon Sep 17 00:00:00 2001
From: Steffen Holst Larsen <sholstla at amd.com>
Date: Thu, 30 Jul 2026 00:25:00 -0500
Subject: [PATCH 2/2] Use materialized_use_empty

Signed-off-by: Steffen Holst Larsen <sholstla at amd.com>
---
 llvm/lib/IR/Verifier.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index f523fa869a27e..929b044ad0088 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -818,7 +818,8 @@ void Verifier::visitGlobalVariable(const GlobalVariable &GV) {
   // An exemption to this is sectioned global variables with local linkage
   // and no uses. These are usually used for metadata.
   Check(!GVType->isSized() ||
-            (GV.use_empty() && GV.hasLocalLinkage() && GV.hasSection()) ||
+            (GV.hasLocalLinkage() && GV.hasSection() &&
+             GV.materialized_use_empty()) ||
             isUIntN(DL.getAddressSizeInBits(GV.getAddressSpace()),
                     GV.getGlobalSize(DL)),
         "Global variable is too large to fit into the address space", &GV,



More information about the llvm-commits mailing list