[llvm] 1eb5b6e - [InferAttrs] If readonly is already set, set readnone instead of writeonly

Fangrui Song via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 4 18:59:40 PST 2022


Author: Fangrui Song
Date: 2022-01-04T18:59:35-08:00
New Revision: 1eb5b6e85045d22720f177a02aaf7097930e4b4f

URL: https://github.com/llvm/llvm-project/commit/1eb5b6e85045d22720f177a02aaf7097930e4b4f
DIFF: https://github.com/llvm/llvm-project/commit/1eb5b6e85045d22720f177a02aaf7097930e4b4f.diff

LOG: [InferAttrs] If readonly is already set, set readnone instead of writeonly

D116426 may lead to an assertion failure `Attributes 'readonly and writeonly' are incompatible!` if the builtin function already has `readonly`.

Added: 
    

Modified: 
    llvm/lib/Transforms/Utils/BuildLibCalls.cpp
    llvm/test/Transforms/InferFunctionAttrs/annotate.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
index 167705235d516..bc7c5361b6058 100644
--- a/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
+++ b/llvm/lib/Transforms/Utils/BuildLibCalls.cpp
@@ -75,8 +75,13 @@ static bool setOnlyReadsMemory(Function &F) {
 static bool setDoesNotReadMemory(Function &F) {
   if (F.doesNotReadMemory()) // writeonly or readnone
     return false;
-  F.setDoesNotReadMemory();
   ++NumWriteOnly;
+  if (F.hasFnAttribute(Attribute::ReadOnly)) {
+    F.removeFnAttr(Attribute::ReadOnly);
+    F.setDoesNotAccessMemory();
+  } else {
+    F.setDoesNotReadMemory();
+  }
   return true;
 }
 

diff  --git a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
index 8869477231d51..55b77f7d5a2a1 100644
--- a/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
+++ b/llvm/test/Transforms/InferFunctionAttrs/annotate.ll
@@ -201,8 +201,8 @@ declare i32 @abs(i32)
 ; CHECK: declare noundef i32 @access(i8* nocapture noundef readonly, i32 noundef) [[NOFREE_NOUNWIND:#[0-9]+]]
 declare i32 @access(i8*, i32)
 
-; CHECK: declare double @acos(double) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
-declare double @acos(double)
+; CHECK: declare double @acos(double) [[NOFREE_NOUNWIND_WILLRETURN_READNONE:#[0-9]+]]
+declare double @acos(double) readonly
 
 ; CHECK: declare float @acosf(float) [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]]
 declare float @acosf(float)
@@ -1065,6 +1065,7 @@ declare void @memset_pattern16(i8*, i8*, i64)
 
 ; CHECK-DAG: attributes [[NOFREE_NOUNWIND_WILLRETURN]] = { mustprogress nofree nounwind willreturn }
 ; CHECK-DAG: attributes [[NOFREE_NOUNWIND_WILLRETURN_WRITEONLY]] = { mustprogress nofree nounwind willreturn writeonly }
+; CHECK-DAG: attributes [[NOFREE_NOUNWIND_WILLRETURN_READNONE]] = { mustprogress nofree nosync nounwind readnone willreturn }
 ; CHECK-DAG: attributes [[NOFREE_NOUNWIND]] = { nofree nounwind }
 ; CHECK-DAG: attributes [[INACCESSIBLEMEMONLY_NOFREE_NOUNWIND_WILLRETURN]] = { inaccessiblememonly mustprogress nofree nounwind willreturn }
 ; CHECK-DAG: attributes [[NOFREE_NOUNWIND_READONLY_WILLRETURN]] = { mustprogress nofree nounwind readonly willreturn }


        


More information about the llvm-commits mailing list