[PATCH] D72666: [IR] ArgMemOnly functions with WriteOnly ptr args do not read memory.

Florian Hahn via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Jan 13 16:41:50 PST 2020


fhahn created this revision.
fhahn added reviewers: jdoerfert, reames, efriedma, nlopes, lebedev.ri.
Herald added a reviewer: sstefan1.
Herald added a project: LLVM.

Calls/functions with ArgMemOnly only access memory via pointer-type
arguments. If all pointer-type arguments are WriteOnly, this means that
the function does not read memory.

This is relevant, for example, for the current definition of
@llvm.memset, which is marked as ArgMemOnly with WriteOnly pointers arguments.
Without this patch, doesNotReadMemory returns false for @llvm.memset.

I am not sure where the best place to add tests for this would be?

This impacts a single attributor test, which uses the following
declaration

  declare <4 x i32> @test11_1(<4 x i32*>) argmemonly nounwind readonly

With the patch, this function will be treated as readnone. I think that
is fine according to the ArgMemOnly definition: there are no
pointer-typed arguments (the only argument is a vector of pointers), so
no memory can be accessed.

If 'pointer-typed arguments' could be interpreted as any argument containing a
pointer, this patch would have to be more careful with checking the arguments.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D72666

Files:
  llvm/include/llvm/IR/Function.h
  llvm/include/llvm/IR/InstrTypes.h
  llvm/test/Transforms/Attributor/readattrs.ll


Index: llvm/test/Transforms/Attributor/readattrs.ll
===================================================================
--- llvm/test/Transforms/Attributor/readattrs.ll
+++ llvm/test/Transforms/Attributor/readattrs.ll
@@ -93,8 +93,7 @@
 
 ; ATTRIBUTOR: declare <4 x i32> @test11_1
 declare <4 x i32> @test11_1(<4 x i32*>) argmemonly nounwind readonly
-; ATTRIBUTOR: readonly
-; ATTRIBUTOR-NOT: readnone
+; ATTRIBUTOR: readnone
 ; ATTRIBUTOR: define <4 x i32> @test11_2
 define <4 x i32> @test11_2(<4 x i32*> %ptrs) {
   %res = call <4 x i32> @test11_1(<4 x i32*> %ptrs)
Index: llvm/include/llvm/IR/InstrTypes.h
===================================================================
--- llvm/include/llvm/IR/InstrTypes.h
+++ llvm/include/llvm/IR/InstrTypes.h
@@ -1673,7 +1673,8 @@
 
   /// Determine if the call does not access or only writes memory.
   bool doesNotReadMemory() const {
-    return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
+    return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly) ||
+           (onlyAccessesArgMemory() && argsWriteOnly());
   }
   void setDoesNotReadMemory() {
     addAttribute(AttributeList::FunctionIndex, Attribute::WriteOnly);
@@ -2146,6 +2147,14 @@
 
     return hasFnAttrOnCalledFunction(Kind);
   }
+
+  bool argsWriteOnly() const {
+    for (unsigned I = 0; I < getNumArgOperands(); I++)
+      if (getArgOperand(I)->getType()->isPointerTy() &&
+          !paramHasAttr(I, Attribute::WriteOnly))
+        return false;
+    return true;
+  }
 };
 
 template <>
Index: llvm/include/llvm/IR/Function.h
===================================================================
--- llvm/include/llvm/IR/Function.h
+++ llvm/include/llvm/IR/Function.h
@@ -498,7 +498,12 @@
 
   /// Determine if the function does not access or only writes memory.
   bool doesNotReadMemory() const {
-    return doesNotAccessMemory() || hasFnAttribute(Attribute::WriteOnly);
+    return doesNotAccessMemory() || hasFnAttribute(Attribute::WriteOnly) ||
+           (hasFnAttribute(Attribute::ArgMemOnly) &&
+            all_of(args(), [](const Argument &A) {
+              return !A.getType()->isPointerTy() ||
+                     A.hasAttribute(Attribute::WriteOnly);
+            }));
   }
   void setDoesNotReadMemory() {
     addFnAttr(Attribute::WriteOnly);


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D72666.237813.patch
Type: text/x-patch
Size: 2305 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200114/3a5183b2/attachment.bin>


More information about the llvm-commits mailing list