[flang-commits] [flang] [flang][Lower] Add alternative real expression lowering (PR #207371)

via flang-commits flang-commits at lists.llvm.org
Mon Jul 6 03:22:05 PDT 2026


https://github.com/jeanPerier commented:

Thanks for experimenting with this @tblah, this is interesting, and doing expression balancing at the expression level has priort art in classic flang.

Do you know what is having the most impact with your change on SNBone, is it this the balancing of the expression in general (into a tree with less depth), or is it specifically related to the self assignment context (`x = x + ....`) where moving the read and add of `x` at the end of the chain reduce the dependency and allows for more vectorization.

I am asking to try to understand if there is really a point to limit this to assignment context, and also because if reducing the height of the expression tree is a purpose, maybe this should be a done more generically rather than just moving the head position (which only reduces the height by one while in theory with a long add chain you could reduce the height in a logarithmic way).

My main concern is to be doing it in lowering and to lower expressions differently when it is a LHS. I however understand your concerns about losing the expression context in the IR. We could improve the IR to preserve that info at least for some time in MLIR. There would be other benefit from having "statement aware" optimizations since Fortran gives some statement level guarantees, but this would be a much bigger change and I am OK to have it as an Expr rewrite to start with.

I am also curious of the limitations (no function reference, parenthesis (in the terms, I get why parenthesis around the adds would prevent it), vector subscripts...) inside the expressions, are you seeing the same restriction with classic flang? Should the rewrite also be applied to sub-expression trees or does it only matters at the top level?

FYI, I think that classic flang is doing that expression rebalancing here: https://github.com/flang-compiler/flang/blob/b8c0bd9e53b682057684e21644e823a819e2d363/tools/flang1/flang1exe/semant2.c#L2925

https://github.com/llvm/llvm-project/pull/207371


More information about the flang-commits mailing list