some tech debt cleanup

gccjit-ravi534
Dibyendu Majumdar 7 years ago
parent 98dbf83670
commit 1c703d4ec0

@ -588,7 +588,10 @@ typedef struct Table {
GCObject *gclist;
/** RAVI extension */
RaviArray ravi_array;
#if RAVI_USE_NEWHASH
// TODO we should reorganize this structure
unsigned int hmask; /* Hash part mask (size of hash part - 1) - borrowed from LuaJIT */
#endif
} Table;

@ -44,10 +44,7 @@ LUAI_FUNC const TValue *luaH_getint (Table *t, lua_Integer key);
LUAI_FUNC void luaH_setint (lua_State *L, Table *t, lua_Integer key,
TValue *value);
/** RAVI change start - attempt to inline some functions **/
#define NEW_HASH 1
#if NEW_HASH
#if RAVI_USE_NEWHASH
/*
Like LuaJIT we use a pre-computed hmask to minimise the number of steps
@ -64,7 +61,9 @@ required to get to a node in the hash table
*/
#define hashmod(t, n) (gnode(t, ((n) % ((t)->hmask|1))))
#define hashpointer(t, p) hashmod(t, point2uint(p))
#else
#define hashpow2(t,n) (gnode(t, lmod((n), sizenode(t))))
#define hashstr(t,str) hashpow2(t, (str)->hash)
#define hashboolean(t,p) hashpow2(t, p)
@ -75,6 +74,7 @@ required to get to a node in the hash table
*/
#define hashmod(t,n) (gnode(t, ((n) % ((sizenode(t)-1)|1))))
#define hashpointer(t,p) hashmod(t, point2uint(p))
#endif
#if defined(RAVI_ENABLED)

@ -816,5 +816,11 @@
#endif
#define RAVI_USE_NEWHASH 1
#define RAVI_USE_LLVM_BRANCH_WEIGHTS 1
/* If following is defined as true then LLVM instructions emitted for arithmetic ops
priority floating point ops, else default is to prioritise integer ops */
#define RAVI_USE_LLVM_ARITH_FLOATPRIORITY 1
#endif

@ -351,7 +351,9 @@ struct LuaLLVMTypes {
llvm::MDNode *tbaa_Table_array;
llvm::MDNode *tbaa_Table_flags;
llvm::MDNode *tbaa_Table_metatable;
#if RAVI_USE_NEWHASH
llvm::MDNode *tbaa_Table_hmask;
#endif
};
// The hierarchy of objects
@ -464,12 +466,14 @@ class RaviJITFunction {
lua_CFunction *func_ptrptr_;
public:
RaviJITFunction(lua_CFunction *p, const std::shared_ptr<RaviJITModule>& module,
RaviJITFunction(lua_CFunction *p,
const std::shared_ptr<RaviJITModule> &module,
llvm::FunctionType *type,
llvm::GlobalValue::LinkageTypes linkage,
const std::string &name);
RaviJITFunction(lua_CFunction *p, const std::shared_ptr<RaviJITModule>& module,
const std::string &name);
RaviJITFunction(lua_CFunction *p,
const std::shared_ptr<RaviJITModule> &module,
const std::string &name);
~RaviJITFunction();
@ -1045,11 +1049,26 @@ class RaviCodeGenerator {
void emit_LOADBOOL(RaviFunctionDef *def, int A, int B, int C, int j, int pc);
void emit_ARITH(RaviFunctionDef *def, int A, int B, int C, OpCode op, TMS tms,
int pc);
// Code size priority so go via function calls
void emit_ARITH_calls(RaviFunctionDef *def, int A, int B, int C, OpCode op,
TMS tms, int pc);
// integer arith priority over floating
void emit_ARITH_intpriority(RaviFunctionDef *def, int A, int B, int C,
OpCode op, TMS tms, int pc);
void emit_ARITH_new(RaviFunctionDef *def, int A, int B, int C, OpCode op,
TMS tms, int pc);
// floating arith priority over integer
void emit_ARITH_floatpriority(RaviFunctionDef *def, int A, int B, int C,
OpCode op, TMS tms, int pc);
inline void emit_ARITH(RaviFunctionDef *def, int A, int B, int C, OpCode op,
TMS tms, int pc) {
#if RAVI_USE_LLVM_ARITH_FLOATPRIORITY
emit_ARITH_floatpriority(def, A, B, C, op, tms, pc);
#else
emit_ARITH_intpriority(def, A, B, C, op, tms, pc);
#endif
}
void emit_MOD(RaviFunctionDef *def, int A, int B, int C, int pc);

@ -327,7 +327,9 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) {
if (size == 0) { /* no elements to hash part? */
t->node = cast(Node *, dummynode); /* use common 'dummynode' */
t->lsizenode = 0;
t->hmask = 0;
#if RAVI_USE_NEWHASH
t->hmask = 0;
#endif
t->lastfree = NULL; /* signal that it is using dummy node */
}
else {
@ -344,7 +346,9 @@ static void setnodevector (lua_State *L, Table *t, unsigned int size) {
setnilvalue(gval(n));
}
t->lsizenode = cast_byte(lsize);
t->hmask = size - 1;
#if RAVI_USE_NEWHASH
t->hmask = size - 1;
#endif
t->lastfree = gnode(t, size); /* all positions are free */
}
}

@ -27,36 +27,35 @@
namespace ravi {
// OP_ADD, OP_SUB, OP_MUL and OP_DIV
void RaviCodeGenerator::emit_ARITH(RaviFunctionDef *def, int A, int B, int C,
OpCode op, TMS tms, int pc) {
#if 0
// Code size priority so go via function calls
void RaviCodeGenerator::emit_ARITH_calls(RaviFunctionDef *def, int A, int B,
int C, OpCode op, TMS tms, int pc) {
bool traced = emit_debug_trace(def, op, pc);
emit_load_base(def);
llvm::Value *ra = emit_gep_register(def, A);
llvm::Value *rb = emit_gep_register_or_constant(def, B);
llvm::Value *rc = emit_gep_register_or_constant(def, C);
switch (op) {
case OP_ADD:
CreateCall4(def->builder, def->raviV_op_addF,
def->L, ra, rb, rc);
break;
case OP_SUB:
CreateCall4(def->builder, def->raviV_op_subF,
def->L, ra, rb, rc);
break;
case OP_MUL:
CreateCall4(def->builder, def->raviV_op_mulF,
def->L, ra, rb, rc);
break;
case OP_DIV:
CreateCall4(def->builder, def->raviV_op_divF,
def->L, ra, rb, rc);
break;
default:
lua_assert(0);
case OP_ADD:
CreateCall4(def->builder, def->raviV_op_addF, def->L, ra, rb, rc);
break;
case OP_SUB:
CreateCall4(def->builder, def->raviV_op_subF, def->L, ra, rb, rc);
break;
case OP_MUL:
CreateCall4(def->builder, def->raviV_op_mulF, def->L, ra, rb, rc);
break;
case OP_DIV:
CreateCall4(def->builder, def->raviV_op_divF, def->L, ra, rb, rc);
break;
default: lua_assert(0);
}
}
#else
// OP_ADD, OP_SUB, OP_MUL and OP_DIV
void RaviCodeGenerator::emit_ARITH_intpriority(RaviFunctionDef *def, int A,
int B, int C, OpCode op, TMS tms,
int pc) {
// TValue *rb = RKB(i);
// TValue *rc = RKC(i);
// lua_Number nb; lua_Number nc;
@ -240,12 +239,14 @@ void RaviCodeGenerator::emit_ARITH(RaviFunctionDef *def, int A, int B, int C,
def->f->getBasicBlockList().push_back(done_block);
def->builder->SetInsertPoint(done_block);
#endif
}
// OP_ADD, OP_SUB, OP_MUL and OP_DIV
void RaviCodeGenerator::emit_ARITH_new(RaviFunctionDef *def, int A, int B,
int C, OpCode op, TMS tms, int pc) {
// This version prioritizes floating point arith over integer arith
// Also slow path is slower as it goes via function calls
void RaviCodeGenerator::emit_ARITH_floatpriority(RaviFunctionDef *def, int A,
int B, int C, OpCode op,
TMS tms, int pc) {
// TValue *rb = RKB(i);
// TValue *rc = RKC(i);
// lua_Number nb; lua_Number nc;
@ -443,7 +444,7 @@ void RaviCodeGenerator::emit_ARITH_new(RaviFunctionDef *def, int A, int B,
result = def->builder->CreateFDiv(
def->builder->CreateSIToFP(lhs, def->types->lua_NumberT),
def->builder->CreateSIToFP(rhs, def->types->lua_NumberT));
break;
break;
default: lua_assert(0);
}

@ -108,9 +108,16 @@ void RaviCodeGenerator::attach_branch_weights(RaviFunctionDef *def,
llvm::Instruction *ins,
uint32_t true_branch,
uint32_t false_branch) {
#if RAVI_USE_LLVM_BRANCH_WEIGHTS
ins->setMetadata(llvm::LLVMContext::MD_prof,
def->jitState->types()->mdbuilder.createBranchWeights(
true_branch, false_branch));
#else
(void)def;
(void)ins;
(void)true_branch;
(void)false_branch;
#endif
}
llvm::Value *RaviCodeGenerator::emit_gep(RaviFunctionDef *def, const char *name,
@ -353,7 +360,7 @@ llvm::Value *RaviCodeGenerator::emit_table_get_hashsize(RaviFunctionDef *def,
llvm::Value *RaviCodeGenerator::emit_table_get_hashstr(RaviFunctionDef *def,
llvm::Value *table,
TString *key) {
#if NEW_HASH
#if RAVI_USE_NEWHASH
unsigned int hash = key->hash;
llvm::Value *hmask_ptr = emit_gep(def, "hmask", table, 0, 12);
llvm::Instruction *hmask = def->builder->CreateLoad(hmask_ptr);
@ -1801,7 +1808,7 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
case OP_ADD: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_ARITH_new(def, A, B, C, OP_ADD, TM_ADD, pc);
emit_ARITH(def, A, B, C, OP_ADD, TM_ADD, pc);
} break;
case OP_RAVI_ADDFF: {
int B = GETARG_B(i);
@ -1822,7 +1829,7 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
case OP_SUB: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_ARITH_new(def, A, B, C, OP_SUB, TM_SUB, pc);
emit_ARITH(def, A, B, C, OP_SUB, TM_SUB, pc);
} break;
case OP_RAVI_SUBFF: {
int B = GETARG_B(i);
@ -1848,7 +1855,7 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
case OP_MUL: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_ARITH_new(def, A, B, C, OP_MUL, TM_MUL, pc);
emit_ARITH(def, A, B, C, OP_MUL, TM_MUL, pc);
} break;
case OP_RAVI_MULFF: {
int B = GETARG_B(i);
@ -1869,7 +1876,7 @@ bool RaviCodeGenerator::compile(lua_State *L, Proto *p,
case OP_DIV: {
int B = GETARG_B(i);
int C = GETARG_C(i);
emit_ARITH_new(def, A, B, C, OP_DIV, TM_DIV, pc);
emit_ARITH(def, A, B, C, OP_DIV, TM_DIV, pc);
} break;
case OP_RAVI_DIVFF: {
int B = GETARG_B(i);

@ -25,7 +25,9 @@
This will hopefully eventually contain a Lua binding for LLVM.
*/
#if USE_DMR_C
#include <dmr_c_llvm.h>
#endif
#include <ravijit.h>
#include "ravi_llvmcodegen.h"
@ -795,7 +797,7 @@ static int module_compile_C(lua_State *L) {
codebuffer = lua_tostring(L, 2);
}
#if USE_LLVM
#if USE_DMR_C
if (dmrC_llvmcompile(argc, argv, llvm::wrap(mh->M->module()), codebuffer)) {
lua_pushboolean(L, true);
}

@ -484,7 +484,9 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
elements.push_back(pTableT); /* metatable */
elements.push_back(pGCObjectT); /* gclist */
elements.push_back(RaviArrayT); /* RaviArray */
#if RAVI_USE_NEWHASH
elements.push_back(C_intT); /* hmask */
#endif
TableT->setBody(elements);
// struct lua_longjmp; /* defined in ldo.c */
@ -1287,8 +1289,10 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
std::pair<llvm::MDNode *, uint64_t>(tbaa_pointerT, 28)); /* gclist */
nodes.push_back(std::pair<llvm::MDNode *, uint64_t>(tbaa_RaviArrayT,
32)); /* ravi_array */
#if RAVI_USE_NEWHASH
nodes.push_back(
std::pair<llvm::MDNode *, uint64_t>(tbaa_intT, 48)); /* hmask */
#endif
tbaa_TableT = mdbuilder.createTBAAStructTypeNode("Table", nodes);
tbaa_Table_flags =
@ -1303,14 +1307,16 @@ LuaLLVMTypes::LuaLLVMTypes(llvm::LLVMContext &context) : mdbuilder(context) {
mdbuilder.createTBAAStructTagNode(tbaa_TableT, tbaa_pointerT, 16);
tbaa_Table_metatable =
mdbuilder.createTBAAStructTagNode(tbaa_TableT, tbaa_pointerT, 24);
tbaa_Table_hmask =
mdbuilder.createTBAAStructTagNode(tbaa_TableT, tbaa_intT, 48);
tbaa_RaviArray_dataT =
mdbuilder.createTBAAStructTagNode(tbaa_TableT, tbaa_pointerT, 32);
tbaa_RaviArray_lenT =
mdbuilder.createTBAAStructTagNode(tbaa_TableT, tbaa_intT, 36);
tbaa_RaviArray_typeT =
mdbuilder.createTBAAStructTagNode(tbaa_TableT, tbaa_charT, 44);
#if RAVI_USE_NEWHASH
tbaa_Table_hmask =
mdbuilder.createTBAAStructTagNode(tbaa_TableT, tbaa_intT, 48);
#endif
}
void LuaLLVMTypes::dump() {

Loading…
Cancel
Save