Currently, the memory pattern optimization relies on all functions being inlined and then searches for elementwise accesses as its heuristic to disable function promotion.
It would be really nice if we instead allowed things like
transformed parameters {
matrix[N,M] foo = some_function(N, M, other_arguments) + possibly_other_expressions;
}
to be code generated to something like auto foo = to_soa(some_function(...) ...) when there are no further elementwise accesses of foo, even if the code in some_function does use for-loops to construct it. This is sort of on-the-way to something like a comprehension, but I believe it is implementable right now for many models.