[Mesa-dev] [PATCH 1/2] nir: Use alloca instead of variable length arrays.

Jose Fonseca jfonseca at vmware.com
Tue Mar 3 05:20:40 PST 2015


On 02/03/15 20:03, Jason Ekstrand wrote:
>
>
> On Mon, Mar 2, 2015 at 8:02 AM, Jose Fonseca <jfonseca at vmware.com
> <mailto:jfonseca at vmware.com>> wrote:
>
>     On 27/02/15 16:15, Brian Paul wrote:
>
>         On 02/27/2015 09:10 AM, Ian Romanick wrote:
>
>             On 02/26/2015 10:07 AM, Brian Paul wrote:
>
>                 On 02/26/2015 09:51 AM, Jose Fonseca wrote:
>
[...]
>
>             It occurs to me that this may be the only reasonable
>             use-case in all
>             history of memset returning the pointer that you pass to it:
>
>                   unsigned *work = memset(alloca(work_size), 0, work_size);
>
>
>         That's cool!
>
>
>     Yes, indeed, that can save another source line.
>
>
>     FWIW, I also tried to prototype a few macros for dealing with VLAs:
>
>     http://cgit.freedesktop.org/~__jrfonseca/mesa/commit/?h=nir-__vla
>     <https://urldefense.proofpoint.com/v2/url?u=http-3A__cgit.freedesktop.org_-7Ejrfonseca_mesa_commit_-3Fh-3Dnir-2Dvla&d=AwMFaQ&c=Sqcl0Ez6M0X8aeM67LKIiDJAXVeAw-YihVMNtXt-uEs&r=zfmBZnnVGHeYde45pMKNnVyzeaZbdIqVLprmZCM2zzE&m=GcBQbNm0KBs2XIKN1fx-lDFftq6SDUMPhaSl0l9qLqs&s=J8rc6LjrrcurIGknA42ShkJEmizswzfrcexlCzrWdRs&e=>
>
>     On one hand, they save quite a bit of typing, even compared with
>     vanilla C99 VLAs.  On the other hand, being macros it's always a bit
>     magical and tricky to read..
>
>
> I'm OK with the macro magic.  I have two qualms, however.  First, I'm
> not sure I like the fact that it defines a second size variable behind
> your back.  Second, I'd like to keep it to one line if we can and
> require the user to provide the semicolon.  Since we already have the
> type from the user, this shouldn't be hard as we can do
>
> _type *_name = alloca((_length) * sizeof(_type))
>
> Then we don't have to worry about when the variable is declared vs. when
> the sizeof happens.

Sure. I've just posted a review request that does this.

A semi-colon is already required (I was only using semi-colon for all 
but the last statements), but with the Ian's memset trick we can indeed 
squeeze everything into a single statement.

The only drawback of not declaring an additional variable with the size 
is that we'll need to evaluate the size twice for VLA_FILL/ZERO 
variants, therefore care must be taken to never use expressions with 
side effects (+=, ++, --, funcions, etc).

 > Also, hiding it in a macro means that we could do
>
> _type _name[_length]
>
> for GCC/clang builds if we wanted to.

I also thought about it, but unfortunately that would prevent using 
-Werror=vla, and therefore prevent catching instances where people might 
inadvertently use VLA directly without these macros.

But at least having these macro means, when one day MSVC supports VLA, 
it's trivial to refactor the code to use them.

Jose


More information about the mesa-dev mailing list