Commit 45349e60 authored by Kevin Modzelewski's avatar Kevin Modzelewski

It seems to be able to run some simple cases

parent 12e5d956
...@@ -231,12 +231,14 @@ public: ...@@ -231,12 +231,14 @@ public:
void setattr(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var, BoxedString* attr, void setattr(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var, BoxedString* attr,
CompilerVariable* v) override { CompilerVariable* v) override {
llvm::Constant* ptr = embedRelocatablePtr(attr, g.llvm_boxedstring_type_ptr); llvm::Constant* ptr = embedRelocatablePtr(attr, g.llvm_boxedstring_type_ptr);
emitter.setType(ptr, RefType::BORROWED);
ConcreteCompilerVariable* converted = v->makeConverted(emitter, UNKNOWN); ConcreteCompilerVariable* converted = v->makeConverted(emitter, UNKNOWN);
// g.funcs.setattr->dump(); // g.funcs.setattr->dump();
// var->getValue()->dump(); llvm::errs() << '\n'; // var->getValue()->dump(); llvm::errs() << '\n';
// ptr->dump(); llvm::errs() << '\n'; // ptr->dump(); llvm::errs() << '\n';
// converted->getValue()->dump(); llvm::errs() << '\n'; // converted->getValue()->dump(); llvm::errs() << '\n';
bool do_patchpoint = ENABLE_ICSETATTRS; bool do_patchpoint = ENABLE_ICSETATTRS;
llvm::Instruction* inst;
if (do_patchpoint) { if (do_patchpoint) {
ICSetupInfo* pp = createSetattrIC(info.getTypeRecorder(), info.getBJitICInfo()); ICSetupInfo* pp = createSetattrIC(info.getTypeRecorder(), info.getBJitICInfo());
...@@ -245,10 +247,11 @@ public: ...@@ -245,10 +247,11 @@ public:
llvm_args.push_back(ptr); llvm_args.push_back(ptr);
llvm_args.push_back(converted->getValue()); llvm_args.push_back(converted->getValue());
emitter.createIC(pp, (void*)pyston::setattr, llvm_args, info.unw_info); inst = emitter.createIC(pp, (void*)pyston::setattr, llvm_args, info.unw_info);
} else { } else {
emitter.createCall3(info.unw_info, g.funcs.setattr, var->getValue(), ptr, converted->getValue()); inst = emitter.createCall3(info.unw_info, g.funcs.setattr, var->getValue(), ptr, converted->getValue());
} }
emitter.refConsumed(converted->getValue(), inst);
} }
void delattr(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var, BoxedString* attr) override { void delattr(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var, BoxedString* attr) override {
...@@ -456,6 +459,7 @@ public: ...@@ -456,6 +459,7 @@ public:
rtn = emitter.createCall3(info.unw_info, rt_func, var->getValue(), converted_rhs->getValue(), rtn = emitter.createCall3(info.unw_info, rt_func, var->getValue(), converted_rhs->getValue(),
getConstantInt(op_type, g.i32)); getConstantInt(op_type, g.i32));
} }
emitter.setType(rtn, RefType::OWNED);
if (op_type == AST_TYPE::In || op_type == AST_TYPE::NotIn || op_type == AST_TYPE::Is if (op_type == AST_TYPE::In || op_type == AST_TYPE::NotIn || op_type == AST_TYPE::Is
|| op_type == AST_TYPE::IsNot) { || op_type == AST_TYPE::IsNot) {
...@@ -788,6 +792,7 @@ CompilerVariable* makeFunction(IREmitter& emitter, FunctionMetadata* f, Compiler ...@@ -788,6 +792,7 @@ CompilerVariable* makeFunction(IREmitter& emitter, FunctionMetadata* f, Compiler
closure_v = convertedClosure->getValue(); closure_v = convertedClosure->getValue();
} else { } else {
closure_v = getNullPtr(g.llvm_closure_type_ptr); closure_v = getNullPtr(g.llvm_closure_type_ptr);
emitter.setType(closure_v, RefType::BORROWED);
} }
llvm::Value* scratch; llvm::Value* scratch;
...@@ -813,6 +818,7 @@ CompilerVariable* makeFunction(IREmitter& emitter, FunctionMetadata* f, Compiler ...@@ -813,6 +818,7 @@ CompilerVariable* makeFunction(IREmitter& emitter, FunctionMetadata* f, Compiler
g.funcs.createFunctionFromMetadata, g.funcs.createFunctionFromMetadata,
std::vector<llvm::Value*>{ embedRelocatablePtr(f, g.llvm_functionmetadata_type_ptr), closure_v, globals, std::vector<llvm::Value*>{ embedRelocatablePtr(f, g.llvm_functionmetadata_type_ptr), closure_v, globals,
scratch, getConstantInt(defaults.size(), g.i64) }); scratch, getConstantInt(defaults.size(), g.i64) });
emitter.setType(boxed, RefType::OWNED);
return new ConcreteCompilerVariable(typeFromClass(function_cls), boxed); return new ConcreteCompilerVariable(typeFromClass(function_cls), boxed);
} }
...@@ -2201,6 +2207,7 @@ public: ...@@ -2201,6 +2207,7 @@ public:
ConcreteCompilerVariable* makeConverted(IREmitter& emitter, VAR* var, ConcreteCompilerType* other_type) override { ConcreteCompilerVariable* makeConverted(IREmitter& emitter, VAR* var, ConcreteCompilerType* other_type) override {
assert(other_type == STR || other_type == UNKNOWN); assert(other_type == STR || other_type == UNKNOWN);
llvm::Value* boxed = embedRelocatablePtr(var->getValue(), g.llvm_value_type_ptr); llvm::Value* boxed = embedRelocatablePtr(var->getValue(), g.llvm_value_type_ptr);
emitter.setType(boxed, RefType::BORROWED);
return new ConcreteCompilerVariable(other_type, boxed); return new ConcreteCompilerVariable(other_type, boxed);
} }
......
...@@ -86,19 +86,19 @@ public: ...@@ -86,19 +86,19 @@ public:
virtual llvm::Function* getIntrinsic(llvm::Intrinsic::ID) = 0; virtual llvm::Function* getIntrinsic(llvm::Intrinsic::ID) = 0;
virtual llvm::Value* createCall(const UnwindInfo& unw_info, llvm::Value* callee, virtual llvm::Instruction* createCall(const UnwindInfo& unw_info, llvm::Value* callee,
const std::vector<llvm::Value*>& args, ExceptionStyle target_exception_style = CXX) const std::vector<llvm::Value*>& args,
= 0;
virtual llvm::Value* createCall(const UnwindInfo& unw_info, llvm::Value* callee,
ExceptionStyle target_exception_style = CXX) = 0; ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Value* createCall(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1, virtual llvm::Instruction* createCall(const UnwindInfo& unw_info, llvm::Value* callee,
ExceptionStyle target_exception_style = CXX) = 0; ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Value* createCall2(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1, virtual llvm::Instruction* createCall(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1,
ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Instruction* createCall2(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1,
llvm::Value* arg2, ExceptionStyle target_exception_style = CXX) = 0; llvm::Value* arg2, ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Value* createCall3(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1, virtual llvm::Instruction* createCall3(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1,
llvm::Value* arg2, llvm::Value* arg3, ExceptionStyle target_exception_style = CXX) llvm::Value* arg2, llvm::Value* arg3,
= 0; ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Value* createIC(const ICSetupInfo* pp, void* func_addr, const std::vector<llvm::Value*>& args, virtual llvm::Instruction* createIC(const ICSetupInfo* pp, void* func_addr, const std::vector<llvm::Value*>& args,
const UnwindInfo& unw_info, ExceptionStyle target_exception_style = CXX) = 0; const UnwindInfo& unw_info, ExceptionStyle target_exception_style = CXX) = 0;
virtual void checkAndPropagateCapiException(const UnwindInfo& unw_info, llvm::Value* returned_val, virtual void checkAndPropagateCapiException(const UnwindInfo& unw_info, llvm::Value* returned_val,
...@@ -108,6 +108,7 @@ public: ...@@ -108,6 +108,7 @@ public:
virtual BORROWED(Box*) getFloatConstant(double d) = 0; virtual BORROWED(Box*) getFloatConstant(double d) = 0;
virtual llvm::Value* setType(llvm::Value* v, RefType reftype) = 0; virtual llvm::Value* setType(llvm::Value* v, RefType reftype) = 0;
virtual void refConsumed(llvm::Value* v, llvm::Instruction* inst) = 0;
virtual ConcreteCompilerVariable* getNone() = 0; virtual ConcreteCompilerVariable* getNone() = 0;
}; };
......
...@@ -517,6 +517,10 @@ public: ...@@ -517,6 +517,10 @@ public:
return irstate->getSourceInfo()->parent_module->getFloatConstant(d); return irstate->getSourceInfo()->parent_module->getFloatConstant(d);
} }
void refConsumed(llvm::Value* v, llvm::Instruction* inst) {
irstate->getRefcounts()->refConsumed(v, inst);
}
llvm::Value* setType(llvm::Value* v, RefType reftype) { llvm::Value* setType(llvm::Value* v, RefType reftype) {
irstate->getRefcounts()->setType(v, reftype); irstate->getRefcounts()->setType(v, reftype);
return v; return v;
...@@ -1075,12 +1079,15 @@ private: ...@@ -1075,12 +1079,15 @@ private:
ConcreteCompilerVariable* getEllipsis() { ConcreteCompilerVariable* getEllipsis() {
llvm::Constant* ellipsis = embedRelocatablePtr(Ellipsis, g.llvm_value_type_ptr, "cEllipsis"); llvm::Constant* ellipsis = embedRelocatablePtr(Ellipsis, g.llvm_value_type_ptr, "cEllipsis");
auto ellipsis_cls = Ellipsis->cls; auto ellipsis_cls = Ellipsis->cls;
emitter.setType(ellipsis, RefType::BORROWED);
return new ConcreteCompilerVariable(typeFromClass(ellipsis_cls), ellipsis); return new ConcreteCompilerVariable(typeFromClass(ellipsis_cls), ellipsis);
} }
llvm::Constant* embedParentModulePtr() { llvm::Constant* embedParentModulePtr() {
BoxedModule* parent_module = irstate->getSourceInfo()->parent_module; BoxedModule* parent_module = irstate->getSourceInfo()->parent_module;
return embedRelocatablePtr(parent_module, g.llvm_value_type_ptr, "cParentModule"); auto r = embedRelocatablePtr(parent_module, g.llvm_value_type_ptr, "cParentModule");
emitter.setType(r, RefType::BORROWED);
return r;
} }
ConcreteCompilerVariable* _getGlobal(AST_Name* node, const UnwindInfo& unw_info) { ConcreteCompilerVariable* _getGlobal(AST_Name* node, const UnwindInfo& unw_info) {
...@@ -1277,6 +1284,7 @@ private: ...@@ -1277,6 +1284,7 @@ private:
llvm::Value* rtn llvm::Value* rtn
= embedRelocatablePtr(irstate->getSourceInfo()->parent_module->getStringConstant(node->str_data, true), = embedRelocatablePtr(irstate->getSourceInfo()->parent_module->getStringConstant(node->str_data, true),
g.llvm_value_type_ptr); g.llvm_value_type_ptr);
emitter.setType(rtn, RefType::BORROWED);
return new ConcreteCompilerVariable(STR, rtn); return new ConcreteCompilerVariable(STR, rtn);
} else if (node->str_type == AST_Str::UNICODE) { } else if (node->str_type == AST_Str::UNICODE) {
...@@ -2571,28 +2579,34 @@ public: ...@@ -2571,28 +2579,34 @@ public:
if (scope_info->takesClosure()) { if (scope_info->takesClosure()) {
passed_closure = AI; passed_closure = AI;
emitter.setType(passed_closure, RefType::BORROWED);
symbol_table[internString(PASSED_CLOSURE_NAME)] symbol_table[internString(PASSED_CLOSURE_NAME)]
= new ConcreteCompilerVariable(getPassedClosureType(), AI); = new ConcreteCompilerVariable(getPassedClosureType(), AI);
++AI; ++AI;
} }
if (scope_info->createsClosure()) { if (scope_info->createsClosure()) {
if (!passed_closure) if (!passed_closure) {
passed_closure = getNullPtr(g.llvm_closure_type_ptr); passed_closure = getNullPtr(g.llvm_closure_type_ptr);
emitter.setType(passed_closure, RefType::BORROWED);
}
llvm::Value* new_closure = emitter.getBuilder()->CreateCall2( llvm::Value* new_closure = emitter.getBuilder()->CreateCall2(
g.funcs.createClosure, passed_closure, getConstantInt(scope_info->getClosureSize(), g.i64)); g.funcs.createClosure, passed_closure, getConstantInt(scope_info->getClosureSize(), g.i64));
emitter.setType(new_closure, RefType::OWNED);
symbol_table[internString(CREATED_CLOSURE_NAME)] symbol_table[internString(CREATED_CLOSURE_NAME)]
= new ConcreteCompilerVariable(getCreatedClosureType(), new_closure); = new ConcreteCompilerVariable(getCreatedClosureType(), new_closure);
} }
if (irstate->getSourceInfo()->is_generator) { if (irstate->getSourceInfo()->is_generator) {
symbol_table[internString(PASSED_GENERATOR_NAME)] = new ConcreteCompilerVariable(GENERATOR, AI); symbol_table[internString(PASSED_GENERATOR_NAME)] = new ConcreteCompilerVariable(GENERATOR, AI);
emitter.setType(AI, RefType::BORROWED);
++AI; ++AI;
} }
if (!irstate->getSourceInfo()->scoping->areGlobalsFromModule()) { if (!irstate->getSourceInfo()->scoping->areGlobalsFromModule()) {
irstate->setGlobals(AI); irstate->setGlobals(AI);
emitter.setType(AI, RefType::BORROWED);
++AI; ++AI;
} }
...@@ -2607,8 +2621,10 @@ public: ...@@ -2607,8 +2621,10 @@ public:
if (arg_types[i]->llvmType() == g.i64) if (arg_types[i]->llvmType() == g.i64)
loaded = emitter.getBuilder()->CreatePtrToInt(loaded, arg_types[i]->llvmType()); loaded = emitter.getBuilder()->CreatePtrToInt(loaded, arg_types[i]->llvmType());
else else {
assert(arg_types[i]->llvmType() == g.llvm_value_type_ptr); assert(arg_types[i]->llvmType() == g.llvm_value_type_ptr);
emitter.setType(loaded, RefType::BORROWED);
}
python_parameters.push_back(loaded); python_parameters.push_back(loaded);
} }
...@@ -2617,6 +2633,7 @@ public: ...@@ -2617,6 +2633,7 @@ public:
} }
python_parameters.push_back(AI); python_parameters.push_back(AI);
emitter.setType(AI, RefType::BORROWED);
++AI; ++AI;
} }
......
...@@ -239,8 +239,6 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -239,8 +239,6 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
for (auto&& BB : *f) { for (auto&& BB : *f) {
if (llvm::succ_begin(&BB) == llvm::succ_end(&BB)) { if (llvm::succ_begin(&BB) == llvm::succ_end(&BB)) {
printf("%s is a terminator block\n", BB.getName().data());
block_queue.push_back(&BB); block_queue.push_back(&BB);
} }
} }
...@@ -311,6 +309,9 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -311,6 +309,9 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
} }
} }
// A place to store any decrefs we might have to do, since those will split the basic block:
llvm::SmallVector<std::pair<llvm::Value*, llvm::Instruction*>, 4> last_uses;
for (auto &I : llvm::iterator_range<llvm::BasicBlock::reverse_iterator>(BB.rbegin(), BB.rend())) { for (auto &I : llvm::iterator_range<llvm::BasicBlock::reverse_iterator>(BB.rbegin(), BB.rend())) {
llvm::DenseMap<llvm::Value*, int> num_consumed_by_inst; llvm::DenseMap<llvm::Value*, int> num_consumed_by_inst;
llvm::DenseMap<llvm::Value*, int> num_times_as_op; llvm::DenseMap<llvm::Value*, int> num_times_as_op;
...@@ -344,13 +345,8 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -344,13 +345,8 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
if (num_times_as_op[op] > num_consumed) { if (num_times_as_op[op] > num_consumed) {
if (rt->vars[op].reftype == RefType::OWNED) { if (rt->vars[op].reftype == RefType::OWNED) {
if (state.refs[op] == 0) { if (state.refs[op] == 0) {
if (llvm::InvokeInst* invoke = llvm::dyn_cast<llvm::InvokeInst>(&I)) { // Don't do any updates now since we are iterating over the bb
addDecrefs(op, 1, findIncrefPt(invoke->getNormalDest())); last_uses.push_back(std::make_pair(op, &I));
addDecrefs(op, 1, findIncrefPt(invoke->getUnwindDest()));
} else {
assert(&I != I.getParent()->getTerminator());
addDecrefs(op, 1, I.getNextNode());
}
state.refs[op] = 1; state.refs[op] = 1;
} }
} }
...@@ -380,22 +376,40 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) { ...@@ -380,22 +376,40 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
} }
} }
for (auto& p : last_uses) {
if (llvm::InvokeInst* invoke = llvm::dyn_cast<llvm::InvokeInst>(p.second)) {
addDecrefs(p.first, 1, findIncrefPt(invoke->getNormalDest()));
addDecrefs(p.first, 1, findIncrefPt(invoke->getUnwindDest()));
} else {
assert(p.second != p.second->getParent()->getTerminator());
addDecrefs(p.first, 1, p.second->getNextNode());
}
}
if (&BB == &BB.getParent()->front()) { if (&BB == &BB.getParent()->front()) {
for (auto&& p : state.refs) { for (auto&& p : state.refs) {
llvm::outs() << *p.first << " " << p.second << '\n'; llvm::outs() << *p.first << " " << p.second << '\n';
assert(llvm::isa<llvm::GlobalVariable>(p.first));
// Anything left should either be an argument or a global variable
#ifndef NDEBUG
if (!llvm::isa<llvm::GlobalVariable>(p.first)) {
bool found = false;
for (auto&& arg : f->args()) {
if (&arg == p.first) {
found = true;
break;
}
}
assert(found);
}
#endif
assert(rt->vars[p.first].reftype == RefType::BORROWED); assert(rt->vars[p.first].reftype == RefType::BORROWED);
addIncrefs(p.first, p.second, findIncrefPt(&BB)); addIncrefs(p.first, p.second, findIncrefPt(&BB));
} }
state.refs.clear(); state.refs.clear();
} }
llvm::outs() << "End of " << BB.getName() << ":\n";
for (auto&& p : state.refs) {
llvm::outs() << "With " << p.second << " refs: " << *p.first << '\n';
}
llvm::outs() << '\n';
for (auto&& PBB : llvm::iterator_range<llvm::pred_iterator>(llvm::pred_begin(&BB), llvm::pred_end(&BB))) { for (auto&& PBB : llvm::iterator_range<llvm::pred_iterator>(llvm::pred_begin(&BB), llvm::pred_end(&BB))) {
bool all_succ_done = true; bool all_succ_done = true;
for (auto&& SBB : llvm::iterator_range<llvm::succ_iterator>(llvm::succ_begin(PBB), llvm::succ_end(PBB))) { for (auto&& SBB : llvm::iterator_range<llvm::succ_iterator>(llvm::succ_begin(PBB), llvm::succ_end(PBB))) {
......
...@@ -132,7 +132,8 @@ llvm::Constant* embedRelocatablePtr(const void* addr, llvm::Type* type, llvm::St ...@@ -132,7 +132,8 @@ llvm::Constant* embedRelocatablePtr(const void* addr, llvm::Type* type, llvm::St
assert(!relocatable_syms.count(name)); assert(!relocatable_syms.count(name));
name = shared_name; name = shared_name;
} else { } else {
name = (llvm::Twine("c") + llvm::Twine(relocatable_syms.size())).str(); int nsyms = relocatable_syms.size();
name = (llvm::Twine("c") + llvm::Twine(nsyms)).str();
} }
relocatable_syms[name] = addr; relocatable_syms[name] = addr;
......
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