llvm.org GIT mirror llvm / 12bfff4
[IR] Add verification for module flags with the "require" behavior. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172549 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 7 years ago
2 changed file(s) with 54 addition(s) and 12 deletion(s). Raw diff Collapse all Expand all
242242 void visitNamedMDNode(NamedMDNode &NMD);
243243 void visitMDNode(MDNode &MD, Function *F);
244244 void visitModuleFlags(Module &M);
245 void visitModuleFlag(MDNode *Op, SmallSetVector &SeenIDs);
245 void visitModuleFlag(MDNode *Op, DenseMap &SeenIDs,
246 SmallVectorImpl &Requirements);
246247 void visitFunction(Function &F);
247248 void visitBasicBlock(BasicBlock &BB);
248249 using InstVisitor::visit;
528529 const NamedMDNode *Flags = M.getModuleFlagsMetadata();
529530 if (!Flags) return;
530531
531 // Scan each flag.
532 SmallSetVector SeenIDs;
532 // Scan each flag, and track the flags and requirements.
533 DenseMap SeenIDs;
534 SmallVector Requirements;
533535 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) {
536 visitModuleFlag(Flags->getOperand(I), SeenIDs, Requirements);
537 }
538
539 // Validate that the requirements in the module are valid.
540 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
541 MDNode *Requirement = Requirements[I];
542 MDString *Flag = cast(Requirement->getOperand(0));
543 Value *ReqValue = Requirement->getOperand(1);
544
545 MDNode *Op = SeenIDs.lookup(Flag);
546 if (!Op) {
547 CheckFailed("invalid requirement on flag, flag is not present in module",
548 Flag);
549 continue;
550 }
551
552 if (Op->getOperand(2) != ReqValue) {
553 CheckFailed(("invalid requirement on flag, "
554 "flag does not have the required value"),
555 Flag);
556 continue;
557 }
558 }
559 }
560
561 void Verifier::visitModuleFlag(MDNode *Op, DenseMap&SeenIDs,
562 SmallVectorImpl &Requirements) {
540563 // Each module flag should have three arguments, the merge behavior (a
541564 // constant int), the flag ID (an MDString), and the value.
542565 Assert1(Op->getNumOperands() == 3,
557580
558581 // Unless this is a "requires" flag, check the ID is unique.
559582 if (BehaviorValue != Module::Require) {
560 Assert1(SeenIDs.insert(ID),
583 bool Inserted = SeenIDs.insert(std::make_pair(ID, Op)).second;
584 Assert1(Inserted,
561585 "module flag identifiers must be unique (or of 'require' type)",
562586 ID);
563587 }
574598 ("invalid value for 'require' module flag "
575599 "(first value operand should be a string)"),
576600 Value->getOperand(0));
601
602 // Append it to the list of requirements, to check once all module flags are
603 // scanned.
604 Requirements.push_back(Value);
577605 }
578606 }
579607
3030 !8 = metadata !{ i32 2, metadata !"foo", i32 50 }
3131 ; CHECK-NOT: module flag identifiers must be unique
3232 !9 = metadata !{ i32 2, metadata !"bar", i32 51 }
33 !10 = metadata !{ i32 3, metadata !"bar", i32 51 }
33 !10 = metadata !{ i32 3, metadata !"bar", metadata !{ metadata !"bar", i32 51 } }
34
35 ; Check that any 'require' module flags are valid.
36 ; CHECK: invalid requirement on flag, flag is not present in module
37 !11 = metadata !{ i32 3, metadata !"bar",
38 metadata !{ metadata !"no-such-flag", i32 52 } }
39 ; CHECK: invalid requirement on flag, flag does not have the required value
40 !12 = metadata !{ i32 1, metadata !"flag-0", i32 53 }
41 !13 = metadata !{ i32 3, metadata !"bar",
42 metadata !{ metadata !"flag-0", i32 54 } }
43 ; CHECK-NOT: invalid requirement on flag, flag is not present in module
44 ; CHECK-NOT: invalid requirement on flag, flag does not have the required value
45 !14 = metadata !{ i32 1, metadata !"flag-1", i32 55 }
46 !15 = metadata !{ i32 3, metadata !"bar",
47 metadata !{ metadata !"flag-1", i32 55 } }
3448
3549 !llvm.module.flags = !{
36 !0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10 }
50 !0, !1, !2, !3, !4, !5, !6, !7, !8, !9, !10, !11, !12, !13, !14, !15 }