[PATCH] D99334: [TransformUtils] Don't generate invalid llvm.dbg.cu

Luke Drummond via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Thu Mar 25 05:55:36 PDT 2021


ldrumm created this revision.
ldrumm added reviewers: dexonsmith, nikic, akhuang.
Herald added a subscriber: hiraditya.
ldrumm requested review of this revision.
Herald added a project: LLVM.

CloneFunctionInto generated an empty (zero operand; invalid) compile
unit in the destination module even in cases where neither the source
nor dest function's parents had them. This caused verifier failures.

This fix ensures that in the case there is no compile-unit to copy from,
an empty one is not created


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D99334

Files:
  llvm/lib/Transforms/Utils/CloneFunction.cpp
  llvm/unittests/Transforms/Utils/CloningTest.cpp


Index: llvm/unittests/Transforms/Utils/CloningTest.cpp
===================================================================
--- llvm/unittests/Transforms/Utils/CloningTest.cpp
+++ llvm/unittests/Transforms/Utils/CloningTest.cpp
@@ -849,6 +849,48 @@
   EXPECT_FALSE(haveCompileUnitsInCommon(*ImplModule, *DeclModule));
 }
 
+TEST(CloneFunction, CloneFunctionToDifferentModuleNoCU) {
+  StringRef ImplAssembly = R"(
+    define void @foo() {
+      ret void
+    }
+  )";
+  StringRef DeclAssembly = R"(
+    declare void @foo()
+  )";
+
+  LLVMContext Context;
+  SMDiagnostic Error;
+
+  auto ImplModule = parseAssemblyString(ImplAssembly, Error, Context);
+  EXPECT_TRUE(ImplModule != nullptr);
+  EXPECT_TRUE(ImplModule->getNamedMetadata("llvm.dbg.cu") == nullptr);
+  auto *ImplFunction = ImplModule->getFunction("foo");
+  EXPECT_TRUE(ImplFunction != nullptr);
+
+  auto DeclModule = parseAssemblyString(DeclAssembly, Error, Context);
+  EXPECT_TRUE(DeclModule != nullptr);
+  // No DICompileUnits defined here.
+  EXPECT_TRUE(GetDICompileUnitCount(*DeclModule) == 0);
+  auto *DeclFunction = DeclModule->getFunction("foo");
+  EXPECT_TRUE(DeclFunction != nullptr);
+
+  EXPECT_FALSE(verifyModule(*ImplModule, &errs()));
+  EXPECT_FALSE(verifyModule(*DeclModule, &errs()));
+
+  ValueToValueMapTy VMap;
+  VMap[ImplFunction] = DeclFunction;
+  // No args to map
+  SmallVector<ReturnInst *, 8> Returns;
+  CloneFunctionInto(DeclFunction, ImplFunction, VMap,
+                    CloneFunctionChangeType::DifferentModule, Returns);
+
+  EXPECT_FALSE(verifyModule(*ImplModule, &errs()));
+  EXPECT_FALSE(verifyModule(*DeclModule, &errs()));
+  // DeclModule should not have a compile unit because it didn't start with one
+  // and nor did ImplModule
+  EXPECT_TRUE(DeclModule->getNamedMetadata("llvm.dbg.cu") == nullptr);
+}
 class CloneModule : public ::testing::Test {
 protected:
   void SetUp() override {
Index: llvm/lib/Transforms/Utils/CloneFunction.cpp
===================================================================
--- llvm/lib/Transforms/Utils/CloneFunction.cpp
+++ llvm/lib/Transforms/Utils/CloneFunction.cpp
@@ -260,6 +260,9 @@
   // visiting the metadata attached to global values, which would allow this
   // code to be deleted. Alternatively, perhaps give responsibility for this
   // update to CloneFunctionInto's callers.
+  if (!OldFunc->getParent()->getNamedMetadata("llvm.dbg.cu"))
+    return;
+
   auto *NewModule = NewFunc->getParent();
   auto *NMD = NewModule->getOrInsertNamedMetadata("llvm.dbg.cu");
   // Avoid multiple insertions of the same DICompileUnit to NMD.


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D99334.333271.patch
Type: text/x-patch
Size: 2602 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20210325/4722d553/attachment.bin>


More information about the llvm-commits mailing list