20#include "llvm/IR/IntrinsicsSPIRV.h"
25#define DEBUG_TYPE "spirv-builtins"
29#define GET_BuiltinGroup_DECL
30#include "SPIRVGenTables.inc"
34 InstructionSet::InstructionSet
Set;
40#define GET_DemangledBuiltins_DECL
41#define GET_DemangledBuiltins_IMPL
63 InstructionSet::InstructionSet
Set;
67#define GET_NativeBuiltins_DECL
68#define GET_NativeBuiltins_IMPL
86#define GET_GroupBuiltins_DECL
87#define GET_GroupBuiltins_IMPL
97#define GET_IntelSubgroupsBuiltins_DECL
98#define GET_IntelSubgroupsBuiltins_IMPL
105#define GET_AtomicFloatingBuiltins_DECL
106#define GET_AtomicFloatingBuiltins_IMPL
113#define GET_GroupUniformBuiltins_DECL
114#define GET_GroupUniformBuiltins_IMPL
118 InstructionSet::InstructionSet
Set;
122using namespace BuiltIn;
123#define GET_GetBuiltins_DECL
124#define GET_GetBuiltins_IMPL
128 InstructionSet::InstructionSet
Set;
132#define GET_ImageQueryBuiltins_DECL
133#define GET_ImageQueryBuiltins_IMPL
141#define GET_IntegerDotProductBuiltins_DECL
142#define GET_IntegerDotProductBuiltins_IMPL
146 InstructionSet::InstructionSet
Set;
157 InstructionSet::InstructionSet
Set;
164using namespace FPRoundingMode;
165#define GET_ConvertBuiltins_DECL
166#define GET_ConvertBuiltins_IMPL
168using namespace InstructionSet;
169#define GET_VectorLoadStoreBuiltins_DECL
170#define GET_VectorLoadStoreBuiltins_IMPL
172#define GET_CLMemoryScope_DECL
173#define GET_CLSamplerAddressingMode_DECL
174#define GET_CLMemoryFenceFlags_DECL
175#define GET_ExtendedBuiltins_DECL
176#include "SPIRVGenTables.inc"
188 StringRef PassPrefix =
"(anonymous namespace)::";
189 std::string BuiltinName;
192 BuiltinName = DemangledCall.
substr(PassPrefix.
size());
194 BuiltinName = DemangledCall;
197 BuiltinName = BuiltinName.
substr(0, BuiltinName.find(
'('));
200 if (BuiltinName.rfind(
"__spirv_ocl_", 0) == 0)
201 BuiltinName = BuiltinName.
substr(12);
206 std::size_t Pos1 = BuiltinName.
rfind(
'<');
207 if (Pos1 != std::string::npos && BuiltinName.back() ==
'>') {
208 std::size_t Pos2 = BuiltinName.rfind(
' ', Pos1);
209 if (Pos2 == std::string::npos)
213 BuiltinName = BuiltinName.substr(Pos2, Pos1 - Pos2);
214 BuiltinName = BuiltinName.substr(BuiltinName.find_last_of(
' ') + 1);
241 static const std::regex SpvWithR(
242 "(__spirv_(ImageSampleExplicitLod|ImageRead|ImageWrite|ImageQuerySizeLod|"
244 "SDotKHR|SUDotKHR|SDotAccSatKHR|UDotAccSatKHR|SUDotAccSatKHR|"
245 "ReadClockKHR|SubgroupBlockReadINTEL|SubgroupImageBlockReadINTEL|"
246 "SubgroupImageMediaBlockReadINTEL|SubgroupImageMediaBlockWriteINTEL|"
248 "UConvert|SConvert|FConvert|SatConvert)[^_]*)(_R[^_]*_?(\\w+)?.*)?");
250 if (std::regex_match(BuiltinName, Match, SpvWithR) && Match.size() > 1) {
251 std::ssub_match SubMatch;
252 if (DecorationId && Match.size() > 3) {
257 BuiltinName = SubMatch.str();
274static std::unique_ptr<const SPIRV::IncomingCall>
276 SPIRV::InstructionSet::InstructionSet Set,
283 DemangledCall.
slice(DemangledCall.
find(
'(') + 1, DemangledCall.
find(
')'));
284 BuiltinArgs.
split(BuiltinArgumentTypes,
',', -1,
false);
289 if ((Builtin = SPIRV::lookupBuiltin(BuiltinName, Set)))
290 return std::make_unique<SPIRV::IncomingCall>(
291 BuiltinName, Builtin, ReturnRegister, ReturnType,
Arguments);
296 if (BuiltinArgumentTypes.
size() >= 1) {
297 char FirstArgumentType = BuiltinArgumentTypes[0][0];
302 switch (FirstArgumentType) {
305 if (Set == SPIRV::InstructionSet::OpenCL_std)
307 else if (Set == SPIRV::InstructionSet::GLSL_std_450)
315 if (Set == SPIRV::InstructionSet::OpenCL_std)
317 else if (Set == SPIRV::InstructionSet::GLSL_std_450)
324 if (Set == SPIRV::InstructionSet::OpenCL_std ||
325 Set == SPIRV::InstructionSet::GLSL_std_450)
331 if (!Prefix.empty() &&
332 (Builtin = SPIRV::lookupBuiltin(Prefix + BuiltinName, Set)))
333 return std::make_unique<SPIRV::IncomingCall>(
334 BuiltinName, Builtin, ReturnRegister, ReturnType,
Arguments);
341 switch (FirstArgumentType) {
362 if (!Suffix.empty() &&
363 (Builtin = SPIRV::lookupBuiltin(BuiltinName + Suffix, Set)))
364 return std::make_unique<SPIRV::IncomingCall>(
365 BuiltinName, Builtin, ReturnRegister, ReturnType,
Arguments);
380 assert(
MI->getOpcode() == TargetOpcode::G_ADDRSPACE_CAST &&
381 MI->getOperand(1).isReg());
382 Register BitcastReg =
MI->getOperand(1).getReg();
410 Register ValueReg =
MI->getOperand(0).getReg();
416 assert(Ty &&
"Type is expected");
428 if (
MI->getOpcode() == TargetOpcode::G_GLOBAL_VALUE)
429 return MI->getOperand(1).getGlobal()->getType();
431 "Blocks in OpenCL C must be traceable to allocation site");
443static std::tuple<Register, SPIRVType *>
449 if (ResultType->
getOpcode() == SPIRV::OpTypeVector) {
464 return std::make_tuple(ResultRegister, BoolType);
475 if (ReturnType->getOpcode() == SPIRV::OpTypeVector) {
486 return MIRBuilder.
buildSelect(ReturnRegister, SourceRegister, TrueConst,
496 if (!DestinationReg.isValid())
501 MIRBuilder.
buildLoad(DestinationReg, PtrRegister, PtrInfo,
Align());
502 return DestinationReg;
518 VariableType, MIRBuilder, SPIRV::StorageClass::Input);
524 SPIRV::StorageClass::Input,
nullptr, isConst,
525 hasLinkageTy, SPIRV::LinkageType::Import, MIRBuilder,
532 return LoadedRegister;
541 SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB,
542 MachineRegisterInfo &
MRI);
545static SPIRV::MemorySemantics::MemorySemantics
548 case std::memory_order_relaxed:
549 return SPIRV::MemorySemantics::None;
550 case std::memory_order_acquire:
551 return SPIRV::MemorySemantics::Acquire;
552 case std::memory_order_release:
553 return SPIRV::MemorySemantics::Release;
554 case std::memory_order_acq_rel:
555 return SPIRV::MemorySemantics::AcquireRelease;
556 case std::memory_order_seq_cst:
557 return SPIRV::MemorySemantics::SequentiallyConsistent;
565 case SPIRV::CLMemoryScope::memory_scope_work_item:
566 return SPIRV::Scope::Invocation;
567 case SPIRV::CLMemoryScope::memory_scope_work_group:
568 return SPIRV::Scope::Workgroup;
569 case SPIRV::CLMemoryScope::memory_scope_device:
570 return SPIRV::Scope::Device;
571 case SPIRV::CLMemoryScope::memory_scope_all_svm_devices:
572 return SPIRV::Scope::CrossDevice;
573 case SPIRV::CLMemoryScope::memory_scope_sub_group:
574 return SPIRV::Scope::Subgroup;
587 SPIRV::Scope::Scope Scope,
591 if (CLScopeRegister.
isValid()) {
596 if (CLScope ==
static_cast<unsigned>(Scope)) {
597 MRI->setRegClass(CLScopeRegister, &SPIRV::iIDRegClass);
598 return CLScopeRegister;
606 if (
MRI->getRegClassOrNull(
Reg))
610 SpvType ? GR->
getRegClass(SpvType) : &SPIRV::iIDRegClass);
614 Register PtrRegister,
unsigned &Semantics,
617 if (SemanticsRegister.
isValid()) {
619 std::memory_order Order =
624 if (
static_cast<unsigned>(Order) == Semantics) {
625 MRI->setRegClass(SemanticsRegister, &SPIRV::iIDRegClass);
626 return SemanticsRegister;
639 unsigned Sz = Call->Arguments.size() - ImmArgs.size();
640 for (
unsigned i = 0; i < Sz; ++i)
641 MIB.addUse(Call->Arguments[i]);
650 if (Call->isSpirvOp())
653 assert(Call->Arguments.size() == 2 &&
654 "Need 2 arguments for atomic init translation");
656 .
addUse(Call->Arguments[0])
657 .
addUse(Call->Arguments[1]);
666 if (Call->isSpirvOp())
669 Register PtrRegister = Call->Arguments[0];
674 Call->Arguments.size() > 1
678 if (Call->Arguments.size() > 2) {
680 MemSemanticsReg = Call->Arguments[2];
683 SPIRV::MemorySemantics::SequentiallyConsistent |
689 .
addDef(Call->ReturnRegister)
701 if (Call->isSpirvOp())
707 Register PtrRegister = Call->Arguments[0];
709 SPIRV::MemorySemantics::SequentiallyConsistent |
716 .
addUse(Call->Arguments[1]);
724 if (Call->isSpirvOp())
728 bool IsCmpxchg = Call->Builtin->Name.contains(
"cmpxchg");
731 Register ObjectPtr = Call->Arguments[0];
732 Register ExpectedArg = Call->Arguments[1];
733 Register Desired = Call->Arguments[2];
735 LLT DesiredLLT =
MRI->getType(Desired);
738 SPIRV::OpTypePointer);
741 assert(IsCmpxchg ? ExpectedType == SPIRV::OpTypeInt
742 : ExpectedType == SPIRV::OpTypePointer);
747 auto StorageClass =
static_cast<SPIRV::StorageClass::StorageClass
>(
755 ? SPIRV::MemorySemantics::None
756 : SPIRV::MemorySemantics::SequentiallyConsistent | MemSemStorage;
759 ? SPIRV::MemorySemantics::None
760 : SPIRV::MemorySemantics::SequentiallyConsistent | MemSemStorage;
761 if (Call->Arguments.size() >= 4) {
762 assert(Call->Arguments.size() >= 5 &&
763 "Need 5+ args for explicit atomic cmpxchg");
770 if (
static_cast<unsigned>(MemOrdEq) == MemSemEqual)
771 MemSemEqualReg = Call->Arguments[3];
772 if (
static_cast<unsigned>(MemOrdNeq) == MemSemEqual)
773 MemSemUnequalReg = Call->Arguments[4];
777 if (!MemSemUnequalReg.
isValid())
781 auto Scope = IsCmpxchg ? SPIRV::Scope::Workgroup : SPIRV::Scope::Device;
782 if (Call->Arguments.size() >= 6) {
783 assert(Call->Arguments.size() == 6 &&
784 "Extra args for explicit atomic cmpxchg");
785 auto ClScope =
static_cast<SPIRV::CLMemoryScope
>(
788 if (ClScope ==
static_cast<unsigned>(Scope))
789 ScopeReg = Call->Arguments[5];
799 Register Tmp = !IsCmpxchg ?
MRI->createGenericVirtualRegister(DesiredLLT)
800 : Call->ReturnRegister;
801 if (!
MRI->getRegClassOrNull(Tmp))
825 if (Call->isSpirvOp())
831 Call->Arguments.size() >= 4 ? Call->Arguments[3] :
Register();
833 assert(Call->Arguments.size() <= 4 &&
834 "Too many args for explicit atomic RMW");
835 ScopeRegister =
buildScopeReg(ScopeRegister, SPIRV::Scope::Workgroup,
836 MIRBuilder, GR,
MRI);
838 Register PtrRegister = Call->Arguments[0];
839 unsigned Semantics = SPIRV::MemorySemantics::None;
841 Call->Arguments.size() >= 3 ? Call->Arguments[2] :
Register();
843 Semantics, MIRBuilder, GR);
844 Register ValueReg = Call->Arguments[1];
847 if (Call->ReturnType->getOpcode() == SPIRV::OpTypeFloat) {
848 if (Opcode == SPIRV::OpAtomicIAdd) {
849 Opcode = SPIRV::OpAtomicFAddEXT;
850 }
else if (Opcode == SPIRV::OpAtomicISub) {
853 Opcode = SPIRV::OpAtomicFAddEXT;
855 MRI->createGenericVirtualRegister(
MRI->getType(ValueReg));
864 ValueReg = NegValueReg;
868 .
addDef(Call->ReturnRegister)
882 assert(Call->Arguments.size() == 4 &&
883 "Wrong number of atomic floating-type builtin");
884 Register PtrReg = Call->Arguments[0];
885 Register ScopeReg = Call->Arguments[1];
886 Register MemSemanticsReg = Call->Arguments[2];
887 Register ValueReg = Call->Arguments[3];
889 .
addDef(Call->ReturnRegister)
903 bool IsSet = Opcode == SPIRV::OpAtomicFlagTestAndSet;
905 if (Call->isSpirvOp())
910 Register PtrRegister = Call->Arguments[0];
911 unsigned Semantics = SPIRV::MemorySemantics::SequentiallyConsistent;
913 Call->Arguments.size() >= 2 ? Call->Arguments[1] :
Register();
915 Semantics, MIRBuilder, GR);
917 assert((Opcode != SPIRV::OpAtomicFlagClear ||
918 (Semantics != SPIRV::MemorySemantics::Acquire &&
919 Semantics != SPIRV::MemorySemantics::AcquireRelease)) &&
920 "Invalid memory order argument!");
923 Call->Arguments.size() >= 3 ? Call->Arguments[2] :
Register();
943 if ((Opcode == SPIRV::OpControlBarrierArriveINTEL ||
944 Opcode == SPIRV::OpControlBarrierWaitINTEL) &&
945 !ST->canUseExtension(SPIRV::Extension::SPV_INTEL_split_barrier)) {
946 std::string DiagMsg = std::string(Builtin->
Name) +
947 ": the builtin requires the following SPIR-V "
948 "extension: SPV_INTEL_split_barrier";
952 if (Call->isSpirvOp())
957 unsigned MemSemantics = SPIRV::MemorySemantics::None;
959 if (MemFlags & SPIRV::CLK_LOCAL_MEM_FENCE)
960 MemSemantics |= SPIRV::MemorySemantics::WorkgroupMemory;
962 if (MemFlags & SPIRV::CLK_GLOBAL_MEM_FENCE)
963 MemSemantics |= SPIRV::MemorySemantics::CrossWorkgroupMemory;
965 if (MemFlags & SPIRV::CLK_IMAGE_MEM_FENCE)
966 MemSemantics |= SPIRV::MemorySemantics::ImageMemory;
968 if (Opcode == SPIRV::OpMemoryBarrier)
972 else if (Opcode == SPIRV::OpControlBarrierArriveINTEL)
973 MemSemantics |= SPIRV::MemorySemantics::Release;
974 else if (Opcode == SPIRV::OpControlBarrierWaitINTEL)
975 MemSemantics |= SPIRV::MemorySemantics::Acquire;
977 MemSemantics |= SPIRV::MemorySemantics::SequentiallyConsistent;
980 MemFlags == MemSemantics
984 SPIRV::Scope::Scope Scope = SPIRV::Scope::Workgroup;
985 SPIRV::Scope::Scope MemScope = Scope;
986 if (Call->Arguments.size() >= 2) {
988 ((Opcode != SPIRV::OpMemoryBarrier && Call->Arguments.size() == 2) ||
989 (Opcode == SPIRV::OpMemoryBarrier && Call->Arguments.size() == 3)) &&
990 "Extra args for explicitly scoped barrier");
991 Register ScopeArg = (Opcode == SPIRV::OpMemoryBarrier) ? Call->Arguments[2]
992 : Call->Arguments[1];
993 SPIRV::CLMemoryScope CLScope =
996 if (!(MemFlags & SPIRV::CLK_LOCAL_MEM_FENCE) ||
997 (Opcode == SPIRV::OpMemoryBarrier))
999 if (CLScope ==
static_cast<unsigned>(Scope))
1000 ScopeReg = Call->Arguments[1];
1007 if (Opcode != SPIRV::OpMemoryBarrier)
1009 MIB.
addUse(MemSemanticsReg);
1021 if ((Opcode == SPIRV::OpBitFieldInsert ||
1022 Opcode == SPIRV::OpBitFieldSExtract ||
1023 Opcode == SPIRV::OpBitFieldUExtract || Opcode == SPIRV::OpBitReverse) &&
1024 !ST->canUseExtension(SPIRV::Extension::SPV_KHR_bit_instructions)) {
1025 std::string DiagMsg = std::string(Builtin->
Name) +
1026 ": the builtin requires the following SPIR-V "
1027 "extension: SPV_KHR_bit_instructions";
1032 if (Call->isSpirvOp())
1037 .
addDef(Call->ReturnRegister)
1039 for (
unsigned i = 0; i < Call->Arguments.size(); ++i)
1040 MIB.
addUse(Call->Arguments[i]);
1051 if (Call->isSpirvOp())
1056 .
addDef(Call->ReturnRegister)
1058 .
addUse(Call->Arguments[0]);
1068 if (Call->isSpirvOp())
1073 .
addDef(Call->ReturnRegister)
1075 for (
unsigned i = 0; i < Call->Arguments.size(); ++i)
1076 MIB.
addUse(Call->Arguments[i]);
1087 if (Call->isSpirvOp())
1091 .
addDef(Call->ReturnRegister)
1093 for (
unsigned i = 0; i < Call->Arguments.size(); ++i)
1094 MIB.
addUse(Call->Arguments[i]);
1101 case SPIRV::Dim::DIM_1D:
1102 case SPIRV::Dim::DIM_Buffer:
1104 case SPIRV::Dim::DIM_2D:
1105 case SPIRV::Dim::DIM_Cube:
1106 case SPIRV::Dim::DIM_Rect:
1108 case SPIRV::Dim::DIM_3D:
1121 return arrayed ? numComps + 1 : numComps;
1134 SPIRV::lookupExtendedBuiltin(Builtin->
Name, Builtin->
Set)->Number;
1139 .
addDef(Call->ReturnRegister)
1141 .
addImm(
static_cast<uint32_t>(SPIRV::InstructionSet::OpenCL_std))
1144 for (
auto Argument : Call->Arguments)
1155 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
1159 std::tie(CompareRegister, RelationType) =
1167 for (
auto Argument : Call->Arguments)
1171 return buildSelectInst(MIRBuilder, Call->ReturnRegister, CompareRegister,
1172 Call->ReturnType, GR);
1180 SPIRV::lookupGroupBuiltin(Builtin->
Name);
1183 if (Call->isSpirvOp()) {
1186 if (GroupBuiltin->
Opcode ==
1187 SPIRV::OpSubgroupMatrixMultiplyAccumulateINTEL &&
1188 Call->Arguments.size() > 4)
1195 Register GroupOpReg = Call->Arguments[1];
1197 if (!
MI ||
MI->getOpcode() != TargetOpcode::G_CONSTANT)
1199 "Group Operation parameter must be an integer constant");
1200 uint64_t GrpOp =
MI->getOperand(1).getCImm()->getValue().getZExtValue();
1201 Register ScopeReg = Call->Arguments[0];
1203 .
addDef(Call->ReturnRegister)
1207 for (
unsigned i = 2; i < Call->Arguments.size(); ++i)
1208 MIB.
addUse(Call->Arguments[i]);
1215 Register BoolReg = Call->Arguments[0];
1220 if (ArgInstruction->
getOpcode() == TargetOpcode::G_CONSTANT) {
1221 if (BoolRegType->
getOpcode() != SPIRV::OpTypeBool)
1225 if (BoolRegType->
getOpcode() == SPIRV::OpTypeInt) {
1227 MRI->setRegClass(Arg0, &SPIRV::iIDRegClass);
1234 }
else if (BoolRegType->
getOpcode() != SPIRV::OpTypeBool) {
1241 Register GroupResultRegister = Call->ReturnRegister;
1242 SPIRVType *GroupResultType = Call->ReturnType;
1246 const bool HasBoolReturnTy =
1251 if (HasBoolReturnTy)
1252 std::tie(GroupResultRegister, GroupResultType) =
1255 auto Scope = Builtin->
Name.
starts_with(
"sub_group") ? SPIRV::Scope::Subgroup
1256 : SPIRV::Scope::Workgroup;
1260 if (GroupBuiltin->
Opcode == SPIRV::OpGroupBroadcast &&
1261 Call->Arguments.size() > 2) {
1267 Register ElemReg = Call->Arguments[1];
1269 if (!ElemType || ElemType->
getOpcode() != SPIRV::OpTypeInt)
1271 unsigned VecLen = Call->Arguments.size() - 1;
1272 VecReg =
MRI->createGenericVirtualRegister(
1274 MRI->setRegClass(VecReg, &SPIRV::vIDRegClass);
1280 for (
unsigned i = 1; i < Call->Arguments.size(); i++) {
1281 MIB.
addUse(Call->Arguments[i]);
1290 .
addDef(GroupResultRegister)
1296 if (Call->Arguments.size() > 0) {
1302 for (
unsigned i = 1; i < Call->Arguments.size(); i++)
1303 MIB.addUse(Call->Arguments[i]);
1307 if (HasBoolReturnTy)
1308 buildSelectInst(MIRBuilder, Call->ReturnRegister, GroupResultRegister,
1309 Call->ReturnType, GR);
1320 SPIRV::lookupIntelSubgroupsBuiltin(Builtin->
Name);
1322 if (IntelSubgroups->
IsMedia &&
1323 !ST->canUseExtension(SPIRV::Extension::SPV_INTEL_media_block_io)) {
1324 std::string DiagMsg = std::string(Builtin->
Name) +
1325 ": the builtin requires the following SPIR-V "
1326 "extension: SPV_INTEL_media_block_io";
1328 }
else if (!IntelSubgroups->
IsMedia &&
1329 !ST->canUseExtension(SPIRV::Extension::SPV_INTEL_subgroups)) {
1330 std::string DiagMsg = std::string(Builtin->
Name) +
1331 ": the builtin requires the following SPIR-V "
1332 "extension: SPV_INTEL_subgroups";
1337 if (Call->isSpirvOp()) {
1338 bool IsSet = OpCode != SPIRV::OpSubgroupBlockWriteINTEL &&
1339 OpCode != SPIRV::OpSubgroupImageBlockWriteINTEL &&
1340 OpCode != SPIRV::OpSubgroupImageMediaBlockWriteINTEL;
1346 if (IntelSubgroups->
IsBlock) {
1349 if (Arg0Type->getOpcode() == SPIRV::OpTypeImage) {
1355 case SPIRV::OpSubgroupBlockReadINTEL:
1356 OpCode = SPIRV::OpSubgroupImageBlockReadINTEL;
1358 case SPIRV::OpSubgroupBlockWriteINTEL:
1359 OpCode = SPIRV::OpSubgroupImageBlockWriteINTEL;
1380 .
addDef(Call->ReturnRegister)
1382 for (
size_t i = 0; i < Call->Arguments.size(); ++i)
1383 MIB.
addUse(Call->Arguments[i]);
1393 if (!ST->canUseExtension(
1394 SPIRV::Extension::SPV_KHR_uniform_group_instructions)) {
1395 std::string DiagMsg = std::string(Builtin->
Name) +
1396 ": the builtin requires the following SPIR-V "
1397 "extension: SPV_KHR_uniform_group_instructions";
1401 SPIRV::lookupGroupUniformBuiltin(Builtin->
Name);
1404 Register GroupResultReg = Call->ReturnRegister;
1405 Register ScopeReg = Call->Arguments[0];
1406 Register ValueReg = Call->Arguments[2];
1409 Register ConstGroupOpReg = Call->Arguments[1];
1411 if (!Const || Const->getOpcode() != TargetOpcode::G_CONSTANT)
1413 "expect a constant group operation for a uniform group instruction",
1416 if (!ConstOperand.
isCImm())
1426 MIB.addUse(ValueReg);
1437 if (!ST->canUseExtension(SPIRV::Extension::SPV_KHR_shader_clock)) {
1438 std::string DiagMsg = std::string(Builtin->
Name) +
1439 ": the builtin requires the following SPIR-V "
1440 "extension: SPV_KHR_shader_clock";
1444 Register ResultReg = Call->ReturnRegister;
1447 SPIRV::Scope::Scope ScopeArg =
1449 .
EndsWith(
"device", SPIRV::Scope::Scope::Device)
1450 .
EndsWith(
"work_group", SPIRV::Scope::Scope::Workgroup)
1451 .
EndsWith(
"sub_group", SPIRV::Scope::Scope::Subgroup);
1491 SPIRV::BuiltIn::BuiltIn BuiltinValue,
1493 Register IndexRegister = Call->Arguments[0];
1494 const unsigned ResultWidth = Call->ReturnType->getOperand(1).getImm();
1502 Register ToTruncate = Call->ReturnRegister;
1505 bool IsConstantIndex =
1506 IndexInstruction->getOpcode() == TargetOpcode::G_CONSTANT;
1511 Register DefaultReg = Call->ReturnRegister;
1512 if (PointerSize != ResultWidth) {
1513 DefaultReg =
MRI->createGenericVirtualRegister(
LLT::scalar(PointerSize));
1514 MRI->setRegClass(DefaultReg, &SPIRV::iIDRegClass);
1516 MIRBuilder.
getMF());
1517 ToTruncate = DefaultReg;
1521 MIRBuilder.
buildCopy(DefaultReg, NewRegister);
1529 Register Extracted = Call->ReturnRegister;
1530 if (!IsConstantIndex || PointerSize != ResultWidth) {
1531 Extracted =
MRI->createGenericVirtualRegister(
LLT::scalar(PointerSize));
1532 MRI->setRegClass(Extracted, &SPIRV::iIDRegClass);
1539 ExtractInst.
addUse(LoadedVector).
addUse(IndexRegister);
1542 if (!IsConstantIndex) {
1551 MRI->setRegClass(CompareRegister, &SPIRV::iIDRegClass);
1565 Register SelectionResult = Call->ReturnRegister;
1566 if (PointerSize != ResultWidth) {
1569 MRI->setRegClass(SelectionResult, &SPIRV::iIDRegClass);
1571 MIRBuilder.
getMF());
1574 MIRBuilder.
buildSelect(SelectionResult, CompareRegister, Extracted,
1576 ToTruncate = SelectionResult;
1578 ToTruncate = Extracted;
1582 if (PointerSize != ResultWidth)
1592 SPIRV::BuiltIn::BuiltIn
Value =
1593 SPIRV::lookupGetBuiltin(Builtin->
Name, Builtin->
Set)->
Value;
1595 if (
Value == SPIRV::BuiltIn::GlobalInvocationId)
1601 if (Call->ReturnType->getOpcode() == SPIRV::OpTypeVector)
1608 LLType, Call->ReturnRegister);
1617 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
1620 case SPIRV::OpStore:
1622 case SPIRV::OpAtomicLoad:
1624 case SPIRV::OpAtomicStore:
1626 case SPIRV::OpAtomicCompareExchange:
1627 case SPIRV::OpAtomicCompareExchangeWeak:
1630 case SPIRV::OpAtomicIAdd:
1631 case SPIRV::OpAtomicISub:
1632 case SPIRV::OpAtomicOr:
1633 case SPIRV::OpAtomicXor:
1634 case SPIRV::OpAtomicAnd:
1635 case SPIRV::OpAtomicExchange:
1637 case SPIRV::OpMemoryBarrier:
1639 case SPIRV::OpAtomicFlagTestAndSet:
1640 case SPIRV::OpAtomicFlagClear:
1643 if (Call->isSpirvOp())
1655 unsigned Opcode = SPIRV::lookupAtomicFloatingBuiltin(Builtin->
Name)->Opcode;
1658 case SPIRV::OpAtomicFAddEXT:
1659 case SPIRV::OpAtomicFMinEXT:
1660 case SPIRV::OpAtomicFMaxEXT:
1673 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
1684 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
1686 if (Opcode == SPIRV::OpGenericCastToPtrExplicit) {
1687 SPIRV::StorageClass::StorageClass ResSC =
1693 .
addDef(Call->ReturnRegister)
1695 .
addUse(Call->Arguments[0])
1698 MIRBuilder.
buildInstr(TargetOpcode::G_ADDRSPACE_CAST)
1699 .
addDef(Call->ReturnRegister)
1700 .
addUse(Call->Arguments[0]);
1709 if (Call->isSpirvOp())
1714 SPIRV::OpTypeVector;
1716 uint32_t OC = IsVec ? SPIRV::OpDot : SPIRV::OpFMulS;
1717 bool IsSwapReq =
false;
1722 (ST->canUseExtension(SPIRV::Extension::SPV_KHR_integer_dot_product) ||
1726 SPIRV::lookupIntegerDotProductBuiltin(Builtin->
Name);
1736 bool IsFirstSigned = TypeStrs[0].trim()[0] !=
'u';
1737 bool IsSecondSigned = TypeStrs[1].trim()[0] !=
'u';
1739 if (Call->BuiltinName ==
"dot") {
1740 if (IsFirstSigned && IsSecondSigned)
1742 else if (!IsFirstSigned && !IsSecondSigned)
1745 OC = SPIRV::OpSUDot;
1749 }
else if (Call->BuiltinName ==
"dot_acc_sat") {
1750 if (IsFirstSigned && IsSecondSigned)
1751 OC = SPIRV::OpSDotAccSat;
1752 else if (!IsFirstSigned && !IsSecondSigned)
1753 OC = SPIRV::OpUDotAccSat;
1755 OC = SPIRV::OpSUDotAccSat;
1764 .
addDef(Call->ReturnRegister)
1768 MIB.
addUse(Call->Arguments[1]);
1769 MIB.
addUse(Call->Arguments[0]);
1771 for (
size_t i = 2; i < Call->Arguments.size(); ++i)
1772 MIB.
addUse(Call->Arguments[i]);
1774 for (
size_t i = 0; i < Call->Arguments.size(); ++i)
1775 MIB.
addUse(Call->Arguments[i]);
1780 if (!IsVec && OC != SPIRV::OpFMulS)
1790 SPIRV::BuiltIn::BuiltIn
Value =
1791 SPIRV::lookupGetBuiltin(Builtin->
Name, Builtin->
Set)->
Value;
1794 assert(Call->ReturnType->getOpcode() == SPIRV::OpTypeInt);
1798 MIRBuilder, Call->ReturnType, GR,
Value, LLType, Call->ReturnRegister,
1813 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
1815 Register SRetReg = Call->Arguments[0];
1820 if (RetType->
getOpcode() != SPIRV::OpTypeStruct)
1822 "overflow builtins");
1826 if (!OpType1 || !OpType2 || OpType1 != OpType2)
1828 if (OpType1->
getOpcode() == SPIRV::OpTypeVector)
1830 case SPIRV::OpIAddCarryS:
1831 Opcode = SPIRV::OpIAddCarryV;
1833 case SPIRV::OpISubBorrowS:
1834 Opcode = SPIRV::OpISubBorrowV;
1839 Register ResReg =
MRI->createVirtualRegister(&SPIRV::iIDRegClass);
1841 MRI->getRegClassOrNull(Call->Arguments[1])) {
1842 MRI->setRegClass(ResReg, DstRC);
1843 MRI->setType(ResReg,
MRI->getType(Call->Arguments[1]));
1851 .
addUse(Call->Arguments[1])
1852 .
addUse(Call->Arguments[2]);
1861 SPIRV::BuiltIn::BuiltIn
Value =
1862 SPIRV::lookupGetBuiltin(Call->Builtin->Name, Call->Builtin->Set)->
Value;
1863 const bool IsDefaultOne = (
Value == SPIRV::BuiltIn::GlobalSize ||
1864 Value == SPIRV::BuiltIn::NumWorkgroups ||
1865 Value == SPIRV::BuiltIn::WorkgroupSize ||
1866 Value == SPIRV::BuiltIn::EnqueuedWorkgroupSize);
1876 SPIRV::lookupImageQueryBuiltin(Builtin->
Name, Builtin->
Set)->Component;
1880 unsigned NumExpectedRetComponents =
1881 Call->ReturnType->getOpcode() == SPIRV::OpTypeVector
1882 ? Call->ReturnType->getOperand(2).getImm()
1887 Register QueryResult = Call->ReturnRegister;
1888 SPIRVType *QueryResultType = Call->ReturnType;
1889 if (NumExpectedRetComponents != NumActualRetComponents) {
1890 unsigned Bitwidth = Call->ReturnType->getOpcode() == SPIRV::OpTypeInt
1891 ? Call->ReturnType->getOperand(1).getImm()
1898 IntTy, NumActualRetComponents, MIRBuilder,
true);
1903 IsDimBuf ? SPIRV::OpImageQuerySize : SPIRV::OpImageQuerySizeLod;
1907 .
addUse(Call->Arguments[0]);
1910 if (NumExpectedRetComponents == NumActualRetComponents)
1912 if (NumExpectedRetComponents == 1) {
1914 unsigned ExtractedComposite =
1915 Component == 3 ? NumActualRetComponents - 1 : Component;
1916 assert(ExtractedComposite < NumActualRetComponents &&
1917 "Invalid composite index!");
1920 if (QueryResultType->
getOpcode() == SPIRV::OpTypeVector) {
1922 if (TypeReg != NewTypeReg &&
1924 TypeReg = NewTypeReg;
1926 MIRBuilder.
buildInstr(SPIRV::OpCompositeExtract)
1927 .
addDef(Call->ReturnRegister)
1930 .
addImm(ExtractedComposite);
1931 if (NewType !=
nullptr)
1936 auto MIB = MIRBuilder.
buildInstr(SPIRV::OpVectorShuffle)
1937 .
addDef(Call->ReturnRegister)
1941 for (
unsigned i = 0; i < NumExpectedRetComponents; ++i)
1942 MIB.
addImm(i < NumActualRetComponents ? i : 0xffffffff);
1950 assert(Call->ReturnType->getOpcode() == SPIRV::OpTypeInt &&
1951 "Image samples query result must be of int type!");
1956 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
1958 Register Image = Call->Arguments[0];
1959 SPIRV::Dim::Dim ImageDimensionality =
static_cast<SPIRV::Dim::Dim
>(
1961 (void)ImageDimensionality;
1964 case SPIRV::OpImageQuerySamples:
1965 assert(ImageDimensionality == SPIRV::Dim::DIM_2D &&
1966 "Image must be of 2D dimensionality");
1968 case SPIRV::OpImageQueryLevels:
1969 assert((ImageDimensionality == SPIRV::Dim::DIM_1D ||
1970 ImageDimensionality == SPIRV::Dim::DIM_2D ||
1971 ImageDimensionality == SPIRV::Dim::DIM_3D ||
1972 ImageDimensionality == SPIRV::Dim::DIM_Cube) &&
1973 "Image must be of 1D/2D/3D/Cube dimensionality");
1978 .
addDef(Call->ReturnRegister)
1985static SPIRV::SamplerAddressingMode::SamplerAddressingMode
1987 switch (Bitmask & SPIRV::CLK_ADDRESS_MODE_MASK) {
1988 case SPIRV::CLK_ADDRESS_CLAMP:
1989 return SPIRV::SamplerAddressingMode::Clamp;
1990 case SPIRV::CLK_ADDRESS_CLAMP_TO_EDGE:
1991 return SPIRV::SamplerAddressingMode::ClampToEdge;
1992 case SPIRV::CLK_ADDRESS_REPEAT:
1993 return SPIRV::SamplerAddressingMode::Repeat;
1994 case SPIRV::CLK_ADDRESS_MIRRORED_REPEAT:
1995 return SPIRV::SamplerAddressingMode::RepeatMirrored;
1996 case SPIRV::CLK_ADDRESS_NONE:
1997 return SPIRV::SamplerAddressingMode::None;
2004 return (Bitmask & SPIRV::CLK_NORMALIZED_COORDS_TRUE) ? 1 : 0;
2007static SPIRV::SamplerFilterMode::SamplerFilterMode
2009 if (Bitmask & SPIRV::CLK_FILTER_LINEAR)
2010 return SPIRV::SamplerFilterMode::Linear;
2011 if (Bitmask & SPIRV::CLK_FILTER_NEAREST)
2012 return SPIRV::SamplerFilterMode::Nearest;
2013 return SPIRV::SamplerFilterMode::Nearest;
2020 if (Call->isSpirvOp())
2023 Register Image = Call->Arguments[0];
2027 if (HasOclSampler) {
2028 Register Sampler = Call->Arguments[1];
2041 Register SampledImage =
MRI->createVirtualRegister(&SPIRV::iIDRegClass);
2052 if (Call->ReturnType->getOpcode() != SPIRV::OpTypeVector) {
2056 MRI->createGenericVirtualRegister(GR->
getRegType(TempType));
2059 MIRBuilder.
buildInstr(SPIRV::OpImageSampleExplicitLod)
2063 .
addUse(Call->Arguments[2])
2064 .
addImm(SPIRV::ImageOperand::Lod)
2066 MIRBuilder.
buildInstr(SPIRV::OpCompositeExtract)
2067 .
addDef(Call->ReturnRegister)
2072 MIRBuilder.
buildInstr(SPIRV::OpImageSampleExplicitLod)
2073 .
addDef(Call->ReturnRegister)
2076 .
addUse(Call->Arguments[2])
2077 .
addImm(SPIRV::ImageOperand::Lod)
2080 }
else if (HasMsaa) {
2082 .
addDef(Call->ReturnRegister)
2085 .
addUse(Call->Arguments[1])
2086 .
addImm(SPIRV::ImageOperand::Sample)
2087 .
addUse(Call->Arguments[2]);
2090 .
addDef(Call->ReturnRegister)
2093 .
addUse(Call->Arguments[1]);
2101 if (Call->isSpirvOp())
2105 .
addUse(Call->Arguments[0])
2106 .
addUse(Call->Arguments[1])
2107 .
addUse(Call->Arguments[2]);
2116 if (Call->Builtin->Name.contains_insensitive(
2117 "__translate_sampler_initializer")) {
2124 return Sampler.isValid();
2125 }
else if (Call->Builtin->Name.contains_insensitive(
"__spirv_SampledImage")) {
2127 Register Image = Call->Arguments[0];
2132 Call->ReturnRegister.isValid()
2133 ? Call->ReturnRegister
2134 :
MRI->createVirtualRegister(&SPIRV::iIDRegClass);
2139 .
addUse(Call->Arguments[1]);
2141 }
else if (Call->Builtin->Name.contains_insensitive(
2142 "__spirv_ImageSampleExplicitLod")) {
2144 std::string ReturnType = DemangledCall.
str();
2145 if (DemangledCall.
contains(
"_R")) {
2146 ReturnType = ReturnType.substr(ReturnType.find(
"_R") + 2);
2147 ReturnType = ReturnType.substr(0, ReturnType.find(
'('));
2154 std::string DiagMsg =
2155 "Unable to recognize SPIRV type name: " + ReturnType;
2158 MIRBuilder.
buildInstr(SPIRV::OpImageSampleExplicitLod)
2159 .
addDef(Call->ReturnRegister)
2161 .
addUse(Call->Arguments[0])
2162 .
addUse(Call->Arguments[1])
2163 .
addImm(SPIRV::ImageOperand::Lod)
2164 .
addUse(Call->Arguments[3]);
2172 MIRBuilder.
buildSelect(Call->ReturnRegister, Call->Arguments[0],
2173 Call->Arguments[1], Call->Arguments[2]);
2181 SPIRV::OpCompositeConstructContinuedINTEL,
2182 Call->Arguments, Call->ReturnRegister,
2192 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2193 bool IsSet = Opcode != SPIRV::OpCooperativeMatrixStoreKHR &&
2194 Opcode != SPIRV::OpCooperativeMatrixStoreCheckedINTEL &&
2195 Opcode != SPIRV::OpCooperativeMatrixPrefetchINTEL;
2196 unsigned ArgSz = Call->Arguments.size();
2197 unsigned LiteralIdx = 0;
2200 case SPIRV::OpCooperativeMatrixLoadKHR:
2201 LiteralIdx = ArgSz > 3 ? 3 : 0;
2203 case SPIRV::OpCooperativeMatrixStoreKHR:
2204 LiteralIdx = ArgSz > 4 ? 4 : 0;
2206 case SPIRV::OpCooperativeMatrixLoadCheckedINTEL:
2207 LiteralIdx = ArgSz > 7 ? 7 : 0;
2209 case SPIRV::OpCooperativeMatrixStoreCheckedINTEL:
2210 LiteralIdx = ArgSz > 8 ? 8 : 0;
2213 case SPIRV::OpCooperativeMatrixMulAddKHR:
2214 LiteralIdx = ArgSz > 3 ? 3 : 0;
2220 if (Opcode == SPIRV::OpCooperativeMatrixPrefetchINTEL) {
2222 auto MIB = MIRBuilder.
buildInstr(SPIRV::OpCooperativeMatrixPrefetchINTEL)
2223 .
addUse(Call->Arguments[0])
2224 .
addUse(Call->Arguments[1])
2225 .
addUse(Call->Arguments[2])
2227 .
addUse(Call->Arguments[4]);
2229 MIB.
addUse(Call->Arguments[5]);
2239 if (Opcode == SPIRV::OpCooperativeMatrixLengthKHR) {
2244 .
addDef(Call->ReturnRegister)
2250 IsSet ? TypeReg :
Register(0), ImmArgs);
2259 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2263 case SPIRV::OpSpecConstant: {
2267 buildOpDecorate(Call->ReturnRegister, MIRBuilder, SPIRV::Decoration::SpecId,
2270 Register ConstRegister = Call->Arguments[1];
2273 (Const->getOpcode() == TargetOpcode::G_CONSTANT ||
2274 Const->getOpcode() == TargetOpcode::G_FCONSTANT) &&
2275 "Argument should be either an int or floating-point constant");
2278 if (Call->ReturnType->getOpcode() == SPIRV::OpTypeBool) {
2279 assert(ConstOperand.
isCImm() &&
"Int constant operand is expected");
2281 ? SPIRV::OpSpecConstantTrue
2282 : SPIRV::OpSpecConstantFalse;
2285 .
addDef(Call->ReturnRegister)
2288 if (Call->ReturnType->getOpcode() != SPIRV::OpTypeBool) {
2289 if (Const->getOpcode() == TargetOpcode::G_CONSTANT)
2296 case SPIRV::OpSpecConstantComposite: {
2298 SPIRV::OpSpecConstantCompositeContinuedINTEL,
2299 Call->Arguments, Call->ReturnRegister,
2314 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2325 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2337 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2348 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2363 Register TmpReg =
MRI->createVirtualRegister(&SPIRV::iIDRegClass);
2367 unsigned NumArgs = Call->Arguments.size();
2369 Register GlobalWorkSize = Call->Arguments[NumArgs < 4 ? 1 : 2];
2371 NumArgs == 2 ?
Register(0) : Call->Arguments[NumArgs < 4 ? 2 : 3];
2372 Register GlobalWorkOffset = NumArgs <= 3 ?
Register(0) : Call->Arguments[1];
2376 if (SpvTy->
getOpcode() == SPIRV::OpTypePointer) {
2382 unsigned Size = Call->Builtin->Name ==
"ndrange_3D" ? 3 : 2;
2387 FieldTy, MIRBuilder, SPIRV::AccessQualifier::ReadWrite,
true);
2388 GlobalWorkSize =
MRI->createVirtualRegister(&SPIRV::iIDRegClass);
2397 SpvFieldTy, *ST.getInstrInfo());
2402 LocalWorkSize = Const;
2403 if (!GlobalWorkOffset.
isValid())
2404 GlobalWorkOffset = Const;
2412 .
addUse(GlobalWorkOffset);
2414 .
addUse(Call->Arguments[0])
2426 SPIRV::AccessQualifier::ReadWrite,
true);
2434 bool IsSpirvOp = Call->isSpirvOp();
2435 bool HasEvents = Call->Builtin->Name.contains(
"events") || IsSpirvOp;
2442 if (Call->Builtin->Name.contains(
"_varargs") || IsSpirvOp) {
2443 const unsigned LocalSizeArrayIdx = HasEvents ? 9 : 6;
2444 Register GepReg = Call->Arguments[LocalSizeArrayIdx];
2451 assert(LocalSizeTy &&
"Local size type is expected");
2453 cast<ArrayType>(LocalSizeTy)->getNumElements();
2457 Int32Ty, MIRBuilder, SPIRV::StorageClass::Function);
2458 for (
unsigned I = 0;
I < LocalSizeNum; ++
I) {
2460 MRI->setType(
Reg, LLType);
2474 auto MIB = MIRBuilder.
buildInstr(SPIRV::OpEnqueueKernel)
2475 .
addDef(Call->ReturnRegister)
2479 const unsigned BlockFIdx = HasEvents ? 6 : 3;
2480 for (
unsigned i = 0; i < BlockFIdx; i++)
2481 MIB.addUse(Call->Arguments[i]);
2488 MIB.addUse(NullPtr);
2489 MIB.addUse(NullPtr);
2497 Register BlockLiteralReg = Call->Arguments[BlockFIdx + 1];
2499 MIB.addUse(BlockLiteralReg);
2509 for (
unsigned i = 0; i < LocalSizes.
size(); i++)
2510 MIB.addUse(LocalSizes[i]);
2520 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2523 case SPIRV::OpRetainEvent:
2524 case SPIRV::OpReleaseEvent:
2526 case SPIRV::OpCreateUserEvent:
2527 case SPIRV::OpGetDefaultQueue:
2529 .
addDef(Call->ReturnRegister)
2531 case SPIRV::OpIsValidEvent:
2533 .
addDef(Call->ReturnRegister)
2535 .
addUse(Call->Arguments[0]);
2536 case SPIRV::OpSetUserEventStatus:
2538 .
addUse(Call->Arguments[0])
2539 .
addUse(Call->Arguments[1]);
2540 case SPIRV::OpCaptureEventProfilingInfo:
2542 .
addUse(Call->Arguments[0])
2543 .
addUse(Call->Arguments[1])
2544 .
addUse(Call->Arguments[2]);
2545 case SPIRV::OpBuildNDRange:
2547 case SPIRV::OpEnqueueKernel:
2560 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2562 bool IsSet = Opcode == SPIRV::OpGroupAsyncCopy;
2564 if (Call->isSpirvOp())
2571 case SPIRV::OpGroupAsyncCopy: {
2573 Call->ReturnType->getOpcode() == SPIRV::OpTypeEvent
2577 unsigned NumArgs = Call->Arguments.size();
2578 Register EventReg = Call->Arguments[NumArgs - 1];
2580 .
addDef(Call->ReturnRegister)
2583 .
addUse(Call->Arguments[0])
2584 .
addUse(Call->Arguments[1])
2585 .
addUse(Call->Arguments[2])
2586 .
addUse(Call->Arguments.size() > 4
2587 ? Call->Arguments[3]
2590 if (NewType !=
nullptr)
2595 case SPIRV::OpGroupWaitEvents:
2598 .
addUse(Call->Arguments[0])
2599 .
addUse(Call->Arguments[1]);
2611 SPIRV::lookupConvertBuiltin(Call->Builtin->Name, Call->Builtin->Set);
2613 if (!Builtin && Call->isSpirvOp()) {
2616 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2621 assert(Builtin &&
"Conversion builtin not found.");
2624 SPIRV::Decoration::SaturatedConversion, {});
2627 SPIRV::Decoration::FPRoundingMode,
2628 {(unsigned)Builtin->RoundingMode});
2630 std::string NeedExtMsg;
2631 bool IsRightComponentsNumber =
true;
2632 unsigned Opcode = SPIRV::OpNop;
2639 : SPIRV::OpSatConvertSToU;
2642 : SPIRV::OpSConvert;
2644 SPIRV::OpTypeFloat)) {
2649 if (!ST->canUseExtension(
2650 SPIRV::Extension::SPV_INTEL_bfloat16_conversion))
2651 NeedExtMsg =
"SPV_INTEL_bfloat16_conversion";
2652 IsRightComponentsNumber =
2655 Opcode = SPIRV::OpConvertBF16ToFINTEL;
2657 bool IsSourceSigned =
2659 Opcode = IsSourceSigned ? SPIRV::OpConvertSToF : SPIRV::OpConvertUToF;
2663 SPIRV::OpTypeFloat)) {
2670 if (!ST->canUseExtension(
2671 SPIRV::Extension::SPV_INTEL_bfloat16_conversion))
2672 NeedExtMsg =
"SPV_INTEL_bfloat16_conversion";
2673 IsRightComponentsNumber =
2676 Opcode = SPIRV::OpConvertFToBF16INTEL;
2679 : SPIRV::OpConvertFToU;
2682 SPIRV::OpTypeFloat)) {
2686 if (!ST->canUseExtension(
2687 SPIRV::Extension::SPV_INTEL_tensor_float32_conversion))
2688 NeedExtMsg =
"SPV_INTEL_tensor_float32_conversion";
2689 IsRightComponentsNumber =
2692 Opcode = SPIRV::OpRoundFToTF32INTEL;
2695 Opcode = SPIRV::OpFConvert;
2700 if (!NeedExtMsg.empty()) {
2701 std::string DiagMsg = std::string(Builtin->
Name) +
2702 ": the builtin requires the following SPIR-V "
2707 if (!IsRightComponentsNumber) {
2708 std::string DiagMsg =
2709 std::string(Builtin->
Name) +
2710 ": result and argument must have the same number of components";
2713 assert(Opcode != SPIRV::OpNop &&
2714 "Conversion between the types not implemented!");
2717 .
addDef(Call->ReturnRegister)
2719 .
addUse(Call->Arguments[0]);
2728 SPIRV::lookupVectorLoadStoreBuiltin(Call->Builtin->Name,
2729 Call->Builtin->Set);
2733 .
addDef(Call->ReturnRegister)
2735 .
addImm(
static_cast<uint32_t>(SPIRV::InstructionSet::OpenCL_std))
2737 for (
auto Argument : Call->Arguments)
2755 SPIRV::lookupNativeBuiltin(Builtin->
Name, Builtin->
Set)->Opcode;
2756 bool IsLoad = Opcode == SPIRV::OpLoad;
2760 MIB.
addDef(Call->ReturnRegister);
2764 MIB.
addUse(Call->Arguments[0]);
2768 MIB.addUse(Call->Arguments[1]);
2770 unsigned NumArgs = Call->Arguments.size();
2771 if ((IsLoad && NumArgs >= 2) || NumArgs >= 3)
2773 if ((IsLoad && NumArgs >= 3) || NumArgs >= 4)
2786std::tuple<int, unsigned, unsigned>
2788 SPIRV::InstructionSet::InstructionSet Set) {
2791 std::unique_ptr<const IncomingCall> Call =
2794 return std::make_tuple(-1, 0, 0);
2796 switch (Call->Builtin->Group) {
2797 case SPIRV::Relational:
2799 case SPIRV::Barrier:
2800 case SPIRV::CastToPtr:
2801 case SPIRV::ImageMiscQuery:
2802 case SPIRV::SpecConstant:
2803 case SPIRV::Enqueue:
2804 case SPIRV::AsyncCopy:
2805 case SPIRV::LoadStore:
2806 case SPIRV::CoopMatr:
2808 SPIRV::lookupNativeBuiltin(Call->Builtin->Name, Call->Builtin->Set))
2809 return std::make_tuple(Call->Builtin->Group, R->Opcode, 0);
2811 case SPIRV::Extended:
2812 if (
const auto *R = SPIRV::lookupExtendedBuiltin(Call->Builtin->Name,
2813 Call->Builtin->Set))
2814 return std::make_tuple(Call->Builtin->Group, 0, R->Number);
2816 case SPIRV::VectorLoadStore:
2817 if (
const auto *R = SPIRV::lookupVectorLoadStoreBuiltin(Call->Builtin->Name,
2818 Call->Builtin->Set))
2819 return std::make_tuple(SPIRV::Extended, 0, R->Number);
2822 if (
const auto *R = SPIRV::lookupGroupBuiltin(Call->Builtin->Name))
2823 return std::make_tuple(Call->Builtin->Group, R->Opcode, 0);
2825 case SPIRV::AtomicFloating:
2826 if (
const auto *R = SPIRV::lookupAtomicFloatingBuiltin(Call->Builtin->Name))
2827 return std::make_tuple(Call->Builtin->Group, R->Opcode, 0);
2829 case SPIRV::IntelSubgroups:
2830 if (
const auto *R = SPIRV::lookupIntelSubgroupsBuiltin(Call->Builtin->Name))
2831 return std::make_tuple(Call->Builtin->Group, R->Opcode, 0);
2833 case SPIRV::GroupUniform:
2834 if (
const auto *R = SPIRV::lookupGroupUniformBuiltin(Call->Builtin->Name))
2835 return std::make_tuple(Call->Builtin->Group, R->Opcode, 0);
2837 case SPIRV::IntegerDot:
2839 SPIRV::lookupIntegerDotProductBuiltin(Call->Builtin->Name))
2840 return std::make_tuple(Call->Builtin->Group, R->Opcode, 0);
2842 case SPIRV::WriteImage:
2843 return std::make_tuple(Call->Builtin->Group, SPIRV::OpImageWrite, 0);
2845 return std::make_tuple(Call->Builtin->Group, TargetOpcode::G_SELECT, 0);
2846 case SPIRV::Construct:
2847 return std::make_tuple(Call->Builtin->Group, SPIRV::OpCompositeConstruct,
2849 case SPIRV::KernelClock:
2850 return std::make_tuple(Call->Builtin->Group, SPIRV::OpReadClockKHR, 0);
2852 return std::make_tuple(-1, 0, 0);
2854 return std::make_tuple(-1, 0, 0);
2858 SPIRV::InstructionSet::InstructionSet Set,
2863 LLVM_DEBUG(
dbgs() <<
"Lowering builtin call: " << DemangledCall <<
"\n");
2867 assert(SpvType &&
"Inconsistent return register: expected valid type info");
2868 std::unique_ptr<const IncomingCall> Call =
2873 return std::nullopt;
2877 assert(Args.size() >= Call->Builtin->MinNumArgs &&
2878 "Too few arguments to generate the builtin");
2879 if (Call->Builtin->MaxNumArgs && Args.size() > Call->Builtin->MaxNumArgs)
2880 LLVM_DEBUG(
dbgs() <<
"More arguments provided than required!\n");
2883 switch (Call->Builtin->Group) {
2884 case SPIRV::Extended:
2886 case SPIRV::Relational:
2890 case SPIRV::Variable:
2894 case SPIRV::AtomicFloating:
2896 case SPIRV::Barrier:
2898 case SPIRV::CastToPtr:
2901 case SPIRV::IntegerDot:
2905 case SPIRV::ICarryBorrow:
2907 case SPIRV::GetQuery:
2909 case SPIRV::ImageSizeQuery:
2911 case SPIRV::ImageMiscQuery:
2913 case SPIRV::ReadImage:
2915 case SPIRV::WriteImage:
2917 case SPIRV::SampleImage:
2921 case SPIRV::Construct:
2923 case SPIRV::SpecConstant:
2925 case SPIRV::Enqueue:
2927 case SPIRV::AsyncCopy:
2929 case SPIRV::Convert:
2931 case SPIRV::VectorLoadStore:
2933 case SPIRV::LoadStore:
2935 case SPIRV::IntelSubgroups:
2937 case SPIRV::GroupUniform:
2939 case SPIRV::KernelClock:
2941 case SPIRV::CoopMatr:
2943 case SPIRV::ExtendedBitOps:
2945 case SPIRV::BindlessINTEL:
2947 case SPIRV::TernaryBitwiseINTEL:
2949 case SPIRV::Block2DLoadStore:
2960 [[maybe_unused]]
bool IsOCLBuiltinType = TypeStr.
consume_front(
"ocl_");
2961 assert(IsOCLBuiltinType &&
"Invalid OpenCL builtin prefix");
2978 unsigned VecElts = 0;
2989 TypeStr = TypeStr.
substr(0, TypeStr.
find(
']'));
3001 auto Pos1 = DemangledCall.
find(
'(');
3004 auto Pos2 = DemangledCall.
find(
')');
3007 DemangledCall.
slice(Pos1 + 1, Pos2)
3008 .
split(BuiltinArgsTypeStrs,
',', -1,
false);
3016 if (ArgIdx >= BuiltinArgsTypeStrs.
size())
3018 StringRef TypeStr = BuiltinArgsTypeStrs[ArgIdx].trim();
3027#define GET_BuiltinTypes_DECL
3028#define GET_BuiltinTypes_IMPL
3035#define GET_OpenCLTypes_DECL
3036#define GET_OpenCLTypes_IMPL
3038#include "SPIRVGenTables.inc"
3046 if (
Name.starts_with(
"void"))
3048 else if (
Name.starts_with(
"int") ||
Name.starts_with(
"uint"))
3050 else if (
Name.starts_with(
"float"))
3052 else if (
Name.starts_with(
"half"))
3065 unsigned Opcode = TypeRecord->
Opcode;
3080 "Invalid number of parameters for SPIR-V pipe builtin!");
3083 SPIRV::AccessQualifier::AccessQualifier(
3091 "Invalid number of parameters for SPIR-V coop matrices builtin!");
3093 "SPIR-V coop matrices builtin type must have a type parameter!");
3096 SPIRV::AccessQualifier::ReadWrite,
true);
3099 MIRBuilder, ExtensionType, ElemType, ExtensionType->
getIntParameter(0),
3108 OpaqueType, SPIRV::AccessQualifier::ReadOnly, MIRBuilder);
3117 "Inline SPIR-V type builtin takes an opcode, size, and alignment "
3123 if (
const TargetExtType *ParamEType = dyn_cast<TargetExtType>(Param)) {
3124 if (ParamEType->getName() ==
"spirv.IntegralConstant") {
3125 assert(ParamEType->getNumTypeParameters() == 1 &&
3126 "Inline SPIR-V integral constant builtin must have a type "
3128 assert(ParamEType->getNumIntParameters() == 1 &&
3129 "Inline SPIR-V integral constant builtin must have a "
3132 auto OperandValue = ParamEType->getIntParameter(0);
3133 auto *OperandType = ParamEType->getTypeParameter(0);
3136 OperandType, MIRBuilder, SPIRV::AccessQualifier::ReadWrite,
true);
3139 OperandValue, MIRBuilder, OperandSPIRVType,
true)));
3141 }
else if (ParamEType->getName() ==
"spirv.Literal") {
3142 assert(ParamEType->getNumTypeParameters() == 0 &&
3143 "Inline SPIR-V literal builtin does not take type "
3145 assert(ParamEType->getNumIntParameters() == 1 &&
3146 "Inline SPIR-V literal builtin must have an integer "
3149 auto OperandValue = ParamEType->getIntParameter(0);
3156 Param, MIRBuilder, SPIRV::AccessQualifier::ReadWrite,
true);
3168 "Vulkan buffers have exactly one type for the type of the buffer.");
3170 "Vulkan buffer have 2 integer parameters: storage class and is "
3174 auto SC =
static_cast<SPIRV::StorageClass::StorageClass
>(
3189 StringRef NameWithParameters = TypeName;
3196 SPIRV::lookupOpenCLType(NameWithParameters);
3199 NameWithParameters);
3207 "Unknown builtin opaque type!");
3211 if (!NameWithParameters.
contains(
'_'))
3215 unsigned BaseNameLength = NameWithParameters.
find(
'_') - 1;
3216 SplitString(NameWithParameters.
substr(BaseNameLength + 1), Parameters,
"_");
3219 bool HasTypeParameter = !
isDigit(Parameters[0][0]);
3220 if (HasTypeParameter)
3223 for (
unsigned i = HasTypeParameter ? 1 : 0; i < Parameters.size(); i++) {
3224 unsigned IntParameter = 0;
3225 bool ValidLiteral = !Parameters[i].getAsInteger(10, IntParameter);
3228 "Invalid format of SPIR-V builtin parameter literal!");
3232 NameWithParameters.
substr(0, BaseNameLength),
3233 TypeParameters, IntParameters);
3237 SPIRV::AccessQualifier::AccessQualifier AccessQual,
3259 if (
Name ==
"spirv.Type") {
3261 }
else if (
Name ==
"spirv.VulkanBuffer") {
3263 }
else if (
Name ==
"spirv.Layout") {
3277 switch (TypeRecord->
Opcode) {
3278 case SPIRV::OpTypeImage:
3281 case SPIRV::OpTypePipe:
3284 case SPIRV::OpTypeDeviceEvent:
3287 case SPIRV::OpTypeSampler:
3290 case SPIRV::OpTypeSampledImage:
3293 case SPIRV::OpTypeCooperativeMatrixKHR:
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
AMDGPU Lower Kernel Arguments
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
mir Rename Register Operands
static bool isDigit(const char C)
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
APInt bitcastToAPInt() const
static APFloat getZero(const fltSemantics &Sem, bool Negative=false)
Factory for Positive and Negative Zero.
static APInt getAllOnes(unsigned numBits)
Return an APInt of a specified width with all bits set.
uint64_t getZExtValue() const
Get zero extended value.
This class represents an incoming formal argument to a Function.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
static LLVM_ABI ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
@ ICMP_ULT
unsigned less than
const APFloat & getValueAPF() const
const APInt & getValue() const
Return the constant as an APInt value reference.
A parsed version of the target data layout string in and methods for querying it.
Tagged union holding either a T or a Error.
Class to represent fixed width SIMD vectors.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
static LLVM_ABI IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
static constexpr LLT vector(ElementCount EC, unsigned ScalarSizeInBits)
Get a low-level vector of some number of elements and element width.
static constexpr LLT scalar(unsigned SizeInBits)
Get a low-level scalar or aggregate "bag of bits".
static constexpr LLT pointer(unsigned AddressSpace, unsigned SizeInBits)
Get a low-level pointer in the given address space.
static constexpr LLT fixed_vector(unsigned NumElements, unsigned ScalarSizeInBits)
Get a low-level fixed-width vector of some number of elements and element width.
This is an important class for using LLVM in a threaded context.
static MCOperand createReg(MCRegister Reg)
static MCOperand createImm(int64_t Val)
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Helper class to build MachineInstr.
LLVMContext & getContext() const
MachineInstrBuilder buildSelect(const DstOp &Res, const SrcOp &Tst, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_SELECT Tst, Op0, Op1.
MachineInstrBuilder buildICmp(CmpInst::Predicate Pred, const DstOp &Res, const SrcOp &Op0, const SrcOp &Op1, std::optional< unsigned > Flags=std::nullopt)
Build and insert a Res = G_ICMP Pred, Op0, Op1.
MachineBasicBlock::iterator getInsertPt()
Current insertion point for new instructions.
MachineInstrBuilder buildIntrinsic(Intrinsic::ID ID, ArrayRef< Register > Res, bool HasSideEffects, bool isConvergent)
Build and insert a G_INTRINSIC instruction.
MachineInstrBuilder buildLoad(const DstOp &Res, const SrcOp &Addr, MachineMemOperand &MMO)
Build and insert Res = G_LOAD Addr, MMO.
MachineInstrBuilder buildZExtOrTrunc(const DstOp &Res, const SrcOp &Op)
Build and insert Res = G_ZEXT Op, Res = G_TRUNC Op, or Res = COPY Op depending on the differing sizes...
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
MachineRegisterInfo * getMRI()
Getter for MRI.
MachineInstrBuilder buildCopy(const DstOp &Res, const SrcOp &Op)
Build and insert Res = COPY Op.
const DataLayout & getDataLayout() const
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addUse(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register use operand.
const MachineInstrBuilder & addDef(Register RegNo, unsigned Flags=0, unsigned SubReg=0) const
Add a virtual register definition operand.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
const MachineOperand & getOperand(unsigned i) const
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
const ConstantInt * getCImm() const
bool isCImm() const
isCImm - Test if this is a MO_CImmediate operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
const MDNode * getMetadata() const
Register getReg() const
getReg - Returns the register number.
const ConstantFP * getFPImm() const
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
LLVM_ABI void setType(Register VReg, LLT Ty)
Set the low-level type of VReg to Ty.
LLVM_ABI void setRegClass(Register Reg, const TargetRegisterClass *RC)
setRegClass - Set the register class of the specified virtual register.
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
unsigned getNumVirtRegs() const
getNumVirtRegs - Return the number of virtual registers created.
static LLVM_ABI PointerType * get(Type *ElementType, unsigned AddressSpace)
This constructs a pointer to an object of the specified type in a numbered address space.
Wrapper class representing virtual and physical registers.
constexpr bool isValid() const
SPIRVType * getOrCreateOpTypePipe(MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AccQual)
SPIRVType * getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
SPIRVType * getOrCreateSPIRVBoolType(MachineIRBuilder &MIRBuilder, bool EmitIR)
void assignSPIRVTypeToVReg(SPIRVType *Type, Register VReg, const MachineFunction &MF)
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
SPIRVType * getOrCreateUnknownType(const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode, const ArrayRef< MCOperand > Operands)
unsigned getScalarOrVectorComponentCount(Register VReg) const
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
unsigned getPointerSize() const
SPIRVType * getOrCreateSPIRVPointerType(const Type *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC)
SPIRVType * getOrCreateOpTypeByOpcode(const Type *Ty, MachineIRBuilder &MIRBuilder, unsigned Opcode)
Register buildConstantFP(APFloat Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType=nullptr)
SPIRVType * getPointeeType(SPIRVType *PtrType)
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
bool isScalarOfType(Register VReg, unsigned TypeOpcode) const
Register buildGlobalVariable(Register Reg, SPIRVType *BaseType, StringRef Name, const GlobalValue *GV, SPIRV::StorageClass::StorageClass Storage, const MachineInstr *Init, bool IsConst, bool HasLinkageTy, SPIRV::LinkageType::LinkageType LinkageType, MachineIRBuilder &MIRBuilder, bool IsInstSelector)
SPIRVType * getOrCreateOpTypeSampledImage(SPIRVType *ImageType, MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateVulkanBufferType(MachineIRBuilder &MIRBuilder, Type *ElemType, SPIRV::StorageClass::StorageClass SC, bool IsWritable, bool EmitIr=false)
SPIRVType * getOrCreateSPIRVTypeByName(StringRef TypeStr, MachineIRBuilder &MIRBuilder, bool EmitIR, SPIRV::StorageClass::StorageClass SC=SPIRV::StorageClass::Function, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite)
SPIRVType * getOrCreateLayoutType(MachineIRBuilder &MIRBuilder, const TargetExtType *T, bool EmitIr=false)
Register getOrCreateConsIntVector(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR)
const TargetRegisterClass * getRegClass(SPIRVType *SpvType) const
SPIRVType * getOrCreateSPIRVVectorType(SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder, bool EmitIR)
SPIRVType * getOrCreateOpTypeCoopMatr(MachineIRBuilder &MIRBuilder, const TargetExtType *ExtensionType, const SPIRVType *ElemType, uint32_t Scope, uint32_t Rows, uint32_t Columns, uint32_t Use, bool EmitIR)
bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const
Register getOrCreateConstIntArray(uint64_t Val, size_t Num, MachineInstr &I, SPIRVType *SpvType, const SPIRVInstrInfo &TII)
SPIRVType * getOrCreateOpTypeDeviceEvent(MachineIRBuilder &MIRBuilder)
SPIRVType * getImageType(const TargetExtType *ExtensionType, const SPIRV::AccessQualifier::AccessQualifier Qualifier, MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
Register buildConstantInt(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVType *SpvType, bool EmitIR, bool ZeroAsNull=true)
LLT getRegType(SPIRVType *SpvType) const
SPIRV::StorageClass::StorageClass getPointerStorageClass(Register VReg) const
SPIRVType * getOrCreateOpTypeSampler(MachineIRBuilder &MIRBuilder)
Register buildConstantSampler(Register Res, unsigned AddrMode, unsigned Param, unsigned FilerMode, MachineIRBuilder &MIRBuilder)
Register getOrCreateConstNullPtr(MachineIRBuilder &MIRBuilder, SPIRVType *SpvType)
unsigned getScalarOrVectorBitWidth(const SPIRVType *Type) const
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
bool consume_back(StringRef Suffix)
Returns true if this StringRef has the given suffix and removes that suffix.
bool getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
std::string str() const
str - Get the contents as an std::string.
constexpr StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
bool contains_insensitive(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
constexpr size_t size() const
size - Get the string size.
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
bool consume_front(StringRef Prefix)
Returns true if this StringRef has the given prefix and removes that prefix.
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
bool ends_with(StringRef Suffix) const
Check if this string ends with the given Suffix.
static constexpr size_t npos
A switch()-like statement whose cases are string literals.
StringSwitch & EndsWith(StringLiteral S, T Value)
Class to represent struct types.
Class to represent target extensions types, which are generally unintrospectable from target-independ...
ArrayRef< Type * > type_params() const
Return the type parameters for this particular target extension type.
unsigned getNumIntParameters() const
static LLVM_ABI TargetExtType * get(LLVMContext &Context, StringRef Name, ArrayRef< Type * > Types={}, ArrayRef< unsigned > Ints={})
Return a target extension type having the specified name and optional type and integer parameters.
Type * getTypeParameter(unsigned i) const
unsigned getNumTypeParameters() const
unsigned getIntParameter(unsigned i) const
The instances of the Type class are immutable: once they are created, they are never changed.
static LLVM_ABI Type * getFloatTy(LLVMContext &C)
static LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
static LLVM_ABI IntegerType * getInt32Ty(LLVMContext &C)
static LLVM_ABI Type * getHalfTy(LLVMContext &C)
LLVM_ABI StringRef getStructName() const
static LLVM_ABI Type * getVoidTy(LLVMContext &C)
LLVM Value Representation.
LLVM_ABI Value(Type *Ty, unsigned scid)
static LLVM_ABI VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Represents a version number in the form major[.minor[.subminor[.build]]].
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
LLVM_C_ABI LLVMTypeRef LLVMVectorType(LLVMTypeRef ElementType, unsigned ElementCount)
Create a vector type that contains a defined type and has a specific number of elements.
std::string lookupBuiltinNameHelper(StringRef DemangledCall, FPDecorationId *DecorationId)
Parses the name part of the demangled builtin call.
Type * parseBuiltinCallArgumentType(StringRef TypeStr, LLVMContext &Ctx)
bool parseBuiltinTypeStr(SmallVector< StringRef, 10 > &BuiltinArgsTypeStrs, const StringRef DemangledCall, LLVMContext &Ctx)
std::tuple< int, unsigned, unsigned > mapBuiltinToOpcode(const StringRef DemangledCall, SPIRV::InstructionSet::InstructionSet Set)
Helper function for finding a builtin function attributes by a demangled function name.
Type * parseBuiltinCallArgumentBaseType(const StringRef DemangledCall, unsigned ArgIdx, LLVMContext &Ctx)
Parses the provided ArgIdx argument base type in the DemangledCall skeleton.
TargetExtType * parseBuiltinTypeNameToTargetExtType(std::string TypeName, LLVMContext &Context)
Translates a string representing a SPIR-V or OpenCL builtin type to a TargetExtType that can be furth...
std::optional< bool > lowerBuiltin(const StringRef DemangledCall, SPIRV::InstructionSet::InstructionSet Set, MachineIRBuilder &MIRBuilder, const Register OrigRet, const Type *OrigRetTy, const SmallVectorImpl< Register > &Args, SPIRVGlobalRegistry *GR)
SPIRVType * lowerBuiltinType(const Type *OpaqueType, SPIRV::AccessQualifier::AccessQualifier AccessQual, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
This is an optimization pass for GlobalISel generic memory operations.
void buildOpName(Register Target, const StringRef &Name, MachineIRBuilder &MIRBuilder)
static bool build2DBlockIOINTELInst(const SPIRV::IncomingCall *Call, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building Intel's 2d block io instructions.
static SPIRVType * getVulkanBufferType(const TargetExtType *ExtensionType, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
void insertAssignInstr(Register Reg, Type *Ty, SPIRVType *SpirvTy, SPIRVGlobalRegistry *GR, MachineIRBuilder &MIB, MachineRegisterInfo &MRI)
Helper external function for inserting ASSIGN_TYPE instuction between Reg and its definition,...
static bool generateBindlessImageINTELInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateGetQueryInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateLoadStoreInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRVType * getInlineSpirvType(const TargetExtType *ExtensionType, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateConstructInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool buildAtomicFlagInst(const SPIRV::IncomingCall *Call, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building atomic flag instructions (e.g.
static Register buildBuiltinVariableLoad(MachineIRBuilder &MIRBuilder, SPIRVType *VariableType, SPIRVGlobalRegistry *GR, SPIRV::BuiltIn::BuiltIn BuiltinValue, LLT LLType, Register Reg=Register(0), bool isConst=true, bool hasLinkageTy=true)
Helper function for building a load instruction for loading a builtin global variable of BuiltinValue...
static bool generateImageSizeQueryInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRV::SamplerFilterMode::SamplerFilterMode getSamplerFilterModeFromBitmask(unsigned Bitmask)
static bool buildAtomicStoreInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building an atomic store instruction.
void addNumImm(const APInt &Imm, MachineInstrBuilder &MIB)
static bool buildExtendedBitOpsInst(const SPIRV::IncomingCall *Call, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building extended bit operations.
static const Type * getBlockStructType(Register ParamReg, MachineRegisterInfo *MRI)
static bool generateGroupInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
FPDecorationId demangledPostfixToDecorationId(const std::string &S)
static unsigned getNumComponentsForDim(SPIRV::Dim::Dim dim)
static bool generateICarryBorrowInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static Register buildScopeReg(Register CLScopeRegister, SPIRV::Scope::Scope Scope, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI)
static std::tuple< Register, SPIRVType * > buildBoolRegister(MachineIRBuilder &MIRBuilder, const SPIRVType *ResultType, SPIRVGlobalRegistry *GR)
Helper function building either a resulting scalar or vector bool register depending on the expected ...
static unsigned getNumSizeComponents(SPIRVType *imgType)
Helper function for obtaining the number of size components.
uint64_t getIConstVal(Register ConstReg, const MachineRegisterInfo *MRI)
static Register buildConstantIntReg32(uint64_t Val, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRVType * getSampledImageType(const TargetExtType *OpaqueType, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
SmallVector< MachineInstr *, 4 > createContinuedInstructions(MachineIRBuilder &MIRBuilder, unsigned Opcode, unsigned MinWC, unsigned ContinuedOpcode, ArrayRef< Register > Args, Register ReturnRegister, Register TypeID)
SPIRV::MemorySemantics::MemorySemantics getMemSemanticsForStorageClass(SPIRV::StorageClass::StorageClass SC)
constexpr unsigned storageClassToAddressSpace(SPIRV::StorageClass::StorageClass SC)
static bool generateSampleImageInst(const StringRef DemangledCall, const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateBarrierInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRVType * getCoopMatrType(const TargetExtType *ExtensionType, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateKernelClockInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static void setRegClassIfNull(Register Reg, MachineRegisterInfo *MRI, SPIRVGlobalRegistry *GR)
static bool generateGroupUniformInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateWaveInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
void buildOpDecorate(Register Reg, MachineIRBuilder &MIRBuilder, SPIRV::Decoration::Decoration Dec, const std::vector< uint32_t > &DecArgs, StringRef StrImm)
Register createVirtualRegister(SPIRVType *SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF)
static bool buildBarrierInst(const SPIRV::IncomingCall *Call, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building barriers, i.e., memory/control ordering operations.
static bool generateAsyncCopy(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRV::Scope::Scope getSPIRVScope(SPIRV::CLMemoryScope ClScope)
static SPIRVType * getSamplerType(MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
static Register buildLoadInst(SPIRVType *BaseType, Register PtrRegister, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR, LLT LowLevelType, Register DestinationReg=Register(0))
Helper function for building a load instruction loading into the DestinationReg.
static bool generateEnqueueInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
static bool buildSelectInst(MachineIRBuilder &MIRBuilder, Register ReturnRegister, Register SourceRegister, const SPIRVType *ReturnType, SPIRVGlobalRegistry *GR)
Helper function for building either a vector or scalar select instruction depending on the expected R...
static const Type * getMachineInstrType(MachineInstr *MI)
static SPIRV::SamplerAddressingMode::SamplerAddressingMode getSamplerAddressingModeFromBitmask(unsigned Bitmask)
static bool generateAtomicInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRVType * getLayoutType(const TargetExtType *ExtensionType, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateDotOrFMulInst(const StringRef DemangledCall, const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateConvertInst(const StringRef DemangledCall, const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateTernaryBitwiseFunctionINTELInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateCastToPtrInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
constexpr bool isGenericCastablePtr(SPIRV::StorageClass::StorageClass SC)
static Register buildMemSemanticsReg(Register SemanticsRegister, Register PtrRegister, unsigned &Semantics, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static unsigned getConstFromIntrinsic(Register Reg, MachineRegisterInfo *MRI)
static bool generateImageMiscQueryInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateSelectInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder)
static bool buildAtomicLoadInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building an atomic load instruction.
static bool generateIntelSubgroupsInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateExtendedBitOpsInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateSpecConstantInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRVType * getOrCreateSPIRVDeviceEventPointer(MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Type * parseBasicTypeName(StringRef &TypeName, LLVMContext &Ctx)
static bool generateVectorLoadStoreInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool genWorkgroupQuery(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR, SPIRV::BuiltIn::BuiltIn BuiltinValue, uint64_t DefaultValue)
static bool generateCoopMatrInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool buildBindlessImageINTELInst(const SPIRV::IncomingCall *Call, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building Intel's bindless image instructions.
static std::unique_ptr< const SPIRV::IncomingCall > lookupBuiltin(StringRef DemangledCall, SPIRV::InstructionSet::InstructionSet Set, Register ReturnRegister, const SPIRVType *ReturnType, const SmallVectorImpl< Register > &Arguments)
Looks up the demangled builtin call in the SPIRVBuiltins.td records using the provided DemangledCall ...
static bool buildAtomicFloatingRMWInst(const SPIRV::IncomingCall *Call, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building an atomic floating-type instruction.
MachineInstr * getDefInstrMaybeConstant(Register &ConstReg, const MachineRegisterInfo *MRI)
constexpr unsigned BitWidth
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
const MachineInstr SPIRVType
static bool generate2DBlockIOINTELInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateReadImageInst(const StringRef DemangledCall, const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
bool hasBuiltinTypePrefix(StringRef Name)
static bool buildEnqueueKernel(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Type * getMDOperandAsType(const MDNode *N, unsigned I)
static bool buildTernaryBitwiseFunctionINTELInst(const SPIRV::IncomingCall *Call, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building Intel's OpBitwiseFunctionINTEL instruction.
static bool buildAtomicRMWInst(const SPIRV::IncomingCall *Call, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building atomic instructions.
static SPIRV::MemorySemantics::MemorySemantics getSPIRVMemSemantics(std::memory_order MemOrder)
static bool generateRelationalInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool buildAtomicInitInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder)
Helper function for translating atomic init to OpStore.
static bool generateWriteImageInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRVType * getPipeType(const TargetExtType *ExtensionType, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static Type * parseTypeString(const StringRef Name, LLVMContext &Context)
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
static bool generateAtomicFloatingInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool generateExtInst(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static bool buildNDRange(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static SPIRVType * getNonParameterizedType(const TargetExtType *ExtensionType, const SPIRV::BuiltinType *TypeRecord, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
static MachineInstr * getBlockStructInstr(Register ParamReg, MachineRegisterInfo *MRI)
static bool buildOpFromWrapper(MachineIRBuilder &MIRBuilder, unsigned Opcode, const SPIRV::IncomingCall *Call, Register TypeReg, ArrayRef< uint32_t > ImmArgs={})
static unsigned getSamplerParamFromBitmask(unsigned Bitmask)
static bool buildAtomicCompareExchangeInst(const SPIRV::IncomingCall *Call, const SPIRV::DemangledBuiltin *Builtin, unsigned Opcode, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Helper function for building an atomic compare-exchange instruction.
std::string getLinkStringForBuiltIn(SPIRV::BuiltIn::BuiltIn BuiltInValue)
static bool generateBuiltinVar(const SPIRV::IncomingCall *Call, MachineIRBuilder &MIRBuilder, SPIRVGlobalRegistry *GR)
Implement std::hash so that hash_code can be used in STL containers.
static LLVM_ABI const fltSemantics & IEEEsingle() LLVM_READNONE
This struct is a compact representation of a valid (non-zero power of two) alignment.
This class contains a discriminated union of information about pointers in memory operands,...
FPRoundingMode::FPRoundingMode RoundingMode
InstructionSet::InstructionSet Set
InstructionSet::InstructionSet Set
InstructionSet::InstructionSet Set
InstructionSet::InstructionSet Set
const SmallVectorImpl< Register > & Arguments
const std::string BuiltinName
const SPIRVType * ReturnType
const Register ReturnRegister
const DemangledBuiltin * Builtin
IncomingCall(const std::string BuiltinName, const DemangledBuiltin *Builtin, const Register ReturnRegister, const SPIRVType *ReturnType, const SmallVectorImpl< Register > &Arguments)
InstructionSet::InstructionSet Set
StringRef SpirvTypeLiteral
InstructionSet::InstructionSet Set
FPRoundingMode::FPRoundingMode RoundingMode