Skip to content

Commit ea6935b

Browse files
authored
Fix incorrect translation of FPGA decoration on arrays (#983)
* Fix incorrect translation of FPGA decoration on arrays var.annotation shall be emitted, but instead the translator was generating ptr.annotation. It was happening due to incorrect assumption, that for attributed static memory variable decoration shall be applied to alloca instruction, whilst in the reality the alloca instruction might be hidden by several cast instructions. Signed-off-by: Dmitry Sidorov <[email protected]>
1 parent 2c61561 commit ea6935b

File tree

2 files changed

+27
-1
lines changed

2 files changed

+27
-1
lines changed

lib/SPIRV/SPIRVReader.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3700,8 +3700,16 @@ void SPIRVToLLVM::transIntelFPGADecorations(SPIRVValue *BV, Value *V) {
37003700
Value *BaseInst =
37013701
AL ? Builder.CreateBitCast(V, Int8PtrTyPrivate, V->getName()) : Inst;
37023702

3703+
// Try to find alloca instruction for statically allocated variables.
3704+
// Alloca might be hidden by a couple of casts.
3705+
bool isStaticMemoryAttribute = AL ? true : false;
3706+
while (!isStaticMemoryAttribute && Inst &&
3707+
(isa<BitCastInst>(Inst) || isa<AddrSpaceCastInst>(Inst))) {
3708+
Inst = dyn_cast<Instruction>(Inst->getOperand(0));
3709+
isStaticMemoryAttribute = (Inst && isa<AllocaInst>(Inst));
3710+
}
37033711
auto AnnotationFn =
3704-
AL // Static memory attributes if true
3712+
isStaticMemoryAttribute
37053713
? llvm::Intrinsic::getDeclaration(M, Intrinsic::var_annotation)
37063714
: llvm::Intrinsic::getDeclaration(M, Intrinsic::ptr_annotation,
37073715
AllocatedTy);

test/transcoding/IntelFPGAMemoryAttributes.ll

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,9 @@
168168
; } s;
169169
; s.field = 0;
170170
; }
171+
; void memory_attribute_on_array() {
172+
; [[intelfpga::register]] int register_var[32];
173+
; }
171174
;
172175
; template <typename name, typename Func>
173176
; __attribute__((sycl_kernel)) void kernel_single_task(Func kernelFunc) {
@@ -194,6 +197,7 @@
194197
; templ_bank_bits_attr<4, 5>();
195198
; force_pow2_depth_attr();
196199
; templ_force_pow2_depth_attr<1>();
200+
; memory_attribute_on_array();
197201
; });
198202
; return 0;
199203
; }
@@ -304,6 +308,7 @@ target triple = "spir"
304308
; CHECK-LLVM: [[STR_FP2_SCT:@[0-9_.]+]] = {{.*}}{memory:DEFAULT}{force_pow2_depth:1}
305309
; CHECK-LLVM: [[STR_FP2_TE1:@[0-9_.]+]] = {{.*}}{memory:DEFAULT}{force_pow2_depth:1}
306310
; CHECK-LLVM: [[STR_FP2_TE2:@[0-9_.]+]] = {{.*}}{memory:DEFAULT}{force_pow2_depth:1}
311+
; CHECK-LLVM: [[STR_REG_ARR:@[0-9_.]+]] = {{.*}}{register:1}
307312
@.str = private unnamed_addr constant [42 x i8] c"{memory:DEFAULT}{sizeinfo:4}{numbanks:16}\00", section "llvm.metadata"
308313
@.str.1 = private unnamed_addr constant [25 x i8] c"intel-fpga-local-var.cpp\00", section "llvm.metadata"
309314
@.str.2 = private unnamed_addr constant [41 x i8] c"{memory:DEFAULT}{sizeinfo:4}{numbanks:2}\00", section "llvm.metadata"
@@ -371,6 +376,7 @@ entry:
371376
call spir_func void @_Z20templ_bank_bits_attrILi4ELi5EEvv()
372377
call spir_func void @_Z21force_pow2_depth_attrv()
373378
call spir_func void @_Z27templ_force_pow2_depth_attrILi1EEvv()
379+
call spir_func void @_Z25memory_attribute_on_arrayv()
374380
ret void
375381
}
376382

@@ -814,6 +820,18 @@ entry:
814820
ret void
815821
}
816822

823+
; Function Attrs: convergent noinline norecurse nounwind optnone mustprogress
824+
define dso_local spir_func void @_Z25memory_attribute_on_arrayv() #2 {
825+
entry:
826+
%register_var = alloca [32 x i32], align 4
827+
%register_var.ascast = addrspacecast [32 x i32]* %register_var to [32 x i32] addrspace(4)*
828+
%register_var.ascast1 = bitcast [32 x i32] addrspace(4)* %register_var.ascast to i8 addrspace(4)*
829+
%register_var.ascast2 = addrspacecast i8 addrspace(4)* %register_var.ascast1 to i8*
830+
; CHECK-LLVM: call void @llvm.var.annotation(i8* %{{[a-zA-Z0-9_.]+}}, i8* getelementptr inbounds ([{{[0-9]+}} x i8], [{{[0-9]+}} x i8]* [[STR_REG_ARR]], i32 0, i32 0), i8* undef, i32 undef, i8* undef)
831+
call void @llvm.var.annotation(i8* %register_var.ascast2, i8* getelementptr inbounds ([13 x i8], [13 x i8]* @.str.4, i32 0, i32 0), i8* getelementptr inbounds ([25 x i8], [25 x i8]* @.str.1, i32 0, i32 0), i32 2, i8* null)
832+
ret void
833+
}
834+
817835
attributes #0 = { norecurse nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "sycl-module-id"="intel-fpga-local-var.cpp" "uniform-work-group-size"="true" "unsafe-fp-math"="false" "use-soft-float"="false" }
818836
attributes #1 = { argmemonly nounwind willreturn }
819837
attributes #2 = { inlinehint norecurse nounwind "correctly-rounded-divide-sqrt-fp-math"="false" "disable-tail-calls"="false" "frame-pointer"="none" "less-precise-fpmad"="false" "min-legal-vector-width"="0" "no-infs-fp-math"="false" "no-jump-tables"="false" "no-nans-fp-math"="false" "no-signed-zeros-fp-math"="false" "no-trapping-math"="false" "stack-protector-buffer-size"="8" "unsafe-fp-math"="false" "use-soft-float"="false" }

0 commit comments

Comments
 (0)