Skip to content

Commit

Permalink
[DIBuilder] Add a more fine-grained finalization method
Browse files Browse the repository at this point in the history
Summary:
Clang wants to clone a function before it is done building the entire
compilation unit. As of now, there is no good way to do that, because
CloneFunction doesn't like dealing with temporary metadata. However,
as long as clang doesn't want to add any variables to this SP, it
should be fine to just prematurely finalize it. Add an API to allow this.

This is done in preparation of a clang commit to fix the assertion that
necessitated the revert of D33655.

Reviewers: aprantl, dblaikie

Subscribers: llvm-commits

Differential Revision: https://reviews.llvm.org/D33704

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304467 91177308-0d34-0410-b5e6-96231b3b80d8
  • Loading branch information
Keno committed Jun 1, 2017
1 parent ccb80b9 commit cb70331
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 16 deletions.
4 changes: 4 additions & 0 deletions include/llvm/IR/DIBuilder.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ namespace llvm {
/// Construct any deferred debug info descriptors.
void finalize();

/// Finalize a specific subprogram - no new variables may be added to this
/// subprogram afterwards.
void finalizeSubprogram(DISubprogram *SP);

/// A CompileUnit provides an anchor for all debugging
/// information generated during this instance of compilation.
/// \param Lang Source programming language, eg. dwarf::DW_LANG_C99
Expand Down
33 changes: 17 additions & 16 deletions lib/IR/DIBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,21 @@ void DIBuilder::trackIfUnresolved(MDNode *N) {
UnresolvedNodes.emplace_back(N);
}

void DIBuilder::finalizeSubprogram(DISubprogram *SP) {
MDTuple *Temp = SP->getVariables().get();
if (!Temp || !Temp->isTemporary())
return;

SmallVector<Metadata *, 4> Variables;

auto PV = PreservedVariables.find(SP);
if (PV != PreservedVariables.end())
Variables.append(PV->second.begin(), PV->second.end());

DINodeArray AV = getOrCreateArray(Variables);
TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
}

void DIBuilder::finalize() {
if (!CUNode) {
assert(!AllowUnresolvedNodes &&
Expand All @@ -62,25 +77,11 @@ void DIBuilder::finalize() {
CUNode->replaceRetainedTypes(MDTuple::get(VMContext, RetainValues));

DISubprogramArray SPs = MDTuple::get(VMContext, AllSubprograms);
auto resolveVariables = [&](DISubprogram *SP) {
MDTuple *Temp = SP->getVariables().get();
if (!Temp)
return;

SmallVector<Metadata *, 4> Variables;

auto PV = PreservedVariables.find(SP);
if (PV != PreservedVariables.end())
Variables.append(PV->second.begin(), PV->second.end());

DINodeArray AV = getOrCreateArray(Variables);
TempMDTuple(Temp)->replaceAllUsesWith(AV.get());
};
for (auto *SP : SPs)
resolveVariables(SP);
finalizeSubprogram(SP);
for (auto *N : RetainValues)
if (auto *SP = dyn_cast<DISubprogram>(N))
resolveVariables(SP);
finalizeSubprogram(SP);

if (!AllGVs.empty())
CUNode->replaceGlobalVariables(MDTuple::get(VMContext, AllGVs));
Expand Down

0 comments on commit cb70331

Please sign in to comment.