Skip to content

Commit b359eca

Browse files
KornevNikitasys-ce-bb
authored andcommitted
Support sycl_ext_oneapi_prefetch (#2189)
SYCL part: #11458 #11597 Handle new properties and decorate prefetch's arg. Original commit: KhronosGroup/SPIRV-LLVM-Translator@a76f24e
1 parent fb2bc1c commit b359eca

File tree

2 files changed

+118
-5
lines changed

2 files changed

+118
-5
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2946,10 +2946,12 @@ struct AnnotationDecorations {
29462946
DecorationsInfoVec MemoryAccessesVec;
29472947
DecorationsInfoVec BufferLocationVec;
29482948
DecorationsInfoVec LatencyControlVec;
2949+
DecorationsInfoVec CacheControlVec;
29492950

29502951
bool empty() {
29512952
return (MemoryAttributesVec.empty() && MemoryAccessesVec.empty() &&
2952-
BufferLocationVec.empty() && LatencyControlVec.empty());
2953+
BufferLocationVec.empty() && LatencyControlVec.empty() &&
2954+
CacheControlVec.empty());
29532955
}
29542956
};
29552957

@@ -3144,6 +3146,8 @@ AnnotationDecorations tryParseAnnotationString(SPIRVModule *BM,
31443146
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_buffer_location);
31453147
const bool AllowFPGALatencyControl =
31463148
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_latency_control);
3149+
const bool AllowCacheControls =
3150+
BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_cache_controls);
31473151

31483152
bool ValidDecorationFound = false;
31493153
DecorationsInfoVec DecorationsVec;
@@ -3173,6 +3177,11 @@ AnnotationDecorations tryParseAnnotationString(SPIRVModule *BM,
31733177
DecorationLatencyControlConstraintINTEL)) {
31743178
Decorates.LatencyControlVec.emplace_back(
31753179
static_cast<Decoration>(DecorationKind), std::move(DecValues));
3180+
} else if (AllowCacheControls &&
3181+
DecorationKind ==
3182+
internal::DecorationCacheControlLoadINTEL) {
3183+
Decorates.CacheControlVec.emplace_back(
3184+
static_cast<Decoration>(DecorationKind), std::move(DecValues));
31763185
} else {
31773186
DecorationsVec.emplace_back(static_cast<Decoration>(DecorationKind),
31783187
std::move(DecValues));
@@ -3282,7 +3291,7 @@ void addAnnotationDecorations(SPIRVEntry *E, DecorationsInfoVec &Decorations) {
32823291
if (I.first != DecorationUserSemantic)
32833292
continue;
32843293

3285-
switch (I.first) {
3294+
switch (static_cast<size_t>(I.first)) {
32863295
case DecorationUserSemantic:
32873296
M->getErrorLog().checkError(I.second.size() == 1,
32883297
SPIRVEC_InvalidLlvmModule,
@@ -3403,6 +3412,21 @@ void addAnnotationDecorations(SPIRVEntry *E, DecorationsInfoVec &Decorations) {
34033412
}
34043413
break;
34053414
}
3415+
case spv::internal::DecorationCacheControlLoadINTEL: {
3416+
if (M->isAllowedToUseExtension(ExtensionID::SPV_INTEL_cache_controls)) {
3417+
M->getErrorLog().checkError(
3418+
I.second.size() == 2, SPIRVEC_InvalidLlvmModule,
3419+
"CacheControlLoadINTEL requires exactly 2 extra operands");
3420+
SPIRVWord CacheLevel = 0;
3421+
SPIRVWord CacheControl = 0;
3422+
StringRef(I.second[0]).getAsInteger(10, CacheLevel);
3423+
StringRef(I.second[1]).getAsInteger(10, CacheControl);
3424+
E->addDecorate(new SPIRVDecorateCacheControlLoadINTEL(
3425+
E, CacheLevel,
3426+
static_cast<internal::LoadCacheControlINTEL>(CacheControl)));
3427+
}
3428+
}
3429+
34063430
default:
34073431
// Other decorations are either not supported by the translator or
34083432
// handled in other places.
@@ -4233,9 +4257,7 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
42334257
} else {
42344258
// Memory accesses to a standalone pointer variable
42354259
auto *DecSubj = transValue(II->getArgOperand(0), BB);
4236-
if (Decorations.MemoryAccessesVec.empty() &&
4237-
Decorations.BufferLocationVec.empty() &&
4238-
Decorations.LatencyControlVec.empty())
4260+
if (Decorations.empty())
42394261
DecSubj->addDecorate(new SPIRVDecorateUserSemanticAttr(
42404262
DecSubj, AnnotationString.c_str()));
42414263
else {
@@ -4248,6 +4270,8 @@ SPIRVValue *LLVMToSPIRVBase::transIntrinsicInst(IntrinsicInst *II,
42484270
addAnnotationDecorations(DecSubj, Decorations.BufferLocationVec);
42494271
addAnnotationDecorations(DecSubj, Decorations.LatencyControlVec);
42504272
}
4273+
4274+
addAnnotationDecorations(DecSubj, Decorations.CacheControlVec);
42514275
}
42524276
II->replaceAllUsesWith(II->getOperand(0));
42534277
}
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv --spirv-ext=+SPV_INTEL_cache_controls -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3+
4+
; RUN: llvm-spirv --spirv-ext=+SPV_INTEL_cache_controls %t.bc -o %t.spv
5+
; RUN: llvm-spirv -r %t.spv --spirv-target-env=SPV-IR -o - | llvm-dis -o - | FileCheck %s --check-prefix=CHECK-LLVM
6+
7+
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"
8+
target triple = "spir64-unknown-unknown"
9+
10+
$_ZTSZ4mainEUlvE_ = comdat any
11+
12+
; https://github.com/KhronosGroup/SPIRV-Registry/blob/main/extensions/INTEL/SPV_INTEL_cache_controls.asciidoc
13+
; These strings are:
14+
; {CacheControlLoadINTEL_Token:\22CacheLevel,CacheControl\22}
15+
@.str.1 = private unnamed_addr addrspace(1) constant [16 x i8] c"../prefetch.hpp\00", section "llvm.metadata"
16+
@.str.9 = private unnamed_addr addrspace(1) constant [13 x i8] c"{6442:\220,1\22}\00", section "llvm.metadata"
17+
@.str.10 = private unnamed_addr addrspace(1) constant [13 x i8] c"{6442:\221,1\22}\00", section "llvm.metadata"
18+
@.str.11 = private unnamed_addr addrspace(1) constant [13 x i8] c"{6442:\222,3\22}\00", section "llvm.metadata"
19+
20+
; these CHECK-SPIRV check that prefetch's arg is decorated with the appropriate
21+
; CacheLevel and CacheControl values.
22+
23+
; CHECK-SPIRV: Decorate [[PTR_ID1:.*]] CacheControlLoadINTEL 0 1
24+
; CHECK-SPIRV: Decorate [[PTR_ID2:.*]] CacheControlLoadINTEL 1 1
25+
; CHECK-SPIRV: Decorate [[PTR_ID3:.*]] CacheControlLoadINTEL 2 3
26+
27+
; CHECK-SPIRV: ExtInst [[#]] [[#]] [[#]] prefetch [[PTR_ID1]] [[#]]
28+
; CHECK-SPIRV: ExtInst [[#]] [[#]] [[#]] prefetch [[PTR_ID2]] [[#]]
29+
; CHECK-SPIRV: ExtInst [[#]] [[#]] [[#]] prefetch [[PTR_ID3]] [[#]]
30+
31+
; Check that the appropriate !spirv.Decorations are preserved after reverse
32+
; translation
33+
34+
; CHECK-LLVM: %[[CALL1:.*]] = call spir_func ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobal{{.*}} !spirv.Decorations ![[MD1:.*]]
35+
; CHECK-LLVM: call spir_func void @_Z20__spirv_ocl_prefetch{{.*}}(ptr addrspace(1) %[[CALL1]], i64 1)
36+
; CHECK-LLVM: %[[CALL2:.*]] = call spir_func ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobal{{.*}} !spirv.Decorations ![[MD2:.*]]
37+
; CHECK-LLVM: call spir_func void @_Z20__spirv_ocl_prefetch{{.*}}(ptr addrspace(1) %[[CALL2]], i64 1)
38+
; CHECK-LLVM: %[[CALL3:.*]] = call spir_func ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobal{{.*}} !spirv.Decorations ![[MD3:.*]]
39+
; CHECK-LLVM: call spir_func void @_Z20__spirv_ocl_prefetch{{.*}}(ptr addrspace(1) %[[CALL3]], i64 2)
40+
41+
42+
; Function Attrs: convergent norecurse nounwind
43+
define weak_odr dso_local spir_kernel void @_ZTSZ4mainEUlvE_(ptr addrspace(1) noundef align 1 %_arg_dataPtr) local_unnamed_addr comdat !srcloc !5 !kernel_arg_buffer_location !6 !sycl_fixed_targets !7 !sycl_kernel_omit_args !8 {
44+
entry:
45+
%0 = addrspacecast ptr addrspace(1) %_arg_dataPtr to ptr addrspace(4)
46+
%call.i.i.i.i = tail call spir_func noundef ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobalPvi(ptr addrspace(4) noundef %0, i32 noundef 5)
47+
%1 = tail call ptr addrspace(1) @llvm.ptr.annotation.p1.p1(ptr addrspace(1) %call.i.i.i.i, ptr addrspace(1) @.str.9, ptr addrspace(1) @.str.1, i32 76, ptr addrspace(1) null)
48+
tail call spir_func void @_Z20__spirv_ocl_prefetchPU3AS1Kcm(ptr addrspace(1) noundef %1, i64 noundef 1)
49+
%arrayidx3.i = getelementptr inbounds i8, ptr addrspace(4) %0, i64 1
50+
%call.i.i.i13.i = tail call spir_func noundef ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobalPvi(ptr addrspace(4) noundef %arrayidx3.i, i32 noundef 5)
51+
%2 = tail call ptr addrspace(1) @llvm.ptr.annotation.p1.p1(ptr addrspace(1) %call.i.i.i13.i, ptr addrspace(1) @.str.10, ptr addrspace(1) @.str.1, i32 80, ptr addrspace(1) null)
52+
tail call spir_func void @_Z20__spirv_ocl_prefetchPU3AS1Kcm(ptr addrspace(1) noundef %2, i64 noundef 1)
53+
%arrayidx7.i = getelementptr inbounds i8, ptr addrspace(4) %0, i64 2
54+
%call.i.i.i16.i = tail call spir_func noundef ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobalPvi(ptr addrspace(4) noundef %arrayidx7.i, i32 noundef 5)
55+
%3 = tail call ptr addrspace(1) @llvm.ptr.annotation.p1.p1(ptr addrspace(1) %call.i.i.i16.i, ptr addrspace(1) @.str.11, ptr addrspace(1) @.str.1, i32 80, ptr addrspace(1) null)
56+
tail call spir_func void @_Z20__spirv_ocl_prefetchPU3AS1Kcm(ptr addrspace(1) noundef %3, i64 noundef 2)
57+
ret void
58+
}
59+
60+
; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(inaccessiblemem: readwrite)
61+
declare ptr addrspace(1) @llvm.ptr.annotation.p1.p1(ptr addrspace(1), ptr addrspace(1), ptr addrspace(1), i32, ptr addrspace(1))
62+
63+
; Function Attrs: convergent nounwind
64+
declare dso_local spir_func void @_Z20__spirv_ocl_prefetchPU3AS1Kcm(ptr addrspace(1) noundef, i64 noundef) local_unnamed_addr
65+
66+
; Function Attrs: convergent mustprogress nofree nounwind willreturn memory(none)
67+
declare dso_local spir_func noundef ptr addrspace(1) @_Z41__spirv_GenericCastToPtrExplicit_ToGlobalPvi(ptr addrspace(4) noundef, i32 noundef) local_unnamed_addr
68+
69+
!llvm.module.flags = !{!0, !1}
70+
!opencl.spir.version = !{!2}
71+
!spirv.Source = !{!3}
72+
!llvm.ident = !{!4}
73+
74+
; CHECK-LLVM: ![[MD1]] = !{![[MD10:.*]]}
75+
; ![[MD10]] = !{i32 6442, i32 0, i32 1}
76+
; CHECK-LLVM: ![[MD2]] = !{![[MD20:.*]]}
77+
; ![[MD20]] = !{i32 6442, i32 1, i32 1}
78+
; CHECK-LLVM: ![[MD3]] = !{![[MD30:.*]]}
79+
; ![[MD30]] = !{i32 6442, i32 2, i32 3}
80+
81+
!0 = !{i32 1, !"wchar_size", i32 4}
82+
!1 = !{i32 7, !"frame-pointer", i32 2}
83+
!2 = !{i32 1, i32 2}
84+
!3 = !{i32 4, i32 100000}
85+
!4 = !{!"clang version 18.0.0"}
86+
!5 = !{i32 1522}
87+
!6 = !{i32 -1}
88+
!7 = !{}
89+
!8 = !{i1 false}

0 commit comments

Comments
 (0)