This is an archive of the discontinued LLVM Phabricator instance.

[Align] Add isAligned taking an APInt
ClosedPublic

Authored by gchatelet on Jun 20 2023, 7:42 AM.

Diff Detail

Event Timeline

gchatelet created this revision.Jun 20 2023, 7:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 7:42 AM
gchatelet requested review of this revision.Jun 20 2023, 7:42 AM
Herald added a project: Restricted Project. · View Herald TranscriptJun 20 2023, 7:42 AM
gchatelet updated this revision to Diff 532932.Jun 20 2023, 7:43 AM
  • Rename variable
courbet accepted this revision.Jun 20 2023, 7:50 AM
This revision is now accepted and ready to land.Jun 20 2023, 7:50 AM
nikic requested changes to this revision.Jun 20 2023, 7:59 AM
nikic added a subscriber: nikic.

This does not look worth an APInt.h include in Alignment.h.

This revision now requires changes to proceed.Jun 20 2023, 7:59 AM
gchatelet added a comment.EditedJun 20 2023, 8:07 AM

@nikic I agree this is not ideal. Whether it is APInt.h including Alignment.h or the opposite...
Do you have a suggestion on where this should land? If at all?

An alternative would be to document how to handle APInt in Alignment.h but not include the code (it wouldn't be tested though).

Make it a template in the header ;)

template<typename APIntTy>
bool isAligned(Align LHS, const APIntTy &SizeInBytes) {
...
}
arsenm added a subscriber: arsenm.Jun 21 2023, 4:23 PM

Could also add isAligned to APInt?

Make it a template in the header ;)

template<typename APIntTy>
bool isAligned(Align LHS, const APIntTy &SizeInBytes) {
...
}

Yep courbet@ made the same suggestion to me but then it conflicts with various types that have implicit conversion to uin64_t most notably TypeSize.
It could work with some enable_it_t incantation but I'm not sure it bears its own weight.

Could also add isAligned to APInt?

I believe @nikic is opposed to this as well as it would also add a dependency.

Could also add isAligned to APInt?

I believe @nikic is opposed to this as well as it would also add a dependency.

There's no dependency if you just use the raw value instead of the Align wrapper. Also could forward declare Align?

Could also add isAligned to APInt?

I believe @nikic is opposed to this as well as it would also add a dependency.

There's no dependency if you just use the raw value instead of the Align wrapper. Also could forward declare Align?

I just checked and it seems that clang is able to optimize out the additional operations
https://godbolt.org/z/G65eKTY3b

Now it's a bit of a bummer to lose the type since you also lose the isPowerOfTwo semantic associated with it :-/
That was the original motivation behind Align.

Forward declaring wouldn't work as-is because Log2 takes Align by value.
I can try to make it take a const ref instead... let's see.

gchatelet updated this revision to Diff 533568.Jun 22 2023, 5:28 AM
  • Implement isAligned as an APInt member function with Align being forward declared.
arsenm added inline comments.Jun 22 2023, 5:56 AM
llvm/include/llvm/ADT/APInt.h
439

Pass by value or just use the unsigned?

gchatelet added inline comments.Jun 22 2023, 6:52 AM
llvm/include/llvm/ADT/APInt.h
439

If we pass by value then we can't forward declare anymore as layout information is needed to copy the value.
Using uint64_t means giving up on types and writing .value() at all call sites.

nikic added a comment.Jun 23 2023, 6:03 AM

Does isAligned() need to be defined in the header? If it's in the source file then we just need the forward declaration.

gchatelet updated this revision to Diff 534527.Jun 26 2023, 6:42 AM
  • Implement the function in the source file to allow pass by value.

Does isAligned() need to be defined in the header? If it's in the source file then we just need the forward declaration.

I never thought it would work to forward declare and pass by value even if the implementation is in the source file. Thx for the suggestion!

nikic added inline comments.Jun 26 2023, 6:47 AM
llvm/lib/Support/APInt.cpp
168

Copy and paste mistake :) You can just drop the comment as it's already in the header.

llvm/unittests/ADT/APIntTest.cpp
1861

What does the k here mean?

gchatelet updated this revision to Diff 534538.Jun 26 2023, 6:58 AM
  • Remove comment
  • Rename variable
gchatelet marked 2 inline comments as done.Jun 26 2023, 7:00 AM
gchatelet added inline comments.
llvm/lib/Support/APInt.cpp
168

Duh! Thx : )

llvm/unittests/ADT/APIntTest.cpp
1861

k is for constants but it's not constant here and it's from another style guide... Thx for noticing.

nikic accepted this revision.Jun 26 2023, 8:12 AM

LGTM

This revision is now accepted and ready to land.Jun 26 2023, 8:12 AM
This revision was landed with ongoing or failed builds.Jun 27 2023, 2:28 AM
This revision was automatically updated to reflect the committed changes.
gchatelet marked 2 inline comments as done.