Restore "[ThinLTO] Serialize the Module SourceFileName to/from LLVM assembly"

This restores commit 264869, with a fix for windows bots to properly
escape '\' in the path when serializing out. Added test.

llvm-svn: 264884
diff --git a/llvm/lib/IR/AsmWriter.cpp b/llvm/lib/IR/AsmWriter.cpp
index ab56f08..4750cf6 100644
--- a/llvm/lib/IR/AsmWriter.cpp
+++ b/llvm/lib/IR/AsmWriter.cpp
@@ -2203,6 +2203,22 @@
   Out << " ]";
 }
 
+/// Escape any backslashes in the source file (e.g. Windows paths)
+/// before emitting, so that it is parsed properly by the lexer on input.
+static void EscapeBackslashes(std::string Str,
+                              SmallVectorImpl<char> &Res) {
+  for (auto C : Str) {
+    switch (C) {
+    default:
+      break;
+    case '\\':
+      Res.push_back('\\');
+      break;
+    }
+    Res.push_back(C);
+  }
+}
+
 void AssemblyWriter::printModule(const Module *M) {
   Machine.initialize();
 
@@ -2215,6 +2231,12 @@
       M->getModuleIdentifier().find('\n') == std::string::npos)
     Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n";
 
+  if (!M->getSourceFileName().empty()) {
+    SmallString<128> EscapedName;
+    EscapeBackslashes(M->getSourceFileName(), EscapedName);
+    Out << "source_filename = \"" << EscapedName << "\"\n";
+  }
+
   const std::string &DL = M->getDataLayoutStr();
   if (!DL.empty())
     Out << "target datalayout = \"" << DL << "\"\n";