Skip to content

Commit 7c0f43c

Browse files
vmaksimovladimirlaz
authored andcommitted
Fix translation of llvm.ptr.annotation (intel#678)
This patch follows from 71039dd3c5dbfafab78f6b86a5bb06ba2af93c89 * Replaced the destination operand with the result of its intrinsic call for store inst. * Replaced the read-from operand with the result of its intrinsic call for load inst.
1 parent c0fe5ad commit 7c0f43c

File tree

3 files changed

+59
-22
lines changed

3 files changed

+59
-22
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 23 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,6 +1425,20 @@ Value *SPIRVToLLVM::oclTransConstantPipeStorage(
14251425
GlobalValue::NotThreadLocal, SPIRAS_Global);
14261426
}
14271427

1428+
// A pointer annotation may have been generated for the operand. If the operand
1429+
// is used further in IR, it should be replaced with the intrinsic call result.
1430+
// Otherwise, the generated pointer annotation call is left unused.
1431+
static void replaceOperandWithAnnotationIntrinsicCallResult(Value *&V) {
1432+
if (Use *SingleUse = V->getSingleUndroppableUse()) {
1433+
if (auto *II = dyn_cast<IntrinsicInst>(SingleUse->getUser())) {
1434+
if (II->getIntrinsicID() == Intrinsic::ptr_annotation &&
1435+
II->getType() == V->getType())
1436+
// Overwrite the future operand with the intrinsic call result.
1437+
V = II;
1438+
}
1439+
}
1440+
}
1441+
14281442
/// For instructions, this function assumes they are created in order
14291443
/// and appended to the given basic block. An instruction may use a
14301444
/// instruction from another BB which has not been translated. Such
@@ -1764,15 +1778,11 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
17641778
StoreInst *SI = nullptr;
17651779
auto *Src = transValue(BS->getSrc(), F, BB);
17661780
auto *Dst = transValue(BS->getDst(), F, BB);
1767-
// A pointer annotation may have been generated for the source variable.
1768-
if (Use *SingleUse = Src->getSingleUndroppableUse()) {
1769-
if (auto *II = dyn_cast<IntrinsicInst>(SingleUse->getUser())) {
1770-
if (II->getIntrinsicID() == Intrinsic::ptr_annotation &&
1771-
II->getType() == Src->getType())
1772-
// Overwrite the future store operand with the intrinsic call result.
1773-
Src = II;
1774-
}
1775-
}
1781+
// A ptr.annotation may have been generated for the source variable.
1782+
replaceOperandWithAnnotationIntrinsicCallResult(Src);
1783+
// A ptr.annotation may have been generated for the destination variable.
1784+
replaceOperandWithAnnotationIntrinsicCallResult(Dst);
1785+
17761786
bool isVolatile = BS->SPIRVMemoryAccess::isVolatile();
17771787
uint64_t AlignValue = BS->SPIRVMemoryAccess::getAlignment();
17781788
if (0 == AlignValue)
@@ -1786,7 +1796,10 @@ Value *SPIRVToLLVM::transValueWithoutDecoration(SPIRVValue *BV, Function *F,
17861796

17871797
case OpLoad: {
17881798
SPIRVLoad *BL = static_cast<SPIRVLoad *>(BV);
1789-
auto V = transValue(BL->getSrc(), F, BB);
1799+
auto *V = transValue(BL->getSrc(), F, BB);
1800+
// A ptr.annotation may have been generated for the source variable.
1801+
replaceOperandWithAnnotationIntrinsicCallResult(V);
1802+
17901803
Type *Ty = V->getType()->getPointerElementType();
17911804
LoadInst *LI = nullptr;
17921805
uint64_t AlignValue = BL->SPIRVMemoryAccess::getAlignment();

llvm-spirv/test/transcoding/IntelFPGAMemoryAccesses.ll

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222
; y = __builtin_intel_fpga_mem(&C->Field2, 0, 127);
2323
; z = __builtin_intel_fpga_mem(C, BURST_COAL | CACHE_SIZE_FLAG | DONT_STATICALLY_COAL | PREFETCH, 127);
2424
; t = __builtin_intel_fpga_mem((double *) A, BURST_COAL | CACHE_SIZE_FLAG, 0);
25+
; *__builtin_intel_fpga_mem(A, BURST_COAL | CACHE_SIZE_FLAG, 0) = 5;
26+
; int s = *__builtin_intel_fpga_mem(B, DONT_STATICALLY_COAL | PREFETCH, 0);
2527
; }
2628
;
2729
; template <typename name, typename Func>
@@ -81,6 +83,8 @@ target triple = "spir64-unknown-unknown-sycldevice"
8183
@.str.6 = private unnamed_addr constant [28 x i8] c"{params:15}{cache-size:127}\00", section "llvm.metadata"
8284
; TODO: Investigate why the same global annotation string shows up twice in backwards translation.
8385
; CHECK-LLVM: [[PARAM_3_CACHE_0_DOUBLE:@[a-z0-9_.]+]] = {{.*}}{params:3}{cache-size:0}
86+
; CHECK-LLVM: [[PARAM_3_CACHE_0_DOUBLE2:@[a-z0-9_.]+]] = {{.*}}{params:3}{cache-size:0}
87+
; CHECK-LLVM: [[PARAM_12_CACHE_0_DOUBLE:@[a-z0-9_.]+]] = {{.*}}{params:12}
8488

8589
; Function Attrs: norecurse nounwind
8690
define spir_kernel void @_ZTSZ4mainE11fake_kernel() #0 !kernel_arg_addr_space !4 !kernel_arg_access_qual !4 !kernel_arg_type !4 !kernel_arg_base_type !4 !kernel_arg_type_qual !4 {
@@ -142,10 +146,12 @@ entry:
142146
; CHECK-LLVM: %[[INT_VAR:[[:alnum:].]+]] = alloca i32 addrspace(4)*, align 8
143147
; CHECK-LLVM: %[[STRUCT_VAR:[[:alnum:].]+]] = alloca %struct{{.*}}State addrspace(4)*, align 8
144148
; CHECK-LLVM: %[[DOUBLE_VAR:[[:alnum:].]+]] = alloca double addrspace(4)*, align 8
149+
; CHECK-LLVM: %[[INT_VAR_1:[[:alnum:].]+]] = alloca i32, align 4
145150
%x = alloca float addrspace(4)*, align 8
146151
%y = alloca i32 addrspace(4)*, align 8
147152
%z = alloca %struct._ZTS5State.State addrspace(4)*, align 8
148153
%t = alloca double addrspace(4)*, align 8
154+
%s = alloca i32, align 4
149155
store float addrspace(4)* %A, float addrspace(4)** %A.addr, align 8, !tbaa !5
150156
store i32 addrspace(4)* %B, i32 addrspace(4)** %B.addr, align 8, !tbaa !5
151157
store %struct._ZTS5State.State addrspace(4)* %C, %struct._ZTS5State.State addrspace(4)** %C.addr, align 8, !tbaa !5
@@ -206,14 +212,32 @@ entry:
206212
%17 = bitcast float addrspace(4)* %16 to double addrspace(4)*
207213
%18 = call double addrspace(4)* @llvm.ptr.annotation.p4f64(double addrspace(4)* %17, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i32 0, i32 0), i32 0) #6
208214
store double addrspace(4)* %18, double addrspace(4)** %t, align 8, !tbaa !5
209-
%19 = bitcast double addrspace(4)** %t to i8*
210-
call void @llvm.lifetime.end.p0i8(i64 8, i8* %19) #5
211-
%20 = bitcast %struct._ZTS5State.State addrspace(4)** %z to i8*
212-
call void @llvm.lifetime.end.p0i8(i64 8, i8* %20) #5
213-
%21 = bitcast i32 addrspace(4)** %y to i8*
214-
call void @llvm.lifetime.end.p0i8(i64 8, i8* %21) #5
215-
%22 = bitcast float addrspace(4)** %x to i8*
216-
call void @llvm.lifetime.end.p0i8(i64 8, i8* %22) #5
215+
; CHECK-LLVM: %[[FLOAT_FUNC_PARAM_LOAD:[[:alnum:].]+]] = load float addrspace(4)*, float addrspace(4)** %[[FLOAT_FUNC_PARAM]]
216+
; CHECK-LLVM: %[[INTRINSIC_CALL:[[:alnum:].]+]] = call float addrspace(4)* @llvm.ptr.annotation.p4f32(float addrspace(4)* %[[FLOAT_FUNC_PARAM_LOAD]], i8* getelementptr inbounds ({{.*}} [[PARAM_3_CACHE_0_DOUBLE2]]
217+
; CHECK-LLVM: store float 5.000000e+00, float addrspace(4)* %[[INTRINSIC_CALL]]
218+
%19 = load float addrspace(4)*, float addrspace(4)** %A.addr, align 8, !tbaa !5
219+
%20 = call float addrspace(4)* @llvm.ptr.annotation.p4f32(float addrspace(4)* %19, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str, i32 0, i32 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i32 0, i32 0), i32 0) #6
220+
store float 5.000000e+00, float addrspace(4)* %20, align 4, !tbaa !5
221+
%21 = bitcast i32* %s to i8*
222+
call void @llvm.lifetime.start.p0i8(i64 4, i8* %21) #5
223+
; CHECK-LLVM: %[[INT1_FUNC_PARAM_LOAD:[[:alnum:].]+]] = load i32 addrspace(4)*, i32 addrspace(4)** %[[INT_FUNC_PARAM]]
224+
; CHECK-LLVM: %[[INTRINSIC_CALL:[[:alnum:].]+]] = call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* %[[INT1_FUNC_PARAM_LOAD]], i8* getelementptr inbounds ({{.*}} [[PARAM_12_CACHE_0_DOUBLE]]
225+
; CHECK-LLVM: %[[INTRINSIC_RESULT_LOAD:[[:alnum:].]+]] = load i32, i32 addrspace(4)* %[[INTRINSIC_CALL]]
226+
; CHECK-LLVM: store i32 %[[INTRINSIC_RESULT_LOAD]], i32* %[[INT_VAR_1]]
227+
%22 = load i32 addrspace(4)*, i32 addrspace(4)** %B.addr, align 8, !tbaa !5
228+
%23 = call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* %22, i8* getelementptr inbounds ([26 x i8], [26 x i8]* @.str.2, i32 0, i32 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i32 0, i32 0), i32 0) #6
229+
%24 = load i32, i32 addrspace(4)* %23, align 4, !tbaa !5
230+
store i32 %24, i32* %s, align 4, !tbaa !5
231+
%25 = bitcast i32* %s to i8*
232+
call void @llvm.lifetime.end.p0i8(i64 4, i8* %25) #5
233+
%26 = bitcast double addrspace(4)** %t to i8*
234+
call void @llvm.lifetime.end.p0i8(i64 8, i8* %26) #5
235+
%27 = bitcast %struct._ZTS5State.State addrspace(4)** %z to i8*
236+
call void @llvm.lifetime.end.p0i8(i64 8, i8* %27) #5
237+
%28 = bitcast i32 addrspace(4)** %y to i8*
238+
call void @llvm.lifetime.end.p0i8(i64 8, i8* %28) #5
239+
%29 = bitcast float addrspace(4)** %x to i8*
240+
call void @llvm.lifetime.end.p0i8(i64 8, i8* %29) #5
217241
ret void
218242
}
219243

llvm-spirv/test/transcoding/intel_fpga_lsu_optimized.ll

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,29 +91,29 @@ entry:
9191
; CHECK-LLVM: [[PTR_i:[%0-9a-z.]+]] = getelementptr inbounds i32, i32 addrspace(1)* {{[%0-9a-z._]+}}, i64 {{[%0-9a-z.]+}}
9292
; CHECK-LLVM: [[PTR_i27_AS_CAST:[%0-9a-z.]+]] = addrspacecast i32 addrspace(1)* [[PTR_i27]] to i32 addrspace(4)*
9393
; CHECK-LLVM: [[PTR_ANNOT_CALL:[%0-9a-z.]+]] = call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* [[PTR_i27_AS_CAST]], i8* getelementptr inbounds ({{.*}} [[PTR_i27_ANNOT_STR]]
94-
; TODO: add check that load is called for result of ptr.annotation when corresponding bug is fixed
94+
; CHECK-LLVM: [[PTR_RESULT_LOAD:[%0-9a-z.]+]] = load i32, i32 addrspace(4)* [[PTR_ANNOT_CALL]]
9595
%add.ptr.i15.i = getelementptr inbounds i32, i32 addrspace(1)* %add.ptr.i27, i64 1
9696
%7 = addrspacecast i32 addrspace(1)* %add.ptr.i15.i to i32 addrspace(4)*
9797
%8 = tail call dereferenceable(4) i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* %7, i8* getelementptr inbounds ([28 x i8], [28 x i8]* @.str.2, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 0) #2
9898
%9 = load i32, i32 addrspace(4)* %8, align 4, !tbaa !9
9999
; CHECK-LLVM: [[PTR_i15_i:[%0-9a-z.]+]] = getelementptr inbounds i32, i32 addrspace(1)* {{[%0-9a-z._]+}}, i64 {{[%0-9a-z.]+}}
100100
; CHECK-LLVM: [[PTR_i15_i_AS_CAST:[%0-9a-z.]+]] = addrspacecast i32 addrspace(1)* [[PTR_i15_i]] to i32 addrspace(4)*
101101
; CHECK-LLVM: [[PTR_ANNOT_CALL:[%0-9a-z.]+]] = call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* [[PTR_i15_i_AS_CAST]], i8* getelementptr inbounds ({{.*}} [[PTR_i15_i_ANNOT_STR]]
102-
; TODO: add check that load is called for result of ptr.annotation when corresponding bug is fixed
102+
; CHECK-LLVM: [[PTR_RESULT_LOAD_1:[%0-9a-z.]+]] = load i32, i32 addrspace(4)* [[PTR_ANNOT_CALL]]
103103
%10 = addrspacecast i32 addrspace(1)* %add.ptr.i to i32 addrspace(4)*
104104
%11 = tail call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* %10, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.3, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 0) #2
105105
store i32 %6, i32 addrspace(4)* %11, align 4, !tbaa !9
106106
; CHECK-LLVM: [[PTR_i_AS_CAST:[%0-9a-z.]+]] = addrspacecast i32 addrspace(1)* [[PTR_i]] to i32 addrspace(4)*
107107
; CHECK-LLVM: [[PTR_ANNOT_CALL:[%0-9a-z.]+]] = call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* [[PTR_i_AS_CAST]], i8* getelementptr inbounds ({{.*}} [[PTR_i_ANNOT_STR]]
108-
; TODO: add check that store is called for result of ptr.annotation when corresponding bug is fixed
108+
; CHECK-LLVM: store i32 [[PTR_RESULT_LOAD]], i32 addrspace(4)* [[PTR_ANNOT_CALL]]
109109
%add.ptr.i.i = getelementptr inbounds i32, i32 addrspace(1)* %add.ptr.i, i64 1
110110
%12 = addrspacecast i32 addrspace(1)* %add.ptr.i.i to i32 addrspace(4)*
111111
%13 = tail call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* %12, i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.4, i64 0, i64 0), i8* getelementptr inbounds ([14 x i8], [14 x i8]* @.str.1, i64 0, i64 0), i32 0) #2
112112
store i32 %9, i32 addrspace(4)* %13, align 4, !tbaa !9
113113
; CHECK-LLVM: [[PTR_i_i:[%0-9a-z.]+]] = getelementptr inbounds i32, i32 addrspace(1)* {{[%0-9a-z._]+}}, i64 {{[%0-9a-z.]+}}
114114
; CHECK-LLVM: [[PTR_i_i_AS_CAST:[%0-9a-z.]+]] = addrspacecast i32 addrspace(1)* [[PTR_i_i]] to i32 addrspace(4)*
115115
; CHECK-LLVM: [[PTR_ANNOT_CALL:[%0-9a-z.]+]] = call i32 addrspace(4)* @llvm.ptr.annotation.p4i32(i32 addrspace(4)* [[PTR_i_i_AS_CAST]], i8* getelementptr inbounds ({{.*}} [[PTR_i_i_ANNOT_STR]]
116-
; TODO: add check that store is called for result of ptr.annotation when corresponding bug is fixed
116+
; CHECK-LLVM: store i32 [[PTR_RESULT_LOAD_1]], i32 addrspace(4)* [[PTR_ANNOT_CALL]]
117117
ret void
118118
}
119119

0 commit comments

Comments
 (0)