Skip to content

[SPIR-V] Do not emit spv_ptrcast if GEP result is of expected type #78122

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,9 @@ void SPIRVEmitIntrinsics::insertPtrCastInstr(Instruction *I) {
if (A && A->getAllocatedType() == ExpectedElementType)
return;

if (dyn_cast<GetElementPtrInst>(Pointer))
// Do not emit spv_ptrcast if Pointer is a result of GEP of expected type.
GetElementPtrInst *GEPI = dyn_cast<GetElementPtrInst>(Pointer);
if (GEPI && GEPI->getResultElementType() == ExpectedElementType)
return;

setInsertPointSkippingPhis(*IRB, I);
Expand Down
18 changes: 9 additions & 9 deletions llvm/test/CodeGen/SPIRV/logical-struct-access.ll
Original file line number Diff line number Diff line change
@@ -1,27 +1,27 @@
; RUN: llc -O0 -mtriple=spirv-unknown-unknown %s -o - | FileCheck %s

; CHECK: [[uint:%[0-9]+]] = OpTypeInt 32 0
; CHECK-DAG: [[uint:%[0-9]+]] = OpTypeInt 32 0

%A = type {
i32,
i32
}
; CHECK: [[A:%[0-9]+]] = OpTypeStruct [[uint]] [[uint]]
; CHECK-DAG: [[A:%[0-9]+]] = OpTypeStruct [[uint]] [[uint]]

%B = type {
%A,
i32,
%A
}
; CHECK: [[B:%[0-9]+]] = OpTypeStruct [[A]] [[uint]] [[A]]
; CHECK-DAG: [[B:%[0-9]+]] = OpTypeStruct [[A]] [[uint]] [[A]]

; CHECK: [[uint_0:%[0-9]+]] = OpConstant [[uint]] 0
; CHECK: [[uint_1:%[0-9]+]] = OpConstant [[uint]] 1
; CHECK: [[uint_2:%[0-9]+]] = OpConstant [[uint]] 2
; CHECK-DAG: [[uint_0:%[0-9]+]] = OpConstant [[uint]] 0
; CHECK-DAG: [[uint_1:%[0-9]+]] = OpConstant [[uint]] 1
; CHECK-DAG: [[uint_2:%[0-9]+]] = OpConstant [[uint]] 2

; CHECK: [[ptr_uint:%[0-9]+]] = OpTypePointer Function [[uint]]
; CHECK: [[ptr_A:%[0-9]+]] = OpTypePointer Function [[A]]
; CHECK: [[ptr_B:%[0-9]+]] = OpTypePointer Function [[B]]
; CHECK-DAG: [[ptr_uint:%[0-9]+]] = OpTypePointer Function [[uint]]
; CHECK-DAG: [[ptr_A:%[0-9]+]] = OpTypePointer Function [[A]]
; CHECK-DAG: [[ptr_B:%[0-9]+]] = OpTypePointer Function [[B]]

define void @main() #1 {
entry:
Expand Down
33 changes: 33 additions & 0 deletions llvm/test/CodeGen/SPIRV/pointers/getelementptr-bitcast-load.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
; RUN: llc -O0 -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}

; CHECK-DAG: %[[#INT8:]] = OpTypeInt 8 0
; CHECK-DAG: %[[#VEC3:]] = OpTypeVector %[[#INT8]] 3
; CHECK-DAG: %[[#VEC4:]] = OpTypeVector %[[#INT8]] 4
; CHECK-DAG: %[[#PTR_VEC3:]] = OpTypePointer CrossWorkgroup %[[#VEC3]]
; CHECK-DAG: %[[#PTR_VEC4:]] = OpTypePointer CrossWorkgroup %[[#VEC4]]

; CHECK: %[[#AC1:]] = OpInBoundsPtrAccessChain %[[#PTR_VEC3]] %[[#]] %[[#]]
; CHECK: %[[#BC1:]] = OpBitcast %[[#PTR_VEC4]] %[[#AC1]]
; CHECK: %[[#LD1:]] = OpLoad %[[#VEC4]] %[[#BC1]] Aligned 4
; CHECK: OpReturn

define spir_kernel void @foo(ptr addrspace(1) %a, i64 %b) {
%index = getelementptr inbounds <3 x i8>, ptr addrspace(1) %a, i64 %b
%loadv = load <4 x i8>, ptr addrspace(1) %index, align 4
ret void
}

; CHECK: %[[#AC2:]] = OpInBoundsPtrAccessChain %[[#PTR_VEC3]] %[[#]] %[[#]]
; CHECK: %[[#BC2:]] = OpBitcast %[[#PTR_VEC4]] %[[#AC2]]
; CHECK: %[[#LD2:]] = OpLoad %[[#VEC4]] %[[#BC2]] Aligned 4
; CHECK: OpReturn

define spir_kernel void @bar(ptr addrspace(1) %a, i64 %b) {
%index = getelementptr inbounds <3 x i8>, ptr addrspace(1) %a, i64 %b
; This redundant bitcast is left here itentionally to simulate the conversion
; from older LLVM IR with typed pointers.
%cast = bitcast ptr addrspace(1) %index to ptr addrspace(1)
%loadv = load <4 x i8>, ptr addrspace(1) %cast, align 4
ret void
}