Commit 2f7d52be authored by Kevin Modzelewski's avatar Kevin Modzelewski

Change how the llvm jit passes exceptions between blocks

Our IR doesn't explicitly represent the data transfer between an Invoke
statement and its corresponding LandingPad.  We use a couple different
techniques to pass it through: in the ast interpreter/bjit, we stash it
into an interpreter-local variable and then pull it back out.  Previous
to this change, in the LLVM tier we would pass it directly through
an exception, using either the C++ or CAPI exception-passing mechanism.

This works but is a pain, since it requires coordination between the invoke
and the landingpad.  These live in different basic blocks, so we ended up
having this other code that lives separate from the normal irgen that has
to decide which exception style to use, and it has to respect certain
restrictions within irgen (ie it has to be careful to not request CAPI
exceptions for cases that we haven't added support for yet).

This commit changes the approach so that the exception data is passed
directly as LLVM Values, and its up to the Invoke to figure out how
to get it into that form.  This adds a bit more complexity to the invoke,
but it should make the interface easier to extend (such as in the next commit).
parent 31c96c0e
...@@ -337,9 +337,7 @@ public: ...@@ -337,9 +337,7 @@ public:
CompilerVariable* slice) override { CompilerVariable* slice) override {
ConcreteCompilerVariable* converted_slice = slice->makeConverted(emitter, slice->getBoxType()); ConcreteCompilerVariable* converted_slice = slice->makeConverted(emitter, slice->getBoxType());
ExceptionStyle target_exception_style = CXX; ExceptionStyle target_exception_style = info.preferredExceptionStyle();
if (FORCE_LLVM_CAPI || info.unw_info.capi_exc_dest)
target_exception_style = CAPI;
bool do_patchpoint = ENABLE_ICGETITEMS; bool do_patchpoint = ENABLE_ICGETITEMS;
llvm::Value* rtn; llvm::Value* rtn;
...@@ -500,9 +498,7 @@ CompilerVariable* UnknownType::getattr(IREmitter& emitter, const OpInfo& info, C ...@@ -500,9 +498,7 @@ CompilerVariable* UnknownType::getattr(IREmitter& emitter, const OpInfo& info, C
llvm::Value* rtn_val = NULL; llvm::Value* rtn_val = NULL;
ExceptionStyle target_exception_style = CXX; ExceptionStyle target_exception_style = cls_only ? CXX : info.preferredExceptionStyle();
if (info.unw_info.capi_exc_dest || (!cls_only && FORCE_LLVM_CAPI))
target_exception_style = CAPI;
llvm::Value* llvm_func; llvm::Value* llvm_func;
void* raw_func; void* raw_func;
...@@ -656,9 +652,7 @@ CompilerVariable* UnknownType::call(IREmitter& emitter, const OpInfo& info, Conc ...@@ -656,9 +652,7 @@ CompilerVariable* UnknownType::call(IREmitter& emitter, const OpInfo& info, Conc
bool pass_keywords = (argspec.num_keywords != 0); bool pass_keywords = (argspec.num_keywords != 0);
int npassed_args = argspec.totalPassed(); int npassed_args = argspec.totalPassed();
ExceptionStyle exception_style = ((FORCE_LLVM_CAPI && !info.unw_info.cxx_exc_dest) || info.unw_info.capi_exc_dest) ExceptionStyle exception_style = info.preferredExceptionStyle();
? ExceptionStyle::CAPI
: ExceptionStyle::CXX;
llvm::Value* func; llvm::Value* func;
if (pass_keywords) if (pass_keywords)
...@@ -691,10 +685,7 @@ CompilerVariable* UnknownType::callattr(IREmitter& emitter, const OpInfo& info, ...@@ -691,10 +685,7 @@ CompilerVariable* UnknownType::callattr(IREmitter& emitter, const OpInfo& info,
bool pass_keywords = (flags.argspec.num_keywords != 0); bool pass_keywords = (flags.argspec.num_keywords != 0);
int npassed_args = flags.argspec.totalPassed(); int npassed_args = flags.argspec.totalPassed();
ExceptionStyle exception_style = ((FORCE_LLVM_CAPI && !info.unw_info.cxx_exc_dest && !flags.null_on_nonexistent) ExceptionStyle exception_style = flags.null_on_nonexistent ? CXX : info.preferredExceptionStyle();
|| info.unw_info.capi_exc_dest)
? ExceptionStyle::CAPI
: ExceptionStyle::CXX;
if (exception_style == CAPI) if (exception_style == CAPI)
assert(!flags.null_on_nonexistent); // Will conflict with CAPI's null-on-exception assert(!flags.null_on_nonexistent); // Will conflict with CAPI's null-on-exception
...@@ -1522,7 +1513,7 @@ public: ...@@ -1522,7 +1513,7 @@ public:
if (canStaticallyResolveGetattrs()) { if (canStaticallyResolveGetattrs()) {
Box* rtattr = typeLookup(cls, attr, nullptr); Box* rtattr = typeLookup(cls, attr, nullptr);
if (rtattr == NULL) { if (rtattr == NULL) {
ExceptionStyle exception_style = (FORCE_LLVM_CAPI || info.unw_info.capi_exc_dest) ? CAPI : CXX; ExceptionStyle exception_style = info.preferredExceptionStyle();
llvm::Value* raise_func = exception_style == CXX ? g.funcs.raiseAttributeErrorStr llvm::Value* raise_func = exception_style == CXX ? g.funcs.raiseAttributeErrorStr
: g.funcs.raiseAttributeErrorStrCapi; : g.funcs.raiseAttributeErrorStrCapi;
llvm::CallSite call = emitter.createCall3( llvm::CallSite call = emitter.createCall3(
...@@ -1717,10 +1708,8 @@ public: ...@@ -1717,10 +1708,8 @@ public:
CompilerVariable* callattr(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var, BoxedString* attr, CompilerVariable* callattr(IREmitter& emitter, const OpInfo& info, ConcreteCompilerVariable* var, BoxedString* attr,
CallattrFlags flags, const std::vector<CompilerVariable*>& args, CallattrFlags flags, const std::vector<CompilerVariable*>& args,
const std::vector<BoxedString*>* keyword_names) override { const std::vector<BoxedString*>* keyword_names) override {
ExceptionStyle exception_style = CXX; // XXX investigate
// Not safe to force-capi here since most of the functions won't have capi variants: ExceptionStyle exception_style = info.preferredExceptionStyle();
if (/*FORCE_LLVM_CAPI ||*/ info.unw_info.capi_exc_dest)
exception_style = CAPI;
ConcreteCompilerVariable* called_constant = tryCallattrConstant( ConcreteCompilerVariable* called_constant = tryCallattrConstant(
emitter, info, var, attr, flags.cls_only, flags.argspec, args, keyword_names, NULL, exception_style); emitter, info, var, attr, flags.cls_only, flags.argspec, args, keyword_names, NULL, exception_style);
...@@ -1783,9 +1772,7 @@ public: ...@@ -1783,9 +1772,7 @@ public:
static BoxedString* attr = internStringImmortal("__getitem__"); static BoxedString* attr = internStringImmortal("__getitem__");
bool no_attribute = false; bool no_attribute = false;
ExceptionStyle exception_style = CXX; ExceptionStyle exception_style = info.preferredExceptionStyle();
if (FORCE_LLVM_CAPI || info.unw_info.capi_exc_dest)
exception_style = CAPI;
ConcreteCompilerVariable* called_constant = tryCallattrConstant( ConcreteCompilerVariable* called_constant = tryCallattrConstant(
emitter, info, var, attr, true, ArgPassSpec(1, 0, 0, 0), { slice }, NULL, &no_attribute, exception_style); emitter, info, var, attr, true, ArgPassSpec(1, 0, 0, 0), { slice }, NULL, &no_attribute, exception_style);
...@@ -2293,9 +2280,7 @@ public: ...@@ -2293,9 +2280,7 @@ public:
rtn->incvref(); rtn->incvref();
return rtn; return rtn;
} else { } else {
ExceptionStyle target_exception_style = CXX; ExceptionStyle target_exception_style = info.preferredExceptionStyle();
if (FORCE_LLVM_CAPI || info.unw_info.capi_exc_dest)
target_exception_style = CAPI;
if (target_exception_style == CAPI) { if (target_exception_style == CAPI) {
llvm::CallSite call = emitter.createCall(info.unw_info, g.funcs.raiseIndexErrorStrCapi, llvm::CallSite call = emitter.createCall(info.unw_info, g.funcs.raiseIndexErrorStrCapi,
......
...@@ -495,6 +495,7 @@ static void emitBBs(IRGenState* irstate, TypeAnalysis* types, const OSREntryDesc ...@@ -495,6 +495,7 @@ static void emitBBs(IRGenState* irstate, TypeAnalysis* types, const OSREntryDesc
std::unordered_map<CFGBlock*, ConcreteSymbolTable*> phi_ending_symbol_tables; std::unordered_map<CFGBlock*, ConcreteSymbolTable*> phi_ending_symbol_tables;
typedef std::unordered_map<InternedString, std::pair<ConcreteCompilerType*, llvm::PHINode*>> PHITable; typedef std::unordered_map<InternedString, std::pair<ConcreteCompilerType*, llvm::PHINode*>> PHITable;
std::unordered_map<CFGBlock*, PHITable*> created_phis; std::unordered_map<CFGBlock*, PHITable*> created_phis;
std::unordered_map<CFGBlock*, llvm::SmallVector<IRGenerator::ExceptionState, 2>> incoming_exception_state;
CFGBlock* initial_block = NULL; CFGBlock* initial_block = NULL;
if (entry_descriptor) { if (entry_descriptor) {
...@@ -759,6 +760,11 @@ static void emitBBs(IRGenState* irstate, TypeAnalysis* types, const OSREntryDesc ...@@ -759,6 +760,11 @@ static void emitBBs(IRGenState* irstate, TypeAnalysis* types, const OSREntryDesc
} }
} }
auto exc_it = incoming_exception_state.find(block);
if (exc_it != incoming_exception_state.end()) {
generator->setIncomingExceptionState(exc_it->second);
}
// Generate loop safepoints on backedges. // Generate loop safepoints on backedges.
for (CFGBlock* predecessor : block->predecessors) { for (CFGBlock* predecessor : block->predecessors) {
if (predecessor->idx > block->idx) { if (predecessor->idx > block->idx) {
...@@ -777,6 +783,15 @@ static void emitBBs(IRGenState* irstate, TypeAnalysis* types, const OSREntryDesc ...@@ -777,6 +783,15 @@ static void emitBBs(IRGenState* irstate, TypeAnalysis* types, const OSREntryDesc
phi_ending_symbol_tables[block] = ending_st.phi_symbol_table; phi_ending_symbol_tables[block] = ending_st.phi_symbol_table;
llvm_exit_blocks[block] = ending_st.ending_block; llvm_exit_blocks[block] = ending_st.ending_block;
if (ending_st.exception_state.size()) {
AST_stmt* last_stmt = block->body.back();
assert(last_stmt->type == AST_TYPE::Invoke);
CFGBlock* exc_block = ast_cast<AST_Invoke>(last_stmt)->exc_dest;
assert(!incoming_exception_state.count(exc_block));
incoming_exception_state.insert(std::make_pair(exc_block, ending_st.exception_state));
}
if (into_hax.count(block)) if (into_hax.count(block))
ASSERT(ending_st.symbol_table->size() == 0, "%d", block->idx); ASSERT(ending_st.symbol_table->size() == 0, "%d", block->idx);
} }
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#include "llvm/IR/Intrinsics.h" #include "llvm/IR/Intrinsics.h"
#include "llvm/IR/IRBuilder.h" #include "llvm/IR/IRBuilder.h"
#include "core/options.h"
#include "core/types.h" #include "core/types.h"
namespace pyston { namespace pyston {
...@@ -33,18 +34,18 @@ struct UnwindInfo { ...@@ -33,18 +34,18 @@ struct UnwindInfo {
public: public:
AST_stmt* current_stmt; AST_stmt* current_stmt;
llvm::BasicBlock* capi_exc_dest; llvm::BasicBlock* exc_dest;
llvm::BasicBlock* cxx_exc_dest;
bool hasHandler() const { return cxx_exc_dest != NULL || capi_exc_dest != NULL; } bool hasHandler() const { return exc_dest != NULL; }
UnwindInfo(AST_stmt* current_stmt, llvm::BasicBlock* capi_exc_dest, llvm::BasicBlock* cxx_exc_dest) UnwindInfo(AST_stmt* current_stmt, llvm::BasicBlock* exc_dest) : current_stmt(current_stmt), exc_dest(exc_dest) {}
: current_stmt(current_stmt), capi_exc_dest(capi_exc_dest), cxx_exc_dest(cxx_exc_dest) {}
ExceptionStyle preferredExceptionStyle() const;
// Risky! This means that we can't unwind from this location, and should be used in the // Risky! This means that we can't unwind from this location, and should be used in the
// rare case that there are language-specific reasons that the statement should not unwind // rare case that there are language-specific reasons that the statement should not unwind
// (ex: loading function arguments into the appropriate scopes). // (ex: loading function arguments into the appropriate scopes).
static UnwindInfo cantUnwind() { return UnwindInfo(NULL, NULL, NULL); } static UnwindInfo cantUnwind() { return UnwindInfo(NULL, NULL); }
}; };
// TODO get rid of this // TODO get rid of this
...@@ -146,6 +147,8 @@ public: ...@@ -146,6 +147,8 @@ public:
: effort(effort), type_recorder(type_recorder), unw_info(unw_info) {} : effort(effort), type_recorder(type_recorder), unw_info(unw_info) {}
TypeRecorder* getTypeRecorder() const { return type_recorder; } TypeRecorder* getTypeRecorder() const { return type_recorder; }
ExceptionStyle preferredExceptionStyle() const { return unw_info.preferredExceptionStyle(); }
}; };
} }
......
This diff is collapsed.
...@@ -72,8 +72,6 @@ private: ...@@ -72,8 +72,6 @@ private:
llvm::Value* frame_info_arg; llvm::Value* frame_info_arg;
int scratch_size; int scratch_size;
llvm::DenseMap<CFGBlock*, ExceptionStyle> landingpad_styles;
public: public:
IRGenState(CLFunction* clfunc, CompiledFunction* cf, SourceInfo* source_info, std::unique_ptr<PhiAnalysis> phis, IRGenState(CLFunction* clfunc, CompiledFunction* cf, SourceInfo* source_info, std::unique_ptr<PhiAnalysis> phis,
ParamNames* param_names, GCBuilder* gc, llvm::MDNode* func_dbg_info); ParamNames* param_names, GCBuilder* gc, llvm::MDNode* func_dbg_info);
...@@ -107,15 +105,19 @@ public: ...@@ -107,15 +105,19 @@ public:
ParamNames* getParamNames() { return param_names; } ParamNames* getParamNames() { return param_names; }
void setFrameInfoArgument(llvm::Value* v) { frame_info_arg = v; } void setFrameInfoArgument(llvm::Value* v) { frame_info_arg = v; }
ExceptionStyle getLandingpadStyle(AST_Invoke* invoke);
ExceptionStyle getLandingpadStyle(CFGBlock* block);
}; };
// turns CFGBlocks into LLVM IR // turns CFGBlocks into LLVM IR
class IRGenerator { class IRGenerator {
private: private:
public: public:
struct ExceptionState {
llvm::BasicBlock* from_block;
ConcreteCompilerVariable* exc_type, *exc_value, *exc_tb;
ExceptionState(llvm::BasicBlock* from_block, ConcreteCompilerVariable* exc_type,
ConcreteCompilerVariable* exc_value, ConcreteCompilerVariable* exc_tb)
: from_block(from_block), exc_type(exc_type), exc_value(exc_value), exc_tb(exc_tb) {}
};
struct EndingState { struct EndingState {
// symbol_table records which Python variables are bound to what CompilerVariables at the end of this block. // symbol_table records which Python variables are bound to what CompilerVariables at the end of this block.
// phi_symbol_table records the ones that will need to be `phi'd. // phi_symbol_table records the ones that will need to be `phi'd.
...@@ -123,8 +125,14 @@ public: ...@@ -123,8 +125,14 @@ public:
SymbolTable* symbol_table; SymbolTable* symbol_table;
ConcreteSymbolTable* phi_symbol_table; ConcreteSymbolTable* phi_symbol_table;
llvm::BasicBlock* ending_block; llvm::BasicBlock* ending_block;
EndingState(SymbolTable* symbol_table, ConcreteSymbolTable* phi_symbol_table, llvm::BasicBlock* ending_block) llvm::SmallVector<ExceptionState, 2> exception_state;
: symbol_table(symbol_table), phi_symbol_table(phi_symbol_table), ending_block(ending_block) {}
EndingState(SymbolTable* symbol_table, ConcreteSymbolTable* phi_symbol_table, llvm::BasicBlock* ending_block,
llvm::ArrayRef<ExceptionState> exception_state)
: symbol_table(symbol_table),
phi_symbol_table(phi_symbol_table),
ending_block(ending_block),
exception_state(exception_state.begin(), exception_state.end()) {}
}; };
virtual ~IRGenerator() {} virtual ~IRGenerator() {}
...@@ -139,6 +147,10 @@ public: ...@@ -139,6 +147,10 @@ public:
virtual void doSafePoint(AST_stmt* next_statement) = 0; virtual void doSafePoint(AST_stmt* next_statement) = 0;
virtual void addFrameStackmapArgs(PatchpointInfo* pp, AST_stmt* current_stmt, virtual void addFrameStackmapArgs(PatchpointInfo* pp, AST_stmt* current_stmt,
std::vector<llvm::Value*>& stackmap_args) = 0; std::vector<llvm::Value*>& stackmap_args) = 0;
virtual void addOutgoingExceptionState(ExceptionState exception_state) = 0;
virtual void setIncomingExceptionState(llvm::SmallVector<ExceptionState, 2> exc_state) = 0;
virtual llvm::BasicBlock* getCXXExcDest(llvm::BasicBlock* final_dest) = 0;
virtual llvm::BasicBlock* getCAPIExcDest(llvm::BasicBlock* final_dest, AST_stmt* current_stmt) = 0;
}; };
class IREmitter; class IREmitter;
......
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