Skip to content

Commit

Permalink
Rewrite LOAD_GLOBAL _Unbound to LOAD_IMMEDIATE in builtins module
Browse files Browse the repository at this point in the history
This should save some caches and be faster. Ideally we would do this in
all Skybison-controlled modules but I don't think we have a good signal
for that yet.
  • Loading branch information
tekknolagi committed Dec 7, 2022
1 parent 80d8286 commit c33475e
Showing 1 changed file with 20 additions and 3 deletions.
23 changes: 20 additions & 3 deletions runtime/bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ struct RewrittenOp {
bool needs_inline_cache;
};

static RewrittenOp rewriteOperation(const Function& function, BytecodeOp op,
static RewrittenOp rewriteOperation(Thread* thread, const Function& function,
BytecodeOp op,
bool use_load_fast_reverse_unchecked) {
auto cached_binop = [](Interpreter::BinaryOp bin_op) {
return RewrittenOp{BINARY_OP_ANAMORPHIC, static_cast<int32_t>(bin_op),
Expand Down Expand Up @@ -178,6 +179,22 @@ static RewrittenOp rewriteOperation(const Function& function, BytecodeOp op,
return cached_inplace(Interpreter::BinaryOp::XOR);
case LOAD_ATTR:
return RewrittenOp{LOAD_ATTR_ANAMORPHIC, op.arg, true};
case LOAD_GLOBAL: {
RawObject module = function.moduleObject();
if (module.isModule() &&
Module::cast(module).id() == thread->runtime()->builtinsModuleId()) {
RawTuple names = Tuple::cast(Code::cast(function.code()).names());
RawStr name = Str::cast(names.at(op.arg));
if (name.equalsCStr("_Unbound")) {
DCHECK(Unbound::object() ==
objectFromOparg(opargFromObject(Unbound::object())),
"Expected to be able to fit _Unbound in a byte");
return RewrittenOp{LOAD_IMMEDIATE, opargFromObject(Unbound::object()),
false};
}
}
break;
}
case LOAD_FAST: {
CHECK(op.arg < Code::cast(function.code()).nlocals(),
"unexpected local number");
Expand Down Expand Up @@ -305,7 +322,7 @@ void rewriteBytecode(Thread* thread, const Function& function) {
use_load_fast_reverse_unchecked = false;
continue;
}
RewrittenOp rewritten = rewriteOperation(function, op, false);
RewrittenOp rewritten = rewriteOperation(thread, function, op, false);
if (rewritten.needs_inline_cache) {
num_caches++;
}
Expand All @@ -326,7 +343,7 @@ void rewriteBytecode(Thread* thread, const Function& function) {
BytecodeOp op = nextBytecodeOp(bytecode, &i);
word previous_index = i - 1;
RewrittenOp rewritten =
rewriteOperation(function, op, use_load_fast_reverse_unchecked);
rewriteOperation(thread, function, op, use_load_fast_reverse_unchecked);
if (rewritten.bc == UNUSED_BYTECODE_0) continue;
if (rewritten.needs_inline_cache) {
rewrittenBytecodeOpAtPut(bytecode, previous_index, rewritten.bc);
Expand Down

0 comments on commit c33475e

Please sign in to comment.