Skip to content

Commit 5e3ae4c

Browse files
authored
[NVPTX] improve Boolean ISel (#80166)
Add TableGen patterns to convert more instructions to boolean expressions: - **mul -> and/or**: i1 multiply instructions currently cannot be selected causing the compiler to crash. See #57404 - **select -> and/or**: Converting selects to and/or can enable more optimizations. `InstCombine` cannot do this as aggressively due to poison semantics.
1 parent 0f728a0 commit 5e3ae4c

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

llvm/lib/Target/NVPTX/NVPTXInstrInfo.td

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1539,6 +1539,16 @@ defm OR : BITWISE<"or", or>;
15391539
defm AND : BITWISE<"and", and>;
15401540
defm XOR : BITWISE<"xor", xor>;
15411541

1542+
// PTX does not support mul on predicates, convert to and instructions
1543+
def : Pat<(mul Int1Regs:$a, Int1Regs:$b), (ANDb1rr Int1Regs:$a, Int1Regs:$b)>;
1544+
def : Pat<(mul Int1Regs:$a, (i1 imm:$b)), (ANDb1ri Int1Regs:$a, imm:$b)>;
1545+
1546+
// These transformations were once reliably performed by instcombine, but thanks
1547+
// to poison semantics they are no longer safe for LLVM IR, perform them here
1548+
// instead.
1549+
def : Pat<(select Int1Regs:$a, Int1Regs:$b, 0), (ANDb1rr Int1Regs:$a, Int1Regs:$b)>;
1550+
def : Pat<(select Int1Regs:$a, 1, Int1Regs:$b), (ORb1rr Int1Regs:$a, Int1Regs:$b)>;
1551+
15421552
// Lower logical v2i16/v4i8 ops as bitwise ops on b32.
15431553
foreach vt = [v2i16, v4i8] in {
15441554
def: Pat<(or (vt Int32Regs:$a), (vt Int32Regs:$b)),
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
; RUN: llc < %s -march=nvptx64 -mcpu=sm_20 | FileCheck %s
2+
; RUN: %if ptxas %{ llc < %s -march=nvptx64 -mcpu=sm_20 | %ptxas-verify %}
3+
4+
; CHECK-LABEL: m2and_rr
5+
define i1 @m2and_rr(i1 %a, i1 %b) {
6+
; CHECK: and.pred %p{{[0-9]+}}, %p{{[0-9]+}}, %p{{[0-9]+}}
7+
; CHECK-NOT: mul
8+
%r = mul i1 %a, %b
9+
ret i1 %r
10+
}
11+
12+
; CHECK-LABEL: m2and_ri
13+
define i1 @m2and_ri(i1 %a) {
14+
; CHECK-NOT: mul
15+
%r = mul i1 %a, 1
16+
ret i1 %r
17+
}
18+
19+
; CHECK-LABEL: select2or
20+
define i1 @select2or(i1 %a, i1 %b) {
21+
; CHECK: or.b16 %rs{{[0-9]+}}, %rs{{[0-9]+}}, %rs{{[0-9]+}}
22+
; CHECK-NOT: selp
23+
%r = select i1 %a, i1 1, i1 %b
24+
ret i1 %r
25+
}
26+
27+
; CHECK-LABEL: select2and
28+
define i1 @select2and(i1 %a, i1 %b) {
29+
; CHECK: and.b16 %rs{{[0-9]+}}, %rs{{[0-9]+}}, %rs{{[0-9]+}}
30+
; CHECK-NOT: selp
31+
%r = select i1 %a, i1 %b, i1 0
32+
ret i1 %r
33+
}

0 commit comments

Comments
 (0)