[PATCH] D75173: [Transform][MemCpyOpt] Add missing DebugLoc to %tmpbitcast

Pierre van Houtryve via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 26 06:14:19 PST 2020


Pierre-vh created this revision.
Pierre-vh added a reviewer: gbedwell.
Herald added subscribers: llvm-commits, hiraditya.
Herald added a project: LLVM.

This is a fix for https://bugs.llvm.org/show_bug.cgi?id=37967

I need some help to be sure that this fix is correct, even though it's trivial, I am not sure I fixed this in the best possible way.

- Is this a good fix for this issue, or is it just working around it?
- Is the test I wrote good enough? I just tested for the output of -debugify-each, but perhaps it'd be better to check the LLVM IR directly?




Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D75173

Files:
  llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
  llvm/test/Transforms/MemCpyOpt/pr37967.ll


Index: llvm/test/Transforms/MemCpyOpt/pr37967.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/MemCpyOpt/pr37967.ll
@@ -0,0 +1,74 @@
+; RUN:  opt -debugify-each -disable-output -instcombine -memcpyopt < %s 2>&1| FileCheck %s
+
+; CHECK: CheckFunctionDebugify [MemCpy Optimization]: PASS
+; CHECK-NOT: ERROR: Instruction with empty DebugLoc in function _Z3bar3FooS_RiS_ --  %tmpcast = bitcast i8* %1 to %struct.Foo*
+
+%struct.Foo = type { i64, i64 }
+
+ at a = dso_local global %struct.Foo* null, align 8
+
+; Function Attrs: noinline uwtable
+define dso_local i32 @_Z3bar3FooS_RiS_(i64 %.coerce0, i64 %.coerce1, i64 %.coerce01, i64 %.coerce12, i32* dereferenceable(4) %c, %struct.Foo* byval(%struct.Foo) align 8 %0) #0 {
+entry:
+  %1 = alloca %struct.Foo, align 8
+  %2 = alloca %struct.Foo, align 8
+  %c.addr = alloca i32*, align 8
+  %agg.tmp = alloca %struct.Foo, align 8
+  %agg.tmp3 = alloca %struct.Foo, align 8
+  %agg.tmp5 = alloca %struct.Foo, align 8
+  %3 = bitcast %struct.Foo* %1 to { i64, i64 }*
+  %4 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %3, i32 0, i32 0
+  store i64 %.coerce0, i64* %4, align 8
+  %5 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %3, i32 0, i32 1
+  store i64 %.coerce1, i64* %5, align 8
+  %6 = bitcast %struct.Foo* %2 to { i64, i64 }*
+  %7 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %6, i32 0, i32 0
+  store i64 %.coerce01, i64* %7, align 8
+  %8 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %6, i32 0, i32 1
+  store i64 %.coerce12, i64* %8, align 8
+  store i32* %c, i32** %c.addr, align 8
+  %9 = load %struct.Foo*, %struct.Foo** @a, align 8
+  %arrayidx = getelementptr inbounds %struct.Foo, %struct.Foo* %9, i64 0
+  %10 = bitcast %struct.Foo* %agg.tmp to i8*
+  %11 = bitcast %struct.Foo* %arrayidx to i8*
+  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %10, i8* align 8 %11, i64 16, i1 false)
+  %12 = load %struct.Foo*, %struct.Foo** @a, align 8
+  %arrayidx4 = getelementptr inbounds %struct.Foo, %struct.Foo* %12, i64 0
+  %13 = bitcast %struct.Foo* %agg.tmp3 to i8*
+  %14 = bitcast %struct.Foo* %arrayidx4 to i8*
+  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %13, i8* align 8 %14, i64 16, i1 false)
+  %15 = load i32*, i32** %c.addr, align 8
+  %16 = load %struct.Foo*, %struct.Foo** @a, align 8
+  %arrayidx6 = getelementptr inbounds %struct.Foo, %struct.Foo* %16, i64 0
+  %17 = bitcast %struct.Foo* %agg.tmp5 to i8*
+  %18 = bitcast %struct.Foo* %arrayidx6 to i8*
+  call void @llvm.memcpy.p0i8.p0i8.i64(i8* align 8 %17, i8* align 8 %18, i64 16, i1 false)
+  %19 = bitcast %struct.Foo* %agg.tmp to { i64, i64 }*
+  %20 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %19, i32 0, i32 0
+  %21 = load i64, i64* %20, align 8
+  %22 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %19, i32 0, i32 1
+  %23 = load i64, i64* %22, align 8
+  %24 = bitcast %struct.Foo* %agg.tmp3 to { i64, i64 }*
+  %25 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %24, i32 0, i32 0
+  %26 = load i64, i64* %25, align 8
+  %27 = getelementptr inbounds { i64, i64 }, { i64, i64 }* %24, i32 0, i32 1
+  %28 = load i64, i64* %27, align 8
+  %call = call i32 @_Z3bar3FooS_RiS_(i64 %21, i64 %23, i64 %26, i64 %28, i32* dereferenceable(4) %15, %struct.Foo* byval(%struct.Foo) align 8 %agg.tmp5)
+  %29 = load i32*, i32** %c.addr, align 8
+  store i32 %call, i32* %29, align 4
+  %30 = load i32*, i32** %c.addr, align 8
+  %31 = load i32, i32* %30, align 4
+  ret i32 %31
+}
+
+; Function Attrs: argmemonly nounwind willreturn
+declare void @llvm.memcpy.p0i8.p0i8.i64(i8* noalias nocapture writeonly, i8* noalias nocapture readonly, i64, i1 immarg) #1
+
+attributes #0 = { noinline uwtable "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "target-cpu"="x86-64" "target-features"="+cx8,+fxsr,+mmx,+sse,+sse2,+x87" "unsafe-fp-math"="false" "use-soft-float"="false" }
+attributes #1 = { argmemonly nounwind willreturn }
+
+!llvm.module.flags = !{!0}
+!llvm.ident = !{!1}
+
+!0 = !{i32 1, !"wchar_size", i32 4}
+!1 = !{!"clang version 11.0.0 (https://github.com/llvm/llvm-project.git def62c7f6548104274f95154bb5a91264646c701)"}
Index: llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
+++ llvm/lib/Transforms/Scalar/MemCpyOptimizer.cpp
@@ -1302,9 +1302,13 @@
     return false;
 
   Value *TmpCast = MDep->getSource();
-  if (MDep->getSource()->getType() != ByValArg->getType())
-    TmpCast = new BitCastInst(MDep->getSource(), ByValArg->getType(),
+  if (MDep->getSource()->getType() != ByValArg->getType()) {
+    BitCastInst* TmpBitCast = new BitCastInst(MDep->getSource(), ByValArg->getType(),
                               "tmpcast", CS.getInstruction());
+    // Set the tmpcast's DebugLoc to MDep's
+    TmpBitCast->setDebugLoc(MDep->getDebugLoc());
+    TmpCast = TmpBitCast;
+  }
 
   LLVM_DEBUG(dbgs() << "MemCpyOptPass: Forwarding memcpy to byval:\n"
                     << "  " << *MDep << "\n"


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D75173.246692.patch
Type: text/x-patch
Size: 5353 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200226/2a30b19d/attachment.bin>


More information about the llvm-commits mailing list