[llvm] ac375c2 - [Bitcode] Avoid duplicating linker option when upgrading

Steven Wu via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 23 13:07:52 PDT 2020


Author: Steven Wu
Date: 2020-07-23T13:07:28-07:00
New Revision: ac375c2fe316dae6eb770b38f90d6b67fadd22ec

URL: https://github.com/llvm/llvm-project/commit/ac375c2fe316dae6eb770b38f90d6b67fadd22ec
DIFF: https://github.com/llvm/llvm-project/commit/ac375c2fe316dae6eb770b38f90d6b67fadd22ec.diff

LOG: [Bitcode] Avoid duplicating linker option when upgrading

Summary:
The upgrading path from old ModuleFlag based linker options to the new
NamedMetadata based linker option in in materializeMetadata() which gets
called once for the module and once for every GV. The linker options are
getting dup'ed every time and it can create massive amount of the linker
options in the object file that gets created from old bitcode. Fix the
problem by checking if the new option exists or not before upgrade
again.

rdar://64543389

Reviewers: pcc, t.p.northover, dexonsmith, arphaman

Reviewed By: arphaman

Subscribers: hiraditya, jkorous, ributzka, llvm-commits

Tags: #llvm

Differential Revision: https://reviews.llvm.org/D83688

Added: 
    llvm/test/Bitcode/Inputs/linker-options.bc
    llvm/test/Bitcode/upgrade-linker-options-2.ll

Modified: 
    llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
index 2c7882b6ad51..9632b5700e8a 100644
--- a/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
+++ b/llvm/lib/Bitcode/Reader/BitcodeReader.cpp
@@ -2969,12 +2969,15 @@ Error BitcodeReader::materializeMetadata() {
   }
 
   // Upgrade "Linker Options" module flag to "llvm.linker.options" module-level
-  // metadata.
-  if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
-    NamedMDNode *LinkerOpts =
-        TheModule->getOrInsertNamedMetadata("llvm.linker.options");
-    for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
-      LinkerOpts->addOperand(cast<MDNode>(MDOptions));
+  // metadata. Only upgrade if the new option doesn't exist to avoid upgrade
+  // multiple times.
+  if (!TheModule->getNamedMetadata("llvm.linker.options")) {
+    if (Metadata *Val = TheModule->getModuleFlag("Linker Options")) {
+      NamedMDNode *LinkerOpts =
+          TheModule->getOrInsertNamedMetadata("llvm.linker.options");
+      for (const MDOperand &MDOptions : cast<MDNode>(Val)->operands())
+        LinkerOpts->addOperand(cast<MDNode>(MDOptions));
+    }
   }
 
   DeferredMetadataInfo.clear();

diff  --git a/llvm/test/Bitcode/Inputs/linker-options.bc b/llvm/test/Bitcode/Inputs/linker-options.bc
new file mode 100644
index 000000000000..cbbfb12d9379
Binary files /dev/null and b/llvm/test/Bitcode/Inputs/linker-options.bc 
diff er

diff  --git a/llvm/test/Bitcode/upgrade-linker-options-2.ll b/llvm/test/Bitcode/upgrade-linker-options-2.ll
new file mode 100644
index 000000000000..b87ed41ef515
--- /dev/null
+++ b/llvm/test/Bitcode/upgrade-linker-options-2.ll
@@ -0,0 +1,12 @@
+;; Test upgrade linker option doesn't create duplicated linker options.
+;; Inputs is generated from IR and checked in as bitcode as it will get rejected by verifier.
+;; define void @test() {
+;;   ret void
+;; }
+;; !llvm.module.flags = !{!0}
+;; !0 = !{i32 6, !"Linker Options", !1}
+;; !1 = !{!2}
+;; !2 = !{!"-framework", !"Foundation"}
+
+; RUN: llvm-dis %S/Inputs/linker-options.bc -o - | FileCheck %s
+; CHECK: !llvm.linker.options = !{!2}


        


More information about the llvm-commits mailing list