Skip to content
This repository was archived by the owner on Dec 29, 2022. It is now read-only.

[linux-port] Use cross-platform TLS methods - #228

Merged
ehsannas merged 1 commit into
google:linuxfrom
pow2clk:pow2clk-tls
May 31, 2018
Merged

ehsannas merged 1 commit into
google:linuxfrom
pow2clk:pow2clk-tls

Conversation

@pow2clk

@pow2clk pow2clk commented May 28, 2018

Copy link
Copy Markdown

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

@AppVeyorBot

Copy link
Copy Markdown

@AppVeyorBot

Copy link
Copy Markdown

@AppVeyorBot

Copy link
Copy Markdown

@ehsannas ehsannas left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Comment thread lib/DxcSupport/dxcmem.cpp Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be initialized to something?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@pow2clk

pow2clk commented May 29, 2018

Copy link
Copy Markdown
Author

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.

@ehsannas

Copy link
Copy Markdown

I see. I'm wondering if there's a way to guard against DxcInitThreadMalloc() being called more than once before any DxcCleanupThreadMalloc() is called. Previous asserts would catch that and DXC developers would know about it when running tests.

Maybe we should add a static boolean in the file initialized to false and set to true in DxcInitThreadMalloc() and set back to false in DxcCleanupThreadMalloc (and bring back asserts)? Would you please give that a try and see if it causes any issues?

Thanks

@pow2clk

pow2clk commented May 31, 2018 •

Copy link
Copy Markdown
Author

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.

@AppVeyorBot

Copy link
Copy Markdown

@ehsannas ehsannas left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good stuff! Overall I think this is the right approach for a cross-platform solution. Just a couple of nits pointed out below. Thanks

Comment thread lib/DxcSupport/dxcmem.cpp Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if you can do

delete g_ThreadMallocTls;

instead? that should also invoke the destructor, shouldn't it?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh yes. You're right.

Comment thread lib/DxcSupport/dxcmem.cpp Outdated

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@ehsannas

Copy link
Copy Markdown

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
@AppVeyorBot

Copy link
Copy Markdown

@ehsannas ehsannas left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

@ehsannas
ehsannas merged commit 05e1f52 into google:linux May 31, 2018
@pow2clk
pow2clk deleted the pow2clk-tls branch June 6, 2018 08:31
@pow2clk
pow2clk restored the pow2clk-tls branch June 6, 2018 08:31
@pow2clk
pow2clk deleted the pow2clk-tls branch June 6, 2018 08:31
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants