llvm-link option and test for recent metadata mapping bug
authorTeresa Johnson <tejohnson@google.com>
Sat, 21 Nov 2015 00:35:38 +0000 (00:35 +0000)
committerTeresa Johnson <tejohnson@google.com>
Sat, 21 Nov 2015 00:35:38 +0000 (00:35 +0000)
Summary:
Add a -preserve-modules option to llvm-link that simulates LTO
clients that don't destroy modules as they are linked. This enables
reproduction of a recent bug introduced by a metadata linking change
that was only caught when the modules weren't destroyed before
writing bitcode (LTO on Windows).

See http://llvm.org/viewvc/llvm-project?view=revision&revision=253170
for more details on the original bug and the fix.

Confirmed the new test added here reproduces the failure using the new
option when I suppress the fix.

Reviewers: pcc

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D14818

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253740 91177308-0d34-0410-b5e6-96231b3b80d8

test/Linker/only-needed-named-metadata.ll
tools/llvm-link/llvm-link.cpp

index a0df5bf2238c51711076860cd86d717b19abd97f..c08d68a76eb9f7fdc5735901758bd3d63907ed92 100644 (file)
 ; ONLYNEEDED-NOT:@analias
 ; ONLYNEEDED-NOT:@globalfunc2()
 
 ; ONLYNEEDED-NOT:@analias
 ; ONLYNEEDED-NOT:@globalfunc2()
 
+; Test -only-needed link with the modules preserved instead of freeing to
+; catch any cross-module references to metadata, which the bitcode writer
+; will assert on.
+; RUN: llvm-link -preserve-modules -o %t3.bc -only-needed %t2.bc %t.bc
+
 @X = global i32 5
 @U = global i32 6
 @U_linkonce = linkonce_odr hidden global i32 6
 @X = global i32 5
 @U = global i32 6
 @U_linkonce = linkonce_odr hidden global i32 6
index c539f75702bed1fae1a0f91e9de81682fa7db397..ada55c93a6e2d22025c4399ef6a70969e12144ea 100644 (file)
@@ -89,6 +89,10 @@ static cl::opt<bool>
 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
                  cl::init(false));
 
 SuppressWarnings("suppress-warnings", cl::desc("Suppress all linking warnings"),
                  cl::init(false));
 
+static cl::opt<bool>
+    PreserveModules("preserve-modules",
+                    cl::desc("Preserve linked modules for testing"));
+
 static cl::opt<bool> PreserveBitcodeUseListOrder(
     "preserve-bc-uselistorder",
     cl::desc("Preserve use-list order when writing LLVM bitcode."),
 static cl::opt<bool> PreserveBitcodeUseListOrder(
     "preserve-bc-uselistorder",
     cl::desc("Preserve use-list order when writing LLVM bitcode."),
@@ -259,6 +263,15 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L,
       return false;
     // All linker flags apply to linking of subsequent files.
     ApplicableFlags = Flags;
       return false;
     // All linker flags apply to linking of subsequent files.
     ApplicableFlags = Flags;
+
+    // If requested for testing, preserve modules by releasing them from
+    // the unique_ptr before the are freed. This can help catch any
+    // cross-module references from e.g. unneeded metadata references
+    // that aren't properly set to null but instead mapped to the source
+    // module version. The bitcode writer will assert if it finds any such
+    // cross-module references.
+    if (PreserveModules)
+      M.release();
   }
 
   return true;
   }
 
   return true;