[llvm] Only guard loop metadata that has non-debug info in it (PR #118825)
Dominik Steenken via llvm-commits
llvm-commits at lists.llvm.org
Wed Dec 18 01:13:29 PST 2024
https://github.com/dominik-steenken updated https://github.com/llvm/llvm-project/pull/118825
>From 8ec819e62c22c6086837eb653ae91e76b0e0842b Mon Sep 17 00:00:00 2001
From: Dominik Steenken <dost at de.ibm.com>
Date: Mon, 25 Nov 2024 14:30:01 +0100
Subject: [PATCH 1/2] Only guard loop metadata that has non-debug info in it
---
llvm/include/llvm/IR/Instruction.h | 4 ++++
llvm/lib/IR/Instruction.cpp | 25 +++++++++++++++++++++++++
llvm/lib/Transforms/Utils/Local.cpp | 2 +-
3 files changed, 30 insertions(+), 1 deletion(-)
diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h
index 730baa8cc00520..ea3d93d856efb7 100644
--- a/llvm/include/llvm/IR/Instruction.h
+++ b/llvm/include/llvm/IR/Instruction.h
@@ -367,6 +367,10 @@ class Instruction : public User,
/// Return true if this instruction has any metadata attached to it.
bool hasMetadata() const { return DbgLoc || Value::hasMetadata(); }
+ // Return true if this instruction contains loop metadata other than
+ // a debug location
+ bool hasLoopMetadataOtherThanDebugLoc() const;
+
/// Return true if this instruction has metadata attached to it other than a
/// debug location.
bool hasMetadataOtherThanDebugLoc() const { return Value::hasMetadata(); }
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 0137bb281e7ec5..8d02bfe6ea9fc7 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -12,6 +12,7 @@
#include "llvm/IR/Instruction.h"
#include "llvm/ADT/DenseSet.h"
+#include "llvm/ADT/STLExtras.h"
#include "llvm/IR/AttributeMask.h"
#include "llvm/IR/Attributes.h"
#include "llvm/IR/Constants.h"
@@ -19,6 +20,7 @@
#include "llvm/IR/Instructions.h"
#include "llvm/IR/IntrinsicInst.h"
#include "llvm/IR/Intrinsics.h"
+#include "llvm/IR/LLVMContext.h"
#include "llvm/IR/MemoryModelRelaxationAnnotations.h"
#include "llvm/IR/Module.h"
#include "llvm/IR/Operator.h"
@@ -461,6 +463,29 @@ bool Instruction::hasPoisonGeneratingMetadata() const {
hasMetadata(LLVMContext::MD_align);
}
+bool Instruction::hasLoopMetadataOtherThanDebugLoc() const {
+ // If there is no loop metadata at all, we also don't have
+ // non-debug loop metadata, obviously.
+ if (!hasMetadata(LLVMContext::MD_loop))
+ return false;
+
+ // If we do have loop metadata, retrieve it.
+ MDNode *LoopMD = getMetadata(LLVMContext::MD_loop);
+
+ // Check if the existing operands are debug locations. This loop
+ // should terminate after at most three iterations. Skip
+ // the first item because it is a self-reference.
+ for (const MDOperand &Op : llvm::drop_begin(LoopMD->operands())) {
+ // check for debug location type by attempting a cast.
+ if (!dyn_cast<DILocation>(Op)) {
+ return true;
+ }
+ }
+
+ // If we get here, then all we have is debug locations in the loop metadata.
+ return false;
+}
+
void Instruction::dropPoisonGeneratingMetadata() {
eraseMetadata(LLVMContext::MD_range);
eraseMetadata(LLVMContext::MD_nonnull);
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index cdc3f0308fe59c..876017e9051067 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1279,7 +1279,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
// | for.body <---- (md2)
// |_______| |______|
if (Instruction *TI = BB->getTerminator())
- if (TI->hasMetadata(LLVMContext::MD_loop))
+ if (TI->hasLoopMetadataOtherThanDebugLoc())
for (BasicBlock *Pred : predecessors(BB))
if (Instruction *PredTI = Pred->getTerminator())
if (PredTI->hasMetadata(LLVMContext::MD_loop))
>From e4f2a4e83217046fdef69b1b3401f63edddb1848 Mon Sep 17 00:00:00 2001
From: Dominik Steenken <dost at de.ibm.com>
Date: Wed, 18 Dec 2024 09:59:51 +0100
Subject: [PATCH 2/2] Rename function per review
---
llvm/include/llvm/IR/Instruction.h | 2 +-
llvm/lib/IR/Instruction.cpp | 2 +-
llvm/lib/Transforms/Utils/Local.cpp | 2 +-
3 files changed, 3 insertions(+), 3 deletions(-)
diff --git a/llvm/include/llvm/IR/Instruction.h b/llvm/include/llvm/IR/Instruction.h
index ea3d93d856efb7..aa480aa8d98636 100644
--- a/llvm/include/llvm/IR/Instruction.h
+++ b/llvm/include/llvm/IR/Instruction.h
@@ -369,7 +369,7 @@ class Instruction : public User,
// Return true if this instruction contains loop metadata other than
// a debug location
- bool hasLoopMetadataOtherThanDebugLoc() const;
+ bool hasNonDebugLocLoopMetadata() const;
/// Return true if this instruction has metadata attached to it other than a
/// debug location.
diff --git a/llvm/lib/IR/Instruction.cpp b/llvm/lib/IR/Instruction.cpp
index 8d02bfe6ea9fc7..147cd84125c8d1 100644
--- a/llvm/lib/IR/Instruction.cpp
+++ b/llvm/lib/IR/Instruction.cpp
@@ -463,7 +463,7 @@ bool Instruction::hasPoisonGeneratingMetadata() const {
hasMetadata(LLVMContext::MD_align);
}
-bool Instruction::hasLoopMetadataOtherThanDebugLoc() const {
+bool Instruction::hasNonDebugLocLoopMetadata() const {
// If there is no loop metadata at all, we also don't have
// non-debug loop metadata, obviously.
if (!hasMetadata(LLVMContext::MD_loop))
diff --git a/llvm/lib/Transforms/Utils/Local.cpp b/llvm/lib/Transforms/Utils/Local.cpp
index 876017e9051067..1c6a7ba3f72559 100644
--- a/llvm/lib/Transforms/Utils/Local.cpp
+++ b/llvm/lib/Transforms/Utils/Local.cpp
@@ -1279,7 +1279,7 @@ bool llvm::TryToSimplifyUncondBranchFromEmptyBlock(BasicBlock *BB,
// | for.body <---- (md2)
// |_______| |______|
if (Instruction *TI = BB->getTerminator())
- if (TI->hasLoopMetadataOtherThanDebugLoc())
+ if (TI->hasNonDebugLocLoopMetadata())
for (BasicBlock *Pred : predecessors(BB))
if (Instruction *PredTI = Pred->getTerminator())
if (PredTI->hasMetadata(LLVMContext::MD_loop))
More information about the llvm-commits
mailing list