[llvm-branch-commits] [llvm-branch] r227475 - Merging r227319:

Hans Wennborg hans at hanshq.net
Thu Jan 29 09:33:20 PST 2015


Author: hans
Date: Thu Jan 29 11:33:19 2015
New Revision: 227475

URL: http://llvm.org/viewvc/llvm-project?rev=227475&view=rev
Log:
Merging r227319:
------------------------------------------------------------------------
r227319 | bsteinbr | 2015-01-28 08:35:59 -0800 (Wed, 28 Jan 2015) | 10 lines

Fix LLVMSetMetadata and LLVMAddNamedMetadataOperand for single value MDNodes

Summary:
MetadataAsValue uses a canonical format that strips the MDNode if it
contains only a single constant value. This triggers an assertion when
trying to cast the value to a MDNode.

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D7165
------------------------------------------------------------------------

Added:
    llvm/branches/release_36/test/Bindings/llvm-c/add_named_metadata_operand.ll
      - copied unchanged from r227319, llvm/trunk/test/Bindings/llvm-c/add_named_metadata_operand.ll
    llvm/branches/release_36/test/Bindings/llvm-c/set_metadata.ll
      - copied unchanged from r227319, llvm/trunk/test/Bindings/llvm-c/set_metadata.ll
    llvm/branches/release_36/tools/llvm-c-test/metadata.c
      - copied unchanged from r227319, llvm/trunk/tools/llvm-c-test/metadata.c
Modified:
    llvm/branches/release_36/   (props changed)
    llvm/branches/release_36/lib/IR/Core.cpp
    llvm/branches/release_36/tools/llvm-c-test/CMakeLists.txt
    llvm/branches/release_36/tools/llvm-c-test/llvm-c-test.h
    llvm/branches/release_36/tools/llvm-c-test/main.c

Propchange: llvm/branches/release_36/
------------------------------------------------------------------------------
--- svn:mergeinfo (original)
+++ svn:mergeinfo Thu Jan 29 11:33:19 2015
@@ -1,3 +1,3 @@
 /llvm/branches/Apple/Pertwee:110850,110961
 /llvm/branches/type-system-rewrite:133420-134817
-/llvm/trunk:155241,226023,226029,226044,226046,226048,226058,226075,226170-226171,226182,226473,226664,226708,226711,226755,227005,227085,227250,227260-227261,227294,227299-227300
+/llvm/trunk:155241,226023,226029,226044,226046,226048,226058,226075,226170-226171,226182,226473,226664,226708,226711,226755,227005,227085,227250,227260-227261,227294,227299-227300,227319

Modified: llvm/branches/release_36/lib/IR/Core.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_36/lib/IR/Core.cpp?rev=227475&r1=227474&r2=227475&view=diff
==============================================================================
--- llvm/branches/release_36/lib/IR/Core.cpp (original)
+++ llvm/branches/release_36/lib/IR/Core.cpp Thu Jan 29 11:33:19 2015
@@ -563,9 +563,23 @@ LLVMValueRef LLVMGetMetadata(LLVMValueRe
   return nullptr;
 }
 
-void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
-  MDNode *N =
-      MD ? cast<MDNode>(unwrap<MetadataAsValue>(MD)->getMetadata()) : nullptr;
+// MetadataAsValue uses a canonical format which strips the actual MDNode for
+// MDNode with just a single constant value, storing just a ConstantAsMetadata
+// This undoes this canonicalization, reconstructing the MDNode.
+static MDNode *extractMDNode(MetadataAsValue *MAV) {
+  Metadata *MD = MAV->getMetadata();
+  assert((isa<MDNode>(MD) || isa<ConstantAsMetadata>(MD)) &&
+      "Expected a metadata node or a canonicalized constant");
+
+  if (MDNode *N = dyn_cast<MDNode>(MD))
+    return N;
+
+  return MDNode::get(MAV->getContext(), MD);
+}
+
+void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
+  MDNode *N = Val ? extractMDNode(unwrap<MetadataAsValue>(Val)) : nullptr;
+
   unwrap<Instruction>(Inst)->setMetadata(KindID, N);
 }
 
@@ -795,7 +809,7 @@ void LLVMAddNamedMetadataOperand(LLVMMod
     return;
   if (!Val)
     return;
-  N->addOperand(cast<MDNode>(unwrap<MetadataAsValue>(Val)->getMetadata()));
+  N->addOperand(extractMDNode(unwrap<MetadataAsValue>(Val)));
 }
 
 /*--.. Operations on scalar constants ......................................--*/

Modified: llvm/branches/release_36/tools/llvm-c-test/CMakeLists.txt
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_36/tools/llvm-c-test/CMakeLists.txt?rev=227475&r1=227474&r2=227475&view=diff
==============================================================================
--- llvm/branches/release_36/tools/llvm-c-test/CMakeLists.txt (original)
+++ llvm/branches/release_36/tools/llvm-c-test/CMakeLists.txt Thu Jan 29 11:33:19 2015
@@ -41,6 +41,7 @@ add_llvm_tool(llvm-c-test
   include-all.c
   main.c
   module.c
+  metadata.c
   object.c
   targets.c
   )

Modified: llvm/branches/release_36/tools/llvm-c-test/llvm-c-test.h
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_36/tools/llvm-c-test/llvm-c-test.h?rev=227475&r1=227474&r2=227475&view=diff
==============================================================================
--- llvm/branches/release_36/tools/llvm-c-test/llvm-c-test.h (original)
+++ llvm/branches/release_36/tools/llvm-c-test/llvm-c-test.h Thu Jan 29 11:33:19 2015
@@ -27,6 +27,10 @@ int calc(void);
 // disassemble.c
 int disassemble(void);
 
+// metadata.c
+int add_named_metadata_operand(void);
+int set_metadata(void);
+
 // object.c
 int object_list_sections(void);
 int object_list_symbols(void);

Modified: llvm/branches/release_36/tools/llvm-c-test/main.c
URL: http://llvm.org/viewvc/llvm-project/llvm/branches/release_36/tools/llvm-c-test/main.c?rev=227475&r1=227474&r2=227475&view=diff
==============================================================================
--- llvm/branches/release_36/tools/llvm-c-test/main.c (original)
+++ llvm/branches/release_36/tools/llvm-c-test/main.c Thu Jan 29 11:33:19 2015
@@ -65,6 +65,10 @@ int main(int argc, char **argv) {
     return disassemble();
   } else if (argc == 2 && !strcmp(argv[1], "--calc")) {
     return calc();
+  } else if (argc == 2 && !strcmp(argv[1], "--add-named-metadata-operand")) {
+    return add_named_metadata_operand();
+  } else if (argc == 2 && !strcmp(argv[1], "--set-metadata")) {
+    return set_metadata();
   } else {
     print_usage();
   }





More information about the llvm-branch-commits mailing list