[llvm] [IR] Provide array with poison-generating metadata IDs. (PR #123188)

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Thu Jan 16 08:30:03 PST 2025


https://github.com/fhahn updated https://github.com/llvm/llvm-project/pull/123188

>From 499d85383684adae439ee4bf446ff2261752d82b Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Wed, 15 Jan 2025 21:44:46 +0000
Subject: [PATCH 1/2] [IR] Provide array with poison-generating metadata IDs.

Add Instruction::PoisonGeneratingMetadataIDs containing IDs of
poison-generating metadata to allow easier re-use.

Currently it is a static const array in Instruction, maybe here's a
better place?
---
 llvm/include/llvm/IR/Instruction.h |  4 ++++
 llvm/lib/IR/Instruction.cpp        | 10 ++++------
 2 files changed, 8 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h
index aa480aa8d98636..645aa57d053e2e 100644
--- a/llvm/include/llvm/IR/Instruction.h
+++ b/llvm/include/llvm/IR/Instruction.h
@@ -503,6 +503,10 @@ class Instruction : public User,
   /// Determine whether the the nneg flag is set.
   bool hasNonNeg() const LLVM_READONLY;
 
+  /// Metadata IDs that may generate poison.
+  constexpr static const unsigned PoisonGeneratingMetadataIDs[] = {
+      LLVMContext::MD_range, LLVMContext::MD_nonnull, LLVMContext::MD_align};
+
   /// Return true if this operator has flags which may cause this instruction
   /// to evaluate to poison despite having non-poison inputs.
   bool hasPoisonGeneratingFlags() const LLVM_READONLY;
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 147cd84125c8d1..12aefd8b1e96c1 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -458,9 +458,8 @@ void Instruction::dropPoisonGeneratingFlags() {
 }
 
 bool Instruction::hasPoisonGeneratingMetadata() const {
-  return hasMetadata(LLVMContext::MD_range) ||
-         hasMetadata(LLVMContext::MD_nonnull) ||
-         hasMetadata(LLVMContext::MD_align);
+  return any_of(PoisonGeneratingMetadataIDs,
+                [this](unsigned ID) { return hasMetadata(ID); });
 }
 
 bool Instruction::hasNonDebugLocLoopMetadata() const {
@@ -487,9 +486,8 @@ bool Instruction::hasNonDebugLocLoopMetadata() const {
 }
 
 void Instruction::dropPoisonGeneratingMetadata() {
-  eraseMetadata(LLVMContext::MD_range);
-  eraseMetadata(LLVMContext::MD_nonnull);
-  eraseMetadata(LLVMContext::MD_align);
+  for (unsigned ID : PoisonGeneratingMetadataIDs)
+    eraseMetadata(ID);
 }
 
 bool Instruction::hasPoisonGeneratingReturnAttributes() const {

>From 51b24dcddee6b2374b70731cd821223411753426 Mon Sep 17 00:00:00 2001
From: Florian Hahn <flo at fhahn.com>
Date: Thu, 16 Jan 2025 16:29:20 +0000
Subject: [PATCH 2/2] !fixup move to Metadata::PoisonGeneratingIDs.

---
 llvm/include/llvm/IR/Instruction.h | 4 ----
 llvm/include/llvm/IR/Metadata.h    | 4 ++++
 llvm/lib/IR/Instruction.cpp        | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h
index 645aa57d053e2e..aa480aa8d98636 100644
--- a/llvm/include/llvm/IR/Instruction.h
+++ b/llvm/include/llvm/IR/Instruction.h
@@ -503,10 +503,6 @@ class Instruction : public User,
   /// Determine whether the the nneg flag is set.
   bool hasNonNeg() const LLVM_READONLY;
 
-  /// Metadata IDs that may generate poison.
-  constexpr static const unsigned PoisonGeneratingMetadataIDs[] = {
-      LLVMContext::MD_range, LLVMContext::MD_nonnull, LLVMContext::MD_align};
-
   /// Return true if this operator has flags which may cause this instruction
   /// to evaluate to poison despite having non-poison inputs.
   bool hasPoisonGeneratingFlags() const LLVM_READONLY;
diff --git a/llvm/include/llvm/IR/Metadata.h b/llvm/include/llvm/IR/Metadata.h
index df2384c5f6e69a..ec7d030a20de8c 100644
--- a/llvm/include/llvm/IR/Metadata.h
+++ b/llvm/include/llvm/IR/Metadata.h
@@ -138,6 +138,10 @@ class Metadata {
   void printAsOperand(raw_ostream &OS, ModuleSlotTracker &MST,
                       const Module *M = nullptr) const;
   /// @}
+
+  /// Metadata IDs that may generate poison.
+  constexpr static const unsigned PoisonGeneratingIDs[] = {
+      LLVMContext::MD_range, LLVMContext::MD_nonnull, LLVMContext::MD_align};
 };
 
 // Create wrappers for C Binding types (see CBindingWrapping.h).
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 12aefd8b1e96c1..9eaae62a6390b7 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -458,7 +458,7 @@ void Instruction::dropPoisonGeneratingFlags() {
 }
 
 bool Instruction::hasPoisonGeneratingMetadata() const {
-  return any_of(PoisonGeneratingMetadataIDs,
+  return any_of(Metadata::PoisonGeneratingIDs,
                 [this](unsigned ID) { return hasMetadata(ID); });
 }
 
@@ -486,7 +486,7 @@ bool Instruction::hasNonDebugLocLoopMetadata() const {
 }
 
 void Instruction::dropPoisonGeneratingMetadata() {
-  for (unsigned ID : PoisonGeneratingMetadataIDs)
+  for (unsigned ID : Metadata::PoisonGeneratingIDs)
     eraseMetadata(ID);
 }
 



More information about the llvm-commits mailing list