[llvm] 4233a15 - [FunctionAttrs] Handle zero writes in initializes inference.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Sat Jan 18 12:01:27 PST 2025


Author: Florian Hahn
Date: 2025-01-18T20:01:07Z
New Revision: 4233a15c9f8e6f77a00a5770a35b70ab8a2705c6

URL: https://github.com/llvm/llvm-project/commit/4233a15c9f8e6f77a00a5770a35b70ab8a2705c6
DIFF: https://github.com/llvm/llvm-project/commit/4233a15c9f8e6f77a00a5770a35b70ab8a2705c6.diff

LOG: [FunctionAttrs] Handle zero writes in initializes inference.

ConstantRange's constructor asserts that the range not empty, except if
lower/upper are min or max values.

Check if the length is strictly positive instead of just non-negative so
std::nullopt is returned when the size is 0. If that's the case, the
access doesn't initialize anything.

This should fix a crash when building on macOS with ASan & UBsan after
https://github.com/llvm/llvm-project/pull/97373 /
https://github.com/llvm/llvm-project/pull/117104 landed:
https://green.lab.llvm.org/job/llvm.org/job/clang-stage2-cmake-RgSan/664/console

Added: 
    

Modified: 
    llvm/lib/Transforms/IPO/FunctionAttrs.cpp
    llvm/test/Transforms/FunctionAttrs/initializes.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
index 56bfc8432cbb2d..17f946e5acdf84 100644
--- a/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
+++ b/llvm/lib/Transforms/IPO/FunctionAttrs.cpp
@@ -633,10 +633,12 @@ ArgumentAccessInfo getArgmentAccessInfo(const Instruction *I,
       [](Value *Length,
          std::optional<int64_t> Offset) -> std::optional<ConstantRange> {
     auto *ConstantLength = dyn_cast<ConstantInt>(Length);
-    if (ConstantLength && Offset && !ConstantLength->isNegative())
+    if (ConstantLength && Offset &&
+        ConstantLength->getValue().isStrictlyPositive()) {
       return ConstantRange(
           APInt(64, *Offset, true),
           APInt(64, *Offset + ConstantLength->getSExtValue(), true));
+    }
     return std::nullopt;
   };
   if (auto *SI = dyn_cast<StoreInst>(I)) {

diff  --git a/llvm/test/Transforms/FunctionAttrs/initializes.ll b/llvm/test/Transforms/FunctionAttrs/initializes.ll
index 62f217aa3f7de9..e04a095c8a43b4 100644
--- a/llvm/test/Transforms/FunctionAttrs/initializes.ll
+++ b/llvm/test/Transforms/FunctionAttrs/initializes.ll
@@ -611,3 +611,27 @@ define void @caller_byval(ptr %p) {
   call void @callee_byval(ptr byval(i32) %p)
   ret void
 }
+
+define void @memset_offset_0_size_0(ptr %dst, ptr %src) {
+; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
+; CHECK-LABEL: define void @memset_offset_0_size_0(
+; CHECK-SAME: ptr nocapture writeonly [[DST:%.*]], ptr nocapture readonly [[SRC:%.*]]) #[[ATTR1]] {
+; CHECK-NEXT:    call void @llvm.memmove.p0.p0.i64(ptr [[DST]], ptr [[SRC]], i64 0, i1 false)
+; CHECK-NEXT:    ret void
+;
+  call void @llvm.memmove.p0.p0.i64(ptr %dst, ptr %src, i64 0, i1 false)
+  ret void
+}
+
+define void @memset_offset_1_size_0(ptr %dst, ptr %src) {
+; CHECK: Function Attrs: mustprogress nofree norecurse nosync nounwind willreturn memory(argmem: readwrite)
+; CHECK-LABEL: define void @memset_offset_1_size_0(
+; CHECK-SAME: ptr nocapture writeonly [[DST:%.*]], ptr nocapture readonly [[SRC:%.*]]) #[[ATTR1]] {
+; CHECK-NEXT:    [[DST_1:%.*]] = getelementptr inbounds i8, ptr [[DST]], i64 1
+; CHECK-NEXT:    call void @llvm.memmove.p0.p0.i64(ptr [[DST_1]], ptr [[SRC]], i64 0, i1 false)
+; CHECK-NEXT:    ret void
+;
+  %dst.1 = getelementptr inbounds i8, ptr %dst, i64 1
+  call void @llvm.memmove.p0.p0.i64(ptr %dst.1, ptr %src, i64 0, i1 false)
+  ret void
+}


        


More information about the llvm-commits mailing list