Skip to content

Commit acb0f79

Browse files
committed
wip some fixes for block local opt
1 parent 8780ba5 commit acb0f79

File tree

6 files changed

+48
-9
lines changed

6 files changed

+48
-9
lines changed

src/asm_writing/rewriter.cpp

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,6 @@ std::vector<Location> Rewriter::getDecrefLocations() {
12311231
for (RewriterVar& var : vars) {
12321232
if (var.locations.size() && var.needsDecref()) {
12331233
bool found_location = false;
1234-
12351234
for (Location l : var.locations) {
12361235
if (l.type == Location::Scratch) {
12371236
// convert to stack based location because later on we may not know the offset of the scratch area
@@ -1249,7 +1248,10 @@ std::vector<Location> Rewriter::getDecrefLocations() {
12491248
} else
12501249
RELEASE_ASSERT(0, "not implemented");
12511250
}
1252-
RELEASE_ASSERT(found_location, "");
1251+
if (!found_location) {
1252+
assert(0);
1253+
failed = true;
1254+
}
12531255
}
12541256
}
12551257

@@ -1353,7 +1355,22 @@ void RewriterVar::refUsed() {
13531355
}
13541356

13551357
bool RewriterVar::needsDecref() {
1356-
return reftype == RefType::OWNED && !this->refHandedOff();
1358+
if (reftype == RefType::OWNED && !this->refHandedOff())
1359+
return true;
1360+
if (reftype == RefType::OWNED && this->num_refs_consumed > 0) {
1361+
if (uses[0] == rewriter->current_action_idx)
1362+
return false;
1363+
if (uses[this->last_refconsumed_numuses - 1] > rewriter->current_action_idx) {
1364+
return true;
1365+
}
1366+
return false;
1367+
}
1368+
if (reftype == RefType::OWNED) {
1369+
if (uses[0] == rewriter->current_action_idx)
1370+
return false;
1371+
return true;
1372+
}
1373+
return false;
13571374
}
13581375

13591376
void RewriterVar::registerOwnedAttr(int byte_offset) {
@@ -1506,6 +1523,7 @@ void Rewriter::commit() {
15061523
_incref(var, 1);
15071524
}
15081525

1526+
current_action_idx = i;
15091527
actions[i].action();
15101528

15111529
if (failed) {

src/asm_writing/rewriter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,7 @@ class Rewriter : public ICSlotRewrite::CommitHook {
566566
}
567567

568568
llvm::ArrayRef<assembler::Register> allocatable_regs;
569+
int current_action_idx = -1;
569570

570571
public:
571572
// This should be called exactly once for each argument

src/codegen/ast_interpreter.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -477,7 +477,7 @@ void ASTInterpreter::doStore(AST_Name* node, STOLEN(Value) value) {
477477
if (is_live)
478478
jit->emitSetLocal(name, node->vreg, closure, value);
479479
else
480-
jit->emitSetBlockLocal(name, value);
480+
jit->emitSetBlockLocal(name, node->vreg, value);
481481
}
482482

483483
if (closure) {
@@ -1781,6 +1781,10 @@ int ASTInterpreterJitInterface::getBoxedLocalsOffset() {
17811781
return offsetof(ASTInterpreter, frame_info.boxedLocals);
17821782
}
17831783

1784+
int ASTInterpreterJitInterface::getCreatedClosureOffset() {
1785+
return offsetof(ASTInterpreter, created_closure);
1786+
}
1787+
17841788
int ASTInterpreterJitInterface::getCurrentBlockOffset() {
17851789
return offsetof(ASTInterpreter, current_block);
17861790
}

src/codegen/ast_interpreter.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ struct ASTInterpreterJitInterface {
3939
static constexpr uint64_t osr_dummy_value = -1;
4040

4141
static int getBoxedLocalsOffset();
42+
static int getCreatedClosureOffset();
4243
static int getCurrentBlockOffset();
4344
static int getCurrentInstOffset();
4445
static int getEdgeCountOffset();

src/codegen/baseline_jit.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -393,8 +393,7 @@ RewriterVar* JitFragmentWriter::emitGetBlockLocal(InternedString s, int vreg) {
393393
if (it == local_syms.end()) {
394394
auto r = emitGetLocal(s, vreg);
395395
assert(r->reftype == RefType::OWNED);
396-
emitSetLocal(s, vreg, false, imm(nullptr)); // clear out the vreg
397-
emitSetBlockLocal(s, r);
396+
emitSetBlockLocal(s, vreg, r);
398397
return r;
399398
}
400399
return it->second;
@@ -403,6 +402,8 @@ RewriterVar* JitFragmentWriter::emitGetBlockLocal(InternedString s, int vreg) {
403402
void JitFragmentWriter::emitKillTemporary(InternedString s, int vreg) {
404403
if (!local_syms.count(s))
405404
emitSetLocal(s, vreg, false, imm(nullptr));
405+
else
406+
local_syms[s]->refUsed();
406407
}
407408

408409
RewriterVar* JitFragmentWriter::emitGetBoxedLocal(BoxedString* s) {
@@ -558,8 +559,19 @@ std::vector<RewriterVar*> JitFragmentWriter::emitUnpackIntoArray(RewriterVar* v,
558559
}
559560

560561
RewriterVar* JitFragmentWriter::emitYield(RewriterVar* v) {
561-
auto rtn = call(false, (void*)ASTInterpreterJitInterface::yieldHelper, getInterp(), v)->setType(RefType::OWNED);
562+
llvm::SmallVector<RewriterVar*, 8> local_args;
563+
local_args.push_back(interp->getAttr(ASTInterpreterJitInterface::getCreatedClosureOffset()));
564+
for (auto&& sym : local_syms) {
565+
if (sym.second->reftype == RefType::OWNED)
566+
local_args.push_back(sym.second);
567+
}
568+
auto&& args = allocArgs(local_args, RewriterVar::SetattrType::REF_USED);
569+
RewriterVar* generator = interp->getAttr(ASTInterpreterJitInterface::getGeneratorOffset());
570+
auto rtn = call(false, (void*)yield, generator, v, args, imm(local_args.size()))->setType(RefType::OWNED);
562571
v->refConsumed();
572+
for (auto&& var : local_args) {
573+
var->refUsed();
574+
}
563575
return rtn;
564576
}
565577

@@ -650,10 +662,13 @@ void JitFragmentWriter::emitSetAttr(AST_expr* node, RewriterVar* obj, BoxedStrin
650662
attr->refConsumed(rtn.second);
651663
}
652664

653-
void JitFragmentWriter::emitSetBlockLocal(InternedString s, STOLEN(RewriterVar*) v) {
665+
void JitFragmentWriter::emitSetBlockLocal(InternedString s, int vreg, STOLEN(RewriterVar*) v) {
654666
if (LOG_BJIT_ASSEMBLY)
655667
comment("BJIT: emitSetBlockLocal() start");
656668
RewriterVar* prev = local_syms[s];
669+
if (!prev) {
670+
emitSetLocal(s, vreg, false, imm(nullptr)); // clear out the vreg
671+
}
657672
local_syms[s] = v;
658673
if (LOG_BJIT_ASSEMBLY)
659674
comment("BJIT: emitSetBlockLocal() end");

src/codegen/baseline_jit.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ class JitFragmentWriter : public Rewriter {
278278
void emitRaise3(RewriterVar* arg0, RewriterVar* arg1, RewriterVar* arg2);
279279
void emitReturn(RewriterVar* v);
280280
void emitSetAttr(AST_expr* node, RewriterVar* obj, BoxedString* s, STOLEN(RewriterVar*) attr);
281-
void emitSetBlockLocal(InternedString s, STOLEN(RewriterVar*) v);
281+
void emitSetBlockLocal(InternedString s, int vreg, STOLEN(RewriterVar*) v);
282282
void emitSetCurrentInst(AST_stmt* node);
283283
void emitSetExcInfo(RewriterVar* type, RewriterVar* value, RewriterVar* traceback);
284284
void emitSetGlobal(Box* global, BoxedString* s, STOLEN(RewriterVar*) v);

0 commit comments

Comments
 (0)