Skip to content
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

Don't look up layouts in STORE_ATTR_INSTANCE_OVERFLOW #491

Draft
wants to merge 1 commit into
base: trunk
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 24 additions & 15 deletions runtime/interpreter-gen-x64.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1512,24 +1512,33 @@ void emitHandler<STORE_ATTR_INSTANCE>(EmitEnv* env) {
template <>
void emitHandler<STORE_ATTR_INSTANCE_OVERFLOW>(EmitEnv* env) {
ScratchReg r_base(env);
ScratchReg r_layout_id(env);
ScratchReg r_cache_value(env);
ScratchReg r_caches(env);
ScratchReg r_packed_location(env);
Label slow_path;

__ popq(r_base);
emitGetLayoutId(env, r_layout_id, r_base);
__ movq(r_caches, Address(env->frame, Frame::kCachesOffset));
emitIcLookupMonomorphic(env, &slow_path, r_cache_value, r_layout_id,
r_caches);
emitConvertFromSmallInt(env, r_cache_value);

{
ScratchReg r_scratch(env);
emitLoadOverflowTuple(env, r_scratch, r_layout_id, r_base);
// The real tuple index is -offset - 1, which is the same as ~offset.
__ notq(r_cache_value);
__ popq(Address(r_scratch, r_cache_value, TIMES_8, heapObjectDisp(0)));
ScratchReg r_layout_id(env);
ScratchReg r_caches(env);
__ popq(r_base);
emitGetLayoutId(env, r_layout_id, r_base);
__ movq(r_caches, Address(env->frame, Frame::kCachesOffset));
emitIcLookupMonomorphic(env, &slow_path, r_packed_location, r_layout_id,
r_caches);
}

ScratchReg r_overflow_offset(env);
// uword packed_location = SmallInt::cast(cached).value();
emitConvertFromSmallInt(env, r_packed_location);
__ movq(r_overflow_offset, r_packed_location);
// uword overflow_offset = packed_location & 0xffffffff;
__ andq(r_overflow_offset, Immediate(0xffffffff));
// uword offset = packed_location >> 32;
__ shrq(r_packed_location, Immediate(32));
{
ScratchReg r_overflow_tuple(env);
__ movq(r_overflow_tuple,
Address(r_base, r_overflow_offset, TIMES_1, heapObjectDisp(0)));
__ popq(Address(r_overflow_tuple, r_packed_location, TIMES_8,
heapObjectDisp(0)));
emitNextOpcode(env);
}

Expand Down
40 changes: 31 additions & 9 deletions runtime/interpreter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3288,9 +3288,20 @@ Continue Interpreter::storeAttrUpdateCache(Thread* thread, word arg,
icUpdateAttr(thread, caches, cache, saved_layout_id, location, name,
dependent);
} else {
rewriteCurrentBytecode(frame, STORE_ATTR_INSTANCE_OVERFLOW);
icUpdateAttr(thread, caches, cache, saved_layout_id, location, name,
dependent);
uword offset = -SmallInt::cast(*location).value() - 1;
Layout layout(&scope, thread->runtime()->layoutOf(*receiver));
uword overflow_offset = layout.overflowOffset();
// Save one bit for the SmallInt tag
uword packed_location = (offset << 32) | overflow_offset;
// We need to check that they fit in 32 bits each and 63 bits together.
if (Utils::fits<uint32_t>(offset) &&
Utils::fits<uint32_t>(overflow_offset) &&
SmallInt::isValid(packed_location)) {
location = SmallInt::fromWord(packed_location);
rewriteCurrentBytecode(frame, STORE_ATTR_INSTANCE_OVERFLOW);
icUpdateAttr(thread, caches, cache, saved_layout_id, location, name,
dependent);
}
}
} else {
// Layout transition.
Expand All @@ -3316,6 +3327,18 @@ Continue Interpreter::storeAttrUpdateCache(Thread* thread, word arg,
currentBytecode(thread) == STORE_ATTR_INSTANCE_OVERFLOW ||
currentBytecode(thread) == STORE_ATTR_POLYMORPHIC,
"unexpected opcode");
if (ic_state == ICState::kMonomorphic &&
currentBytecode(thread) == STORE_ATTR_INSTANCE_OVERFLOW) {
// Fix up the packed cache; save only the index into the overflow tuple.
// That's what STORE_ATTR_POLYMORPHIC expects. Also, make it negative
// again to indicate that it's an overflow offset.
word index = cache * kIcPointersPerEntry;
RawObject cached = caches.at(index + kIcEntryValueOffset);
uword packed_location = SmallInt::cast(cached).value();
caches.atPut(index + kIcEntryValueOffset,
SmallInt::fromWord(~(packed_location >> 32)));
}

if (saved_layout_id == receiver_layout_id) {
rewriteCurrentBytecode(frame, STORE_ATTR_POLYMORPHIC);
icUpdateAttr(thread, caches, cache, saved_layout_id, location, name,
Expand Down Expand Up @@ -3384,13 +3407,12 @@ HANDLER_INLINE Continue Interpreter::doStoreAttrInstanceOverflow(Thread* thread,
EVENT_CACHE(STORE_ATTR_INSTANCE_OVERFLOW);
return storeAttrUpdateCache(thread, arg, cache);
}
word offset = SmallInt::cast(cached).value();
DCHECK(offset < 0, "unexpected offset");
uword packed_location = SmallInt::cast(cached).value();
uword overflow_offset = packed_location & 0xffffffff;
uword offset = packed_location >> 32;
RawInstance instance = Instance::cast(receiver);
RawLayout layout = Layout::cast(thread->runtime()->layoutOf(receiver));
RawTuple overflow =
Tuple::cast(instance.instanceVariableAt(layout.overflowOffset()));
overflow.atPut(-offset - 1, thread->stackPeek(1));
RawTuple overflow = Tuple::cast(instance.instanceVariableAt(overflow_offset));
overflow.atPut(offset, thread->stackPeek(1));
thread->stackDrop(2);
return Continue::NEXT;
}
Expand Down