22#include "llvm/IR/IntrinsicsSPIRV.h"
24#define DEBUG_TYPE "spirv-lower"
60 unsigned Intrinsic)
const {
61 unsigned AlignIdx = 3;
63 case Intrinsic::spv_load:
66 case Intrinsic::spv_store: {
67 if (
I.getNumOperands() >= AlignIdx + 1) {
68 auto *AlignOp = cast<ConstantInt>(
I.getOperand(AlignIdx));
69 Info.align =
Align(AlignOp->getZExtValue());
72 cast<ConstantInt>(
I.getOperand(AlignIdx - 1))->getZExtValue());
73 Info.memVT = MVT::i64;
85std::pair<unsigned, const TargetRegisterClass *>
91 return std::make_pair(0u, RC);
94 RC = VT.
isVector() ? &SPIRV::vfIDRegClass : &SPIRV::fIDRegClass;
96 RC = VT.
isVector() ? &SPIRV::vIDRegClass : &SPIRV::iIDRegClass;
98 RC = &SPIRV::iIDRegClass;
100 return std::make_pair(0u, RC);
105 return TypeInst && TypeInst->
getOpcode() == SPIRV::OpFunctionParameter
124 I.getOperand(OpIdx).setReg(NewReg);
131 SPIRV::StorageClass::StorageClass SC =
132 static_cast<SPIRV::StorageClass::StorageClass
>(
138 ResTy, MIB, SPIRV::AccessQualifier::ReadWrite, EmitIR);
150 Register OpReg =
I.getOperand(OpIdx).getReg();
153 if (!ResType || !OpType || OpType->
getOpcode() != SPIRV::OpTypePointer)
162 bool IsEqualTypes = IsSameMF ? ElemType == ResType
172 "insert validation bitcast: incompatible result and operand types");
182 constexpr unsigned OpIdx = 2;
184 Register OpReg =
I.getOperand(OpIdx).getReg();
187 if (!OpType || OpType->
getOpcode() != SPIRV::OpTypePointer)
190 if (!ElemType || ElemType->
getOpcode() == SPIRV::OpTypeEvent)
203 Register PtrReg =
I.getOperand(0).getReg();
208 if (!PonteeElemType || PonteeElemType->
getOpcode() == SPIRV::OpTypeVoid ||
209 (PonteeElemType->
getOpcode() == SPIRV::OpTypeInt &&
213 SPIRV::StorageClass::StorageClass SC =
214 static_cast<SPIRV::StorageClass::StorageClass
>(
229 Register OpReg =
I.getOperand(OpIdx).getReg();
232 if (!OpType || OpType->
getOpcode() != SPIRV::OpTypePointer)
235 if (!ElemType || ElemType->
getOpcode() != SPIRV::OpTypeStruct ||
243 unsigned MemberTypeOp = MemberType->
getOpcode();
244 if (MemberTypeOp != SPIRV::OpTypeVector && MemberTypeOp != SPIRV::OpTypeInt &&
245 MemberTypeOp != SPIRV::OpTypeFloat && MemberTypeOp != SPIRV::OpTypeBool)
249 SPIRV::StorageClass::StorageClass SC =
250 static_cast<SPIRV::StorageClass::StorageClass
>(
270 if (FunDef->
getOpcode() != SPIRV::OpFunction)
274 FunDef && FunDef->
getOpcode() == SPIRV::OpFunctionParameter &&
279 DefPtrType && DefPtrType->
getOpcode() == SPIRV::OpTypePointer
306 const Function *
F = dyn_cast<Function>(GV);
325 &FunCall->getParent()->getParent()->getRegInfo();
334 if (BaseTypeInst && BaseTypeInst->
getOpcode() == SPIRV::OpTypePointer) {
346 if (ProcessedMF.find(&MF) != ProcessedMF.end())
358 switch (
MI.getOpcode()) {
359 case SPIRV::OpAtomicLoad:
360 case SPIRV::OpAtomicExchange:
361 case SPIRV::OpAtomicCompareExchange:
362 case SPIRV::OpAtomicCompareExchangeWeak:
363 case SPIRV::OpAtomicIIncrement:
364 case SPIRV::OpAtomicIDecrement:
365 case SPIRV::OpAtomicIAdd:
366 case SPIRV::OpAtomicISub:
367 case SPIRV::OpAtomicSMin:
368 case SPIRV::OpAtomicUMin:
369 case SPIRV::OpAtomicSMax:
370 case SPIRV::OpAtomicUMax:
371 case SPIRV::OpAtomicAnd:
372 case SPIRV::OpAtomicOr:
373 case SPIRV::OpAtomicXor:
382 case SPIRV::OpAtomicStore:
393 case SPIRV::OpPtrCastToGeneric:
394 case SPIRV::OpGenericCastToPtr:
397 case SPIRV::OpPtrAccessChain:
398 case SPIRV::OpInBoundsPtrAccessChain:
399 if (
MI.getNumOperands() == 4)
403 case SPIRV::OpFunctionCall:
406 if (
MI.getNumOperands() > 3)
410 case SPIRV::OpFunction:
429 case SPIRV::OpBitwiseOrS:
430 case SPIRV::OpBitwiseOrV:
435 case SPIRV::OpBitwiseAndS:
436 case SPIRV::OpBitwiseAndV:
441 case SPIRV::OpBitwiseXorS:
442 case SPIRV::OpBitwiseXorV:
447 case SPIRV::OpLifetimeStart:
448 case SPIRV::OpLifetimeStop:
449 if (
MI.getOperand(1).getImm() > 0)
452 case SPIRV::OpGroupAsyncCopy:
456 case SPIRV::OpGroupWaitEvents:
460 case SPIRV::OpConstantI: {
462 if (
Type->getOpcode() != SPIRV::OpTypeInt &&
MI.getOperand(2).isImm() &&
463 MI.getOperand(2).getImm() == 0) {
466 for (
unsigned i =
MI.getNumOperands() - 1; i > 1; --i)
480 case SPIRV::OpExtInst: {
482 if (!
MI.getOperand(2).isImm() || !
MI.getOperand(3).isImm() ||
483 MI.getOperand(2).getImm() != SPIRV::InstructionSet::OpenCL_std)
485 switch (
MI.getOperand(3).getImm()) {
486 case SPIRV::OpenCLExtInst::frexp:
487 case SPIRV::OpenCLExtInst::lgamma_r:
488 case SPIRV::OpenCLExtInst::remquo: {
494 assert(RetType &&
"Expected return type");
496 STI,
MRI, GR,
MI,
MI.getNumOperands() - 1,
497 RetType->
getOpcode() != SPIRV::OpTypeVector
502 case SPIRV::OpenCLExtInst::fract:
503 case SPIRV::OpenCLExtInst::modf:
504 case SPIRV::OpenCLExtInst::sincos:
507 assert(
MI.getOperand(
MI.getNumOperands() - 2).isReg() &&
510 STI,
MRI, GR,
MI,
MI.getNumOperands() - 1,
512 MI.getOperand(
MI.getNumOperands() - 2).getReg()));
514 case SPIRV::OpenCLExtInst::prefetch:
517 assert(
MI.getOperand(
MI.getNumOperands() - 2).isReg() &&
520 MI.getNumOperands() - 2);
532 ProcessedMF.insert(&MF);
unsigned const MachineRegisterInfo * MRI
MachineBasicBlock MachineBasicBlock::iterator MBBI
Analysis containing CSE Info
unsigned const TargetRegisterInfo * TRI
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
static void doInsertBitcast(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI, SPIRVGlobalRegistry &GR, MachineInstr &I, Register OpReg, unsigned OpIdx, SPIRVType *NewPtrType)
static void validateLifetimeStart(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI, SPIRVGlobalRegistry &GR, MachineInstr &I)
static void validateGroupWaitEventsPtr(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI, SPIRVGlobalRegistry &GR, MachineInstr &I)
static void validatePtrUnwrapStructField(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI, SPIRVGlobalRegistry &GR, MachineInstr &I, unsigned OpIdx)
Register getTypeReg(MachineRegisterInfo *MRI, Register OpReg)
void validateAccessChain(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI, SPIRVGlobalRegistry &GR, MachineInstr &I)
void validateFunCallMachineDef(const SPIRVSubtarget &STI, MachineRegisterInfo *DefMRI, MachineRegisterInfo *CallMRI, SPIRVGlobalRegistry &GR, MachineInstr &FunCall, MachineInstr *FunDef)
void validateForwardCalls(const SPIRVSubtarget &STI, MachineRegisterInfo *DefMRI, SPIRVGlobalRegistry &GR, MachineInstr &FunDef)
const Function * validateFunCall(const SPIRVSubtarget &STI, MachineRegisterInfo *CallMRI, SPIRVGlobalRegistry &GR, MachineInstr &FunCall)
static void validatePtrTypes(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI, SPIRVGlobalRegistry &GR, MachineInstr &I, unsigned OpIdx, SPIRVType *ResType, const Type *ResTy=nullptr)
static SPIRVType * createNewPtrType(SPIRVGlobalRegistry &GR, MachineInstr &I, SPIRVType *OpType, bool ReuseType, bool EmitIR, SPIRVType *ResType, const Type *ResTy)
This class represents a function call, abstracting a target machine's calling convention.
LLVMContext & getContext() const
getContext - Return a reference to the LLVMContext associated with this function.
This is an important class for using LLVM in a threaded context.
bool isVector() const
Return true if this is a vector value type.
bool isInteger() const
Return true if this is an integer or a vector integer type.
bool isFloatingPoint() const
Return true if this is a FP or a vector FP type.
MachineInstr * remove_instr(MachineInstr *I)
Remove the possibly bundled instruction from the instruction list without deleting it.
instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
iterator getFirstTerminator()
Returns an iterator to the first terminator instruction of this basic block.
pred_iterator pred_begin()
const MachineFunction * getParent() const
Return the MachineFunction containing this basic block.
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.
MachineInstrBuilder buildInstr(unsigned Opcode)
Build and insert <empty> = Opcode <empty>.
MachineFunction & getMF()
Getter for the function we currently build.
bool constrainAllUses(const TargetInstrInfo &TII, const TargetRegisterInfo &TRI, const RegisterBankInfo &RBI) const
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 MachineBasicBlock * getParent() const
unsigned getNumOperands() const
Retuns the total number of operands.
const MachineOperand & getOperand(unsigned i) const
Flags
Flags values. These may be or'd together.
const GlobalValue * getGlobal() const
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
MachineInstr * getVRegDef(Register Reg) const
getVRegDef - Return the machine instr that defines the specified virtual register or null if none is ...
Wrapper class representing virtual and physical registers.
SPIRVType * getSPIRVTypeForVReg(Register VReg, const MachineFunction *MF=nullptr) const
void addForwardCall(const Function *F, MachineInstr *MI)
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
bool isBitcastCompatible(const SPIRVType *Type1, const SPIRVType *Type2) const
const MachineInstr * getFunctionDefinition(const Function *F)
SPIRVType * getPointeeType(SPIRVType *PtrType)
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineIRBuilder &MIRBuilder, SPIRV::AccessQualifier::AccessQualifier AQ=SPIRV::AccessQualifier::ReadWrite, bool EmitIR=true)
SmallPtrSet< MachineInstr *, 8 > * getForwardCalls(const Function *F)
bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const
MachineFunction * setCurrentFunc(MachineFunction &MF)
SPIRVType * getOrCreateSPIRVPointerType(SPIRVType *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SClass=SPIRV::StorageClass::Function)
SPIRVType * getOrCreateSPIRVVectorType(SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder)
SPIRVType * getOrCreateSPIRVIntegerType(unsigned BitWidth, MachineIRBuilder &MIRBuilder)
const Function * getFunctionByDefinition(const MachineInstr *MI)
const SPIRVInstrInfo * getInstrInfo() const override
SPIRVGlobalRegistry * getSPIRVGlobalRegistry() const
const SPIRVRegisterInfo * getRegisterInfo() const override
const RegisterBankInfo * getRegBankInfo() const override
unsigned getNumRegisters(LLVMContext &Context, EVT VT, std::optional< MVT > RegisterVT=std::nullopt) const override
Return the number of registers that this ValueType will eventually require.
unsigned getNumRegistersForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Certain targets require unusual breakdowns of certain types.
MVT getRegisterTypeForCallingConv(LLVMContext &Context, CallingConv::ID CC, EVT VT) const override
Certain combinations of ABIs, Targets and features require that types are legal for some operations a...
bool getTgtMemIntrinsic(IntrinsicInfo &Info, const CallInst &I, MachineFunction &MF, unsigned Intrinsic) const override
Given an intrinsic, checks if on the target the intrinsic will need to map to a MemIntrinsicNode (tou...
void finalizeLowering(MachineFunction &MF) const override
Execute target specific actions to finalize target lowering.
std::pair< unsigned, const TargetRegisterClass * > getRegForInlineAsmConstraint(const TargetRegisterInfo *TRI, StringRef Constraint, MVT VT) const override
Given a physical register constraint (e.g.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
StringRef - Represent a constant reference to a string, i.e.
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
static 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.
virtual void finalizeLowering(MachineFunction &MF) const
Execute target specific actions to finalize target lowering.
MVT getRegisterType(MVT VT) const
Return the type of registers that this ValueType will eventually require.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
The instances of the Type class are immutable: once they are created, they are never changed.
static IntegerType * getInt8Ty(LLVMContext &C)
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
This is an optimization pass for GlobalISel generic memory operations.
Register createVirtualRegister(SPIRVType *SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF)
void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This struct is a compact representation of a valid (non-zero power of two) alignment.
TypeSize getSizeInBits() const
Return the size of the specified value type in bits.
bool isVector() const
Return true if this is a vector value type.
EVT getVectorElementType() const
Given a vector type, return the type of each element.
unsigned getVectorNumElements() const
Given a vector type, return the number of elements it contains.
bool isInteger() const
Return true if this is an integer or a vector integer type.