Skip to content

Commit 238db10

Browse files
committed
[SYCL] Add extension and implement fp control kernel property
1 parent 24700a4 commit 238db10

File tree

10 files changed

+872
-3
lines changed

10 files changed

+872
-3
lines changed

llvm/lib/SYCLLowerIR/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,11 @@ endif()
1212
# NOTE: could have been added earlier from llvm/projects
1313
if (NOT TARGET LLVMGenXIntrinsics)
1414
if (NOT DEFINED LLVMGenXIntrinsics_SOURCE_DIR)
15-
set(LLVMGenXIntrinsics_GIT_REPO https://github.com/intel/vc-intrinsics.git)
15+
set(LLVMGenXIntrinsics_GIT_REPO https://github.com/againull/vc-intrinsics.git)
1616
# Author: Jinsong Ji <[email protected]>
1717
# Date: Thu Aug 10 14:41:52 2023 +0000
1818
# Guard removed typed pointer enum within version macro
19-
set(LLVMGenXIntrinsics_GIT_TAG 17a53f4304463b8e7e639d57ef17479040a8a2ad)
19+
set(LLVMGenXIntrinsics_GIT_TAG 9dd83b42f8c11205a17f286ccbd837e3efd75eff)
2020

2121
message(STATUS "vc-intrinsics repo is missing. Will try to download it from ${LLVMGenXIntrinsics_GIT_REPO}")
2222
include(FetchContent)

llvm/lib/SYCLLowerIR/CompileTimePropertiesPass.cpp

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,37 @@ const StringMap<Decor> SpirvDecorMap = {
6262
};
6363
#undef SYCL_COMPILE_TIME_PROPERTY
6464

65+
enum FloatControl {
66+
RTE = 0, // Round to nearest or even
67+
RTP = 1 << 4, // Round towards +ve inf
68+
RTN = 2 << 4, // Round towards -ve inf
69+
RTZ = 3 << 4, // Round towards zero
70+
71+
DENORM_FTZ = 0, // Denorm mode flush to zero
72+
DENORM_D_ALLOW = 1 << 6, // Denorm mode double allow
73+
DENORM_F_ALLOW = 1 << 7, // Denorm mode float allow
74+
DENORM_HF_ALLOW = 1 << 10, // Denorm mode half allow
75+
76+
FLOAT_MODE_IEEE = 0, // Single precision float IEEE mode
77+
FLOAT_MODE_ALT = 1 // Single precision float ALT mode
78+
};
79+
80+
enum FloatControlMask {
81+
ROUND_MASK = (RTE | RTP | RTN | RTZ),
82+
FLOAT_MASK = (FLOAT_MODE_IEEE | FLOAT_MODE_ALT),
83+
DENORM_MASK = (DENORM_D_ALLOW | DENORM_F_ALLOW | DENORM_HF_ALLOW)
84+
};
85+
86+
// SPIRV execution modes for FP control.
87+
constexpr uint32_t SPIRV_ROUNDING_MODE_RTE = 4462;
88+
constexpr uint32_t SPIRV_ROUNDING_MODE_RTZ = 4463;
89+
constexpr uint32_t SPIRV_ROUNDING_MODE_RTP_INTEL = 5620;
90+
constexpr uint32_t SPIRV_ROUNDING_MODE_RTN_INTEL = 5621;
91+
constexpr uint32_t SPIRV_DENORM_FLUSH_TO_ZERO = 4460;
92+
constexpr uint32_t SPIRV_DENORM_PRESERVE = 4459;
93+
constexpr uint32_t SPIRV_FLOATING_POINT_MODE_ALT_INTEL = 5622;
94+
constexpr uint32_t SPIRV_FLOATING_POINT_MODE_IEEE_INTEL = 5623;
95+
6596
/// Builds a metadata node for a SPIR-V decoration (decoration code is
6697
/// \c uint32_t integers) with no value.
6798
///
@@ -213,6 +244,66 @@ attributeToExecModeMetadata(const Attribute &Attr, Function &F) {
213244
if (!AttrKindStr.startswith("sycl-"))
214245
return std::nullopt;
215246

247+
auto AddFPControlMetadataForWidth = [&](int32_t SPIRVFPControl,
248+
int32_t Width) {
249+
auto NamedMD = M.getOrInsertNamedMetadata("spirv.ExecutionMode");
250+
std::vector<Metadata *> ValueVec;
251+
ValueVec.push_back(ConstantAsMetadata::get(&F));
252+
ValueVec.push_back(ConstantAsMetadata::get(
253+
ConstantInt::get(Type::getInt32Ty(Ctx), SPIRVFPControl)));
254+
ValueVec.push_back(ConstantAsMetadata::get(
255+
ConstantInt::get(Type::getInt32Ty(Ctx), Width)));
256+
NamedMD->addOperand(MDNode::get(Ctx, ValueVec));
257+
};
258+
259+
auto AddFPControlMetadata = [&](int32_t SPIRVFPControl) {
260+
for (int32_t Width : {64, 32, 16}) {
261+
AddFPControlMetadataForWidth(SPIRVFPControl, Width);
262+
}
263+
};
264+
265+
if (AttrKindStr == "sycl-floating-point-control") {
266+
uint32_t FPControl = getAttributeAsInteger<uint32_t>(Attr);
267+
switch (FPControl & ROUND_MASK) {
268+
case RTE:
269+
AddFPControlMetadata(SPIRV_ROUNDING_MODE_RTE);
270+
break;
271+
case RTP:
272+
AddFPControlMetadata(SPIRV_ROUNDING_MODE_RTP_INTEL);
273+
break;
274+
case RTN:
275+
AddFPControlMetadata(SPIRV_ROUNDING_MODE_RTN_INTEL);
276+
break;
277+
case RTZ:
278+
AddFPControlMetadata(SPIRV_ROUNDING_MODE_RTZ);
279+
break;
280+
default:
281+
llvm_unreachable("Unexpected rounding mode value");
282+
}
283+
switch (FPControl & FLOAT_MASK) {
284+
case FLOAT_MODE_IEEE:
285+
AddFPControlMetadata(SPIRV_FLOATING_POINT_MODE_IEEE_INTEL);
286+
break;
287+
case FLOAT_MODE_ALT:
288+
AddFPControlMetadata(SPIRV_FLOATING_POINT_MODE_ALT_INTEL);
289+
break;
290+
default:
291+
llvm_unreachable("Unexpected single precision fp mode value");
292+
}
293+
if (!(FPControl & DENORM_MASK)) {
294+
AddFPControlMetadata(SPIRV_DENORM_FLUSH_TO_ZERO);
295+
} else {
296+
if (FPControl & DENORM_HF_ALLOW)
297+
AddFPControlMetadataForWidth(SPIRV_DENORM_PRESERVE, 16);
298+
299+
if (FPControl & DENORM_F_ALLOW)
300+
AddFPControlMetadataForWidth(SPIRV_DENORM_PRESERVE, 32);
301+
302+
if (FPControl & DENORM_D_ALLOW)
303+
AddFPControlMetadataForWidth(SPIRV_DENORM_PRESERVE, 64);
304+
}
305+
}
306+
216307
if (AttrKindStr == "sycl-work-group-size" ||
217308
AttrKindStr == "sycl-work-group-size-hint") {
218309
// Split values in the comma-separated list integers.
Lines changed: 223 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,223 @@
1+
; RUN: opt -passes=compile-time-properties %s -S | FileCheck %s
2+
3+
4+
define spir_kernel void @"Kernel0"() #0 {
5+
entry:
6+
ret void
7+
}
8+
9+
define spir_kernel void @"Kernel1"() #1 {
10+
entry:
11+
ret void
12+
}
13+
14+
define spir_kernel void @"Kernel2"() #2 {
15+
entry:
16+
ret void
17+
}
18+
19+
define spir_kernel void @"Kernel3"() #3 {
20+
entry:
21+
ret void
22+
}
23+
24+
define spir_kernel void @"Kernel4"() #4 {
25+
entry:
26+
ret void
27+
}
28+
29+
define spir_kernel void @"Kernel5"() #5 {
30+
entry:
31+
ret void
32+
}
33+
34+
define spir_kernel void @"Kernel6"() #6 {
35+
entry:
36+
ret void
37+
}
38+
39+
define spir_kernel void @"Kernel7"() #7 {
40+
entry:
41+
ret void
42+
}
43+
44+
define spir_kernel void @"Kernel8"() #8 {
45+
entry:
46+
ret void
47+
}
48+
49+
define spir_kernel void @"Kernel9"() #9 {
50+
entry:
51+
ret void
52+
}
53+
54+
define spir_kernel void @"Kernel10"() #10 {
55+
entry:
56+
ret void
57+
}
58+
59+
define spir_kernel void @"Kernel11"() #11 {
60+
entry:
61+
ret void
62+
}
63+
64+
define spir_kernel void @"Kernel12"() #12 {
65+
entry:
66+
ret void
67+
}
68+
69+
; SPIRV execution modes for FP control. | BitMask
70+
; ROUNDING_MODE_RTE = 4462; | 00000000000
71+
; ROUNDING_MODE_RTP_INTEL = 5620; | 00000010000
72+
; ROUNDING_MODE_RTN_INTEL = 5621; | 00000100000
73+
; ROUNDING_MODE_RTZ = 4463; | 00000110000
74+
; DEMORM_FLUSH_TO_ZERO = 4460; | 00000000000
75+
; DENORM_PRESERVE (double) = 4459; | 00001000000
76+
; DENORM_PRESERVE (float) = 4459; | 00010000000
77+
; DENORM_PRESERVE (half) = 4459; | 10000000000
78+
; FLOATING_POINT_MODE_ALT_INTEL = 5622; | 00000000001
79+
; FLOATING_POINT_MODE_IEEE_INTEL = 5623; | 00000000000
80+
81+
; rte + ftz + ieee (Default)
82+
; CHECK: !0 = !{ptr @Kernel0, i32 4462, i32 64}
83+
; CHECK: !1 = !{ptr @Kernel0, i32 4462, i32 32}
84+
; CHECK: !2 = !{ptr @Kernel0, i32 4462, i32 16}
85+
; CHECK: !3 = !{ptr @Kernel0, i32 5623, i32 64}
86+
; CHECK: !4 = !{ptr @Kernel0, i32 5623, i32 32}
87+
; CHECK: !5 = !{ptr @Kernel0, i32 5623, i32 16}
88+
; CHECK: !6 = !{ptr @Kernel0, i32 4460, i32 64}
89+
; CHECK: !7 = !{ptr @Kernel0, i32 4460, i32 32}
90+
; CHECK: !8 = !{ptr @Kernel0, i32 4460, i32 16}
91+
attributes #0 = { "sycl-floating-point-control"="0" }
92+
93+
; rtp + ftz + ieee
94+
; CHECK: !9 = !{ptr @Kernel1, i32 5620, i32 64}
95+
; CHECK: !10 = !{ptr @Kernel1, i32 5620, i32 32}
96+
; CHECK: !11 = !{ptr @Kernel1, i32 5620, i32 16}
97+
; CHECK: !12 = !{ptr @Kernel1, i32 5623, i32 64}
98+
; CHECK: !13 = !{ptr @Kernel1, i32 5623, i32 32}
99+
; CHECK: !14 = !{ptr @Kernel1, i32 5623, i32 16}
100+
; CHECK: !15 = !{ptr @Kernel1, i32 4460, i32 64}
101+
; CHECK: !16 = !{ptr @Kernel1, i32 4460, i32 32}
102+
; CHECK: !17 = !{ptr @Kernel1, i32 4460, i32 16}
103+
attributes #1 = { "sycl-floating-point-control"="16" }
104+
105+
; rtn + ftz + ieee
106+
; CHECK: !18 = !{ptr @Kernel2, i32 5621, i32 64}
107+
; CHECK: !19 = !{ptr @Kernel2, i32 5621, i32 32}
108+
; CHECK: !20 = !{ptr @Kernel2, i32 5621, i32 16}
109+
; CHECK: !21 = !{ptr @Kernel2, i32 5623, i32 64}
110+
; CHECK: !22 = !{ptr @Kernel2, i32 5623, i32 32}
111+
; CHECK: !23 = !{ptr @Kernel2, i32 5623, i32 16}
112+
; CHECK: !24 = !{ptr @Kernel2, i32 4460, i32 64}
113+
; CHECK: !25 = !{ptr @Kernel2, i32 4460, i32 32}
114+
; CHECK: !26 = !{ptr @Kernel2, i32 4460, i32 16}
115+
attributes #2 = { "sycl-floating-point-control"="32" }
116+
117+
; rtz + ftz + ieee
118+
; CHECK: !27 = !{ptr @Kernel3, i32 4463, i32 64}
119+
; CHECK: !28 = !{ptr @Kernel3, i32 4463, i32 32}
120+
; CHECK: !29 = !{ptr @Kernel3, i32 4463, i32 16}
121+
; CHECK: !30 = !{ptr @Kernel3, i32 5623, i32 64}
122+
; CHECK: !31 = !{ptr @Kernel3, i32 5623, i32 32}
123+
; CHECK: !32 = !{ptr @Kernel3, i32 5623, i32 16}
124+
; CHECK: !33 = !{ptr @Kernel3, i32 4460, i32 64}
125+
; CHECK: !34 = !{ptr @Kernel3, i32 4460, i32 32}
126+
; CHECK: !35 = !{ptr @Kernel3, i32 4460, i32 16}
127+
attributes #3 = { "sycl-floating-point-control"="48" }
128+
129+
; rte + denorm_preserve(double) + ieee
130+
; CHECK: !36 = !{ptr @Kernel4, i32 4462, i32 64}
131+
; CHECK: !37 = !{ptr @Kernel4, i32 4462, i32 32}
132+
; CHECK: !38 = !{ptr @Kernel4, i32 4462, i32 16}
133+
; CHECK: !39 = !{ptr @Kernel4, i32 5623, i32 64}
134+
; CHECK: !40 = !{ptr @Kernel4, i32 5623, i32 32}
135+
; CHECK: !41 = !{ptr @Kernel4, i32 5623, i32 16}
136+
; CHECK: !42 = !{ptr @Kernel4, i32 4459, i32 64}
137+
attributes #4 = { "sycl-floating-point-control"="64" }
138+
139+
; rte + denorm_preserve(float) + ieee
140+
; CHECK: !43 = !{ptr @Kernel5, i32 4462, i32 64}
141+
; CHECK: !44 = !{ptr @Kernel5, i32 4462, i32 32}
142+
; CHECK: !45 = !{ptr @Kernel5, i32 4462, i32 16}
143+
; CHECK: !46 = !{ptr @Kernel5, i32 5623, i32 64}
144+
; CHECK: !47 = !{ptr @Kernel5, i32 5623, i32 32}
145+
; CHECK: !48 = !{ptr @Kernel5, i32 5623, i32 16}
146+
; CHECK: !49 = !{ptr @Kernel5, i32 4459, i32 32}
147+
attributes #5 = { "sycl-floating-point-control"="128" }
148+
149+
; rte + denorm_preserve(half) + ieee
150+
; CHECK: !50 = !{ptr @Kernel6, i32 4462, i32 64}
151+
; CHECK: !51 = !{ptr @Kernel6, i32 4462, i32 32}
152+
; CHECK: !52 = !{ptr @Kernel6, i32 4462, i32 16}
153+
; CHECK: !53 = !{ptr @Kernel6, i32 5623, i32 64}
154+
; CHECK: !54 = !{ptr @Kernel6, i32 5623, i32 32}
155+
; CHECK: !55 = !{ptr @Kernel6, i32 5623, i32 16}
156+
; CHECK: !56 = !{ptr @Kernel6, i32 4459, i32 16}
157+
attributes #6 = { "sycl-floating-point-control"="1024" }
158+
159+
; rte + denorm_allow + ieee
160+
; CHECK: !57 = !{ptr @Kernel7, i32 4462, i32 64}
161+
; CHECK: !58 = !{ptr @Kernel7, i32 4462, i32 32}
162+
; CHECK: !59 = !{ptr @Kernel7, i32 4462, i32 16}
163+
; CHECK: !60 = !{ptr @Kernel7, i32 5623, i32 64}
164+
; CHECK: !61 = !{ptr @Kernel7, i32 5623, i32 32}
165+
; CHECK: !62 = !{ptr @Kernel7, i32 5623, i32 16}
166+
; CHECK: !63 = !{ptr @Kernel7, i32 4459, i32 16}
167+
; CHECK: !64 = !{ptr @Kernel7, i32 4459, i32 32}
168+
; CHECK: !65 = !{ptr @Kernel7, i32 4459, i32 64}
169+
attributes #7 = { "sycl-floating-point-control"="1216" }
170+
171+
; rte + ftz + alt
172+
; CHECK: !66 = !{ptr @Kernel8, i32 4462, i32 64}
173+
; CHECK: !67 = !{ptr @Kernel8, i32 4462, i32 32}
174+
; CHECK: !68 = !{ptr @Kernel8, i32 4462, i32 16}
175+
; CHECK: !69 = !{ptr @Kernel8, i32 5622, i32 64}
176+
; CHECK: !70 = !{ptr @Kernel8, i32 5622, i32 32}
177+
; CHECK: !71 = !{ptr @Kernel8, i32 5622, i32 16}
178+
; CHECK: !72 = !{ptr @Kernel8, i32 4460, i32 64}
179+
; CHECK: !73 = !{ptr @Kernel8, i32 4460, i32 32}
180+
; CHECK: !74 = !{ptr @Kernel8, i32 4460, i32 16}
181+
attributes #8 = { "sycl-floating-point-control"="1" }
182+
183+
; rtz + denorm_preserve(double) + ieee
184+
; CHECK: !75 = !{ptr @Kernel9, i32 4463, i32 64}
185+
; CHECK: !76 = !{ptr @Kernel9, i32 4463, i32 32}
186+
; CHECK: !77 = !{ptr @Kernel9, i32 4463, i32 16}
187+
; CHECK: !78 = !{ptr @Kernel9, i32 5623, i32 64}
188+
; CHECK: !79 = !{ptr @Kernel9, i32 5623, i32 32}
189+
; CHECK: !80 = !{ptr @Kernel9, i32 5623, i32 16}
190+
; CHECK: !81 = !{ptr @Kernel9, i32 4459, i32 64}
191+
attributes #9 = { "sycl-floating-point-control"="112" }
192+
193+
; rtp + denorm_preserve(float) + ieee
194+
; CHECK: !82 = !{ptr @Kernel10, i32 5620, i32 64}
195+
; CHECK: !83 = !{ptr @Kernel10, i32 5620, i32 32}
196+
; CHECK: !84 = !{ptr @Kernel10, i32 5620, i32 16}
197+
; CHECK: !85 = !{ptr @Kernel10, i32 5623, i32 64}
198+
; CHECK: !86 = !{ptr @Kernel10, i32 5623, i32 32}
199+
; CHECK: !87 = !{ptr @Kernel10, i32 5623, i32 16}
200+
; CHECK: !88 = !{ptr @Kernel10, i32 4459, i32 32}
201+
attributes #10 = { "sycl-floating-point-control"="144" }
202+
203+
; rtp + denorm_preserve(float) + alt
204+
; CHECK: !89 = !{ptr @Kernel11, i32 5620, i32 64}
205+
; CHECK: !90 = !{ptr @Kernel11, i32 5620, i32 32}
206+
; CHECK: !91 = !{ptr @Kernel11, i32 5620, i32 16}
207+
; CHECK: !92 = !{ptr @Kernel11, i32 5622, i32 64}
208+
; CHECK: !93 = !{ptr @Kernel11, i32 5622, i32 32}
209+
; CHECK: !94 = !{ptr @Kernel11, i32 5622, i32 16}
210+
; CHECK: !95 = !{ptr @Kernel11, i32 4459, i32 32}
211+
attributes #11 = { "sycl-floating-point-control"="145" }
212+
213+
; rtz + denorm_allow + alt
214+
; CHECK: !96 = !{ptr @Kernel12, i32 4463, i32 64}
215+
; CHECK: !97 = !{ptr @Kernel12, i32 4463, i32 32}
216+
; CHECK: !98 = !{ptr @Kernel12, i32 4463, i32 16}
217+
; CHECK: !99 = !{ptr @Kernel12, i32 5622, i32 64}
218+
; CHECK: !100 = !{ptr @Kernel12, i32 5622, i32 32}
219+
; CHECK: !101 = !{ptr @Kernel12, i32 5622, i32 16}
220+
; CHECK: !102 = !{ptr @Kernel12, i32 4459, i32 16}
221+
; CHECK: !103 = !{ptr @Kernel12, i32 4459, i32 32}
222+
; CHECK: !104 = !{ptr @Kernel12, i32 4459, i32 64}
223+
attributes #12 = { "sycl-floating-point-control"="1265" }

0 commit comments

Comments
 (0)