Skip to content

Commit 4f47372

Browse files
authored
[SPIR-V] Add Float16 support when targeting Vulkan (#77115)
Add Float16 to Vulkan's available capabilities, and guard Float16Buffer (Kernel-only capability) against being added outside OpenCL environments. Add tests to verify half and half vector types, and validate with spirv-val. Fixes #66398
1 parent 2aae304 commit 4f47372

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -597,8 +597,9 @@ void RequirementHandler::initAvailableCapabilitiesForVulkan(
597597
const SPIRVSubtarget &ST) {
598598
addAvailableCaps({Capability::Shader, Capability::Linkage});
599599

600-
// Provided by Vulkan version 1.0.
601-
addAvailableCaps({Capability::Int16, Capability::Int64, Capability::Float64});
600+
// Provided by all supported Vulkan versions.
601+
addAvailableCaps({Capability::Int16, Capability::Int64, Capability::Float16,
602+
Capability::Float64});
602603
}
603604

604605
} // namespace SPIRV
@@ -733,7 +734,10 @@ void addInstrRequirements(const MachineInstr &MI,
733734
auto SC = MI.getOperand(1).getImm();
734735
Reqs.getAndAddRequirements(SPIRV::OperandCategory::StorageClassOperand, SC,
735736
ST);
736-
// If it's a type of pointer to float16, add Float16Buffer capability.
737+
// If it's a type of pointer to float16 targeting OpenCL, add Float16Buffer
738+
// capability.
739+
if (!ST.isOpenCLEnv())
740+
break;
737741
assert(MI.getOperand(2).isReg());
738742
const MachineRegisterInfo &MRI = MI.getMF()->getRegInfo();
739743
SPIRVType *TypeDef = MRI.getVRegDef(MI.getOperand(2).getReg());

llvm/test/CodeGen/SPIRV/basic_float_types.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,22 @@
11
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s
22
; RUN: llc -O0 -mtriple=spirv32-unknown-unknown %s -o - | FileCheck %s
33
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
4+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-unknown %s -o - -filetype=obj | spirv-val %}
45

56
define void @main() {
67
entry:
8+
9+
; CHECK-DAG: OpCapability Float16
10+
; CHECK-DAG: OpCapability Float64
11+
12+
; CHECK-DAG: %[[#half:]] = OpTypeFloat 16
713
; CHECK-DAG: %[[#float:]] = OpTypeFloat 32
814
; CHECK-DAG: %[[#double:]] = OpTypeFloat 64
915

16+
; CHECK-DAG: %[[#v2half:]] = OpTypeVector %[[#half]] 2
17+
; CHECK-DAG: %[[#v3half:]] = OpTypeVector %[[#half]] 3
18+
; CHECK-DAG: %[[#v4half:]] = OpTypeVector %[[#half]] 4
19+
1020
; CHECK-DAG: %[[#v2float:]] = OpTypeVector %[[#float]] 2
1121
; CHECK-DAG: %[[#v3float:]] = OpTypeVector %[[#float]] 3
1222
; CHECK-DAG: %[[#v4float:]] = OpTypeVector %[[#float]] 4
@@ -15,21 +25,37 @@ entry:
1525
; CHECK-DAG: %[[#v3double:]] = OpTypeVector %[[#double]] 3
1626
; CHECK-DAG: %[[#v4double:]] = OpTypeVector %[[#double]] 4
1727

28+
; CHECK-DAG: %[[#ptr_Function_half:]] = OpTypePointer Function %[[#half]]
1829
; CHECK-DAG: %[[#ptr_Function_float:]] = OpTypePointer Function %[[#float]]
1930
; CHECK-DAG: %[[#ptr_Function_double:]] = OpTypePointer Function %[[#double]]
31+
; CHECK-DAG: %[[#ptr_Function_v2half:]] = OpTypePointer Function %[[#v2half]]
32+
; CHECK-DAG: %[[#ptr_Function_v3half:]] = OpTypePointer Function %[[#v3half]]
33+
; CHECK-DAG: %[[#ptr_Function_v4half:]] = OpTypePointer Function %[[#v4half]]
2034
; CHECK-DAG: %[[#ptr_Function_v2float:]] = OpTypePointer Function %[[#v2float]]
2135
; CHECK-DAG: %[[#ptr_Function_v3float:]] = OpTypePointer Function %[[#v3float]]
2236
; CHECK-DAG: %[[#ptr_Function_v4float:]] = OpTypePointer Function %[[#v4float]]
2337
; CHECK-DAG: %[[#ptr_Function_v2double:]] = OpTypePointer Function %[[#v2double]]
2438
; CHECK-DAG: %[[#ptr_Function_v3double:]] = OpTypePointer Function %[[#v3double]]
2539
; CHECK-DAG: %[[#ptr_Function_v4double:]] = OpTypePointer Function %[[#v4double]]
2640

41+
; CHECK: %[[#]] = OpVariable %[[#ptr_Function_half]] Function
42+
%half_Val = alloca half, align 2
43+
2744
; CHECK: %[[#]] = OpVariable %[[#ptr_Function_float]] Function
2845
%float_Val = alloca float, align 4
2946

3047
; CHECK: %[[#]] = OpVariable %[[#ptr_Function_double]] Function
3148
%double_Val = alloca double, align 8
3249

50+
; CHECK: %[[#]] = OpVariable %[[#ptr_Function_v2half]] Function
51+
%half2_Val = alloca <2 x half>, align 4
52+
53+
; CHECK: %[[#]] = OpVariable %[[#ptr_Function_v3half]] Function
54+
%half3_Val = alloca <3 x half>, align 8
55+
56+
; CHECK: %[[#]] = OpVariable %[[#ptr_Function_v4half]] Function
57+
%half4_Val = alloca <4 x half>, align 8
58+
3359
; CHECK: %[[#]] = OpVariable %[[#ptr_Function_v2float]] Function
3460
%float2_Val = alloca <2 x float>, align 8
3561

0 commit comments

Comments
 (0)