swr/rast: Store cached files in multiple subdirs
authorAlok Hota <alok.hota@intel.com>
Thu, 20 Sep 2018 03:23:15 +0000 (22:23 -0500)
committerAlok Hota <alok.hota@intel.com>
Wed, 16 Jan 2019 19:53:30 +0000 (13:53 -0600)
This improves cache filesystem performance, especially during CI tests
Also updated jitcache magic number due to codegen parameter changes
Removed 2 `if constexpr` to prevent C++17 requirement

src/gallium/drivers/swr/rasterizer/jitter/JitManager.cpp
src/gallium/drivers/swr/rasterizer/jitter/JitManager.h
src/gallium/drivers/swr/rasterizer/jitter/builder_misc.cpp

index 1b2b570318c0a05b1e628d93689d228fa498c96f..a549721f147336bd97096d00e245c0a5afc03977 100644 (file)
@@ -582,7 +582,7 @@ struct JitCacheFileHeader
     uint64_t GetObjectCRC() const { return m_objCRC; }
 
 private:
-    static const uint64_t JC_MAGIC_NUMBER = 0xfedcba9876543211ULL + 4;
+    static const uint64_t JC_MAGIC_NUMBER = 0xfedcba9876543210ULL + 6;
     static const size_t   JC_STR_MAX_LEN  = 32;
     static const uint32_t JC_PLATFORM_KEY = (LLVM_VERSION_MAJOR << 24) |
                                             (LLVM_VERSION_MINOR << 16) | (LLVM_VERSION_PATCH << 8) |
@@ -634,6 +634,15 @@ JitCache::JitCache()
     {
         mCacheDir = KNOB_JIT_CACHE_DIR;
     }
+
+    // Create cache dir at startup to allow jitter to write debug.ll files
+    // to that directory.
+    if (!llvm::sys::fs::exists(mCacheDir.str()) &&
+        llvm::sys::fs::create_directories(mCacheDir.str()))
+    {
+        SWR_INVALID("Unable to create directory: %s", mCacheDir.c_str());
+    }
+
 }
 
 int ExecUnhookedProcess(const std::string& CmdLine, std::string* pStdOut, std::string* pStdErr)
@@ -641,6 +650,26 @@ int ExecUnhookedProcess(const std::string& CmdLine, std::string* pStdOut, std::s
     return ExecCmd(CmdLine, "", pStdOut, pStdErr);
 }
 
+/// Calculate actual directory where module will be cached.
+/// This is always a subdirectory of mCacheDir.  Full absolute
+/// path name will be stored in mCurrentModuleCacheDir
+void JitCache::CalcModuleCacheDir()
+{
+    mModuleCacheDir.clear();
+
+    llvm::SmallString<MAX_PATH> moduleDir = mCacheDir;
+
+    // Create 4 levels of directory hierarchy based on CRC, 256 entries each
+    uint8_t* pCRC = (uint8_t*)&mCurrentModuleCRC;
+    for (uint32_t i = 0; i < 4; ++i)
+    {
+        llvm::sys::path::append(moduleDir, std::to_string((int)pCRC[i]));
+    }
+
+    mModuleCacheDir = moduleDir;
+}
+
+
 /// notifyObjectCompiled - Provides a pointer to compiled code for Module M.
 void JitCache::notifyObjectCompiled(const llvm::Module* M, llvm::MemoryBufferRef Obj)
 {
@@ -650,16 +679,22 @@ void JitCache::notifyObjectCompiled(const llvm::Module* M, llvm::MemoryBufferRef
         return;
     }
 
-    if (!llvm::sys::fs::exists(mCacheDir.str()) &&
-        llvm::sys::fs::create_directories(mCacheDir.str()))
+    if (!mModuleCacheDir.size())
     {
-        SWR_INVALID("Unable to create directory: %s", mCacheDir.c_str());
+        SWR_INVALID("Unset module cache directory");
+        return;
+    }
+
+    if (!llvm::sys::fs::exists(mModuleCacheDir.str()) &&
+        llvm::sys::fs::create_directories(mModuleCacheDir.str()))
+    {
+        SWR_INVALID("Unable to create directory: %s", mModuleCacheDir.c_str());
         return;
     }
 
     JitCacheFileHeader header;
 
-    llvm::SmallString<MAX_PATH> filePath = mCacheDir;
+    llvm::SmallString<MAX_PATH> filePath = mModuleCacheDir;
     llvm::sys::path::append(filePath, moduleID);
 
     llvm::SmallString<MAX_PATH> objPath = filePath;
@@ -699,12 +734,14 @@ std::unique_ptr<llvm::MemoryBuffer> JitCache::getObject(const llvm::Module* M)
         return nullptr;
     }
 
-    if (!llvm::sys::fs::exists(mCacheDir))
+    CalcModuleCacheDir();
+
+    if (!llvm::sys::fs::exists(mModuleCacheDir))
     {
         return nullptr;
     }
 
-    llvm::SmallString<MAX_PATH> filePath = mCacheDir;
+    llvm::SmallString<MAX_PATH> filePath = mModuleCacheDir;
     llvm::sys::path::append(filePath, moduleID);
 
     llvm::SmallString<MAX_PATH> objFilePath = filePath;
index 5659191525dadeee65f54c4c81272b695654978c..bb7ca8b4a3e506d5985cadb0f82d4a524aa8c8d9 100644 (file)
@@ -113,9 +113,15 @@ public:
 private:
     std::string                 mCpu;
     llvm::SmallString<MAX_PATH> mCacheDir;
+    llvm::SmallString<MAX_PATH> mModuleCacheDir;
     uint32_t                    mCurrentModuleCRC = 0;
     JitManager*                 mpJitMgr          = nullptr;
     llvm::CodeGenOpt::Level     mOptLevel         = llvm::CodeGenOpt::None;
+
+    /// Calculate actual directory where module will be cached.
+    /// This is always a subdirectory of mCacheDir.  Full absolute
+    /// path name will be stored in mCurrentModuleCacheDir
+    void CalcModuleCacheDir();
 };
 
 //////////////////////////////////////////////////////////////////////////
index 65eec4e4c68540fecdb96189c073ff4229e2db4e..24b733a28ae8ab734272339901a11726407d7d4f 100644 (file)
@@ -774,13 +774,6 @@ namespace SwrJit
     {
         SWR_ASSERT((numIntBits + numFracBits) <= 32, "Can only handle 32-bit fixed-point values");
         Value* fixed = nullptr;
-        if constexpr (false) // This doesn't work for negative numbers!!
-        {
-            fixed = FP_TO_SI(VROUND(FMUL(vFloat, VIMMED1(float(1 << numFracBits))),
-                                    C(_MM_FROUND_TO_NEAREST_INT)),
-                             mSimdInt32Ty);
-        }
-        else
         {
             // Do round to nearest int on fractional bits first
             // Not entirely perfect for negative numbers, but close enough
@@ -844,35 +837,13 @@ namespace SwrJit
     {
         SWR_ASSERT((numIntBits + numFracBits) <= 32, "Can only handle 32-bit fixed-point values");
         Value* fixed = nullptr;
-        if constexpr (true) // KNOB_SIM_FAST_MATH?  Below works correctly from a precision
-                            // standpoint...
+        // KNOB_SIM_FAST_MATH?  Below works correctly from a precision
+        // standpoint...
         {
             fixed = FP_TO_UI(VROUND(FMUL(vFloat, VIMMED1(float(1 << numFracBits))),
                                     C(_MM_FROUND_TO_NEAREST_INT)),
                              mSimdInt32Ty);
         }
-        else
-        {
-            // Do round to nearest int on fractional bits first
-            vFloat = VROUND(FMUL(vFloat, VIMMED1(float(1 << numFracBits))),
-                            C(_MM_FROUND_TO_NEAREST_INT));
-            vFloat = FMUL(vFloat, VIMMED1(1.0f / float(1 << numFracBits)));
-
-            // TODO: Handle INF, NAN, overflow / underflow, etc.
-
-            Value* vSgn      = FCMP_OLT(vFloat, VIMMED1(0.0f));
-            Value* vFloatInt = BITCAST(vFloat, mSimdInt32Ty);
-            Value* vFixed    = AND(vFloatInt, VIMMED1((1 << 23) - 1));
-            vFixed           = OR(vFixed, VIMMED1(1 << 23));
-
-            Value* vExp = LSHR(SHL(vFloatInt, VIMMED1(1)), VIMMED1(24));
-            vExp        = SUB(vExp, VIMMED1(127));
-
-            Value* vExtraBits = SUB(VIMMED1(23 - numFracBits), vExp);
-
-            fixed = LSHR(vFixed, vExtraBits, name);
-        }
-
         return fixed;
     }