[Mesa-dev] [PATCH 4/7] mesa: Convert some runtime asserts to static asserts.

Eric Anholt eric at anholt.net
Sun Sep 22 12:46:35 PDT 2013


Brian Paul <brian.e.paul at gmail.com> writes:

> On Fri, Sep 20, 2013 at 7:52 PM, Eric Anholt <eric at anholt.net> wrote:
>> Noticed while grepping through the code for something else.
>> ---
>>  src/mesa/program/program.c | 36 ++++++++++++++++++++++--------------
>>  1 file changed, 22 insertions(+), 14 deletions(-)
>>
>> diff --git a/src/mesa/program/program.c b/src/mesa/program/program.c
>> index 2529c13..5dd68d9 100644
>> --- a/src/mesa/program/program.c
>> +++ b/src/mesa/program/program.c
>> @@ -55,27 +55,35 @@ _mesa_init_program(struct gl_context *ctx)
>>      * If this assertion fails, we need to increase the field
>>      * size for register indexes (see INST_INDEX_BITS).
>>      */
>> -   ASSERT(ctx->Const.VertexProgram.MaxUniformComponents / 4
>> +   STATIC_ASSERT(ctx->Const.VertexProgram.MaxUniformComponents / 4
>>            <= (1 << INST_INDEX_BITS));
>> -   ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents / 4
>> +   STATIC_ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents / 4
>>            <= (1 << INST_INDEX_BITS));
>>
>> -   ASSERT(ctx->Const.VertexProgram.MaxTemps <= (1 << INST_INDEX_BITS));
>> -   ASSERT(ctx->Const.VertexProgram.MaxLocalParams <= (1 << INST_INDEX_BITS));
>> -   ASSERT(ctx->Const.FragmentProgram.MaxTemps <= (1 << INST_INDEX_BITS));
>> -   ASSERT(ctx->Const.FragmentProgram.MaxLocalParams <= (1 << INST_INDEX_BITS));
>> -
>> -   ASSERT(ctx->Const.VertexProgram.MaxUniformComponents <= 4 * MAX_UNIFORMS);
>> -   ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents <= 4 * MAX_UNIFORMS);
>> -
>> -   ASSERT(ctx->Const.VertexProgram.MaxAddressOffset <= (1 << INST_INDEX_BITS));
>> -   ASSERT(ctx->Const.FragmentProgram.MaxAddressOffset <= (1 << INST_INDEX_BITS));
>> +   STATIC_ASSERT(ctx->Const.VertexProgram.MaxTemps <=
>> +                 (1 << INST_INDEX_BITS));
>> +   STATIC_ASSERT(ctx->Const.VertexProgram.MaxLocalParams <=
>> +                 (1 << INST_INDEX_BITS));
>> +   STATIC_ASSERT(ctx->Const.FragmentProgram.MaxTemps <=
>> +                 (1 << INST_INDEX_BITS));
>> +   STATIC_ASSERT(ctx->Const.FragmentProgram.MaxLocalParams <=
>> +                 (1 << INST_INDEX_BITS));
>> +
>> +   STATIC_ASSERT(ctx->Const.VertexProgram.MaxUniformComponents <=
>> +                 4 * MAX_UNIFORMS);
>> +   STATIC_ASSERT(ctx->Const.FragmentProgram.MaxUniformComponents <=
>> +                 4 * MAX_UNIFORMS);
>> +
>> +   STATIC_ASSERT(ctx->Const.VertexProgram.MaxAddressOffset <=
>> +                 (1 << INST_INDEX_BITS));
>> +   STATIC_ASSERT(ctx->Const.FragmentProgram.MaxAddressOffset <=
>> +                 (1 << INST_INDEX_BITS));
>
> Are you sure about those?  How does the compiler know the values of
> ctx->Const.Foo at compile time?
>
> It's worrisome that our STATIC_ASSERT macro is silent at compile time
> when the expression isn't a compile-time constant.  The problem with
> our macro now is variable-sized arrays are OK at compile time.

Having looked at things a bit more, it seems to be an intentional
feature of the macro.  You want the compiler to stop if it can prove
that the assert fails, but not if it can't prove the value either way
(think -O0, where constant expression evaluation is much more limited).
I'm not sure how to make a more emphatic macro that won't make us
occasionally check in broken code in the -O0 case (which I know I'm not
testing, at least).
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 835 bytes
Desc: not available
URL: <http://lists.freedesktop.org/archives/mesa-dev/attachments/20130922/3c2144bc/attachment.pgp>


More information about the mesa-dev mailing list