llvm.org GIT mirror llvm / 8dd938e
[IR] Add verifier support for llvm.module.flags. - Also, update the LangRef documentation on module flags to match the implementation. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172498 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 7 years ago
3 changed file(s) with 123 addition(s) and 20 deletion(s). Raw diff Collapse all Expand all
24612461 (or more) metadata with the same ID. The supported behaviors are
24622462 described below.
24632463 - The second element is a metadata string that is a unique ID for the
2464 metadata. How each ID is interpreted is documented below.
2464 metadata. Each module may only have one flag entry for each unique ID (not
2465 including entries with the **Require** behavior).
24652466 - The third element is the value of the flag.
24662467
24672468 When two (or more) modules are merged together, the resulting
2468 ``llvm.module.flags`` metadata is the union of the modules'
2469 ``llvm.module.flags`` metadata. The only exception being a flag with the
2470 *Override* behavior, which may override another flag's value (see
2471 below).
2469 ``llvm.module.flags`` metadata is the union of the modules' flags. That is, for
2470 each unique metadata ID string, there will be exactly one entry in the merged
2471 modules ``llvm.module.flags`` metadata table, and the value for that entry will
2472 be determined by the merge behavior flag, as described below. The only exception
2473 is that entries with the *Require* behavior are always preserved.
24722474
24732475 The following behaviors are supported:
24742476
24812483
24822484 * - 1
24832485 - **Error**
2484 Emits an error if two values disagree. It is an error to have an
2485 ID with both an Error and a Warning behavior.
2486 Emits an error if two values disagree, otherwise the resulting value
2487 is that of the operands.
24862488
24872489 * - 2
24882490 - **Warning**
2489 Emits a warning if two values disagree.
2491 Emits a warning if two values disagree. The result value will be the
2492 operand for the flag from the first module being linked.
24902493
24912494 * - 3
24922495 - **Require**
2493 Emits an error when the specified value is not present or doesn't
2494 have the specified value. It is an error for two (or more)
2495 ``llvm.module.flags`` with the same ID to have the Require behavior
2496 but different values. There may be multiple Require flags per ID.
2496 Adds a requirement that another module flag be present and have a
2497 specified value after linking is performed. The value must be a
2498 metadata pair, where the first element of the pair is the ID of the
2499 module flag to be restricted, and the second element of the pair is
2500 the value the module flag should be restricted to. This behavior can
2501 be used to restrict the allowable results (via triggering of an
2502 error) of linking IDs with the **Override** behavior.
24972503
24982504 * - 4
24992505 - **Override**
2500 Uses the specified value if the two values disagree. It is an
2501 error for two (or more) ``llvm.module.flags`` with the same ID
2502 to have the Override behavior but different values.
2506 Uses the specified value, regardless of the behavior or value of the
2507 other module. If both modules specify **Override**, but the values
2508 differ, an error will be emitted.
2509
2510 It is an error for a particular unique flag ID to have multiple behaviors,
2511 except in the case of **Require** (which adds restrictions on another metadata
2512 value) or **Override**.
25032513
25042514 An example of module flags:
25052515
25212531
25222532 - Metadata ``!1`` has the ID ``!"bar"`` and the value '37'. The
25232533 behavior if two or more ``!"bar"`` flags are seen is to use the value
2524 '37' if their values are not equal.
2534 '37'.
25252535
25262536 - Metadata ``!2`` has the ID ``!"qux"`` and the value '42'. The
25272537 behavior if two or more ``!"qux"`` flags are seen is to emit a
25332543
25342544 metadata !{ metadata !"foo", i32 1 }
25352545
2536 The behavior is to emit an error if the ``llvm.module.flags`` does
2537 not contain a flag with the ID ``!"foo"`` that has the value '1'. If
2538 two or more ``!"qux"`` flags exist, then they must have the same
2539 value or an error will be issued.
2546 The behavior is to emit an error if the ``llvm.module.flags`` does not
2547 contain a flag with the ID ``!"foo"`` that has the value '1' after linking is
2548 performed.
25402549
25412550 Objective-C Garbage Collection Module Flags Metadata
25422551 ----------------------------------------------------
199199 E = M.named_metadata_end(); I != E; ++I)
200200 visitNamedMDNode(*I);
201201
202 visitModuleFlags(M);
203
202204 // If the module is broken, abort at this time.
203205 return abortIfBroken();
204206 }
239241 void visitGlobalAlias(GlobalAlias &GA);
240242 void visitNamedMDNode(NamedMDNode &NMD);
241243 void visitMDNode(MDNode &MD, Function *F);
244 void visitModuleFlags(Module &M);
245 void visitModuleFlag(MDNode *Op, SmallSetVector &SeenIDs);
242246 void visitFunction(Function &F);
243247 void visitBasicBlock(BasicBlock &BB);
244248 using InstVisitor::visit;
517521
518522 Assert2(ActualF == F, "function-local metadata used in wrong function",
519523 &MD, Op);
524 }
525 }
526
527 void Verifier::visitModuleFlags(Module &M) {
528 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
529 if (!Flags) return;
530
531 // Scan each flag.
532 SmallSetVector SeenIDs;
533 for (unsigned I = 0, E = Flags->getNumOperands(); I != E; ++I) {
534 visitModuleFlag(Flags->getOperand(I), SeenIDs);
535 }
536 }
537
538 void Verifier::visitModuleFlag(MDNode *Op,
539 SmallSetVector &SeenIDs) {
540 // Each module flag should have three arguments, the merge behavior (a
541 // constant int), the flag ID (an MDString), and the value.
542 Assert1(Op->getNumOperands() == 3,
543 "incorrect number of operands in module flag", Op);
544 ConstantInt *Behavior = dyn_cast(Op->getOperand(0));
545 MDString *ID = dyn_cast(Op->getOperand(1));
546 Assert1(Behavior,
547 "invalid behavior operand in module flag (expected constant integer)",
548 Op->getOperand(0));
549 unsigned BehaviorValue = Behavior->getZExtValue();
550 Assert1((Module::Error <= BehaviorValue &&
551 BehaviorValue <= Module::Override),
552 "invalid behavior operand in module flag (unexpected constant)",
553 Op->getOperand(0));
554 Assert1(ID,
555 "invalid ID operand in module flag (expected metadata string)",
556 Op->getOperand(1));
557
558 // Unless this is a "requires" flag, check the ID is unique.
559 if (BehaviorValue != Module::Require) {
560 Assert1(SeenIDs.insert(ID),
561 "module flag identifiers must be unique (or of 'require' type)",
562 ID);
563 }
564
565 // If this is a "requires" flag, sanity check the value.
566 if (BehaviorValue == Module::Require) {
567 // The value should itself be an MDNode with two operands, a flag ID (an
568 // MDString), and a value.
569 MDNode *Value = dyn_cast(Op->getOperand(2));
570 Assert1(Value && Value->getNumOperands() == 2,
571 "invalid value for 'require' module flag (expected metadata pair)",
572 Op->getOperand(2));
573 Assert1(isa(Value->getOperand(0)),
574 ("invalid value for 'require' module flag "
575 "(first value operand should be a string)"),
576 Value->getOperand(0));
520577 }
521578 }
522579
0 ; RUN: not llvm-as < %s -o /dev/null 2>&1 | FileCheck %s
1
2 ; Check that module flags are structurally correct.
3 ;
4 ; CHECK: incorrect number of operands in module flag
5 ; CHECK: metadata !0
6 !0 = metadata !{ i32 1 }
7 ; CHECK: invalid behavior operand in module flag (expected constant integer)
8 ; CHECK: metadata !"foo"
9 !1 = metadata !{ metadata !"foo", metadata !"foo", i32 42 }
10 ; CHECK: invalid behavior operand in module flag (unexpected constant)
11 ; CHECK: i32 999
12 !2 = metadata !{ i32 999, metadata !"foo", i32 43 }
13 ; CHECK: invalid ID operand in module flag (expected metadata string)
14 ; CHECK: i32 1
15 !3 = metadata !{ i32 1, i32 1, i32 44 }
16 ; CHECK: invalid value for 'require' module flag (expected metadata pair)
17 ; CHECK: i32 45
18 !4 = metadata !{ i32 3, metadata !"bla", i32 45 }
19 ; CHECK: invalid value for 'require' module flag (expected metadata pair)
20 ; CHECK: metadata !
21 !5 = metadata !{ i32 3, metadata !"bla", metadata !{ i32 46 } }
22 ; CHECK: invalid value for 'require' module flag (first value operand should be a string)
23 ; CHECK: i32 47
24 !6 = metadata !{ i32 3, metadata !"bla", metadata !{ i32 47, i32 48 } }
25
26 ; Check that module flags only have unique IDs.
27 ;
28 ; CHECK: module flag identifiers must be unique (or of 'require' type)
29 !7 = metadata !{ i32 1, metadata !"foo", i32 49 }
30 !8 = metadata !{ i32 2, metadata !"foo", i32 50 }
31 ; CHECK-NOT: module flag identifiers must be unique
32 !9 = metadata !{ i32 2, metadata !"bar", i32 51 }
33 !10 = metadata !{ i32 3, metadata !"bar", i32 51 }
34
35 !llvm.module.flags = !{
36 !0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10 }