llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

X86 CodeGen: Miscompile vector kernel

Open llvmbot opened this issue 13 years ago • 3 comments

Bugzilla Link 15467
Resolution INVALID
Resolved on Mar 07, 2013 15:53
Version trunk
OS All
Attachments Failing test
Reporter LLVM Bugzilla Contributor
CC @delena,@darkbuck

Extended Description

../Release+Asserts/bin/clang++ -O3 -fvectorize test.cpp -o with; ../Release+Asserts/bin/clang++ -O3 -fno-vectorize test.cpp -o without ; ./with 1 > with.out ; ./without 1 > without.out ; diff with.out without.out

../Release+Asserts/bin/clang++ --version clang version 3.3 (trunk 176630) (llvm/trunk 176632) Target: x86_64-apple-darwin13.0.0 Thread model: posix

cat reduced.ll ; ModuleID = 'test.ll' target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64-S128" target triple = "x86_64-apple-macosx10.8.0"

@​A2 = external global [1024 x i16], align 16 @​C2 = external global [1024 x i16], align 16 @​D2 = external global [1024 x i16], align 16

define void @​_Z7kernel2v() nounwind uwtable noinline ssp { vector.ph: br label %vector.body

vector.body: ; preds = %vector.body, %vector.ph %index = phi i64 [ 0, %vector.ph ], [ %index.next, %vector.body ] %0 = getelementptr inbounds [1024 x i16]* @​C2, i64 0, i64 %index %1 = bitcast i16* %0 to <4 x i16>* %wide.load = load <4 x i16>* %1, align 8 %2 = zext <4 x i16> %wide.load to <4 x i32> %3 = getelementptr inbounds [1024 x i16]* @​D2, i64 0, i64 %index %4 = bitcast i16* %3 to <4 x i16>* %wide.load12 = load <4 x i16>* %4, align 8 %5 = zext <4 x i16> %wide.load12 to <4 x i32> %6 = shl <4 x i32> %2, %5 %7 = trunc <4 x i32> %6 to <4 x i16> %8 = getelementptr inbounds [1024 x i16]* @​A2, i64 0, i64 %index %9 = bitcast i16* %8 to <4 x i16>* store <4 x i16> %7, <4 x i16>* %9, align 8 %index.next = add i64 %index, 4 %10 = icmp eq i64 %index.next, 1024 br i1 %10, label %for.end, label %vector.body

for.end: ; preds = %vector.body ret void }

Produces the failing code in kernel2: ../Release+Asserts/bin/llc -mcpu=core2 reduced.ll

It looks like a problem during ISel.

llvmbot avatar Mar 07 '13 21:03 llvmbot

Duh, I should be more careful with auto generated code :).

Thanks for taking a look!

Closing as not a bug.

I don't see it's bug but an undefined behavior. X86 has 2 different shift behaviors, taking 32-bit integer as examples:

  • scalar shift (SAR/SHL/SRL), the shift amount will be masked 0x1F, as a result (x << 32) == (x << 0), (y << 33) == (y << 33)
  • vector shift (PSLL,PSRA,PSRL for immediate shift amount and PSLLV,PSRAV,PSRLV for varialble shift amount), the shift amount is maximized at 32, as a result (x << 32) == 0 and (y << 33) == 0

As C doesn't define the behavior of shift when the shift amount is beyond the width of the operand type, both behaviors are valid.

Back to the test reported in this bug. On AVX, there's no variable shift. The variable shift is emulated by multiplication. The shift amount is converted to (1 << ShAmt) though FP2INT trick. It follows the behavior of vector version of shift, i.e. if ShAmt > 31, the result will be 0.

With the following change, you could ask it to following scalar version behavior. But, the fundamental question is which behavior is expected in a vectorized code.

diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index b19f2f6..7b42424 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -11608,6 +11608,7 @@ SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const { if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) { Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));

  • Op = DAG.getNode(ISD::AND, dl, VT, Op, DAG.getConstant(0x0f800000U, VT)); Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT)); Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op); Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);

llvmbot avatar Mar 07 '13 23:03 llvmbot

typo fix to prevent misunderstanding

I don't see it's bug but an undefined behavior. X86 has 2 different shift behaviors, taking 32-bit integer as examples:

  • scalar shift (SAR/SHL/SRL), the shift amount will be masked with 0x1F, as a result (x << 32) == (x << 0), (y << 33) == (y << 1)
  • vector shift (PSLL,PSRA,PSRL for immediate shift amount and PSLLV,PSRAV,PSRLV for varialble shift amount), the shift amount is maximized at 32, as a result (x << 32) == 0 and (y << 33) == 0

As C doesn't define the behavior of shift when the shift amount is beyond the width of the operand type, both behaviors are valid.

Back to the test reported in this bug. On AVX, there's no variable shift. The variable shift is emulated by multiplication. The shift amount is converted to (1 << ShAmt) though FP2INT trick. It follows the behavior of vector version of shift, i.e. if ShAmt > 31, the result will be 0.

With the following change, you could ask it to following scalar version behavior. But, the fundamental question is which behavior is expected in a vectorized code.

diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index b19f2f6..7b42424 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -11608,6 +11608,7 @@ SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const { if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) { Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));

  • Op = DAG.getNode(ISD::AND, dl, VT, Op, DAG.getConstant(0x0f800000U, VT)); Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT)); Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op); Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);

darkbuck avatar Mar 07 '13 23:03 darkbuck

I don't see it's bug but an undefined behavior. X86 has 2 different shift behaviors, taking 32-bit integer as examples:

  • scalar shift (SAR/SHL/SRL), the shift amount will be masked 0x1F, as a result (x << 32) == (x << 0), (y << 33) == (y << 33)
  • vector shift (PSLL,PSRA,PSRL for immediate shift amount and PSLLV,PSRAV,PSRLV for varialble shift amount), the shift amount is maximized at 32, as a result (x << 32) == 0 and (y << 33) == 0

As C doesn't define the behavior of shift when the shift amount is beyond the width of the operand type, both behaviors are valid.

Back to the test reported in this bug. On AVX, there's no variable shift. The variable shift is emulated by multiplication. The shift amount is converted to (1 << ShAmt) though FP2INT trick. It follows the behavior of vector version of shift, i.e. if ShAmt > 31, the result will be 0.

With the following change, you could ask it to following scalar version behavior. But, the fundamental question is which behavior is expected in a vectorized code.

diff --git a/lib/Target/X86/X86ISelLowering.cpp b/lib/Target/X86/X86ISelLowering.cpp index b19f2f6..7b42424 100644 --- a/lib/Target/X86/X86ISelLowering.cpp +++ b/lib/Target/X86/X86ISelLowering.cpp @@ -11608,6 +11608,7 @@ SDValue X86TargetLowering::LowerShift(SDValue Op, SelectionDAG &DAG) const { if (VT == MVT::v4i32 && Op->getOpcode() == ISD::SHL) { Op = DAG.getNode(ISD::SHL, dl, VT, Amt, DAG.getConstant(23, VT));

  • Op = DAG.getNode(ISD::AND, dl, VT, Op, DAG.getConstant(0x0f800000U, VT)); Op = DAG.getNode(ISD::ADD, dl, VT, Op, DAG.getConstant(0x3f800000U, VT)); Op = DAG.getNode(ISD::BITCAST, dl, MVT::v4f32, Op); Op = DAG.getNode(ISD::FP_TO_SINT, dl, VT, Op);

darkbuck avatar Mar 07 '13 23:03 darkbuck