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:
void setattr(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var, BoxedString* attr,
CompilerVariable* v) override {
llvm::Constant* ptr = embedRelocatablePtr(attr, g.llvm_boxedstring_type_ptr);
emitter.setType(ptr, RefType::BORROWED);
ConcreteCompilerVariable* converted = v->makeConverted(emitter, UNKNOWN);
// g.funcs.setattr->dump();
// var->getValue()->dump(); llvm::errs() << '\n';
// ptr->dump(); llvm::errs() << '\n';
// converted->getValue()->dump(); llvm::errs() << '\n';
bool do_patchpoint = ENABLE_ICSETATTRS;
llvm::Instruction* inst;
if (do_patchpoint) {
ICSetupInfo* pp = createSetattrIC(info.getTypeRecorder(), info.getBJitICInfo());
......@@ -245,10 +247,11 @@ public:
llvm_args.push_back(ptr);
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 {
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 {
......@@ -456,6 +459,7 @@ public:
rtn = emitter.createCall3(info.unw_info, rt_func, var->getValue(), converted_rhs->getValue(),
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
|| op_type == AST_TYPE::IsNot) {
......@@ -788,6 +792,7 @@ CompilerVariable* makeFunction(IREmitter& emitter, FunctionMetadata* f, Compiler
closure_v = convertedClosure->getValue();
} else {
closure_v = getNullPtr(g.llvm_closure_type_ptr);
emitter.setType(closure_v, RefType::BORROWED);
}
llvm::Value* scratch;
......@@ -813,6 +818,7 @@ CompilerVariable* makeFunction(IREmitter& emitter, FunctionMetadata* f, Compiler
g.funcs.createFunctionFromMetadata,
std::vector<llvm::Value*>{ embedRelocatablePtr(f, g.llvm_functionmetadata_type_ptr), closure_v, globals,
scratch, getConstantInt(defaults.size(), g.i64) });
emitter.setType(boxed, RefType::OWNED);
return new ConcreteCompilerVariable(typeFromClass(function_cls), boxed);
}
......@@ -2201,6 +2207,7 @@ public:
ConcreteCompilerVariable* makeConverted(IREmitter& emitter, VAR* var, ConcreteCompilerType* other_type) override {
assert(other_type == STR || other_type == UNKNOWN);
llvm::Value* boxed = embedRelocatablePtr(var->getValue(), g.llvm_value_type_ptr);
emitter.setType(boxed, RefType::BORROWED);
return new ConcreteCompilerVariable(other_type, boxed);
}
......
......@@ -86,20 +86,20 @@ public:
virtual llvm::Function* getIntrinsic(llvm::Intrinsic::ID) = 0;
virtual llvm::Value* createCall(const UnwindInfo& unw_info, llvm::Value* callee,
const std::vector<llvm::Value*>& args, ExceptionStyle target_exception_style = CXX)
= 0;
virtual llvm::Value* createCall(const UnwindInfo& unw_info, llvm::Value* callee,
ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Value* createCall(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1,
ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Value* createCall2(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1,
llvm::Value* arg2, ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Value* createCall3(const UnwindInfo& unw_info, llvm::Value* callee, llvm::Value* arg1,
llvm::Value* arg2, llvm::Value* arg3, ExceptionStyle target_exception_style = CXX)
= 0;
virtual llvm::Value* createIC(const ICSetupInfo* pp, void* func_addr, const std::vector<llvm::Value*>& args,
const UnwindInfo& unw_info, ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Instruction* createCall(const UnwindInfo& unw_info, llvm::Value* callee,
const std::vector<llvm::Value*>& args,
ExceptionStyle target_exception_style = CXX) = 0;
virtual llvm::Instruction* createCall(const UnwindInfo& unw_info, llvm::Value* callee,
ExceptionStyle target_exception_style = CXX) = 0;
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;
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) = 0;
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;
virtual void checkAndPropagateCapiException(const UnwindInfo& unw_info, llvm::Value* returned_val,
llvm::Value* exc_val, bool double_check = false) = 0;
......@@ -108,6 +108,7 @@ public:
virtual BORROWED(Box*) getFloatConstant(double d) = 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;
};
......
......@@ -517,6 +517,10 @@ public:
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) {
irstate->getRefcounts()->setType(v, reftype);
return v;
......@@ -1075,12 +1079,15 @@ private:
ConcreteCompilerVariable* getEllipsis() {
llvm::Constant* ellipsis = embedRelocatablePtr(Ellipsis, g.llvm_value_type_ptr, "cEllipsis");
auto ellipsis_cls = Ellipsis->cls;
emitter.setType(ellipsis, RefType::BORROWED);
return new ConcreteCompilerVariable(typeFromClass(ellipsis_cls), ellipsis);
}
llvm::Constant* embedParentModulePtr() {
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) {
......@@ -1277,6 +1284,7 @@ private:
llvm::Value* rtn
= embedRelocatablePtr(irstate->getSourceInfo()->parent_module->getStringConstant(node->str_data, true),
g.llvm_value_type_ptr);
emitter.setType(rtn, RefType::BORROWED);
return new ConcreteCompilerVariable(STR, rtn);
} else if (node->str_type == AST_Str::UNICODE) {
......@@ -2571,28 +2579,34 @@ public:
if (scope_info->takesClosure()) {
passed_closure = AI;
emitter.setType(passed_closure, RefType::BORROWED);
symbol_table[internString(PASSED_CLOSURE_NAME)]
= new ConcreteCompilerVariable(getPassedClosureType(), AI);
++AI;
}
if (scope_info->createsClosure()) {
if (!passed_closure)
if (!passed_closure) {
passed_closure = getNullPtr(g.llvm_closure_type_ptr);
emitter.setType(passed_closure, RefType::BORROWED);
}
llvm::Value* new_closure = emitter.getBuilder()->CreateCall2(
g.funcs.createClosure, passed_closure, getConstantInt(scope_info->getClosureSize(), g.i64));
emitter.setType(new_closure, RefType::OWNED);
symbol_table[internString(CREATED_CLOSURE_NAME)]
= new ConcreteCompilerVariable(getCreatedClosureType(), new_closure);
}
if (irstate->getSourceInfo()->is_generator) {
symbol_table[internString(PASSED_GENERATOR_NAME)] = new ConcreteCompilerVariable(GENERATOR, AI);
emitter.setType(AI, RefType::BORROWED);
++AI;
}
if (!irstate->getSourceInfo()->scoping->areGlobalsFromModule()) {
irstate->setGlobals(AI);
emitter.setType(AI, RefType::BORROWED);
++AI;
}
......@@ -2607,8 +2621,10 @@ public:
if (arg_types[i]->llvmType() == g.i64)
loaded = emitter.getBuilder()->CreatePtrToInt(loaded, arg_types[i]->llvmType());
else
else {
assert(arg_types[i]->llvmType() == g.llvm_value_type_ptr);
emitter.setType(loaded, RefType::BORROWED);
}
python_parameters.push_back(loaded);
}
......@@ -2617,6 +2633,7 @@ public:
}
python_parameters.push_back(AI);
emitter.setType(AI, RefType::BORROWED);
++AI;
}
......
......@@ -239,8 +239,6 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
for (auto&& BB : *f) {
if (llvm::succ_begin(&BB) == llvm::succ_end(&BB)) {
printf("%s is a terminator block\n", BB.getName().data());
block_queue.push_back(&BB);
}
}
......@@ -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())) {
llvm::DenseMap<llvm::Value*, int> num_consumed_by_inst;
llvm::DenseMap<llvm::Value*, int> num_times_as_op;
......@@ -344,13 +345,8 @@ void RefcountTracker::addRefcounts(IRGenState* irstate) {
if (num_times_as_op[op] > num_consumed) {
if (rt->vars[op].reftype == RefType::OWNED) {
if (state.refs[op] == 0) {
if (llvm::InvokeInst* invoke = llvm::dyn_cast<llvm::InvokeInst>(&I)) {
addDecrefs(op, 1, findIncrefPt(invoke->getNormalDest()));
addDecrefs(op, 1, findIncrefPt(invoke->getUnwindDest()));
} else {
assert(&I != I.getParent()->getTerminator());
addDecrefs(op, 1, I.getNextNode());
}
// Don't do any updates now since we are iterating over the bb
last_uses.push_back(std::make_pair(op, &I));
state.refs[op] = 1;
}
}
......@@ -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()) {
for (auto&& p : state.refs) {
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);
addIncrefs(p.first, p.second, findIncrefPt(&BB));
}
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))) {
bool all_succ_done = true;
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
assert(!relocatable_syms.count(name));
name = shared_name;
} 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;
......
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