This is an archive of the discontinued LLVM Phabricator instance.

Bug 14070 - Deeply nested struct types cause opt run time to explode
AbandonedPublic

Authored by iid_iunknown on Aug 15 2014, 7:53 AM.

Details

Summary

PROBLEM

The problem appears on deeply nested structures due to the getFoldedSizeOf implementation (lib/IR/ConstantFold.cpp), which is very ineffective on such data types.

getFoldedSizeOf runs through the structure's fields to find out if all the fields are of the same size. If the function finds they are of different size, it calls ConstantExpr::getSizeOf with the structure type as an argument. ConstantExpr::getSizeOf internally leads to getFoldedSizeOf call that iterates the structure's fields again calculating structure size.

Since getFoldedSizeOf has recursive nature, the number of its calls grows immensely with structure nesting depth increase. E.g., for the attached structure having nesting depth=21 it is called 31719423 times.

PROPOSED SOLUTION

getFoldedSizeOf is doing the same work twice:

  1. when iterating the structure's fields to compare field sizes;
  2. when finally calling ConstantExpr::getSizeOf to calculate structure size.

The idea is to eliminate the 2nd step and calculate the whole size at the 1st step by summarizing sizes of separate fields.

This solution reduces getFoldedSizeOf calls from over 31M to about 80. The structure is processed in a fraction of a second.

Regression and performance tests show no regression.

Diff Detail

Event Timeline

iid_iunknown retitled this revision from to Bug 14070 - Deeply nested struct types cause opt run time to explode.
iid_iunknown updated this object.
iid_iunknown edited the test plan for this revision. (Show Details)
iid_iunknown added reviewers: rafael, chandlerc.
iid_iunknown set the repository for this revision to rL LLVM.
iid_iunknown added a project: deleted.
iid_iunknown added a subscriber: Unknown Object (MLST).

I suspect Nick is a better reviewer here than I am... The structure of this code makes zero sense to me and I suspect it should be refactored more heavily than just this point fix.... But he would know better.

You should at least fix the formatting errors. Run clang-format over the code maybe?

chandlerc resigned from this revision.Mar 29 2015, 1:07 PM
chandlerc removed a reviewer: chandlerc.

I suspect Nick is a better reviewer here than I am... The structure of this code makes zero sense to me and I suspect it should be refactored more heavily than just this point fix.... But he would know better.

You should at least fix the formatting errors. Run clang-format over the code maybe?

iid_iunknown abandoned this revision.Dec 30 2015, 1:55 PM