On 29.08.23 18:28, Jakub Jelinek wrote:
One thing is that for C++ one needs to be careful about vars optimized by NRV by the FE.
Thanks for the warning.
And, just from the gimplify_bind_expr function name,
(I forgot that one change in there which makes it look larger is that I moved the stack handling below the variable cleaning as it makes sense to GOMP_free before instead of after wiping the stack via __builtin_stack_restore.)
I'm guessing you are adding the allocations at the start of surrounding BIND_EXPR, is that where we generally want to allocate them?
I actually started with this in a FE implementation, but that does not work in general. I need to put the GOMP_alloc right before the associated DECL_EXPR, otherwise, it will break for code like: omp_allocator_handle_t my_allocator = omp_low_lat_mem_alloc; int n = 5; pragma omp allocate(n) allocator(my_allocator) int B[n] where my_allocator strictly needs to be (initialized) before GOMP_alloc and the 'n = 5' - but that needs in turn to happen before the 'int B[n]' to make sure 'n' is allocated and has the value 5.
Other option is on DECL_EXPR, e.g. for C++ right before they are constructed, or for C initialized if they have initializer. Though, that can have a drawback, one can e.g. in C (and I think in C++ too as long as the vars don't need any kind of construction) jump across that initialization and one would then bypass this allocation.
For your example, I get (the full gimple dump is below): foo.c:7:11: warning: statement will never be executed [-Wswitch-unreachable] 7 | int j; I wonder whether we should just error out as GCC does when using a VLA at that place: error: switch jumps into scope of identifier with variably modified type (with a similar but modified msg wording.) In a sense, the 'allocate' is very similar to VLA such that we should be able to piggyback on the VLA diagnostic. Tobias PS: The gimple dump for your program: void foo (int i) { switch (i) <default: <D.2896>, case 42: <D.2895>> { int j [value-expr: *D.2899]; try { D.2899 = __builtin_GOMP_alloc (0, 4, 0B); <D.2895>: *D.2899 = 5; use (D.2899); goto <D.2897>; <D.2896>: *D.2899 = 24; use (D.2899); goto <D.2897>; } finally { __builtin_GOMP_free (D.2899, 0B); } } <D.2897>: } ----------------- Siemens Electronic Design Automation GmbH; Anschrift: Arnulfstraße 201, 80634 München; Gesellschaft mit beschränkter Haftung; Geschäftsführer: Thomas Heurung, Frank Thürauf; Sitz der Gesellschaft: München; Registergericht München, HRB 106955