xo-jit: + explicit env for captured function args [wip, not tested]
This commit is contained in:
parent
fdc5d46fd7
commit
d7192c1d97
7 changed files with 991 additions and 220 deletions
|
|
@ -161,6 +161,7 @@ namespace xo {
|
|||
llvm::AllocaInst * create_entry_frame_alloca(llvm::Function * llvm_fn,
|
||||
llvm::StructType * frame_llvm_type);
|
||||
|
||||
#ifdef OBSOLETE // see activation_record::create_entry_block_alloca()
|
||||
/** codegen helper for a user-defined function (codegen_lambda()):
|
||||
* create stack slot on behalf of some formal parameter to a function,
|
||||
* so we can avoid SSA restriction on function body
|
||||
|
|
@ -170,6 +171,7 @@ namespace xo {
|
|||
llvm::AllocaInst * create_entry_block_alloca(llvm::Function * llvm_fn,
|
||||
const std::string & var_name,
|
||||
TypeDescr var_type);
|
||||
#endif
|
||||
|
||||
private:
|
||||
/** (re)create pipeline to turn expressions into llvm IR code **/
|
||||
|
|
|
|||
|
|
@ -28,31 +28,61 @@ namespace xo {
|
|||
int j_rt_slot)
|
||||
: i_rt_link_{i_rt_link}, j_rt_slot_{j_rt_slot} {}
|
||||
|
||||
static runtime_binding_path stackonly() {
|
||||
return runtime_binding_path(0, -1);
|
||||
}
|
||||
static runtime_binding_path local(int j_rt_slot) {
|
||||
return runtime_binding_path(0, j_rt_slot);
|
||||
}
|
||||
|
||||
bool is_stackonly() const { return (i_rt_link_ == 0) && (j_rt_slot_ == -1); }
|
||||
bool is_captured() const { return !is_stackonly(); }
|
||||
|
||||
public:
|
||||
/** nnumber of parent runtime env links to traverse. -1 if global. -2 if sentinel **/
|
||||
int i_rt_link_ = -2;
|
||||
/** slot# within runtime environment where this variable bound.
|
||||
/** >= 0: slot# within explicit runtime environment where this variable bound.
|
||||
* (local vars only -- ignored for global vars)
|
||||
* -1: stack-only parameter
|
||||
**/
|
||||
int j_rt_slot_ = 0;
|
||||
};
|
||||
|
||||
struct runtime_binding_detail {
|
||||
/** Formal index position for this formal parameter.
|
||||
* Index into @ref activation_record::binding_v_,
|
||||
* also for @ref Lambda::fn_arg
|
||||
**/
|
||||
int i_argno_ = -1;
|
||||
|
||||
/** instructions for establishing stack address of this variable
|
||||
* In practice will be either an AllocaInst (for non-captured variables),
|
||||
* or result of IRBuilder<>::CreateInBoundsGEP (for captured variables).
|
||||
**/
|
||||
llvm::Value * llvm_addr_ = nullptr;
|
||||
|
||||
/** llvm type associated with stack-allocated variable.
|
||||
* Determines (when combined with llvm::DataLayout) how much space
|
||||
* will be required for this particular variable
|
||||
**/
|
||||
llvm::Type * llvm_type_ = nullptr;
|
||||
};
|
||||
|
||||
/**
|
||||
* 1. pattern for a stack frame associated with a user-defined function (some Lambda lm)
|
||||
*
|
||||
* 2. each function needs its own IR builder, to keep track of things like insert point
|
||||
*
|
||||
* 3. simple case first.
|
||||
* if lm->needs_closure_flag() is false, then
|
||||
* if lm->needs_closure_flag() is false, then:
|
||||
*
|
||||
* a. all formal parameters of lm
|
||||
* a. still need a closure-shaped object, because when we invoke function, we may
|
||||
* not know until runtime whether it relies on closure.
|
||||
* For such function we will generate a closure with empty environment pointer.
|
||||
* b. all formal parameters of lm
|
||||
* are used only in the layer associated with that lambda's body; in particular
|
||||
* they aren't free in any nested lambda
|
||||
* b. conversely, the top layer of lm's body has no free variables.
|
||||
* c. conversely, the top layer of lm's body has no free variables.
|
||||
* The only variables that *do* appear are lm's formal parameters.
|
||||
*
|
||||
* In this case, all of lm's formals will be allocated on the stack using regular
|
||||
|
|
@ -70,20 +100,76 @@ namespace xo {
|
|||
class activation_record {
|
||||
public:
|
||||
using Lambda = xo::ast::Lambda;
|
||||
using TypeDescr = xo::reflect::TypeDescr;
|
||||
|
||||
public:
|
||||
activation_record(const ref::rp<Lambda> & lm)
|
||||
: lambda_{lm} {}
|
||||
activation_record(const ref::rp<Lambda> & lm);
|
||||
|
||||
llvm::AllocaInst * lookup_var(const std::string & var_name) const;
|
||||
const ref::rp<Lambda> lambda() const { return lambda_; }
|
||||
|
||||
/**
|
||||
* @p j_slot index number (0-based) for var_name in formal parameter list for
|
||||
* its originating lambda
|
||||
/** retrieve @c llvm::Value* representing the primary stack location
|
||||
* for formal parameter @p var_name
|
||||
**/
|
||||
llvm::AllocaInst * alloc_var(std::size_t j_slot,
|
||||
const std::string & var_name,
|
||||
llvm::AllocaInst * alloca);
|
||||
const runtime_binding_detail * lookup_var(const std::string & var_name) const;
|
||||
|
||||
/** Remember allocation of a function variable on the stack
|
||||
*
|
||||
* @param var_name. formal parameter name
|
||||
* @param binding. address + supporting details for
|
||||
* primary (stack-allocated) storage for this variable
|
||||
**/
|
||||
const runtime_binding_detail * alloc_var(const std::string & var_name,
|
||||
const runtime_binding_detail & binding);
|
||||
|
||||
#ifdef NOT_USING
|
||||
llvm::AllocaInst * create_runtime_localenv_alloca(ref::brw<LlvmContext> llvm_cx,
|
||||
//const llvm::DataLayout & data_layout,
|
||||
llvm::Function * llvm_fn,
|
||||
llvm::IRBuilder<> & fn_ir_builder);
|
||||
#endif
|
||||
|
||||
runtime_binding_detail create_entry_block_alloca(ref::brw<LlvmContext> llvm_cx,
|
||||
//const llvm::DataLayout & data_layout,
|
||||
llvm::Function * llvm_fn,
|
||||
llvm::IRBuilder<> & fn_ir_builder,
|
||||
int i_arg,
|
||||
const std::string & var_name,
|
||||
TypeDescr var_td);
|
||||
|
||||
/** generate instructions that establish stacck location for a local-environment slot
|
||||
*
|
||||
* @param llvm_cx. handle for context -- manages storage for llvm::Types + related
|
||||
* @param localenv_llvm_type. describes contents of local environment
|
||||
* for a particular function. Same as @c localenv_alloca->getAllocatedType()
|
||||
* @param localenv_alloca. stack location for local environment
|
||||
* @param i_slot. 0-based slot number within local environment,
|
||||
* for which address is required
|
||||
* @param fn_ir_builder. insertion point for generated instructions
|
||||
* that compute target slot address (will be at/near top of function,
|
||||
* since we will copy captured function arguments to localenv,
|
||||
* then use the localenv copy exclusively.
|
||||
* @return value representing localenv slot address
|
||||
**/
|
||||
llvm::Value * runtime_localenv_slot_addr(ref::brw<LlvmContext> llvm_cx,
|
||||
llvm::StructType * localenv_llvm_type,
|
||||
llvm::AllocaInst * localenv_alloca,
|
||||
int i_slot,
|
||||
llvm::IRBuilder<> & fn_ir_builder);
|
||||
|
||||
/** establish storage for formal parameters on behalf of a new-but-empty
|
||||
* llvm function @p llvm_fn. Creates llvm IR instructions on function
|
||||
* entry that
|
||||
* 1. allocates stack space for function parameters.
|
||||
* 2. stores incoming parameters in that stack space.
|
||||
*
|
||||
* Strategy:
|
||||
* - for stackonly parameters, use individual @c llvm::AllocaInst instances
|
||||
* - create custom @c llvm::StructType for captured parameters, also initially stack-allocated
|
||||
**/
|
||||
bool bind_locals(ref::brw<LlvmContext> llvm_cx,
|
||||
//const llvm::DataLayout & data_layout,
|
||||
llvm::Function * llvm_fn,
|
||||
llvm::IRBuilder<> & ir_builder);
|
||||
|
||||
private:
|
||||
/** this activation record created on behalf of a call to @ref lambda_.
|
||||
|
|
@ -94,16 +180,27 @@ namespace xo {
|
|||
**/
|
||||
ref::rp<Lambda> lambda_;
|
||||
|
||||
/** @c binding_v_[i] specifies how/where to get location for formal parameter number *i*
|
||||
* of @ref lambda_.
|
||||
*
|
||||
/** @c binding_v_[i] specifies how/where we mean to navigate to
|
||||
* location for formal parameter number *i* of @ref lambda_.
|
||||
**/
|
||||
std::vector<runtime_binding_path> binding_v_;
|
||||
|
||||
/** maps named slots in a stack frame to logical addresses.
|
||||
* Only applies to not-captured vars with i_rt_link_=0
|
||||
/** if this function requires an explicit environment,
|
||||
* gives stack location for that environment.
|
||||
**/
|
||||
std::map<std::string, llvm::AllocaInst*> frame_; /* <-> kaleidoscope NamedValues */
|
||||
llvm::AllocaInst * localenv_alloca_ = nullptr;
|
||||
|
||||
/** maps named slots in a stack frame to logical addresses.
|
||||
*
|
||||
* - For captured arguments: will refer to slot within stack-allocated local environment
|
||||
* (an llvm::StructType, created by type2llvm::create_localenv_llvm_type())
|
||||
*
|
||||
* - For non-captured arguments: will refer to stack-allocated argument copy
|
||||
*
|
||||
* In either case using copy-to-stack to evade directly confronting
|
||||
* so we don't have to comply with llvm IR's SSA requirement.
|
||||
**/
|
||||
std::map<std::string, runtime_binding_detail> frame_; /* <-> kaleidoscope NamedValues */
|
||||
}; /*activation_record*/
|
||||
|
||||
} /*namespace jit*/
|
||||
|
|
|
|||
181
include/xo/jit/type2llvm.hpp
Normal file
181
include/xo/jit/type2llvm.hpp
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
/** @file type2llvm.hpp
|
||||
*
|
||||
* Author: Roland Conybeare
|
||||
**/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "LlvmContext.hpp"
|
||||
#include "xo/expression/Lambda.hpp"
|
||||
#include "xo/reflect/TypeDescr.hpp"
|
||||
#include <llvm/IR/DerivedTypes.h>
|
||||
//#include <cstdint>
|
||||
|
||||
namespace xo {
|
||||
namespace jit {
|
||||
/**
|
||||
**/
|
||||
struct type2llvm {
|
||||
public:
|
||||
using Lambda = xo::ast::Lambda;
|
||||
using TypeDescr = xo::reflect::TypeDescr;
|
||||
|
||||
public:
|
||||
/** establish suitable llvm representation for a c++ type (described by @p td)
|
||||
* llvm types are unique'd, at least within @p llvm_cx
|
||||
**/
|
||||
static llvm::Type * td_to_llvm_type(xo::ref::brw<LlvmContext> llvm_cx,
|
||||
TypeDescr td);
|
||||
|
||||
/** establish llvm representation for a function type
|
||||
* described by @p fn_td
|
||||
**/
|
||||
static llvm::FunctionType * function_td_to_llvm_type(xo::ref::brw<LlvmContext> llvm_cx,
|
||||
TypeDescr fn_td);
|
||||
|
||||
/** establish llvm concrete representation for a particular lambda's
|
||||
* runtime local environment:
|
||||
*
|
||||
* ^
|
||||
* | parent
|
||||
* +-------+ |
|
||||
* parent_env [0] | o-------/
|
||||
* +-------+
|
||||
* unwind_fn [1] | o-------> env * (*)(env*, ctl)
|
||||
* +-------+
|
||||
* arg[i] [2+i] . ... .
|
||||
* . ... .
|
||||
* +-------+
|
||||
*
|
||||
* ctl=0 unwind. finalization for any arg[i] that requires it.
|
||||
* returns nullptr
|
||||
* ctl=1 copy. copy runtime environment to heap destination
|
||||
* and return address of the copy
|
||||
*
|
||||
* arg[] comprises the subset of lambda arg names arg[j] for which
|
||||
* lambda->is_captured(arg[j]) is true
|
||||
**/
|
||||
static llvm::StructType *
|
||||
create_localenv_llvm_type(xo::ref::brw<LlvmContext> llvm_cx,
|
||||
xo::ref::brw<Lambda> lambda);
|
||||
|
||||
/** establish llvm rep'n for a pointer to an abstract local environment:
|
||||
*
|
||||
* +-------+
|
||||
* | o-------------\
|
||||
* +-------+ |
|
||||
* |
|
||||
* |
|
||||
* |
|
||||
* v
|
||||
* +-------+
|
||||
* parent_env [0] | o-------> _env_api*
|
||||
* +-------+
|
||||
* unwind_fn [1] | o-------> env * (*)(env*, ctl)
|
||||
* +-------+
|
||||
**/
|
||||
static llvm::PointerType *
|
||||
env_api_llvm_ptr_type(xo::ref::brw<LlvmContext> llvm_cx);
|
||||
|
||||
/** function type:
|
||||
* @code
|
||||
* env_api_* (env_api* env, int ctl);
|
||||
* @endcode
|
||||
*
|
||||
* ctl=0 unwind. finalization for any arg[i] that requires it.
|
||||
* returns nullptr
|
||||
* ctl=1 copy. copy runtime environment to heap destination
|
||||
* and return address of the copy
|
||||
*
|
||||
* returns function-pointer type
|
||||
**/
|
||||
static llvm::PointerType *
|
||||
require_localenv_unwind_llvm_fnptr_type(xo::ref::brw<LlvmContext> llvm_cx,
|
||||
llvm::PointerType * hint_envptr_llvm_type = nullptr);
|
||||
|
||||
private:
|
||||
/** establish llvm representation for a function-pointer type
|
||||
* described by @p fn_td
|
||||
**/
|
||||
static llvm::PointerType * function_td_to_llvm_fnptr_type(xo::ref::brw<LlvmContext> llvm_cx,
|
||||
TypeDescr fn_td);
|
||||
|
||||
/** establish llvm representation for a struct type described by @p struct_td
|
||||
**/
|
||||
static llvm::StructType * struct_td_to_llvm_type(xo::ref::brw<LlvmContext> llvm_cx,
|
||||
TypeDescr struct_td);
|
||||
|
||||
/** establish llvm representation for a pointer type described by @p pointer_td **/
|
||||
static llvm::PointerType * pointer_td_to_llvm_type(xo::ref::brw<LlvmContext> llvm_cx,
|
||||
TypeDescr pointer_td);
|
||||
|
||||
/** establish llvm abstract representation for a local environment:
|
||||
*
|
||||
* ^
|
||||
* | parent
|
||||
* +-------+ |
|
||||
* parent_env [0] | o-------/
|
||||
* +-------+
|
||||
* unwind_fn [1] | o-------> env * (*)(env*, ctl)
|
||||
* +-------+
|
||||
*
|
||||
* ctl=0 unwind. finalization for any arg[i] that requires it.
|
||||
* returns nullptr
|
||||
* ctl=1 copy. copy runtime environment to heap destination
|
||||
* and return address of the copy
|
||||
*
|
||||
* Concrete implementation will probably occupy additional memory,
|
||||
* to store captured lambda variables.
|
||||
*
|
||||
* @see type2llvm::function_td_to_llvm_closure_type
|
||||
**/
|
||||
static llvm::StructType *
|
||||
env_api_llvm_type(xo::ref::brw<LlvmContext> llvm_cx);
|
||||
|
||||
/** establish llvm abstract representation for a closure:
|
||||
* struct with
|
||||
* - [0] function pointer
|
||||
* - [1] runtime localenv pointer
|
||||
*
|
||||
* +-------+
|
||||
* | o---------> native function
|
||||
* +-------+
|
||||
* | o---------> runtime localenv
|
||||
* +-------+ (possibly nullptr)
|
||||
*
|
||||
* 1. for primitives, localenv will be null pointer
|
||||
* 2. for lambdas L with L->requires_closure_flag() = false,
|
||||
* localenv will also be null pointer
|
||||
* 3. for lambdas with L->requires_closure_flag() = true,
|
||||
*
|
||||
* localenv will (for lambdas requiring closures)
|
||||
* in practice be struct:
|
||||
*
|
||||
* ^
|
||||
* | parent
|
||||
* +-------+ |
|
||||
* parent_env [0] | o-------/
|
||||
* +-------+
|
||||
* unwind_fn [1] | o-------> env * (*)(env*, ctl)
|
||||
* +-------+
|
||||
* arg[i] [2+i] . ... .
|
||||
* . ... .
|
||||
* +-------+
|
||||
*
|
||||
* ctl=0 unwind. finalization for any arg[i] that requires it.
|
||||
* returns nullptr
|
||||
* ctl=1 copy. copy runtime environment to heap destination
|
||||
* and return address of the copy
|
||||
*
|
||||
* Implementation here will just use generic pointer for runtime
|
||||
* localenv.
|
||||
**/
|
||||
static llvm::StructType *
|
||||
function_td_to_llvm_closure_type(xo::ref::brw<LlvmContext> llvm_cx,
|
||||
TypeDescr fn_td);
|
||||
|
||||
}; /*type2llvm*/
|
||||
} /*namespace jit*/
|
||||
} /*namespace xo*/
|
||||
|
||||
/** end type2llvm.hpp **/
|
||||
Loading…
Add table
Add a link
Reference in a new issue