Skip to content

Commit 177b88e

Browse files
committed
Add support for SPV_INTEL_fpga_buffer_location extension
This extension adds a function parameter decoration that is useful for FPGA targets. This decoration indicates that a particular global memory pointer can only access a particular physical memory location. Knowing this information at compile time can allow FPGA compilers to generate load store units of lower area for accesses done through such a pointer. Specification: intel/llvm#2084 Signed-off-by: Dmitry Sidorov <[email protected]>
1 parent 5202e9c commit 177b88e

File tree

8 files changed

+119
-0
lines changed

8 files changed

+119
-0
lines changed

include/LLVMSPIRVExtensions.inc

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ EXT(SPV_INTEL_optimization_hints)
2222
EXT(SPV_INTEL_float_controls2)
2323
EXT(SPV_INTEL_vector_compute)
2424
EXT(SPV_INTEL_usm_storage_classes)
25+
EXT(SPV_INTEL_fpga_buffer_location)

lib/SPIRV/SPIRVReader.cpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,28 @@ static void addOCLKernelArgumentMetadata(
168168
Fn->setMetadata(MDName, MDNode::get(*Context, ValueVec));
169169
}
170170

171+
static void addBufferLocationMetadata(
172+
LLVMContext *Context, SPIRVFunction *BF, llvm::Function *Fn,
173+
std::function<Metadata *(SPIRVFunctionParameter *)> Func) {
174+
std::vector<Metadata *> ValueVec;
175+
bool DecorationFound = false;
176+
BF->foreachArgument([&](SPIRVFunctionParameter *Arg) {
177+
if (Arg->getType()->isTypePointer() &&
178+
Arg->hasDecorate(DecorationBufferLocationINTEL)) {
179+
DecorationFound = true;
180+
ValueVec.push_back(Func(Arg));
181+
} else {
182+
llvm::Metadata *DefaultNode =
183+
ConstantAsMetadata::get(
184+
ConstantInt::get(Type::getInt32Ty(*Context), -1));
185+
ValueVec.push_back(DefaultNode);
186+
}
187+
});
188+
if (DecorationFound)
189+
Fn->setMetadata("kernel_arg_buffer_location",
190+
MDNode::get(*Context, ValueVec));
191+
}
192+
171193
Value *SPIRVToLLVM::getTranslatedValue(SPIRVValue *BV) {
172194
auto Loc = ValueMap.find(BV);
173195
if (Loc != ValueMap.end())
@@ -3443,6 +3465,15 @@ bool SPIRVToLLVM::transOCLMetadata(SPIRVFunction *BF) {
34433465
Arg->getName());
34443466
});
34453467
}
3468+
// Generate metadata for kernel_arg_buffer_location
3469+
addBufferLocationMetadata(Context, BF, F, [=](SPIRVFunctionParameter *Arg) {
3470+
auto Literals = Arg->getDecorationLiterals(DecorationBufferLocationINTEL);
3471+
assert(Literals.size() == 1 &&
3472+
"BufferLocationINTEL decoration shall have 1 ID literal");
3473+
3474+
return ConstantAsMetadata::get(
3475+
ConstantInt::get(Type::getInt32Ty(*Context), Literals[0]));
3476+
});
34463477
return true;
34473478
}
34483479

lib/SPIRV/SPIRVWriter.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,13 @@ SPIRVFunction *LLVMToSPIRV::transFunctionDecl(Function *F) {
523523
BM->addEntryPoint(ExecutionModelKernel, BF->getId());
524524
else if (F->getLinkage() != GlobalValue::InternalLinkage)
525525
BF->setLinkageType(transLinkageType(F));
526+
527+
// Translate OpenCL/SYCL buffer_location metadata if it's attached to the
528+
// translated function declaration
529+
MDNode *BufferLocation = nullptr;
530+
if (BM->isAllowedToUseExtension(ExtensionID::SPV_INTEL_fpga_buffer_location))
531+
BufferLocation = ((*F).getMetadata("kernel_arg_buffer_location"));
532+
526533
auto Attrs = F->getAttributes();
527534

528535
for (Function::arg_iterator I = F->arg_begin(), E = F->arg_end(); I != E;
@@ -548,6 +555,15 @@ SPIRVFunction *LLVMToSPIRV::transFunctionDecl(Function *F) {
548555
BA->addDecorate(DecorationMaxByteOffset,
549556
Attrs.getAttribute(ArgNo + 1, Attribute::Dereferenceable)
550557
.getDereferenceableBytes());
558+
if (BufferLocation && I->getType()->isPointerTy()) {
559+
// Order of integer numbers in MD node follows the order of function
560+
// parameters on which we shall attach the appropriate decoration. Add
561+
// decoration only if MD value is not negative.
562+
BM->addCapability(CapabilityFPGABufferLocationINTEL);
563+
int LocID = getMDOperandAsInt(BufferLocation, ArgNo);
564+
if (LocID >= 0)
565+
BA->addDecorate(DecorationBufferLocationINTEL, LocID);
566+
}
551567
}
552568
if (Attrs.hasAttribute(AttributeList::ReturnIndex, Attribute::ZExt))
553569
BF->addDecorate(DecorationFuncParamAttr, FunctionParameterAttributeZext);

lib/SPIRV/libSPIRV/SPIRVDecorate.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ class SPIRVDecorate : public SPIRVDecorateGeneric {
159159
return getSet(ExtensionID::SPV_INTEL_function_pointers);
160160
case DecorationIOPipeStorageINTEL:
161161
return getSet(ExtensionID::SPV_INTEL_io_pipes);
162+
case DecorationBufferLocationINTEL:
163+
return getSet(ExtensionID::SPV_INTEL_fpga_buffer_location);
162164
default:
163165
return SPIRVExtSet();
164166
}
@@ -257,6 +259,8 @@ class SPIRVMemberDecorate : public SPIRVDecorateGeneric {
257259
return getSet(ExtensionID::SPV_INTEL_fpga_memory_attributes);
258260
case DecorationIOPipeStorageINTEL:
259261
return getSet(ExtensionID::SPV_INTEL_io_pipes);
262+
case DecorationBufferLocationINTEL:
263+
return getSet(ExtensionID::SPV_INTEL_fpga_buffer_location);
260264
default:
261265
return SPIRVExtSet();
262266
}

lib/SPIRV/libSPIRV/SPIRVEnum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ template <> inline void SPIRVMap<Decoration, SPIRVCapVec>::init() {
394394
ADD_VEC_INIT(DecorationFuncParamIOKind, {CapabilityVectorComputeINTEL});
395395
ADD_VEC_INIT(DecorationStackCallINTEL, {CapabilityVectorComputeINTEL});
396396
ADD_VEC_INIT(DecorationSIMTCallINTEL, {CapabilityVectorComputeINTEL});
397+
ADD_VEC_INIT(DecorationBufferLocationINTEL,
398+
{CapabilityFPGABufferLocationINTEL});
397399
}
398400

399401
template <> inline void SPIRVMap<BuiltIn, SPIRVCapVec>::init() {

lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -370,6 +370,7 @@ template <> inline void SPIRVMap<Decoration, std::string>::init() {
370370
add(DecorationGlobalVariableOffsetINTEL, "GlobalVariableOffsetINTEL");
371371
add(DecorationFuncParamIOKind, "FuncParamIOKind");
372372
add(DecorationSIMTCallINTEL, "SIMTCallINTEL");
373+
add(DecorationBufferLocationINTEL, "BufferLocationINTEL");
373374
}
374375
SPIRV_DEF_NAMEMAP(Decoration, SPIRVDecorationNameMap)
375376

@@ -553,6 +554,7 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
553554
"GroupNonUniformShuffleRelative");
554555
add(CapabilityGroupNonUniformClustered, "GroupNonUniformClustered");
555556
add(CapabilityUSMStorageClassesINTEL, "USMStorageClassesINTEL");
557+
add(CapabilityFPGABufferLocationINTEL, "FPGABufferLocationINTEL");
556558
}
557559
SPIRV_DEF_NAMEMAP(Capability, SPIRVCapabilityNameMap)
558560

lib/SPIRV/libSPIRV/spirv.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -502,6 +502,7 @@ enum Decoration {
502502
DecorationMergeINTEL = 5834,
503503
DecorationBankBitsINTEL = 5835,
504504
DecorationForcePow2DepthINTEL = 5836,
505+
DecorationBufferLocationINTEL = 5921,
505506
DecorationIOPipeStorageINTEL = 5944,
506507
DecorationMax = 0x7fffffff,
507508
};
@@ -956,6 +957,7 @@ enum Capability {
956957
CapabilityFPGARegINTEL = 5948,
957958
CapabilityKernelAttributesINTEL = 5892,
958959
CapabilityFPGAKernelAttributesINTEL = 5897,
960+
CapabilityFPGABufferLocationINTEL = 5920,
959961
CapabilityUSMStorageClassesINTEL = 5935,
960962
CapabilityIOPipeINTEL = 5943,
961963
CapabilityMax = 0x7fffffff,
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_fpga_buffer_location -o %t.spv
3+
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
4+
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV
5+
6+
; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
7+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
8+
9+
; RUN: llvm-spirv -spirv-text -r %t.spt -o %t.rev.bc
10+
; RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM
11+
12+
; CHECK-SPIRV: 2 Capability FPGABufferLocationINTEL
13+
; CHECK-SPIRV: 9 Extension "SPV_INTEL_fpga_buffer_location"
14+
; CHECK-SPIRV: 3 Name [[ARGA:[0-9]+]] "a"
15+
; CHECK-SPIRV: 3 Name [[ARGB:[0-9]+]] "b"
16+
; CHECK-SPIRV: 3 Name [[ARGC:[0-9]+]] "c"
17+
; CHECK-SPIRV: 3 Name [[ARGD:[0-9]+]] "d"
18+
; CHECK-SPIRV: 3 Name [[ARGE:[0-9]+]] "e"
19+
; CHECK-SPIRV-NOT: 4 Decorate [[ARGC]] BufferLocationINTEL -1
20+
; CHECK-SPIRV-NOT: 4 Decorate [[ARGC]] BufferLocationINTEL -1
21+
; CHECK-SPIRV: 4 Decorate [[ARGA]] BufferLocationINTEL 1
22+
; CHECK-SPIRV: 4 Decorate [[ARGB]] BufferLocationINTEL 2
23+
; CHECK-SPIRV-NOT: 4 Decorate [[ARGD]] BufferLocationINTEL -1
24+
; CHECK-SPIRV-NOT: 4 Decorate [[ARGE]] BufferLocationINTEL 3
25+
26+
; CHECK-SPIRV: 5 Function
27+
; CHECK-SPIRV: 3 FunctionParameter {{[0-9]+}} [[ARGA]]
28+
; CHECK-SPIRV: 3 FunctionParameter {{[0-9]+}} [[ARGB]]
29+
; CHECK-SPIRV: 3 FunctionParameter {{[0-9]+}} [[ARGC]]
30+
; CHECK-SPIRV: 3 FunctionParameter {{[0-9]+}} [[ARGD]]
31+
; CHECK-SPIRV: 3 FunctionParameter {{[0-9]+}} [[ARGE]]
32+
33+
; CHECK-LLVM: define spir_kernel void @test{{.*}} !kernel_arg_buffer_location ![[BUFLOC_MD:[0-9]+]] {{.*}}
34+
; CHECK-LLVM: ![[BUFLOC_MD]] = !{i32 1, i32 2, i32 -1, i32 -1, i32 -1}
35+
36+
; ModuleID = 'buffer_location.cl'
37+
source_filename = "buffer_location.cl"
38+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024"
39+
target triple = "spir64-unknown-unknown"
40+
41+
; Function Attrs: norecurse nounwind readnone
42+
define spir_kernel void @test(i32 addrspace(1)* %a, float addrspace(1)* %b, i32 addrspace(1)* %c, i32 %d, i32 %e) local_unnamed_addr !kernel_arg_addr_space !3 !kernel_arg_access_qual !4 !kernel_arg_type !5 !kernel_arg_base_type !5 !kernel_arg_buffer_location !6 {
43+
entry:
44+
ret void
45+
}
46+
47+
!opencl.enable.FP_CONTRACT = !{}
48+
!opencl.ocl.version = !{!0}
49+
!opencl.spir.version = !{!0}
50+
!opencl.used.extensions = !{!1}
51+
!opencl.used.optional.core.features = !{!1}
52+
!opencl.compiler.options = !{!1}
53+
!llvm.ident = !{!2}
54+
55+
!0 = !{i32 2, i32 0}
56+
!1 = !{}
57+
!2 = !{!""}
58+
!3 = !{i32 1, i32 1, i32 1}
59+
!4 = !{!"none", !"none", !"none"}
60+
!5 = !{!"int*", !"float*", !"int*"}
61+
!6 = !{i32 1, i32 2, i32 -1, i32 -1, i32 3}

0 commit comments

Comments
 (0)