Skip to content

Commit a428eba

Browse files
committed
[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 llvm-svn: 332010
1 parent 262e090 commit a428eba

File tree

7 files changed

+70
-33
lines changed

7 files changed

+70
-33
lines changed

llvm/bindings/go/llvm/IRBindings.cpp

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,13 +36,6 @@ LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
3636
MDNode::get(*unwrap(C), ArrayRef<Metadata *>(unwrap(MDs), Count)));
3737
}
3838

39-
LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs,
40-
unsigned Count) {
41-
return wrap(MDTuple::getTemporary(*unwrap(C),
42-
ArrayRef<Metadata *>(unwrap(MDs), Count))
43-
.release());
44-
}
45-
4639
void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
4740
LLVMMetadataRef Val) {
4841
NamedMDNode *N = unwrap(M)->getOrInsertNamedMetadata(name);
@@ -58,12 +51,6 @@ void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD) {
5851
unwrap<Instruction>(Inst)->setMetadata(KindID, N);
5952
}
6053

61-
void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New) {
62-
auto *Node = unwrap<MDNode>(MD);
63-
Node->replaceAllUsesWith(unwrap<Metadata>(New));
64-
MDNode::deleteTemporary(Node);
65-
}
66-
6754
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
6855
unsigned Col, LLVMMetadataRef Scope,
6956
LLVMMetadataRef InlinedAt) {

llvm/bindings/go/llvm/IRBindings.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,11 @@ LLVMMetadataRef LLVMConstantAsMetadata(LLVMValueRef Val);
3838
LLVMMetadataRef LLVMMDString2(LLVMContextRef C, const char *Str, unsigned SLen);
3939
LLVMMetadataRef LLVMMDNode2(LLVMContextRef C, LLVMMetadataRef *MDs,
4040
unsigned Count);
41-
LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef C, LLVMMetadataRef *MDs,
42-
unsigned Count);
4341

4442
void LLVMAddNamedMetadataOperand2(LLVMModuleRef M, const char *name,
4543
LLVMMetadataRef Val);
4644
void LLVMSetMetadata2(LLVMValueRef Inst, unsigned KindID, LLVMMetadataRef MD);
4745

48-
void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef MD, LLVMMetadataRef New);
49-
5046
void LLVMSetCurrentDebugLocation2(LLVMBuilderRef Bref, unsigned Line,
5147
unsigned Col, LLVMMetadataRef Scope,
5248
LLVMMetadataRef InlinedAt);

llvm/bindings/go/llvm/dibuilder.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -585,3 +585,17 @@ func boolToCInt(v bool) C.int {
585585
}
586586
return 0
587587
}
588+
589+
//-------------------------------------------------------------------------
590+
// llvm.Metadata
591+
//-------------------------------------------------------------------------
592+
593+
func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) {
594+
ptr, nvals := llvmMetadataRefs(mds)
595+
md.C = C.LLVMTemporaryMDNode(c.C, ptr, C.size_t(nvals))
596+
return
597+
}
598+
599+
func (md Metadata) ReplaceAllUsesWith(new Metadata) {
600+
C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)
601+
}

llvm/bindings/go/llvm/ir.go

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -784,11 +784,6 @@ func (c Context) MDNode(mds []Metadata) (md Metadata) {
784784
md.C = C.LLVMMDNode2(c.C, ptr, nvals)
785785
return
786786
}
787-
func (c Context) TemporaryMDNode(mds []Metadata) (md Metadata) {
788-
ptr, nvals := llvmMetadataRefs(mds)
789-
md.C = C.LLVMTemporaryMDNode(c.C, ptr, nvals)
790-
return
791-
}
792787
func (v Value) ConstantAsMetadata() (md Metadata) {
793788
md.C = C.LLVMConstantAsMetadata(v.C)
794789
return
@@ -1916,11 +1911,3 @@ func (pm PassManager) FinalizeFunc() bool { return C.LLVMFinalizeFunctionPassMan
19161911
// the module provider.
19171912
// See llvm::PassManagerBase::~PassManagerBase.
19181913
func (pm PassManager) Dispose() { C.LLVMDisposePassManager(pm.C) }
1919-
1920-
//-------------------------------------------------------------------------
1921-
// llvm.Metadata
1922-
//-------------------------------------------------------------------------
1923-
1924-
func (md Metadata) ReplaceAllUsesWith(new Metadata) {
1925-
C.LLVMMetadataReplaceAllUsesWith(md.C, new.C)
1926-
}

llvm/include/llvm-c/DebugInfo.h

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -830,6 +830,33 @@ LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder,
830830
LLVMMetadataRef Expr,
831831
LLVMMetadataRef Decl,
832832
uint32_t AlignInBits);
833+
/**
834+
* Create a new temporary \c MDNode. Suitable for use in constructing cyclic
835+
* \c MDNode structures. A temporary \c MDNode is not uniqued, may be RAUW'd,
836+
* and must be manually deleted with \c LLVMDisposeTemporaryMDNode.
837+
* \param Ctx The context in which to construct the temporary node.
838+
* \param Data The metadata elements.
839+
* \param NumElements Number of metadata elements.
840+
*/
841+
LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
842+
size_t NumElements);
843+
844+
/**
845+
* Deallocate a temporary node.
846+
*
847+
* Calls \c replaceAllUsesWith(nullptr) before deleting, so any remaining
848+
* references will be reset.
849+
* \param TempNode The temporary metadata node.
850+
*/
851+
void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode);
852+
853+
/**
854+
* Replace all uses of temporary metadata.
855+
* \param TempTargetMetadata The temporary metadata node.
856+
* \param Replacement The replacement metadata node.
857+
*/
858+
void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TempTargetMetadata,
859+
LLVMMetadataRef Replacement);
833860

834861
/**
835862
* Create a new descriptor for the specified global variable that is temporary

llvm/lib/IR/DebugInfo.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1152,6 +1152,23 @@ LLVMDIBuilderCreateGlobalVariableExpression(LLVMDIBuilderRef Builder,
11521152
unwrapDI<MDNode>(Decl), AlignInBits));
11531153
}
11541154

1155+
LLVMMetadataRef LLVMTemporaryMDNode(LLVMContextRef Ctx, LLVMMetadataRef *Data,
1156+
size_t Count) {
1157+
return wrap(
1158+
MDTuple::getTemporary(*unwrap(Ctx), {unwrap(Data), Count}).release());
1159+
}
1160+
1161+
void LLVMDisposeTemporaryMDNode(LLVMMetadataRef TempNode) {
1162+
MDNode::deleteTemporary(unwrapDI<MDNode>(TempNode));
1163+
}
1164+
1165+
void LLVMMetadataReplaceAllUsesWith(LLVMMetadataRef TargetMetadata,
1166+
LLVMMetadataRef Replacement) {
1167+
auto *Node = unwrapDI<MDNode>(TargetMetadata);
1168+
Node->replaceAllUsesWith(unwrap<Metadata>(Replacement));
1169+
MDNode::deleteTemporary(Node);
1170+
}
1171+
11551172
LLVMMetadataRef
11561173
LLVMDIBuilderCreateTempGlobalVariableFwdDecl(LLVMDIBuilderRef Builder,
11571174
LLVMMetadataRef Scope,

llvm/tools/llvm-c-test/debuginfo.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -93,14 +93,23 @@ int llvm_test_dibuilder(void) {
9393
LLVMMetadataRef ParamTypes[] = {Int64Ty, Int64Ty, VectorTy};
9494
LLVMMetadataRef FunctionTy =
9595
LLVMDIBuilderCreateSubroutineType(DIB, File, ParamTypes, 3, 0);
96+
97+
LLVMMetadataRef ReplaceableFunctionMetadata =
98+
LLVMDIBuilderCreateReplaceableCompositeType(DIB, 0x15, "foo", 3,
99+
File, File, 42,
100+
0, 0, 0,
101+
LLVMDIFlagFwdDecl,
102+
"", 0);
103+
104+
LLVMMetadataRef FooParamLocation =
105+
LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0,
106+
ReplaceableFunctionMetadata, NULL);
96107
LLVMMetadataRef FunctionMetadata =
97108
LLVMDIBuilderCreateFunction(DIB, File, "foo", 3, "foo", 3,
98109
File, 42, FunctionTy, true, true,
99110
42, 0, false);
111+
LLVMMetadataReplaceAllUsesWith(ReplaceableFunctionMetadata, FunctionMetadata);
100112

101-
LLVMMetadataRef FooParamLocation =
102-
LLVMDIBuilderCreateDebugLocation(LLVMGetGlobalContext(), 42, 0,
103-
FunctionMetadata, NULL);
104113
LLVMMetadataRef FooParamExpression =
105114
LLVMDIBuilderCreateExpression(DIB, NULL, 0);
106115
LLVMMetadataRef FooParamVar1 =

0 commit comments

Comments
 (0)