llvm.org GIT mirror llvm / f65176f
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 ------------------------------------------------------------------------ git-svn-id: https://llvm.org/svn/llvm-project/llvm/branches/release_36@227475 91177308-0d34-0410-b5e6-96231b3b80d8 Hans Wennborg 4 years ago
7 changed file(s) with 70 addition(s) and 4 deletion(s). Raw diff Collapse all Expand all
562562 return nullptr;
563563 }
564564
565 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef MD) {
566 MDNode *N =
567 MD ? cast(unwrap(MD)->getMetadata()) : nullptr;
565 // MetadataAsValue uses a canonical format which strips the actual MDNode for
566 // MDNode with just a single constant value, storing just a ConstantAsMetadata
567 // This undoes this canonicalization, reconstructing the MDNode.
568 static MDNode *extractMDNode(MetadataAsValue *MAV) {
569 Metadata *MD = MAV->getMetadata();
570 assert((isa(MD) || isa(MD)) &&
571 "Expected a metadata node or a canonicalized constant");
572
573 if (MDNode *N = dyn_cast(MD))
574 return N;
575
576 return MDNode::get(MAV->getContext(), MD);
577 }
578
579 void LLVMSetMetadata(LLVMValueRef Inst, unsigned KindID, LLVMValueRef Val) {
580 MDNode *N = Val ? extractMDNode(unwrap(Val)) : nullptr;
581
568582 unwrap(Inst)->setMetadata(KindID, N);
569583 }
570584
794808 return;
795809 if (!Val)
796810 return;
797 N->addOperand(cast(unwrap(Val)->getMetadata()));
811 N->addOperand(extractMDNode(unwrap(Val)));
798812 }
799813
800814 /*--.. Operations on scalar constants ......................................--*/
0 ; RUN: llvm-c-test --add-named-metadata-operand < /dev/null
1 ; This used to trigger an assertion
0 ; RUN: llvm-c-test --set-metadata < /dev/null
1 ; This used to trigger an assertion
4040 include-all.c
4141 main.c
4242 module.c
43 metadata.c
4344 object.c
4445 targets.c
4546 )
2626 // disassemble.c
2727 int disassemble(void);
2828
29 // metadata.c
30 int add_named_metadata_operand(void);
31 int set_metadata(void);
32
2933 // object.c
3034 int object_list_sections(void);
3135 int object_list_symbols(void);
6464 return disassemble();
6565 } else if (argc == 2 && !strcmp(argv[1], "--calc")) {
6666 return calc();
67 } else if (argc == 2 && !strcmp(argv[1], "--add-named-metadata-operand")) {
68 return add_named_metadata_operand();
69 } else if (argc == 2 && !strcmp(argv[1], "--set-metadata")) {
70 return set_metadata();
6771 } else {
6872 print_usage();
6973 }
0 /*===-- object.c - tool for testing libLLVM and llvm-c API ----------------===*\
1 |* *|
2 |* The LLVM Compiler Infrastructure *|
3 |* *|
4 |* This file is distributed under the University of Illinois Open Source *|
5 |* License. See LICENSE.TXT for details. *|
6 |* *|
7 |*===----------------------------------------------------------------------===*|
8 |* *|
9 |* This file implements the --add-named-metadata-operand and --set-metadata *|
10 |* commands in llvm-c-test. *|
11 |* *|
12 \*===----------------------------------------------------------------------===*/
13
14 #include "llvm-c-test.h"
15 #include "llvm-c/Core.h"
16
17 int add_named_metadata_operand(void) {
18 LLVMModuleRef m = LLVMModuleCreateWithName("Mod");
19 LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) };
20
21 // This used to trigger an assertion
22 LLVMAddNamedMetadataOperand(m, "name", LLVMMDNode(values, 1));
23
24 return 0;
25 }
26
27 int set_metadata(void) {
28 LLVMBuilderRef b = LLVMCreateBuilder();
29 LLVMValueRef values[] = { LLVMConstInt(LLVMInt32Type(), 0, 0) };
30
31 // This used to trigger an assertion
32 LLVMSetMetadata(
33 LLVMBuildRetVoid(b),
34 LLVMGetMDKindID("kind", 4),
35 LLVMMDNode(values, 1));
36
37 return 0;
38 }