This is an archive of the discontinued LLVM Phabricator instance.

[flang] Limit the depth of expressions that the compiler will handle
AbandonedPublic

Authored by PeteSteinfeld on May 11 2021, 10:44 AM.

Details

Summary

The compiler analyzes expressions recusively. Large expressions consume large
amounts of stack space. Very large expressions can cause the compiler to
overflow the stack, which results in a seg fault.

I fixed this by imposing a limit of 1000 for the depth of the expression tree
analyzed by the compiler. Upon reaching this limit, the compiler will emit a
message, unwind from the analysis of that expression, and proceed with the rest
of the compilation.

The limit of 1000 is a little arbitrary. I experimentally verified that the
compiler cannot handle a limit of 5000, and 1000 seems big enough.

I added a test that will produce a seg fault without this change. It also
contains an expression with 1000 operations that gets constant folded. This
demonstrates that, if we can analyze an expression, we can fold it. I also
included two cases where we create array constructors of many more elements
than the expression limit to verify that we can handle large array
constructors.

Diff Detail

Event Timeline

PeteSteinfeld created this revision.May 11 2021, 10:44 AM
PeteSteinfeld requested review of this revision.May 11 2021, 10:44 AM
Herald added a project: Restricted Project. · View Herald TranscriptMay 11 2021, 10:44 AM
PeteSteinfeld added a project: Restricted Project.

Instead of imposing an arbitrary limit on the size of an expression it would be better to change the algorithm to handle larger trees with more use of iteration and tail recursion, I think.

Instead of imposing an arbitrary limit on the size of an expression it would be better to change the algorithm to handle larger trees with more use of iteration and tail recursion, I think.

I considered getting rid of recursion for expression processing, but it seemed like a relatively big effort. I'll have to think about the implications of your suggestion.

PeteSteinfeld abandoned this revision.Sep 17 2021, 6:26 AM