[flang] Avoid crashing from recursion on very tall expression parse trees
authorPeter Klausler <pklausler@nvidia.com>
Thu, 19 Jan 2023 22:32:09 +0000 (14:32 -0800)
committerPeter Klausler <pklausler@nvidia.com>
Wed, 1 Feb 2023 22:09:07 +0000 (14:09 -0800)
commitb617df6e7cbc35230a4b7140cf66dabebe9700e7
treedb5db2e6b6e818cd45842231071a8d45e200811a
parent27004e027312a59e3b6645f5df58e97c2a2da6ef
[flang] Avoid crashing from recursion on very tall expression parse trees

In the parse tree visitation framework (Parser/parse-tree-visitor.h)
and in the semantic analyzer for expressions (Semantics/expression.cpp)
avoid crashing due to stack size limitations by using an iterative
traversal algorithm rather than straightforward recursive tree walking.
The iterative approach is the obvious one of building a work queue and
using it to (in the case of the parse tree visitor) call the visitor
object's Pre() and Post() routines on subexpressions in the same order
as they would have been called during a recursive traversal.

This change helps the compiler survive some artificial stress tests
and perhaps with future exposure to machine-generated source code.

Differential Revision: https://reviews.llvm.org/D142771
flang/include/flang/Parser/parse-tree-visitor.h
flang/include/flang/Semantics/expression.h
flang/lib/Semantics/expression.cpp
flang/test/Evaluate/big-expr-tree.F90 [new file with mode: 0644]