Merge "remove dead code (ALOGE) in rsCpuIntrinsicBLAS.cpp"
diff --git a/cpu_ref/rsCpuCore.cpp b/cpu_ref/rsCpuCore.cpp
index e2d3bf1..3ce9d9d 100644
--- a/cpu_ref/rsCpuCore.cpp
+++ b/cpu_ref/rsCpuCore.cpp
@@ -115,6 +115,8 @@
     mLinkRuntimeCallback = nullptr;
     mSelectRTCallback = nullptr;
     mSetupCompilerCallback = nullptr;
+    mEmbedGlobalInfo = true;
+    mEmbedGlobalInfoSkipConstant = true;
 }
 
 
diff --git a/cpu_ref/rsCpuCore.h b/cpu_ref/rsCpuCore.h
index afe8ef5..843fa54 100644
--- a/cpu_ref/rsCpuCore.h
+++ b/cpu_ref/rsCpuCore.h
@@ -135,6 +135,28 @@
     }
     virtual bool getInForEach() { return mInForEach; }
 
+    // Set to true if we should embed global variable information in the code.
+    virtual void setEmbedGlobalInfo(bool v) {
+        mEmbedGlobalInfo = v;
+    }
+
+    // Returns true if we should embed global variable information in the code.
+    virtual bool getEmbedGlobalInfo() const {
+        return mEmbedGlobalInfo;
+    }
+
+    // Set to true if we should skip constant (immutable) global variables when
+    // potentially embedding information about globals.
+    virtual void setEmbedGlobalInfoSkipConstant(bool v) {
+        mEmbedGlobalInfoSkipConstant = v;
+    }
+
+    // Returns true if we should skip constant (immutable) global variables when
+    // potentially embedding information about globals.
+    virtual bool getEmbedGlobalInfoSkipConstant() const {
+        return mEmbedGlobalInfoSkipConstant;
+    }
+
 protected:
     Context *mRSC;
     uint32_t version_major;
@@ -164,6 +186,16 @@
     RSSelectRTCallback mSelectRTCallback;
     RSSetupCompilerCallback mSetupCompilerCallback;
     String8 mBccPluginName;
+
+    // Specifies whether we should embed global variable information in the
+    // code via special RS variables that can be examined later by the driver.
+    // Defaults to true.
+    bool mEmbedGlobalInfo;
+
+    // Specifies whether we should skip constant (immutable) global variables
+    // when potentially embedding information about globals.
+    // Defaults to true.
+    bool mEmbedGlobalInfoSkipConstant;
 };
 
 
diff --git a/cpu_ref/rsCpuExecutable.cpp b/cpu_ref/rsCpuExecutable.cpp
index e783108..f229f52 100644
--- a/cpu_ref/rsCpuExecutable.cpp
+++ b/cpu_ref/rsCpuExecutable.cpp
@@ -128,12 +128,13 @@
     const char *compiler_rt = SYSLIBPATH"/libcompiler_rt.so";
     const char *mTriple = "-mtriple=" DEFAULT_TARGET_TRIPLE_STRING;
     const char *libPath = "--library-path=" SYSLIBPATH;
+    const char *vendorLibPath = "--library-path=" SYSLIBPATH_VENDOR;
 
     std::vector<const char *> args = {
         LD_EXE_PATH,
         "-shared",
         "-nostdlib",
-        compiler_rt, mTriple, libPath,
+        compiler_rt, mTriple, vendorLibPath, libPath,
         linkDriverName.c_str(), "-lm", "-lc",
         objFileName.c_str(),
         "-o", sharedLibName.c_str(),
@@ -311,6 +312,11 @@
     uint32_t checksum = 0;
 
     const char *rsInfo = (const char *) dlsym(sharedObj, ".rs.info");
+    int numEntries = 0;
+    const int *rsGlobalEntries = (const int *) dlsym(sharedObj, ".rs.global_entries");
+    const char **rsGlobalNames = (const char **) dlsym(sharedObj, ".rs.global_names");
+    const void **rsGlobalAddresses = (const void **) dlsym(sharedObj, ".rs.global_addresses");
+    const size_t *rsGlobalSizes = (const size_t *) dlsym(sharedObj, ".rs.global_sizes");
 
     if (strgets(line, MAXLINE, &rsInfo) == nullptr) {
         return nullptr;
@@ -545,11 +551,25 @@
 
 #endif  // RS_COMPATIBILITY_LIB
 
+    // Read in information about mutable global variables provided by bcc's
+    // RSGlobalInfoPass
+    if (rsGlobalEntries) {
+        numEntries = *rsGlobalEntries;
+        if (numEntries > 0) {
+            rsAssert(rsGlobalNames);
+            rsAssert(rsGlobalAddresses);
+            rsAssert(rsGlobalSizes);
+        }
+    } else {
+        ALOGD("Missing .rs.global_entries from shared object");
+    }
+
     return new ScriptExecutable(
         RSContext, fieldAddress, fieldIsObject, fieldName, varCount,
         invokeFunctions, funcCount,
         forEachFunctions, forEachSignatures, forEachCount,
         pragmaKeys, pragmaValues, pragmaCount,
+        rsGlobalNames, rsGlobalAddresses, rsGlobalSizes, numEntries,
         isThreadable, checksum);
 
 error:
@@ -590,5 +610,14 @@
     return nullptr;
 }
 
+bool ScriptExecutable::dumpGlobalInfo() const {
+    ALOGE("Globals: %p %p %p", mGlobalAddresses, mGlobalSizes, mGlobalNames);
+    for (int i = 0; i < mGlobalEntries; i++) {
+        ALOGE("Global[%d]: %p %zu %s", i, mGlobalAddresses[i], mGlobalSizes[i],
+              mGlobalNames[i]);
+    }
+    return true;
+}
+
 }  // namespace renderscript
 }  // namespace android
diff --git a/cpu_ref/rsCpuExecutable.h b/cpu_ref/rsCpuExecutable.h
index fd79ca1..ec872f6 100644
--- a/cpu_ref/rsCpuExecutable.h
+++ b/cpu_ref/rsCpuExecutable.h
@@ -66,6 +66,8 @@
                      size_t forEachCount,
                      const char** pragmaKeys, const char** pragmaValues,
                      size_t pragmaCount,
+                     const char **globalNames, const void **globalAddresses,
+                     const size_t *globalSizes, size_t globalEntries,
                      bool isThreadable, uint32_t buildChecksum) :
         mFieldAddress(fieldAddress), mFieldIsObject(fieldIsObject),
         mFieldName(fieldName), mExportedVarCount(varCount),
@@ -73,7 +75,9 @@
         mForEachFunctions(forEachFunctions), mForEachSignatures(forEachSignatures),
         mForEachCount(forEachCount),
         mPragmaKeys(pragmaKeys), mPragmaValues(pragmaValues),
-        mPragmaCount(pragmaCount), mIsThreadable(isThreadable),
+        mPragmaCount(pragmaCount), mGlobalNames(globalNames),
+        mGlobalAddresses(globalAddresses), mGlobalSizes(globalSizes),
+        mGlobalEntries(globalEntries), mIsThreadable(isThreadable),
         mBuildChecksum(buildChecksum), mRS(RSContext) {
     }
 
@@ -134,10 +138,35 @@
     const char ** getPragmaKeys() const { return mPragmaKeys; }
     const char ** getPragmaValues() const { return mPragmaValues; }
 
+    const char* getGlobalName(int i) const {
+        if (i < mGlobalEntries) {
+            return mGlobalNames[i];
+        } else {
+            return nullptr;
+        }
+    }
+    const void* getGlobalAddress(int i) const {
+        if (i < mGlobalEntries) {
+            return mGlobalAddresses[i];
+        } else {
+            return nullptr;
+        }
+    }
+    size_t getGlobalSize(int i) const {
+        if (i < mGlobalEntries) {
+            return mGlobalSizes[i];
+        } else {
+            return 0;
+        }
+    }
+    int getGlobalEntries() const { return mGlobalEntries; }
+
     bool getThreadable() const { return mIsThreadable; }
 
     uint32_t getBuildChecksum() const { return mBuildChecksum; }
 
+    bool dumpGlobalInfo() const;
+
 private:
     void** mFieldAddress;
     bool* mFieldIsObject;
@@ -155,6 +184,11 @@
     const char ** mPragmaValues;
     size_t mPragmaCount;
 
+    const char ** mGlobalNames;
+    const void ** mGlobalAddresses;
+    const size_t * mGlobalSizes;
+    int mGlobalEntries;
+
     bool mIsThreadable;
     uint32_t mBuildChecksum;
 
diff --git a/cpu_ref/rsCpuScript.cpp b/cpu_ref/rsCpuScript.cpp
index 3282374..65ced66 100644
--- a/cpu_ref/rsCpuScript.cpp
+++ b/cpu_ref/rsCpuScript.cpp
@@ -55,6 +55,9 @@
 #endif
 
 namespace {
+
+static const bool kDebugGlobalVariables = false;
+
 #ifndef RS_COMPATIBILITY_LIB
 
 static bool is_force_recompile() {
@@ -83,11 +86,18 @@
                                 const std::string& bcFileName,
                                 const char* cacheDir, const char* resName,
                                 const char* core_lib, bool useRSDebugContext,
-                                const char* bccPluginName) {
+                                const char* bccPluginName, bool emitGlobalInfo,
+                                bool emitGlobalInfoSkipConstant) {
     rsAssert(cacheDir && resName && core_lib);
     args->push_back(android::renderscript::RsdCpuScriptImpl::BCC_EXE_PATH);
     args->push_back("-unroll-runtime");
     args->push_back("-scalarize-load-store");
+    if (emitGlobalInfo) {
+        args->push_back("-rs-global-info");
+        if (emitGlobalInfoSkipConstant) {
+            args->push_back("-rs-global-info-skip-constant");
+        }
+    }
     args->push_back("-o");
     args->push_back(resName);
     args->push_back("-output_path");
@@ -276,6 +286,10 @@
     mIsThreadable = mScriptExec->getThreadable();
     //ALOGE("Script isThreadable? %d", mIsThreadable);
 
+    if (kDebugGlobalVariables) {
+        mScriptExec->dumpGlobalInfo();
+    }
+
     return true;
 }
 
@@ -326,8 +340,11 @@
     bcFileName.append(".bc");
 
     std::vector<const char*> compileArguments;
+    bool emitGlobalInfo = mCtx->getEmbedGlobalInfo();
+    bool emitGlobalInfoSkipConstant = mCtx->getEmbedGlobalInfoSkipConstant();
     setCompileArguments(&compileArguments, bcFileName, cacheDir, resName, core_lib,
-                        useRSDebugContext, bccPluginName);
+                        useRSDebugContext, bccPluginName, emitGlobalInfo,
+                        emitGlobalInfoSkipConstant);
 
     mChecksumNeeded = isChecksumNeeded();
     if (mChecksumNeeded) {
@@ -889,6 +906,22 @@
     return nullptr;
 }
 
+int RsdCpuScriptImpl::getGlobalEntries() const {
+    return mScriptExec->getGlobalEntries();
+}
+
+const char * RsdCpuScriptImpl::getGlobalName(int i) const {
+    return mScriptExec->getGlobalName(i);
+}
+
+const void * RsdCpuScriptImpl::getGlobalAddress(int i) const {
+    return mScriptExec->getGlobalAddress(i);
+}
+
+size_t RsdCpuScriptImpl::getGlobalSize(int i) const {
+    return mScriptExec->getGlobalSize(i);
+}
+
 void RsdCpuScriptImpl::preLaunch(uint32_t slot, const Allocation ** ains,
                                  uint32_t inLen, Allocation * aout,
                                  const void * usr, uint32_t usrLen,
diff --git a/cpu_ref/rsCpuScript.h b/cpu_ref/rsCpuScript.h
index 22f973b..b7e12e7 100644
--- a/cpu_ref/rsCpuScript.h
+++ b/cpu_ref/rsCpuScript.h
@@ -107,6 +107,11 @@
     virtual Allocation * getAllocationForPointer(const void *ptr) const;
     bool storeRSInfoFromSO();
 
+    int getGlobalEntries() const override;
+    const char * getGlobalName(int i) const override;
+    const void * getGlobalAddress(int i) const override;
+    size_t getGlobalSize(int i) const override;
+
 protected:
     RsdCpuReferenceImpl *mCtx;
     const Script *mScript;
@@ -153,8 +158,10 @@
 
 #ifdef __LP64__
 #define SYSLIBPATH "/system/lib64"
+#define SYSLIBPATH_VENDOR "/system/vendor/lib64"
 #else
 #define SYSLIBPATH "/system/lib"
+#define SYSLIBPATH_VENDOR "/system/vendor/lib"
 #endif
 
 }
diff --git a/cpu_ref/rsd_cpu.h b/cpu_ref/rsd_cpu.h
index c53e880..8452828 100644
--- a/cpu_ref/rsd_cpu.h
+++ b/cpu_ref/rsd_cpu.h
@@ -87,6 +87,17 @@
         virtual void setGlobalObj(uint32_t slot, ObjectBase *obj) = 0;
 
         virtual Allocation * getAllocationForPointer(const void *ptr) const = 0;
+
+        // Returns number of global variables in this Script (may be 0 if
+        // compiler is not configured to emit this information).
+        virtual int getGlobalEntries() const = 0;
+        // Returns the name of the global variable at index i.
+        virtual const char * getGlobalName(int i) const = 0;
+        // Returns the CPU address of the global variable at index i.
+        virtual const void * getGlobalAddress(int i) const = 0;
+        // Returns the size (in bytes) of the global variable at index i.
+        virtual size_t getGlobalSize(int i) const = 0;
+
         virtual ~CpuScript() {}
     };
     typedef CpuScript * (* script_lookup_t)(Context *, const Script *s);
diff --git a/driver/runtime/build_bc_lib_internal.mk b/driver/runtime/build_bc_lib_internal.mk
index c5c8078..a44461b 100644
--- a/driver/runtime/build_bc_lib_internal.mk
+++ b/driver/runtime/build_bc_lib_internal.mk
@@ -84,10 +84,8 @@
 $(ll_bc_files): $(intermediates)/%.bc: $(LOCAL_PATH)/%.ll $(LLVM_AS)
 	@mkdir -p $(dir $@)
 	$(hide) $(LLVM_AS) $< -o $@
-	$(call transform-d-to-p-args,$(@:%.bc=%.d),$(@:%.bc=%.P))
 
 -include $(c_bc_files:%.bc=%.P)
--include $(ll_bc_files:%.bc=%.P)
 
 $(LOCAL_BUILT_MODULE): PRIVATE_BC_FILES := $(c_bc_files) $(ll_bc_files)
 $(LOCAL_BUILT_MODULE): $(c_bc_files) $(ll_bc_files)
diff --git a/rsEnv.h b/rsEnv.h
index b82eaf1..924e171 100644
--- a/rsEnv.h
+++ b/rsEnv.h
@@ -27,3 +27,7 @@
 #define RS_PROGRAM_VERTEX_PROJECTION_OFFSET 16
 #define RS_PROGRAM_VERTEX_TEXTURE_OFFSET 32
 #define RS_PROGRAM_VERTEX_MVP_OFFSET 48
+
+#define RS_KERNEL_MAX_ARGUMENTS 256
+#define RS_CLOSURE_MAX_NUMBER_ARGS_AND_BINDINGS 1024
+#define RS_SCRIPT_GROUP_MAX_NUMBER_CLOSURES (2<<20)