Skip to content

Commit

Permalink
[Backport to 10] Implement SPV_INTEL_global_variable_decorations exte…
Browse files Browse the repository at this point in the history
…nsion (KhronosGroup#1389)

* Implement SPV_INTEL_global_variable_decorations extension

This change adds support of decorations applied to global variables through
`spirv.Decorations` metadata in LLVM IR.

These decorations are mostly intended to help code generation for FPGA devices:
* HostAccessINTEL
* InitModeINTEL
* ImplementInCSRINTEL

Spec:
https://github.com/intel/llvm/blob/sycl/sycl/doc/extensions/DeviceGlobal/SPV_INTEL_global_variable_decorations.asciidoc
  • Loading branch information
vmaksimo authored and MrSidims committed Mar 16, 2023
1 parent 103f88c commit 98628f3
Show file tree
Hide file tree
Showing 9 changed files with 243 additions and 3 deletions.
1 change: 1 addition & 0 deletions include/LLVMSPIRVExtensions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,4 @@ EXT(SPV_INTEL_tensor_float32_conversion)
EXT(SPV_EXT_relaxed_printf_string_address_space)
EXT(SPV_INTEL_hw_thread_queries)
EXT(SPV_INTEL_split_barrier)
EXT(SPV_INTEL_global_variable_decorations)
10 changes: 10 additions & 0 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2978,6 +2978,16 @@ transDecorationsToMetadataList(llvm::LLVMContext *Context,
OPs.push_back(LinkTypeMD);
break;
}
case spv::internal::DecorationHostAccessINTEL: {
const auto *const HostAccDeco =
static_cast<const SPIRVDecorateHostAccessINTEL *>(Deco);
auto *const AccModeMD = ConstantAsMetadata::get(ConstantInt::get(
Type::getInt32Ty(*Context), HostAccDeco->getAccessMode()));
auto *const NameMD = MDString::get(*Context, HostAccDeco->getVarName());
OPs.push_back(AccModeMD);
OPs.push_back(NameMD);
break;
}
case DecorationMergeINTEL: {
const auto MergeAttrLits = Deco->getVecLiteral();
std::string FirstString = getString(MergeAttrLits);
Expand Down
59 changes: 59 additions & 0 deletions lib/SPIRV/SPIRVWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1644,6 +1644,19 @@ SPIRVValue *LLVMToSPIRV::mapValue(Value *V, SPIRVValue *BV) {
break; \
}

void checkIsGlobalVar(SPIRVEntry *E, Decoration Dec) {
std::string ErrStr =
SPIRVDecorationNameMap::map(Dec) + " can only be applied to a variable";

E->getErrorLog().checkError(E->isVariable(), SPIRVEC_InvalidModule, ErrStr);

auto AddrSpace = SPIRSPIRVAddrSpaceMap::rmap(
static_cast<SPIRVVariable *>(E)->getStorageClass());
ErrStr += " in a global (module) scope";
E->getErrorLog().checkError(AddrSpace == SPIRAS_Global, SPIRVEC_InvalidModule,
ErrStr);
}

static void transMetadataDecorations(Metadata *MD, SPIRVEntry *Target) {
auto *ArgDecoMD = dyn_cast<MDNode>(MD);
assert(ArgDecoMD && "Decoration list must be a metadata node");
Expand Down Expand Up @@ -1694,6 +1707,52 @@ static void transMetadataDecorations(Metadata *MD, SPIRVEntry *Target) {
Target, Name->getString().str(), TypeKind));
break;
}
case spv::internal::DecorationHostAccessINTEL: {
checkIsGlobalVar(Target, DecoKind);

assert(NumOperands == 3 && "HostAccessINTEL requires 2 extra operands "
"after the decoration kind number");
auto *AccessMode =
mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
assert(AccessMode &&
"HostAccessINTEL requires first extra operand to be an int");
auto *Name = dyn_cast<MDString>(DecoMD->getOperand(2));
assert(Name &&
"HostAccessINTEL requires second extra operand to be a string");

Target->addDecorate(new SPIRVDecorateHostAccessINTEL(
Target, AccessMode->getZExtValue(), Name->getString().str()));
break;
}
case spv::internal::DecorationInitModeINTEL: {
checkIsGlobalVar(Target, DecoKind);
assert(static_cast<SPIRVVariable *>(Target)->getInitializer() &&
"InitModeINTEL only be applied to a global (module scope) "
"variable which has an Initializer operand");

assert(NumOperands == 2 &&
"InitModeINTEL requires exactly 1 extra operand");
auto *Trigger = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
assert(Trigger &&
"InitModeINTEL requires extra operand to be an integer");

Target->addDecorate(
new SPIRVDecorateInitModeINTEL(Target, Trigger->getZExtValue()));
break;
}
case spv::internal::DecorationImplementInCSRINTEL: {
checkIsGlobalVar(Target, DecoKind);

assert(NumOperands == 2 &&
"ImplementInCSRINTEL requires exactly 1 extra operand");
auto *Value = mdconst::dyn_extract<ConstantInt>(DecoMD->getOperand(1));
assert(Value &&
"ImplementInCSRINTEL requires extra operand to be an integer");

Target->addDecorate(
new SPIRVDecorateImplementInCSRINTEL(Target, Value->getZExtValue()));
break;
}
default: {
if (NumOperands == 1) {
Target->addDecorate(new SPIRVDecorate(DecoKind, Target));
Expand Down
10 changes: 8 additions & 2 deletions lib/SPIRV/libSPIRV/SPIRVDecorate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ size_t SPIRVDecorateGeneric::getLiteralCount() const { return Literals.size(); }
void SPIRVDecorate::encode(spv_ostream &O) const {
SPIRVEncoder Encoder = getEncoder(O);
Encoder << Target << Dec;
switch (Dec) {
switch (static_cast<size_t>(Dec)) {
case DecorationLinkageAttributes:
SPIRVDecorateLinkageAttr::encodeLiterals(Encoder, Literals);
break;
Expand All @@ -114,6 +114,9 @@ void SPIRVDecorate::encode(spv_ostream &O) const {
case DecorationUserSemantic:
SPIRVDecorateUserSemanticAttr::encodeLiterals(Encoder, Literals);
break;
case spv::internal::DecorationHostAccessINTEL:
SPIRVDecorateHostAccessINTEL::encodeLiterals(Encoder, Literals);
break;
default:
Encoder << Literals;
}
Expand All @@ -127,7 +130,7 @@ void SPIRVDecorate::setWordCount(SPIRVWord Count) {
void SPIRVDecorate::decode(std::istream &I) {
SPIRVDecoder Decoder = getDecoder(I);
Decoder >> Target >> Dec;
switch (Dec) {
switch (static_cast<size_t>(Dec)) {
case DecorationLinkageAttributes:
SPIRVDecorateLinkageAttr::decodeLiterals(Decoder, Literals);
break;
Expand All @@ -140,6 +143,9 @@ void SPIRVDecorate::decode(std::istream &I) {
case DecorationUserSemantic:
SPIRVDecorateUserSemanticAttr::decodeLiterals(Decoder, Literals);
break;
case spv::internal::DecorationHostAccessINTEL:
SPIRVDecorateHostAccessINTEL::decodeLiterals(Decoder, Literals);
break;
default:
Decoder >> Literals;
}
Expand Down
67 changes: 67 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVDecorate.h
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,10 @@ class SPIRVDecorate : public SPIRVDecorateGeneric {
return getSet(ExtensionID::SPV_INTEL_float_controls2);
case DecorationCallableFunctionINTEL:
return getSet(ExtensionID::SPV_INTEL_fast_composite);
case internal::DecorationHostAccessINTEL:
case internal::DecorationInitModeINTEL:
case internal::DecorationImplementInCSRINTEL:
return getSet(ExtensionID::SPV_INTEL_global_variable_decorations);
default:
return SPIRVExtSet();
}
Expand Down Expand Up @@ -635,6 +639,69 @@ class SPIRVDecorateNoAliasINTEL : public SPIRVDecorateId {
AliasList){};
};

class SPIRVDecorateHostAccessINTEL : public SPIRVDecorate {
public:
// Complete constructor for SPIRVHostAccessINTEL
SPIRVDecorateHostAccessINTEL(SPIRVEntry *TheTarget, SPIRVWord AccessMode,
const std::string &VarName)
: SPIRVDecorate(spv::internal::DecorationHostAccessINTEL, TheTarget) {
Literals.push_back(AccessMode);
for (auto &I : getVec(VarName))
Literals.push_back(I);
WordCount += Literals.size();
};

SPIRVWord getAccessMode() const { return Literals.front(); }
std::string getVarName() const {
return getString(Literals.cbegin() + 1, Literals.cend());
}

static void encodeLiterals(SPIRVEncoder &Encoder,
const std::vector<SPIRVWord> &Literals) {
#ifdef _SPIRV_SUPPORT_TEXT_FMT
if (SPIRVUseTextFormat) {
Encoder << Literals.front();
std::string Name = getString(Literals.cbegin() + 1, Literals.cend());
Encoder << Name;
} else
#endif
Encoder << Literals;
}

static void decodeLiterals(SPIRVDecoder &Decoder,
std::vector<SPIRVWord> &Literals) {
#ifdef _SPIRV_SUPPORT_TEXT_FMT
if (SPIRVUseTextFormat) {
SPIRVWord Mode;
Decoder >> Mode;
std::string Name;
Decoder >> Name;
Literals.front() = Mode;
std::copy_n(getVec(Name).begin(), Literals.size() - 1,
Literals.begin() + 1);

} else
#endif
Decoder >> Literals;
}
};

class SPIRVDecorateInitModeINTEL : public SPIRVDecorate {
public:
// Complete constructor for SPIRVInitModeINTEL
SPIRVDecorateInitModeINTEL(SPIRVEntry *TheTarget, SPIRVWord Trigger)
: SPIRVDecorate(spv::internal::DecorationInitModeINTEL, TheTarget,
Trigger){};
};

class SPIRVDecorateImplementInCSRINTEL : public SPIRVDecorate {
public:
// Complete constructor for SPIRVImplementInCSRINTEL
SPIRVDecorateImplementInCSRINTEL(SPIRVEntry *TheTarget, SPIRVWord Value)
: SPIRVDecorate(spv::internal::DecorationImplementInCSRINTEL, TheTarget,
Value){};
};

} // namespace SPIRV

#endif // SPIRV_LIBSPIRV_SPIRVDECORATE_H
6 changes: 6 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ template <> inline void SPIRVMap<Decoration, SPIRVCapVec>::init() {
{internal::CapabilityMemoryAccessAliasingINTEL});
ADD_VEC_INIT(internal::DecorationNoAliasINTEL,
{internal::CapabilityMemoryAccessAliasingINTEL});
ADD_VEC_INIT(internal::DecorationHostAccessINTEL,
{internal::CapabilityGlobalVariableDecorationsINTEL});
ADD_VEC_INIT(internal::DecorationInitModeINTEL,
{internal::CapabilityGlobalVariableDecorationsINTEL});
ADD_VEC_INIT(internal::DecorationImplementInCSRINTEL,
{internal::CapabilityGlobalVariableDecorationsINTEL});
}

template <> inline void SPIRVMap<BuiltIn, SPIRVCapVec>::init() {
Expand Down
5 changes: 5 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,9 @@ template <> inline void SPIRVMap<Decoration, std::string>::init() {

add(internal::DecorationAliasScopeINTEL, "AliasScopeINTEL");
add(internal::DecorationNoAliasINTEL, "NoAliasINTEL");
add(internal::DecorationHostAccessINTEL, "HostAccessINTEL");
add(internal::DecorationInitModeINTEL, "InitModeINTEL");
add(internal::DecorationImplementInCSRINTEL, "ImplementInCSRINTEL");
}
SPIRV_DEF_NAMEMAP(Decoration, SPIRVDecorationNameMap)

Expand Down Expand Up @@ -586,6 +589,8 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
"TensorFloat32ConversionINTEL");
add(internal::CapabilityHWThreadQueryINTEL, "HWThreadQueryINTEL");
add(CapabilitySplitBarrierINTEL, "SplitBarrierINTEL");
add(internal::CapabilityGlobalVariableDecorationsINTEL,
"GlobalVariableDecorationsINTEL");

add(CapabilityMax, "Max");
}
Expand Down
14 changes: 13 additions & 1 deletion lib/SPIRV/libSPIRV/spirv_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,10 @@ enum InternalOp {

enum InternalDecoration {
IDecAliasScopeINTEL = 5914,
IDecNoAliasINTEL = 5915
IDecNoAliasINTEL = 5915,
IDecHostAccessINTEL = 6147,
IDecInitModeINTEL = 6148,
IDecImplementInCSRINTEL = 6149,
};

enum InternalCapability {
Expand All @@ -59,6 +62,7 @@ enum InternalCapability {
ICapabilityTensorFloat32ConversionINTEL = 6425,
ICapabilityMaskedGatherScatterINTEL = 6427,
ICapabilityHWThreadQueryINTEL = 6134,
ICapGlobalVariableDecorationsINTEL = 6146
};

enum InternalMemoryAccessMask {
Expand Down Expand Up @@ -100,9 +104,17 @@ constexpr Decoration DecorationAliasScopeINTEL =
static_cast<Decoration>(IDecAliasScopeINTEL );
constexpr Decoration DecorationNoAliasINTEL =
static_cast<Decoration>(IDecNoAliasINTEL);
constexpr Decoration DecorationHostAccessINTEL =
static_cast<Decoration>(IDecHostAccessINTEL);
constexpr Decoration DecorationInitModeINTEL =
static_cast<Decoration>(IDecInitModeINTEL);
constexpr Decoration DecorationImplementInCSRINTEL =
static_cast<Decoration>(IDecImplementInCSRINTEL);

constexpr Capability CapabilityBfloat16ConversionINTEL =
static_cast<Capability>(ICapBfloat16ConversionINTEL);
constexpr Capability CapabilityGlobalVariableDecorationsINTEL =
static_cast<Capability>(ICapGlobalVariableDecorationsINTEL);

constexpr Capability CapabilityMemoryAccessAliasingINTEL =
static_cast<Capability>(ICapMemoryAccessAliasingINTEL);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
; RUN: llvm-as %s -o %t.bc
; RUN: llvm-spirv %t.bc --spirv-ext=+SPV_INTEL_global_variable_decorations -o %t.spv
; RUN: llvm-spirv %t.spv -to-text -o %t.spt
; RUN: FileCheck < %t.spt %s --check-prefix=CHECK-SPIRV

; RUN: llvm-spirv -r %t.spv --spirv-target-env=SPV-IR -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-SPV-IR

; RUN: llvm-spirv -r %t.spv -o %t.rev.bc
; RUN: llvm-dis %t.rev.bc
; RUN: FileCheck < %t.rev.ll %s --check-prefix=CHECK-LLVM

; Expected to fail - the decorations require enabled extension to be translated.
; RUN: not llvm-spirv %t.bc -o %t.spv

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"
target triple = "spir64-unknown-unknown"

@int_var = addrspace(1) global i32 42, !spirv.Decorations !1
@float_var = addrspace(1) global float 1.0, !spirv.Decorations !6
@bool_var = addrspace(1) global i1 0, !spirv.Decorations !9

; CHECK-SPIRV: Capability GlobalVariableDecorationsINTEL
; CHECK-SPIRV: Extension "SPV_INTEL_global_variable_decorations"
; CHECK-SPIRV: Decorate [[#INT_VAR_ID:]] HostAccessINTEL 1 "IntVarName"
; CHECK-SPIRV: Decorate [[#BOOL_VAR_ID:]] HostAccessINTEL 3 "BoolVarName"

; CHECK-SPIRV: Decorate [[#INT_VAR_ID]] InitModeINTEL 0
; CHECK-SPIRV: Decorate [[#BOOL_VAR_ID]] InitModeINTEL 0
; CHECK-SPIRV: Decorate [[#FLOAT_VAR_ID:]] InitModeINTEL 1

; CHECK-SPIRV: Decorate [[#BOOL_VAR_ID]] ImplementInCSRINTEL 0
; CHECK-SPIRV: Decorate [[#INT_VAR_ID]] ImplementInCSRINTEL 1
; CHECK-SPIRV: Decorate [[#FLOAT_VAR_ID]] ImplementInCSRINTEL 1


; 5 is a global storage
; CHECK-SPIRV: Variable [[#]] [[#INT_VAR_ID]] 5
; CHECK-SPIRV: Variable [[#]] [[#FLOAT_VAR_ID]] 5
; CHECK-SPIRV: Variable [[#]] [[#BOOL_VAR_ID]] 5

!1 = !{!2, !3, !4}
!2 = !{i32 6147, i32 1, !"IntVarName"} ; HostAccessINTEL 1 "IntVarName"
!3 = !{i32 6149, i1 true} ; ImplementInCSRINTEL = true
!4 = !{i32 6148, i32 0} ; InitModeINTEL = 0
!5 = !{i32 6148, i32 1} ; InitModeINTEL = 1
!6 = !{!3, !5}
!7 = !{i32 6147, i32 3, !"BoolVarName"} ; HostAccessINTEL 3 "BoolVarName"
!8 = !{i32 6149, i1 false} ; ImplementInCSRINTEL = false
!9 = !{!7, !8, !4}

; CHECK-SPV-IR: @int_var = addrspace(1) global i32 42, !spirv.Decorations ![[#INT_VAR_DEC:]]
; CHECK-SPV-IR: @float_var = addrspace(1) global float 1.000000e+00, !spirv.Decorations ![[#FLOAT_VAR_DEC:]]
; CHECK-SPV-IR: @bool_var = addrspace(1) global i1 false, !spirv.Decorations ![[#BOOL_VAR_DEC:]]

; CHECK-SPV-IR: ![[#INT_VAR_DEC]] = !{![[#]], ![[#MD_HOST_ACCESS_INTVAR:]], ![[#MD_INIT_0:]], ![[#MD_CSR_1:]]}
; CHECK-SPV-IR: ![[#MD_HOST_ACCESS_INTVAR]] = !{i32 6147, i32 1, !"IntVarName"}
; CHECK-SPV-IR: ![[#MD_INIT_0]] = !{i32 6148, i32 0}
; CHECK-SPV-IR: ![[#MD_CSR_1]] = !{i32 6149, i32 1}
; CHECK-SPV-IR: ![[#FLOAT_VAR_DEC]] = !{![[#]], ![[#MD_INIT_1:]], ![[#MD_CSR_1]]}
; CHECK-SPV-IR: ![[#MD_INIT_1]] = !{i32 6148, i32 1}
; CHECK-SPV-IR: ![[#BOOL_VAR_DEC]] = !{![[#]], ![[#MD_HOST_ACCESS_BOOLVAR:]], ![[#MD_INIT_0]], ![[#MD_CSR_0:]]}
; CHECK-SPV-IR: ![[#MD_HOST_ACCESS_BOOLVAR]] = !{i32 6147, i32 3, !"BoolVarName"}
; CHECK-SPV-IR: ![[#MD_CSR_0]] = !{i32 6149, i32 0}


; CHECK-LLVM-NOT: @int_var = {{.*}}, !spirv.Decorations ![[#]]
; CHECK-LLVM-NOT: @float_var = {{.*}}, !spirv.Decorations ![[#]]
; CHECK-LLVM-NOT: @bool_var = {{.*}}, !spirv.Decorations ![[#]]

; CHECK-LLVM: @int_var = addrspace(1) global i32 42
; CHECK-LLVM: @float_var = addrspace(1) global float 1.000000e+00
; CHECK-LLVM: @bool_var = addrspace(1) global i1 false

0 comments on commit 98628f3

Please sign in to comment.