* Moves the implementation of DxcThreadMalloc to dxcmem.cpp; removes DxcSwapThreadMalloc

* Remove DxcSetThreadMalloc(OrDefault) -- they were always been used for installing the default allocator

* Deletes copy and move ctors and assignment

* stores the allocator in the TLS slot.

* DxcSwapThreadMalloc should be able to install a null allocator

* Marks the DxcThreadMalloc members as hidden (linux only)
This commit is contained in:
John Porto 2019-05-20 20:52:10 -07:00 коммит произвёл GitHub
Родитель c9aa79b127
Коммит c9b9676c5d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
10 изменённых файлов: 60 добавлений и 48 удалений

Просмотреть файл

@ -48,26 +48,34 @@ void DxcCleanupThreadMalloc() throw();
// Used by APIs entry points to set up per-thread/invocation allocator. // Used by APIs entry points to set up per-thread/invocation allocator.
// Setting the IMalloc on the thread increases the reference count, // Setting the IMalloc on the thread increases the reference count,
// clearing it decreases it. // clearing it decreases it.
void DxcSetThreadMallocToDefault() throw();
void DxcClearThreadMalloc() throw(); void DxcClearThreadMalloc() throw();
void DxcSetThreadMalloc(IMalloc *pMalloc) throw();
void DxcSetThreadMallocOrDefault(IMalloc *pMalloc) throw();
// Swapping does not AddRef or Release new or prior. The pattern is to keep both alive,
// either in TLS, or on the stack to restore later. The returned value is the effective
// IMalloc also available in TLS.
IMalloc *DxcSwapThreadMalloc(IMalloc *pMalloc, IMalloc **ppPrior) throw();
IMalloc *DxcSwapThreadMallocOrDefault(IMalloc *pMalloc, IMalloc **ppPrior) throw();
// Used to retrieve the current invocation's allocator or perform an alloc/free/realloc. // Used to retrieve the current invocation's allocator or perform an alloc/free/realloc.
IMalloc *DxcGetThreadMallocNoRef() throw(); IMalloc *DxcGetThreadMallocNoRef() throw();
struct DxcThreadMalloc { #if defined(LLVM_ON_UNIX)
DxcThreadMalloc(IMalloc *pMallocOrNull) throw() { #define DXC_HIDDEN_LINKAGE __attribute__(( visibility("hidden") ))
p = DxcSwapThreadMallocOrDefault(pMallocOrNull, &pPrior); #else // LLVM_ON_UNIX
} #define DXC_HIDDEN_LINKAGE
~DxcThreadMalloc() { #endif // LLVM_ON_UNIX
DxcSwapThreadMalloc(pPrior, nullptr);
} class DxcThreadMalloc {
public:
explicit DXC_HIDDEN_LINKAGE DxcThreadMalloc(IMalloc *pMallocOrNull) throw();
DXC_HIDDEN_LINKAGE ~DxcThreadMalloc();
IMalloc *GetInstalledAllocator() const { return p; }
private:
// Copy constructor and assignment are dangerous and should always be
// deleted...
DxcThreadMalloc(const DxcThreadMalloc &) = delete;
DxcThreadMalloc &operator =(const DxcThreadMalloc &) = delete;
// Move constructor and assignment should be OK to be added if needed.
DxcThreadMalloc(DxcThreadMalloc &&) = delete;
DxcThreadMalloc &operator =(DxcThreadMalloc &&) = delete;
IMalloc *p; IMalloc *p;
IMalloc *pPrior; IMalloc *pPrior;
}; };

Просмотреть файл

@ -66,16 +66,15 @@ void DxcClearThreadMalloc() throw() {
g_ThreadMallocTls->erase(); g_ThreadMallocTls->erase();
pMalloc->Release(); pMalloc->Release();
} }
void DxcSetThreadMalloc(IMalloc *pMalloc) throw() {
void DxcSetThreadMallocToDefault() throw() {
DXASSERT(g_ThreadMallocTls != nullptr, "else prior to DxcInitThreadMalloc or after DxcCleanupThreadMalloc"); DXASSERT(g_ThreadMallocTls != nullptr, "else prior to DxcInitThreadMalloc or after DxcCleanupThreadMalloc");
DXASSERT(DxcGetThreadMallocNoRef() == nullptr, "else nested allocation invoked"); DXASSERT(DxcGetThreadMallocNoRef() == nullptr, "else nested allocation invoked");
g_ThreadMallocTls->set(pMalloc); g_ThreadMallocTls->set(g_pDefaultMalloc);
pMalloc->AddRef(); g_pDefaultMalloc->AddRef();
} }
void DxcSetThreadMallocOrDefault(IMalloc *pMalloc) throw() {
DxcSetThreadMalloc(pMalloc ? pMalloc : g_pDefaultMalloc); static IMalloc *DxcSwapThreadMalloc(IMalloc *pMalloc, IMalloc **ppPrior) throw() {
}
IMalloc *DxcSwapThreadMalloc(IMalloc *pMalloc, IMalloc **ppPrior) throw() {
DXASSERT(g_ThreadMallocTls != nullptr, "else prior to DxcInitThreadMalloc or after DxcCleanupThreadMalloc"); DXASSERT(g_ThreadMallocTls != nullptr, "else prior to DxcInitThreadMalloc or after DxcCleanupThreadMalloc");
IMalloc *pPrior = DxcGetThreadMallocNoRef(); IMalloc *pPrior = DxcGetThreadMallocNoRef();
if (ppPrior) { if (ppPrior) {
@ -84,8 +83,13 @@ IMalloc *DxcSwapThreadMalloc(IMalloc *pMalloc, IMalloc **ppPrior) throw() {
g_ThreadMallocTls->set(pMalloc); g_ThreadMallocTls->set(pMalloc);
return pMalloc; return pMalloc;
} }
IMalloc *DxcSwapThreadMallocOrDefault(IMalloc *pMallocOrNull, IMalloc **ppPrior) throw() {
return DxcSwapThreadMalloc(pMallocOrNull ? pMallocOrNull : g_pDefaultMalloc, ppPrior); DXC_HIDDEN_LINKAGE DxcThreadMalloc::DxcThreadMalloc(IMalloc *pMallocOrNull) throw() {
p = DxcSwapThreadMalloc(pMallocOrNull ? pMallocOrNull : g_pDefaultMalloc, &pPrior);
}
DXC_HIDDEN_LINKAGE DxcThreadMalloc::~DxcThreadMalloc() {
DxcSwapThreadMalloc(pPrior, nullptr);
} }
#ifndef _WIN32 #ifndef _WIN32

Просмотреть файл

@ -1095,7 +1095,7 @@ int dxc::main(int argc, const char **argv_) {
const char *pStage = "Operation"; const char *pStage = "Operation";
int retVal = 0; int retVal = 0;
if (FAILED(DxcInitThreadMalloc())) return 1; if (FAILED(DxcInitThreadMalloc())) return 1;
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
try { try {
pStage = "Argument processing"; pStage = "Argument processing";
if (initHlslOptTable()) throw std::bad_alloc(); if (initHlslOptTable()) throw std::bad_alloc();

Просмотреть файл

@ -51,7 +51,7 @@ static HRESULT InitMaybeFail() throw() {
HRESULT hr; HRESULT hr;
bool fsSetup = false, memSetup = false; bool fsSetup = false, memSetup = false;
IFC(DxcInitThreadMalloc()); IFC(DxcInitThreadMalloc());
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
memSetup = true; memSetup = true;
if (::llvm::sys::fs::SetupPerThreadFileSystem()) { if (::llvm::sys::fs::SetupPerThreadFileSystem()) {
hr = E_FAIL; hr = E_FAIL;
@ -86,7 +86,7 @@ HRESULT __attribute__ ((constructor)) DllMain() {
} }
void __attribute__ ((destructor)) DllShutdown() { void __attribute__ ((destructor)) DllShutdown() {
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
::hlsl::options::cleanupHlslOptTable(); ::hlsl::options::cleanupHlslOptTable();
::llvm::sys::fs::CleanupPerThreadFileSystem(); ::llvm::sys::fs::CleanupPerThreadFileSystem();
::llvm::llvm_shutdown(); ::llvm::llvm_shutdown();
@ -105,7 +105,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID reserved) {
result = SUCCEEDED(hr) ? TRUE : FALSE; result = SUCCEEDED(hr) ? TRUE : FALSE;
} else if (Reason == DLL_PROCESS_DETACH) { } else if (Reason == DLL_PROCESS_DETACH) {
DxcEtw_DXCompilerShutdown_Start(); DxcEtw_DXCompilerShutdown_Start();
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
::hlsl::options::cleanupHlslOptTable(); ::hlsl::options::cleanupHlslOptTable();
::llvm::sys::fs::CleanupPerThreadFileSystem(); ::llvm::sys::fs::CleanupPerThreadFileSystem();
::llvm::llvm_shutdown(); ::llvm::llvm_shutdown();

Просмотреть файл

@ -101,7 +101,7 @@ HRESULT STDMETHODCALLTYPE DxcAssembler::AssembleToContainer(
parseIR(memBuf->getMemBufferRef(), Err, Context); parseIR(memBuf->getMemBufferRef(), Err, Context);
CComPtr<AbstractMemoryStream> pOutputStream; CComPtr<AbstractMemoryStream> pOutputStream;
IFT(CreateMemoryStream(TM.p, &pOutputStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pOutputStream));
raw_stream_ostream outStream(pOutputStream.p); raw_stream_ostream outStream(pOutputStream.p);
// Check for success. // Check for success.
@ -148,7 +148,7 @@ HRESULT STDMETHODCALLTYPE DxcAssembler::AssembleToContainer(
flags |= SerializeDxilFlags::DebugNameDependOnSource; flags |= SerializeDxilFlags::DebugNameDependOnSource;
} }
dxcutil::AssembleToContainer(std::move(M), pResultBlob, dxcutil::AssembleToContainer(std::move(M), pResultBlob,
TM.p, flags, TM.GetInstalledAllocator(), flags,
pOutputStream); pOutputStream);
IFT(DxcOperationResult::CreateFromResultErrorStatus(pResultBlob, nullptr, S_OK, ppResult)); IFT(DxcOperationResult::CreateFromResultErrorStatus(pResultBlob, nullptr, S_OK, ppResult));

Просмотреть файл

@ -124,7 +124,7 @@ int __cdecl main(int argc, _In_reads_z_(argc) char **argv) {
return 1; return 1;
llvm::sys::fs::AutoCleanupPerThreadFileSystem auto_cleanup_fs; llvm::sys::fs::AutoCleanupPerThreadFileSystem auto_cleanup_fs;
if (FAILED(DxcInitThreadMalloc())) return 1; if (FAILED(DxcInitThreadMalloc())) return 1;
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
try { try {
llvm::sys::fs::MSFileSystem *msfPtr; llvm::sys::fs::MSFileSystem *msfPtr;
IFT(CreateMSFileSystemForDisk(&msfPtr)); IFT(CreateMSFileSystemForDisk(&msfPtr));

Просмотреть файл

@ -45,7 +45,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) {
DisableThreadLibraryCalls(hinstDLL); DisableThreadLibraryCalls(hinstDLL);
DxcInitThreadMalloc(); DxcInitThreadMalloc();
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
if (hlsl::options::initHlslOptTable()) { if (hlsl::options::initHlslOptTable()) {
DxcClearThreadMalloc(); DxcClearThreadMalloc();
@ -55,7 +55,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID) {
return TRUE; return TRUE;
} }
} else if (Reason == DLL_PROCESS_DETACH) { } else if (Reason == DLL_PROCESS_DETACH) {
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
libshare::LibCacheManager::ReleaseLibCacheManager(); libshare::LibCacheManager::ReleaseLibCacheManager();
::hlsl::options::cleanupHlslOptTable(); ::hlsl::options::cleanupHlslOptTable();
DxcClearThreadMalloc(); DxcClearThreadMalloc();

Просмотреть файл

@ -44,7 +44,7 @@ static HRESULT InitMaybeFail() throw() {
HRESULT hr; HRESULT hr;
bool fsSetup = false, memSetup = false; bool fsSetup = false, memSetup = false;
IFC(DxcInitThreadMalloc()); IFC(DxcInitThreadMalloc());
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
memSetup = true; memSetup = true;
if (::llvm::sys::fs::SetupPerThreadFileSystem()) { if (::llvm::sys::fs::SetupPerThreadFileSystem()) {
hr = E_FAIL; hr = E_FAIL;
@ -84,7 +84,7 @@ BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD Reason, LPVOID reserved) {
result = SUCCEEDED(hr) ? TRUE : FALSE; result = SUCCEEDED(hr) ? TRUE : FALSE;
} else if (Reason == DLL_PROCESS_DETACH) { } else if (Reason == DLL_PROCESS_DETACH) {
DxcEtw_DXCompilerShutdown_Start(); DxcEtw_DXCompilerShutdown_Start();
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
::hlsl::options::cleanupHlslOptTable(); ::hlsl::options::cleanupHlslOptTable();
::llvm::sys::fs::CleanupPerThreadFileSystem(); ::llvm::sys::fs::CleanupPerThreadFileSystem();
::llvm::llvm_shutdown(); ::llvm::llvm_shutdown();

Просмотреть файл

@ -251,7 +251,7 @@ HRESULT STDMETHODCALLTYPE DxcDxrFallbackCompiler::RenameAndLink(
// Create a diagnostic printer // Create a diagnostic printer
CComPtr<AbstractMemoryStream> pDiagStream; CComPtr<AbstractMemoryStream> pDiagStream;
IFT(CreateMemoryStream(TM.p, &pDiagStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pDiagStream));
raw_stream_ostream DiagStream(pDiagStream); raw_stream_ostream DiagStream(pDiagStream);
DiagnosticPrinterRawOStream DiagPrinter(DiagStream); DiagnosticPrinterRawOStream DiagPrinter(DiagStream);
PrintDiagnosticContext DiagContext(DiagPrinter); PrintDiagnosticContext DiagContext(DiagPrinter);
@ -318,14 +318,14 @@ HRESULT STDMETHODCALLTYPE DxcDxrFallbackCompiler::RenameAndLink(
if (M) if (M)
{ {
CComPtr<AbstractMemoryStream> pOutputStream; CComPtr<AbstractMemoryStream> pOutputStream;
IFT(CreateMemoryStream(TM.p, &pOutputStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pOutputStream));
raw_stream_ostream outStream(pOutputStream.p); raw_stream_ostream outStream(pOutputStream.p);
WriteBitcodeToFile(M.get(), outStream); WriteBitcodeToFile(M.get(), outStream);
outStream.flush(); outStream.flush();
// Validation. // Validation.
dxcutil::AssembleToContainer( dxcutil::AssembleToContainer(
std::move(M), pResultBlob, TM.p, SerializeDxilFlags::None, std::move(M), pResultBlob, TM.GetInstalledAllocator(), SerializeDxilFlags::None,
pOutputStream pOutputStream
#if !DISABLE_GET_CUSTOM_DIAG_ID #if !DISABLE_GET_CUSTOM_DIAG_ID
, Diag , Diag
@ -371,7 +371,7 @@ HRESULT STDMETHODCALLTYPE DxcDxrFallbackCompiler::PatchShaderBindingTables(
// Create a diagnostic printer // Create a diagnostic printer
CComPtr<AbstractMemoryStream> pDiagStream; CComPtr<AbstractMemoryStream> pDiagStream;
IFT(CreateMemoryStream(TM.p, &pDiagStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pDiagStream));
raw_stream_ostream DiagStream(pDiagStream); raw_stream_ostream DiagStream(pDiagStream);
DiagnosticPrinterRawOStream DiagPrinter(DiagStream); DiagnosticPrinterRawOStream DiagPrinter(DiagStream);
PrintDiagnosticContext DiagContext(DiagPrinter); PrintDiagnosticContext DiagContext(DiagPrinter);
@ -404,14 +404,14 @@ HRESULT STDMETHODCALLTYPE DxcDxrFallbackCompiler::PatchShaderBindingTables(
if (M) if (M)
{ {
CComPtr<AbstractMemoryStream> pOutputStream; CComPtr<AbstractMemoryStream> pOutputStream;
IFT(CreateMemoryStream(TM.p, &pOutputStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pOutputStream));
raw_stream_ostream outStream(pOutputStream.p); raw_stream_ostream outStream(pOutputStream.p);
WriteBitcodeToFile(M.get(), outStream); WriteBitcodeToFile(M.get(), outStream);
outStream.flush(); outStream.flush();
dxcutil::AssembleToContainer( dxcutil::AssembleToContainer(
std::move(M), std::move(M),
pResultBlob, pResultBlob,
TM.p, TM.GetInstalledAllocator(),
SerializeDxilFlags::None, SerializeDxilFlags::None,
pOutputStream); pOutputStream);
} }
@ -459,7 +459,7 @@ HRESULT STDMETHODCALLTYPE DxcDxrFallbackCompiler::Link(
// Create a diagnostic printer // Create a diagnostic printer
CComPtr<AbstractMemoryStream> pDiagStream; CComPtr<AbstractMemoryStream> pDiagStream;
IFT(CreateMemoryStream(TM.p, &pDiagStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pDiagStream));
raw_stream_ostream DiagStream(pDiagStream); raw_stream_ostream DiagStream(pDiagStream);
DiagnosticPrinterRawOStream DiagPrinter(DiagStream); DiagnosticPrinterRawOStream DiagPrinter(DiagStream);
PrintDiagnosticContext DiagContext(DiagPrinter); PrintDiagnosticContext DiagContext(DiagPrinter);
@ -558,14 +558,14 @@ HRESULT STDMETHODCALLTYPE DxcDxrFallbackCompiler::Link(
if (M) if (M)
{ {
CComPtr<AbstractMemoryStream> pOutputStream; CComPtr<AbstractMemoryStream> pOutputStream;
IFT(CreateMemoryStream(TM.p, &pOutputStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pOutputStream));
raw_stream_ostream outStream(pOutputStream.p); raw_stream_ostream outStream(pOutputStream.p);
WriteBitcodeToFile(M.get(), outStream); WriteBitcodeToFile(M.get(), outStream);
outStream.flush(); outStream.flush();
// Validation. // Validation.
HRESULT valHR = dxcutil::ValidateAndAssembleToContainer( HRESULT valHR = dxcutil::ValidateAndAssembleToContainer(
std::move(M), pResultBlob, TM.p, SerializeDxilFlags::None, std::move(M), pResultBlob, TM.GetInstalledAllocator(), SerializeDxilFlags::None,
pOutputStream, pOutputStream,
/*bDebugInfo*/ false /*bDebugInfo*/ false
#if !DISABLE_GET_CUSTOM_DIAG_ID #if !DISABLE_GET_CUSTOM_DIAG_ID
@ -625,7 +625,7 @@ HRESULT STDMETHODCALLTYPE DxcDxrFallbackCompiler::Compile(
// Create a diagnostic printer // Create a diagnostic printer
CComPtr<AbstractMemoryStream> pDiagStream; CComPtr<AbstractMemoryStream> pDiagStream;
IFT(CreateMemoryStream(TM.p, &pDiagStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pDiagStream));
raw_stream_ostream DiagStream(pDiagStream); raw_stream_ostream DiagStream(pDiagStream);
DiagnosticPrinterRawOStream DiagPrinter(DiagStream); DiagnosticPrinterRawOStream DiagPrinter(DiagStream);
PrintDiagnosticContext DiagContext(DiagPrinter); PrintDiagnosticContext DiagContext(DiagPrinter);
@ -719,14 +719,14 @@ HRESULT STDMETHODCALLTYPE DxcDxrFallbackCompiler::Compile(
if (M) if (M)
{ {
CComPtr<AbstractMemoryStream> pOutputStream; CComPtr<AbstractMemoryStream> pOutputStream;
IFT(CreateMemoryStream(TM.p, &pOutputStream)); IFT(CreateMemoryStream(TM.GetInstalledAllocator(), &pOutputStream));
raw_stream_ostream outStream(pOutputStream.p); raw_stream_ostream outStream(pOutputStream.p);
WriteBitcodeToFile(M.get(), outStream); WriteBitcodeToFile(M.get(), outStream);
outStream.flush(); outStream.flush();
dxcutil::AssembleToContainer( dxcutil::AssembleToContainer(
std::move(M), std::move(M),
pResultBlob, pResultBlob,
TM.p, TM.GetInstalledAllocator(),
SerializeDxilFlags::None, SerializeDxilFlags::None,
pOutputStream); pOutputStream);
} }

Просмотреть файл

@ -31,7 +31,7 @@ bool TestModuleSetup() {
return false; return false;
if (FAILED(DxcInitThreadMalloc())) if (FAILED(DxcInitThreadMalloc()))
return false; return false;
DxcSetThreadMallocOrDefault(nullptr); DxcSetThreadMallocToDefault();
if (hlsl::options::initHlslOptTable()) { if (hlsl::options::initHlslOptTable()) {
return false; return false;