Merge "Revert "Structure layout logic cleanup.""
diff --git a/include/bcc/Compiler.h b/include/bcc/Compiler.h
index 708e1b0..73f8274 100644
--- a/include/bcc/Compiler.h
+++ b/include/bcc/Compiler.h
@@ -67,9 +67,7 @@
 
     kIllegalGlobalFunction,
 
-    kErrInvalidTargetMachine,
-
-    kErrInvalidLayout
+    kErrInvalidTargetMachine
   };
 
   static const char *GetErrorString(enum ErrorCode pErrCode);
diff --git a/include/bcc/Script.h b/include/bcc/Script.h
index 3b10d82..b4135c5 100644
--- a/include/bcc/Script.h
+++ b/include/bcc/Script.h
@@ -17,8 +17,6 @@
 #ifndef BCC_SCRIPT_H
 #define BCC_SCRIPT_H
 
-#include "slang_version.h"
-
 #include <llvm/Support/CodeGen.h>
 #include "bcc/Source.h"
 
@@ -67,10 +65,6 @@
     return getSource().getCompilerVersion();
   }
 
-  bool isStructExplicitlyPaddedBySlang() const {
-    return getCompilerVersion() >= SlangVersion::N_STRUCT_EXPLICIT_PADDING;
-  }
-
   void setOptimizationLevel(llvm::CodeGenOpt::Level pOptimizationLevel) {
     mOptimizationLevel = pOptimizationLevel;
   }
diff --git a/lib/Android.bp b/lib/Android.bp
index 6ac3652..e16b034 100644
--- a/lib/Android.bp
+++ b/lib/Android.bp
@@ -43,8 +43,6 @@
 
     shared_libs: ["libbcinfo"],
 
-    header_libs: ["slang_headers"],
-
     target: {
         windows: {
             enabled: true,
diff --git a/lib/Compiler.cpp b/lib/Compiler.cpp
index 5000fa1..ca14d1f 100644
--- a/lib/Compiler.cpp
+++ b/lib/Compiler.cpp
@@ -45,74 +45,6 @@
 #include <string>
 #include <set>
 
-namespace {
-
-// Name of metadata node where list of exported types resides
-// (should be synced with slang_rs_metadata.h)
-static const llvm::StringRef ExportedTypeMetadataName = "#rs_export_type";
-
-// Every exported struct type must have the same layout according to
-// the Module's DataLayout that it does according to the
-// TargetMachine's DataLayout -- that is, the front end (represented
-// by Module) and back end (represented by TargetMachine) must agree.
-bool validateLayoutOfExportedTypes(const llvm::Module &module,
-                                   const llvm::DataLayout &moduleDataLayout,
-                                   const llvm::DataLayout &targetDataLayout) {
-  if (moduleDataLayout == targetDataLayout)
-    return true;
-
-  const llvm::NamedMDNode *const exportedTypesMD =
-      module.getNamedMetadata(ExportedTypeMetadataName);
-  if (!exportedTypesMD)
-    return true;
-
-  bool allOk = true;
-  for (const llvm::MDNode *const exportedTypeMD : exportedTypesMD->operands()) {
-    bccAssert(exportedTypeMD->getNumOperands() == 1);
-
-    // The name of the type in LLVM is the name of the type in the
-    // metadata with "struct." prepended.
-    std::string exportedTypeName =
-        "struct." +
-        llvm::cast<llvm::MDString>(exportedTypeMD->getOperand(0))->getString().str();
-
-    llvm::StructType *const exportedType = module.getTypeByName(exportedTypeName);
-
-    if (!exportedType) {
-      // presumably this means the type got optimized away
-      continue;
-    }
-
-    const llvm::StructLayout *const moduleStructLayout = moduleDataLayout.getStructLayout(exportedType);
-    const llvm::StructLayout *const targetStructLayout = targetDataLayout.getStructLayout(exportedType);
-
-    if (moduleStructLayout->getSizeInBits() != targetStructLayout->getSizeInBits()) {
-      ALOGE("%s: getSizeInBits() does not match (%u, %u)", exportedTypeName.c_str(),
-            unsigned(moduleStructLayout->getSizeInBits()), unsigned(targetStructLayout->getSizeInBits()));
-      allOk = false;
-    }
-
-    // We deliberately do not check alignment of the struct as a whole -- the explicit padding
-    // from slang doesn't force the alignment.
-
-    for (unsigned elementCount = exportedType->getNumElements(), elementIdx = 0;
-         elementIdx < elementCount; ++elementIdx) {
-      if (moduleStructLayout->getElementOffsetInBits(elementIdx) !=
-          targetStructLayout->getElementOffsetInBits(elementIdx)) {
-        ALOGE("%s: getElementOffsetInBits(%u) does not match (%u, %u)",
-              exportedTypeName.c_str(), elementIdx,
-              unsigned(moduleStructLayout->getElementOffsetInBits(elementIdx)),
-              unsigned(targetStructLayout->getElementOffsetInBits(elementIdx)));
-        allOk = false;
-      }
-    }
-  }
-
-  return allOk;
-}
-
-}  // end unnamed namespace
-
 using namespace bcc;
 
 const char *Compiler::GetErrorString(enum ErrorCode pErrCode) {
@@ -145,8 +77,6 @@
     return "Use of undefined external function";
   case kErrInvalidTargetMachine:
     return "Invalid/unexpected llvm::TargetMachine.";
-  case kErrInvalidLayout:
-    return "Invalid layout (RenderScript ABI and native ABI are incompatible)";
   }
 
   // This assert should never be reached as the compiler verifies that the
@@ -322,17 +252,12 @@
     return kErrInvalidSource;
   }
 
-  if (script.isStructExplicitlyPaddedBySlang()) {
-    if (!validateLayoutOfExportedTypes(module, module.getDataLayout(), dl))
-      return kErrInvalidLayout;
-  } else {
-    if (getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86) {
-      // Detect and fail if TargetMachine datalayout is different than what we
-      // expect.  This is to detect changes in default target layout for x86 and
-      // update X86_CUSTOM_DL_STRING in include/bcc/Config/Config.h appropriately.
-      if (dl.getStringRepresentation().compare(X86_DEFAULT_DL_STRING) != 0) {
-        return kErrInvalidTargetMachine;
-      }
+  if (getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86) {
+    // Detect and fail if TargetMachine datalayout is different than what we
+    // expect.  This is to detect changes in default target layout for x86 and
+    // update X86_CUSTOM_DL_STRING in include/bcc/Config/Config.h appropriately.
+    if (dl.getStringRepresentation().compare(X86_DEFAULT_DL_STRING) != 0) {
+      return kErrInvalidTargetMachine;
     }
   }
 
diff --git a/lib/RSCompilerDriver.cpp b/lib/RSCompilerDriver.cpp
index be91343..6ed7400 100644
--- a/lib/RSCompilerDriver.cpp
+++ b/lib/RSCompilerDriver.cpp
@@ -20,7 +20,6 @@
 #include "FileMutex.h"
 #include "Log.h"
 #include "RSScriptGroupFusion.h"
-#include "slang_version.h"
 
 #include "bcc/BCCContext.h"
 #include "bcc/Compiler.h"
@@ -131,8 +130,7 @@
   // (during LinkRuntime below) to ensure that RenderScript-driver-provided
   // structs (like Allocation_t) don't get forced into using the ARM layout
   // rules.
-  if (!pScript.isStructExplicitlyPaddedBySlang() &&
-      (mCompiler.getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86)) {
+  if (mCompiler.getTargetMachine().getTargetTriple().getArch() == llvm::Triple::x86) {
     mCompiler.translateGEPs(pScript);
   }
 
@@ -277,7 +275,7 @@
 // Assertion-enabled builds can't compile legacy bitcode (due to the use of
 // getName() with anonymous structure definitions).
 #ifdef _DEBUG
-  static const uint32_t kSlangMinimumFixedStructureNames = SlangVersion::M_RS_OBJECT;
+  static const uint32_t kSlangMinimumFixedStructureNames = 2310;
   uint32_t version = wrapper.getCompilerVersion();
   if (version < kSlangMinimumFixedStructureNames) {
     ALOGE("Found invalid legacy bitcode compiled with a version %u llvm-rs-cc "
diff --git a/lib/RSKernelExpand.cpp b/lib/RSKernelExpand.cpp
index 2f451d7..e2fb6f4 100644
--- a/lib/RSKernelExpand.cpp
+++ b/lib/RSKernelExpand.cpp
@@ -22,8 +22,6 @@
 #include "bcc/Config.h"
 #include "bcinfo/MetadataExtractor.h"
 
-#include "slang_version.h"
-
 #include <cstdlib>
 #include <functional>
 #include <unordered_set>
@@ -91,7 +89,7 @@
   static char ID;
 
 private:
-  static const size_t RS_KERNEL_INPUT_LIMIT = 8;  // see frameworks/base/libs/rs/cpu_ref/rsCpuCoreRuntime.h
+  static const size_t RS_KERNEL_INPUT_LIMIT = 8; // see frameworks/base/libs/rs/cpu_ref/rsCpuCoreRuntime.h
 
   typedef std::unordered_set<llvm::Function *> FunctionSet;
 
@@ -132,8 +130,6 @@
   llvm::FunctionType *ExpandedForEachType;
   llvm::Type *RsExpandKernelDriverInfoPfxTy;
 
-  // Initialized when we begin to process each Module
-  bool mStructExplicitlyPaddedBySlang;
   uint32_t mExportForEachCount;
   const char **mExportForEachNameList;
   const uint32_t *mExportForEachSignatureList;
@@ -678,7 +674,7 @@
       llvm::LoadInst *InBufPtr = Builder.CreateLoad(InBufPtrAddr, "input_buf");
 
       llvm::Value *CastInBufPtr = nullptr;
-      if (mStructExplicitlyPaddedBySlang || (Module->getTargetTriple() != DEFAULT_X86_TRIPLE_STRING)) {
+      if (Module->getTargetTriple() != DEFAULT_X86_TRIPLE_STRING) {
         CastInBufPtr = Builder.CreatePointerCast(InBufPtr, InType, "casted_in");
       } else {
         // The disagreement between module and x86 target machine datalayout
@@ -686,7 +682,7 @@
         // code and bcc codegen for GetElementPtr. To solve this issue, skip the
         // cast to InType and leave CastInBufPtr as an int8_t*.  The buffer is
         // later indexed with an explicit byte offset computed based on
-        // X86_CUSTOM_DL_STRING and then bitcast to actual input type.
+        // X86_CUSTOM_DL_STRING and then bitcast it to actual input type.
         CastInBufPtr = InBufPtr;
       }
 
@@ -732,7 +728,7 @@
     for (size_t Index = 0; Index < NumInputs; ++Index) {
 
       llvm::Value *InPtr = nullptr;
-      if (mStructExplicitlyPaddedBySlang || (Module->getTargetTriple() != DEFAULT_X86_TRIPLE_STRING)) {
+      if (Module->getTargetTriple() != DEFAULT_X86_TRIPLE_STRING) {
         InPtr = Builder.CreateInBoundsGEP(InBufPtrs[Index], Offset);
       } else {
         // Treat x86 input buffer as byte[], get indexed pointer with explicit
@@ -788,7 +784,7 @@
     }
 
     llvm::DataLayout DL(Module);
-    if (!mStructExplicitlyPaddedBySlang && (Module->getTargetTriple() == DEFAULT_X86_TRIPLE_STRING)) {
+    if (Module->getTargetTriple() == DEFAULT_X86_TRIPLE_STRING) {
       DL.reset(X86_CUSTOM_DL_STRING);
     }
 
@@ -921,7 +917,7 @@
 
     // TODO: Refactor this to share functionality with ExpandOldStyleForEach.
     llvm::DataLayout DL(Module);
-    if (!mStructExplicitlyPaddedBySlang && (Module->getTargetTriple() == DEFAULT_X86_TRIPLE_STRING)) {
+    if (Module->getTargetTriple() == DEFAULT_X86_TRIPLE_STRING) {
       DL.reset(X86_CUSTOM_DL_STRING);
     }
     llvm::Type *Int32Ty = llvm::Type::getInt32Ty(*Context);
@@ -1005,7 +1001,7 @@
         OutBasePtr->setMetadata("tbaa", TBAAPointer);
       }
 
-      if (mStructExplicitlyPaddedBySlang || (Module->getTargetTriple() != DEFAULT_X86_TRIPLE_STRING)) {
+      if (Module->getTargetTriple() != DEFAULT_X86_TRIPLE_STRING) {
         CastedOutBasePtr = Builder.CreatePointerCast(OutBasePtr, OutTy, "casted_out");
       } else {
         // The disagreement between module and x86 target machine datalayout
@@ -1013,7 +1009,7 @@
         // code and bcc codegen for GetElementPtr. To solve this issue, skip the
         // cast to OutTy and leave CastedOutBasePtr as an int8_t*.  The buffer
         // is later indexed with an explicit byte offset computed based on
-        // X86_CUSTOM_DL_STRING and then bitcast to actual output type.
+        // X86_CUSTOM_DL_STRING and then bitcast it to actual output type.
         CastedOutBasePtr = OutBasePtr;
       }
     }
@@ -1057,7 +1053,7 @@
     if (CastedOutBasePtr) {
       llvm::Value *OutOffset = Builder.CreateSub(IV, Arg_x1);
 
-      if (mStructExplicitlyPaddedBySlang || (Module->getTargetTriple() != DEFAULT_X86_TRIPLE_STRING)) {
+      if (Module->getTargetTriple() != DEFAULT_X86_TRIPLE_STRING) {
         OutPtr = Builder.CreateInBoundsGEP(CastedOutBasePtr, OutOffset);
       } else {
         // Treat x86 output buffer as byte[], get indexed pointer with explicit
@@ -1378,8 +1374,6 @@
       return false;
     }
 
-    mStructExplicitlyPaddedBySlang = (me.getCompilerVersion() >= SlangVersion::N_STRUCT_EXPLICIT_PADDING);
-
     // Expand forEach_* style kernels.
     mExportForEachCount = me.getExportForEachSignatureCount();
     mExportForEachNameList = me.getExportForEachNameList();
diff --git a/tools/bcc/Android.bp b/tools/bcc/Android.bp
index 507fdd9..791d498 100644
--- a/tools/bcc/Android.bp
+++ b/tools/bcc/Android.bp
@@ -27,8 +27,6 @@
         "libLLVM",
     ],
 
-    header_libs: ["slang_headers"],
-
     target: {
         host: {
             host_ldlibs: ["-ldl"],
diff --git a/tools/bcc_compat/Android.bp b/tools/bcc_compat/Android.bp
index 12e703e..51f5a81 100644
--- a/tools/bcc_compat/Android.bp
+++ b/tools/bcc_compat/Android.bp
@@ -33,15 +33,12 @@
             host_ldlibs: ["-ldl"],
         },
     },
-
     shared_libs: [
         "libbcc",
         "libbcinfo",
         "libLLVM",
     ],
 
-    header_libs: ["slang_headers"],
-
     product_variables: {
         unbundled_build: {
             // Don't build for unbundled branches