-
-
Notifications
You must be signed in to change notification settings - Fork 51
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add support for Thread local Storage. #155
Open
aliaspider
wants to merge
1
commit into
devkitPro:master
Choose a base branch
from
aliaspider:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
cmake_minimum_required(VERSION 3.2) | ||
project(tls_test CXX) | ||
include("${DEVKITPRO}/wut/share/wut.cmake" REQUIRED) | ||
|
||
add_executable(tls_test | ||
main.cpp) | ||
|
||
wut_create_rpx(tls_test) | ||
|
||
install(FILES "${CMAKE_CURRENT_BINARY_DIR}/tls_test.rpx" | ||
DESTINATION "${CMAKE_INSTALL_PREFIX}") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include <coreinit/thread.h> | ||
#include <coreinit/time.h> | ||
|
||
#include <whb/proc.h> | ||
#include <whb/log.h> | ||
#include <whb/log_console.h> | ||
|
||
#include <thread> | ||
|
||
__thread | ||
__attribute((tls_model("global-dynamic"))) | ||
const char* thread_name = ""; | ||
|
||
int | ||
thread1_entry(void) | ||
{ | ||
thread_name = "Thread1"; | ||
WHBLogPrintf("Hello from %-20s (thread_name@0x%08X)\n", thread_name, &thread_name); | ||
return 0; | ||
} | ||
|
||
int | ||
main(int argc, char **argv) | ||
{ | ||
WHBProcInit(); | ||
WHBLogConsoleInit(); | ||
|
||
thread_name = "Main Thread"; | ||
WHBLogPrintf("Hello from %-20s (thread_name@0x%08X)\n", thread_name, &thread_name); | ||
|
||
std::thread th1 = std::thread(thread1_entry); | ||
th1.join(); | ||
|
||
WHBLogPrintf("Hello from %-20s (thread_name@0x%08X)\n", thread_name, &thread_name); | ||
WHBLogConsoleDraw(); | ||
|
||
while(WHBProcIsRunning()) { | ||
OSSleepTicks(OSMillisecondsToTicks(1000)); | ||
} | ||
|
||
WHBLogPrintf("Exiting... good bye."); | ||
WHBLogConsoleDraw(); | ||
OSSleepTicks(OSMillisecondsToTicks(1000)); | ||
|
||
WHBLogConsoleFree(); | ||
WHBProcShutdown(); | ||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
%rename link old_link | ||
|
||
*link: | ||
%(old_link) -T %:getenv(DEVKITPRO /wut/share/wut.ld) --gc-sections --emit-relocs -z nocopyreloc %(wut_entry) | ||
%(old_link) -T %:getenv(DEVKITPRO /wut/share/wut.ld) --gc-sections --emit-relocs -z nocopyreloc --no-tls-optimize %(wut_entry) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this required even with
-ftls-model=global-dynamic
?Most C++ code will just be using thread_local so it will be a shame if we cannot support unmodified code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
sadly yes, I couldn't get
gcc
to use compatible relocations, even when compiling with-ftls-model=global-dynamic
. it seems to ignore that flag unless specified directly as a variable attribute.even when using
-msdata=eabi
, which clearely states thatR2
is reservered forsdata2_base
, gcc will just keep emitting code usingR_PPC_TP*
instead ofR_PPC_DTP*
relocations, expectingR2
to hold the Thread Pointer for whatever reason.the only solution right now is to just
#define
thread_local
asthead_local __attribute((tls_model("global-dynamic")))
. it might even work as a compile flag.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suspect the
-ftls-model=global-dynamic
flag might not be passed properly to the compiler. Can you port this example over to standard Makefiles and see if it works that way?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I did test it with a standard markefile and the result was the same. the flag does get passed, gcc just decided that it knows better and optimized it away.
from https://gcc.gnu.org/onlinedocs/gcc/Code-Gen-Options.html
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alright, nice find. At this point I'm wondering if this requires us to patch the compiler to avoid this "optimization" - I believe that would be more preferable to needing to work around the TLS model every single time a TLS variable is used.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that would certainly be preferable, but it might be a bit harder than it looks.
the main problem here seems to be a bug in
gcc
that makes it emit code that contradicts the chosen ABI. as long as it thinks thatR2
has a thread pointer, it will be very hard to convince it to drop the optimizations.who knows, maybe it is even possible to make
gcc
restrict itself toR_PPC_DTPMOD32
andR_PPC_DTPREL32
, which would make patching relocations inelf2rpl
unnecessary.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Adding this patch to the devkitPPC buildscripts forces gcc (14.2.0) to match the compiler flag instead of optimizing the TLS model:
Not sure if there's a better spot for this info but it seems to be one of the last pieces this PR needed so figured here would make it visible enough