This is the final patch. Context diff also provided as gfc_conv_intrinsic_arith has many blank changes. The changes in gfc_conv_intrinsic_arith involve the new initialization for parent_se and ploop, a few additional conditions for scalar-specific stuff, and a specific handling for scalar masks in the non-scalar case. The rest is the sum array walking/the construction of the base nested gfc_ss structs. We are trying to allow more than one level of sums. The full sum "ARRAY" argument is passed to gfc_walk_subexpr; it returns a chain of full arrays, to which we attach that of the "MASK" argument. From the resulting chain of full arrays, dimension "DIM" is then moved to a new chain of gfc_ss structs. This move in fact is only a move of one element of gfc_ss struct's dim array, as all the rest is shared. The result of this is a chain of reduced (by one dimension) gfc_ss structs, with on each one of them a nested_ss pointer pointing to a gfc_ss struct having the missing dimension. If the sum result is itself an actual argument to another sum call, the reduced chain is reduced further by one dimension and one gets three levels of gfc_ss structs, etc. The handling of reversed vs non-reversed is quite a mess (see the comment of nest_loop_dimension), but I think it's correct. The handling of walk_inline_intrinsic_arith is complicated by the fact that arguments are not consumed by gfc_conv_intrinsic_arith in the same order, depending on whether the mask is array or scalar. So we have to make sure that it is at the right position in the chain generated by walk_inline_intrinsic_arith. See the comment there. OK?