llvm.org GIT mirror llvm / d34cb1e
[WIP] Initial code for module flags. Module flags are key-value pairs associated with the module. They include a 'behavior' value, indicating how module flags react when mergine two files. Normally, it's just the union of the two module flags. But if two module flags have the same key, then the resulting flags are dictated by the behaviors. Allowable behaviors are: Error Emits an error if two values disagree. Warning Emits a warning if two values disagree. Require Emits an error when the specified value is not present or doesn't have the specified value. It is an error for two (or more) llvm.module.flags with the same ID to have the Require behavior but different values. There may be multiple Require flags per ID. Override Uses the specified value if the two values disagree. It is an error for two (or more) llvm.module.flags with the same ID to have the Override behavior but different values. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@150300 91177308-0d34-0410-b5e6-96231b3b80d8 Bill Wendling 8 years ago
15 changed file(s) with 382 addition(s) and 5 deletion(s). Raw diff Collapse all Expand all
152152
153153 /// An enumeration for describing the size of a pointer on the target machine.
154154 enum PointerSize { AnyPointerSize, Pointer32, Pointer64 };
155
156 /// An enumeration for the supported behaviors of module flags. The following
157 /// module flags behavior values are supported:
158 ///
159 /// Value Behavior
160 /// ----- --------
161 /// 1 Error
162 /// Emits an error if two values disagree.
163 ///
164 /// 2 Warning
165 /// Emits a warning if two values disagree.
166 ///
167 /// 3 Require
168 /// Emits an error when the specified value is not present
169 /// or doesn't have the specified value. It is an error for
170 /// two (or more) llvm.module.flags with the same ID to have
171 /// the Require behavior but different values. There may be
172 /// multiple Require flags per ID.
173 ///
174 /// 4 Override
175 /// Uses the specified value if the two values disagree. It
176 /// is an error for two (or more) llvm.module.flags with the
177 /// same ID to have the Override behavior but different
178 /// values.
179 enum ModAttrBehavior { Error = 1, Warning = 2, Require = 3, Override = 4 };
155180
156181 /// @}
157182 /// @name Member Variables
372397 void eraseNamedMetadata(NamedMDNode *NMD);
373398
374399 /// @}
400 /// @name Module Flags Accessors
401 /// @{
402
403 /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
404 /// represents module-level flags. This method returns null if there are no
405 /// module-level flags.
406 NamedMDNode *getModuleFlagsMetadata() const;
407
408 /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module
409 /// that represents module-level flags. If module-level flags aren't found,
410 /// it creates the named metadata that contains them.
411 NamedMDNode *getOrInsertModuleFlagsMetadata();
412
413 /// addModuleFlag - Add a module-level flag to the module-level flags
414 /// metadata. It will create the module-level flags named metadata if it
415 /// doesn't already exist.
416 void addModuleFlag(ModAttrBehavior Behavior, StringRef Key, Value *Val);
417 void addModuleFlag(ModAttrBehavior Behavior, StringRef Key, uint32_t Val);
418 void addModuleFlag(MDNode *Node);
419
420 /// @}
375421 /// @name Materialization
376422 /// @{
377423
1515 #include "llvm/DerivedTypes.h"
1616 #include "llvm/Instructions.h"
1717 #include "llvm/Module.h"
18 #include "llvm/ADT/DenseSet.h"
19 #include "llvm/ADT/Optional.h"
20 #include "llvm/ADT/SetVector.h"
1821 #include "llvm/ADT/SmallPtrSet.h"
19 #include "llvm/ADT/Optional.h"
2022 #include "llvm/Support/raw_ostream.h"
2123 #include "llvm/Support/Path.h"
2224 #include "llvm/Transforms/Utils/Cloning.h"
405407 }
406408
407409 void computeTypeMapping();
410 bool categorizeModuleFlagNodes(const NamedMDNode *ModFlags,
411 DenseMap &ErrorNode,
412 DenseMap &WarningNode,
413 DenseMap &OverrideNode,
414 DenseMap
415 SmallSetVector > &RequireNodes,
416 SmallSetVector &SeenIDs);
408417
409418 bool linkAppendingVarProto(GlobalVariable *DstGV, GlobalVariable *SrcGV);
410419 bool linkGlobalProto(GlobalVariable *SrcGV);
411420 bool linkFunctionProto(Function *SrcF);
412421 bool linkAliasProto(GlobalAlias *SrcA);
422 bool linkModuleFlagsMetadata();
413423
414424 void linkAppendingVarInit(const AppendingVarInfo &AVI);
415425 void linkGlobalInits();
937947 /// linkNamedMDNodes - Insert all of the named mdnodes in Src into the Dest
938948 /// module.
939949 void ModuleLinker::linkNamedMDNodes() {
950 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
940951 for (Module::const_named_metadata_iterator I = SrcM->named_metadata_begin(),
941952 E = SrcM->named_metadata_end(); I != E; ++I) {
953 // Don't link module flags here. Do them separately.
954 if (&*I == SrcModFlags) continue;
942955 NamedMDNode *DestNMD = DstM->getOrInsertNamedMetadata(I->getName());
943956 // Add Src elements into Dest node.
944957 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i)
946959 RF_None, &TypeMap));
947960 }
948961 }
962
963 /// categorizeModuleFlagNodes -
964 bool ModuleLinker::
965 categorizeModuleFlagNodes(const NamedMDNode *ModFlags,
966 DenseMap &ErrorNode,
967 DenseMap &WarningNode,
968 DenseMap &OverrideNode,
969 DenseMap
970 SmallSetVector > &RequireNodes,
971 SmallSetVector &SeenIDs) {
972 bool HasErr = false;
973
974 for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
975 MDNode *Op = ModFlags->getOperand(I);
976 assert(Op->getNumOperands() == 3 && "Invalid module flag metadata!");
977 assert(isa(Op->getOperand(0)) &&
978 "Module flag's first operand must be an integer!");
979 assert(isa(Op->getOperand(1)) &&
980 "Module flag's second operand must be an MDString!");
981
982 ConstantInt *Behavior = cast(Op->getOperand(0));
983 MDString *ID = cast(Op->getOperand(1));
984 Value *Val = Op->getOperand(2);
985 switch (Behavior->getZExtValue()) {
986 default:
987 assert(false && "Invalid behavior in module flag metadata!");
988 break;
989 case Module::Error: {
990 MDNode *&ErrNode = ErrorNode[ID];
991 if (!ErrNode) ErrNode = Op;
992 if (ErrNode->getOperand(2) != Val)
993 HasErr = emitError("Linking module flags '" + ID->getString() +
994 "': IDs have conflicting values!");
995 break;
996 }
997 case Module::Warning: {
998 MDNode *&WarnNode = WarningNode[ID];
999 if (!WarnNode) WarnNode = Op;
1000 if (WarnNode->getOperand(2) != Val)
1001 errs() << "WARNING: Linking module flags '" << ID->getString()
1002 << "': IDs have conflicting values!";
1003 break;
1004 }
1005 case Module::Require: RequireNodes[ID].insert(Op); break;
1006 case Module::Override: {
1007 MDNode *&OvrNode = OverrideNode[ID];
1008 if (!OvrNode) OvrNode = Op;
1009 if (OvrNode->getOperand(2) != Val)
1010 HasErr = emitError("Linking module flags '" + ID->getString() +
1011 "': IDs have conflicting override values!");
1012 break;
1013 }
1014 }
1015
1016 SeenIDs.insert(ID);
1017 }
1018
1019 return HasErr;
1020 }
1021
1022 /// linkModuleFlagsMetadata - Merge the linker flags in Src into the Dest
1023 /// module.
1024 bool ModuleLinker::linkModuleFlagsMetadata() {
1025 const NamedMDNode *SrcModFlags = SrcM->getModuleFlagsMetadata();
1026 if (!SrcModFlags) return false;
1027
1028 NamedMDNode *DstModFlags = DstM->getOrInsertModuleFlagsMetadata();
1029
1030 // If the destination module doesn't have module flags yet, then just copy
1031 // over the source module's flags.
1032 if (DstModFlags->getNumOperands() == 0) {
1033 for (unsigned I = 0, E = SrcModFlags->getNumOperands(); I != E; ++I)
1034 DstModFlags->addOperand(SrcModFlags->getOperand(I));
1035
1036 return false;
1037 }
1038
1039 bool HasErr = false;
1040
1041 // Otherwise, we have to merge them based on their behaviors. First,
1042 // categorize all of the nodes in the modules' module flags. If an error or
1043 // warning occurs, then emit the appropriate message(s).
1044 DenseMap ErrorNode;
1045 DenseMap WarningNode;
1046 DenseMap OverrideNode;
1047 DenseMap > RequireNodes;
1048 SmallSetVector SeenIDs;
1049
1050 HasErr |= categorizeModuleFlagNodes(SrcModFlags, ErrorNode, WarningNode,
1051 OverrideNode, RequireNodes, SeenIDs);
1052 HasErr |= categorizeModuleFlagNodes(DstModFlags, ErrorNode, WarningNode,
1053 OverrideNode, RequireNodes, SeenIDs);
1054
1055 // Check that there isn't both an error and warning node for a flag.
1056 for (SmallSetVector::iterator
1057 I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) {
1058 MDString *ID = *I;
1059 if (ErrorNode[ID] && WarningNode[ID])
1060 HasErr = emitError("Linking module flags '" + ID->getString() +
1061 "': IDs have conflicting behaviors");
1062 }
1063
1064 // Early exit if we had an error.
1065 if (HasErr) return true;
1066
1067 // Get the destination's module flags ready for new operands.
1068 DstModFlags->dropAllReferences();
1069
1070 // Add all of the module flags to the destination module.
1071 DenseMap > AddedNodes;
1072 for (SmallSetVector::iterator
1073 I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) {
1074 MDString *ID = *I;
1075 if (OverrideNode[ID]) {
1076 DstModFlags->addOperand(OverrideNode[ID]);
1077 AddedNodes[ID].push_back(OverrideNode[ID]);
1078 } else if (ErrorNode[ID]) {
1079 DstModFlags->addOperand(ErrorNode[ID]);
1080 AddedNodes[ID].push_back(ErrorNode[ID]);
1081 } else if (WarningNode[ID]) {
1082 DstModFlags->addOperand(WarningNode[ID]);
1083 AddedNodes[ID].push_back(WarningNode[ID]);
1084 }
1085
1086 for (SmallSetVector::iterator
1087 II = RequireNodes[ID].begin(), IE = RequireNodes[ID].end();
1088 II != IE; ++II)
1089 DstModFlags->addOperand(*II);
1090 }
1091
1092 // Now check that all of the requirements have been satisfied.
1093 for (SmallSetVector::iterator
1094 I = SeenIDs.begin(), E = SeenIDs.end(); I != E; ++I) {
1095 MDString *ID = *I;
1096 SmallSetVector &Set = RequireNodes[ID];
1097
1098 for (SmallSetVector::iterator
1099 II = Set.begin(), IE = Set.end(); II != IE; ++II) {
1100 MDNode *Node = *II;
1101 assert(isa(Node->getOperand(2)) &&
1102 "Module flag's third operand must be an MDNode!");
1103 MDNode *Val = cast(Node->getOperand(2));
1104
1105 MDString *ReqID = cast(Val->getOperand(0));
1106 Value *ReqVal = Val->getOperand(1);
1107
1108 bool HasValue = false;
1109 for (SmallVectorImpl::iterator
1110 RI = AddedNodes[ReqID].begin(), RE = AddedNodes[ReqID].end();
1111 RI != RE; ++RI) {
1112 MDNode *ReqNode = *RI;
1113 if (ReqNode->getOperand(2) == ReqVal) {
1114 HasValue = true;
1115 break;
1116 }
1117 }
1118
1119 if (!HasValue)
1120 HasErr = emitError("Linking module flags '" + ReqID->getString() +
1121 "': does not have the required value!");
1122 }
1123 }
1124
1125 return HasErr;
1126 }
9491127
9501128 bool ModuleLinker::run() {
951 assert(DstM && "Null Destination module");
952 assert(SrcM && "Null Source Module");
1129 assert(DstM && "Null destination module");
1130 assert(SrcM && "Null source module");
9531131
9541132 // Inherit the target data from the source module if the destination module
9551133 // doesn't have one already.
10291207 // Link in the function bodies that are defined in the source module into
10301208 // DstM.
10311209 for (Module::iterator SF = SrcM->begin(), E = SrcM->end(); SF != E; ++SF) {
1032
10331210 // Skip if not linking from source.
10341211 if (DoNotLinkFromSource.count(SF)) continue;
10351212
10471224 // Resolve all uses of aliases with aliasees.
10481225 linkAliasBodies();
10491226
1050 // Remap all of the named mdnoes in Src into the DstM module. We do this
1227 // Remap all of the named MDNodes in Src into the DstM module. We do this
10511228 // after linking GlobalValues so that MDNodes that reference GlobalValues
10521229 // are properly remapped.
10531230 linkNamedMDNodes();
1231
1232 // Merge the module flags into the DstM module.
1233 if (linkModuleFlagsMetadata())
1234 return true;
10541235
10551236 // Process vector of lazily linked in functions.
10561237 bool LinkedInAnyFunctions;
320320 return NMD;
321321 }
322322
323 /// eraseNamedMetadata - Remove the given NamedMDNode from this module and
324 /// delete it.
323325 void Module::eraseNamedMetadata(NamedMDNode *NMD) {
324326 static_cast *>(NamedMDSymTab)->erase(NMD->getName());
325327 NamedMDList.erase(NMD);
326328 }
327329
330 /// getModuleFlagsMetadata - Returns the NamedMDNode in the module that
331 /// represents module-level flags. This method returns null if there are no
332 /// module-level flags.
333 NamedMDNode *Module::getModuleFlagsMetadata() const {
334 return getNamedMetadata("llvm.module.flags");
335 }
336
337 /// getOrInsertModuleFlagsMetadata - Returns the NamedMDNode in the module that
338 /// represents module-level flags. If module-level flags aren't found, it
339 /// creates the named metadata that contains them.
340 NamedMDNode *Module::getOrInsertModuleFlagsMetadata() {
341 return getOrInsertNamedMetadata("llvm.module.flags");
342 }
343
344 /// addModuleFlag - Add a module-level flag to the module-level flags
345 /// metadata. It will create the module-level flags named metadata if it doesn't
346 /// already exist.
347 void Module::addModuleFlag(ModAttrBehavior Behavior, StringRef Key,
348 Value *Val) {
349 Type *Int32Ty = Type::getInt32Ty(Context);
350 Value *Ops[3] = {
351 ConstantInt::get(Int32Ty, Behavior), MDString::get(Context, Key), Val
352 };
353 getOrInsertModuleFlagsMetadata()->addOperand(MDNode::get(Context, Ops));
354 }
355 void Module::addModuleFlag(ModAttrBehavior Behavior, StringRef Key,
356 uint32_t Val) {
357 Type *Int32Ty = Type::getInt32Ty(Context);
358 addModuleFlag(Behavior, Key, ConstantInt::get(Int32Ty, Val));
359 }
360 void Module::addModuleFlag(MDNode *Node) {
361 assert(Node->getNumOperands() == 3 &&
362 "Invalid number of operands for module flag!");
363 assert(isa(Node->getOperand(0)) &&
364 isa(Node->getOperand(1)) &&
365 "Invalid operand types for module flag!");
366 getOrInsertModuleFlagsMetadata()->addOperand(Node);
367 }
328368
329369 //===----------------------------------------------------------------------===//
330370 // Methods to control the materialization of GlobalValues in the Module.
0 ; RUN: llvm-link %s %p/module-flags-1-b.ll -S -o - | sort | FileCheck %s
1
2 ; Test basic functionality of module flags.
3
4 ; CHECK: !0 = metadata !{i32 1, metadata !"foo", i32 37}
5 ; CHECK: !1 = metadata !{i32 1, metadata !"qux", i32 42}
6 ; CHECK: !2 = metadata !{i32 1, metadata !"mux", metadata !3}
7 ; CHECK: !3 = metadata !{metadata !"hello world", i32 927}
8 ; CHECK: !4 = metadata !{i32 2, metadata !"bar", i32 42}
9 ; CHECK: !llvm.module.flags = !{!0, !1, !2, !4}
10
11 !0 = metadata !{ i32 1, metadata !"foo", i32 37 }
12 !1 = metadata !{ i32 2, metadata !"bar", i32 42 }
13 !2 = metadata !{ i32 1, metadata !"mux", metadata !{ metadata !"hello world", i32 927 } }
14
15 !llvm.module.flags = !{ !0, !1, !2 }
0 ; This file is used with module-flags-1-a.ll
1 ; RUN: true
2
3 !0 = metadata !{ i32 1, metadata !"foo", i32 37 }
4 !1 = metadata !{ i32 1, metadata !"qux", i32 42 }
5 !2 = metadata !{ i32 1, metadata !"mux", metadata !{ metadata !"hello world", i32 927 } }
6
7 !llvm.module.flags = !{ !0, !1, !2 }
0 ; RUN: llvm-link %s %p/module-flags-2-b.ll -S -o - | sort | FileCheck %s
1
2 ; Test the 'override' behavior.
3
4 ; CHECK: !0 = metadata !{i32 4, metadata !"foo", i32 37}
5 ; CHECK: !llvm.module.flags = !{!0}
6
7 !0 = metadata !{ i32 1, metadata !"foo", i32 927 }
8
9 !llvm.module.flags = !{ !0 }
0 ; This file is used with module-flags-2-a.ll
1 ; RUN: true
2
3 !0 = metadata !{ i32 4, metadata !"foo", i32 37 } ; Override the "foo" value.
4
5 !llvm.module.flags = !{ !0 }
0 ; RUN: llvm-link %s %p/module-flags-3-b.ll -S -o - | sort | FileCheck %s
1
2 ; Test 'require' behavior.
3
4 ; 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}
9
10 !0 = metadata !{ i32 1, metadata !"foo", i32 37 }
11 !1 = metadata !{ i32 1, metadata !"bar", i32 42 }
12
13 !llvm.module.flags = !{ !0, !1 }
0 ; This file is used with module-flags-3-a.ll
1 ; RUN: true
2
3 !0 = metadata !{ i32 3, metadata !"foo",
4 metadata !{ metadata !"bar", i32 42 }
5 }
6
7 !llvm.module.flags = !{ !0 }
0 ; RUN: not llvm-link %s %p/module-flags-4-b.ll -S -o - |& FileCheck %s
1
2 ; Test 'require' error.
3
4 ; CHECK: Linking module flags 'bar': does not have the required value
5
6 !0 = metadata !{ i32 1, metadata !"foo", i32 37 }
7 !1 = metadata !{ i32 1, metadata !"bar", i32 927 }
8
9 !llvm.module.flags = !{ !0, !1 }
0 ; This file is used with module-flags-4-a.ll
1 ; RUN: true
2
3 !0 = metadata !{ i32 3, metadata !"foo",
4 metadata !{ metadata !"bar", i32 42 }
5 }
6
7 !llvm.module.flags = !{ !0 }
0 ; RUN: not llvm-link %s %p/module-flags-5-b.ll -S -o - |& FileCheck %s
1
2 ; Test the 'override' error.
3
4 ; CHECK: Linking module flags 'foo': IDs have conflicting override values
5
6 !0 = metadata !{ i32 4, metadata !"foo", i32 927 }
7
8 !llvm.module.flags = !{ !0 }
0 ; This file is used with module-flags-5-a.ll
1 ; RUN: true
2
3 !0 = metadata !{ i32 4, metadata !"foo", i32 37 } ; Override the "foo" value.
4
5 !llvm.module.flags = !{ !0 }
0 ; RUN: not llvm-link %s %p/module-flags-6-b.ll -S -o - |& FileCheck %s
1
2 ; Test module flags error messages.
3
4 ; CHECK: Linking module flags 'foo': IDs have conflicting values
5
6 !0 = metadata !{ i32 1, metadata !"foo", i32 37 }
7
8 !llvm.module.flags = !{ !0 }
0 ; This file is used with module-flags-6-a.ll
1 ; RUN: true
2
3 !0 = metadata !{ i32 1, metadata !"foo", i32 38 }
4
5 !llvm.module.flags = !{ !0 }