Skip to content

Commit 42f2a1d

Browse files
committed
[SYCL] SPIRV-LLVM-Translator (5642686e67)
Signed-off-by: Vladimir Lazarev <[email protected]>
1 parent c6f836a commit 42f2a1d

40 files changed

+1494
-1385
lines changed

llvm-spirv/include/LLVMSPIRVLib.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,10 @@ bool writeSpirv(Module *M, const SPIRV::TranslatorOpts &Opts, std::ostream &OS,
128128
bool readSpirv(LLVMContext &C, const SPIRV::TranslatorOpts &Opts,
129129
std::istream &IS, Module *&M, std::string &ErrMsg);
130130

131+
using SpecConstInfoTy = std::pair<uint32_t, uint32_t>;
132+
void getSpecConstInfo(std::istream &IS,
133+
std::vector<SpecConstInfoTy> &SpecConstInfo);
134+
131135
/// \brief Convert a SPIRVModule into LLVM IR.
132136
/// \returns null on failure.
133137
std::unique_ptr<Module>

llvm-spirv/include/LLVMSPIRVOpts.h

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
#include <cassert>
4343
#include <cstdint>
4444
#include <map>
45+
#include <unordered_map>
4546

4647
namespace SPIRV {
4748

@@ -99,12 +100,25 @@ class TranslatorOpts {
99100

100101
void enableGenArgNameMD() { GenKernelArgNameMD = true; }
101102

103+
void setSpecConst(uint32_t SpecId, uint64_t SpecValue) {
104+
ExternalSpecialization[SpecId] = SpecValue;
105+
}
106+
107+
bool getSpecializationConstant(uint32_t SpecId, uint64_t &Value) const {
108+
auto It = ExternalSpecialization.find(SpecId);
109+
if (It == ExternalSpecialization.end())
110+
return false;
111+
Value = It->second;
112+
return true;
113+
}
114+
102115
private:
103116
// Common translation options
104117
VersionNumber MaxVersion = VersionNumber::MaximumVersion;
105118
ExtensionsStatusMap ExtStatusMap;
106119
// SPIR-V to LLVM translation options
107120
bool GenKernelArgNameMD;
121+
std::unordered_map<uint32_t, uint64_t> ExternalSpecialization;
108122
};
109123

110124
} // namespace SPIRV

llvm-spirv/lib/SPIRV/OCL20ToSPIRV.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1027,9 +1027,23 @@ void OCL20ToSPIRV::transBuiltin(CallInst *CI, OCLBuiltinTransInfo &Info) {
10271027
unsigned ExtOp = ~0U;
10281028
if (StringRef(Info.UniqName).startswith(kSPIRVName::Prefix))
10291029
return;
1030-
if (OCLSPIRVBuiltinMap::find(Info.UniqName, &OC))
1031-
Info.UniqName = getSPIRVFuncName(OC);
1032-
else if ((ExtOp = getExtOp(Info.MangledName, Info.UniqName)) != ~0U)
1030+
if (OCLSPIRVBuiltinMap::find(Info.UniqName, &OC)) {
1031+
if (OC == OpImageRead) {
1032+
// There are several read_image* functions defined by OpenCL C spec, but
1033+
// all of them use the same SPIR-V Instruction - some of them might only
1034+
// differ by return type, so, we need to include return type into the
1035+
// mangling scheme to get them differentiated.
1036+
//
1037+
// Example: int4 read_imagei(image2d_t, sampler_t, int2)
1038+
// uint4 read_imageui(image2d_t, sampler_t, int2)
1039+
// Both functions above are represented by the same SPIR-V
1040+
// instruction: argument types are the same, only return type is
1041+
// different
1042+
Info.UniqName = getSPIRVFuncName(OC, CI->getType());
1043+
} else {
1044+
Info.UniqName = getSPIRVFuncName(OC);
1045+
}
1046+
} else if ((ExtOp = getExtOp(Info.MangledName, Info.UniqName)) != ~0U)
10331047
Info.UniqName = getSPIRVExtFuncName(SPIRVEIS_OpenCL, ExtOp);
10341048
else
10351049
return;

llvm-spirv/lib/SPIRV/OCLUtil.cpp

Lines changed: 57 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -612,8 +612,10 @@ class OCLBuiltinFuncMangleInfo : public SPIRV::BuiltinFuncMangleInfo {
612612
addSamplerArg(1);
613613
} else if (UnmangledName.find(kOCLSubgroupsAVCIntel::Prefix) !=
614614
std::string::npos) {
615-
if (UnmangledName.find("evaluate_with_single_reference") !=
616-
std::string::npos)
615+
if (UnmangledName.find("evaluate_ipe") != std::string::npos)
616+
addSamplerArg(1);
617+
else if (UnmangledName.find("evaluate_with_single_reference") !=
618+
std::string::npos)
617619
addSamplerArg(2);
618620
else if (UnmangledName.find("evaluate_with_multi_reference") !=
619621
std::string::npos) {
@@ -637,11 +639,19 @@ class OCLBuiltinFuncMangleInfo : public SPIRV::BuiltinFuncMangleInfo {
637639
else if (UnmangledName.find("set_inter_base_multi_reference_penalty") !=
638640
std::string::npos ||
639641
UnmangledName.find("set_inter_shape_penalty") !=
642+
std::string::npos ||
643+
UnmangledName.find("set_inter_direction_penalty") !=
640644
std::string::npos)
641645
addUnsignedArg(0);
642646
else if (UnmangledName.find("set_motion_vector_cost_function") !=
643647
std::string::npos)
644648
addUnsignedArgs(0, 2);
649+
else if (UnmangledName.find("interlaced_field_polarity") !=
650+
std::string::npos)
651+
addUnsignedArg(0);
652+
else if (UnmangledName.find("interlaced_field_polarities") !=
653+
std::string::npos)
654+
addUnsignedArgs(0, 1);
645655
else if (UnmangledName.find(kOCLSubgroupsAVCIntel::MCEPrefix) !=
646656
std::string::npos) {
647657
if (UnmangledName.find("get_default") != std::string::npos)
@@ -653,15 +663,38 @@ class OCLBuiltinFuncMangleInfo : public SPIRV::BuiltinFuncMangleInfo {
653663
else if (UnmangledName.find("set_single_reference") !=
654664
std::string::npos)
655665
addUnsignedArg(1);
666+
else if (UnmangledName.find("set_dual_reference") != std::string::npos)
667+
addUnsignedArg(2);
668+
else if (UnmangledName.find("set_weighted_sad") != std::string::npos ||
669+
UnmangledName.find("set_early_search_termination_threshold") !=
670+
std::string::npos)
671+
addUnsignedArg(0);
656672
else if (UnmangledName.find("adjust_ref_offset") != std::string::npos)
657673
addUnsignedArgs(1, 3);
658674
else if (UnmangledName.find("set_max_motion_vector_count") !=
659675
std::string::npos ||
660676
UnmangledName.find("get_border_reached") != std::string::npos)
661677
addUnsignedArg(0);
678+
else if (UnmangledName.find("shape_distortions") != std::string::npos ||
679+
UnmangledName.find("shape_motion_vectors") !=
680+
std::string::npos ||
681+
UnmangledName.find("shape_reference_ids") !=
682+
std::string::npos) {
683+
if (UnmangledName.find("single_reference") != std::string::npos) {
684+
addUnsignedArg(1);
685+
EraseSubstring(UnmangledName, "_single_reference");
686+
} else if (UnmangledName.find("dual_reference") !=
687+
std::string::npos) {
688+
addUnsignedArgs(1, 2);
689+
EraseSubstring(UnmangledName, "_dual_reference");
690+
}
691+
} else if (UnmangledName.find("ref_window_size") != std::string::npos)
692+
addUnsignedArg(0);
662693
} else if (UnmangledName.find(kOCLSubgroupsAVCIntel::SICPrefix) !=
663694
std::string::npos) {
664-
if (UnmangledName.find("initialize") != std::string::npos)
695+
if (UnmangledName.find("initialize") != std::string::npos ||
696+
UnmangledName.find("set_intra_luma_shape_penalty") !=
697+
std::string::npos)
665698
addUnsignedArg(0);
666699
else if (UnmangledName.find("configure_ipe") != std::string::npos) {
667700
if (UnmangledName.find("_luma") != std::string::npos) {
@@ -672,7 +705,23 @@ class OCLBuiltinFuncMangleInfo : public SPIRV::BuiltinFuncMangleInfo {
672705
addUnsignedArgs(7, 9);
673706
EraseSubstring(UnmangledName, "_chroma");
674707
}
675-
}
708+
} else if (UnmangledName.find("configure_skc") != std::string::npos)
709+
addUnsignedArgs(0, 4);
710+
else if (UnmangledName.find("set_skc") != std::string::npos) {
711+
if (UnmangledName.find("forward_transform_enable"))
712+
addUnsignedArg(0);
713+
} else if (UnmangledName.find("set_block") != std::string::npos) {
714+
if (UnmangledName.find("based_raw_skip_sad") != std::string::npos)
715+
addUnsignedArg(0);
716+
} else if (UnmangledName.find("get_motion_vector_mask") !=
717+
std::string::npos) {
718+
addUnsignedArgs(0, 1);
719+
} else if (UnmangledName.find("luma_mode_cost_function") !=
720+
std::string::npos)
721+
addUnsignedArgs(0, 2);
722+
else if (UnmangledName.find("chroma_mode_cost_function") !=
723+
std::string::npos)
724+
addUnsignedArg(0);
676725
}
677726
} else if (UnmangledName == "intel_sub_group_shuffle_down" ||
678727
UnmangledName == "intel_sub_group_shuffle_up") {
@@ -702,11 +751,14 @@ class OCLBuiltinFuncMangleInfo : public SPIRV::BuiltinFuncMangleInfo {
702751
setArgAttr(0, SPIR::ATTR_CONST);
703752
addUnsignedArg(0);
704753
}
754+
} else if (UnmangledName.find("intel_sub_group_media_block_write") !=
755+
std::string::npos) {
756+
addUnsignedArg(3);
705757
}
706758
}
707759
// Auxiliarry information, it is expected that it is relevant at the moment
708760
// the init method is called.
709-
Function *F; // SPIRV decorated function
761+
Function *F; // SPIRV decorated function
710762
std::vector<Type *> ArgTypes; // Arguments of OCL builtin
711763
};
712764

llvm-spirv/lib/SPIRV/OCLUtil.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -747,6 +747,10 @@ template <> inline void SPIRVMap<std::string, Op, SPIRVInstruction>::init() {
747747
_SPIRV_OP(intel_sub_group_shuffle_down, SubgroupShuffleDownINTEL)
748748
_SPIRV_OP(intel_sub_group_shuffle_up, SubgroupShuffleUpINTEL)
749749
_SPIRV_OP(intel_sub_group_shuffle_xor, SubgroupShuffleXorINTEL)
750+
// Intel media_block_io builtins
751+
_SPIRV_OP(intel_sub_group_media_block_read, SubgroupImageMediaBlockReadINTEL)
752+
_SPIRV_OP(intel_sub_group_media_block_write,
753+
SubgroupImageMediaBlockWriteINTEL)
750754
#undef _SPIRV_OP
751755
}
752756

0 commit comments

Comments
 (0)