Skip to content

Commit

Permalink
Rewrite LOAD_GLOBAL _Unbound to LOAD_IMMEDIATE in frozen modules
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.

Partially addresses #395; we still cannot use it as marker in
dictionaries because it's still in the globals dict.
  • Loading branch information
tekknolagi committed Mar 23, 2023
1 parent 4cbb209 commit 7fdf49f
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 4 deletions.
28 changes: 24 additions & 4 deletions runtime/bytecode.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,8 @@ struct RewrittenOp {
};

static RewrittenOp rewriteOperation(const Function& function, BytecodeOp op,
bool use_load_fast_reverse_unchecked) {
bool use_load_fast_reverse_unchecked,
bool is_builtin_module) {
auto cached_binop = [](Interpreter::BinaryOp bin_op) {
return RewrittenOp{BINARY_OP_ANAMORPHIC, static_cast<int32_t>(bin_op),
true};
Expand Down Expand Up @@ -178,6 +179,20 @@ 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: {
if (is_builtin_module) {
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 +320,9 @@ void rewriteBytecode(Thread* thread, const Function& function) {
use_load_fast_reverse_unchecked = false;
continue;
}
RewrittenOp rewritten = rewriteOperation(function, op, false);
RewrittenOp rewritten = rewriteOperation(
function, op, /*use_load_fast_reverse_unchecked=*/false,
/*is_builtin_module=*/false);
if (rewritten.needs_inline_cache) {
num_caches++;
}
Expand All @@ -322,11 +339,14 @@ void rewriteBytecode(Thread* thread, const Function& function) {
return;
}
word cache = num_global_caches;
RawObject module = function.moduleObject();
bool is_builtin_module =
module.isModule() && Module::cast(module).isBuiltin();
for (word i = 0; i < num_opcodes;) {
BytecodeOp op = nextBytecodeOp(bytecode, &i);
word previous_index = i - 1;
RewrittenOp rewritten =
rewriteOperation(function, op, use_load_fast_reverse_unchecked);
RewrittenOp rewritten = rewriteOperation(
function, op, use_load_fast_reverse_unchecked, is_builtin_module);
if (rewritten.bc == UNUSED_BYTECODE_0) continue;
if (rewritten.needs_inline_cache) {
rewrittenBytecodeOpAtPut(bytecode, previous_index, rewritten.bc);
Expand Down
1 change: 1 addition & 0 deletions runtime/modules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ static RawObject createBuiltinModule(Thread* thread, const Str& name) {
if (builtin_index >= 0) {
const FrozenModule* frozen_module = &kFrozenModules[builtin_index];
Module module(&scope, runtime->newModule(name));
module.setId(module.id() | RawModule::kBuiltinTag);
Object modules(&scope, runtime->modules());
Object result(&scope, objectSetItem(thread, modules, name, module));
if (result.isErrorException()) return *result;
Expand Down
11 changes: 11 additions & 0 deletions runtime/objects.h
Original file line number Diff line number Diff line change
Expand Up @@ -2683,13 +2683,20 @@ class RawModule : public RawAttributeDict {
word id() const;
void setId(word id) const;

// Return true if the module is built-in; if so, the high bit of id is
// tagged.
bool isBuiltin() const;

// Layout.
static const int kNameOffset = RawAttributeDict::kSize;
static const int kDefOffset = kNameOffset + kPointerSize;
static const int kStateOffset = kDefOffset + kPointerSize;
static const int kModuleProxyOffset = kStateOffset + kPointerSize;
static const int kSize = kModuleProxyOffset + kPointerSize;

static const uword kBuiltinTag = uword{1} << sizeof(uword);
static const uword kBuiltinTagMask = ~kBuiltinTag;

// Constants.
static const word kMaxModuleId = RawHeader::kHashCodeMask;

Expand Down Expand Up @@ -7060,6 +7067,10 @@ inline void RawModule::setId(word id) const {
setHeader(header().withHashCode(id));
}

inline bool RawModule::isBuiltin() const {
return id() & kBuiltinTagMask;
}

// RawModuleProxy

inline RawObject RawModuleProxy::module() const {
Expand Down

0 comments on commit 7fdf49f

Please sign in to comment.