[llvm] r341085 - [LLVM-C] Add Bindings For Named Metadata
Robert Widmann via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 30 10:09:43 PDT 2018
Author: codafi
Date: Thu Aug 30 10:09:43 2018
New Revision: 341085
URL: http://llvm.org/viewvc/llvm-project?rev=341085&view=rev
Log:
[LLVM-C] Add Bindings For Named Metadata
Summary: Add a new type for named metadata nodes. Use this to implement iterators and accessors for NamedMDNodes and extend the echo test to use them to copy module-level debug information.
Reviewers: whitequark, deadalnix, aprantl, dexonsmith
Reviewed By: whitequark
Subscribers: Wallbraker, JDevlieghere, llvm-commits, harlanhaskins
Differential Revision: https://reviews.llvm.org/D47179
Modified:
llvm/trunk/include/llvm-c/Core.h
llvm/trunk/include/llvm-c/Types.h
llvm/trunk/include/llvm/IR/Metadata.h
llvm/trunk/lib/IR/Core.cpp
llvm/trunk/test/Bindings/llvm-c/echo.ll
llvm/trunk/tools/llvm-c-test/echo.cpp
Modified: llvm/trunk/include/llvm-c/Core.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Core.h?rev=341085&r1=341084&r2=341085&view=diff
==============================================================================
--- llvm/trunk/include/llvm-c/Core.h (original)
+++ llvm/trunk/include/llvm-c/Core.h Thu Aug 30 10:09:43 2018
@@ -843,6 +843,63 @@ LLVMContextRef LLVMGetModuleContext(LLVM
LLVMTypeRef LLVMGetTypeByName(LLVMModuleRef M, const char *Name);
/**
+ * Obtain an iterator to the first NamedMDNode in a Module.
+ *
+ * @see llvm::Module::named_metadata_begin()
+ */
+LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M);
+
+/**
+ * Obtain an iterator to the last NamedMDNode in a Module.
+ *
+ * @see llvm::Module::named_metadata_end()
+ */
+LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M);
+
+/**
+ * Advance a NamedMDNode iterator to the next NamedMDNode.
+ *
+ * Returns NULL if the iterator was already at the end and there are no more
+ * named metadata nodes.
+ */
+LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NamedMDNode);
+
+/**
+ * Decrement a NamedMDNode iterator to the previous NamedMDNode.
+ *
+ * Returns NULL if the iterator was already at the beginning and there are
+ * no previous named metadata nodes.
+ */
+LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NamedMDNode);
+
+/**
+ * Retrieve a NamedMDNode with the given name, returning NULL if no such
+ * node exists.
+ *
+ * @see llvm::Module::getNamedMetadata()
+ */
+LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
+ const char *Name, size_t NameLen);
+
+/**
+ * Retrieve a NamedMDNode with the given name, creating a new node if no such
+ * node exists.
+ *
+ * @see llvm::Module::getOrInsertNamedMetadata()
+ */
+LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
+ const char *Name,
+ size_t NameLen);
+
+/**
+ * Retrieve the name of a NamedMDNode.
+ *
+ * @see llvm::NamedMDNode::getName()
+ */
+const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NamedMD,
+ size_t *NameLen);
+
+/**
* Obtain the number of operands for named metadata in a module.
*
* @see llvm::Module::getNamedMetadata()
Modified: llvm/trunk/include/llvm-c/Types.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm-c/Types.h?rev=341085&r1=341084&r2=341085&view=diff
==============================================================================
--- llvm/trunk/include/llvm-c/Types.h (original)
+++ llvm/trunk/include/llvm-c/Types.h Thu Aug 30 10:09:43 2018
@@ -90,6 +90,13 @@ typedef struct LLVMOpaqueBasicBlock *LLV
typedef struct LLVMOpaqueMetadata *LLVMMetadataRef;
/**
+ * Represents an LLVM Named Metadata Node.
+ *
+ * This models llvm::NamedMDNode.
+ */
+typedef struct LLVMOpaqueNamedMDNode *LLVMNamedMDNodeRef;
+
+/**
* Represents an LLVM basic block builder.
*
* This models llvm::IRBuilder.
Modified: llvm/trunk/include/llvm/IR/Metadata.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/IR/Metadata.h?rev=341085&r1=341084&r2=341085&view=diff
==============================================================================
--- llvm/trunk/include/llvm/IR/Metadata.h (original)
+++ llvm/trunk/include/llvm/IR/Metadata.h Thu Aug 30 10:09:43 2018
@@ -1316,10 +1316,11 @@ public:
//===----------------------------------------------------------------------===//
/// A tuple of MDNodes.
///
-/// Despite its name, a NamedMDNode isn't itself an MDNode. NamedMDNodes belong
-/// to modules, have names, and contain lists of MDNodes.
+/// Despite its name, a NamedMDNode isn't itself an MDNode.
///
-/// TODO: Inherit from Metadata.
+/// NamedMDNodes are named module-level entities that contain lists of MDNodes.
+///
+/// It is illegal for a NamedMDNode to appear as an operand of an MDNode.
class NamedMDNode : public ilist_node<NamedMDNode> {
friend class LLVMContextImpl;
friend class Module;
@@ -1420,6 +1421,9 @@ public:
}
};
+// Create wrappers for C Binding types (see CBindingWrapping.h).
+DEFINE_ISA_CONVERSION_FUNCTIONS(NamedMDNode, LLVMNamedMDNodeRef)
+
} // end namespace llvm
#endif // LLVM_IR_METADATA_H
Modified: llvm/trunk/lib/IR/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/IR/Core.cpp?rev=341085&r1=341084&r2=341085&view=diff
==============================================================================
--- llvm/trunk/lib/IR/Core.cpp (original)
+++ llvm/trunk/lib/IR/Core.cpp Thu Aug 30 10:09:43 2018
@@ -1065,6 +1065,54 @@ unsigned LLVMGetMDNodeNumOperands(LLVMVa
return cast<MDNode>(MD->getMetadata())->getNumOperands();
}
+LLVMNamedMDNodeRef LLVMGetFirstNamedMetadata(LLVMModuleRef M) {
+ Module *Mod = unwrap(M);
+ Module::named_metadata_iterator I = Mod->named_metadata_begin();
+ if (I == Mod->named_metadata_end())
+ return nullptr;
+ return wrap(&*I);
+}
+
+LLVMNamedMDNodeRef LLVMGetLastNamedMetadata(LLVMModuleRef M) {
+ Module *Mod = unwrap(M);
+ Module::named_metadata_iterator I = Mod->named_metadata_end();
+ if (I == Mod->named_metadata_begin())
+ return nullptr;
+ return wrap(&*--I);
+}
+
+LLVMNamedMDNodeRef LLVMGetNextNamedMetadata(LLVMNamedMDNodeRef NMD) {
+ NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
+ Module::named_metadata_iterator I(NamedNode);
+ if (++I == NamedNode->getParent()->named_metadata_end())
+ return nullptr;
+ return wrap(&*I);
+}
+
+LLVMNamedMDNodeRef LLVMGetPreviousNamedMetadata(LLVMNamedMDNodeRef NMD) {
+ NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
+ Module::named_metadata_iterator I(NamedNode);
+ if (I == NamedNode->getParent()->named_metadata_begin())
+ return nullptr;
+ return wrap(&*--I);
+}
+
+LLVMNamedMDNodeRef LLVMGetNamedMetadata(LLVMModuleRef M,
+ const char *Name, size_t NameLen) {
+ return wrap(unwrap(M)->getNamedMetadata(StringRef(Name, NameLen)));
+}
+
+LLVMNamedMDNodeRef LLVMGetOrInsertNamedMetadata(LLVMModuleRef M,
+ const char *Name, size_t NameLen) {
+ return wrap(unwrap(M)->getOrInsertNamedMetadata({Name, NameLen}));
+}
+
+const char *LLVMGetNamedMetadataName(LLVMNamedMDNodeRef NMD, size_t *NameLen) {
+ NamedMDNode *NamedNode = unwrap<NamedMDNode>(NMD);
+ *NameLen = NamedNode->getName().size();
+ return NamedNode->getName().data();
+}
+
void LLVMGetMDNodeOperands(LLVMValueRef V, LLVMValueRef *Dest) {
auto *MD = cast<MetadataAsValue>(unwrap(V));
if (auto *MDV = dyn_cast<ValueAsMetadata>(MD->getMetadata())) {
Modified: llvm/trunk/test/Bindings/llvm-c/echo.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Bindings/llvm-c/echo.ll?rev=341085&r1=341084&r2=341085&view=diff
==============================================================================
--- llvm/trunk/test/Bindings/llvm-c/echo.ll (original)
+++ llvm/trunk/test/Bindings/llvm-c/echo.ll Thu Aug 30 10:09:43 2018
@@ -166,6 +166,11 @@ exit:
ret void
}
-!llvm.module.flags = !{!1}
+!llvm.module.flags = !{!0}
+!named = !{!1, !2, !3}
-!1 = !{i32 2, !"Debug Info Version", i32 3}
+!0 = !{i32 2, !"Debug Info Version", i32 3}
+!1 = distinct !{}
+!2 = distinct !{}
+!3 = !{!4}
+!4 = distinct !{}
Modified: llvm/trunk/tools/llvm-c-test/echo.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/tools/llvm-c-test/echo.cpp?rev=341085&r1=341084&r2=341085&view=diff
==============================================================================
--- llvm/trunk/tools/llvm-c-test/echo.cpp (original)
+++ llvm/trunk/tools/llvm-c-test/echo.cpp Thu Aug 30 10:09:43 2018
@@ -916,7 +916,7 @@ AliasDecl:
if (!Begin) {
if (End != nullptr)
report_fatal_error("Range has an end but no beginning");
- return;
+ goto NamedMDDecl;
}
Cur = Begin;
@@ -943,6 +943,38 @@ AliasDecl:
Cur = Next;
}
+
+NamedMDDecl:
+ LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
+ LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
+ if (!BeginMD) {
+ if (EndMD != nullptr)
+ report_fatal_error("Range has an end but no beginning");
+ return;
+ }
+
+ LLVMNamedMDNodeRef CurMD = BeginMD;
+ LLVMNamedMDNodeRef NextMD = nullptr;
+ while (true) {
+ size_t NameLen;
+ const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
+ if (LLVMGetNamedMetadata(M, Name, NameLen))
+ report_fatal_error("Named Metadata Node already cloned");
+ LLVMGetOrInsertNamedMetadata(M, Name, NameLen);
+
+ NextMD = LLVMGetNextNamedMetadata(CurMD);
+ if (NextMD == nullptr) {
+ if (CurMD != EndMD)
+ report_fatal_error("");
+ break;
+ }
+
+ LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
+ if (PrevMD != CurMD)
+ report_fatal_error("Next.Previous global is not Current");
+
+ CurMD = NextMD;
+ }
}
static void clone_symbols(LLVMModuleRef Src, LLVMModuleRef M) {
@@ -1041,7 +1073,7 @@ AliasClone:
if (!Begin) {
if (End != nullptr)
report_fatal_error("Range has an end but no beginning");
- return;
+ goto NamedMDClone;
}
Cur = Begin;
@@ -1073,6 +1105,47 @@ AliasClone:
Cur = Next;
}
+
+NamedMDClone:
+ LLVMNamedMDNodeRef BeginMD = LLVMGetFirstNamedMetadata(Src);
+ LLVMNamedMDNodeRef EndMD = LLVMGetLastNamedMetadata(Src);
+ if (!BeginMD) {
+ if (EndMD != nullptr)
+ report_fatal_error("Range has an end but no beginning");
+ return;
+ }
+
+ LLVMNamedMDNodeRef CurMD = BeginMD;
+ LLVMNamedMDNodeRef NextMD = nullptr;
+ while (true) {
+ size_t NameLen;
+ const char *Name = LLVMGetNamedMetadataName(CurMD, &NameLen);
+ LLVMNamedMDNodeRef NamedMD = LLVMGetNamedMetadata(M, Name, NameLen);
+ if (!NamedMD)
+ report_fatal_error("Named MD Node must have been declared already");
+
+ unsigned OperandCount = LLVMGetNamedMetadataNumOperands(Src, Name);
+ LLVMValueRef *OperandBuf = static_cast<LLVMValueRef *>(
+ safe_malloc(OperandCount * sizeof(LLVMValueRef)));
+ LLVMGetNamedMetadataOperands(Src, Name, OperandBuf);
+ for (unsigned i = 0, e = OperandCount; i != e; ++i) {
+ LLVMAddNamedMetadataOperand(M, Name, OperandBuf[i]);
+ }
+ free(OperandBuf);
+
+ NextMD = LLVMGetNextNamedMetadata(CurMD);
+ if (NextMD == nullptr) {
+ if (CurMD != EndMD)
+ report_fatal_error("Last Named MD Node does not match End");
+ break;
+ }
+
+ LLVMNamedMDNodeRef PrevMD = LLVMGetPreviousNamedMetadata(NextMD);
+ if (PrevMD != CurMD)
+ report_fatal_error("Next.Previous Named MD Node is not Current");
+
+ CurMD = NextMD;
+ }
}
int llvm_echo(void) {
@@ -1089,18 +1162,6 @@ int llvm_echo(void) {
LLVMSetSourceFileName(M, SourceFileName, SourceFileLen);
LLVMSetModuleIdentifier(M, ModuleName, ModuleIdentLen);
- size_t SourceFlagsLen;
- LLVMModuleFlagEntry *ModuleFlags =
- LLVMCopyModuleFlagsMetadata(Src, &SourceFlagsLen);
- for (unsigned i = 0; i < SourceFlagsLen; ++i) {
- size_t EntryNameLen;
- const char *EntryName =
- LLVMModuleFlagEntriesGetKey(ModuleFlags, i, &EntryNameLen);
- LLVMAddModuleFlag(M, LLVMModuleFlagEntriesGetFlagBehavior(ModuleFlags, i),
- EntryName, EntryNameLen,
- LLVMModuleFlagEntriesGetMetadata(ModuleFlags, i));
- }
-
LLVMSetTarget(M, LLVMGetTarget(Src));
LLVMSetModuleDataLayout(M, LLVMGetModuleDataLayout(Src));
if (strcmp(LLVMGetDataLayoutStr(M), LLVMGetDataLayoutStr(Src)))
@@ -1115,7 +1176,6 @@ int llvm_echo(void) {
char *Str = LLVMPrintModuleToString(M);
fputs(Str, stdout);
- LLVMDisposeModuleFlagsMetadata(ModuleFlags);
LLVMDisposeMessage(Str);
LLVMDisposeModule(Src);
LLVMDisposeModule(M);
More information about the llvm-commits
mailing list