Reland "[build, vm] Access TLS with less code."

- Avoid TLS initialization checks by using inline initialization.
 - Avoid global offset table indirection by reducing -fPIC to -fPIE.

out/ReleaseXARM64/exe.stripped/dart_precompiled_runtime
11137992 -> 11274776 (-1.21%)

We still need -fPIC in some places because we build a few shared libraries for FFI, so copy some of Fuchsia's GN setup to use -fPIE or -fPIC as appropriate.

Account for older gcc that does not default to -fpie.

TEST=ci
Bug: https://github.com/dart-lang/sdk/issues/51602
Change-Id: I85072153cb1aef9047c1adbf36c7496fbeb11e10
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/286221
Reviewed-by: Daco Harkes <dacoharkes@google.com>
Commit-Queue: Ryan Macnak <rmacnak@google.com>
This commit is contained in:
Ryan Macnak 2023-03-02 23:07:16 +00:00 committed by Commit Queue
parent 2269ac16db
commit e31c3713bf
9 changed files with 49 additions and 31 deletions

View file

@ -18,3 +18,10 @@ config("product") {
"PRODUCT",
]
}
config("shared_library_config") {
if (current_os == "android" || current_os == "fuchsia" ||
current_os == "linux" || current_os == "mac") {
cflags = [ "-fPIC" ]
}
}

View file

@ -342,7 +342,8 @@ set_defaults("static_library") {
}
# Shared library defaults (also for components in component mode).
_shared_library_configs = _native_compiler_configs
_shared_library_configs =
_native_compiler_configs + [ "//build/config:shared_library_config" ]
if (is_win) {
_shared_library_configs += _windows_linker_configs
} else if (is_mac) {

View file

@ -249,15 +249,9 @@ config("compiler") {
]
}
# We need -fPIC:
# 1. On ARM for tcmalloc.
# 2. On Android.
# 3. When using the sanitizers.
# Otherwise there is a performance hit, in particular on ia32.
if (is_android || is_asan || is_lsan || is_msan || is_tsan || is_ubsan ||
(is_linux && current_cpu != "x86") || is_fuchsia) {
cflags += [ "-fPIC" ]
ldflags += [ "-fPIC" ]
if (is_android || is_linux || is_mac || is_fuchsia) {
cflags += [ "-fPIE" ]
ldflags += [ "-fPIE" ]
}
# Linux-specific compiler flags setup.

View file

@ -1036,9 +1036,6 @@ shared_library("entrypoints_verification_test") {
# The only effect of DART_SHARED_LIB is to export the Dart API.
"DART_SHARED_LIB",
]
if (is_linux || is_android) {
cflags = [ "-fPIC" ]
}
if (is_win) {
# TODO(dartbug.com/40579): This wrongly links in dart.exe on precompiled.
libs = [ "dart.lib" ]
@ -1055,9 +1052,6 @@ shared_library("ffi_test_dynamic_library") {
# The only effect of DART_SHARED_LIB is to export the Dart API.
"DART_SHARED_LIB",
]
if (is_linux || is_android) {
cflags = [ "-fPIC" ]
}
if (is_win) {
# TODO(dartbug.com/40579): This wrongly links in dart.exe on precompiled.
libs = [ "dart.lib" ]
@ -1091,9 +1085,6 @@ shared_library("ffi_test_functions") {
# The only effect of DART_SHARED_LIB is to export the Dart API.
"DART_SHARED_LIB",
]
if (is_linux || is_android) {
cflags = [ "-fPIC" ]
}
if (is_win) {
# TODO(dartbug.com/40579): This wrongly links in dart.exe on precompiled.
libs = [ "dart.lib" ]

View file

@ -21,7 +21,7 @@ ISOLATE_UNIT_TEST_CASE(Disassembler) {
assembler.Stop("testing disassembler");
// Only verify that the disassembler does not crash.
AssemblerTest test("Disassembler", &assembler);
AssemblerTest test("Disassembler", &assembler, Thread::Current()->zone());
test.Assemble();
Disassembler::Disassemble(test.entry(), test.entry() + assembler.CodeSize());
}

View file

@ -20,11 +20,6 @@ OSThread* OSThread::thread_list_head_ = NULL;
Mutex* OSThread::thread_list_lock_ = NULL;
bool OSThread::creation_enabled_ = false;
thread_local ThreadState* OSThread::current_vm_thread_ = NULL;
#if defined(DEBUG)
thread_local bool ThreadInterruptScope::in_thread_interrupt_scope_ = false;
#endif
#if defined(SUPPORT_TIMELINE)
inline void UpdateTimelineTrackMetadata(const OSThread& thread) {
RecorderSynchronizationLockScope ls;

View file

@ -306,7 +306,9 @@ class OSThread : public BaseThread {
static OSThread* thread_list_head_;
static bool creation_enabled_;
static thread_local ThreadState* current_vm_thread_;
// Inline initialization is important for avoiding unnecessary TLS
// initialization checks at each use.
static inline thread_local ThreadState* current_vm_thread_ = nullptr;
friend class IsolateGroup; // to access set_thread(Thread*).
friend class OSThreadIterator;
@ -412,7 +414,7 @@ class ThreadInterruptScope : public ValueObject {
private:
ThreadState* saved_current_vm_thread_;
static thread_local bool in_thread_interrupt_scope_;
static inline thread_local bool in_thread_interrupt_scope_ = false;
#endif // DEBUG
};

View file

@ -107,7 +107,7 @@
if (setjmp(*jump.Set()) == 0) { \
compiler::ObjectPoolBuilder object_pool_builder; \
compiler::Assembler assembler(&object_pool_builder, far_branch_level); \
AssemblerTest test("" #name, &assembler); \
AssemblerTest test("" #name, &assembler, thread->zone()); \
AssemblerTestGenerate##name(test.assembler()); \
test.Assemble(); \
AssemblerTestRun##name(&test); \
@ -504,11 +504,11 @@ struct is_double<double> {
class AssemblerTest {
public:
AssemblerTest(const char* name, compiler::Assembler* assembler)
AssemblerTest(const char* name, compiler::Assembler* assembler, Zone* zone)
: name_(name),
assembler_(assembler),
code_(Code::ZoneHandle()),
disassembly_(Thread::Current()->zone()->Alloc<char>(DISASSEMBLY_SIZE)) {
code_(Code::ZoneHandle(zone)),
disassembly_(zone->Alloc<char>(DISASSEMBLY_SIZE)) {
ASSERT(name != NULL);
ASSERT(assembler != NULL);
}

View file

@ -0,0 +1,28 @@
// Copyright (c) 2023, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
import "dart:io";
// Mmmm... pie!
main() {
if (!Platform.isLinux) return; // readelf is a Linux tool.
// Modern Mac and Android binaries are always PIE.
// Fuchsia binaries are always PIE.
var result = Process.runSync("readelf", ["-h", Platform.resolvedExecutable]);
print("stdout:");
print(result.stdout);
print("stderr:");
print(result.stderr);
if (result.exitCode != 0) {
throw "readelf failed";
}
// A position-dependent executable outputs "EXEC (Executable file)".
if (!result.stdout.contains("DYN (Position-Independent Executable file)") &&
!result.stdout.contains("DYN (Shared object file)")) {
throw "Standalone VM should be a position-independent executable";
}
}