|
| 1 | +From 11adcc4de0797c83e61ae0240927f0bafcf041a9 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Keno Fischer < [email protected]> |
| 3 | +Date: Sat, 13 Feb 2016 02:04:29 +0000 |
| 4 | +Subject: [PATCH] [Cloning] Clone every Function's Debug Info |
| 5 | + |
| 6 | +Summary: |
| 7 | +Export the CloneDebugInfoMetadata utility, which clones all debug info |
| 8 | +associated with a function into the first module. Also use this function |
| 9 | +in CloneModule on each function we clone (the CloneFunction entrypoint |
| 10 | +already does this). |
| 11 | + |
| 12 | +Without this, cloning a module will lead to DI quality regressions, |
| 13 | +especially since r252219 reversed the Function <-> DISubprogram edge |
| 14 | +(before we could get lucky and have this edge preserved if the |
| 15 | +DISubprogram itself was, e.g. due to location metadata). |
| 16 | + |
| 17 | +This was verified to fix missing debug information in julia and |
| 18 | +a unittest to verify the new behavior is included. |
| 19 | + |
| 20 | +Patch by Yichao Yu! Thanks! |
| 21 | + |
| 22 | +Reviewers: loladiro, pcc |
| 23 | +Differential Revision: http://reviews.llvm.org/D17165 |
| 24 | + |
| 25 | +git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@260791 91177308-0d34-0410-b5e6-96231b3b80d8 |
| 26 | +--- |
| 27 | + include/llvm/Transforms/Utils/Cloning.h | 5 +++++ |
| 28 | + lib/Transforms/Utils/CloneFunction.cpp | 4 ++-- |
| 29 | + lib/Transforms/Utils/CloneModule.cpp | 1 + |
| 30 | + unittests/Transforms/Utils/Cloning.cpp | 25 +++++++++++++++++++++++++ |
| 31 | + 4 files changed, 33 insertions(+), 2 deletions(-) |
| 32 | + |
| 33 | +diff --git a/include/llvm/Transforms/Utils/Cloning.h b/include/llvm/Transforms/Utils/Cloning.h |
| 34 | +index 4f006f2..0bae2bd 100644 |
| 35 | +--- a/include/llvm/Transforms/Utils/Cloning.h |
| 36 | ++++ b/include/llvm/Transforms/Utils/Cloning.h |
| 37 | +@@ -130,6 +130,11 @@ Function *CloneFunction(const Function *F, ValueToValueMapTy &VMap, |
| 38 | + bool ModuleLevelChanges, |
| 39 | + ClonedCodeInfo *CodeInfo = nullptr); |
| 40 | + |
| 41 | ++/// Clone the module-level debug info associated with OldFunc. The cloned data |
| 42 | ++/// will point to NewFunc instead. |
| 43 | ++void CloneDebugInfoMetadata(Function *NewFunc, const Function *OldFunc, |
| 44 | ++ ValueToValueMapTy &VMap); |
| 45 | ++ |
| 46 | + /// Clone OldFunc into NewFunc, transforming the old arguments into references |
| 47 | + /// to VMap values. Note that if NewFunc already has basic blocks, the ones |
| 48 | + /// cloned into it will be added to the end of the function. This function |
| 49 | +diff --git a/lib/Transforms/Utils/CloneFunction.cpp b/lib/Transforms/Utils/CloneFunction.cpp |
| 50 | +index 6454afb..8b5692a 100644 |
| 51 | +--- a/lib/Transforms/Utils/CloneFunction.cpp |
| 52 | ++++ b/lib/Transforms/Utils/CloneFunction.cpp |
| 53 | +@@ -187,8 +187,8 @@ static void AddOperand(DICompileUnit *CU, DISubprogramArray SPs, |
| 54 | + |
| 55 | + // Clone the module-level debug info associated with OldFunc. The cloned data |
| 56 | + // will point to NewFunc instead. |
| 57 | +-static void CloneDebugInfoMetadata(Function *NewFunc, const Function *OldFunc, |
| 58 | +- ValueToValueMapTy &VMap) { |
| 59 | ++void llvm::CloneDebugInfoMetadata(Function *NewFunc, const Function *OldFunc, |
| 60 | ++ ValueToValueMapTy &VMap) { |
| 61 | + DebugInfoFinder Finder; |
| 62 | + Finder.processModule(*OldFunc->getParent()); |
| 63 | + |
| 64 | +diff --git a/lib/Transforms/Utils/CloneModule.cpp b/lib/Transforms/Utils/CloneModule.cpp |
| 65 | +index 53de62a..b16a02a 100644 |
| 66 | +--- a/lib/Transforms/Utils/CloneModule.cpp |
| 67 | ++++ b/lib/Transforms/Utils/CloneModule.cpp |
| 68 | +@@ -136,6 +136,7 @@ std::unique_ptr<Module> llvm::CloneModule( |
| 69 | + VMap[&*J] = &*DestI++; |
| 70 | + } |
| 71 | + |
| 72 | ++ CloneDebugInfoMetadata(F, &*I, VMap); |
| 73 | + SmallVector<ReturnInst*, 8> Returns; // Ignore returns cloned. |
| 74 | + CloneFunctionInto(F, &*I, VMap, /*ModuleLevelChanges=*/true, Returns); |
| 75 | + } |
| 76 | +diff --git a/unittests/Transforms/Utils/Cloning.cpp b/unittests/Transforms/Utils/Cloning.cpp |
| 77 | +index 25e322e..b761e4e 100644 |
| 78 | +--- a/unittests/Transforms/Utils/Cloning.cpp |
| 79 | ++++ b/unittests/Transforms/Utils/Cloning.cpp |
| 80 | +@@ -423,6 +423,7 @@ class CloneModule : public ::testing::Test { |
| 81 | + void SetupModule() { OldM = new Module("", C); } |
| 82 | + |
| 83 | + void CreateOldModule() { |
| 84 | ++ DIBuilder DBuilder(*OldM); |
| 85 | + IRBuilder<> IBuilder(C); |
| 86 | + |
| 87 | + auto *FuncType = FunctionType::get(Type::getVoidTy(C), false); |
| 88 | +@@ -431,9 +432,25 @@ class CloneModule : public ::testing::Test { |
| 89 | + auto *F = |
| 90 | + Function::Create(FuncType, GlobalValue::PrivateLinkage, "f", OldM); |
| 91 | + F->setPersonalityFn(PersFn); |
| 92 | ++ |
| 93 | ++ // Create debug info |
| 94 | ++ auto *File = DBuilder.createFile("filename.c", "/file/dir/"); |
| 95 | ++ DITypeRefArray ParamTypes = DBuilder.getOrCreateTypeArray(None); |
| 96 | ++ DISubroutineType *DFuncType = DBuilder.createSubroutineType(ParamTypes); |
| 97 | ++ auto *CU = |
| 98 | ++ DBuilder.createCompileUnit(dwarf::DW_LANG_C99, "filename.c", |
| 99 | ++ "/file/dir", "CloneModule", false, "", 0); |
| 100 | ++ // Function DI |
| 101 | ++ auto *Subprogram = DBuilder.createFunction(CU, "f", "f", File, 4, DFuncType, |
| 102 | ++ true, true, 3, 0, false); |
| 103 | ++ F->setSubprogram(Subprogram); |
| 104 | ++ |
| 105 | + auto *Entry = BasicBlock::Create(C, "", F); |
| 106 | + IBuilder.SetInsertPoint(Entry); |
| 107 | + IBuilder.CreateRetVoid(); |
| 108 | ++ |
| 109 | ++ // Finalize the debug info |
| 110 | ++ DBuilder.finalize(); |
| 111 | + } |
| 112 | + |
| 113 | + void CreateNewModule() { NewM = llvm::CloneModule(OldM).release(); } |
| 114 | +@@ -447,4 +464,12 @@ TEST_F(CloneModule, Verify) { |
| 115 | + EXPECT_FALSE(verifyModule(*NewM)); |
| 116 | + } |
| 117 | + |
| 118 | ++TEST_F(CloneModule, Subprogram) { |
| 119 | ++ Function *NewF = NewM->getFunction("f"); |
| 120 | ++ DISubprogram *SP = NewF->getSubprogram(); |
| 121 | ++ EXPECT_TRUE(SP != nullptr); |
| 122 | ++ EXPECT_EQ(SP->getName(), "f"); |
| 123 | ++ EXPECT_EQ(SP->getFile()->getFilename(), "filename.c"); |
| 124 | ++ EXPECT_EQ(SP->getLine(), (unsigned)4); |
| 125 | ++} |
| 126 | + } |
0 commit comments