[Mesa-dev] [PATCH 08/70] mesa: make use of ralloc when creating ARB program instructions

Timothy Arceri timothy.arceri at collabora.com
Fri Nov 11 00:45:50 UTC 2016


This will allow us to move the ARB asm fields in gl_program into
a union as we will be able call ralloc_free() on the entire struct
when destroying the context.
---
 src/mesa/main/ffvertex_prog.c       |  7 +++----
 src/mesa/program/arbprogparse.c     |  9 ++++++---
 src/mesa/program/ir_to_mesa.cpp     |  2 +-
 src/mesa/program/prog_instruction.c | 15 ++-------------
 src/mesa/program/prog_instruction.h |  7 +++----
 src/mesa/program/prog_optimize.c    | 28 +++++++++++++++-------------
 src/mesa/program/prog_optimize.h    |  3 ++-
 src/mesa/program/program.c          | 17 ++++++-----------
 src/mesa/program/program.h          |  3 ++-
 src/mesa/program/program_parse.y    |  5 +++--
 src/mesa/program/program_parser.h   |  3 +++
 src/mesa/program/programopt.c       | 12 ++++++------
 12 files changed, 52 insertions(+), 59 deletions(-)

diff --git a/src/mesa/main/ffvertex_prog.c b/src/mesa/main/ffvertex_prog.c
index 8cec1cb..d4a22c1 100644
--- a/src/mesa/main/ffvertex_prog.c
+++ b/src/mesa/main/ffvertex_prog.c
@@ -586,7 +586,7 @@ static void emit_op3fn(struct tnl_program *p,
       /* double the size */
       p->max_inst *= 2;
 
-      newInst = _mesa_alloc_instructions(p->max_inst);
+      newInst = _mesa_alloc_instructions(p->program, p->max_inst);
       if (!newInst) {
          _mesa_error(NULL, GL_OUT_OF_MEMORY, "vertex program build");
          return;
@@ -595,8 +595,7 @@ static void emit_op3fn(struct tnl_program *p,
       _mesa_copy_instructions(newInst, p->program->Instructions,
                               p->program->NumInstructions);
 
-      _mesa_free_instructions(p->program->Instructions,
-                              p->program->NumInstructions);
+      ralloc_free(p->program->Instructions);
 
       p->program->Instructions = newInst;
    }
@@ -1632,7 +1631,7 @@ create_new_program( const struct state_key *key,
     * If we need more, we'll grow the instruction array as needed.
     */
    p.max_inst = 32;
-   p.program->Instructions = _mesa_alloc_instructions(p.max_inst);
+   p.program->Instructions = _mesa_alloc_instructions(program, p.max_inst);
    p.program->String = NULL;
    p.program->NumInstructions =
    p.program->NumTemporaries =
diff --git a/src/mesa/program/arbprogparse.c b/src/mesa/program/arbprogparse.c
index e545d71..a7e21d9 100644
--- a/src/mesa/program/arbprogparse.c
+++ b/src/mesa/program/arbprogparse.c
@@ -170,17 +170,20 @@ _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
    memset(&prog, 0, sizeof(prog));
    memset(&state, 0, sizeof(state));
    state.prog = &prog;
+   state.mem_ctx = program;
 
    if (!_mesa_parse_arb_program(ctx, target, (const GLubyte*) str, len,
 				&state)) {
+      ralloc_free(prog.Instructions);
+      ralloc_free(prog.String);
       _mesa_error(ctx, GL_INVALID_OPERATION, "glProgramString(bad program)");
       return;
    }
 
    if ((ctx->_Shader->Flags & GLSL_NO_OPT) == 0)
-      _mesa_optimize_program(ctx, &prog);
+      _mesa_optimize_program(ctx, &prog, program);
 
-   free(program->String);
+   ralloc_free(program->String);
 
    /* Copy the relevant contents of the arb_program struct into the 
     * vertex_program struct.
@@ -202,7 +205,7 @@ _mesa_parse_arb_vertex_program(struct gl_context *ctx, GLenum target,
    program->IsPositionInvariant = (state.option.PositionInvariant)
       ? GL_TRUE : GL_FALSE;
 
-   free(program->Instructions);
+   ralloc_free(program->Instructions);
    program->Instructions = prog.Instructions;
 
    if (program->Parameters)
diff --git a/src/mesa/program/ir_to_mesa.cpp b/src/mesa/program/ir_to_mesa.cpp
index f4c2ad6..c2231fb 100644
--- a/src/mesa/program/ir_to_mesa.cpp
+++ b/src/mesa/program/ir_to_mesa.cpp
@@ -2930,7 +2930,7 @@ get_mesa_program(struct gl_context *ctx,
    _mesa_reference_program(ctx, &shader->Program, prog);
 
    if ((ctx->_Shader->Flags & GLSL_NO_OPT) == 0) {
-      _mesa_optimize_program(ctx, prog);
+      _mesa_optimize_program(ctx, prog, prog);
    }
 
    /* This has to be done last.  Any operation that can cause
diff --git a/src/mesa/program/prog_instruction.c b/src/mesa/program/prog_instruction.c
index 28858f6..a37a903 100644
--- a/src/mesa/program/prog_instruction.c
+++ b/src/mesa/program/prog_instruction.c
@@ -64,10 +64,9 @@ _mesa_init_instructions(struct prog_instruction *inst, GLuint count)
  * \return pointer to instruction memory
  */
 struct prog_instruction *
-_mesa_alloc_instructions(GLuint numInst)
+_mesa_alloc_instructions(void *mem_ctx, GLuint numInst)
 {
-   return
-      calloc(numInst, sizeof(struct prog_instruction));
+   return rzalloc_array(mem_ctx, struct prog_instruction, numInst);
 }
 
 
@@ -88,16 +87,6 @@ _mesa_copy_instructions(struct prog_instruction *dest,
 
 
 /**
- * Free an array of instructions
- */
-void
-_mesa_free_instructions(struct prog_instruction *inst, GLuint count)
-{
-   free(inst);
-}
-
-
-/**
  * Basic info about each instruction
  */
 struct instruction_info
diff --git a/src/mesa/program/prog_instruction.h b/src/mesa/program/prog_instruction.h
index 05a9a04..d7de4e9 100644
--- a/src/mesa/program/prog_instruction.h
+++ b/src/mesa/program/prog_instruction.h
@@ -261,19 +261,18 @@ struct prog_instruction
 extern "C" {
 #endif
 
+struct gl_program;
+
 extern void
 _mesa_init_instructions(struct prog_instruction *inst, GLuint count);
 
 extern struct prog_instruction *
-_mesa_alloc_instructions(GLuint numInst);
+_mesa_alloc_instructions(void *mem_ctx, GLuint numInst);
 
 extern struct prog_instruction *
 _mesa_copy_instructions(struct prog_instruction *dest,
                         const struct prog_instruction *src, GLuint n);
 
-extern void
-_mesa_free_instructions(struct prog_instruction *inst, GLuint count);
-
 extern GLuint
 _mesa_num_inst_src_regs(enum prog_opcode opcode);
 
diff --git a/src/mesa/program/prog_optimize.c b/src/mesa/program/prog_optimize.c
index 321cffa..d8bba6d 100644
--- a/src/mesa/program/prog_optimize.c
+++ b/src/mesa/program/prog_optimize.c
@@ -159,7 +159,8 @@ is_swizzle_regular(GLuint swz)
  * \return number of instructions removed
  */
 static GLuint
-remove_instructions(struct gl_program *prog, const GLboolean *removeFlags)
+remove_instructions(struct gl_program *prog, const GLboolean *removeFlags,
+                    void *mem_ctx)
 {
    GLint i, removeEnd = 0, removeCount = 0;
    GLuint totalRemoved = 0;
@@ -184,7 +185,7 @@ remove_instructions(struct gl_program *prog, const GLboolean *removeFlags)
           */
          if (removeCount > 0) {
             GLint removeStart = removeEnd - removeCount + 1;
-            _mesa_delete_instructions(prog, removeStart, removeCount);
+            _mesa_delete_instructions(prog, removeStart, removeCount, mem_ctx);
             removeStart = removeCount = 0; /* reset removal info */
          }
       }
@@ -192,7 +193,7 @@ remove_instructions(struct gl_program *prog, const GLboolean *removeFlags)
    /* Finish removing if the first instruction was to be removed. */
    if (removeCount > 0) {
       GLint removeStart = removeEnd - removeCount + 1;
-      _mesa_delete_instructions(prog, removeStart, removeCount);
+      _mesa_delete_instructions(prog, removeStart, removeCount, mem_ctx);
    }
    return totalRemoved;
 }
@@ -236,7 +237,7 @@ replace_regs(struct gl_program *prog, gl_register_file file, const GLint map[])
  * write to such registers.  Be careful with condition code setters.
  */
 static GLboolean
-_mesa_remove_dead_code_global(struct gl_program *prog)
+_mesa_remove_dead_code_global(struct gl_program *prog, void *mem_ctx)
 {
    GLboolean tempRead[REG_ALLOCATE_MAX_PROGRAM_TEMPS][4];
    GLboolean *removeInst; /* per-instruction removal flag */
@@ -325,7 +326,7 @@ _mesa_remove_dead_code_global(struct gl_program *prog)
    }
 
    /* now remove the instructions which aren't needed */
-   rem = remove_instructions(prog, removeInst);
+   rem = remove_instructions(prog, removeInst, mem_ctx);
 
    if (dbg) {
       printf("Optimize: End dead code removal.\n");
@@ -568,7 +569,7 @@ _mesa_remove_extra_move_use(struct gl_program *prog)
  * with a proper control flow graph
  */
 static GLboolean
-_mesa_remove_dead_code_local(struct gl_program *prog)
+_mesa_remove_dead_code_local(struct gl_program *prog, void *mem_ctx)
 {
    GLboolean *removeInst;
    GLuint i, arg, rem = 0;
@@ -600,7 +601,7 @@ _mesa_remove_dead_code_local(struct gl_program *prog)
          removeInst[i] = GL_TRUE;
    }
 
-   rem = remove_instructions(prog, removeInst);
+   rem = remove_instructions(prog, removeInst, mem_ctx);
 
 done:
    free(removeInst);
@@ -704,7 +705,7 @@ _mesa_merge_mov_into_inst(struct prog_instruction *inst,
  * Try to remove extraneous MOV instructions from the given program.
  */
 static GLboolean
-_mesa_remove_extra_moves(struct gl_program *prog)
+_mesa_remove_extra_moves(struct gl_program *prog, void *mem_ctx)
 {
    GLboolean *removeInst; /* per-instruction removal flag */
    GLuint i, rem = 0, nesting = 0;
@@ -790,7 +791,7 @@ _mesa_remove_extra_moves(struct gl_program *prog)
    }
 
    /* now remove the instructions which aren't needed */
-   rem = remove_instructions(prog, removeInst);
+   rem = remove_instructions(prog, removeInst, mem_ctx);
 
    free(removeInst);
 
@@ -1310,7 +1311,8 @@ _mesa_simplify_cmp(struct gl_program * program)
  * instructions, temp regs, etc.
  */
 void
-_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program)
+_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program,
+                       void *mem_ctx)
 {
    GLboolean any_change;
 
@@ -1319,11 +1321,11 @@ _mesa_optimize_program(struct gl_context *ctx, struct gl_program *program)
    do {
       any_change = GL_FALSE;
       _mesa_remove_extra_move_use(program);
-      if (_mesa_remove_dead_code_global(program))
+      if (_mesa_remove_dead_code_global(program, mem_ctx))
          any_change = GL_TRUE;
-      if (_mesa_remove_extra_moves(program))
+      if (_mesa_remove_extra_moves(program, mem_ctx))
          any_change = GL_TRUE;
-      if (_mesa_remove_dead_code_local(program))
+      if (_mesa_remove_dead_code_local(program, mem_ctx))
          any_change = GL_TRUE;
 
       any_change = _mesa_constant_fold(program) || any_change;
diff --git a/src/mesa/program/prog_optimize.h b/src/mesa/program/prog_optimize.h
index 1f20ac0..c99ce9e 100644
--- a/src/mesa/program/prog_optimize.h
+++ b/src/mesa/program/prog_optimize.h
@@ -46,7 +46,8 @@ _mesa_find_temp_intervals(const struct prog_instruction *instructions,
                           GLint intEnd[MAX_PROGRAM_TEMPS]);
 
 extern void
-_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program);
+_mesa_optimize_program(struct gl_context *ctx, struct gl_program *program,
+                       void *mem_ctx);
 
 extern GLboolean
 _mesa_constant_fold(struct gl_program *prog);
diff --git a/src/mesa/program/program.c b/src/mesa/program/program.c
index f4b36f4..75e9b5b 100644
--- a/src/mesa/program/program.c
+++ b/src/mesa/program/program.c
@@ -247,12 +247,6 @@ _mesa_delete_program(struct gl_context *ctx, struct gl_program *prog)
    if (prog == &_mesa_DummyProgram)
       return;
 
-   free(prog->String);
-   free(prog->LocalParams);
-
-   if (prog->Instructions) {
-      _mesa_free_instructions(prog->Instructions, prog->NumInstructions);
-   }
    if (prog->Parameters) {
       _mesa_free_parameter_list(prog->Parameters);
    }
@@ -358,7 +352,7 @@ _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
    }
 
    /* Alloc storage for new instructions */
-   newInst = _mesa_alloc_instructions(newLen);
+   newInst = _mesa_alloc_instructions(prog, newLen);
    if (!newInst) {
       return GL_FALSE;
    }
@@ -375,7 +369,7 @@ _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
                            origLen - start);
 
    /* free old instructions */
-   _mesa_free_instructions(prog->Instructions, origLen);
+   ralloc_free(prog->Instructions);
 
    /* install new instructions */
    prog->Instructions = newInst;
@@ -389,7 +383,8 @@ _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count)
  * Adjust branch targets accordingly.
  */
 GLboolean
-_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
+_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
+                          void *mem_ctx)
 {
    const GLuint origLen = prog->NumInstructions;
    const GLuint newLen = origLen - count;
@@ -407,7 +402,7 @@ _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
    }
 
    /* Alloc storage for new instructions */
-   newInst = _mesa_alloc_instructions(newLen);
+   newInst = _mesa_alloc_instructions(mem_ctx, newLen);
    if (!newInst) {
       return GL_FALSE;
    }
@@ -421,7 +416,7 @@ _mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count)
                            newLen - start);
 
    /* free old instructions */
-   _mesa_free_instructions(prog->Instructions, origLen);
+   ralloc_free(prog->Instructions);
 
    /* install new instructions */
    prog->Instructions = newInst;
diff --git a/src/mesa/program/program.h b/src/mesa/program/program.h
index 6460948..18122f7 100644
--- a/src/mesa/program/program.h
+++ b/src/mesa/program/program.h
@@ -93,7 +93,8 @@ extern  GLboolean
 _mesa_insert_instructions(struct gl_program *prog, GLuint start, GLuint count);
 
 extern  GLboolean
-_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count);
+_mesa_delete_instructions(struct gl_program *prog, GLuint start, GLuint count,
+                          void *mem_ctx);
 
 extern void
 _mesa_find_used_registers(const struct gl_program *prog,
diff --git a/src/mesa/program/program_parse.y b/src/mesa/program/program_parse.y
index 41aeb24..760b592 100644
--- a/src/mesa/program/program_parse.y
+++ b/src/mesa/program/program_parse.y
@@ -2511,7 +2511,7 @@ _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *st
 
    /* Make a copy of the program string and force it to be NUL-terminated.
     */
-   strz = (GLubyte *) malloc(len + 1);
+   strz = (GLubyte *) ralloc_size(state->mem_ctx, len + 1);
    if (strz == NULL) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY, "glProgramStringARB");
       return GL_FALSE;
@@ -2565,7 +2565,8 @@ _mesa_parse_arb_program(struct gl_context *ctx, GLenum target, const GLubyte *st
    /* Add one instruction to store the "END" instruction.
     */
    state->prog->Instructions =
-      _mesa_alloc_instructions(state->prog->NumInstructions + 1);
+      _mesa_alloc_instructions(state->mem_ctx,
+                               state->prog->NumInstructions + 1);
 
    if (state->prog->Instructions == NULL) {
       goto error;
diff --git a/src/mesa/program/program_parser.h b/src/mesa/program/program_parser.h
index 05ceb92..412aca1 100644
--- a/src/mesa/program/program_parser.h
+++ b/src/mesa/program/program_parser.h
@@ -132,6 +132,9 @@ struct asm_parser_state {
    struct gl_context *ctx;
    struct gl_program *prog;
 
+   /** Memory context to attach instructions to. */
+   void *mem_ctx;
+
    /**
     * Per-program target limits
     */
diff --git a/src/mesa/program/programopt.c b/src/mesa/program/programopt.c
index 1b50b5b..2d03376 100644
--- a/src/mesa/program/programopt.c
+++ b/src/mesa/program/programopt.c
@@ -70,7 +70,7 @@ _mesa_insert_mvp_dp4_code(struct gl_context *ctx, struct gl_program *vprog)
    }
 
    /* Alloc storage for new instructions */
-   newInst = _mesa_alloc_instructions(newLen);
+   newInst = _mesa_alloc_instructions(vprog, newLen);
    if (!newInst) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY,
                   "glProgramString(inserting position_invariant code)");
@@ -102,7 +102,7 @@ _mesa_insert_mvp_dp4_code(struct gl_context *ctx, struct gl_program *vprog)
    _mesa_copy_instructions (newInst + 4, vprog->Instructions, origLen);
 
    /* free old instructions */
-   _mesa_free_instructions(vprog->Instructions, origLen);
+   ralloc_free(vprog->Instructions);
 
    /* install new instructions */
    vprog->Instructions = newInst;
@@ -138,7 +138,7 @@ _mesa_insert_mvp_mad_code(struct gl_context *ctx, struct gl_program *vprog)
    }
 
    /* Alloc storage for new instructions */
-   newInst = _mesa_alloc_instructions(newLen);
+   newInst = _mesa_alloc_instructions(vprog, newLen);
    if (!newInst) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY,
                   "glProgramString(inserting position_invariant code)");
@@ -203,7 +203,7 @@ _mesa_insert_mvp_mad_code(struct gl_context *ctx, struct gl_program *vprog)
    _mesa_copy_instructions (newInst + 4, vprog->Instructions, origLen);
 
    /* free old instructions */
-   _mesa_free_instructions(vprog->Instructions, origLen);
+   ralloc_free(vprog->Instructions);
 
    /* install new instructions */
    vprog->Instructions = newInst;
@@ -270,7 +270,7 @@ _mesa_append_fog_code(struct gl_context *ctx, struct gl_program *fprog,
    }
 
    /* Alloc storage for new instructions */
-   newInst = _mesa_alloc_instructions(newLen);
+   newInst = _mesa_alloc_instructions(fprog, newLen);
    if (!newInst) {
       _mesa_error(ctx, GL_OUT_OF_MEMORY,
                   "glProgramString(inserting fog_option code)");
@@ -403,7 +403,7 @@ _mesa_append_fog_code(struct gl_context *ctx, struct gl_program *fprog,
    inst++;
 
    /* free old instructions */
-   _mesa_free_instructions(fprog->Instructions, origLen);
+   ralloc_free(fprog->Instructions);
 
    /* install new instructions */
    fprog->Instructions = newInst;
-- 
2.7.4



More information about the mesa-dev mailing list