llvmpipe: Trim the fragment shader cached based on LLVM IR instruction count.

Number of fragment shader variants is not very representative of the
memory used by LLVM, neither is number of shader instructions, as often
texture sampling constitutes most of the generated code.

This change adds an additional trim criteria: least recently used
fragment shader variants will be freed until the total number of LLVM IR
instruction falls below a specified threshold.

Reviewed-by: Brian Paul <brianp@vmware.com>
This commit is contained in:
José Fonseca 2011-11-30 17:12:00 +00:00
parent f32c7232a8
commit 6cf7245f69
6 changed files with 62 additions and 11 deletions

View file

@ -411,3 +411,28 @@ lp_build_context_init(struct lp_build_context *bld,
bld->zero = LLVMConstNull(bld->vec_type);
bld->one = lp_build_one(gallivm, type);
}
/**
* Count the number of instructions in a function.
*/
unsigned
lp_build_count_instructions(LLVMValueRef function)
{
unsigned num_instrs = 0;
LLVMBasicBlockRef block;
block = LLVMGetFirstBasicBlock(function);
while (block) {
LLVMValueRef instr;
instr = LLVMGetFirstInstruction(block);
while (instr) {
++num_instrs;
instr = LLVMGetNextInstruction(instr);
}
block = LLVMGetNextBasicBlock(block);
}
return num_instrs;
}

View file

@ -398,4 +398,8 @@ lp_build_context_init(struct lp_build_context *bld,
struct lp_type type);
unsigned
lp_build_count_instructions(LLVMValueRef function);
#endif /* !LP_BLD_TYPE_H */

View file

@ -129,6 +129,7 @@ struct llvmpipe_context {
/** List of all fragment shader variants */
struct lp_fs_variant_list_item fs_variants_list;
unsigned nr_fs_variants;
unsigned nr_fs_instrs;
/** JIT code generation */
struct gallivm_state *gallivm;

View file

@ -72,6 +72,12 @@
*/
#define LP_MAX_SHADER_VARIANTS 1024
/**
* Max number of instructions (for all fragment shaders combined per context)
* that will be kept around.
*/
#define LP_MAX_SHADER_INSTRUCTIONS (128*1024)
/**
* Max number of setup variants that will be kept around.
*

View file

@ -804,6 +804,7 @@ generate_fragment(struct llvmpipe_context *lp,
LLVMWriteBitcodeToFile(gallivm->module, "llvmpipe.bc");
}
variant->nr_instrs += lp_build_count_instructions(function);
/*
* Translate the LLVM IR into machine code.
*/
@ -1129,6 +1130,7 @@ llvmpipe_remove_shader_variant(struct llvmpipe_context *lp,
/* remove from context's list */
remove_from_list(&variant->list_item_global);
lp->nr_fs_variants--;
lp->nr_fs_instrs -= variant->nr_instrs;
FREE(variant);
}
@ -1350,11 +1352,22 @@ llvmpipe_update_fs(struct llvmpipe_context *lp)
/* variant not found, create it now */
int64_t t0, t1, dt;
unsigned i;
unsigned variants_to_cull;
if (0) {
debug_printf("%u variants,\t%u instrs,\t%u instrs/variant\n",
lp->nr_fs_variants,
lp->nr_fs_instrs,
lp->nr_fs_variants ? lp->nr_fs_instrs / lp->nr_fs_variants : 0);
}
/* First, check if we've exceeded the max number of shader variants.
* If so, free 25% of them (the least recently used ones).
*/
if (lp->nr_fs_variants >= LP_MAX_SHADER_VARIANTS) {
variants_to_cull = lp->nr_fs_variants >= LP_MAX_SHADER_VARIANTS ? LP_MAX_SHADER_VARIANTS / 4 : 0;
if (variants_to_cull ||
lp->nr_fs_instrs >= LP_MAX_SHADER_INSTRUCTIONS) {
struct pipe_context *pipe = &lp->pipe;
/*
@ -1370,17 +1383,15 @@ llvmpipe_update_fs(struct llvmpipe_context *lp)
* pending for destruction on flush.
*/
if (lp->nr_fs_variants >= LP_MAX_SHADER_VARIANTS) {
for (i = 0; i < LP_MAX_SHADER_VARIANTS / 4; i++) {
struct lp_fs_variant_list_item *item;
if (is_empty_list(&lp->fs_variants_list)) {
break;
}
item = last_elem(&lp->fs_variants_list);
assert(item);
assert(item->base);
llvmpipe_remove_shader_variant(lp, item->base);
for (i = 0; i < variants_to_cull || lp->nr_fs_instrs >= LP_MAX_SHADER_INSTRUCTIONS; i++) {
struct lp_fs_variant_list_item *item;
if (is_empty_list(&lp->fs_variants_list)) {
break;
}
item = last_elem(&lp->fs_variants_list);
assert(item);
assert(item->base);
llvmpipe_remove_shader_variant(lp, item->base);
}
}
@ -1401,6 +1412,7 @@ llvmpipe_update_fs(struct llvmpipe_context *lp)
insert_at_head(&shader->variants, &variant->list_item_local);
insert_at_head(&lp->fs_variants_list, &variant->list_item_global);
lp->nr_fs_variants++;
lp->nr_fs_instrs += variant->nr_instrs;
shader->variants_cached++;
}
}

View file

@ -88,6 +88,9 @@ struct lp_fragment_shader_variant
lp_jit_frag_func jit_function[2];
/* Total number of LLVM instructions generated */
unsigned nr_instrs;
struct lp_fs_variant_list_item list_item_global, list_item_local;
struct lp_fragment_shader *shader;