[llvm] [IR] Accept nonnull metadata on phi nodes (PR #202877)

Nikolas Klauser via llvm-commits llvm-commits at lists.llvm.org
Sun Jun 21 08:16:12 PDT 2026


https://github.com/philnik777 updated https://github.com/llvm/llvm-project/pull/202877

>From 10ec75be1a9da8ff0a05de4b7d92cc9cf78e44f3 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Wed, 10 Jun 2026 09:35:00 +0200
Subject: [PATCH 1/2] [IR] Accept nonnull metadata on phi nodes

---
 llvm/lib/IR/Verifier.cpp               |  6 +++---
 llvm/test/Verifier/nonnull_metadata.ll | 26 +++++++++++++++-----------
 2 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index f0363b6553440..73e627249129b 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -5924,9 +5924,9 @@ void Verifier::visitInstruction(Instruction &I) {
   if (MDNode *MD = I.getMetadata(LLVMContext::MD_nonnull)) {
     Check(I.getType()->isPointerTy(), "nonnull applies only to pointer types",
           &I);
-    Check(isa<LoadInst>(I),
-          "nonnull applies only to load instructions, use attributes"
-          " for calls or invokes",
+    Check((isa<LoadInst, PHINode>(I)),
+          "nonnull applies only to load instructions or phi nodes, use "
+          "attributes for calls or invokes",
           &I);
     Check(MD->getNumOperands() == 0, "nonnull metadata must be empty", &I);
   }
diff --git a/llvm/test/Verifier/nonnull_metadata.ll b/llvm/test/Verifier/nonnull_metadata.ll
index 9ef67dc163c59..73a1edfb698bc 100644
--- a/llvm/test/Verifier/nonnull_metadata.ll
+++ b/llvm/test/Verifier/nonnull_metadata.ll
@@ -2,20 +2,24 @@
 
 declare ptr @dummy()
 
-; CHECK: nonnull applies only to pointer types
-define void @test_not_pointer(ptr %p) {
+define void @test_not_pointer(ptr %p, i32 %i) {
+entry:
+  ; CHECK: nonnull applies only to pointer types
   load i32, ptr %p, !nonnull !{}
-  ret void
-}
-
-; CHECK: nonnull applies only to load instructions, use attributes for calls or invokes
-define void @test_not_load() {
+  ; CHECK: nonnull applies only to load instructions or phi nodes, use attributes for calls or invokes
   call ptr @dummy(), !nonnull !{}
+  ; CHECK: nonnull metadata must be empty
+  load ptr, ptr %p, !nonnull !{i32 0}
+  ; This one is valid
+  load ptr, ptr %p, !nonnull !{}
   ret void
-}
 
-; CHECK: nonnull metadata must be empty
-define void @test_invalid_arg(ptr %p) {
-  load ptr, ptr %p, !nonnull !{i32 0}
+bb:
+  ; This one is valid
+  phi ptr [%p, %entry], !nonnull !{}
+  ; CHECK: nonnull metadata must be empty
+  phi ptr [%p, %entry], !nonnull !{i32 0}
+  ; CHECK: nonnull applies only to pointer types
+  phi i32 [%i, %entry], !nonnull !{}
   ret void
 }

>From 2dec5d4b251e2a7116b93291eacc83db0ce0af80 Mon Sep 17 00:00:00 2001
From: Nikolas Klauser <nikolasklauser at berlin.de>
Date: Sun, 21 Jun 2026 17:15:57 +0200
Subject: [PATCH 2/2] Address comments

---
 llvm/lib/IR/Verifier.cpp                 | 10 ++++++----
 llvm/test/Verifier/nofpclass-metadata.ll | 12 +++++++++++-
 llvm/test/Verifier/noundef-metadata.ll   | 14 +++++++++++++-
 llvm/test/Verifier/range-metadata.ll     | 13 +++++++++++--
 4 files changed, 41 insertions(+), 8 deletions(-)

diff --git a/llvm/lib/IR/Verifier.cpp b/llvm/lib/IR/Verifier.cpp
index 097f401060b24..4cf212c92ced9 100644
--- a/llvm/lib/IR/Verifier.cpp
+++ b/llvm/lib/IR/Verifier.cpp
@@ -5994,13 +5994,14 @@ void Verifier::visitInstruction(Instruction &I) {
   }
 
   if (MDNode *Range = I.getMetadata(LLVMContext::MD_range)) {
-    Check(isa<LoadInst>(I) || isa<CallInst>(I) || isa<InvokeInst>(I),
-          "Ranges are only for loads, calls and invokes!", &I);
+    Check((isa<LoadInst, CallInst, InvokeInst, PHINode>(I)),
+          "Ranges are only for loads, calls, invokes and phi nodes!", &I);
     visitRangeMetadata(I, Range, I.getType());
   }
 
   if (MDNode *MD = I.getMetadata(LLVMContext::MD_nofpclass)) {
-    Check(isa<LoadInst>(I), "nofpclass is only for loads", &I);
+    Check((isa<LoadInst, PHINode>(I)),
+          "nofpclass is only for loads and phi nodes", &I);
     visitNoFPClassMetadata(I, MD, I.getType());
   }
 
@@ -6027,7 +6028,8 @@ void Verifier::visitInstruction(Instruction &I) {
   }
 
   if (MDNode *MD = I.getMetadata(LLVMContext::MD_noundef)) {
-    Check(isa<LoadInst>(I), "noundef applies only to load instructions", &I);
+    Check((isa<LoadInst, PHINode>(I)),
+          "noundef applies only to load instructions or phi nodes", &I);
     Check(MD->getNumOperands() == 0, "noundef metadata must be empty", &I);
   }
 
diff --git a/llvm/test/Verifier/nofpclass-metadata.ll b/llvm/test/Verifier/nofpclass-metadata.ll
index 0acd6e240cecc..beb7522746a6e 100644
--- a/llvm/test/Verifier/nofpclass-metadata.ll
+++ b/llvm/test/Verifier/nofpclass-metadata.ll
@@ -4,7 +4,8 @@ declare float @func()
 
 %struct = type { i32, float }
 
-define void @test(ptr %ptr) {
+define void @test(ptr %ptr, float %f) {
+entry:
   ; CHECK: nofpclass is only for loads
   call float @func(), !nofpclass !{i32 3}
 
@@ -41,5 +42,14 @@ define void @test(ptr %ptr) {
   ; CHECK: Invalid value for 'nofpclass' test mask
   load float, ptr %ptr, align 4, !nofpclass !{i32 1024}
 
+  ret void
+
+bb:
+  ; This one is valid
+  phi float [%f, %entry], !nofpclass !{i32 3}
+
+  ; CHECK: nofpclass must have exactly one entry
+  phi float [%f, %entry], !nofpclass !{}
+
   ret void
 }
diff --git a/llvm/test/Verifier/noundef-metadata.ll b/llvm/test/Verifier/noundef-metadata.ll
index 9d5e959e9fc32..47160dda8b636 100644
--- a/llvm/test/Verifier/noundef-metadata.ll
+++ b/llvm/test/Verifier/noundef-metadata.ll
@@ -1,12 +1,24 @@
 ; RUN: not llvm-as < %s 2>&1 | FileCheck %s
 
-define void @test(ptr %ptr) {
+define void @test(ptr %ptr, i32 %i) {
 entry:
   ; This one is valid
   load i32, ptr %ptr, !noundef !{}
+
   ; CHECK: noundef metadata must be empty
   load i32, ptr %ptr, !noundef !{i32 0}
+
   ; CHECK: noundef applies only to load instructions
   store i32 0, ptr %ptr, !noundef !{}
+
+  ret void
+
+bb:
+  ; This one is valid
+  phi i32 [%i, %entry], !noundef !{}
+
+  ; CHECK: noundef metadata must be empty
+  phi i32 [%i, %entry], !noundef !{i32 0}
+
   ret void
 }
diff --git a/llvm/test/Verifier/range-metadata.ll b/llvm/test/Verifier/range-metadata.ll
index 00cc604769116..2e18044b7f983 100644
--- a/llvm/test/Verifier/range-metadata.ll
+++ b/llvm/test/Verifier/range-metadata.ll
@@ -2,9 +2,9 @@
 
 declare i8 @func()
 
-define void @test(ptr %x) {
+define void @test(ptr %x, i32 %i) {
 entry:
-  ; CHECK: Ranges are only for loads, calls and invokes!
+  ; CHECK: Ranges are only for loads, calls, invokes and phi nodes!
   store i8 0, ptr %x, align 1, !range !{i8 0, i8 1}
 
   ; CHECK: It should have at least one range!
@@ -64,5 +64,14 @@ entry:
   ; CHECK: The upper and lower limits cannot be the same value{{$}}
   load i32, ptr %x, !range !{i32 123, i32 123}
 
+  ret void
+
+bb:
+  ; This one is valid
+  phi i32 [%i, %entry], !range !{i32 0, i32 1}
+
+  ; CHECK: It should have at least one range!
+  phi i32 [%i, %entry], !range !{}
+
   ret void
 }



More information about the llvm-commits mailing list