Skip to content

Commit 4f97731

Browse files
committed
[SPIR-V] Add spv.gep support for ptrcast legal
Adds support the the spv.gep intrinsic to the spv ptrcast legalization step. Those intrinsics are generated by the backend thus not directly visible in the tests. This is a pre-requisite to implement addrspacecast legalization for logical SPIR-V.
1 parent 7d4ea77 commit 4f97731

File tree

2 files changed

+53
-0
lines changed

2 files changed

+53
-0
lines changed

llvm/lib/Target/SPIRV/SPIRVLegalizePointerCast.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -170,6 +170,12 @@ class SPIRVLegalizePointerCast : public FunctionPass {
170170
DeadInstructions.push_back(Intrin);
171171
continue;
172172
}
173+
174+
if (Intrin->getIntrinsicID() == Intrinsic::spv_gep) {
175+
GR->replaceAllUsesWith(CastedOperand, OriginalOperand,
176+
/* DeleteOld= */ false);
177+
continue;
178+
}
173179
}
174180

175181
llvm_unreachable("Unsupported ptrcast user. Please fix.");
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
; RUN: llc -verify-machineinstrs -O0 -mtriple=spirv-unknown-vulkan-compute %s -o - | FileCheck %s --match-full-lines
2+
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv-unknown-vulkan %s -o - -filetype=obj | spirv-val %}
3+
4+
; CHECK-DAG: %[[#uint:]] = OpTypeInt 32 0
5+
; CHECK-DAG: %[[#uint64:]] = OpTypeInt 64 0
6+
; CHECK-DAG: %[[#uint_pp:]] = OpTypePointer Private %[[#uint]]
7+
; CHECK-DAG: %[[#uint_0:]] = OpConstant %[[#uint]] 0
8+
; CHECK-DAG: %[[#uint_1:]] = OpConstant %[[#uint]] 1
9+
; CHECK-DAG: %[[#uint_10:]] = OpConstant %[[#uint]] 10
10+
; CHECK-DAG: %[[#t_array:]] = OpTypeArray %[[#uint]] %[[#uint_10]]
11+
; CHECK-DAG: %[[#t_s1:]] = OpTypeStruct %[[#t_array]]
12+
; CHECK-DAG: %[[#t_s2_s_a_s:]] = OpTypeStruct %[[#uint]] %[[#uint]]
13+
; CHECK-DAG: %[[#t_s2_s_a:]] = OpTypeArray %[[#t_s2_s_a_s]] %[[#uint_10]]
14+
; CHECK-DAG: %[[#t_s2_s:]] = OpTypeStruct %[[#t_s2_s_a]]
15+
; CHECK-DAG: %[[#t_s2:]] = OpTypeStruct %[[#t_s2_s]] %[[#uint]]
16+
; CHECK-DAG: %[[#null_s1:]] = OpConstantNull %[[#t_s1]]
17+
; CHECK-DAG: %[[#null_s2:]] = OpConstantNull %[[#t_s2]]
18+
; CHECK-DAG: %[[#ptr_s1:]] = OpTypePointer Private %[[#t_s1]]
19+
; CHECK-DAG: %[[#ptr_s2:]] = OpTypePointer Private %[[#t_s2]]
20+
21+
%S1 = type { [10 x i32] }
22+
%S2 = type { { [10 x { i32, i32 } ] }, i32 }
23+
24+
; CHECK-DAG: %[[#global1:]] = OpVariable %[[#ptr_s1]] Private %[[#null_s1]]
25+
@global1 = internal addrspace(10) global %S1 zeroinitializer
26+
; CHECK-DAG: %[[#global2:]] = OpVariable %[[#ptr_s2]] Private %[[#null_s2]]
27+
@global2 = internal addrspace(10) global %S2 zeroinitializer
28+
29+
define spir_func noundef i32 @foo(i64 noundef %index) local_unnamed_addr {
30+
; CHECK: %[[#index:]] = OpFunctionParameter %[[#uint64]]
31+
entry:
32+
; CHECK: %[[#ptr:]] = OpInBoundsAccessChain %[[#uint_pp]] %[[#global1]] %[[#uint_0]] %[[#index]]
33+
%ptr = getelementptr inbounds %S1, ptr addrspace(10) @global1, i64 0, i32 0, i64 %index
34+
; CHECK: %[[#val:]] = OpLoad %[[#uint]] %[[#ptr]] Aligned 4
35+
%val = load i32, ptr addrspace(10) %ptr
36+
ret i32 %val
37+
}
38+
39+
define spir_func noundef i32 @bar(i64 noundef %index) local_unnamed_addr {
40+
; CHECK: %[[#index:]] = OpFunctionParameter %[[#uint64]]
41+
entry:
42+
; CHECK: %[[#ptr:]] = OpInBoundsAccessChain %[[#uint_pp]] %[[#global2]] %[[#uint_0]] %[[#uint_0]] %[[#index]] %[[#uint_1]]
43+
%ptr = getelementptr inbounds %S2, ptr addrspace(10) @global2, i64 0, i32 0, i32 0, i64 %index, i32 1
44+
; CHECK: %[[#val:]] = OpLoad %[[#uint]] %[[#ptr]] Aligned 4
45+
%val = load i32, ptr addrspace(10) %ptr
46+
ret i32 %val
47+
}

0 commit comments

Comments
 (0)