From 42400512520b9041d4caf5a1a5fd77f54a298f1d Mon Sep 17 00:00:00 2001 From: Robert Widmann Date: Thu, 10 May 2018 18:09:53 +0000 Subject: [PATCH] [LLVM-C] Move DIBuilder Bindings For Temporary MDNodes Summary: Move LLVMTemporaryMDNode and LLVMMetadataReplaceAllUsesWith to the C bindings and add LLVMDeleteTemporaryMDNode for deleting non-RAUW'ed temporary nodes. Reviewers: whitequark, harlanhaskins, deadalnix Reviewed By: whitequark Subscribers: llvm-commits Differential Revision: https://reviews.llvm.org/D46632 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@332010 91177308-0d34-0410-b5e6-96231b3b80d8 --- bindings/go/llvm/IRBindings.cpp | 13 ------------- bindings/go/llvm/IRBindings.h | 4 ---- bindings/go/llvm/dibuilder.go | 14 ++++++++++++++ bindings/go/llvm/ir.go | 13 ------------- include/llvm-c/DebugInfo.h | 27 +++++++++++++++++++++++++++ lib/IR/DebugInfo.cpp | 17 +++++++++++++++++ tools/llvm-c-test/debuginfo.c | 15 ++++++++++++--- 7 files changed, 70 insertions(+), 33 deletions(-) diff --git a/bindings/go/llvm/IRBindings.cpp b/bindings/go/llvm/IRBindings.cpp index f161feefa1d6..b8e3063eb9ac 100644 --- a/bindings/go/llvm/IRBindings.cpp +++ b/bindings/go/llvm/IRBindings.cpp @@ -36,13 +36,6 @@ LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs, MDNode::get(*unwrap(C), ArrayRef(unwrap(MDs), Count))); } -LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs, - unsigned Count) { - return wrap(MDTuple::getTemporary(*unwrap(C), - ArrayRef(unwrap(MDs), Count)) - .release()); -} - void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name, LLVMMetadataRef Val) { NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name); @@ -58,12 +51,6 @@ void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) { unwrap(Inst)->setMetadata(KindID, N); } -void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New) { - auto *Node = unwrap(MD); - Node->replaceAllUsesWith(unwrap(New)); - MDNode::deleteTemporary(Node); -} - void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line, unsigned Col, LLVMMetadataRef Scope, LLVMMetadataRef InlinedAt) { diff --git a/bindings/go/llvm/IRBindings.h b/bindings/go/llvm/IRBindings.h index f7fe374669a4..a2f7d2db57d6 100644 --- a/bindings/go/llvm/IRBindings.h +++ b/bindings/go/llvm/IRBindings.h @@ -38,15 +38,11 @@ LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef Val); LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen); LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs, unsigned Count); -LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs, - unsigned Count); void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name, LLVMMetadataRef Val); void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD); -void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New); - void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line, unsigned Col, LLVMMetadataRef Scope, LLVMMetadataRef InlinedAt); diff --git a/bindings/go/llvm/dibuilder.go b/bindings/go/llvm/dibuilder.go index e8318b88ca3c..ce7247af15be 100644 --- a/bindings/go/llvm/dibuilder.go +++ b/bindings/go/llvm/dibuilder.go @@ -585,3 +585,17 @@ func boolToCInt(v bool) C.int { } return 0 } + +//------------------------------------------------------------------------- +// llvm.Metadata +//------------------------------------------------------------------------- + +func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) { + ptr, nvals := llvmMetadataRefs(mds) + md.C = C.LLVMTemporaryMDNode(c.C, ptr, C.size_t(nvals)) + return +} + +func (md Metadata) ReplaceAllUsesWith(new Metadata) { + C.LLVMMetadataReplaceAllUsesWith(md.C, new.C) +} diff --git a/bindings/go/llvm/ir.go b/bindings/go/llvm/ir.go index 988db49f861d..a336c0ef44c7 100644 --- a/bindings/go/llvm/ir.go +++ b/bindings/go/llvm/ir.go @@ -784,11 +784,6 @@ func (c Context) MDNode(mds []Metadata) (md Metadata) { md.C = C.LLVMMDNode2(c.C, ptr, nvals) return } -func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) { - ptr, nvals := llvmMetadataRefs(mds) - md.C = C.LLVMTemporaryMDNode(c.C, ptr, nvals) - return -} func (v Value) ConstantAsMetadata() (md Metadata) { md.C = C.LLVMConstantAsMetadata(v.C) return @@ -1916,11 +1911,3 @@ func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassMan // the module provider. // See llvm::PassManagerBase::~PassManagerBase. func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) } - -//------------------------------------------------------------------------- -// llvm.Metadata -//------------------------------------------------------------------------- - -func (md Metadata) ReplaceAllUsesWith(new Metadata) { - C.LLVMMetadataReplaceAllUsesWith(md.C, new.C) -} diff --git a/include/llvm-c/DebugInfo.h b/include/llvm-c/DebugInfo.h index fff7bd569256..6d5e7fe8b118 100644 --- a/include/llvm-c/DebugInfo.h +++ b/include/llvm-c/DebugInfo.h @@ -830,6 +830,33 @@ LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder, LLVMMetadataRef Expr, LLVMMetadataRef Decl, uint32_t AlignInBits); +/** + * Create a new temporary \c MDNode. Suitable for use in constructing cyclic + * \c MDNode structures. A temporary \c MDNode is not uniqued, may be RAUW'd, + * and must be manually deleted with \c LLVMDisposeTemporaryMDNode. + * \param Ctx The context in which to construct the temporary node. + * \param Data The metadata elements. + * \param NumElements Number of metadata elements. + */ +LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data, + size_t NumElements); + +/** + * Deallocate a temporary node. + * + * Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining + * references will be reset. + * \param TempNode The temporary metadata node. + */ +void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode); + +/** + * Replace all uses of temporary metadata. + * \param TempTargetMetadata The temporary metadata node. + * \param Replacement The replacement metadata node. + */ +void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TempTargetMetadata, + LLVMMetadataRef Replacement); /** * Create a new descriptor for the specified global variable that is temporary diff --git a/lib/IR/DebugInfo.cpp b/lib/IR/DebugInfo.cpp index 8cf38e74135a..78ccb399fed8 100644 --- a/lib/IR/DebugInfo.cpp +++ b/lib/IR/DebugInfo.cpp @@ -1152,6 +1152,23 @@ LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder, unwrapDI(Decl), AlignInBits)); } +LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data, + size_t Count) { + return wrap( + MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release()); +} + +void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) { + MDNode::deleteTemporary(unwrapDI(TempNode)); +} + +void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata, + LLVMMetadataRef Replacement) { + auto *Node = unwrapDI(TargetMetadata); + Node->replaceAllUsesWith(unwrap(Replacement)); + MDNode::deleteTemporary(Node); +} + LLVMMetadataRef LLVMDIBuilderCreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef Builder, LLVMMetadataRef Scope, diff --git a/tools/llvm-c-test/debuginfo.c b/tools/llvm-c-test/debuginfo.c index 8bbe2b6d98f0..6cc351686003 100644 --- a/tools/llvm-c-test/debuginfo.c +++ b/tools/llvm-c-test/debuginfo.c @@ -93,14 +93,23 @@ int llvm_test_dibuilder(void) { LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty, VectorTy}; LLVMMetadataRef FunctionTy = LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 3, 0); + + LLVMMetadataRef ReplaceableFunctionMetadata = + LLVMDIBuilderCreateReplaceableCompositeType(DIB, 0x15, "foo", 3, + File, File, 42, + 0, 0, 0, + LLVMDIFlagFwdDecl, + "", 0); + + LLVMMetadataRef FooParamLocation = + LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0, + ReplaceableFunctionMetadata, NULL); LLVMMetadataRef FunctionMetadata = LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3, File, 42, FunctionTy, true, true, 42, 0, false); + LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata, FunctionMetadata); - LLVMMetadataRef FooParamLocation = - LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0, - FunctionMetadata, NULL); LLVMMetadataRef FooParamExpression = LLVMDIBuilderCreateExpression(DIB, NULL, 0); LLVMMetadataRef FooParamVar1 =