Skip to content

Commit f5f4cc5

Browse files
committed
Translate function attributes (incl. parameter attrs) for entry point kernels
Original commit: KhronosGroup/SPIRV-LLVM-Translator@aded5af
1 parent 86aa271 commit f5f4cc5

File tree

7 files changed

+63
-50
lines changed

7 files changed

+63
-50
lines changed

llvm-spirv/lib/SPIRV/SPIRVReader.cpp

Lines changed: 55 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2970,12 +2970,65 @@ bool SPIRVToLLVM::foreachFuncCtlMask(SourceTy Source, FuncTy Func) {
29702970
return true;
29712971
}
29722972

2973+
void SPIRVToLLVM::transFunctionAttrs(SPIRVFunction *BF, Function *F) {
2974+
if (BF->hasDecorate(DecorationReferencedIndirectlyINTEL))
2975+
F->addFnAttr("referenced-indirectly");
2976+
if (isFuncNoUnwind())
2977+
F->addFnAttr(Attribute::NoUnwind);
2978+
foreachFuncCtlMask(BF, [&](Attribute::AttrKind Attr) { F->addFnAttr(Attr); });
2979+
2980+
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
2981+
++I) {
2982+
auto *BA = BF->getArgument(I->getArgNo());
2983+
mapValue(BA, &(*I));
2984+
setName(&(*I), BA);
2985+
AttributeMask IllegalAttrs = AttributeFuncs::typeIncompatible(I->getType());
2986+
BA->foreachAttr([&](SPIRVFuncParamAttrKind Kind) {
2987+
// Skip this function parameter attribute as it will translated among
2988+
// OpenCL metadata
2989+
if (Kind == FunctionParameterAttributeRuntimeAlignedINTEL)
2990+
return;
2991+
Attribute::AttrKind LLVMKind = SPIRSPIRVFuncParamAttrMap::rmap(Kind);
2992+
if (IllegalAttrs.contains(LLVMKind))
2993+
return;
2994+
Type *AttrTy = nullptr;
2995+
switch (LLVMKind) {
2996+
case Attribute::AttrKind::ByVal:
2997+
case Attribute::AttrKind::StructRet:
2998+
AttrTy = transType(BA->getType()->getPointerElementType());
2999+
break;
3000+
default:
3001+
break; // do nothing
3002+
}
3003+
// Make sure to use a correct constructor for a typed/typeless attribute
3004+
auto A = AttrTy ? Attribute::get(*Context, LLVMKind, AttrTy)
3005+
: Attribute::get(*Context, LLVMKind);
3006+
I->addAttr(A);
3007+
});
3008+
3009+
AttrBuilder Builder(*Context);
3010+
SPIRVWord MaxOffset = 0;
3011+
if (BA->hasDecorate(DecorationMaxByteOffset, 0, &MaxOffset))
3012+
Builder.addDereferenceableAttr(MaxOffset);
3013+
SPIRVWord AlignmentBytes = 0;
3014+
if (BA->hasDecorate(DecorationAlignment, 0, &AlignmentBytes))
3015+
Builder.addAlignmentAttr(AlignmentBytes);
3016+
I->addAttrs(Builder);
3017+
}
3018+
BF->foreachReturnValueAttr([&](SPIRVFuncParamAttrKind Kind) {
3019+
if (Kind == FunctionParameterAttributeNoWrite)
3020+
return;
3021+
F->addRetAttr(SPIRSPIRVFuncParamAttrMap::rmap(Kind));
3022+
});
3023+
}
3024+
29733025
Function *SPIRVToLLVM::transFunction(SPIRVFunction *BF) {
29743026
auto Loc = FuncMap.find(BF);
29753027
if (Loc != FuncMap.end())
29763028
return Loc->second;
29773029

29783030
auto IsKernel = isKernel(BF);
3031+
29793032
if (IsKernel) {
29803033
// search for a previous function with the same name
29813034
// upgrade it to a kernel and drop this if it's found
@@ -2988,6 +3041,7 @@ Function *SPIRVToLLVM::transFunction(SPIRVFunction *BF) {
29883041
F->setDSOLocal(false);
29893042
F = cast<Function>(mapValue(BF, F));
29903043
mapFunction(BF, F);
3044+
transFunctionAttrs(BF, F);
29913045
return F;
29923046
}
29933047
}
@@ -3036,49 +3090,7 @@ Function *SPIRVToLLVM::transFunction(SPIRVFunction *BF) {
30363090

30373091
F->setCallingConv(IsKernel ? CallingConv::SPIR_KERNEL
30383092
: CallingConv::SPIR_FUNC);
3039-
if (BF->hasDecorate(DecorationReferencedIndirectlyINTEL))
3040-
F->addFnAttr("referenced-indirectly");
3041-
if (isFuncNoUnwind())
3042-
F->addFnAttr(Attribute::NoUnwind);
3043-
foreachFuncCtlMask(BF, [&](Attribute::AttrKind Attr) { F->addFnAttr(Attr); });
3044-
3045-
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
3046-
++I) {
3047-
auto BA = BF->getArgument(I->getArgNo());
3048-
mapValue(BA, &(*I));
3049-
setName(&(*I), BA);
3050-
BA->foreachAttr([&](SPIRVFuncParamAttrKind Kind) {
3051-
Attribute::AttrKind LLVMKind = SPIRSPIRVFuncParamAttrMap::rmap(Kind);
3052-
Type *AttrTy = nullptr;
3053-
switch (LLVMKind) {
3054-
case Attribute::AttrKind::ByVal:
3055-
case Attribute::AttrKind::StructRet:
3056-
AttrTy = transType(BA->getType()->getPointerElementType());
3057-
break;
3058-
default:
3059-
break; // do nothing
3060-
}
3061-
// Make sure to use a correct constructor for a typed/typeless attribute
3062-
auto A = AttrTy ? Attribute::get(*Context, LLVMKind, AttrTy)
3063-
: Attribute::get(*Context, LLVMKind);
3064-
I->addAttr(A);
3065-
});
3066-
3067-
AttrBuilder Builder(*Context);
3068-
SPIRVWord MaxOffset = 0;
3069-
if (BA->hasDecorate(DecorationMaxByteOffset, 0, &MaxOffset))
3070-
Builder.addDereferenceableAttr(MaxOffset);
3071-
SPIRVWord AlignmentBytes = 0;
3072-
if (BA->hasDecorate(DecorationAlignment, 0, &AlignmentBytes))
3073-
Builder.addAlignmentAttr(AlignmentBytes);
3074-
I->addAttrs(Builder);
3075-
}
3076-
BF->foreachReturnValueAttr([&](SPIRVFuncParamAttrKind Kind) {
3077-
if (Kind == FunctionParameterAttributeNoWrite)
3078-
return;
3079-
F->addRetAttr(SPIRSPIRVFuncParamAttrMap::rmap(Kind));
3080-
});
3081-
3093+
transFunctionAttrs(BF, F);
30823094
// Creating all basic blocks before creating instructions.
30833095
for (size_t I = 0, E = BF->getNumBasicBlock(); I != E; ++I) {
30843096
transValue(BF->getBasicBlock(I), F, nullptr);

llvm-spirv/lib/SPIRV/SPIRVReader.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ class SPIRVToLLVM : private BuiltinCallHelper {
104104
std::vector<Value *> transValue(const std::vector<SPIRVValue *> &,
105105
Function *F, BasicBlock *);
106106
Function *transFunction(SPIRVFunction *F);
107+
void transFunctionAttrs(SPIRVFunction *BF, Function *F);
107108
Value *transBlockInvoke(SPIRVValue *Invoke, BasicBlock *BB);
108109
Instruction *transWGSizeQueryBI(SPIRVInstruction *BI, BasicBlock *BB);
109110
Instruction *transSGSizeQueryBI(SPIRVInstruction *BI, BasicBlock *BB);

llvm-spirv/test/extensions/INTEL/SPV_INTEL_fpga_argument_interfaces/sycl-kernel-arg-annotation.ll

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ entry:
5353
; CHECK-SPIRV: Capability FPGAArgumentInterfacesINTEL
5454
; CHECK-SPIRV: Extension "SPV_INTEL_fpga_argument_interfaces"
5555
; CHECK-SPIRV: Extension "SPV_INTEL_fpga_buffer_location"
56+
; CHECK-SPIRV-DAG: Name [[IDS:[0-9]+]] "_arg_p"
5657
; CHECK-SPIRV-DAG: Name [[ID:[0-9]+]] "_arg_p"
5758
; CHECK-SPIRV: Decorate [[ID]] Alignment 4
5859
; CHECK-SPIRV: Decorate [[ID]] MMHostInterfaceAddressWidthINTEL 32

llvm-spirv/test/extensions/INTEL/SPV_INTEL_function_pointers/alias.ll

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
; XFAIL: *
2-
31
; RUN: llvm-as %s -o %t.bc
42
; RUN: llvm-spirv -spirv-ext=+SPV_INTEL_function_pointers -spirv-text %t.bc -o - | FileCheck %s --check-prefix=CHECK-SPIRV
53
; RUN: llvm-spirv -spirv-ext=+SPV_INTEL_function_pointers %t.bc -o %t.spv
@@ -34,7 +32,7 @@ target triple = "spir64-unknown-unknown"
3432

3533
; CHECK-LLVM: define spir_func i32 @foo(i32 %x)
3634

37-
; CHECK-LLVM: define spir_func void @bar(ptr %y)
35+
; CHECK-LLVM: define spir_kernel void @bar(ptr %y)
3836
; CHECK-LLVM: [[PTRTOINT:%.*]] = ptrtoint ptr @foo to i64
3937
; CHECK-LLVM: store i64 [[PTRTOINT]], ptr %y, align 8
4038

llvm-spirv/test/image.ll

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
; XFAIL: *
2-
31
; RUN: llvm-as %s -o %t.bc
42
; RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV
53
; RUN: llvm-spirv %t.bc -o %t.spv

llvm-spirv/test/transcoding/kernel_arg_type_qual.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16
1414
target triple = "spir64-unknown-unknown."
1515

1616
; CHECK-SPIRV: String [[#]] "kernel_arg_type_qual.test.volatile,const,,"
17-
; CHECK-SPIRV: Name [[ARG:[0-9]+]] "g"
17+
; CHECK-SPIRV: Name [[ARG:1[0-9]+]] "g"
1818
; CHECK-SPIRV: Decorate [[ARG]] Volatile
1919
; CHECK-SPIRV-NEGATIVE-NOT: String [[#]] "kernel_arg_type_qual.test.volatile,const,,"
2020

llvm-spirv/test/transcoding/registerallocmode.ll

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,11 @@
1919
; CHECK-LLVM: @[[FLAG0:[0-9]+]] = private unnamed_addr constant [20 x i8] c"num-thread-per-eu 4\00", section "llvm.metadata"
2020
; CHECK-LLVM: @[[FLAG1:[0-9]+]] = private unnamed_addr constant [20 x i8] c"num-thread-per-eu 8\00", section "llvm.metadata"
2121
; CHECK-LLVM: @[[FLAG2:[0-9]+]] = private unnamed_addr constant [20 x i8] c"num-thread-per-eu 0\00", section "llvm.metadata"
22+
; CHECK-LLVM: @[[FLAG3:[0-9]+]] = private unnamed_addr constant [20 x i8] c"num-thread-per-eu 4\00", section "llvm.metadata"
23+
; CHECK-LLVM: @[[FLAG4:[0-9]+]] = private unnamed_addr constant [20 x i8] c"num-thread-per-eu 8\00", section "llvm.metadata"
24+
; CHECK-LLVM: @[[FLAG5:[0-9]+]] = private unnamed_addr constant [20 x i8] c"num-thread-per-eu 0\00", section "llvm.metadata"
2225

23-
; CHECK-LLVM: @llvm.global.annotations = appending global [3 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @main_l3, ptr @[[FLAG0]], ptr undef, i32 undef, ptr undef }, { ptr, ptr, ptr, i32, ptr } { ptr @main_l6, ptr @[[FLAG1]], ptr undef, i32 undef, ptr undef }, { ptr, ptr, ptr, i32, ptr } { ptr @main_l9, ptr @[[FLAG2]], ptr undef, i32 undef, ptr undef }], section "llvm.metadata"
26+
; CHECK-LLVM: @llvm.global.annotations = appending global [6 x { ptr, ptr, ptr, i32, ptr }] [{ ptr, ptr, ptr, i32, ptr } { ptr @main_l3, ptr @[[FLAG0]], ptr undef, i32 undef, ptr undef }, { ptr, ptr, ptr, i32, ptr } { ptr @main_l6, ptr @[[FLAG1]], ptr undef, i32 undef, ptr undef }, { ptr, ptr, ptr, i32, ptr } { ptr @main_l9, ptr @[[FLAG2]], ptr undef, i32 undef, ptr undef }, { ptr, ptr, ptr, i32, ptr } { ptr @main_l3, ptr @[[FLAG3]], ptr undef, i32 undef, ptr undef }, { ptr, ptr, ptr, i32, ptr } { ptr @main_l6, ptr @[[FLAG4]], ptr undef, i32 undef, ptr undef }, { ptr, ptr, ptr, i32, ptr } { ptr @main_l9, ptr @[[FLAG5]], ptr undef, i32 undef, ptr undef }], section "llvm.metadata"
2427

2528
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
2629
target triple = "spir64"

0 commit comments

Comments
 (0)