Skip to content

Commit 2524207

Browse files
aratajewpszymich
authored andcommitted
Handle byval generic pointer in LegalizeFunctionSignatures
When generic pointer argument with `byval` attribute was legalized to the actual argument passed by value, LegalizeFunctionSignatures wasn't inserting addrspace cast back to generic addrspace when handling uses of this argument. (cherry picked from commit be0cfaa)
1 parent 77bd259 commit 2524207

File tree

2 files changed

+61
-0
lines changed

2 files changed

+61
-0
lines changed

IGC/AdaptorCommon/LegalizeFunctionSignatures.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,10 @@ void LegalizeFunctionSignatures::FixFunctionBody(Module& M)
400400
NewArgIt->removeAttr(llvm::Attribute::ByVal);
401401
Value* newArgPtr = builder.CreateAlloca(NewArgIt->getType());
402402
StoreToStruct(builder, &*NewArgIt, newArgPtr);
403+
// cast back to original addrspace
404+
IGC_ASSERT(OldArgIt->getType()->getPointerAddressSpace() == ADDRESS_SPACE_GENERIC ||
405+
OldArgIt->getType()->getPointerAddressSpace() == ADDRESS_SPACE_PRIVATE);
406+
newArgPtr = builder.CreateAddrSpaceCast(newArgPtr, OldArgIt->getType());
403407
VMap[&*OldArgIt] = newArgPtr;
404408
}
405409
else if (!isLegalSignatureType(M, OldArgIt->getType(), isStackCall))
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2023 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
;
9+
; RUN: igc_opt %s -S -o - --igc-legalize-function-signatures | FileCheck %s
10+
11+
; ------------------------------------------------
12+
; LegalizeFunctionSignatures
13+
; ------------------------------------------------
14+
15+
; The test checks if struct argument passed as generic pointer with byval attribute is correctly legalized
16+
17+
%structtype = type { i32, i32 }
18+
19+
define spir_kernel void @kernel() {
20+
; CHECK-LABEL: @kernel(
21+
; CHECK: [[AL:%.*]] = alloca %structtype
22+
; CHECK: [[TMP0:%.*]] = addrspacecast %structtype* [[AL]] to %structtype addrspace(4)*
23+
; CHECK: [[GEP0:%.*]] = getelementptr inbounds %structtype, %structtype addrspace(4)* [[TMP0]], i32 0, i32 0
24+
; CHECK: [[L0:%.*]] = load i32, i32 addrspace(4)* [[GEP0]]
25+
; CHECK: [[I0:%.*]] = insertvalue %structtype undef, i32 [[L0]], 0
26+
; CHECK: [[GEP1:%.*]] = getelementptr inbounds %structtype, %structtype addrspace(4)* [[TMP0]], i32 0, i32 1
27+
; CHECK: [[L1:%.*]] = load i32, i32 addrspace(4)* [[GEP1]]
28+
; CHECK: [[I1:%.*]] = insertvalue %structtype [[I0]], i32 [[L1]], 1
29+
; CHECK: call i32 @foo(%structtype [[I1]])
30+
; CHECK: ret void
31+
32+
%1 = alloca %structtype
33+
%2 = addrspacecast %structtype* %1 to %structtype addrspace(4)*
34+
call i32 @foo(%structtype addrspace(4)* byval(%structtype) %2)
35+
ret void
36+
}
37+
38+
define spir_func i32 @foo(%structtype addrspace(4)* byval(%structtype) %src) #0 {
39+
; CHECK-LABEL: define spir_func i32 @foo(%structtype %src)
40+
; CHECK: [[AL:%.*]] = alloca %structtype
41+
; CHECK: [[GEP0:%.*]] = getelementptr inbounds %structtype, %structtype* [[AL]], i32 0, i32 0
42+
; CHECK: [[E0:%.*]] = extractvalue %structtype %src, 0
43+
; CHECK: store i32 [[E0]], i32* [[GEP0]]
44+
; CHECK: [[GEP1:%.*]] = getelementptr inbounds %structtype, %structtype* [[AL]], i32 0, i32 1
45+
; CHECK: [[E1:%.*]] = extractvalue %structtype %src, 1
46+
; CHECK: store i32 [[E1]], i32* [[GEP1]]
47+
;
48+
; CHECK: [[ASC:%.*]] = addrspacecast %structtype* [[AL]] to %structtype addrspace(4)*
49+
; CHECK: [[GEP2:%.*]] = getelementptr inbounds %structtype, %structtype addrspace(4)* [[ASC]], i64 0, i32 1
50+
; CHECK: [[LOAD:%.*]] = load i32, i32 addrspace(4)* [[GEP2]]
51+
; CHECK: ret i32 [[LOAD]]
52+
%1 = getelementptr inbounds %structtype, %structtype addrspace(4)* %src, i64 0, i32 1
53+
%2 = load i32, i32 addrspace(4)* %1
54+
ret i32 %2
55+
}
56+
57+
attributes #0 = { "visaStackCall" }

0 commit comments

Comments
 (0)