ソースを参照

Fix problems with MSFileSystem used from independent locations

CompilerTest would hit appverif break due to using before other
code would call setup.
Adding setup leads to independent locations trying to setup/cleanup
the file system, which would trigger assert.

- Make [Setup/Cleanup]PerThreadFileSystem() support tracking/ignoring
nested calls.
- Added setup/cleanup to CompilerTest and DxilModuleTest
- Added some asserts
Tex Riddell 7 年 前
コミット
fc2d6fe4da

+ 12 - 1
lib/Support/Windows/MSFileSystem.inc.cpp

@@ -53,9 +53,15 @@ namespace fs {
 
 static DWORD g_FileSystemTls;
 
+// skip, but keep track of setup/cleanup nesting
+static unsigned g_FileSystemSetupNested = 0;
+
 error_code SetupPerThreadFileSystem() throw()
 {
-  assert(g_FileSystemTls == 0 && "otherwise this has already been initialized");
+  if (g_FileSystemSetupNested++)
+    return error_code();
+
+  assert(g_FileSystemTls == 0 && "otherwise this has already been initialized, and nesting guard failed.");
   g_FileSystemTls = TlsAlloc();
   if (g_FileSystemTls == TLS_OUT_OF_INDEXES)
   {
@@ -67,12 +73,17 @@ error_code SetupPerThreadFileSystem() throw()
 
 void CleanupPerThreadFileSystem() throw()
 {
+  assert(g_FileSystemSetupNested && "otherwise, Cleanup called without matching Setup");
+  if (--g_FileSystemSetupNested)
+    return;
+  assert(g_FileSystemTls != 0 && "otherwise this has not been initialized");
   TlsFree(g_FileSystemTls);
   g_FileSystemTls = 0;
 }
 
 MSFileSystemRef GetCurrentThreadFileSystem() throw()
 {
+  assert(g_FileSystemTls != 0 && "otherwise this has not been initialized");
   return (MSFileSystemRef)TlsGetValue(g_FileSystemTls);
 }
 

+ 9 - 0
tools/clang/unittests/HLSL/CompilerTest.cpp

@@ -389,6 +389,7 @@ public:
   END_TEST_CLASS()
 
   TEST_CLASS_SETUP(InitSupport);
+  TEST_CLASS_CLEANUP(Cleanup);
 
   TEST_METHOD(CompileWhenDebugThenDIPresent)
   TEST_METHOD(CompileDebugLines)
@@ -1750,6 +1751,14 @@ bool CompilerTest::InitSupport() {
   if (!m_dllSupport.IsEnabled()) {
     VERIFY_SUCCEEDED(m_dllSupport.Initialize());
     m_ver.Initialize(m_dllSupport);
+    llvm::sys::fs::SetupPerThreadFileSystem();
+  }
+  return true;
+}
+bool CompilerTest::Cleanup() {
+  if (m_dllSupport.IsEnabled()) {
+    m_dllSupport.Cleanup();
+    llvm::sys::fs::CleanupPerThreadFileSystem();
   }
   return true;
 }

+ 18 - 1
tools/clang/unittests/HLSL/DxilModuleTest.cpp

@@ -41,6 +41,9 @@ public:
     TEST_METHOD_PROPERTY(L"Priority", L"0")
   END_TEST_CLASS()
 
+  TEST_CLASS_SETUP(InitSupport);
+  TEST_CLASS_CLEANUP(Cleanup);
+
   dxc::DxcDllSupport m_dllSupport;
 
   // Basic loading tests.
@@ -58,6 +61,21 @@ public:
   TEST_METHOD(Precise7);
 };
 
+bool DxilModuleTest::InitSupport() {
+  if (!m_dllSupport.IsEnabled()) {
+    VERIFY_SUCCEEDED(m_dllSupport.Initialize());
+  }
+  llvm::sys::fs::SetupPerThreadFileSystem();
+  return true;
+}
+bool DxilModuleTest::Cleanup() {
+  if (m_dllSupport.IsEnabled()) {
+    m_dllSupport.Cleanup();
+  }
+  llvm::sys::fs::CleanupPerThreadFileSystem();
+  return true;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // Compilation and dxil module loading support.
 
@@ -69,7 +87,6 @@ public:
     , m_msf(CreateMSFileSystem())
     , m_pts(m_msf.get())
   {
-    VERIFY_SUCCEEDED(m_dllSupport.Initialize());
     m_ver.Initialize(m_dllSupport);
     VERIFY_SUCCEEDED(m_dllSupport.CreateInstance(CLSID_DxcCompiler, &pCompiler));
   }