llvm.org GIT mirror llvm / 1e08165
[Linker] Change module flag linking to be more extensible. - Instead of computing a bunch of buckets of different flag types, just do an incremental link resolving conflicts as they arise. - This also has the advantage of making the link result deterministic and not dependent on map iteration order. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@172634 91177308-0d34-0410-b5e6-96231b3b80d8 Daniel Dunbar 7 years ago
5 changed file(s) with 124 addition(s) and 145 deletion(s). Raw diff Collapse all Expand all
420420 }
421421
422422 void computeTypeMapping();
423 bool categorizeModuleFlagNodes(const NamedMDNode *ModFlags,
424 DenseMap &ErrorNode,
425 DenseMap &WarningNode,
426 DenseMap &OverrideNode,
427 DenseMap
428 SmallSetVector > &RequireNodes,
429 SmallSetVector &SeenIDs);
430423
431424 bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV);
432425 bool linkGlobalProto(GlobalVariable *SrcGV);
986979 }
987980 }
988981
989 /// categorizeModuleFlagNodes - Categorize the module flags according to their
990 /// type: Error, Warning, Override, and Require.
991 bool ModuleLinker::
992 categorizeModuleFlagNodes(const NamedMDNode *ModFlags,
993 DenseMap &ErrorNode,
994 DenseMap &WarningNode,
995 DenseMap &OverrideNode,
996 DenseMap
997 SmallSetVector > &RequireNodes,
998 SmallSetVector &SeenIDs) {
999 bool HasErr = false;
1000
1001 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
1002 MDNode *Op = ModFlags->getOperand(I);
1003 ConstantInt *Behavior = cast(Op->getOperand(0));
1004 MDString *ID = cast(Op->getOperand(1));
1005 Value *Val = Op->getOperand(2);
1006 switch (Behavior->getZExtValue()) {
1007 case Module::Error: {
1008 MDNode *&ErrNode = ErrorNode[ID];
1009 if (!ErrNode) ErrNode = Op;
1010 if (ErrNode->getOperand(2) != Val)
1011 HasErr = emitError("linking module flags '" + ID->getString() +
1012 "': IDs have conflicting values");
1013 break;
1014 }
1015 case Module::Warning: {
1016 MDNode *&WarnNode = WarningNode[ID];
1017 if (!WarnNode) WarnNode = Op;
1018 if (WarnNode->getOperand(2) != Val)
1019 errs() << "WARNING: linking module flags '" << ID->getString()
1020 << "': IDs have conflicting values";
1021 break;
1022 }
1023 case Module::Require: RequireNodes[ID].insert(Op); break;
1024 case Module::Override: {
1025 MDNode *&OvrNode = OverrideNode[ID];
1026 if (!OvrNode) OvrNode = Op;
1027 if (OvrNode->getOperand(2) != Val)
1028 HasErr = emitError("linking module flags '" + ID->getString() +
1029 "': IDs have conflicting override values");
1030 break;
1031 }
1032 }
1033
1034 SeenIDs.insert(ID);
1035 }
1036
1037 return HasErr;
1038 }
1039
1040982 /// linkModuleFlagsMetadata - Merge the linker flags in Src into the Dest
1041983 /// module.
1042984 bool ModuleLinker::linkModuleFlagsMetadata() {
985 // If the source module has no module flags, we are done.
1043986 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1044987 if (!SrcModFlags) return false;
1045988
1046 NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
1047
1048989 // If the destination module doesn't have module flags yet, then just copy
1049990 // over the source module's flags.
991 NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
1050992 if (DstModFlags->getNumOperands() == 0) {
1051993 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1052994 DstModFlags->addOperand(SrcModFlags->getOperand(I));
1054996 return false;
1055997 }
1056998
999 // First build a map of the existing module flags and requirements.
1000 DenseMap Flags;
1001 SmallSetVector Requirements;
1002 for (unsigned I = 0, E = DstModFlags->getNumOperands(); I != E; ++I) {
1003 MDNode *Op = DstModFlags->getOperand(I);
1004 ConstantInt *Behavior = cast(Op->getOperand(0));
1005 MDString *ID = cast(Op->getOperand(1));
1006
1007 if (Behavior->getZExtValue() == Module::Require) {
1008 Requirements.insert(cast(Op->getOperand(2)));
1009 } else {
1010 Flags[ID] = Op;
1011 }
1012 }
1013
1014 // Merge in the flags from the source module, and also collect its set of
1015 // requirements.
10571016 bool HasErr = false;
1058
1059 // Otherwise, we have to merge them based on their behaviors. First,
1060 // categorize all of the nodes in the modules' module flags. If an error or
1061 // warning occurs, then emit the appropriate message(s).
1062 DenseMap ErrorNode;
1063 DenseMap WarningNode;
1064 DenseMap OverrideNode;
1065 DenseMap > RequireNodes;
1066 SmallSetVector SeenIDs;
1067
1068 HasErr |= categorizeModuleFlagNodes(SrcModFlags, ErrorNode, WarningNode,
1069 OverrideNode, RequireNodes, SeenIDs);
1070 HasErr |= categorizeModuleFlagNodes(DstModFlags, ErrorNode, WarningNode,
1071 OverrideNode, RequireNodes, SeenIDs);
1072
1073 // Check that there isn't both an error and warning node for a flag.
1074 for (SmallSetVector::iterator
1075 I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) {
1076 MDString *ID = *I;
1077 if (ErrorNode[ID] && WarningNode[ID])
1078 HasErr = emitError("linking module flags '" + ID->getString() +
1079 "': IDs have conflicting behaviors");
1080 }
1081
1082 // Early exit if we had an error.
1083 if (HasErr) return true;
1084
1085 // Get the destination's module flags ready for new operands.
1086 DstModFlags->dropAllReferences();
1087
1088 // Add all of the module flags to the destination module.
1089 DenseMap > AddedNodes;
1090 for (SmallSetVector::iterator
1091 I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) {
1092 MDString *ID = *I;
1093 if (OverrideNode[ID]) {
1094 DstModFlags->addOperand(OverrideNode[ID]);
1095 AddedNodes[ID].push_back(OverrideNode[ID]);
1096 } else if (ErrorNode[ID]) {
1097 DstModFlags->addOperand(ErrorNode[ID]);
1098 AddedNodes[ID].push_back(ErrorNode[ID]);
1099 } else if (WarningNode[ID]) {
1100 DstModFlags->addOperand(WarningNode[ID]);
1101 AddedNodes[ID].push_back(WarningNode[ID]);
1102 }
1103
1104 for (SmallSetVector::iterator
1105 II = RequireNodes[ID].begin(), IE = RequireNodes[ID].end();
1106 II != IE; ++II)
1107 DstModFlags->addOperand(*II);
1108 }
1109
1110 // Now check that all of the requirements have been satisfied.
1111 for (SmallSetVector::iterator
1112 I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) {
1113 MDString *ID = *I;
1114 SmallSetVector &Set = RequireNodes[ID];
1115
1116 for (SmallSetVector::iterator
1117 II = Set.begin(), IE = Set.end(); II != IE; ++II) {
1118 MDNode *Node = *II;
1119 MDNode *Val = cast(Node->getOperand(2));
1120
1121 MDString *ReqID = cast(Val->getOperand(0));
1122 Value *ReqVal = Val->getOperand(1);
1123
1124 bool HasValue = false;
1125 for (SmallVectorImpl::iterator
1126 RI = AddedNodes[ReqID].begin(), RE = AddedNodes[ReqID].end();
1127 RI != RE; ++RI) {
1128 MDNode *ReqNode = *RI;
1129 if (ReqNode->getOperand(2) == ReqVal) {
1130 HasValue = true;
1131 break;
1132 }
1017 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I) {
1018 MDNode *SrcOp = SrcModFlags->getOperand(I);
1019 ConstantInt *SrcBehavior = cast(SrcOp->getOperand(0));
1020 MDString *ID = cast(SrcOp->getOperand(1));
1021 MDNode *DstOp = Flags.lookup(ID);
1022 unsigned SrcBehaviorValue = SrcBehavior->getZExtValue();
1023
1024 // If this is a requirement, add it and continue.
1025 if (SrcBehaviorValue == Module::Require) {
1026 // If the destination module does not already have this requirement, add
1027 // it.
1028 if (Requirements.insert(cast(SrcOp->getOperand(2)))) {
1029 DstModFlags->addOperand(SrcOp);
11331030 }
1134
1135 if (!HasValue)
1136 HasErr = emitError("linking module flags '" + ReqID->getString() +
1137 "': does not have the required value");
1031 continue;
1032 }
1033
1034 // If there is no existing flag with this ID, just add it.
1035 if (!DstOp) {
1036 Flags[ID] = SrcOp;
1037 DstModFlags->addOperand(SrcOp);
1038 continue;
1039 }
1040
1041 // Otherwise, perform a merge.
1042 ConstantInt *DstBehavior = cast(DstOp->getOperand(0));
1043 unsigned DstBehaviorValue = DstBehavior->getZExtValue();
1044
1045 // If either flag has override behavior, handle it first.
1046 if (DstBehaviorValue == Module::Override) {
1047 // Diagnose inconsistent flags which both have override behavior.
1048 if (SrcBehaviorValue == Module::Override &&
1049 SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1050 HasErr |= emitError("linking module flags '" + ID->getString() +
1051 "': IDs have conflicting override values");
1052 }
1053 continue;
1054 } else if (SrcBehaviorValue == Module::Override) {
1055 // Update the destination flag to that of the source.
1056 DstOp->replaceOperandWith(0, SrcBehavior);
1057 DstOp->replaceOperandWith(2, SrcOp->getOperand(2));
1058 continue;
1059 }
1060
1061 // Diagnose inconsistent merge behavior types.
1062 if (SrcBehaviorValue != DstBehaviorValue) {
1063 HasErr |= emitError("linking module flags '" + ID->getString() +
1064 "': IDs have conflicting behaviors");
1065 continue;
1066 }
1067
1068 // Perform the merge for standard behavior types.
1069 switch (SrcBehaviorValue) {
1070 case Module::Require:
1071 case Module::Override: assert(0 && "not possible"); break;
1072 case Module::Error: {
1073 // Emit an error if the values differ.
1074 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1075 HasErr |= emitError("linking module flags '" + ID->getString() +
1076 "': IDs have conflicting values");
1077 }
1078 continue;
1079 }
1080 case Module::Warning: {
1081 // Emit a warning if the values differ.
1082 if (SrcOp->getOperand(2) != DstOp->getOperand(2)) {
1083 errs() << "WARNING: linking module flags '" << ID->getString()
1084 << "': IDs have conflicting values";
1085 }
1086 continue;
1087 }
1088 }
1089 }
1090
1091 // Check all of the requirements.
1092 for (unsigned I = 0, E = Requirements.size(); I != E; ++I) {
1093 MDNode *Requirement = Requirements[I];
1094 MDString *Flag = cast(Requirement->getOperand(0));
1095 Value *ReqValue = Requirement->getOperand(1);
1096
1097 MDNode *Op = Flags[Flag];
1098 if (!Op || Op->getOperand(2) != ReqValue) {
1099 HasErr |= emitError("linking module flags '" + Flag->getString() +
1100 "': does not have the required value");
1101 continue;
11381102 }
11391103 }
11401104
22 ; Test basic functionality of module flags.
33
44 ; CHECK: !0 = metadata !{i32 1, metadata !"foo", i32 37}
5 ; CHECK: !1 = metadata !{i32 1, metadata !"qux", i32 42}
5 ; CHECK: !1 = metadata !{i32 2, metadata !"bar", i32 42}
66 ; CHECK: !2 = metadata !{i32 1, metadata !"mux", metadata !3}
77 ; CHECK: !3 = metadata !{metadata !"hello world", i32 927}
8 ; CHECK: !4 = metadata !{i32 2, metadata !"bar", i32 42}
8 ; CHECK: !4 = metadata !{i32 1, metadata !"qux", i32 42}
99 ; CHECK: !llvm.module.flags = !{!0, !1, !2, !4}
1010
1111 !0 = metadata !{ i32 1, metadata !"foo", i32 37 }
22 ; Test 'require' behavior.
33
44 ; CHECK: !0 = metadata !{i32 1, metadata !"foo", i32 37}
5 ; CHECK: !1 = metadata !{i32 3, metadata !"foo", metadata !2}
6 ; CHECK: !2 = metadata !{metadata !"bar", i32 42}
7 ; CHECK: !3 = metadata !{i32 1, metadata !"bar", i32 42}
8 ; CHECK: !llvm.module.flags = !{!0, !1, !3}
5 ; CHECK: !1 = metadata !{i32 1, metadata !"bar", i32 42}
6 ; CHECK: !2 = metadata !{i32 3, metadata !"foo", metadata !3}
7 ; CHECK: !3 = metadata !{metadata !"bar", i32 42}
8 ; CHECK: !llvm.module.flags = !{!0, !1, !2}
99
1010 !0 = metadata !{ i32 1, metadata !"foo", i32 37 }
1111 !1 = metadata !{ i32 1, metadata !"bar", i32 42 }
0 ; RUN: not llvm-link %s %p/module-flags-7-b.ll -S -o - 2>&1 | FileCheck %s
1
2 ; Test module flags error messages.
3
4 ; CHECK: linking module flags 'foo': IDs have conflicting behaviors
5
6 !0 = metadata !{ i32 1, metadata !"foo", i32 37 }
7
8 !llvm.module.flags = !{ !0 }
0 ; This file is used with module-flags-7-a.ll
1 ; RUN: true
2
3 !0 = metadata !{ i32 2, metadata !"foo", i32 37 }
4
5 !llvm.module.flags = !{ !0 }