[PATCH] D118168: [LLVM] Introduce llvm.loop.finite metadata to represent loops which are known to iterate a finite number of times

William Moses via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 25 11:07:33 PST 2022


wsmoses created this revision.
wsmoses added reviewers: jdoerfert, fhahn, nikic, reames.
Herald added a subscriber: hiraditya.
wsmoses requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Introduce a new loop metadata llvm.loop.finite which specifies that the loop is known to iterate a finite number of times. This is a stronger form of llvm.loop.mustprogress and useful for various analyses, such as scalar evolution (see: https://reviews.llvm.org/D118090)


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D118168

Files:
  llvm/docs/LangRef.rst
  llvm/include/llvm/Analysis/LoopInfo.h
  llvm/lib/Analysis/LoopInfo.cpp


Index: llvm/lib/Analysis/LoopInfo.cpp
===================================================================
--- llvm/lib/Analysis/LoopInfo.cpp
+++ llvm/lib/Analysis/LoopInfo.cpp
@@ -564,6 +564,22 @@
   setLoopID(NewLoopID);
 }
 
+void Loop::setLoopFinite() {
+  LLVMContext &Context = getHeader()->getContext();
+
+  MDNode *Finite = findOptionMDForLoop(this, "llvm.loop.finite");
+
+  if (Finite)
+    return;
+
+  MDNode *FiniteMD =
+      MDNode::get(Context, MDString::get(Context, "llvm.loop.finite"));
+  MDNode *LoopID = getLoopID();
+  MDNode *NewLoopID =
+      makePostTransformationMetadata(Context, LoopID, {}, {FiniteMD});
+  setLoopID(NewLoopID);
+}
+
 bool Loop::isAnnotatedParallel() const {
   MDNode *DesiredLoopIdMetadata = getLoopID();
 
@@ -1107,6 +1123,12 @@
   return getOptionalIntLoopAttribute(TheLoop, Name).getValueOr(Default);
 }
 
+static const char *LLVMLoopFinite = "llvm.loop.finite";
+
+bool llvm::hasFinite(const Loop *L) {
+  return getBooleanLoopAttribute(L, LLVMLoopFinite);
+}
+
 static const char *LLVMLoopMustProgress = "llvm.loop.mustprogress";
 
 bool llvm::hasMustProgress(const Loop *L) {
@@ -1114,7 +1136,8 @@
 }
 
 bool llvm::isMustProgress(const Loop *L) {
-  return L->getHeader()->getParent()->mustProgress() || hasMustProgress(L);
+  return L->getHeader()->getParent()->mustProgress() || hasMustProgress(L) ||
+         hasFinite(L);
 }
 
 bool llvm::isValidAsAccessGroup(MDNode *Node) {
Index: llvm/include/llvm/Analysis/LoopInfo.h
===================================================================
--- llvm/include/llvm/Analysis/LoopInfo.h
+++ llvm/include/llvm/Analysis/LoopInfo.h
@@ -853,6 +853,9 @@
   /// Add llvm.loop.mustprogress to this loop's loop id metadata.
   void setLoopMustProgress();
 
+  /// Add llvm.loop.finite to this loop's loop id metadata.
+  void setLoopFinite();
+
   void dump() const;
   void dumpVerbose() const;
 
@@ -1332,6 +1335,9 @@
 /// the containing function attribute too.
 bool hasMustProgress(const Loop *L);
 
+/// Look for the loop attribute that requires the loop to be finite.
+bool hasFinite(const Loop *L);
+
 /// Return true if this loop can be assumed to make progress.  (i.e. can't
 /// be infinite without side effects without also being undefined)
 bool isMustProgress(const Loop *L);
Index: llvm/docs/LangRef.rst
===================================================================
--- llvm/docs/LangRef.rst
+++ llvm/docs/LangRef.rst
@@ -6873,6 +6873,14 @@
 not found to interact with the environment in an observable way, the loop may
 be removed. This corresponds to the ``mustprogress`` function attribute.
 
+'``llvm.loop.finite``' Metadata
+^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
+
+The ``llvm.loop.finite`` metadata indicates that this loop is finite. The loop
+attached must terminate. The reason for termination is unspecified. This is a
+ stronger form of ``llvm.loop.mustprogress`` which simply says that the loop
+either may terminate, unwind or interact with the environment.
+
 '``irr_loop``' Metadata
 ^^^^^^^^^^^^^^^^^^^^^^^
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D118168.402975.patch
Type: text/x-patch
Size: 3041 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220125/e883674a/attachment.bin>


More information about the llvm-commits mailing list