This is an archive of the discontinued LLVM Phabricator instance.

[InstCombine] Enhance deduction of alignment for aligned_alloc
ClosedPublic

Authored by xbolva00 on Apr 19 2021, 12:22 PM.

Details

Summary

This patch improves https://reviews.llvm.org/D76971 (Deduce attributes for aligned_alloc in InstCombine) and implements "TODO" item mentioned in the review of that patch.

The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a multiple of alignment.

Currently, we simply bail out if we see a non-constant size - change that.

Diff Detail

Event Timeline

xbolva00 created this revision.Apr 19 2021, 12:22 PM
xbolva00 requested review of this revision.Apr 19 2021, 12:22 PM
Herald added a project: Restricted Project. · View Herald TranscriptApr 19 2021, 12:22 PM
jdoerfert accepted this revision.Apr 19 2021, 12:45 PM

The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a multiple of alignment. So for a non-zero alignment size need to be non-zero.

I don't think the last part is true, 0 x Alignment = 0. That said, for non-zero sizes we know alignment holds, so this is fine.

This revision is now accepted and ready to land.Apr 19 2021, 12:45 PM

The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a multiple of alignment. So for a non-zero alignment size need to be non-zero.

I don't think the last part is true, 0 x Alignment = 0. That said, for non-zero sizes we know alignment holds, so this is fine.

So should we just drop isKnownNonZero check? To allow annotation for case like aligned_alloc(32, dynamicsize)?

xbolva00 updated this revision to Diff 338639.Apr 19 2021, 2:15 PM
xbolva00 edited the summary of this revision. (Show Details)
jdoerfert added a comment.EditedApr 19 2021, 4:37 PM

The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a multiple of alignment. So for a non-zero alignment size need to be non-zero.

I don't think the last part is true, 0 x Alignment = 0. That said, for non-zero sizes we know alignment holds, so this is fine.

So should we just drop isKnownNonZero check? To allow annotation for case like aligned_alloc(32, dynamicsize)?

No, isKnownNonZero is the important part.

p = aligned_alloc(32, dynamicsize)
===>
dynamicsize == 0 || p % 32 == 0

(if p would be null for size = 0 we could avoid the check but the standard allows for a special pointer return I think)

jdoerfert added inline comments.Apr 19 2021, 4:39 PM
llvm/test/Transforms/InstCombine/deref-alloc-fns.ll
55

This is now wrong, IMHO.

xbolva00 updated this revision to Diff 338673.Apr 19 2021, 5:02 PM

Revert changes back.

The function aligned_alloc() is the same as memalign(), except for the added restriction that size should be a multiple of alignment. So for a non-zero alignment size need to be non-zero.

I don't think the last part is true, 0 x Alignment = 0. That said, for non-zero sizes we know alignment holds, so this is fine.

So should we just drop isKnownNonZero check? To allow annotation for case like aligned_alloc(32, dynamicsize)?

No, isKnownNonZero is the important part.

p = aligned_alloc(32, dynamicsize)
===>
dynamicsize == 0 || p % 32 == 0

(if p would be null for size = 0 we could avoid the check but the standard allows for a special pointer return I think)

Ah, right. Reverted to previous version which was OK. Thanks.

This revision was landed with ongoing or failed builds.Apr 19 2021, 5:04 PM
This revision was automatically updated to reflect the committed changes.