This is an archive of the discontinued LLVM Phabricator instance.

[X86] Don't peek through bitcasts before checking ISD::isBuildVectorOfConstantSDNodes in combineTruncatedArithmetic
ClosedPublic

Authored by craig.topper on Feb 26 2019, 6:04 PM.

Details

Summary

We don't have any combines that can look through a bitcast to truncate a build vector of constants. So the truncate will stick around and give us something like this pattern (binop (trunc X), (trunc (bitcast (build_vector)))) which has two truncates in it. Which will be reversed by hoistLogicOpWithSameOpcodeHands in the generic DAG combiner. Thus causing an infinite loop.

Even if we had a combine for (truncate (bitcast (build_vector))), I think it would need to be implemented in getNode otherwise DAG combiner visit ordering would probably still visit the binop first and reverse it. Or combineTruncatedArithmetic would need to do its own constant folding.

This came to me as infinite loop a colleague found with ISPC. In his case the bitcast got introduced after type legalization do it being a v4i64 build_vector on a 32-bit target. But that also means you need to have delayed combineTruncatedArithmetic seeing the constant until after type legalization. So my early attempts at a simple test case have failed. I'll keep at it.

Diff Detail

Repository
rL LLVM

Event Timeline

craig.topper created this revision.Feb 26 2019, 6:04 PM

Compiling this with "-mattr=avx2 -mtriple=i686-unknown-unknown" should be enough to hit the bug. I'll get that turned into a real test case later tonight.

define <8 x i32> @foo(<8 x i64> %x, <4 x i64> %y) {
  %a = shufflevector <4 x i64> %y, <4 x i64> <i64 12345, i64 67890, i64 13579, i64 24680>, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %b = and <8 x i64> %x, %a
  %c = trunc <8 x i64> %b to <8 x i32>
  ret <8 x i32> %c
}

Add test case

spatel accepted this revision.Feb 28 2019, 9:12 AM

LGTM

lib/Target/X86/X86ISelLowering.cpp
38719 ↗(On Diff #188603)

Please add to this comment to make it explicit that we're not peeking through bitcast to avoid fighting with another transform.

This revision is now accepted and ready to land.Feb 28 2019, 9:12 AM
This revision was automatically updated to reflect the committed changes.
Herald added a project: Restricted Project. · View Herald TranscriptFeb 28 2019, 10:49 AM