am 24d69e52: Merge "Added in fix to allow generation of valid debug info"

* commit '24d69e521538d70863dc0a3745733b8c27fcccd7':
  Added in fix to allow generation of valid debug info
diff --git a/llvm-rs-as.cpp b/llvm-rs-as.cpp
index c63a1ac..5f3a901 100644
--- a/llvm-rs-as.cpp
+++ b/llvm-rs-as.cpp
@@ -91,7 +91,8 @@
   if (Force || !CheckBitcodeOutputToConsole(Out->os(), true)) {
     slang::writeBitcode(Out->os(), *M,
         /* TargetAPI = */ ModuleTargetAPI,
-        /* OptimizationLevel = */ 3);
+        /* OptimizationLevel = */ 3,
+        /* GenerateDebugInfo = */ false);
 
     if (!Out->os().has_error()) {
       // Declare success.
diff --git a/slang_backend.cpp b/slang_backend.cpp
index 0936494..83c157f 100644
--- a/slang_backend.cpp
+++ b/slang_backend.cpp
@@ -327,7 +327,7 @@
     }
     case Slang::OT_Bitcode: {
       writeBitcode(mBufferOutStream, *mpModule, getTargetAPI(),
-                   mCodeGenOpts.OptimizationLevel);
+                   mCodeGenOpts.OptimizationLevel, mCodeGenOpts.getDebugInfo());
       break;
     }
     case Slang::OT_Nothing: {
diff --git a/slang_bitcode_gen.cpp b/slang_bitcode_gen.cpp
index 83d96bf..dc0eb8d 100644
--- a/slang_bitcode_gen.cpp
+++ b/slang_bitcode_gen.cpp
@@ -25,42 +25,64 @@
 #include "slang_assert.h"
 #include "slang_bitcode_gen.h"
 #include "slang_version.h"
+#include "llvm/Bitcode/ReaderWriter.h"
 
 namespace slang {
 
 void writeBitcode(llvm::raw_ostream &Out,
                   const llvm::Module &M,
                   uint32_t TargetAPI,
-                  uint32_t OptimizationLevel) {
+                  uint32_t OptimizationLevel,
+                  bool GenerateDebugInfo) {
   std::string BitcodeStr;
   llvm::raw_string_ostream Bitcode(BitcodeStr);
 
-  // Create the bitcode.
-  switch (TargetAPI) {
-  case SLANG_HC_TARGET_API:
-  case SLANG_HC_MR1_TARGET_API:
-  case SLANG_HC_MR2_TARGET_API: {
-    // Pre-ICS targets must use the LLVM 2.9 BitcodeWriter
-    llvm_2_9::WriteBitcodeToFile(&M, Bitcode);
-    break;
-  }
-  case SLANG_ICS_TARGET_API:
-  case SLANG_ICS_MR1_TARGET_API: {
-    // ICS targets must use the LLVM 2.9_func BitcodeWriter
-    llvm_2_9_func::WriteBitcodeToFile(&M, Bitcode);
-    break;
-  }
-  default: {
-    if (TargetAPI != SLANG_DEVELOPMENT_TARGET_API &&
-        (TargetAPI < SLANG_MINIMUM_TARGET_API ||
-         TargetAPI > SLANG_MAXIMUM_TARGET_API)) {
-      slangAssert(false && "Invalid target API value");
+  // The older bitcode writers will produce invalid bitcode if the -g
+  // flag is set using WriteBitcodeToFile. As such we use the ToT writer
+  // when -g is set. However, this will produce a bitcode file linked to
+  // this version of LLVM as the debug info format can change between
+  // versions.
+  // If bcc receives a bitcode file with a format of debug info
+  // which is either ahead or behind the version it is expecting it will
+  // fail the verification stage. Failing this stage results in the bitcode
+  // loader returning null and the compiler will terminate abruptly. Bitcode
+  // files with debug info are as such only capable of targeting devices with
+  // LLVM libraries with the same debug info version as the version of slang
+  // which was used to compile the file. This is due to the fact that LLVM
+  // offers no backwards or forwards compatibility guarantee for its debug
+  // bitcode. At the moment the only practical guarantee which can be made
+  // is that the debug bitcode emitted by any slang will work with the bcc
+  // version which was newest at the time when llvm-rs-cc was built.
+  if (GenerateDebugInfo) {
+    llvm::WriteBitcodeToFile(&M, Bitcode);
+  } else {
+    // Create the bitcode.
+    switch (TargetAPI) {
+    case SLANG_HC_TARGET_API:
+    case SLANG_HC_MR1_TARGET_API:
+    case SLANG_HC_MR2_TARGET_API: {
+      // Pre-ICS targets must use the LLVM 2.9 BitcodeWriter
+      llvm_2_9::WriteBitcodeToFile(&M, Bitcode);
+      break;
     }
-    // Switch to the 3.2 BitcodeWriter by default, and don't use
-    // LLVM's included BitcodeWriter at all (for now).
-    llvm_3_2::WriteBitcodeToFile(&M, Bitcode);
-    break;
-  }
+    case SLANG_ICS_TARGET_API:
+    case SLANG_ICS_MR1_TARGET_API: {
+      // ICS targets must use the LLVM 2.9_func BitcodeWriter
+      llvm_2_9_func::WriteBitcodeToFile(&M, Bitcode);
+      break;
+    }
+    default: {
+      if (TargetAPI != SLANG_DEVELOPMENT_TARGET_API &&
+          (TargetAPI < SLANG_MINIMUM_TARGET_API ||
+           TargetAPI > SLANG_MAXIMUM_TARGET_API)) {
+        slangAssert(false && "Invalid target API value");
+      }
+      // Switch to the 3.2 BitcodeWriter by default, and don't use
+      // LLVM's included BitcodeWriter at all (for now).
+      llvm_3_2::WriteBitcodeToFile(&M, Bitcode);
+      break;
+    }
+    }
   }
 
   const uint32_t CompilerVersion = SlangVersion::CURRENT;
diff --git a/slang_bitcode_gen.h b/slang_bitcode_gen.h
index cc0e9f6..b1f97c9 100644
--- a/slang_bitcode_gen.h
+++ b/slang_bitcode_gen.h
@@ -31,7 +31,8 @@
 void writeBitcode(llvm::raw_ostream &Out,
                   const llvm::Module &M,
                   uint32_t TargetAPI,
-                  uint32_t OptimizationLevel);
+                  uint32_t OptimizationLevel,
+                  bool GenerateDebugInfo);
 
 } // end namespace slang