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:
ExprTypeMap& expr_types;
TypeSpeculations& type_speculations;
TypeAnalysis::SpeculationLevel speculation;
const ConstantVRegInfo& constant_vregs;
const CodeConstants& code_constants;
BasicBlockTypePropagator(CFGBlock* block, TypeMap& initial, ExprTypeMap& expr_types,
TypeSpeculations& type_speculations, TypeAnalysis::SpeculationLevel speculation,
const ConstantVRegInfo& constant_vregs)
const CodeConstants& code_constants)
: block(block),
sym_table(initial),
expr_types(expr_types),
type_speculations(type_speculations),
speculation(speculation),
constant_vregs(constant_vregs) {}
code_constants(code_constants) {}
void run() {
for (int i = 0; i < block->body.size(); i++) {
......@@ -118,7 +118,7 @@ private:
printf("in propagator, speculating that %s would actually be %s, at ",
old_type->debugName().c_str(), speculated_type->debugName().c_str());
fflush(stdout);
print_bst(node, constant_vregs);
print_bst(node, code_constants);
llvm::outs().flush();
printf("\n");
}
......@@ -131,7 +131,7 @@ private:
}
CompilerType* getConstantType(int vreg) {
Box* o = constant_vregs.getConstant(vreg);
Box* o = code_constants.getConstant(vreg);
if (o->cls == int_cls)
return INT;
else if (o->cls == float_cls)
......@@ -284,7 +284,7 @@ private:
if (VERBOSITY() >= 2 && func == UNDEF) {
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");
}
......@@ -297,7 +297,7 @@ private:
if (VERBOSITY() >= 2 && func == UNDEF) {
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");
}
......@@ -399,7 +399,7 @@ private:
if (VERBOSITY() >= 2 && rtn == UNDEF) {
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");
}
_doSet(node->vreg_dst, rtn);
......@@ -551,9 +551,9 @@ private:
public:
static TypeMap propagate(CFGBlock* block, const TypeMap& starting, ExprTypeMap& expr_types,
TypeSpeculations& type_speculations, TypeAnalysis::SpeculationLevel speculation,
const ConstantVRegInfo& constant_vregs) {
const CodeConstants& code_constants) {
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;
}
};
......@@ -632,7 +632,7 @@ public:
}
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()");
CFG* cfg = initial_block->cfg;
......@@ -673,7 +673,7 @@ public:
}
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) {
printf("before (after):\n");
......@@ -730,7 +730,7 @@ public:
// public entry point:
TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& arg_names, const std::vector<ConcreteCompilerType*>& arg_types,
EffortLevel effort, TypeAnalysis::SpeculationLevel speculation,
const ConstantVRegInfo& constant_vregs) {
const CodeConstants& code_constants) {
// if (effort == EffortLevel::INTERPRETED) {
// return new NullTypeAnalysis();
//}
......@@ -750,11 +750,11 @@ TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& arg_names, const std::v
assert(i == arg_types.size());
return PropagatingTypeAnalysis::doAnalysis(speculation, std::move(initial_types), cfg->getStartingBlock(),
constant_vregs);
code_constants);
}
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&& vreg_info = cfg->getVRegInfo();
TypeMap initial_types(vreg_info.getTotalNumOfVRegs());
......@@ -764,6 +764,6 @@ TypeAnalysis* doTypeAnalysis(const OSREntryDescriptor* entry_descriptor, EffortL
}
return PropagatingTypeAnalysis::doAnalysis(speculation, std::move(initial_types),
entry_descriptor->backedge->target, constant_vregs);
entry_descriptor->backedge->target, code_constants);
}
}
......@@ -24,7 +24,7 @@
namespace pyston {
class CFGBlock;
class ConstantVRegInfo;
class CodeConstants;
class BoxedClass;
class BST_stmt_with_dest;
class OSREntryDescriptor;
......@@ -45,9 +45,9 @@ public:
TypeAnalysis* doTypeAnalysis(CFG* cfg, const ParamNames& param_names,
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::SpeculationLevel speculation, const ConstantVRegInfo& constant_vregs);
TypeAnalysis::SpeculationLevel speculation, const CodeConstants& code_constants);
}
#endif
......@@ -181,7 +181,7 @@ public:
BoxedClosure* getPassedClosure() { return frame_info.passed_closure; }
Box** getVRegs() { return vregs; }
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 setGenerator(Box* gen);
......@@ -900,7 +900,7 @@ Value ASTInterpreter::visit_stmt(BST_stmt* node) {
if (0) {
printf("%20s % 2d ", getCode()->name->c_str(), current_block->idx);
print_bst(node, getConstantVRegInfo());
print_bst(node, getCodeConstants());
printf("\n");
}
......@@ -1528,7 +1528,7 @@ Value ASTInterpreter::visit_set(BST_Set* node) {
Value ASTInterpreter::getVReg(int vreg, bool is_kill) {
assert(vreg != VREG_UNDEFINED);
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);
}
......@@ -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("\n\n");
current_block->cfg->print(getConstantVRegInfo());
current_block->cfg->print(getCodeConstants());
assert(0);
return Value();
......
......@@ -1027,7 +1027,7 @@ std::pair<CompiledFunction*, llvm::Function*> doCompile(BoxedCode* code, SourceI
assert((entry_descriptor != NULL) + (spec != NULL) == 1);
if (VERBOSITY("irgen") >= 2)
source->cfg->print(code->constant_vregs);
source->cfg->print(code->code_constants);
assert(g.cur_module == NULL);
......@@ -1101,10 +1101,10 @@ std::pair<CompiledFunction*, llvm::Function*> doCompile(BoxedCode* code, SourceI
speculation_level = TypeAnalysis::SOME;
TypeAnalysis* types;
if (entry_descriptor)
types = doTypeAnalysis(entry_descriptor, effort, speculation_level, code->constant_vregs);
types = doTypeAnalysis(entry_descriptor, effort, speculation_level, code->code_constants);
else
types = doTypeAnalysis(source->cfg, *param_names, spec->arg_types, effort, speculation_level,
code->constant_vregs);
code->code_constants);
_t2.split();
......
......@@ -694,6 +694,9 @@ void BoxedCode::addVersion(void* f, ConcreteCompilerType* rtn_type, const std::v
}
bool BoxedCode::tryDeallocatingTheBJitCode() {
if (code_blocks.empty())
return true;
// we can only delete the code object if we are not executing it currently
assert(bjit_num_inside >= 0);
if (bjit_num_inside != 0) {
......
......@@ -69,6 +69,10 @@ IRGenState::IRGenState(BoxedCode* code, CompiledFunction* cf, llvm::Function* fu
IRGenState::~IRGenState() {
}
const CodeConstants& IRGenState::getCodeConstants() {
return code->code_constants;
}
llvm::Value* IRGenState::getPassedClosure() {
assert(getScopeInfo().takesClosure());
assert(passed_closure);
......@@ -721,9 +725,9 @@ public:
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 {
irstate->getRefcounts()->refConsumed(v, inst);
......@@ -1225,7 +1229,7 @@ private:
CompilerVariable* evalVReg(int vreg, bool is_kill = true) {
assert(vreg != VREG_UNDEFINED);
if (vreg < 0) {
Box* o = irstate->getCode()->constant_vregs.getConstant(vreg);
Box* o = irstate->getCode()->code_constants.getConstant(vreg);
if (o->cls == int_cls) {
return makeInt(((BoxedInt*)o)->n);
} else if (o->cls == float_cls) {
......@@ -1612,7 +1616,7 @@ private:
printf("Speculating that %s is actually %s, at ", rtn->getType()->debugName().c_str(),
speculated_type->debugName().c_str());
fflush(stdout);
print_bst(node, irstate->getCode()->constant_vregs);
print_bst(node, irstate->getCode()->code_constants);
llvm::outs().flush();
printf("\n");
}
......@@ -1624,7 +1628,7 @@ private:
auto source = irstate->getSourceInfo();
printf("On %s:%d, function %s:\n", irstate->getCode()->filename->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(),
speculated_type->debugName().c_str());
......
......@@ -92,6 +92,7 @@ public:
CompiledFunction* getCurFunction() { return cf; }
BoxedCode* getCode() { return code; }
const CodeConstants& getCodeConstants();
ExceptionStyle getExceptionStyle() { return cf->exception_style; }
......
......@@ -754,8 +754,8 @@ void BST_MakeSlice::accept_stmt(StmtVisitor* v) {
return v->visit_makeslice(this);
}
void print_bst(BST_stmt* bst, const ConstantVRegInfo& constant_vregs) {
PrintVisitor v(constant_vregs, 0, llvm::outs());
void print_bst(BST_stmt* bst, const CodeConstants& code_constants) {
PrintVisitor v(code_constants, 0, llvm::outs());
bst->accept(&v);
v.flush();
}
......@@ -771,7 +771,7 @@ bool PrintVisitor::visit_vreg(int* vreg, bool is_dst) {
if (*vreg != VREG_UNDEFINED) {
stream << "%" << *vreg;
if (*vreg < 0)
stream << "|" << autoDecref(repr(constant_vregs.getConstant(*vreg)))->s() << "|";
stream << "|" << autoDecref(repr(code_constants.getConstant(*vreg)))->s() << "|";
} else
stream << "%undef";
......
This diff is collapsed.
This diff is collapsed.
......@@ -88,7 +88,7 @@ public:
void unconnectFrom(CFGBlock* successor);
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.
......@@ -210,7 +210,7 @@ public:
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 {
......@@ -331,7 +331,7 @@ public:
BoxedCode* computeAllCFGs(AST* ast, bool globals_from_module, FutureFlags future_flags, BoxedString* fn,
BoxedModule* bm);
void printCFG(CFG* cfg, const ConstantVRegInfo& constant_vregs);
void printCFG(CFG* cfg, const CodeConstants& code_constants);
}
#endif
......@@ -107,16 +107,18 @@ void BoxedCode::dealloc(Box* b) noexcept {
Py_XDECREF(o->name);
Py_XDECREF(o->_doc);
o->tryDeallocatingTheBJitCode();
o->source.reset(nullptr);
o->~BoxedCode();
o->cls->tp_free(o);
}
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)
: source(std::move(source)),
constant_vregs(std::move(constant_vregs)),
code_constants(std::move(code_constants)),
filename(incref(filename)),
name(incref(name)),
firstlineno(firstlineno),
......
......@@ -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 /
// finally-blocks in scope.
extern "C" Box* createFunctionFromMetadata(BoxedCode* code, BoxedClosure* closure, Box* globals,
......@@ -3817,21 +3755,12 @@ void BoxedModule::dealloc(Box* b) noexcept {
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);
}
int BoxedModule::traverse(Box* _m, visitproc visit, void* arg) noexcept {
BoxedModule* m = static_cast<BoxedModule*>(_m);
Py_TRAVERSE(m->attrs);
assert(!m->keep_alive.size());
return 0;
}
......@@ -3849,15 +3778,6 @@ extern "C" void _PyModule_Clear(PyObject* b) noexcept {
HCAttrs* attrs = self->getHCAttrsPtr();
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 {
......@@ -4102,6 +4022,33 @@ int BoxedClosure::clear(Box* _o) noexcept {
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
#define PRINT_TOTAL_REFS()
#else /* Py_REF_DEBUG */
......
......@@ -1070,20 +1070,42 @@ public:
};
static_assert(sizeof(BoxedDict) == sizeof(PyDictObject), "");
class ConstantVRegInfo {
class CodeConstants {
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;
// 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:
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)
int addConstant(Box* o) {
int createVRegEntryForConstant(Box* o) {
constants.push_back(o);
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;
};
......@@ -1096,7 +1118,7 @@ public:
class BoxedCode : public Box {
public:
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* name = nullptr;
......@@ -1135,8 +1157,7 @@ public:
// Constructor for Python code objects:
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,
Box* doc);
CodeConstants code_constants, ParamNames param_names, BoxedString* filename, BoxedString* name, Box* doc);
// Constructor for code objects created by the runtime:
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) {
printf("Defined at %s:%d\n", code->filename->c_str(), code->firstlineno);
if (code->source->cfg && levels > 0) {
code->source->cfg->print(code->constant_vregs);
code->source->cfg->print(code->code_constants);
}
} else {
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