Fix RSoV compiler regression

c/299496/ revised the protocol between bcc and bcinfo; bcc now injects
some metadata into the module which is in turn parsed by the
bcinfo::MetadataExtractor(Module *) constructor. Since RSoV uses bcinfo
on non-bcc-processed bitcodes, those missing data caused the constructor
to fail.

This CL uses a different MetadataExtractor constructor so that all the
required information are passed to the MetadataExtractor via full bitcode.

Bug: 30964317
Bug: 36657118
Test: RSoV LIT test, RSoVTest on Angler
Change-Id: I9b423ad8d0bcab5d261e595c10d4676486ec4b3b
(cherry picked from commit 56813de7a00f872fad490b1b3fe5ab1b620c1be5)
diff --git a/rsov/compiler/Context.cpp b/rsov/compiler/Context.cpp
index c4f5b40..79c13b2 100644
--- a/rsov/compiler/Context.cpp
+++ b/rsov/compiler/Context.cpp
@@ -27,19 +27,18 @@
   return c;
 }
 
-Context::Context() : mInitialized(false) {
-}
+Context::Context() : mInitialized(false) {}
 
-bool Context::Initialize(llvm::Module *M) {
+bool Context::Initialize(std::unique_ptr<bcinfo::MetadataExtractor> ME) {
   if (mInitialized) {
     return true;
   }
 
-  mMetadata.reset(new bcinfo::MetadataExtractor(M));
+  mMetadata = std::move(ME);
 
   if (!mMetadata->extract()) {
     llvm::errs() << "cannot extract metadata\n";
-    return false;;
+    return false;
   }
 
   const char **varNames = mMetadata->getExportVarNameList();
diff --git a/rsov/compiler/Context.h b/rsov/compiler/Context.h
index 99f232b..cc86acc 100644
--- a/rsov/compiler/Context.h
+++ b/rsov/compiler/Context.h
@@ -23,19 +23,19 @@
 #include "llvm/ADT/StringMap.h"
 #include "llvm/ADT/StringRef.h"
 
-#include <stdint.h>
 #include <limits>
+#include <stdint.h>
 #include <vector>
 
 // Declare a friend relationship in a class with a test. Used rather that
 // FRIEND_TEST to avoid globally importing gtest/gtest.h into the main
 // RSoV header files.
 #ifdef __HOST__
-#define RSOV_FRIEND_TEST(test_set_name, individual_test)\
-friend class test_set_name##_##individual_test##_Test
+#define RSOV_FRIEND_TEST(test_set_name, individual_test)                       \
+  friend class test_set_name##_##individual_test##_Test
 #else
 #define RSOV_FRIEND_TEST(test_set_name, individual_test)
-#endif  // __HOST__
+#endif // __HOST__
 
 namespace bcinfo {
 class MetadataExtractor;
@@ -60,12 +60,10 @@
 
   // Initialize the internal data struture such as the slot number lookup table,
   // etc.
-  bool Initialize(llvm::Module *M);
+  bool Initialize(std::unique_ptr<bcinfo::MetadataExtractor> ME);
 
   // Returns the total number of exported variables
-  uint32_t getNumExportVar() const {
-    return mExportVarIndices.size();
-  }
+  uint32_t getNumExportVar() const { return mExportVarIndices.size(); }
 
   // Adds the mapping from the slot number of an exported variable to the index
   // of its field in the global buffer
@@ -91,18 +89,14 @@
   }
 
   // Returns the total number of foreach kernels
-  uint32_t getNumForEachKernel() const {
-    return mForEachNameToSlot.size();
-  }
+  uint32_t getNumForEachKernel() const { return mForEachNameToSlot.size(); }
 
   // Checks if a name refers to a foreach kernel function
   bool isForEachKernel(llvm::StringRef name) const {
     return mForEachNameToSlot.count(name) != 0;
   }
 
-  const bcinfo::MetadataExtractor &getMetadata() const {
-    return *mMetadata;
-  }
+  const bcinfo::MetadataExtractor &getMetadata() const { return *mMetadata; }
 
   llvm::SmallVectorImpl<RSAllocationInfo> &getGlobalAllocs() {
     return mGlobalAllocs;
diff --git a/rsov/compiler/RSSPIRVWriter.cpp b/rsov/compiler/RSSPIRVWriter.cpp
index 4f6da2c..22e739d 100644
--- a/rsov/compiler/RSSPIRVWriter.cpp
+++ b/rsov/compiler/RSSPIRVWriter.cpp
@@ -100,12 +100,13 @@
   PassMgr.add(createSPIRVLowerBool());
 }
 
-bool WriteSPIRV(llvm::Module *M, llvm::raw_ostream &OS, std::string &ErrMsg) {
+bool WriteSPIRV(llvm::Module *M, std::unique_ptr<bcinfo::MetadataExtractor> ME,
+                llvm::raw_ostream &OS, std::string &ErrMsg) {
   HandleTargetTriple(*M);
 
   Context &Ctxt = Context::getInstance();
 
-  if (!Ctxt.Initialize(M)) {
+  if (!Ctxt.Initialize(std::move(ME))) {
     ErrMsg = "Failed to intialize rs2spirv";
     return false;
   }
diff --git a/rsov/compiler/RSSPIRVWriter.h b/rsov/compiler/RSSPIRVWriter.h
index 91fe4d0..4b0d80c 100644
--- a/rsov/compiler/RSSPIRVWriter.h
+++ b/rsov/compiler/RSSPIRVWriter.h
@@ -24,9 +24,14 @@
 class raw_ostream;
 } // namespace llvm
 
+namespace bcinfo {
+class MetadataExtractor;
+} // namespace bcinfo
+
 namespace rs2spirv {
 
-bool WriteSPIRV(llvm::Module *M, llvm::raw_ostream &OS, std::string &ErrMsg);
+bool WriteSPIRV(llvm::Module *M, std::unique_ptr<bcinfo::MetadataExtractor> ME,
+                llvm::raw_ostream &OS, std::string &ErrMsg);
 
 } // namespace rs2spirv
 
diff --git a/rsov/compiler/rs2spirv.cpp b/rsov/compiler/rs2spirv.cpp
index 0c1ff29..6701d27 100644
--- a/rsov/compiler/rs2spirv.cpp
+++ b/rsov/compiler/rs2spirv.cpp
@@ -14,6 +14,10 @@
  * limitations under the License.
  */
 
+#include "RSSPIRVWriter.h"
+#include "bcinfo/MetadataExtractor.h"
+#include "spirit/file_utils.h"
+
 #include "llvm/Bitcode/ReaderWriter.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
@@ -26,8 +30,6 @@
 #include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/raw_ostream.h"
 
-#include "RSSPIRVWriter.h"
-
 #define DEBUG_TYPE "rs2spirv"
 
 namespace kExt {
@@ -59,7 +61,6 @@
     errs() << "Fails to open input file: " << Err;
     return -1;
   }
-
   ErrorOr<std::unique_ptr<Module>> MOrErr =
       getStreamedBitcodeModule(InputFile, std::move(DS), Context);
 
@@ -85,7 +86,12 @@
   llvm::StringRef outFile(OutputFile);
   std::error_code EC;
   llvm::raw_fd_ostream OFS(outFile, EC, llvm::sys::fs::F_None);
-  if (!rs2spirv::WriteSPIRV(M.get(), OFS, Err)) {
+
+  std::vector<char> bitcode = android::spirit::readFile<char>(InputFile);
+  std::unique_ptr<bcinfo::MetadataExtractor> ME(
+      new bcinfo::MetadataExtractor(bitcode.data(), bitcode.size()));
+
+  if (!rs2spirv::WriteSPIRV(M.get(), std::move(ME), OFS, Err)) {
     errs() << "compiler error: " << Err << '\n';
     return -1;
   }