21#include "llvm/IR/IntrinsicsSPIRV.h"
23#define DEBUG_TYPE "spirv-lower"
37 if (Ty1->
getOpcode() == SPIRV::OpTypeArray) {
44 return ElemType1 == ElemType2 ||
48 if (Ty1->
getOpcode() == SPIRV::OpTypeStruct) {
54 if (ElemType1 != ElemType2 &&
95 unsigned Intrinsic)
const {
96 unsigned AlignIdx = 3;
98 case Intrinsic::spv_load:
101 case Intrinsic::spv_store: {
102 if (
I.getNumOperands() >= AlignIdx + 1) {
103 auto *AlignOp = cast<ConstantInt>(
I.getOperand(AlignIdx));
104 Info.align =
Align(AlignOp->getZExtValue());
107 cast<ConstantInt>(
I.getOperand(AlignIdx - 1))->getZExtValue());
108 Info.memVT = MVT::i64;
120std::pair<unsigned, const TargetRegisterClass *>
126 return std::make_pair(0u, RC);
129 RC = VT.
isVector() ? &SPIRV::vfIDRegClass : &SPIRV::fIDRegClass;
131 RC = VT.
isVector() ? &SPIRV::vIDRegClass : &SPIRV::iIDRegClass;
133 RC = &SPIRV::iIDRegClass;
135 return std::make_pair(0u, RC);
140 return TypeInst && TypeInst->
getOpcode() == SPIRV::OpFunctionParameter
159 I.getOperand(
OpIdx).setReg(NewReg);
165 SPIRV::StorageClass::StorageClass SC =
166 static_cast<SPIRV::StorageClass::StorageClass
>(
172 ResTy, MIB, SPIRV::AccessQualifier::ReadWrite,
false);
187 if (!ResType || !OpType || OpType->
getOpcode() != SPIRV::OpTypePointer)
196 bool IsEqualTypes = IsSameMF ? ElemType == ResType
206 "insert validation bitcast: incompatible result and operand types");
216 constexpr unsigned OpIdx = 2;
221 if (!OpType || OpType->
getOpcode() != SPIRV::OpTypePointer)
224 if (!ElemType || ElemType->
getOpcode() == SPIRV::OpTypeEvent)
237 Register PtrReg =
I.getOperand(0).getReg();
242 if (!PonteeElemType || PonteeElemType->
getOpcode() == SPIRV::OpTypeVoid ||
243 (PonteeElemType->
getOpcode() == SPIRV::OpTypeInt &&
247 SPIRV::StorageClass::StorageClass SC =
248 static_cast<SPIRV::StorageClass::StorageClass
>(
265 if (!OpType || OpType->
getOpcode() != SPIRV::OpTypePointer)
268 if (!ElemType || ElemType->
getOpcode() != SPIRV::OpTypeStruct ||
276 unsigned MemberTypeOp = MemberType->
getOpcode();
277 if (MemberTypeOp != SPIRV::OpTypeVector && MemberTypeOp != SPIRV::OpTypeInt &&
278 MemberTypeOp != SPIRV::OpTypeFloat && MemberTypeOp != SPIRV::OpTypeBool)
282 SPIRV::StorageClass::StorageClass SC =
283 static_cast<SPIRV::StorageClass::StorageClass
>(
303 if (FunDef->
getOpcode() != SPIRV::OpFunction)
307 FunDef && FunDef->
getOpcode() == SPIRV::OpFunctionParameter &&
312 DefPtrType && DefPtrType->
getOpcode() == SPIRV::OpTypePointer
339 const Function *
F = dyn_cast<Function>(GV);
358 &FunCall->getParent()->getParent()->getRegInfo();
367 if (BaseTypeInst && BaseTypeInst->
getOpcode() == SPIRV::OpTypePointer) {
379 if (ProcessedMF.find(&MF) != ProcessedMF.end())
391 switch (
MI.getOpcode()) {
392 case SPIRV::OpAtomicLoad:
393 case SPIRV::OpAtomicExchange:
394 case SPIRV::OpAtomicCompareExchange:
395 case SPIRV::OpAtomicCompareExchangeWeak:
396 case SPIRV::OpAtomicIIncrement:
397 case SPIRV::OpAtomicIDecrement:
398 case SPIRV::OpAtomicIAdd:
399 case SPIRV::OpAtomicISub:
400 case SPIRV::OpAtomicSMin:
401 case SPIRV::OpAtomicUMin:
402 case SPIRV::OpAtomicSMax:
403 case SPIRV::OpAtomicUMax:
404 case SPIRV::OpAtomicAnd:
405 case SPIRV::OpAtomicOr:
406 case SPIRV::OpAtomicXor:
418 case SPIRV::OpAtomicStore:
429 case SPIRV::OpPtrCastToGeneric:
430 case SPIRV::OpGenericCastToPtr:
431 case SPIRV::OpGenericCastToPtrExplicit:
434 case SPIRV::OpPtrAccessChain:
435 case SPIRV::OpInBoundsPtrAccessChain:
436 if (
MI.getNumOperands() == 4)
440 case SPIRV::OpFunctionCall:
443 if (
MI.getNumOperands() > 3)
447 case SPIRV::OpFunction:
466 case SPIRV::OpBitwiseOrS:
467 case SPIRV::OpBitwiseOrV:
472 case SPIRV::OpBitwiseAndS:
473 case SPIRV::OpBitwiseAndV:
478 case SPIRV::OpBitwiseXorS:
479 case SPIRV::OpBitwiseXorV:
484 case SPIRV::OpLifetimeStart:
485 case SPIRV::OpLifetimeStop:
486 if (
MI.getOperand(1).getImm() > 0)
489 case SPIRV::OpGroupAsyncCopy:
493 case SPIRV::OpGroupWaitEvents:
497 case SPIRV::OpConstantI: {
499 if (
Type->getOpcode() != SPIRV::OpTypeInt &&
MI.getOperand(2).isImm() &&
500 MI.getOperand(2).getImm() == 0) {
503 for (
unsigned i =
MI.getNumOperands() - 1; i > 1; --i)
517 case SPIRV::OpExtInst: {
519 if (!
MI.getOperand(2).isImm() || !
MI.getOperand(3).isImm() ||
520 MI.getOperand(2).getImm() != SPIRV::InstructionSet::OpenCL_std)
522 switch (
MI.getOperand(3).getImm()) {
523 case SPIRV::OpenCLExtInst::frexp:
524 case SPIRV::OpenCLExtInst::lgamma_r:
525 case SPIRV::OpenCLExtInst::remquo: {
531 assert(RetType &&
"Expected return type");
533 RetType->
getOpcode() != SPIRV::OpTypeVector
539 case SPIRV::OpenCLExtInst::fract:
540 case SPIRV::OpenCLExtInst::modf:
541 case SPIRV::OpenCLExtInst::sincos:
544 assert(
MI.getOperand(
MI.getNumOperands() - 2).isReg() &&
547 STI,
MRI, GR,
MI,
MI.getNumOperands() - 1,
549 MI.getOperand(
MI.getNumOperands() - 2).getReg()));
551 case SPIRV::OpenCLExtInst::prefetch:
554 assert(
MI.getOperand(
MI.getNumOperands() - 2).isReg() &&
557 MI.getNumOperands() - 2);
569 ProcessedMF.insert(&MF);
583 if (PointeeType == OpType)
588 if (
I.getOperand(
OpIdx).isDef() &&
609 assert(std::distance(
I.defs().begin(),
I.defs().end()) == 1 &&
610 "Expected only one def");
616 OldResult.
setReg(NewResultReg);
617 OldType.
setReg(NewTypeReg);
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock MachineBasicBlock::iterator MBBI
Analysis containing CSE Info
Register const TargetRegisterInfo * TRI
MachineInstr unsigned OpIdx
static void doInsertBitcast(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI, SPIRVGlobalRegistry &GR, MachineInstr &I, Register OpReg, unsigned OpIdx, SPIRVType *NewPtrType)
static SPIRVType * createNewPtrType(SPIRVGlobalRegistry &GR, MachineInstr &I, SPIRVType *OpType, bool ReuseType, SPIRVType *ResType, const Type *ResTy)
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 bool typesLogicallyMatch(const SPIRVType *Ty1, const SPIRVType *Ty2, SPIRVGlobalRegistry &GR)
static void validatePtrTypes(const SPIRVSubtarget &STI, MachineRegisterInfo *MRI, SPIRVGlobalRegistry &GR, MachineInstr &I, unsigned OpIdx, SPIRVType *ResType, const Type *ResTy=nullptr)
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.
LLVM_ABI MachineInstr * remove_instr(MachineInstr *I)
Remove the possibly bundled instruction from the instruction list without deleting it.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
LLVM_ABI 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.
MachineOperand class - Representation of each machine instruction operand.
const GlobalValue * getGlobal() const
LLVM_ABI void setReg(Register Reg)
Change the register this operand corresponds to.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
LLVM_ABI 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)
SPIRVType * getResultType(Register VReg, MachineFunction *MF=nullptr)
const Type * getTypeForSPIRVType(const SPIRVType *Ty) const
bool isBitcastCompatible(const SPIRVType *Type1, const SPIRVType *Type2) const
SPIRVType * getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
SPIRVType * getOrCreateSPIRVPointerType(const Type *BaseType, MachineIRBuilder &MIRBuilder, SPIRV::StorageClass::StorageClass SC)
const MachineInstr * getFunctionDefinition(const Function *F)
SPIRVType * getPointeeType(SPIRVType *PtrType)
Register getSPIRVTypeID(const SPIRVType *SpirvType) const
SmallPtrSet< MachineInstr *, 8 > * getForwardCalls(const Function *F)
SPIRVType * getOrCreateSPIRVVectorType(SPIRVType *BaseType, unsigned NumElements, MachineIRBuilder &MIRBuilder, bool EmitIR)
bool isScalarOrVectorOfType(Register VReg, unsigned TypeOpcode) const
MachineFunction * setCurrentFunc(MachineFunction &MF)
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
bool enforcePtrTypeCompatibility(MachineInstr &I, unsigned PtrOpIdx, unsigned OpIdx) const
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.
bool insertLogicalCopyOnResult(MachineInstr &I, SPIRVType *NewResultType) const
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 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.
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 LLVM_ABI IntegerType * getInt8Ty(LLVMContext &C)
NodeTy * getNextNode()
Get the next node, or nullptr for the list tail.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is an optimization pass for GlobalISel generic memory operations.
Register createVirtualRegister(SPIRVType *SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF)
LLVM_ABI void report_fatal_error(Error Err, bool gen_crash_diag=true)
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.