Skip to content

Commit bcdefc1

Browse files
agrabezhigcbot
authored andcommitted
Resolve IB_to_private/local builtins from Generic AS
When there is no Local/Private to Generic AS casting it is safe to replace corresponding builtin call by nullptr.
1 parent 1eb005a commit bcdefc1

File tree

2 files changed

+92
-0
lines changed

2 files changed

+92
-0
lines changed

IGC/Compiler/Optimizer/OpenCLPasses/GenericAddressResolution/StaticGASResolution.cpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,34 @@ bool StaticGASResolution::runOnFunction(llvm::Function& F)
7373
changed = true;
7474
}
7575
}
76+
77+
// When there is no Private/Local to Generic AS casting it is safe
78+
// to replace corresponding builtin calls by nullptr.
79+
CallInst* CI = dyn_cast<CallInst>(I);
80+
if (CI)
81+
{
82+
Function* pCalledFunc = CI->getCalledFunction();
83+
if (pCalledFunc && (pCalledFunc->getName() == "__builtin_IB_to_private" ||
84+
pCalledFunc->getName() == "__builtin_IB_to_local"))
85+
{
86+
IGC_ASSERT(IGCLLVM::getNumArgOperands(CI) == 1);
87+
Value* arg = CI->getArgOperand(0);
88+
PointerType* argType = dyn_cast<PointerType>(arg->getType());
89+
IGC_ASSERT(argType != nullptr);
90+
PointerType* dstType = dyn_cast<PointerType>(CI->getType());
91+
IGC_ASSERT(dstType != nullptr);
92+
93+
auto argAS = cast<PointerType>(arg->getType())->getAddressSpace();
94+
if (argAS == ADDRESS_SPACE_GENERIC)
95+
{
96+
Value* newPtr = llvm::ConstantPointerNull::get(dstType);
97+
CI->replaceAllUsesWith(newPtr);
98+
CI->eraseFromParent();
99+
100+
changed = true;
101+
}
102+
}
103+
}
76104
}
77105

78106
return changed;
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
;=========================== begin_copyright_notice ============================
2+
;
3+
; Copyright (C) 2024 Intel Corporation
4+
;
5+
; SPDX-License-Identifier: MIT
6+
;
7+
;============================ end_copyright_notice =============================
8+
;
9+
; REQUIRES: llvm-14-plus
10+
; RUN: igc_opt %s -S -o - -static-gas-resolution | FileCheck %s
11+
;
12+
; Checks that StaticGASResolution removes IB_to_private/IB_to_local builtin calls
13+
; when there is no Private/Local to Generic AS casting
14+
15+
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v16:16:16-v24:32:32-v32:32:32-v48:64:64-v64:64:64-v96:128:128-v128:128:128-v192:256:256-v256:256:256-v512:512:512-v1024:1024:1024-n8:16:32"
16+
17+
define spir_kernel void @test_builtin(i8 addrspace(4)* noundef %p1) {
18+
; CHECK-LABEL: @test_builtin(
19+
; CHECK-NEXT: bb2:
20+
; CHECK-NOT: [[CALL1:%.*]] = call spir_func i8 addrspace(3)* @__builtin_IB_to_local(
21+
; CHECK-NEXT: [[TOBOOL1:%.*]] = icmp eq i8 addrspace(3)* null, null
22+
; CHECK-NEXT: br label [[BB3:%.*]]
23+
; CHECK: bb3:
24+
; CHECK-NOT: [[CALL2:%.*]] = call spir_func i8* @__builtin_IB_to_private(
25+
; CHECK-NEXT: [[TOBOOL2:%.*]] = icmp eq i8* null, null
26+
; CHECK-NEXT: ret void
27+
bb2:
28+
%call.i1 = call spir_func i8 addrspace(3)* @__builtin_IB_to_local(i8 addrspace(4)* noundef %p1)
29+
%tobool1 = icmp eq i8 addrspace(3)* %call.i1, null
30+
br label %bb3
31+
32+
bb3: ; preds = %bb2
33+
%call.i2 = call spir_func i8* @__builtin_IB_to_private(i8 addrspace(4)* noundef %p1)
34+
%tobool2 = icmp eq i8* %call.i2, null
35+
ret void
36+
}
37+
38+
define spir_kernel void @test_builtin1(i8* noundef %p0) {
39+
; CHECK-LABEL: @test_builtin1(
40+
; CHECK-NEXT: bb2:
41+
; CHECK-NEXT: [[P1:%.*]] = addrspacecast i8* [[P0:%.*]] to i8 addrspace(4)*
42+
; CHECK-NEXT: [[CALL_I1:%.*]] = call spir_func i8 addrspace(3)* @__builtin_IB_to_local(i8 addrspace(4)* noundef [[P1]])
43+
; CHECK-NEXT: [[TOBOOL1:%.*]] = icmp eq i8 addrspace(3)* [[CALL_I1]], null
44+
; CHECK-NEXT: br label [[BB3:%.*]]
45+
; CHECK: bb3:
46+
; CHECK-NEXT: [[CALL_I2:%.*]] = call spir_func i8* @__builtin_IB_to_private(i8 addrspace(4)* noundef [[P1]])
47+
; CHECK-NEXT: [[TOBOOL2:%.*]] = icmp eq i8* [[CALL_I2]], null
48+
; CHECK-NEXT: ret void
49+
bb2:
50+
%p1 = addrspacecast i8* %p0 to i8 addrspace(4)*
51+
%call.i1 = call spir_func i8 addrspace(3)* @__builtin_IB_to_local(i8 addrspace(4)* noundef %p1)
52+
%tobool1 = icmp eq i8 addrspace(3)* %call.i1, null
53+
br label %bb3
54+
55+
bb3: ; preds = %bb2
56+
%call.i2 = call spir_func i8* @__builtin_IB_to_private(i8 addrspace(4)* noundef %p1)
57+
%tobool2 = icmp eq i8* %call.i2, null
58+
ret void
59+
}
60+
61+
declare spir_func i8 addrspace(3)* @__builtin_IB_to_local(i8 addrspace(4)*)
62+
63+
declare spir_func i8* @__builtin_IB_to_private(i8 addrspace(4)*)
64+

0 commit comments

Comments
 (0)