Commit 106533f9 authored by Marius Wachtler's avatar Marius Wachtler Committed by GitHub

Merge pull request #1383 from undingen/cleanup_bst

move constants into CodeConstants, call BoxedCode destructor, cleanup BST nodes
parents 1961ce67 a2560414
...@@ -89,17 +89,17 @@ private: ...@@ -89,17 +89,17 @@ private:
ExprTypeMap& expr_types; ExprTypeMap& expr_types;
TypeSpeculations& type_speculations; TypeSpeculations& type_speculations;
TypeAnalysis::SpeculationLevel speculation; TypeAnalysis::SpeculationLevel speculation;
const ConstantVRegInfo& constant_vregs; const CodeConstants& code_constants;
BasicBlockTypePropagator(CFGBlock* block, TypeMap& initial, ExprTypeMap& expr_types, BasicBlockTypePropagator(CFGBlock* block, TypeMap& initial, ExprTypeMap& expr_types,
TypeSpeculations& type_speculations, TypeAnalysis::SpeculationLevel speculation, TypeSpeculations& type_speculations, TypeAnalysis::SpeculationLevel speculation,
const ConstantVRegInfo& constant_vregs) const CodeConstants& code_constants)
: block(block), : block(block),
sym_table(initial), sym_table(initial),
expr_types(expr_types), expr_types(expr_types),
type_speculations(type_speculations), type_speculations(type_speculations),
speculation(speculation), speculation(speculation),
constant_vregs(constant_vregs) {} code_constants(code_constants) {}
void run() { void run() {
for (int i = 0; i < block->body.size(); i++) { for (int i = 0; i < block->body.size(); i++) {
...@@ -118,7 +118,7 @@ private: ...@@ -118,7 +118,7 @@ private:
printf("in propagator, speculating that %s would actually be %s, at ", printf("in propagator, speculating that %s would actually be %s, at ",
old_type->debugName().c_str(), speculated_type->debugName().c_str()); old_type->debugName().c_str(), speculated_type->debugName().c_str());
fflush(stdout); fflush(stdout);
print_bst(node, constant_vregs); print_bst(node, code_constants);
llvm::outs().flush(); llvm::outs().flush();
printf("\n"); printf("\n");
} }
...@@ -131,7 +131,7 @@ private: ...@@ -131,7 +131,7 @@ private:
} }
CompilerType* getConstantType(int vreg) { CompilerType* getConstantType(int vreg) {
Box* o = constant_vregs.getConstant(vreg); Box* o = code_constants.getConstant(vreg);
if (o->cls == int_cls) if (o->cls == int_cls)
return INT; return INT;
else if (o->cls == float_cls) else if (o->cls == float_cls)
...@@ -284,7 +284,7 @@ private: ...@@ -284,7 +284,7 @@ private:
if (VERBOSITY() >= 2 && func == UNDEF) { if (VERBOSITY() >= 2 && func == UNDEF) {
printf("Think %s.%s is undefined, at %d\n", t->debugName().c_str(), node->attr.c_str(), node->lineno); printf("Think %s.%s is undefined, at %d\n", t->debugName().c_str(), node->attr.c_str(), node->lineno);
print_bst(node, constant_vregs); print_bst(node, code_constants);
printf("\n"); printf("\n");
} }
...@@ -297,7 +297,7 @@ private: ...@@ -297,7 +297,7 @@ private:
if (VERBOSITY() >= 2 && func == UNDEF) { if (VERBOSITY() >= 2 && func == UNDEF) {
printf("Think %s.%s is undefined, at %d\n", t->debugName().c_str(), node->attr.c_str(), node->lineno); printf("Think %s.%s is undefined, at %d\n", t->debugName().c_str(), node->attr.c_str(), node->lineno);
print_bst(node, constant_vregs); print_bst(node, code_constants);
printf("\n"); printf("\n");
} }
...@@ -399,7 +399,7 @@ private: ...@@ -399,7 +399,7 @@ private:
if (VERBOSITY() >= 2 && rtn == UNDEF) { if (VERBOSITY() >= 2 && rtn == UNDEF) {
printf("Think %s.%s is undefined, at %d\n", t->debugName().c_str(), node->attr.c_str(), node->lineno); printf("Think %s.%s is undefined, at %d\n", t->debugName().c_str(), node->attr.c_str(), node->lineno);
print_bst(node, constant_vregs); print_bst(node, code_constants);
printf("\n"); printf("\n");
} }
_doSet(node->vreg_dst, rtn); _doSet(node->vreg_dst, rtn);
...@@ -551,9 +551,9 @@ private: ...@@ -551,9 +551,9 @@ private:
public: public:
static TypeMap propagate(CFGBlock* block, const TypeMap& starting, ExprTypeMap& expr_types, static TypeMap propagate(CFGBlock* block, const TypeMap& starting, ExprTypeMap& expr_types,
TypeSpeculations& type_speculations, TypeAnalysis::SpeculationLevel speculation, TypeSpeculations& type_speculations, TypeAnalysis::SpeculationLevel speculation,
const ConstantVRegInfo& constant_vregs) { const CodeConstants& code_constants) {
TypeMap ending = starting; TypeMap ending = starting;
BasicBlockTypePropagator(block, ending, expr_types, type_speculations, speculation, constant_vregs).run(); BasicBlockTypePropagator(block, ending, expr_types, type_speculations, speculation, code_constants).run();
return ending; return ending;
} }
}; };
...@@ -632,7 +632,7 @@ public: ...@@ -632,7 +632,7 @@ public:
} }
static PropagatingTypeAnalysis* doAnalysis(SpeculationLevel speculation, TypeMap&& initial_types, static PropagatingTypeAnalysis* doAnalysis(SpeculationLevel speculation, TypeMap&& initial_types,
CFGBlock* initial_block, const ConstantVRegInfo& constant_vregs) { CFGBlock* initial_block, const CodeConstants& code_constants) {
Timer _t("PropagatingTypeAnalysis::doAnalysis()"); Timer _t("PropagatingTypeAnalysis::doAnalysis()");
CFG* cfg = initial_block->cfg; CFG* cfg = initial_block->cfg;
...@@ -673,7 +673,7 @@ public: ...@@ -673,7 +673,7 @@ public:
} }
TypeMap ending = BasicBlockTypePropagator::propagate(block, starting_types.find(block)->second, expr_types, TypeMap ending = BasicBlockTypePropagator::propagate(block, starting_types.find(block)->second, expr_types,
type_speculations, speculation, constant_vregs); type_speculations, speculation, code_constants);
if (VERBOSITY("types") >= 3) { if (VERBOSITY("types") >= 3) {
printf("before (after):\n"); printf("before (after):\n");
...@@ -730,7 +730,7 @@ public: ...@@ -730,7 +730,7 @@ public:
// public entry point: // public entry point:
TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& arg_names, const std::vector<ConcreteCompilerType*>& arg_types, TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& arg_names, const std::vector<ConcreteCompilerType*>& arg_types,
EffortLevel effort, TypeAnalysis::SpeculationLevel speculation, EffortLevel effort, TypeAnalysis::SpeculationLevel speculation,
const ConstantVRegInfo& constant_vregs) { const CodeConstants& code_constants) {
// if (effort == EffortLevel::INTERPRETED) { // if (effort == EffortLevel::INTERPRETED) {
// return new NullTypeAnalysis(); // return new NullTypeAnalysis();
//} //}
...@@ -750,11 +750,11 @@ TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& arg_names, const std::v ...@@ -750,11 +750,11 @@ TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& arg_names, const std::v
assert(i == arg_types.size()); assert(i == arg_types.size());
return PropagatingTypeAnalysis::doAnalysis(speculation, std::move(initial_types), cfg->getStartingBlock(), return PropagatingTypeAnalysis::doAnalysis(speculation, std::move(initial_types), cfg->getStartingBlock(),
constant_vregs); code_constants);
} }
TypeAnalysis* doTypeAnalysis(const OSREntryDescriptor* entry_descriptor, EffortLevel effort, TypeAnalysis* doTypeAnalysis(const OSREntryDescriptor* entry_descriptor, EffortLevel effort,
TypeAnalysis::SpeculationLevel speculation, const ConstantVRegInfo& constant_vregs) { TypeAnalysis::SpeculationLevel speculation, const CodeConstants& code_constants) {
auto cfg = entry_descriptor->code->source->cfg; auto cfg = entry_descriptor->code->source->cfg;
auto&& vreg_info = cfg->getVRegInfo(); auto&& vreg_info = cfg->getVRegInfo();
TypeMap initial_types(vreg_info.getTotalNumOfVRegs()); TypeMap initial_types(vreg_info.getTotalNumOfVRegs());
...@@ -764,6 +764,6 @@ TypeAnalysis* doTypeAnalysis(const OSREntryDescriptor* entry_descriptor, EffortL ...@@ -764,6 +764,6 @@ TypeAnalysis* doTypeAnalysis(const OSREntryDescriptor* entry_descriptor, EffortL
} }
return PropagatingTypeAnalysis::doAnalysis(speculation, std::move(initial_types), return PropagatingTypeAnalysis::doAnalysis(speculation, std::move(initial_types),
entry_descriptor->backedge->target, constant_vregs); entry_descriptor->backedge->target, code_constants);
} }
} }
...@@ -24,7 +24,7 @@ ...@@ -24,7 +24,7 @@
namespace pyston { namespace pyston {
class CFGBlock; class CFGBlock;
class ConstantVRegInfo; class CodeConstants;
class BoxedClass; class BoxedClass;
class BST_stmt_with_dest; class BST_stmt_with_dest;
class OSREntryDescriptor; class OSREntryDescriptor;
...@@ -45,9 +45,9 @@ public: ...@@ -45,9 +45,9 @@ public:
TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& param_names, TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& param_names,
const std::vector<ConcreteCompilerType*>& arg_types, EffortLevel effort, const std::vector<ConcreteCompilerType*>& arg_types, EffortLevel effort,
TypeAnalysis::SpeculationLevel speculation, const ConstantVRegInfo& constant_vregs); TypeAnalysis::SpeculationLevel speculation, const CodeConstants& code_constants);
TypeAnalysis* doTypeAnalysis(const OSREntryDescriptor* entry_descriptor, EffortLevel effort, TypeAnalysis* doTypeAnalysis(const OSREntryDescriptor* entry_descriptor, EffortLevel effort,
TypeAnalysis::SpeculationLevel speculation, const ConstantVRegInfo& constant_vregs); TypeAnalysis::SpeculationLevel speculation, const CodeConstants& code_constants);
} }
#endif #endif
...@@ -181,7 +181,7 @@ public: ...@@ -181,7 +181,7 @@ public:
BoxedClosure* getPassedClosure() { return frame_info.passed_closure; } BoxedClosure* getPassedClosure() { return frame_info.passed_closure; }
Box** getVRegs() { return vregs; } Box** getVRegs() { return vregs; }
const ScopingResults& getScopeInfo() { return scope_info; } const ScopingResults& getScopeInfo() { return scope_info; }
const ConstantVRegInfo& getConstantVRegInfo() { return getCode()->constant_vregs; } const CodeConstants& getCodeConstants() { return getCode()->code_constants; }
void addSymbol(int vreg, Box* value, bool allow_duplicates); void addSymbol(int vreg, Box* value, bool allow_duplicates);
void setGenerator(Box* gen); void setGenerator(Box* gen);
...@@ -900,7 +900,7 @@ Value ASTInterpreter::visit_stmt(BST_stmt* node) { ...@@ -900,7 +900,7 @@ Value ASTInterpreter::visit_stmt(BST_stmt* node) {
if (0) { if (0) {
printf("%20s % 2d ", getCode()->name->c_str(), current_block->idx); printf("%20s % 2d ", getCode()->name->c_str(), current_block->idx);
print_bst(node, getConstantVRegInfo()); print_bst(node, getCodeConstants());
printf("\n"); printf("\n");
} }
...@@ -1528,7 +1528,7 @@ Value ASTInterpreter::visit_set(BST_Set* node) { ...@@ -1528,7 +1528,7 @@ Value ASTInterpreter::visit_set(BST_Set* node) {
Value ASTInterpreter::getVReg(int vreg, bool is_kill) { Value ASTInterpreter::getVReg(int vreg, bool is_kill) {
assert(vreg != VREG_UNDEFINED); assert(vreg != VREG_UNDEFINED);
if (vreg < 0) { if (vreg < 0) {
Box* o = getConstantVRegInfo().getConstant(vreg); Box* o = getCodeConstants().getConstant(vreg);
return Value(incref(o), jit ? jit->imm(o)->setType(RefType::BORROWED) : NULL); return Value(incref(o), jit ? jit->imm(o)->setType(RefType::BORROWED) : NULL);
} }
...@@ -1564,10 +1564,10 @@ Value ASTInterpreter::getVReg(int vreg, bool is_kill) { ...@@ -1564,10 +1564,10 @@ Value ASTInterpreter::getVReg(int vreg, bool is_kill) {
} }
current_block->print(getConstantVRegInfo()); current_block->print(getCodeConstants());
printf("vreg: %d num cross: %d\n", vreg, getVRegInfo().getNumOfCrossBlockVRegs()); printf("vreg: %d num cross: %d\n", vreg, getVRegInfo().getNumOfCrossBlockVRegs());
printf("\n\n"); printf("\n\n");
current_block->cfg->print(getConstantVRegInfo()); current_block->cfg->print(getCodeConstants());
assert(0); assert(0);
return Value(); return Value();
......
...@@ -1027,7 +1027,7 @@ std::pair<CompiledFunction*, llvm::Function*> doCompile(BoxedCode* code, SourceI ...@@ -1027,7 +1027,7 @@ std::pair<CompiledFunction*, llvm::Function*> doCompile(BoxedCode* code, SourceI
assert((entry_descriptor != NULL) + (spec != NULL) == 1); assert((entry_descriptor != NULL) + (spec != NULL) == 1);
if (VERBOSITY("irgen") >= 2) if (VERBOSITY("irgen") >= 2)
source->cfg->print(code->constant_vregs); source->cfg->print(code->code_constants);
assert(g.cur_module == NULL); assert(g.cur_module == NULL);
...@@ -1101,10 +1101,10 @@ std::pair<CompiledFunction*, llvm::Function*> doCompile(BoxedCode* code, SourceI ...@@ -1101,10 +1101,10 @@ std::pair<CompiledFunction*, llvm::Function*> doCompile(BoxedCode* code, SourceI
speculation_level = TypeAnalysis::SOME; speculation_level = TypeAnalysis::SOME;
TypeAnalysis* types; TypeAnalysis* types;
if (entry_descriptor) if (entry_descriptor)
types = doTypeAnalysis(entry_descriptor, effort, speculation_level, code->constant_vregs); types = doTypeAnalysis(entry_descriptor, effort, speculation_level, code->code_constants);
else else
types = doTypeAnalysis(source->cfg, *param_names, spec->arg_types, effort, speculation_level, types = doTypeAnalysis(source->cfg, *param_names, spec->arg_types, effort, speculation_level,
code->constant_vregs); code->code_constants);
_t2.split(); _t2.split();
......
...@@ -694,6 +694,9 @@ void BoxedCode::addVersion(void* f, ConcreteCompilerType* rtn_type, const std::v ...@@ -694,6 +694,9 @@ void BoxedCode::addVersion(void* f, ConcreteCompilerType* rtn_type, const std::v
} }
bool BoxedCode::tryDeallocatingTheBJitCode() { bool BoxedCode::tryDeallocatingTheBJitCode() {
if (code_blocks.empty())
return true;
// we can only delete the code object if we are not executing it currently // we can only delete the code object if we are not executing it currently
assert(bjit_num_inside >= 0); assert(bjit_num_inside >= 0);
if (bjit_num_inside != 0) { if (bjit_num_inside != 0) {
......
...@@ -69,6 +69,10 @@ IRGenState::IRGenState(BoxedCode* code, CompiledFunction* cf, llvm::Function* fu ...@@ -69,6 +69,10 @@ IRGenState::IRGenState(BoxedCode* code, CompiledFunction* cf, llvm::Function* fu
IRGenState::~IRGenState() { IRGenState::~IRGenState() {
} }
const CodeConstants& IRGenState::getCodeConstants() {
return code->code_constants;
}
llvm::Value* IRGenState::getPassedClosure() { llvm::Value* IRGenState::getPassedClosure() {
assert(getScopeInfo().takesClosure()); assert(getScopeInfo().takesClosure());
assert(passed_closure); assert(passed_closure);
...@@ -721,9 +725,9 @@ public: ...@@ -721,9 +725,9 @@ public:
return rtn; return rtn;
} }
Box* getIntConstant(int64_t n) override { return irstate->getSourceInfo()->parent_module->getIntConstant(n); } Box* getIntConstant(int64_t n) override { return irstate->getCodeConstants().getIntConstant(n); }
Box* getFloatConstant(double d) override { return irstate->getSourceInfo()->parent_module->getFloatConstant(d); } Box* getFloatConstant(double d) override { return irstate->getCodeConstants().getFloatConstant(d); }
void refConsumed(llvm::Value* v, llvm::Instruction* inst) override { void refConsumed(llvm::Value* v, llvm::Instruction* inst) override {
irstate->getRefcounts()->refConsumed(v, inst); irstate->getRefcounts()->refConsumed(v, inst);
...@@ -1225,7 +1229,7 @@ private: ...@@ -1225,7 +1229,7 @@ private:
CompilerVariable* evalVReg(int vreg, bool is_kill = true) { CompilerVariable* evalVReg(int vreg, bool is_kill = true) {
assert(vreg != VREG_UNDEFINED); assert(vreg != VREG_UNDEFINED);
if (vreg < 0) { if (vreg < 0) {
Box* o = irstate->getCode()->constant_vregs.getConstant(vreg); Box* o = irstate->getCode()->code_constants.getConstant(vreg);
if (o->cls == int_cls) { if (o->cls == int_cls) {
return makeInt(((BoxedInt*)o)->n); return makeInt(((BoxedInt*)o)->n);
} else if (o->cls == float_cls) { } else if (o->cls == float_cls) {
...@@ -1612,7 +1616,7 @@ private: ...@@ -1612,7 +1616,7 @@ private:
printf("Speculating that %s is actually %s, at ", rtn->getType()->debugName().c_str(), printf("Speculating that %s is actually %s, at ", rtn->getType()->debugName().c_str(),
speculated_type->debugName().c_str()); speculated_type->debugName().c_str());
fflush(stdout); fflush(stdout);
print_bst(node, irstate->getCode()->constant_vregs); print_bst(node, irstate->getCode()->code_constants);
llvm::outs().flush(); llvm::outs().flush();
printf("\n"); printf("\n");
} }
...@@ -1624,7 +1628,7 @@ private: ...@@ -1624,7 +1628,7 @@ private:
auto source = irstate->getSourceInfo(); auto source = irstate->getSourceInfo();
printf("On %s:%d, function %s:\n", irstate->getCode()->filename->c_str(), printf("On %s:%d, function %s:\n", irstate->getCode()->filename->c_str(),
irstate->getCode()->firstlineno, irstate->getCode()->name->c_str()); irstate->getCode()->firstlineno, irstate->getCode()->name->c_str());
irstate->getSourceInfo()->cfg->print(irstate->getCode()->constant_vregs); irstate->getSourceInfo()->cfg->print(irstate->getCode()->code_constants);
} }
RELEASE_ASSERT(!rtn->canConvertTo(speculated_type), "%s %s", rtn->getType()->debugName().c_str(), RELEASE_ASSERT(!rtn->canConvertTo(speculated_type), "%s %s", rtn->getType()->debugName().c_str(),
speculated_type->debugName().c_str()); speculated_type->debugName().c_str());
......
...@@ -92,6 +92,7 @@ public: ...@@ -92,6 +92,7 @@ public:
CompiledFunction* getCurFunction() { return cf; } CompiledFunction* getCurFunction() { return cf; }
BoxedCode* getCode() { return code; } BoxedCode* getCode() { return code; }
const CodeConstants& getCodeConstants();
ExceptionStyle getExceptionStyle() { return cf->exception_style; } ExceptionStyle getExceptionStyle() { return cf->exception_style; }
......
...@@ -754,8 +754,8 @@ void BST_MakeSlice::accept_stmt(StmtVisitor* v) { ...@@ -754,8 +754,8 @@ void BST_MakeSlice::accept_stmt(StmtVisitor* v) {
return v->visit_makeslice(this); return v->visit_makeslice(this);
} }
void print_bst(BST_stmt* bst, const ConstantVRegInfo& constant_vregs) { void print_bst(BST_stmt* bst, const CodeConstants& code_constants) {
PrintVisitor v(constant_vregs, 0, llvm::outs()); PrintVisitor v(code_constants, 0, llvm::outs());
bst->accept(&v); bst->accept(&v);
v.flush(); v.flush();
} }
...@@ -771,7 +771,7 @@ bool PrintVisitor::visit_vreg(int* vreg, bool is_dst) { ...@@ -771,7 +771,7 @@ bool PrintVisitor::visit_vreg(int* vreg, bool is_dst) {
if (*vreg != VREG_UNDEFINED) { if (*vreg != VREG_UNDEFINED) {
stream << "%" << *vreg; stream << "%" << *vreg;
if (*vreg < 0) if (*vreg < 0)
stream << "|" << autoDecref(repr(constant_vregs.getConstant(*vreg)))->s() << "|"; stream << "|" << autoDecref(repr(code_constants.getConstant(*vreg)))->s() << "|";
} else } else
stream << "%undef"; stream << "%undef";
......
This diff is collapsed.
This diff is collapsed.
...@@ -88,7 +88,7 @@ public: ...@@ -88,7 +88,7 @@ public:
void unconnectFrom(CFGBlock* successor); void unconnectFrom(CFGBlock* successor);
void push_back(BST_stmt* node) { body.push_back(node); } void push_back(BST_stmt* node) { body.push_back(node); }
void print(const ConstantVRegInfo& constant_vregs, llvm::raw_ostream& stream = llvm::outs()); void print(const CodeConstants& code_constants, llvm::raw_ostream& stream = llvm::outs());
}; };
// the vregs are split into three parts. // the vregs are split into three parts.
...@@ -210,7 +210,7 @@ public: ...@@ -210,7 +210,7 @@ public:
blocks.push_back(block); blocks.push_back(block);
} }
void print(const ConstantVRegInfo& constant_vregs, llvm::raw_ostream& stream = llvm::outs()); void print(const CodeConstants& code_constants, llvm::raw_ostream& stream = llvm::outs());
}; };
class VRegSet { class VRegSet {
...@@ -331,7 +331,7 @@ public: ...@@ -331,7 +331,7 @@ public:
BoxedCode* computeAllCFGs(AST* ast, bool globals_from_module, FutureFlags future_flags, BoxedString* fn, BoxedCode* computeAllCFGs(AST* ast, bool globals_from_module, FutureFlags future_flags, BoxedString* fn,
BoxedModule* bm); BoxedModule* bm);
void printCFG(CFG* cfg, const ConstantVRegInfo& constant_vregs); void printCFG(CFG* cfg, const CodeConstants& code_constants);
} }
#endif #endif
...@@ -107,16 +107,18 @@ void BoxedCode::dealloc(Box* b) noexcept { ...@@ -107,16 +107,18 @@ void BoxedCode::dealloc(Box* b) noexcept {
Py_XDECREF(o->name); Py_XDECREF(o->name);
Py_XDECREF(o->_doc); Py_XDECREF(o->_doc);
o->tryDeallocatingTheBJitCode();
o->source.reset(nullptr); o->source.reset(nullptr);
o->~BoxedCode();
o->cls->tp_free(o); o->cls->tp_free(o);
} }
BoxedCode::BoxedCode(int num_args, bool takes_varargs, bool takes_kwargs, int firstlineno, BoxedCode::BoxedCode(int num_args, bool takes_varargs, bool takes_kwargs, int firstlineno,
std::unique_ptr<SourceInfo> source, ConstantVRegInfo constant_vregs, ParamNames param_names, std::unique_ptr<SourceInfo> source, CodeConstants code_constants, ParamNames param_names,
BoxedString* filename, BoxedString* name, Box* doc) BoxedString* filename, BoxedString* name, Box* doc)
: source(std::move(source)), : source(std::move(source)),
constant_vregs(std::move(constant_vregs)), code_constants(std::move(code_constants)),
filename(incref(filename)), filename(incref(filename)),
name(incref(name)), name(incref(name)),
firstlineno(firstlineno), firstlineno(firstlineno),
......
...@@ -480,68 +480,6 @@ std::string BoxedModule::name() { ...@@ -480,68 +480,6 @@ std::string BoxedModule::name() {
} }
} }
BORROWED(BoxedString*) BoxedModule::getStringConstant(llvm::StringRef ast_str, bool intern) {
BoxedString*& r = str_constants[ast_str];
if (intern) {
// If we had previously created a box for this string, we have to create a new
// string (or at least, be prepared to return a different value that we had already
// interned). This is fine, except we have to be careful because we promised
// that we would keep the previously-created string alive.
// So, make sure to put it onto the keep_alive list.
if (r && !PyString_CHECK_INTERNED(r)) {
RELEASE_ASSERT(0, "this codepath has been dead for a little while, make sure it still works");
keep_alive.push_back(r);
r = NULL;
}
if (!r)
r = internStringMortal(ast_str);
} else if (!r)
r = boxString(ast_str);
return r;
}
BORROWED(Box*) BoxedModule::getUnicodeConstant(llvm::StringRef ast_str) {
Box*& r = unicode_constants[ast_str];
if (!r)
r = decodeUTF8StringPtr(ast_str);
return r;
}
BORROWED(BoxedInt*) BoxedModule::getIntConstant(int64_t n) {
BoxedInt*& r = int_constants[n];
if (!r)
r = (BoxedInt*)boxInt(n);
return r;
}
static int64_t getDoubleBits(double d) {
int64_t rtn;
static_assert(sizeof(rtn) == sizeof(d), "");
memcpy(&rtn, &d, sizeof(d));
return rtn;
}
BORROWED(BoxedFloat*) BoxedModule::getFloatConstant(double d) {
BoxedFloat*& r = float_constants[getDoubleBits(d)];
if (!r)
r = static_cast<BoxedFloat*>(boxFloat(d));
return r;
}
BORROWED(Box*) BoxedModule::getPureImaginaryConstant(double d) {
Box*& r = imaginary_constants[getDoubleBits(d)];
if (!r)
r = createPureImaginary(d);
return r;
}
BORROWED(Box*) BoxedModule::getLongConstant(llvm::StringRef ast_str) {
Box*& r = long_constants[ast_str];
if (!r)
r = createLong(ast_str);
return r;
}
// This mustn't throw; our IR generator generates calls to it without "invoke" even when there are exception handlers / // This mustn't throw; our IR generator generates calls to it without "invoke" even when there are exception handlers /
// finally-blocks in scope. // finally-blocks in scope.
extern "C" Box* createFunctionFromMetadata(BoxedCode* code, BoxedClosure* closure, Box* globals, extern "C" Box* createFunctionFromMetadata(BoxedCode* code, BoxedClosure* closure, Box* globals,
...@@ -3817,21 +3755,12 @@ void BoxedModule::dealloc(Box* b) noexcept { ...@@ -3817,21 +3755,12 @@ void BoxedModule::dealloc(Box* b) noexcept {
BoxedModule::clear(self); BoxedModule::clear(self);
self->str_constants.~ContiguousMap();
self->unicode_constants.~ContiguousMap();
self->int_constants.~ContiguousMap();
self->float_constants.~ContiguousMap();
self->imaginary_constants.~ContiguousMap();
self->long_constants.~ContiguousMap();
assert(!self->keep_alive.size());
b->cls->tp_free(self); b->cls->tp_free(self);
} }
int BoxedModule::traverse(Box* _m, visitproc visit, void* arg) noexcept { int BoxedModule::traverse(Box* _m, visitproc visit, void* arg) noexcept {
BoxedModule* m = static_cast<BoxedModule*>(_m); BoxedModule* m = static_cast<BoxedModule*>(_m);
Py_TRAVERSE(m->attrs); Py_TRAVERSE(m->attrs);
assert(!m->keep_alive.size());
return 0; return 0;
} }
...@@ -3849,15 +3778,6 @@ extern "C" void _PyModule_Clear(PyObject* b) noexcept { ...@@ -3849,15 +3778,6 @@ extern "C" void _PyModule_Clear(PyObject* b) noexcept {
HCAttrs* attrs = self->getHCAttrsPtr(); HCAttrs* attrs = self->getHCAttrsPtr();
attrs->moduleClear(); attrs->moduleClear();
clearContiguousMap(self->str_constants);
clearContiguousMap(self->unicode_constants);
clearContiguousMap(self->int_constants);
clearContiguousMap(self->float_constants);
clearContiguousMap(self->imaginary_constants);
clearContiguousMap(self->long_constants);
assert(!self->keep_alive.size());
} }
int BoxedModule::clear(Box* b) noexcept { int BoxedModule::clear(Box* b) noexcept {
...@@ -4102,6 +4022,33 @@ int BoxedClosure::clear(Box* _o) noexcept { ...@@ -4102,6 +4022,33 @@ int BoxedClosure::clear(Box* _o) noexcept {
return 0; return 0;
} }
BORROWED(BoxedInt*) CodeConstants::getIntConstant(int64_t n) const {
BoxedInt*& r = int_constants[n];
if (!r) {
r = (BoxedInt*)boxInt(n);
addOwnedRef(r);
}
return r;
}
BORROWED(BoxedFloat*) CodeConstants::getFloatConstant(double d) const {
int64_t double_as_int64;
static_assert(sizeof(double_as_int64) == sizeof(d), "");
memcpy(&double_as_int64, &d, sizeof(d));
BoxedFloat*& r = float_constants[double_as_int64];
if (!r) {
r = (BoxedFloat*)boxFloat(d);
addOwnedRef(r);
}
return r;
}
void CodeConstants::dealloc() const {
decrefArray(owned_refs.data(), owned_refs.size());
owned_refs.clear();
}
#ifndef Py_REF_DEBUG #ifndef Py_REF_DEBUG
#define PRINT_TOTAL_REFS() #define PRINT_TOTAL_REFS()
#else /* Py_REF_DEBUG */ #else /* Py_REF_DEBUG */
......
...@@ -1070,20 +1070,42 @@ public: ...@@ -1070,20 +1070,42 @@ public:
}; };
static_assert(sizeof(BoxedDict) == sizeof(PyDictObject), ""); static_assert(sizeof(BoxedDict) == sizeof(PyDictObject), "");
class ConstantVRegInfo { class CodeConstants {
private: private:
// stores all constants accessible by vregs in the corrext order
// constants[-(vreg + 1)] will allow one to retrieve the constant for a vreg
std::vector<Box*> constants; std::vector<Box*> constants;
// all objects we need to decref when the code object dies
mutable std::vector<Box*> owned_refs;
// Note: DenseMap doesn't work here since we don't prevent the tombstone/empty
// keys from reaching it.
mutable std::unordered_map<int64_t, BoxedInt*> int_constants;
// I'm not sure how well it works to use doubles as hashtable keys; thankfully
// it's not a big deal if we get misses.
mutable std::unordered_map<int64_t, BoxedFloat*> float_constants;
public: public:
ConstantVRegInfo(){}; CodeConstants() {}
CodeConstants(CodeConstants&&) = default;
CodeConstants& operator=(CodeConstants&&) = default;
~CodeConstants() { dealloc(); }
Box* getConstant(int vreg) const { return constants[-(vreg + 1)]; } BORROWED(Box*) getConstant(int vreg) const { return constants[-(vreg + 1)]; }
// returns the vreg num for the constant (which is a negative number) // returns the vreg num for the constant (which is a negative number)
int addConstant(Box* o) { int createVRegEntryForConstant(Box* o) {
constants.push_back(o); constants.push_back(o);
return -constants.size(); return -constants.size();
} }
void addOwnedRef(Box* o) const { owned_refs.emplace_back(o); }
BORROWED(BoxedInt*) getIntConstant(int64_t n) const;
BORROWED(BoxedFloat*) getFloatConstant(double d) const;
void dealloc() const;
}; };
...@@ -1095,8 +1117,8 @@ public: ...@@ -1095,8 +1117,8 @@ public:
// BoxedCode objects also keep track of any machine code that we have available for this function. // BoxedCode objects also keep track of any machine code that we have available for this function.
class BoxedCode : public Box { class BoxedCode : public Box {
public: public:
std::unique_ptr<SourceInfo> source; // source can be NULL for functions defined in the C/C++ runtime std::unique_ptr<SourceInfo> source; // source can be NULL for functions defined in the C/C++ runtime
const ConstantVRegInfo BORROWED(constant_vregs); // keeps track of all constants inside the bytecode const CodeConstants BORROWED(code_constants); // keeps track of all constants inside the bytecode
BoxedString* filename = nullptr; BoxedString* filename = nullptr;
BoxedString* name = nullptr; BoxedString* name = nullptr;
...@@ -1135,8 +1157,7 @@ public: ...@@ -1135,8 +1157,7 @@ public:
// Constructor for Python code objects: // Constructor for Python code objects:
BoxedCode(int num_args, bool takes_varargs, bool takes_kwargs, int firstlineno, std::unique_ptr<SourceInfo> source, BoxedCode(int num_args, bool takes_varargs, bool takes_kwargs, int firstlineno, std::unique_ptr<SourceInfo> source,
ConstantVRegInfo constant_vregs, ParamNames param_names, BoxedString* filename, BoxedString* name, CodeConstants code_constants, ParamNames param_names, BoxedString* filename, BoxedString* name, Box* doc);
Box* doc);
// Constructor for code objects created by the runtime: // Constructor for code objects created by the runtime:
BoxedCode(int num_args, bool takes_varargs, bool takes_kwargs, const char* name, const char* doc = "", BoxedCode(int num_args, bool takes_varargs, bool takes_kwargs, const char* name, const char* doc = "",
......
...@@ -254,7 +254,7 @@ extern "C" void dumpEx(void* p, int levels) { ...@@ -254,7 +254,7 @@ extern "C" void dumpEx(void* p, int levels) {
printf("Defined at %s:%d\n", code->filename->c_str(), code->firstlineno); printf("Defined at %s:%d\n", code->filename->c_str(), code->firstlineno);
if (code->source->cfg && levels > 0) { if (code->source->cfg && levels > 0) {
code->source->cfg->print(code->constant_vregs); code->source->cfg->print(code->code_constants);
} }
} else { } else {
printf("A builtin function\n"); printf("A builtin function\n");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment