[PATCH] D36556: [IR] AutoUpgrade ModuleFlagBehavior for PIC and PIE level

Steven Wu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 21 14:50:34 PDT 2017


This revision was automatically updated to reflect the committed changes.
Closed by commit rL311387: [IR] AutoUpgrade ModuleFlagBehavior for PIC and PIE level (authored by steven_wu).

Repository:
  rL LLVM

https://reviews.llvm.org/D36556

Files:
  llvm/trunk/lib/IR/AutoUpgrade.cpp
  llvm/trunk/test/Bitcode/upgrade-module-flag.ll
  llvm/trunk/test/Linker/module-flags-pic-1-a.ll


Index: llvm/trunk/test/Bitcode/upgrade-module-flag.ll
===================================================================
--- llvm/trunk/test/Bitcode/upgrade-module-flag.ll
+++ llvm/trunk/test/Bitcode/upgrade-module-flag.ll
@@ -1,9 +1,13 @@
 ; RUN: llvm-as < %s | llvm-dis | FileCheck %s
 ; RUN: verify-uselistorder < %s
 
-!llvm.module.flags = !{!0}
+!llvm.module.flags = !{!0, !1, !2}
 
-!0 = !{i32 1, !"Objective-C Image Info Version", i32 0}
+!0 = !{i32 1, !"PIC Level", i32 1}
+!1 = !{i32 1, !"PIE Level", i32 1}
+!2 = !{i32 1, !"Objective-C Image Info Version", i32 0}
 
-; CHECK: !0 = !{i32 1, !"Objective-C Image Info Version", i32 0}
-; CHECK: !1 = !{i32 4, !"Objective-C Class Properties", i32 0}
+; CHECK: !0 = !{i32 7, !"PIC Level", i32 1}
+; CHECK: !1 = !{i32 7, !"PIE Level", i32 1}
+; CHECK: !2 = !{i32 1, !"Objective-C Image Info Version", i32 0}
+; CHECK: !3 = !{i32 4, !"Objective-C Class Properties", i32 0}
Index: llvm/trunk/test/Linker/module-flags-pic-1-a.ll
===================================================================
--- llvm/trunk/test/Linker/module-flags-pic-1-a.ll
+++ llvm/trunk/test/Linker/module-flags-pic-1-a.ll
@@ -2,8 +2,8 @@
 
 ; test linking modules with specified and default PIC levels
 
-!0 = !{ i32 1, !"PIC Level", i32 1 }
+!0 = !{ i32 7, !"PIC Level", i32 1 }
 
 !llvm.module.flags = !{!0}
 ; CHECK: !llvm.module.flags = !{!0}
-; CHECK: !0 = !{i32 1, !"PIC Level", i32 1}
+; CHECK: !0 = !{i32 7, !"PIC Level", i32 1}
Index: llvm/trunk/lib/IR/AutoUpgrade.cpp
===================================================================
--- llvm/trunk/lib/IR/AutoUpgrade.cpp
+++ llvm/trunk/lib/IR/AutoUpgrade.cpp
@@ -2284,34 +2284,52 @@
 }
 
 bool llvm::UpgradeModuleFlags(Module &M) {
-  const NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
+  NamedMDNode *ModFlags = M.getModuleFlagsMetadata();
   if (!ModFlags)
     return false;
 
-  bool HasObjCFlag = false, HasClassProperties = false;
+  bool HasObjCFlag = false, HasClassProperties = false, Changed = false;
   for (unsigned I = 0, E = ModFlags->getNumOperands(); I != E; ++I) {
     MDNode *Op = ModFlags->getOperand(I);
-    if (Op->getNumOperands() < 2)
+    if (Op->getNumOperands() != 3)
       continue;
     MDString *ID = dyn_cast_or_null<MDString>(Op->getOperand(1));
     if (!ID)
       continue;
     if (ID->getString() == "Objective-C Image Info Version")
       HasObjCFlag = true;
     if (ID->getString() == "Objective-C Class Properties")
       HasClassProperties = true;
+    // Upgrade PIC/PIE Module Flags. The module flag behavior for these two
+    // field was Error and now they are Max.
+    if (ID->getString() == "PIC Level" || ID->getString() == "PIE Level") {
+      if (auto *Behavior =
+              mdconst::dyn_extract_or_null<ConstantInt>(Op->getOperand(0))) {
+        if (Behavior->getLimitedValue() == Module::Error) {
+          Type *Int32Ty = Type::getInt32Ty(M.getContext());
+          Metadata *Ops[3] = {
+              ConstantAsMetadata::get(ConstantInt::get(Int32Ty, Module::Max)),
+              MDString::get(M.getContext(), ID->getString()),
+              Op->getOperand(2)};
+          ModFlags->setOperand(I, MDNode::get(M.getContext(), Ops));
+          Changed = true;
+        }
+      }
+    }
   }
+
   // "Objective-C Class Properties" is recently added for Objective-C. We
   // upgrade ObjC bitcodes to contain a "Objective-C Class Properties" module
   // flag of value 0, so we can correclty downgrade this flag when trying to
   // link an ObjC bitcode without this module flag with an ObjC bitcode with
   // this module flag.
   if (HasObjCFlag && !HasClassProperties) {
     M.addModuleFlag(llvm::Module::Override, "Objective-C Class Properties",
                     (uint32_t)0);
-    return true;
+    Changed = true;
   }
-  return false;
+
+  return Changed;
 }
 
 static bool isOldLoopArgument(Metadata *MD) {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D36556.112061.patch
Type: text/x-patch
Size: 3888 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20170821/3cf822c4/attachment.bin>


More information about the llvm-commits mailing list