[PATCH] D94502: [FunctionAttrs] Derive willreturn for fns with readonly` & `mustprogress`.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 12 08:40:46 PST 2021


fhahn created this revision.
fhahn added reviewers: jdoerfert, atmnpatel, nikic, efriedma.
Herald added a subscriber: hiraditya.
fhahn requested review of this revision.
Herald added a project: LLVM.

Similar to D94125 <https://reviews.llvm.org/D94125>, derive `willreturn` for functions that are `readonly` and
`mustprogress` in FunctionAttrs.

To quote the reasoning from D94125 <https://reviews.llvm.org/D94125>:

  Since D86233 we have `mustprogress` which, in combination with
  `readonly`, implies `willreturn`. The idea is that every side-effect
  has to be modeled as a "write". Consequently, `readonly` means there
  is no side-effect, and `mustprogress` guarantees that we cannot "loop"
  forever without side-effect.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D94502

Files:
  llvm/lib/Transforms/IPO/FunctionAttrs.cpp
  llvm/test/Transforms/FunctionAttrs/willreturn.ll


Index: llvm/test/Transforms/FunctionAttrs/willreturn.ll
===================================================================
--- llvm/test/Transforms/FunctionAttrs/willreturn.ll
+++ llvm/test/Transforms/FunctionAttrs/willreturn.ll
@@ -1,9 +1,8 @@
 ; RUN: opt -function-attrs -S %s | FileCheck %s
 
-; TODO
 define void @mustprogress_readnone() mustprogress {
-; CHECK-NOT: Function Attrs: {{.*}} willreturn
-; CHECK:     define void @mustprogress_readnone()
+; CHECK:      Function Attrs: {{.*}} readnone willreturn
+; CHECK-NEXT: define void @mustprogress_readnone()
 ;
 entry:
   br label %while.body
@@ -12,10 +11,9 @@
   br label %while.body
 }
 
-; TODO
 define i32 @mustprogress_load(i32* %ptr) mustprogress {
-; CHECK-NOT: Function Attrs: {{.*}} willreturn
-; CHECK:     define i32 @mustprogress_load(
+; CHECK:      Function Attrs: {{.*}} readonly willreturn
+; CHECK-NEXT: define i32 @mustprogress_load(
 ;
 entry:
   %r = load i32, i32* %ptr
@@ -35,16 +33,15 @@
 
 define void @mustprogress_call_unknown_fn() mustprogress {
 ; CHECK-NOT: Function Attrs: {{.*}} willreturn
-; CHECK: define void @mustprogress_call_unknown_fn(
+; CHECK:     define void @mustprogress_call_unknown_fn(
 ;
   call void @unknown_fn()
   ret void
 }
 
-; TODO
 define i32 @mustprogress_call_known_functions(i32* %ptr) mustprogress {
-; CHECK-NOT: Function Attrs: {{.*}} willreturn
-; CHECK:     define i32 @mustprogress_call_known_functions(
+; CHECK:      Function Attrs: {{.*}} readonly willreturn
+; CHECK-NEXT: define i32 @mustprogress_call_known_functions(
 ;
   call void @mustprogress_readnone()
   %r = call i32 @mustprogress_load(i32* %ptr)
@@ -53,10 +50,9 @@
 
 declare i32 @__gxx_personality_v0(...)
 
-; TODO
 define i64 @mustprogress_mayunwind() mustprogress personality i8* bitcast (i32 (...)* @__gxx_personality_v0 to i8*) {
-; CHECK-NOT: Function Attrs: {{.*}} willreturn
-; CHECK:     define i64 @mustprogress_mayunwind(
+; CHECK:      Function Attrs: {{.*}} readnone willreturn
+; CHECK-NEXT: define i64 @mustprogress_mayunwind(
 ;
   %a = invoke i64 @fn_noread()
           to label %A unwind label %B
Index: llvm/lib/Transforms/IPO/FunctionAttrs.cpp
===================================================================
--- llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -1424,6 +1424,21 @@
   return Changed;
 }
 
+// Set the willreturn function attribute if possible.
+static bool addWillReturn(const SCCNodeSet &SCCNodes) {
+  bool Changed = false;
+
+  for (Function *F : SCCNodes) {
+    if (!F || !F->onlyReadsMemory() || !F->mustProgress())
+      continue;
+
+    F->addFnAttr(Attribute::WillReturn);
+    F->removeFnAttr(Attribute::NoReturn);
+  }
+
+  return Changed;
+}
+
 static SCCNodesResult createSCCNodeSet(ArrayRef<Function *> Functions) {
   SCCNodesResult Res;
   Res.HasUnknownCall = false;
@@ -1468,6 +1483,7 @@
   Changed |= addArgumentAttrs(Nodes.SCCNodes);
   Changed |= inferConvergent(Nodes.SCCNodes);
   Changed |= addNoReturnAttrs(Nodes.SCCNodes);
+  Changed |= addWillReturn(Nodes.SCCNodes);
 
   // If we have no external nodes participating in the SCC, we can deduce some
   // more precise attributes as well.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D94502.316105.patch
Type: text/x-patch
Size: 3190 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210112/6f98868d/attachment.bin>


More information about the llvm-commits mailing list