Skip to content

Commit c13723b

Browse files
committed
Lower SIToFPInst with bool operand (#869)
`SIToFPInst` LLVM instruction is translated to `OpConvertSToF` SPIR-V instruction. `OpConvertSToF` instruction can not receive boolean operand, so replace it with the select instruction.
1 parent 40e0a92 commit c13723b

File tree

2 files changed

+58
-26
lines changed

2 files changed

+58
-26
lines changed

llvm-spirv/lib/SPIRV/SPIRVLowerBool.cpp

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -77,39 +77,28 @@ class SPIRVLowerBool : public ModulePass, public InstVisitor<SPIRVLowerBool> {
7777
replace(&I, Cmp);
7878
}
7979
}
80-
virtual void visitZExtInst(ZExtInst &I) {
80+
void handleCastInstructions(Instruction &I) {
8181
auto Op = I.getOperand(0);
8282
if (isBoolType(Op->getType())) {
83-
auto Ty = I.getType();
83+
auto Opcode = I.getOpcode();
84+
auto Ty = (Opcode == Instruction::ZExt || Opcode == Instruction::SExt)
85+
? I.getType()
86+
: Type::getInt32Ty(*Context);
8487
auto Zero = getScalarOrVectorConstantInt(Ty, 0, false);
85-
auto One = getScalarOrVectorConstantInt(Ty, 1, false);
88+
auto One = getScalarOrVectorConstantInt(
89+
Ty, (Opcode == Instruction::SExt) ? ~0 : 1, false);
8690
assert(Zero && One && "Couldn't create constant int");
8791
auto Sel = SelectInst::Create(Op, One, Zero, "", &I);
88-
replace(&I, Sel);
89-
}
90-
}
91-
virtual void visitSExtInst(SExtInst &I) {
92-
auto Op = I.getOperand(0);
93-
if (isBoolType(Op->getType())) {
94-
auto Ty = I.getType();
95-
auto Zero = getScalarOrVectorConstantInt(Ty, 0, false);
96-
auto One = getScalarOrVectorConstantInt(Ty, ~0, false);
97-
assert(Zero && One && "Couldn't create constant int");
98-
auto Sel = SelectInst::Create(Op, One, Zero, "", &I);
99-
replace(&I, Sel);
100-
}
101-
}
102-
virtual void visitUIToFPInst(UIToFPInst &I) {
103-
auto Op = I.getOperand(0);
104-
if (isBoolType(Op->getType())) {
105-
auto Ty = Type::getInt32Ty(*Context);
106-
auto Zero = getScalarOrVectorConstantInt(Ty, 0, false);
107-
auto One = getScalarOrVectorConstantInt(Ty, 1, false);
108-
assert(Zero && One && "Couldn't create constant int");
109-
auto Sel = SelectInst::Create(Op, One, Zero, "", &I);
110-
I.setOperand(0, Sel);
92+
if (Opcode == Instruction::ZExt || Opcode == Instruction::SExt)
93+
replace(&I, Sel);
94+
else if (Opcode == Instruction::UIToFP || Opcode == Instruction::SIToFP)
95+
I.setOperand(0, Sel);
11196
}
11297
}
98+
virtual void visitZExtInst(ZExtInst &I) { handleCastInstructions(I); }
99+
virtual void visitSExtInst(SExtInst &I) { handleCastInstructions(I); }
100+
virtual void visitUIToFPInst(UIToFPInst &I) { handleCastInstructions(I); }
101+
virtual void visitSIToFPInst(SIToFPInst &I) { handleCastInstructions(I); }
113102
bool runOnModule(Module &M) override {
114103
Context = &M.getContext();
115104
visit(M);

llvm-spirv/test/sitofp-with-bool.ll

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s
3+
; RUN: llvm-spirv %t.bc -o %t.spv
4+
; RUN: spirv-val %t.spv
5+
6+
; CHECK: TypeInt [[int_32:[0-9]+]] 32 0
7+
; CHECK: Constant {{[0-9]+}} [[zero:[0-9]+]] 0
8+
; CHECK: Constant {{[0-9]+}} [[one:[0-9]+]] 1
9+
; CHECK: TypeBool [[bool:[0-9]+]]
10+
11+
; CHECK: Function
12+
; CHECK: FunctionParameter {{[0-9]+}} [[A:[0-9]+]]
13+
; CHECK: FunctionParameter {{[0-9]+}} [[B:[0-9]+]]
14+
; CHECK: SGreaterThan [[bool]] [[cmp_res:[0-9]+]] [[B]] [[zero]]
15+
; CHECK: Select [[int_32]] [[select_res:[0-9]+]] [[cmp_res]] [[one]] [[zero]]
16+
; CHECK: ConvertSToF {{[0-9]+}} [[stof_res:[0-9]+]] [[select_res]]
17+
; CHECK: Store [[A]] [[stof_res]]
18+
19+
20+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
21+
target triple = "spir64"
22+
23+
; Function Attrs: nofree norecurse nounwind writeonly
24+
define dso_local spir_kernel void @K(float addrspace(1)* nocapture %A, i32 %B) local_unnamed_addr #0 !kernel_arg_addr_space !2 !kernel_arg_access_qual !3 !kernel_arg_type !4 !kernel_arg_base_type !4 !kernel_arg_type_qual !5 {
25+
entry:
26+
%cmp = icmp sgt i32 %B, 0
27+
%conv = sitofp i1 %cmp to float
28+
store float %conv, float addrspace(1)* %A, align 4
29+
ret void
30+
}
31+
32+
attributes #0 = { nofree norecurse nounwind writeonly "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="all" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="true" "stack-protector-buffer-size"="8" "uniform-work-group-size"="false" "unsafe-fp-math"="false" "use-soft-float"="false" }
33+
34+
!llvm.module.flags = !{!0}
35+
!opencl.ocl.version = !{!1}
36+
!opencl.spir.version = !{!1}
37+
38+
!0 = !{i32 1, !"wchar_size", i32 4}
39+
!1 = !{i32 2, i32 0}
40+
!2 = !{i32 1, i32 0}
41+
!3 = !{!"none", !"none"}
42+
!4 = !{!"float*", !"int"}
43+
!5 = !{!"", !""}

0 commit comments

Comments
 (0)