llvm.org GIT mirror llvm / 5db391c
[IR] Add 'Append' and 'AppendUnique' module flag behaviors. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172659 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 7 years ago
7 changed file(s) with 135 addition(s) and 39 deletion(s). Raw diff Collapse all Expand all
25072507 other module. If both modules specify **Override**, but the values
25082508 differ, an error will be emitted.
25092509
2510 * - 5
2511 - **Append**
2512 Appends the two values, which are required to be metadata nodes.
2513
2514 * - 6
2515 - **AppendUnique**
2516 Appends the two values, which are required to be metadata
2517 nodes. However, duplicate entries in the second list are dropped
2518 during the append operation.
2519
25102520 It is an error for a particular unique flag ID to have multiple behaviors,
25112521 except in the case of **Require** (which adds restrictions on another metadata
25122522 value) or **Override**.
146146 /// An enumeration for describing the size of a pointer on the target machine.
147147 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
148148
149 /// An enumeration for the supported behaviors of module flags. The following
150 /// module flags behavior values are supported:
151 ///
152 /// Value Behavior
153 /// ----- --------
154 /// 1 Error
155 /// Emits an error if two values disagree.
156 ///
157 /// 2 Warning
158 /// Emits a warning if two values disagree.
159 ///
160 /// 3 Require
161 /// Emits an error when the specified value is not present
162 /// or doesn't have the specified value. It is an error for
163 /// two (or more) llvm.module.flags with the same ID to have
164 /// the Require behavior but different values. There may be
165 /// multiple Require flags per ID.
166 ///
167 /// 4 Override
168 /// Uses the specified value if the two values disagree. It
169 /// is an error for two (or more) llvm.module.flags with the
170 /// same ID to have the Override behavior but different
171 /// values.
172 enum ModFlagBehavior { Error = 1, Warning = 2, Require = 3, Override = 4 };
149 /// This enumeration defines the supported behaviors of module flags.
150 enum ModFlagBehavior {
151 /// Emits an error if two values disagree, otherwise the resulting value is
152 /// that of the operands.
153 Error = 1,
154
155 /// Emits a warning if two values disagree. The result value will be the
156 /// operand for the flag from the first module being linked.
157 Warning = 2,
158
159 /// Adds a requirement that another module flag be present and have a
160 /// specified value after linking is performed. The value must be a metadata
161 /// pair, where the first element of the pair is the ID of the module flag
162 /// to be restricted, and the second element of the pair is the value the
163 /// module flag should be restricted to. This behavior can be used to
164 /// restrict the allowable results (via triggering of an error) of linking
165 /// IDs with the **Override** behavior.
166 Require = 3,
167
168 /// Uses the specified value, regardless of the behavior or value of the
169 /// other module. If both modules specify **Override**, but the values
170 /// differ, an error will be emitted.
171 Override = 4,
172
173 /// Appends the two values, which are required to be metadata nodes.
174 Append = 5,
175
176 /// Appends the two values, which are required to be metadata
177 /// nodes. However, duplicate entries in the second list are dropped
178 /// during the append operation.
179 AppendUnique = 6
180 };
173181
174182 struct ModuleFlagEntry {
175183 ModFlagBehavior Behavior;
570570 "invalid behavior operand in module flag (expected constant integer)",
571571 Op->getOperand(0));
572572 unsigned BehaviorValue = Behavior->getZExtValue();
573 Assert1((Module::Error <= BehaviorValue &&
574 BehaviorValue <= Module::Override),
575 "invalid behavior operand in module flag (unexpected constant)",
576 Op->getOperand(0));
577573 Assert1(ID,
578574 "invalid ID operand in module flag (expected metadata string)",
579575 Op->getOperand(1));
580576
581 // Unless this is a "requires" flag, check the ID is unique.
582 if (BehaviorValue != Module::Require) {
583 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
584 Assert1(Inserted,
585 "module flag identifiers must be unique (or of 'require' type)",
586 ID);
587 }
588
589 // If this is a "requires" flag, sanity check the value.
590 if (BehaviorValue == Module::Require) {
577 // Sanity check the values for behaviors with additional requirements.
578 switch (BehaviorValue) {
579 default:
580 Assert1(false,
581 "invalid behavior operand in module flag (unexpected constant)",
582 Op->getOperand(0));
583 break;
584
585 case Module::Error:
586 case Module::Warning:
587 case Module::Override:
588 // These behavior types accept any value.
589 break;
590
591 case Module::Require: {
591592 // The value should itself be an MDNode with two operands, a flag ID (an
592593 // MDString), and a value.
593594 MDNode *Value = dyn_cast(Op->getOperand(2));
602603 // Append it to the list of requirements, to check once all module flags are
603604 // scanned.
604605 Requirements.push_back(Value);
606 break;
607 }
608
609 case Module::Append:
610 case Module::AppendUnique: {
611 // These behavior types require the operand be an MDNode.
612 Assert1(isa(Op->getOperand(2)),
613 "invalid value for 'append'-type module flag "
614 "(expected a metadata node)", Op->getOperand(2));
615 break;
616 }
617 }
618
619 // Unless this is a "requires" flag, check the ID is unique.
620 if (BehaviorValue != Module::Require) {
621 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
622 Assert1(Inserted,
623 "module flag identifiers must be unique (or of 'require' type)",
624 ID);
605625 }
606626 }
607627
10851085 }
10861086 continue;
10871087 }
1088 case Module::Append: {
1089 MDNode *DstValue = cast(DstOp->getOperand(2));
1090 MDNode *SrcValue = cast(SrcOp->getOperand(2));
1091 unsigned NumOps = DstValue->getNumOperands() + SrcValue->getNumOperands();
1092 Value **VP, **Values = VP = new Value*[NumOps];
1093 for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i, ++VP)
1094 *VP = DstValue->getOperand(i);
1095 for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i, ++VP)
1096 *VP = SrcValue->getOperand(i);
1097 DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1098 ArrayRef(Values,
1099 NumOps)));
1100 delete[] Values;
1101 break;
1102 }
1103 case Module::AppendUnique: {
1104 SmallSetVector Elts;
1105 MDNode *DstValue = cast(DstOp->getOperand(2));
1106 MDNode *SrcValue = cast(SrcOp->getOperand(2));
1107 for (unsigned i = 0, e = DstValue->getNumOperands(); i != e; ++i)
1108 Elts.insert(DstValue->getOperand(i));
1109 for (unsigned i = 0, e = SrcValue->getNumOperands(); i != e; ++i)
1110 Elts.insert(SrcValue->getOperand(i));
1111 DstOp->replaceOperandWith(2, MDNode::get(DstM->getContext(),
1112 ArrayRef(Elts.begin(),
1113 Elts.end())));
1114 break;
1115 }
10881116 }
10891117 }
10901118
0 ; RUN: llvm-link %s %p/module-flags-8-b.ll -S -o - | sort | FileCheck %s
1
2 ; Test append-type module flags.
3
4 ; CHECK: !0 = metadata !{i32 5, metadata !"flag-0", metadata !1}
5 ; CHECK: !1 = metadata !{i32 0, i32 0, i32 1}
6 ; CHECK: !2 = metadata !{i32 6, metadata !"flag-1", metadata !3}
7 ; CHECK: !3 = metadata !{i32 0, i32 1, i32 2}
8 ; CHECK: !llvm.module.flags = !{!0, !2}
9
10 !0 = metadata !{ i32 5, metadata !"flag-0", metadata !{ i32 0 } }
11 !1 = metadata !{ i32 6, metadata !"flag-1", metadata !{ i32 0, i32 1 } }
12
13 !llvm.module.flags = !{ !0, !1 }
0 ; This file is used with module-flags-6-a.ll
1 ; RUN: true
2
3 !0 = metadata !{ i32 5, metadata !"flag-0", metadata !{ i32 0, i32 1 } }
4 !1 = metadata !{ i32 6, metadata !"flag-1", metadata !{ i32 1, i32 2 } }
5
6 !llvm.module.flags = !{ !0, !1 }
3232 !9 = metadata !{ i32 2, metadata !"bar", i32 51 }
3333 !10 = metadata !{ i32 3, metadata !"bar", metadata !{ metadata !"bar", i32 51 } }
3434
35 ; Check that any 'append'-type module flags are valid.
36 ; CHECK: invalid value for 'append'-type module flag (expected a metadata node)
37 !16 = metadata !{ i32 5, metadata !"flag-2", i32 56 }
38 ; CHECK: invalid value for 'append'-type module flag (expected a metadata node)
39 !17 = metadata !{ i32 5, metadata !"flag-3", i32 57 }
40 ; CHECK-NOT: invalid value for 'append'-type module flag (expected a metadata node)
41 !18 = metadata !{ i32 5, metadata !"flag-4", metadata !{ i32 57 } }
42
3543 ; Check that any 'require' module flags are valid.
3644 ; CHECK: invalid requirement on flag, flag is not present in module
3745 !11 = metadata !{ i32 3, metadata !"bar",
4755 metadata !{ metadata !"flag-1", i32 55 } }
4856
4957 !llvm.module.flags = !{
50 !0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15 }
58 !0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15,
59 !16, !17, !18 }