[PATCH] D94106: [Local] Treat calls that may not return as being alive (WIP).

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 5 11:22:27 PST 2021


fhahn created this revision.
fhahn added reviewers: jdoerfert, efriedma, aaron.ballman, rjmccall, atmnpatel, RalfJung, aqjune.
Herald added subscribers: dexonsmith, hiraditya.
fhahn requested review of this revision.
Herald added a project: LLVM.

With the addition of the `mustprogress` attribute, functions that may
not return (e.g. due to an infinite loop) are well defined, if they are
not marked as `mustprogress`.

This patch updates `wouldInstructionBeTriviallyDead` to not consider
calls that may not return as dead, unless they are in mustprogress
functions.

This has the potential to be quite disruptive. We could just limit it to
functions as marked noreturn as a start. But it might be good to make
this apply widely, so issues are uncovered quickly and frontends will be
encouraged to emit mustprogress, if they can.

There are a bunch of tests that need updating, which I did not do so
far, in case there are any fundamental issues with this approach (Hence
marked as WIP).

Also, there might be cases or attribute combinations I missed.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94106

Files:
  llvm/include/llvm/IR/InstrTypes.h
  llvm/lib/Transforms/Utils/Local.cpp
  llvm/test/Transforms/Inline/dead-calls-mustprogress.ll


Index: llvm/test/Transforms/Inline/dead-calls-mustprogress.ll
===================================================================
--- llvm/test/Transforms/Inline/dead-calls-mustprogress.ll
+++ llvm/test/Transforms/Inline/dead-calls-mustprogress.ll
@@ -46,6 +46,10 @@
 define void @caller_no_mustprogress() ssp {
 ; CHECK-LABEL: @caller_no_mustprogress(
 ; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[WHILE_BODY_I:%.*]]
+; CHECK:       while.body.i:
+; CHECK-NEXT:    br label [[WHILE_BODY_I]]
+; CHECK:       readnone.exit:
 ; CHECK-NEXT:    ret void
 ;
 entry:
Index: llvm/lib/Transforms/Utils/Local.cpp
===================================================================
--- llvm/lib/Transforms/Utils/Local.cpp
+++ llvm/lib/Transforms/Utils/Local.cpp
@@ -413,6 +413,12 @@
     return true;
   }
 
+  if (auto *CB = dyn_cast<CallBase>(I))
+    // Treat calls that may not return as alive (they could loop infinitely),
+    // unless the containing function must make progress.
+    if (!I->getParent()->getParent()->mustProgress() && !CB->willReturn())
+      return false;
+
   if (!I->mayHaveSideEffects())
     return true;
 
Index: llvm/include/llvm/IR/InstrTypes.h
===================================================================
--- llvm/include/llvm/IR/InstrTypes.h
+++ llvm/include/llvm/IR/InstrTypes.h
@@ -1756,6 +1756,13 @@
   bool onlyReadsMemory() const {
     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
   }
+
+  /// Returns true if this function is guaranteed to return.
+  bool willReturn() const {
+    return hasFnAttr(Attribute::WillReturn) ||
+           hasFnAttr(Attribute::MustProgress);
+  }
+
   void setOnlyReadsMemory() {
     addAttribute(AttributeList::FunctionIndex, Attribute::ReadOnly);
   }


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94106.314671.patch
Type: text/x-patch
Size: 1753 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210105/95ab23f6/attachment.bin>


More information about the llvm-commits mailing list