[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
Mon Dec 16 04:43:17 PST 2024
https://github.com/dominik-steenken updated https://github.com/llvm/llvm-project/pull/118825
>From db64757fc1c83b2273488ad8a0e5adbe49d4a620 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] 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..6c19712481ca9a 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))
More information about the llvm-commits
mailing list