Skip to content

Commit

Permalink
Merge (conflict) 8937ecf Implement SPV_INTEL_global_variable_decorati…
Browse files Browse the repository at this point in the history
…ons extension .. '"master"' -> '"xmain-web"' (1 commits)

  CONFLICT (content): Merge conflict in lib/SPIRV/libSPIRV/spirv_internal.hpp
  CONFLICT (content): Merge conflict in lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h
  CONFLICT (content): Merge conflict in lib/SPIRV/libSPIRV/SPIRVDecorate.cpp
  CONFLICT (content): Merge conflict in include/LLVMSPIRVExtensions.inc

  commit 8937ecf
  Author: Viktoria Maximova <[email protected]>
  Date:   Thu Feb 3 17:53:08 2022 +0300

      Implement SPV_INTEL_global_variable_decorations extension (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
iclsrc committed Feb 10, 2022
2 parents 59b33f0 + 8937ecf commit c5e6466
Show file tree
Hide file tree
Showing 9 changed files with 262 additions and 0 deletions.
4 changes: 4 additions & 0 deletions include/LLVMSPIRVExtensions.inc
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,8 @@ EXT(SPV_INTEL_arithmetic_fence)
EXT(SPV_INTEL_bfloat16_conversion)
EXT(SPV_INTEL_joint_matrix)
EXT(SPV_INTEL_hw_thread_queries)
<<<<<<< HEAD
EXT(SPV_INTEL_task_sequence) // INTEL
=======
EXT(SPV_INTEL_global_variable_decorations)
>>>>>>> 8937ecf4698c9283c92c35b65e5b97e6e0acf869
10 changes: 10 additions & 0 deletions lib/SPIRV/SPIRVReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3933,6 +3933,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 @@ -2131,6 +2131,19 @@ void addFuncPointerCallArgumentAttributes(CallInst *CI,
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 @@ -2192,6 +2205,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
20 changes: 20 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVDecorate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,12 @@ size_t SPIRVDecorateGeneric::getLiteralCount() const { return Literals.size(); }
void SPIRVDecorate::encode(spv_ostream &O) const {
SPIRVEncoder Encoder = getEncoder(O);
Encoder << Target << Dec;
<<<<<<< HEAD
int IntDec = Dec; // INTEL
switch (IntDec) { // INTEL
=======
switch (static_cast<size_t>(Dec)) {
>>>>>>> 8937ecf4698c9283c92c35b65e5b97e6e0acf869
case DecorationLinkageAttributes:
SPIRVDecorateLinkageAttr::encodeLiterals(Encoder, Literals);
break;
Expand All @@ -130,6 +134,7 @@ void SPIRVDecorate::encode(spv_ostream &O) const {
case DecorationUserSemantic:
SPIRVDecorateUserSemanticAttr::encodeLiterals(Encoder, Literals);
break;
<<<<<<< HEAD
#if INTEL_CUSTOMIZATION
case internal::DecorationFuncParamDescINTEL:
SPIRVDecorateFuncParamDescAttr::encodeLiterals(Encoder, Literals);
Expand All @@ -138,6 +143,11 @@ void SPIRVDecorate::encode(spv_ostream &O) const {
SPIRVDecorateVectorVariantsTableINTEL::encodeLiterals(Encoder, Literals);
break;
#endif // INTEL_CUSTOMIZATION
=======
case spv::internal::DecorationHostAccessINTEL:
SPIRVDecorateHostAccessINTEL::encodeLiterals(Encoder, Literals);
break;
>>>>>>> 8937ecf4698c9283c92c35b65e5b97e6e0acf869
default:
Encoder << Literals;
}
Expand All @@ -151,12 +161,16 @@ void SPIRVDecorate::setWordCount(SPIRVWord Count) {
void SPIRVDecorate::decode(std::istream &I) {
SPIRVDecoder Decoder = getDecoder(I);
Decoder >> Target >> Dec;
<<<<<<< HEAD
#if INTEL_CUSTOMIZATION
int IntDec = Dec;
switch (IntDec) {
#else
switch (Dec) {
#endif // INTEL_CUSTOMIZATION
=======
switch (static_cast<size_t>(Dec)) {
>>>>>>> 8937ecf4698c9283c92c35b65e5b97e6e0acf869
case DecorationLinkageAttributes:
SPIRVDecorateLinkageAttr::decodeLiterals(Decoder, Literals);
break;
Expand All @@ -169,6 +183,7 @@ void SPIRVDecorate::decode(std::istream &I) {
case DecorationUserSemantic:
SPIRVDecorateUserSemanticAttr::decodeLiterals(Decoder, Literals);
break;
<<<<<<< HEAD
#if INTEL_CUSTOMIZATION
case internal::DecorationFuncParamDescINTEL:
SPIRVDecorateFuncParamDescAttr::decodeLiterals(Decoder, Literals);
Expand All @@ -177,6 +192,11 @@ void SPIRVDecorate::decode(std::istream &I) {
SPIRVDecorateVectorVariantsTableINTEL::decodeLiterals(Decoder, Literals);
break;
#endif // INTEL_CUSTOMIZATION
=======
case spv::internal::DecorationHostAccessINTEL:
SPIRVDecorateHostAccessINTEL::decodeLiterals(Decoder, Literals);
break;
>>>>>>> 8937ecf4698c9283c92c35b65e5b97e6e0acf869
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 @@ -193,6 +193,10 @@ class SPIRVDecorate : public SPIRVDecorateGeneric {
return ExtensionID::SPV_INTEL_fpga_invocation_pipelining_attributes;
case internal::DecorationRuntimeAlignedINTEL:
return ExtensionID::SPV_INTEL_runtime_aligned;
case internal::DecorationHostAccessINTEL:
case internal::DecorationInitModeINTEL:
case internal::DecorationImplementInCSRINTEL:
return ExtensionID::SPV_INTEL_global_variable_decorations;
default:
return {};
}
Expand Down Expand Up @@ -739,6 +743,69 @@ class SPIRVDecoratePipelineEnableINTEL : public SPIRVDecorate {
Enable){};
};

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 @@ -479,6 +479,12 @@ template <> inline void SPIRVMap<Decoration, SPIRVCapVec>::init() {
{internal::CapabilityFPGAInvocationPipeliningAttributesINTEL});
ADD_VEC_INIT(internal::DecorationRuntimeAlignedINTEL,
{internal::CapabilityRuntimeAlignedAttributeINTEL});
ADD_VEC_INIT(internal::DecorationHostAccessINTEL,
{internal::CapabilityGlobalVariableDecorationsINTEL});
ADD_VEC_INIT(internal::DecorationInitModeINTEL,
{internal::CapabilityGlobalVariableDecorationsINTEL});
ADD_VEC_INIT(internal::DecorationImplementInCSRINTEL,
{internal::CapabilityGlobalVariableDecorationsINTEL});
ADD_VEC_INIT(internal::DecorationArgumentAttributeINTEL,
{CapabilityFunctionPointersINTEL});
}
Expand Down
8 changes: 8 additions & 0 deletions lib/SPIRV/libSPIRV/SPIRVNameMapEnum.h
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,9 @@ template <> inline void SPIRVMap<Decoration, std::string>::init() {
add(internal::DecorationMaxConcurrencyINTEL, "MaxConcurrencyINTEL");
add(internal::DecorationPipelineEnableINTEL, "PipelineEnableINTEL");
add(internal::DecorationRuntimeAlignedINTEL, "RuntimeAlignedINTEL");
add(internal::DecorationHostAccessINTEL, "HostAccessINTEL");
add(internal::DecorationInitModeINTEL, "InitModeINTEL");
add(internal::DecorationImplementInCSRINTEL, "ImplementInCSRINTEL");
add(internal::DecorationArgumentAttributeINTEL, "ArgumentAttributeINTEL");

add(DecorationMax, "Max");
Expand Down Expand Up @@ -610,7 +613,12 @@ template <> inline void SPIRVMap<Capability, std::string>::init() {
add(internal::CapabilityBfloat16ConversionINTEL, "Bfloat16ConversionINTEL");
add(internal::CapabilityJointMatrixINTEL, "JointMatrixINTEL");
add(internal::CapabilityHWThreadQueryINTEL, "HWThreadQueryINTEL");
<<<<<<< HEAD
add(internal::CapabilityTaskSequenceINTEL, "TaskSequenceINTEL"); // INTEL
=======
add(internal::CapabilityGlobalVariableDecorationsINTEL,
"GlobalVariableDecorationsINTEL");
>>>>>>> 8937ecf4698c9283c92c35b65e5b97e6e0acf869
}
SPIRV_DEF_NAMEMAP(Capability, SPIRVCapabilityNameMap)

Expand Down
15 changes: 15 additions & 0 deletions lib/SPIRV/libSPIRV/spirv_internal.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,9 @@ enum InternalDecoration {
IDecPipelineEnableINTEL = 5919,
IDecRuntimeAlignedINTEL = 5940,
IDecCallableFunctionINTEL = 6087,
IDecHostAccessINTEL = 6147,
IDecInitModeINTEL = 6148,
IDecImplementInCSRINTEL = 6149,
IDecArgumentAttributeINTEL = 6409
};

Expand All @@ -118,7 +121,11 @@ enum InternalCapability {
ICapabilityJointMatrixINTEL = 6118,
ICapabilityHWThreadQueryINTEL = 6134,
ICapFPArithmeticFenceINTEL = 6144,
<<<<<<< HEAD
ICapabilityTaskSequenceINTEL = 6162 // INTEL
=======
ICapGlobalVariableDecorationsINTEL = 6146
>>>>>>> 8937ecf4698c9283c92c35b65e5b97e6e0acf869
};

enum InternalFunctionControlMask { IFunctionControlOptNoneINTELMask = 0x10000 };
Expand Down Expand Up @@ -226,6 +233,12 @@ constexpr Decoration DecorationCallableFunctionINTEL =
static_cast<Decoration>(IDecCallableFunctionINTEL);
constexpr Decoration DecorationRuntimeAlignedINTEL =
static_cast<Decoration>(IDecRuntimeAlignedINTEL);
constexpr Decoration DecorationHostAccessINTEL =
static_cast<Decoration>(IDecHostAccessINTEL);
constexpr Decoration DecorationInitModeINTEL =
static_cast<Decoration>(IDecInitModeINTEL);
constexpr Decoration DecorationImplementInCSRINTEL =
static_cast<Decoration>(IDecImplementInCSRINTEL);
constexpr Decoration DecorationArgumentAttributeINTEL =
static_cast<Decoration>(IDecArgumentAttributeINTEL);

Expand All @@ -247,6 +260,8 @@ constexpr Capability CapabilityFPArithmeticFenceINTEL =
static_cast<Capability>(ICapFPArithmeticFenceINTEL);
constexpr Capability CapabilityBfloat16ConversionINTEL =
static_cast<Capability>(ICapBfloat16ConversionINTEL);
constexpr Capability CapabilityGlobalVariableDecorationsINTEL =
static_cast<Capability>(ICapGlobalVariableDecorationsINTEL);

constexpr FunctionControlMask FunctionControlOptNoneINTELMask =
static_cast<FunctionControlMask>(IFunctionControlOptNoneINTELMask);
Expand Down
Loading

0 comments on commit c5e6466

Please sign in to comment.