Skip to content

Commit 74f0925

Browse files
committed
address latest pr comments + extra tests
1 parent 5e40350 commit 74f0925

File tree

3 files changed

+40
-8
lines changed

3 files changed

+40
-8
lines changed

clang/test/CodeGenHLSL/builtins/countbits.hlsl

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,13 @@ uint test_countbits_ushort(uint16_t p0)
1010
{
1111
return countbits(p0);
1212
}
13+
// CHECK-LABEL: test_countbits_short
14+
// CHECK: [[A:%.*]] = call i16 @llvm.ctpop.i16
15+
// CHECK-NEXT: sext i16 [[A]] to i32
16+
uint test_countbits_short(int16_t p0)
17+
{
18+
return countbits(p0);
19+
}
1320
// CHECK-LABEL: test_countbits_ushort2
1421
// CHECK: [[A:%.*]] = call <2 x i16> @llvm.ctpop.v2i16
1522
// CHECK-NEXT: zext <2 x i16> [[A]] to <2 x i32>
@@ -39,6 +46,12 @@ uint test_countbits_uint(uint p0)
3946
{
4047
return countbits(p0);
4148
}
49+
// CHECK-LABEL: test_countbits_int
50+
// CHECK: call i32 @llvm.ctpop.i32
51+
uint test_countbits_int(int p0)
52+
{
53+
return countbits(p0);
54+
}
4255
// CHECK-LABEL: test_countbits_uint2
4356
// CHECK: call <2 x i32> @llvm.ctpop.v2i32
4457
uint2 test_countbits_uint2(uint2 p0)
@@ -65,6 +78,13 @@ uint test_countbits_long(uint64_t p0)
6578
{
6679
return countbits(p0);
6780
}
81+
// CHECK-LABEL: test_countbits_slong
82+
// CHECK: [[A:%.*]] = call i64 @llvm.ctpop.i64
83+
// CHECK-NEXT: trunc i64 [[A]] to i32
84+
uint test_countbits_slong(int64_t p0)
85+
{
86+
return countbits(p0);
87+
}
6888
// CHECK-LABEL: test_countbits_long2
6989
// CHECK: [[A:%.*]] = call <2 x i64> @llvm.ctpop.v2i64
7090
// CHECK-NEXT: trunc <2 x i64> [[A]] to <2 x i32>

llvm/lib/Target/DirectX/DXILOpLowering.cpp

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -532,33 +532,36 @@ class OpLowerer {
532532
}
533533

534534
unsigned CastOp;
535-
if (FRT->isIntOrIntVectorTy(16))
535+
unsigned CastOp2;
536+
if (FRT->isIntOrIntVectorTy(16)) {
536537
CastOp = Instruction::ZExt;
537-
else { // must be 64 bits
538+
CastOp2 = Instruction::SExt;
539+
} else { // must be 64 bits
538540
assert(FRT->isIntOrIntVectorTy(64) &&
539541
"Currently only lowering 16, 32, or 64 bit ctpop to CountBits \
540542
is supported.");
541543
CastOp = Instruction::Trunc;
544+
CastOp2 = Instruction::Trunc;
542545
}
543546

544547
// It is correct to replace the ctpop with the dxil op and
545548
// remove all casts to i32
546-
bool nonCastInstr = false;
549+
bool NeedsCast = false;
547550
for (User *User : make_early_inc_range(CI->users())) {
548-
Instruction *I;
549-
if ((I = dyn_cast<Instruction>(User)) != NULL &&
550-
I->getOpcode() == CastOp && I->getType() == RetTy) {
551+
Instruction *I = dyn_cast<Instruction>(User);
552+
if (I && (I->getOpcode() == CastOp || I->getOpcode() == CastOp2) &&
553+
I->getType() == RetTy) {
551554
I->replaceAllUsesWith(*OpCall);
552555
I->eraseFromParent();
553556
} else
554-
nonCastInstr = true;
557+
NeedsCast = true;
555558
}
556559

557560
// It is correct to replace a ctpop with the dxil op and
558561
// a cast from i32 to the return type of the ctpop
559562
// the cast is emitted here if there is a non-cast to i32
560563
// instr which uses the ctpop
561-
if (nonCastInstr) {
564+
if (NeedsCast) {
562565
Value *Cast =
563566
IRB.CreateZExtOrTrunc(*OpCall, F.getReturnType(), "ctpop.cast");
564567
CI->replaceAllUsesWith(Cast);

llvm/test/CodeGen/DirectX/countbits.ll

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ entry:
2020
ret i32 %elt.zext
2121
}
2222

23+
define noundef i32 @test_countbits_short3(i16 noundef %a) {
24+
entry:
25+
; CHECK: [[A:%.*]] = call i32 @dx.op.unaryBits.i16(i32 31, i16 %{{.*}})
26+
; CHECK-NEXT: ret i32 [[A]]
27+
%elt.ctpop = call i16 @llvm.ctpop.i16(i16 %a)
28+
%elt.sext = sext i16 %elt.ctpop to i32
29+
ret i32 %elt.sext
30+
}
31+
2332
define noundef i32 @test_countbits_int(i32 noundef %a) {
2433
entry:
2534
; CHECK: [[A:%.*]] = call i32 @dx.op.unaryBits.i32(i32 31, i32 %{{.*}})

0 commit comments

Comments
 (0)