Skip to content

Commit ed25856

Browse files
authored
Workaround unsupported freeze insn (#2087)
Workaround unsupported freeze insn by: replacing uses of freeze's result with freeze's source or a random (but compilation reproducible) constant if freeze's source is undef/poison deleting freeze insn. Long term solution is to add a freeze instruction extension in SPIR-V. Issue is tracked in (#1140) Signed-off-by: Lu, John <[email protected]>
1 parent ff8c3e7 commit ed25856

File tree

2 files changed

+122
-0
lines changed

2 files changed

+122
-0
lines changed

lib/SPIRV/SPIRVRegularizeLLVM.cpp

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -413,6 +413,17 @@ bool SPIRVRegularizeLLVMBase::regularize() {
413413
if (isa<PossiblyExactOperator>(BO) && BO->isExact())
414414
BO->setIsExact(false);
415415
}
416+
417+
// FIXME: This is not valid handling for freeze instruction
418+
if (auto *FI = dyn_cast<FreezeInst>(&II)) {
419+
auto *V = FI->getOperand(0);
420+
if (isa<UndefValue>(V))
421+
V = Constant::getNullValue(V->getType());
422+
FI->replaceAllUsesWith(V);
423+
FI->dropAllReferences();
424+
ToErase.push_back(FI);
425+
}
426+
416427
// Remove metadata not supported by SPIRV
417428
static const char *MDs[] = {
418429
"fpmath",

test/freeze.ll

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
;; Test to check that freeze instruction does not cause a crash
2+
; RUN: llvm-as %s -o %t.bc
3+
; RUN: llvm-spirv %t.bc -o %t.spv
4+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
5+
; All freeze instructions should be deleted and uses of freeze's result should be replaced
6+
; with freeze's source or a random constant if freeze's source is poison or undef.
7+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM --implicit-check-not="= freeze"
8+
9+
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"
10+
target triple = "spir64-unknown-unknown"
11+
12+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
13+
; test i32
14+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
15+
16+
; CHECK-LLVM: @testfunction_i32A
17+
; Uses of result should be replaced with freeze's source
18+
; CHECK-LLVM-NEXT: add nsw i32 %val, 1
19+
define spir_func i32 @testfunction_i32A(i32 %val) {
20+
%1 = freeze i32 %val
21+
%2 = add nsw i32 %1, 1
22+
ret i32 %2
23+
}
24+
25+
; CHECK-LLVM: @testfunction_i32B
26+
; Frozen poison/undef should produce a constant.
27+
; add should be deleted since both inputs are constant.
28+
; CHECK-LLVM-NEXT: ret i32
29+
define spir_func i32 @testfunction_i32B(i32 %val) {
30+
%1 = freeze i32 poison
31+
%2 = add nsw i32 %1, 1
32+
ret i32 %2
33+
}
34+
35+
; CHECK-LLVM: @testfunction_i32C
36+
; Frozen poison/undef should produce a constant.
37+
; add should be deleted since both inputs are constant.
38+
; CHECK-LLVM-NEXT: ret i32
39+
define spir_func i32 @testfunction_i32C(i32 %val) {
40+
%1 = freeze i32 undef
41+
%2 = add nsw i32 %1, 1
42+
ret i32 %2
43+
}
44+
45+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
46+
; test float
47+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
48+
49+
; CHECK-LLVM: @testfunction_floatA
50+
; freeze should be eliminated.
51+
; Uses of result should be replaced with freeze's source
52+
; CHECK-LLVM-NEXT: fadd float %val
53+
define spir_func float @testfunction_floatA(float %val) {
54+
%1 = freeze float %val
55+
%2 = fadd float %1, 1.0
56+
ret float %2
57+
}
58+
59+
; CHECK-LLVM: @testfunction_floatB
60+
; Frozen poison/undef should produce a constant.
61+
; add should be deleted since both inputs are constant.
62+
; CHECK-LLVM-NEXT: ret float
63+
define spir_func float @testfunction_floatB(float %val) {
64+
%1 = freeze float poison
65+
%2 = fadd float %1, 1.0
66+
ret float %2
67+
}
68+
69+
; CHECK-LLVM: @testfunction_floatC
70+
; Frozen poison/undef should produce a constant.
71+
; add should be deleted since both inputs are constant.
72+
; CHECK-LLVM-NEXT: ret float
73+
define spir_func float @testfunction_floatC(float %val) {
74+
%1 = freeze float undef
75+
%2 = fadd float %1, 1.0
76+
ret float %2
77+
}
78+
79+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
80+
; test ptr
81+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
82+
83+
; CHECK-LLVM: @testfunction_ptrA
84+
; freeze should be eliminated.
85+
; Uses of result should be replaced with freeze's source
86+
; CHECK-LLVM-NEXT: ptrtoint ptr %val to i64
87+
define spir_func i64 @testfunction_ptrA(ptr %val) {
88+
%1 = freeze ptr %val
89+
%2 = ptrtoint ptr %1 to i64
90+
ret i64 %2
91+
}
92+
93+
; CHECK-LLVM: @testfunction_ptrB
94+
; Frozen poison/undef should produce a constant.
95+
; For ptr type this constant is null.
96+
; CHECK-LLVM-NEXT: ptrtoint ptr null to i64
97+
define spir_func i64 @testfunction_ptrB(ptr addrspace(1) %val) {
98+
%1 = freeze ptr poison
99+
%2 = ptrtoint ptr %1 to i64
100+
ret i64 %2
101+
}
102+
103+
; CHECK-LLVM: @testfunction_ptrC
104+
; Frozen poison/undef should produce a constant.
105+
; For ptr type this constant is null.
106+
; CHECK-LLVM-NEXT: ptrtoint ptr null to i64
107+
define spir_func i64 @testfunction_ptrC(ptr addrspace(1) %val) {
108+
%1 = freeze ptr undef
109+
%2 = ptrtoint ptr %1 to i64
110+
ret i64 %2
111+
}

0 commit comments

Comments
 (0)