Skip to content

[IR] Provide array with poison-generating metadata IDs. #123188

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 16, 2025

Conversation

fhahn
Copy link
Contributor

@fhahn fhahn commented Jan 16, 2025

Add Metadata::PoisonGeneratingIDs containing IDs of poison-generating metadata to allow easier re-use.

@llvmbot
Copy link
Member

llvmbot commented Jan 16, 2025

@llvm/pr-subscribers-llvm-transforms

@llvm/pr-subscribers-llvm-ir

Author: Florian Hahn (fhahn)

Changes

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?


Full diff: https://github.com/llvm/llvm-project/pull/123188.diff

2 Files Affected:

  • (modified) llvm/include/llvm/IR/Instruction.h (+4)
  • (modified) llvm/lib/IR/Instruction.cpp (+4-6)
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 {

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Metadata::PoisonGeneratingIDs maybe? No strong opinion.

@fhahn fhahn force-pushed the PoisonGeneratingMetadataIDs branch from a679a5b to 51b24dc Compare January 16, 2025 16:29
@fhahn
Copy link
Contributor Author

fhahn commented Jan 16, 2025

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?

Metadata::PoisonGeneratingIDs maybe? No strong opinion.

Moved there, as it is overall a bit more compact when used outside Instruction:: :)

Copy link
Contributor

@nikic nikic left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Copy link
Member

@dtcxzyw dtcxzyw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can also use this array in ArgumentPromotion:

// Only transfer poison-generating metadata if we also have
// !noundef.
// TODO: Without !noundef, we could merge this metadata across
// all promoted loads.
if (LI->hasMetadata(LLVMContext::MD_noundef))
LI->copyMetadata(*Pair.second.MustExecInstr,
{LLVMContext::MD_range, LLVMContext::MD_nonnull,
LLVMContext::MD_align});
}

fhahn added 3 commits January 16, 2025 18:21
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?
@fhahn fhahn force-pushed the PoisonGeneratingMetadataIDs branch from 51b24dc to e37db0a Compare January 16, 2025 18:22
@fhahn
Copy link
Contributor Author

fhahn commented Jan 16, 2025

We can also use this array in ArgumentPromotion:

// Only transfer poison-generating metadata if we also have
// !noundef.
// TODO: Without !noundef, we could merge this metadata across
// all promoted loads.
if (LI->hasMetadata(LLVMContext::MD_noundef))
LI->copyMetadata(*Pair.second.MustExecInstr,
{LLVMContext::MD_range, LLVMContext::MD_nonnull,
LLVMContext::MD_align});
}

Done, thanks!

@fhahn fhahn merged commit 8c75ecb into llvm:main Jan 16, 2025
7 of 9 checks passed
@fhahn fhahn deleted the PoisonGeneratingMetadataIDs branch January 16, 2025 20:46
github-actions bot pushed a commit to arm/arm-toolchain that referenced this pull request Jan 16, 2025
…123188)

Add Metadata::PoisonGeneratingIDs containing IDs of poison-generating
metadata to allow easier re-use.

PR: llvm/llvm-project#123188
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants