Skip to content

Commit 6b72a9c

Browse files
aamCommit Queue
authored andcommitted
Reapply "[vm] Add graph intrinsics for ThreadLocal hasValue and getValue methods."
This reverts commit 4d7467a. The fix for the failure that caused revert is in patchset 2. TEST=ci Change-Id: I9b7ff0dd049062b086ad43c8368c6ede130edf35 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/465781 Commit-Queue: Alexander Aprelev <[email protected]> Reviewed-by: Slava Egorov <[email protected]>
1 parent 9ac53f7 commit 6b72a9c

File tree

13 files changed

+3540
-3422
lines changed

13 files changed

+3540
-3422
lines changed

runtime/lib/isolate.cc

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1279,79 +1279,70 @@ DEFINE_NATIVE_ENTRY(Isolate_sendOOB, 0, 2) {
12791279
}
12801280

12811281
static void EnsureThreadLocalsTableExistsAndBigEnough(Thread* thread,
1282-
intptr_t index) {
1283-
GrowableObjectArray& locals =
1284-
GrowableObjectArray::Handle(thread->thread_locals());
1285-
if (locals.IsNull()) {
1286-
locals = GrowableObjectArray::New();
1287-
thread->set_thread_locals(locals);
1288-
}
1289-
if (index >= locals.Length()) {
1290-
locals.Grow(index + 1);
1291-
intptr_t old_length = locals.Length();
1292-
locals.SetLength(locals.Capacity());
1293-
for (intptr_t i = old_length; i < locals.Capacity(); i++) {
1294-
locals.SetAt(i, Object::sentinel());
1282+
intptr_t id) {
1283+
Array& locals = Array::Handle(thread->thread_locals());
1284+
if (id >= locals.Length()) {
1285+
intptr_t new_length = id + 1;
1286+
const Array& new_array =
1287+
Array::Handle(Array::Grow(locals, new_length, Heap::kOld));
1288+
for (intptr_t i = locals.Length(); i < new_length; i++) {
1289+
new_array.SetAt(i, Object::sentinel());
12951290
}
1291+
thread->set_thread_locals(new_array);
12961292
}
12971293
}
12981294

1299-
DEFINE_NATIVE_ENTRY(ScopedThreadLocal_allocateId, 0, 0) {
1295+
DEFINE_NATIVE_ENTRY(ThreadLocal_allocateId, 0, 0) {
13001296
auto isolate_group = thread->isolate_group();
1301-
isolate_group->increment_scoped_thread_locals_count();
1302-
intptr_t new_index = isolate_group->scoped_thread_locals_count() - 1;
1297+
isolate_group->increment_thread_locals_count();
1298+
intptr_t new_index = isolate_group->thread_locals_count() - 1;
13031299
EnsureThreadLocalsTableExistsAndBigEnough(thread, new_index);
1304-
GrowableObjectArray& locals =
1305-
GrowableObjectArray::Handle(thread->thread_locals());
1300+
Array& locals = Array::Handle(thread->thread_locals());
13061301
locals.SetAt(new_index, Object::sentinel());
13071302
return Integer::New(new_index);
13081303
}
13091304

13101305
static void ValidateScopedThreadLocalId(Thread* thread, intptr_t id) {
1311-
if (id < 0 || id >= thread->isolate_group()->scoped_thread_locals_count()) {
1306+
if (id < 0 || id >= thread->isolate_group()->thread_locals_count()) {
13121307
const String& msg = String::Handle(String::New("Invalid local id."));
13131308
Exceptions::ThrowStateError(msg);
13141309
UNREACHABLE();
13151310
}
13161311
EnsureThreadLocalsTableExistsAndBigEnough(thread, id);
13171312
}
13181313

1319-
DEFINE_NATIVE_ENTRY(ScopedThreadLocal_hasValue, 0, 1) {
1314+
DEFINE_NATIVE_ENTRY(ThreadLocal_hasValue, 0, 1) {
13201315
GET_NON_NULL_NATIVE_ARGUMENT(Integer, id_obj, arguments->NativeArgAt(0));
13211316
intptr_t id = id_obj.Value();
13221317
ValidateScopedThreadLocalId(thread, id);
1323-
GrowableObjectArray& locals =
1324-
GrowableObjectArray::Handle(thread->thread_locals());
1318+
Array& locals = Array::Handle(thread->thread_locals());
13251319
return locals.At(id) == Object::sentinel().ptr() ? Bool::False().ptr()
13261320
: Bool::True().ptr();
13271321
}
13281322

1329-
DEFINE_NATIVE_ENTRY(ScopedThreadLocal_getValue, 0, 1) {
1323+
DEFINE_NATIVE_ENTRY(ThreadLocal_getValue, 0, 1) {
13301324
GET_NON_NULL_NATIVE_ARGUMENT(Integer, id_obj, arguments->NativeArgAt(0));
13311325
intptr_t id = id_obj.Value();
13321326
ValidateScopedThreadLocalId(thread, id);
1333-
GrowableObjectArray& locals =
1334-
GrowableObjectArray::Handle(thread->thread_locals());
1327+
Array& locals = Array::Handle(thread->thread_locals());
13351328
return locals.At(id);
13361329
}
13371330

1338-
DEFINE_NATIVE_ENTRY(ScopedThreadLocal_setValue, 0, 2) {
1331+
DEFINE_NATIVE_ENTRY(ThreadLocal_setValue, 0, 2) {
13391332
GET_NON_NULL_NATIVE_ARGUMENT(Integer, id_obj, arguments->NativeArgAt(0));
13401333
intptr_t id = id_obj.Value();
13411334
ValidateScopedThreadLocalId(thread, id);
13421335
GET_NON_NULL_NATIVE_ARGUMENT(Instance, value, arguments->NativeArgAt(1));
1343-
GrowableObjectArray& locals =
1344-
GrowableObjectArray::Handle(thread->thread_locals());
1336+
Array& locals = Array::Handle(thread->thread_locals());
13451337
locals.SetAt(id, value);
13461338
return Object::null();
13471339
}
13481340

1349-
DEFINE_NATIVE_ENTRY(ScopedThreadLocal_clearValue, 0, 1) {
1341+
DEFINE_NATIVE_ENTRY(ThreadLocal_clearValue, 0, 1) {
13501342
GET_NON_NULL_NATIVE_ARGUMENT(Integer, id_obj, arguments->NativeArgAt(0));
13511343
intptr_t id = id_obj.Value();
13521344
ValidateScopedThreadLocalId(thread, id);
1353-
GrowableObjectArray& locals =
1354-
GrowableObjectArray::Handle(thread->thread_locals());
1345+
Array& locals = Array::Handle(thread->thread_locals());
13551346
locals.SetAt(id, Object::sentinel());
13561347
return Object::null();
13571348
}

runtime/vm/bootstrap_natives.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -281,11 +281,11 @@ namespace dart {
281281
V(Internal_allocateObjectInstructionsEnd, 0) \
282282
V(InvocationMirror_unpackTypeArguments, 2) \
283283
V(NoSuchMethodError_existingMethodSignature, 3) \
284-
V(ScopedThreadLocal_allocateId, 0) \
285-
V(ScopedThreadLocal_clearValue, 1) \
286-
V(ScopedThreadLocal_getValue, 1) \
287-
V(ScopedThreadLocal_hasValue, 1) \
288-
V(ScopedThreadLocal_setValue, 2) \
284+
V(ThreadLocal_allocateId, 0) \
285+
V(ThreadLocal_clearValue, 1) \
286+
V(ThreadLocal_getValue, 1) \
287+
V(ThreadLocal_hasValue, 1) \
288+
V(ThreadLocal_setValue, 2) \
289289
V(Uri_isWindowsPlatform, 0) \
290290
V(UserTag_new, 2) \
291291
V(UserTag_label, 1) \

runtime/vm/compiler/backend/il.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10763,7 +10763,7 @@ class GenericCheckBoundInstr : public CheckBoundBaseInstr {
1076310763

1076410764
// Phantom checks serve as dependencies inhibiting illegal code motion but
1076510765
// are removed before code generation. Phantom checks are inserted due to
10766-
// unsafe annotations. An early-phaee path-sensitive bounds check removal
10766+
// unsafe annotations. An early-phase path-sensitive bounds check removal
1076710767
// optimization can be implemented by replacing a real check with a phantom
1076810768
// check.
1076910769
kPhantom

runtime/vm/compiler/backend/slot.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,8 @@ class ParsedFunction;
233233
V(ObjectStore, _, record_field_names, Array, VAR) \
234234
V(PersistentHandle, _, ptr, Dynamic, VAR) \
235235
V(Thread, _, current_tag, UserTag, VAR) \
236-
V(Thread, _, default_tag, UserTag, VAR)
236+
V(Thread, _, default_tag, UserTag, VAR) \
237+
V(Thread, _, thread_locals, Array, VAR)
237238

238239
// List of slots that correspond to fields of non-Dart objects containing
239240
// unboxed values in the following format:

runtime/vm/compiler/graph_intrinsifier.cc

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -721,5 +721,54 @@ bool GraphIntrinsifier::Build_DoubleFlipSignBit(FlowGraph* flow_graph) {
721721
return true;
722722
}
723723

724+
static Definition* GetThreadLocalValue(FlowGraph* flow_graph,
725+
BlockBuilder* builder) {
726+
Definition* thread = builder->AddDefinition(new LoadThreadInstr());
727+
Definition* array = builder->AddDefinition(
728+
new LoadFieldInstr(new Value(thread),
729+
/*slot=*/Slot::Thread_thread_locals(),
730+
InnerPointerAccess::kNotUntagged, builder->Source(),
731+
/*calls_initializer=*/false, DeoptId::kNone,
732+
compiler::Assembler::kRelaxedNonAtomic));
733+
Definition* index = builder->AddParameter(0);
734+
735+
Definition* safe_index =
736+
PrepareIndexedOp(flow_graph, builder, array, index, Slot::Array_length());
737+
738+
Definition* local = builder->AddDefinition(new LoadIndexedInstr(
739+
new Value(array), new Value(safe_index), /*index_unboxed=*/false,
740+
/*index_scale=*/target::Instance::ElementSizeFor(kArrayCid), kArrayCid,
741+
kAlignedAccess, DeoptId::kNone, builder->Source(),
742+
new CompileType(CompileType::FromAbstractType(
743+
Type::ZoneHandle(Type::ObjectType()), CompileType::kCanBeNull,
744+
CompileType::kCanBeSentinel))));
745+
return local;
746+
}
747+
748+
bool GraphIntrinsifier::Build_ThreadLocalGetValue(FlowGraph* flow_graph) {
749+
GraphEntryInstr* graph_entry = flow_graph->graph_entry();
750+
auto normal_entry = graph_entry->normal_entry();
751+
BlockBuilder builder(flow_graph, normal_entry, /*with_frame=*/false);
752+
Definition* local = GetThreadLocalValue(flow_graph, &builder);
753+
754+
builder.AddReturn(new Value(local));
755+
return true;
756+
}
757+
758+
bool GraphIntrinsifier::Build_ThreadLocalHasValue(FlowGraph* flow_graph) {
759+
GraphEntryInstr* graph_entry = flow_graph->graph_entry();
760+
auto normal_entry = graph_entry->normal_entry();
761+
BlockBuilder builder(flow_graph, normal_entry, /*with_frame=*/false);
762+
Definition* local = GetThreadLocalValue(flow_graph, &builder);
763+
764+
Definition* sentinel = flow_graph->GetConstant(Object::sentinel());
765+
Definition* result = builder.AddDefinition(new StrictCompareInstr(
766+
builder.Source(), Token::kNE_STRICT, new Value(local),
767+
new Value(sentinel), /*needs_number_check=*/false, DeoptId::kNone));
768+
769+
builder.AddReturn(new Value(result));
770+
return true;
771+
}
772+
724773
} // namespace compiler
725774
} // namespace dart

runtime/vm/compiler/recognized_methods_list.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -670,6 +670,8 @@ namespace dart {
670670
V(TypedDataLibrary, _Float64x2, /, Float64x2Div, 0x12925562) \
671671
V(TypedDataLibrary, _Float64x2, -, Float64x2Sub, 0x2f258e89) \
672672
V(TypedDataLibrary, _Float64x2, +, Float64x2Add, 0x09ecc418) \
673+
V(VMLibrary, ThreadLocal, _getValue, ThreadLocalGetValue, 0xad8f22db) \
674+
V(VMLibrary, ThreadLocal, _hasValue, ThreadLocalHasValue, 0xa6d3b876) \
673675

674676
#define RECOGNIZED_LIST(V) \
675677
OTHER_RECOGNIZED_LIST(V) \

runtime/vm/compiler/runtime_api.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1156,6 +1156,7 @@ class Thread : public AllStatic {
11561156
static uword exit_through_ffi();
11571157
static word dart_stream_offset();
11581158
static word service_extension_stream_offset();
1159+
static word thread_locals_offset();
11591160
static word predefined_symbols_address_offset();
11601161
static word optimize_entry_offset();
11611162
static word deoptimize_entry_offset();

0 commit comments

Comments
 (0)