This is an archive of the discontinued LLVM Phabricator instance.

[AggressiveInstCombine] Recognize table-based ctz implementation and enable it for AARCH64 at -O3
Needs RevisionPublic

Authored by gsocshubham on Feb 4 2022, 9:04 AM.

Details

Summary

This patch is improved version of patch https://reviews.llvm.org/D113291 submitted by @djtodoro
Credit goes to @djtodoro for his submission.

I have added fix in AArch64 backend and I have followed an approach suggested by @craig.topper here -
https://github.com/llvm/llvm-project/issues/45779#issuecomment-1022851172

It generates below assembly -

rbit    w8, w0
clz     w8, w8
and     w0, w8, #0x1f
ret

which is similar assembly emitted by GCC AArch64 - https://gcc.gnu.org/bugzilla/show_bug.cgi?id=90838

Patch fixes - https://github.com/llvm/llvm-project/issues/45779

Diff Detail

Event Timeline

gsocshubham created this revision.Feb 4 2022, 9:04 AM
gsocshubham requested review of this revision.Feb 4 2022, 9:04 AM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 4 2022, 9:04 AM

How hard is to implement support for x86?

Please split the lowering fix from the intrinsic recognition.

craig.topper added inline comments.Feb 4 2022, 9:32 AM
llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
442

getPointerElementType() will be deleted when we move to OpaquePtr. Use LI->getType()?

450

GEP->getSourceElementType() I think.

468

Constant->ConstantInt? And isZeroValue->isZero

478

What does it take to address this FIXME so this will work on other targets?

llvm/test/Transforms/AggressiveInstCombine/AArch64/lower-table-based-ctz.ll
1

Why are there less tests than the other patch?

The changes in AArch64InstrInfo.td need to be their own patch in a separate commit (with their own tests). It's hard to see how they are correct as-is, but if you put up a new patch for them we can sort out what we need to do there.

Patches should also be uploaded with context, as per the llvm docs: https://llvm.org/docs/Phabricator.html#requesting-a-review-via-the-web-interface

Thanks for this.

This should be implemented as a patch on top of the https://reviews.llvm.org/D113291 (as a child of the D113291).

djtodoro requested changes to this revision.Feb 6 2022, 11:23 PM
This revision now requires changes to proceed.Feb 6 2022, 11:23 PM
Wilco1 added a subscriber: Wilco1.Feb 7 2022, 6:48 AM
Wilco1 added inline comments.
llvm/lib/Transforms/AggressiveInstCombine/AggressiveInstCombine.cpp
501

This doesn't check that ZeroTableElem is either zero or InputBits (eg. if table[0] = 1 in ctz2 below, it has to be rejected). Also does it avoid emitting the AND if it is InputBits?

int ctz2 (unsigned x)
{

const int u = 0;
static short table[64] =
  {
    32, 0, 1,12, 2, 6, u,13, 3, u, 7, u, u, u, u,14,
    10, 4, u, u, 8, u, u,25, u, u, u, u, u,21,27,15,
    31,11, 5, u, u, u, u, u, 9, u, u,24, u, u,20,26,
    30, u, u, u, u,23, u,19,29, u,22,18,28,17,16, u
  };

x = (x & -x) * 0x0450FBAF;
return table[x >> 26];

}

How hard is to implement support for x86?

It will be similar as lowering fix - https://reviews.llvm.org/D120462 once intrinsic recognition of cttz is merged.

Please split the lowering fix from the intrinsic recognition.

Sure. I have created a different revision here - https://reviews.llvm.org/D120462
but it still needs some work.

gsocshubham added a comment.EditedFeb 28 2022, 12:15 AM

Thanks for this.

This should be implemented as a patch on top of the https://reviews.llvm.org/D113291 (as a child of the D113291).

Hello @djtodoro

What is the process to submit my patch based on https://reviews.llvm.org/D113291 ?
While submitting current patch, I could not find a option to make my patch as child patch of yours.

Can you please point to the process of doing it?

llvm/test/Transforms/AggressiveInstCombine/AArch64/lower-table-based-ctz.ll
1

This should go once review on this patch - https://reviews.llvm.org/D120462 is ready.

Thanks for this.

This should be implemented as a patch on top of the https://reviews.llvm.org/D113291 (as a child of the D113291).

Hello @djtodoro

What is the process to submit my patch based on https://reviews.llvm.org/D113291 ?
While submitting current patch, I could not find a option to make my patch as child patch of yours.

Can you please point to the process of doing it?

Edit Related Revisions (Bellow "Download Raw DIff") -> Edit Child Revisions

lebedev.ri resigned from this revision.Jan 12 2023, 4:58 PM

This review may be stuck/dead, consider abandoning if no longer relevant.
Removing myself as reviewer in attempt to clean dashboard.

Herald added a project: Restricted Project. · View Herald TranscriptJan 12 2023, 4:58 PM
Herald added a subscriber: StephenFan. · View Herald Transcript