This is an archive of the discontinued LLVM Phabricator instance.

[ISel] Round down mask bit when merge undef(s) for DAG combine
ClosedPublic

Authored by xiangzhangllvm on Jun 24 2022, 7:32 PM.

Details

Summary

We had some problems in merging splat value.
For example
We usually see 0xuuuu0001 as an undef value, then replace it with totally undef 0xuuuuuuuu.

But this is not make sense when we only use part value of it.

Take llvm.fshl.* for example:

t1: v4i32 = Constant:i32<12>, undef:i32, Constant:i32<12>, undef:i32
t2: v2i64 = bitcast t1
t5: v2i64 = fshl t3, t4, t2

We can not convert t2 to {i64 undef, i64 undef}

But this will be replaced or see "OK to replace" in SelectionDAG::isSplatValue.

Diff Detail

Event Timeline

xiangzhangllvm created this revision.Jun 24 2022, 7:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 24 2022, 7:32 PM
Herald added a subscriber: hiraditya. · View Herald Transcript
xiangzhangllvm requested review of this revision.Jun 24 2022, 7:32 PM
Herald added a project: Restricted Project. · View Herald TranscriptJun 24 2022, 7:32 PM

Let me try create a small lit test for it. Thanks

llvm/test/CodeGen/X86/splat-value.ll
2 ↗(On Diff #440478)

If use -debug to check it, you will more easy to find the logic error:

In the DAG it will create "t45: v8i64 = X86ISD::VSHL t4, undef:v2i64" node which is totally no meaning.

pengfei added inline comments.Jun 28 2022, 12:08 AM
llvm/lib/Support/APInt.cpp
2995–2996

Is the comment correct after your change?

llvm/test/CodeGen/X86/splat-value.ll
20–23 ↗(On Diff #440478)

Where's undef in the test?

craig.topper added inline comments.Jun 28 2022, 12:20 AM
llvm/include/llvm/ADT/APInt.h
2245–2246

This comment uses the word "down" as well, but to mean the number of bits is decreasing. I believe this patch is implementing this TODO.

LuoYuanke added inline comments.Jun 28 2022, 12:24 AM
llvm/test/CodeGen/X86/splat-value.ll
31 ↗(On Diff #440478)

Do we need thoes attributes?

llvm/include/llvm/ADT/APInt.h
2245–2246

So, let me remove this TODO, thanks!

llvm/lib/Support/APInt.cpp
2995–2996

Good catch! Let me update the old comment.

llvm/test/CodeGen/X86/splat-value.ll
20–23 ↗(On Diff #440478)

It will generated in the mid of DAG combining.

31 ↗(On Diff #440478)

My copy it from a runfail test, let me recheck it a little later. (Sever can not log on now)

Please add test coverage in APIntTest.cpp

llvm/include/llvm/ADT/APInt.h
2246–2251

Yes, 'down' in the TODO meant when reducing the number of bits - maybe 'MatchAllBits' would be better? Current behaviour is 'MatchAnyBits'.

RKSimon added inline comments.Jun 28 2022, 4:34 AM
llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
2723

I regret using the term 'UndefElts' - what I meant was that these elements shouldn't be trusted.

llvm/test/CodeGen/X86/splat-value.ll
4 ↗(On Diff #440478)

Maybe a better test name - and a comment describing what you're testing for?

31 ↗(On Diff #440478)

Drop the attributes and add -mcpu=cannonlake to the RUN command?

OK, let me add test coverage in APIntTest.cpp, many thanks!

llvm/include/llvm/ADT/APInt.h
2246–2251

Yes, MatchAllBits is better, thanks!

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
2723

: ) This is useful for undef optmization.
So In this case (the lit test I write) the UndefElts is mainly used for undef propagation. Now we give out a choose to select MatchAllBits or MatchAnyBits for it.
For BitCast, I think we should Conservatively use MatchAllBits.

llvm/test/CodeGen/X86/splat-value.ll
4 ↗(On Diff #440478)

OK, this test is about fshl, let me rename it to "define void @test_fshl"

xiangzhangllvm marked 7 inline comments as done.Jun 29 2022, 5:55 PM

Hi Friends, PLS can you accept this fix ? This is related to one of our Must Fix bug. Many thanks!

pengfei accepted this revision.Jun 29 2022, 6:00 PM

I'm fine with this, please wait one more day for others' opinions.

This revision is now accepted and ready to land.Jun 29 2022, 6:00 PM
RKSimon accepted this revision.Jun 30 2022, 2:20 AM

Please pre-commit fshl-splat-undef.ll with current trunk codegen so that the patch commit shows the codegen change.

LGTM with a few minors

llvm/lib/CodeGen/SelectionDAG/SelectionDAG.cpp
2716

OR -> MatchAnyBits

2723

/*MatchAllBits=*/true

llvm/test/CodeGen/X86/fshl-splat-undef.ll
44

remove the function attrs and the #0 ?

46

remove this?

Please pre-commit fshl-splat-undef.ll with current trunk codegen so that the patch commit shows the codegen change.

LGTM with a few minors

Please pre-commit fshl-splat-undef.ll with current trunk codegen so that the patch commit shows the codegen change.

LGTM with a few minors

Sure! Pursuing Perfection : ) Thank you!

Address Simon's comments.

xiangzhangllvm marked 4 inline comments as done.Jun 30 2022, 4:32 AM
LuoYuanke accepted this revision.Jun 30 2022, 5:47 PM