This is an archive of the discontinued LLVM Phabricator instance.

Fix size computation of array allocation in inline cost analysis
ClosedPublic

Authored by eraman on Jun 24 2016, 10:25 AM.

Details

Summary

There are two bugs in computing size allocated by an array alloca:

  • For primitive types, it computes the number of bits and there is a FIXME
  • For aggregate types, the computed size is always 0.

The fix is to use DL.getTypeAllocSize(Ty) as it is done for static alloca

Diff Detail

Repository
rL LLVM

Event Timeline

eraman updated this revision to Diff 61805.Jun 24 2016, 10:25 AM
eraman retitled this revision from to Fix size computation of array allocation in inline cost analysis.
eraman updated this object.
eraman added a reviewer: chandlerc.
eraman added a subscriber: llvm-commits.

While you're here, could you fix this to handle overflow correctly?

declare void @h(i32*)

define void @f(i128 %x) {
   %y = alloca i32, i128 %x
   call void @h(i32* %y)
   ret void
}

define void @g() {
  call void @f(i128 100000000000000000000000000000000000)
  ret void
}
eraman updated this revision to Diff 61858.Jun 24 2016, 5:00 PM

Handle overflow in computing stack size.

eli.friedman added inline comments.Jun 24 2016, 5:19 PM
lib/Analysis/InlineCost.cpp
347 ↗(On Diff #61858)

The addition can overflow as well.

Maybe use SaturatingMultiplyAdd from MathExtras.h instead of APInt?

eraman updated this revision to Diff 62021.Jun 27 2016, 2:48 PM

Use SaturatingAdd and SaturatingMultiplyAdd to update AllocatedSize in visitAlloca

eli.friedman accepted this revision.Jun 27 2016, 3:05 PM
eli.friedman added a reviewer: eli.friedman.

LGTM.

This revision is now accepted and ready to land.Jun 27 2016, 3:05 PM
This revision was automatically updated to reflect the committed changes.