This is an archive of the discontinued LLVM Phabricator instance.

Optimize InstCombine stack memory consumption
ClosedPublic

Authored by kariddi on Jul 1 2014, 7:26 AM.

Details

Summary

Hello,

the proposed patch aims to reduce the stack memory consumption of the InstCombine function "isOnlyCopiedFromConstantGlobal() ", that in certain conditions could overflow the stack because of excessive recursiveness.

For example, in a case like this:

%0 = alloca [50025 x i32], align 4
%1 = getelementptr inbounds [50025 x i32]* %0, i64 0, i64 0
store i32 0, i32* %1
%2 = getelementptr inbounds i32* %1, i64 1
store i32 1, i32* %2
%3 = getelementptr inbounds i32* %2, i64 1
store i32 2, i32* %3
%4 = getelementptr inbounds i32* %3, i64 1
store i32 3, i32* %4
%5 = getelementptr inbounds i32* %4, i64 1
store i32 4, i32* %5
%6 = getelementptr inbounds i32* %5, i64 1
store i32 5, i32* %6
...

This piece of code crashes llvm when trying to apply instcombine on desktop. On embedded devices this could happen with a much lower limit of recursiveness.
Some instructions (getelementptr and bitcasts) make the function recursively call itself on their uses, which is what makes the example above consume so much stack (it becomes a recursive depth-first tree visit with a very big depth).

The patch changes the algorithm to be semantically equivalent, but iterative instead of recursive and the visiting order to be from a depth-first visit to a breadth-first visit (visit all the instructions of the current level before the ones of the next one).

Now if a lot of memory is required a heap allocation is done instead of the the stack allocation, avoiding the possible crash.

Please, help review this patch :)

Diff Detail

Event Timeline

kariddi updated this revision to Diff 10999.Jul 1 2014, 7:26 AM
kariddi retitled this revision from to Optimize InstCombine stack memory consumption.
kariddi updated this object.
kariddi edited the test plan for this revision. (Show Details)
kariddi set the repository for this revision to rL LLVM.
kariddi added a project: deleted.
kariddi added a subscriber: Unknown Object (MLST).
rnk accepted this revision.Jul 1 2014, 1:59 PM
rnk added a reviewer: rnk.
rnk added a subscriber: rnk.

lgtm

lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
63

Why rename 'U' to 'Use'? 'Use' already a type, so this is a bit confusing. If anything, this loop should iterate over users, not uses.

This revision is now accepted and ready to land.Jul 1 2014, 1:59 PM
kariddi updated this revision to Diff 11007.Jul 1 2014, 2:27 PM
kariddi edited edge metadata.

Changed variable name from Use to U.

If the patch is ok and nobody has any other concern I need somebody to commit it because I don't have commit access :)

kariddi added inline comments.Jul 1 2014, 2:35 PM
lib/Transforms/InstCombine/InstCombineLoadStoreAlloca.cpp
63

Thank you Reid, I agree with what you say. I fixed it in the next revision

rnk closed this revision.Jul 1 2014, 2:46 PM

Thanks, landed in r212133.