[PATCH] D95088: [InstCombine] avoid crashing on attribute propagation

Sanjay Patel via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Jan 20 13:46:11 PST 2021


spatel created this revision.
spatel added reviewers: jdoerfert, zequanwu, fhahn.
Herald added subscribers: hiraditya, mcrosier.
spatel requested review of this revision.
Herald added a project: LLVM.

In https://llvm.org/PR48810 , we are crashing while trying to propagate attributes from `mempcpy` (returns void*)  to `memcpy` (returns nothing - void).
We can avoid the crash by removing known incompatible attributes for the void return type.
I'm not sure if this goes far enough (should we just drop all attributes since this isn't the same function?). We also need to audit other transforms in LibCallSimplifier to make sure there are no other cases that have the same problem.


https://reviews.llvm.org/D95088

Files:
  llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
  llvm/test/Transforms/InstCombine/mempcpy.ll


Index: llvm/test/Transforms/InstCombine/mempcpy.ll
===================================================================
--- llvm/test/Transforms/InstCombine/mempcpy.ll
+++ llvm/test/Transforms/InstCombine/mempcpy.ll
@@ -53,4 +53,15 @@
   ret i8* %r
 }
 
+; The original call may have attributes that can not propagate to memcpy.
+
+define i32 @PR48810() {
+; CHECK-LABEL: @PR48810(
+; CHECK-NEXT:    call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 1 undef, i8* align 536870912 null, i64 undef, i1 false)
+; CHECK-NEXT:    ret i32 undef
+;
+  %r = call dereferenceable(1) i8* @mempcpy(i8* undef, i8* null, i64 undef)
+  ret i32 undef
+}
+
 declare i8* @mempcpy(i8*, i8* nocapture readonly, i64)
Index: llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
===================================================================
--- llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
+++ llvm/lib/Transforms/Utils/SimplifyLibCalls.cpp
@@ -1150,7 +1150,11 @@
   // mempcpy(x, y, n) -> llvm.memcpy(align 1 x, align 1 y, n), x + n
   CallInst *NewCI =
       B.CreateMemCpy(Dst, Align(1), CI->getArgOperand(1), Align(1), N);
+  // Propagate attributes, but memcpy has no return value, so make sure that
+  // any return attributes are compliant.
   NewCI->setAttributes(CI->getAttributes());
+  NewCI->removeAttributes(AttributeList::ReturnIndex,
+                          AttributeFuncs::typeIncompatible(NewCI->getType()));
   return B.CreateInBoundsGEP(B.getInt8Ty(), Dst, N);
 }
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D95088.317993.patch
Type: text/x-patch
Size: 1464 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210120/345164ea/attachment.bin>


More information about the llvm-commits mailing list