Skip to content

Commit 6f707a4

Browse files
wenju-hesys-ce-bb
authored andcommitted
Fix target id in decorates when there is forward decl (#2606)
When there is forward declaration of a spirv entry, its decorates are not translated until its definition is seen. Forward id is re-used for its entry. Id in entry decorates should use forward id as well. Original commit: KhronosGroup/SPIRV-LLVM-Translator@305f48884606abf
1 parent de4b365 commit 6f707a4

File tree

4 files changed

+56
-4
lines changed

4 files changed

+56
-4
lines changed

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVEntry.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,16 @@ void SPIRVEntry::takeAnnotations(SPIRVForward *E) {
386386
static_cast<SPIRVFunction *>(this)->takeExecutionModes(E);
387387
}
388388

389+
void SPIRVEntry::replaceTargetIdInDecorates(SPIRVId Id) {
390+
for (auto It = Decorates.begin(), E = Decorates.end(); It != E; ++It)
391+
const_cast<SPIRVDecorate *>(It->second)->setTargetId(Id);
392+
for (auto It = DecorateIds.begin(), E = DecorateIds.end(); It != E; ++It)
393+
const_cast<SPIRVDecorateId *>(It->second)->setTargetId(Id);
394+
for (auto It = MemberDecorates.begin(), E = MemberDecorates.end(); It != E;
395+
++It)
396+
const_cast<SPIRVMemberDecorate *>(It->second)->setTargetId(Id);
397+
}
398+
389399
// Check if an entry has Kind of decoration and get the literal of the
390400
// first decoration of such kind at Index.
391401
bool SPIRVEntry::hasDecorate(Decoration Kind, size_t Index,

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVEntry.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,7 @@ class SPIRVEntry {
371371
void takeDecorates(SPIRVEntry *);
372372
void takeDecorateIds(SPIRVEntry *);
373373
void takeMemberDecorates(SPIRVEntry *);
374+
void replaceTargetIdInDecorates(SPIRVId);
374375

375376
/// After a SPIRV entry is created during reading SPIRV binary by default
376377
/// constructor, this function is called to allow the SPIRV entry to resize

llvm-spirv/lib/SPIRV/libSPIRV/SPIRVModule.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1176,17 +1176,19 @@ SPIRVEntry *SPIRVModuleImpl::replaceForward(SPIRVForward *Forward,
11761176
SPIRVEntry *Entry) {
11771177
SPIRVId Id = Entry->getId();
11781178
SPIRVId ForwardId = Forward->getId();
1179-
if (ForwardId == Id)
1179+
if (ForwardId == Id) {
11801180
IdEntryMap[Id] = Entry;
1181-
else {
1181+
// Annotations include name, decorations, execution modes
1182+
Entry->takeAnnotations(Forward);
1183+
} else {
11821184
auto Loc = IdEntryMap.find(Id);
11831185
assert(Loc != IdEntryMap.end());
11841186
IdEntryMap.erase(Loc);
11851187
Entry->setId(ForwardId);
11861188
IdEntryMap[ForwardId] = Entry;
1189+
// Replace current Id with ForwardId in decorates.
1190+
Entry->replaceTargetIdInDecorates(ForwardId);
11871191
}
1188-
// Annotations include name, decorations, execution modes
1189-
Entry->takeAnnotations(Forward);
11901192
delete Forward;
11911193
return Entry;
11921194
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV
3+
; RUN: llvm-spirv %t.bc -o %t.spv
4+
; RUN: spirv-val %t.spv
5+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
6+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
7+
8+
; Check saturation conversion is translated when there is forward declaration
9+
; of SPIRV entry.
10+
11+
; CHECK-SPIRV: Decorate [[SAT:[0-9]+]] SaturatedConversion
12+
; CHECK-SPIRV: ConvertFToU {{[0-9]+}} [[SAT]]
13+
14+
; CHECK-LLVM: convert_uchar_satf
15+
16+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-G1"
17+
target triple = "spir64"
18+
19+
declare spir_func zeroext i8 @_Z30__spirv_ConvertFToU_Ruchar_satf(float)
20+
21+
define spir_func void @forward(float %val, ptr addrspace(1) %dst) {
22+
entry:
23+
br label %for.cond
24+
25+
for.cond: ; preds = %for.body, %entry
26+
%new_val.0 = phi i8 [ undef, %entry ], [ %call1, %for.body ]
27+
%i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ]
28+
%cmp = icmp ult i32 %i.0, 1
29+
br i1 %cmp, label %for.body, label %for.end
30+
31+
for.body: ; preds = %for.cond
32+
%call1 = call spir_func zeroext i8 @_Z30__spirv_ConvertFToU_Ruchar_satf(float noundef %val)
33+
%inc = add i32 %i.0, 1
34+
br label %for.cond
35+
36+
for.end: ; preds = %for.cond
37+
store i8 %new_val.0, ptr addrspace(1) %dst, align 1
38+
ret void
39+
}

0 commit comments

Comments
 (0)