Skip to content

Commit fca0712

Browse files
artemradsys-ce-bb
authored andcommitted
Can apply MemoryINTEL annotation multiple times on same variable (#2230)
According to SPIRV spec only one instance of a specific decoration can be on a SPIRV-Id, unless the decoration explicitly allows it. LLVM IR has no such limitation. MemoryINTEL can have two annotations on the same pointer in LLVM IR. When it comes to translating this to SPIRV, we can filter out the less precise annotation (DEFAULT) and only keep one that will be translated to a SPIRV decoration. Original commit: KhronosGroup/SPIRV-LLVM-Translator@ff48393
1 parent b0306ba commit fca0712

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3242,6 +3242,28 @@ AnnotationDecorations tryParseAnnotationString(SPIRVModule *BM,
32423242
internal::DecorationCacheControlLoadINTEL) {
32433243
Decorates.CacheControlVec.emplace_back(
32443244
static_cast<Decoration>(DecorationKind), std::move(DecValues));
3245+
} else if (DecorationKind == DecorationMemoryINTEL) {
3246+
// SPIRV doesn't allow the same Decoration to be applied multiple
3247+
// times on a single SPIRVEntry, unless explicitly allowed by the
3248+
// language spec. Filter out the less specific MemoryINTEL
3249+
// decorations, if applied multiple times
3250+
auto CanFilterOut = [](auto &Val) {
3251+
if (!Val.second.empty())
3252+
return (Val.second[0] == "DEFAULT");
3253+
return false;
3254+
};
3255+
auto It = std::find_if(DecorationsVec.begin(), DecorationsVec.end(),
3256+
CanFilterOut);
3257+
3258+
if (It != DecorationsVec.end()) {
3259+
// replace the less specific decoration
3260+
*It = {static_cast<Decoration>(DecorationKind),
3261+
std::move(DecValues)};
3262+
} else {
3263+
// add new decoration
3264+
DecorationsVec.emplace_back(static_cast<Decoration>(DecorationKind),
3265+
std::move(DecValues));
3266+
}
32453267
} else {
32463268
DecorationsVec.emplace_back(static_cast<Decoration>(DecorationKind),
32473269
std::move(DecValues));
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
; Test checks that MemoryINTEL decoration can be applied twice on a single
2+
; variable
3+
4+
; RUN: llvm-as %s -o %t.bc
5+
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_fpga_memory_attributes -o %t.spv
6+
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
7+
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
8+
; RUN: llvm-spirv -r %t.spv --spirv-target-env=SPV-IR -o %t.rev.bc
9+
; RUN: llvm-dis %t.rev.bc
10+
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM
11+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
12+
; RUN: llvm-dis %t.rev.bc
13+
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM
14+
15+
; CHECK-SPIRV: Capability FPGAMemoryAttributesINTEL
16+
; CHECK-SPIRV: Extension "SPV_INTEL_fpga_memory_attributes"
17+
; CHECK-SPIRV: Decorate [[#empty:]] MemoryINTEL "DEFAULT"
18+
; CHECK-SPIRV-NOT: Decorate [[#]] MemoryINTEL "DEFAULT"
19+
; CHECK-SPIRV: Decorate [[#mlab:]] MemoryINTEL "MLAB"
20+
; CHECK-SPIRV-NOT: Decorate [[#]] MemoryINTEL "DEFAULT"
21+
; CHECK-SPIRV: Decorate [[#block_ram:]] MemoryINTEL "BLOCK_RAM"
22+
23+
24+
; ModuleID = '/nfs/site/disks/swuser_work_aradzikh/external/llvm-intel/sycl/test/check_device_code/fpga_mem_local.cpp'
25+
source_filename = "fpga_mem_local.cpp"
26+
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"
27+
target triple = "spir64-unknown-unknown"
28+
29+
%"fpga_mem" = type { [10 x i32] }
30+
31+
@.str.1 = private unnamed_addr addrspace(1) constant [17 x i8] c"{5826:\22DEFAULT\22}\00", section "llvm.metadata"
32+
@.str.2 = private unnamed_addr addrspace(1) constant [30 x i8] c"{5826:\22DEFAULT\22}{5826:\22MLAB\22}\00", section "llvm.metadata"
33+
@.str.3 = private unnamed_addr addrspace(1) constant [35 x i8] c"{5826:\22DEFAULT\22}{5826:\22BLOCK_RAM\22}\00", section "llvm.metadata"
34+
35+
; CHECK-LLVM: [[empty_annot:]] = private unnamed_addr constant [17 x i8] c"{memory:DEFAULT}\00", align 1
36+
; CHECK-LLVM: [[mlab_annot:]] = private unnamed_addr constant [14 x i8] c"{memory:MLAB}\00", align 1
37+
; CHECK-LLVM: [[block_ram_annot:]] = private unnamed_addr constant [19 x i8] c"{memory:BLOCK_RAM}\00", align 1
38+
39+
; Function Attrs: mustprogress norecurse nounwind
40+
define weak_odr dso_local spir_kernel void @kernel(ptr addrspace(4) %out) {
41+
entry:
42+
%empty.i = alloca %"fpga_mem", align 4
43+
%mlab.i = alloca %"fpga_mem", align 4
44+
%block_ram.i = alloca %"fpga_mem", align 4
45+
%empty.ascast.i = addrspacecast ptr %empty.i to ptr addrspace(4)
46+
%mlab.ascast.i = addrspacecast ptr %mlab.i to ptr addrspace(4)
47+
%block_ram.ascast.i = addrspacecast ptr %block_ram.i to ptr addrspace(4)
48+
49+
call void @llvm.var.annotation.p4.p1(ptr addrspace(4) %empty.ascast.i, ptr addrspace(1) @.str.1, ptr addrspace(1) undef, i32 undef, ptr addrspace(1) undef)
50+
; CHECK-SPV-IR: call void @llvm.var.annotation{{.*}}(ptr addrspace(4) %empty{{.*}}, ptr [[empty_annot]]
51+
; CHECK-LLVM: call void @llvm.var.annotation{{.*}}(ptr addrspace(4) %empty{{.*}}, ptr [[empty_annot]]
52+
call void @llvm.var.annotation.p4.p1(ptr addrspace(4) %mlab.ascast.i, ptr addrspace(1) @.str.2, ptr addrspace(1) undef, i32 undef, ptr addrspace(1) undef)
53+
; CHECK-SPV-IR: call void @llvm.var.annotation{{.*}}(ptr addrspace(4) %mlab{{.*}}, ptr [[mlab_annot]]
54+
; CHECK-LLVM: call void @llvm.var.annotation{{.*}}(ptr addrspace(4) %mlab{{.*}}, ptr [[mlab_annot]]
55+
call void @llvm.var.annotation.p4.p1(ptr addrspace(4) %block_ram.ascast.i, ptr addrspace(1) @.str.3, ptr addrspace(1) undef, i32 undef, ptr addrspace(1) undef)
56+
; CHECK-SPV-IR: call void @llvm.var.annotation{{.*}}(ptr addrspace(4) %block_ram{{.*}}, ptr [[block_ram_annot]]
57+
; CHECK-LLVM: call void @llvm.var.annotation{{.*}}(ptr addrspace(4) %block_ram{{.*}}, ptr [[block_ram_annot]]
58+
59+
%l1 = load i32, ptr addrspace(4) %empty.ascast.i, align 4
60+
%l2 = load i32, ptr addrspace(4) %mlab.ascast.i, align 4
61+
%l3 = load i32, ptr addrspace(4) %block_ram.ascast.i, align 4
62+
63+
%add1 = add nsw i32 %l1, %l2
64+
%add2 = add nsw i32 %add1, %l3
65+
store i32 %add2, ptr addrspace(4) %out, align 4
66+
67+
ret void
68+
}
69+
70+
declare void @llvm.var.annotation.p4.p1(ptr addrspace(4), ptr addrspace(1), ptr addrspace(1), i32, ptr addrspace(1))

0 commit comments

Comments
 (0)