Repository navigation
[linux-port] Use cross-platform TLS methods - #228
Conversation
|
❌ Build DirectXShaderCompiler 1.0.113 failed (commit 3f2026c1aa by @pow2clk) |
|
❌ Build DirectXShaderCompiler 1.0.141 failed (commit b574e45829 by @pow2clk) |
|
✅ Build DirectXShaderCompiler 1.0.142 completed (commit 9797ccf12a by @pow2clk) |
ehsannas
left a comment
There was a problem hiding this comment.
Overall this is a great change. I wish I knew about llvm::sys::ThreadLocal before! :)
Just one question:
the original implementation initializes the static g_ThreadMallocTlsIndex to zero, then in the init function it asserts that g_ThreadMallocTlsIndex is zero, and then assigns something else to it. In the cleanup function, asserts that it is non-zero, deletes, then sets it back to zero.
Can we bring back these assertions? or are these assertions no longer applicable?
There was a problem hiding this comment.
should this be initialized to something?
There was a problem hiding this comment.
The constructor doesn't take any parameters. The actual class doesn't have any members. I don't think there's any way to initialize it. more than the default constructor does.
|
The assertions are no longer applicable because the object becomes valid and ready to go when it is initialized well before the init function is called because it's a global static variable. It is initialized when the binary, library or executable, is loaded. The ThreadLocal implementation explicitly mentions that decisions were made in its design to allow this. Initially, I'd made it a pointer, but it failed because to allocate itself required itself to be initialized on windows due to the global operator new function I mentioned. So I used placement new, which worked for the most part, but failed on a few spirv on windows tests. So this is what I'm left with. There's a good chance I might revisit this because there are a couple other issues that run into this, notably the cirtical section replacement I'm still working on. It is not set up for global static initialization. |
|
I see. I'm wondering if there's a way to guard against Maybe we should add a static boolean in the file initialized to false and set to true in Thanks |
|
Well rather than add another static to restore the check that was removed in a previous change, I revisited the placement new approach I took originally and found the bug. I failed to explicitly call the destructor. This caused TLS indices to be leaked and the spirv test managed to exhaust them. With the new() allocation, the repeated init checks as well as the uninitialized usage can be restored, plus the uncontrollable nature of static initialization order is avoided. |
|
✅ Build DirectXShaderCompiler 1.0.153 completed (commit 37d21b13a4 by @pow2clk) |
ehsannas
left a comment
There was a problem hiding this comment.
Good stuff! Overall I think this is the right approach for a cross-platform solution. Just a couple of nits pointed out below. Thanks
There was a problem hiding this comment.
I wonder if you can do
delete g_ThreadMallocTls;instead? that should also invoke the destructor, shouldn't it?
There was a problem hiding this comment.
As you pointed out placement new doesnt really allocate anything. Because of this, it shouldn't be freed using free. Calling the destructor explicitly and then freeing in a way corresponding to the original allocation is the established way to do this and what the compiler expects. There's another example of this in microcom.h.
There was a problem hiding this comment.
Maybe we should handle the out-of-memory case / exception like the existing code does...
IIUC, placement new (line 48) does not actually allocate any memory, so it will not throw an exception. So the out-of-memory case would manifest itself when the return value of Alloc is nullptr...
So, something along these lines:
if (g_ThreadMallocTls == nullptr) {
g_pDefaultMalloc->Release();
g_pDefaultMalloc = nullptr;
return E_OUTOFMEMORY;
}what do you think?
There was a problem hiding this comment.
Placement new can't result in null. Internally, ThreadLocal asserts for the extremely rare case of running out of tls keys. This is the only place we use them, so we shouldn't ever need more than one. I encountered the problem because of a bug. It shouldn't happen in practice.
I could add handling of the allocation failing due to running out of memory though.
|
happy to see the assertions are back :) |
LLVM includes cross-platform thread local storage methods for saving, retrieving, and deleting thread-specific objects. Using these methods is superior to the thread_local storage qualifier because the former works on Apple, Linux, and Windows. It also uses the same mechanism as the previous Windows implementation used without compiler idiosyncracies. Placement new is needed in DxcInitThreadMalloc because operator new requires the TLS malloc to be in place. Fixes google#173
|
✅ Build DirectXShaderCompiler 1.0.156 completed (commit ffd870517d by @pow2clk) |
LLVM includes cross-platform thread local storage methods for
saving, retrieving, and deleting thread-specific objects. Using
these methods is superior to the thread_local storage qualifier
because the former works on Apple, Linux, and Windows. It also
uses the same mechanism as the previous Windows implementation
used without compiler idiosyncracies.
Placement new is needed in DxcInitThreadMalloc because operator
new requires the TLS malloc to be in place.
Fixes #173