15#include "llvm/IR/IntrinsicsSPIRV.h"
37 Register NumeratorReg =
MI.getOperand(1).getReg();
38 Register DivisorReg =
MI.getOperand(2).getReg();
42 if (ShuffleInstr->
getOpcode() != TargetOpcode::G_SHUFFLE_VECTOR)
45 [](
int M) {
return M == 0; }))
66 Register NumeratorReg =
MI.getOperand(1).getReg();
67 Register ResultReg =
MI.getOperand(0).getReg();
70 Builder.buildIntrinsic(Intrinsic::spv_normalize, ResultReg)
71 .addUse(NumeratorReg);
117 !
MRI.getType(DotOperand1).isScalar() ||
118 !
MRI.getType(DotOperand2).isScalar())
127 auto AreNegatedConstantsOrSplats = [&](
Register TrueReg,
Register FalseReg) {
128 std::optional<FPValueAndVReg> TrueVal, FalseVal;
132 APFloat TrueValNegated = TrueVal->Value;
139 std::optional<FPValueAndVReg> MulConstant;
160 if (!MulConstant || !MulConstant->Value.isMinusOne())
162 }
else if (!AreNegatedConstantsOrSplats(TrueReg, FalseReg))
179 if (DotInstr->
getOpcode() == TargetOpcode::G_FMUL) {
187 Register FalseReg =
MI.getOperand(3).getReg();
189 if (TrueInstr->
getOpcode() == TargetOpcode::G_FNEG ||
190 TrueInstr->
getOpcode() == TargetOpcode::G_FMUL)
193 Register ResultReg =
MI.getOperand(0).getReg();
195 Builder.buildIntrinsic(Intrinsic::spv_faceforward, ResultReg)
198 .addUse(DotOperand2);
200 MI.eraseFromParent();
212 if (Rows == 1 || Cols == 1) {
213 Builder.buildCopy(ResReg, InReg);
214 MI.eraseFromParent();
222 Mask.push_back(
C * Rows + R);
225 Builder.buildShuffleVector(ResReg, InReg, InReg, Mask);
226 MI.eraseFromParent();
230SPIRVCombinerHelper::extractColumns(
Register MatrixReg,
uint32_t NumberOfCols,
234 if (NumberOfCols == 1)
239 for (
uint32_t J = 0; J < NumberOfCols; ++J)
241 Builder.buildUnmerge(Cols, MatrixReg);
249SPIRVCombinerHelper::extractRows(
Register MatrixReg, uint32_t NumRows,
259 for (uint32_t
I = 0;
I < NumRows; ++
I)
260 Rows.
push_back(
MRI.createGenericVirtualRegister(VecTy));
261 Builder.buildUnmerge(Rows, MatrixReg);
273 for (uint32_t
I = 0;
I < NumRows; ++
I) {
274 SmallVector<int, 4>
Mask;
275 for (uint32_t k = 0;
k < NumCols; ++
k)
276 Mask.push_back(k * NumRows +
I);
277 Rows.
push_back(
Builder.buildShuffleVector(VecTy, MatrixReg, MatrixReg, Mask)
290 bool IsFloatOp = SpvScalarType->
getOpcode() == SPIRV::OpTypeFloat;
296 Intrinsic::SPVIntrinsics DotIntrinsic =
297 (IsFloatOp ? Intrinsic::spv_fdot : Intrinsic::spv_udot);
298 DotRes =
Builder.buildIntrinsic(DotIntrinsic, {ScalarTy})
304 DotRes =
Builder.buildFMul(VecTy, RowA, ColB).getReg(0);
306 DotRes =
Builder.buildMul(VecTy, RowA, ColB).getReg(0);
315 SmallVector<Register, 16> ResultScalars;
316 for (uint32_t J = 0; J < ColsB.
size(); ++J) {
317 for (uint32_t
I = 0;
I < RowsA.
size(); ++
I) {
319 computeDotProduct(RowsA[
I], ColsB[J], SpvVecType, GR));
322 return ResultScalars;
326SPIRVCombinerHelper::getDotProductVectorType(
Register ResReg, uint32_t K,
329 Type *ScalarResType =
nullptr;
330 for (
auto &
UseMI :
MRI.use_instructions(ResReg)) {
331 if (
UseMI.getOpcode() != TargetOpcode::G_INTRINSIC_W_SIDE_EFFECTS)
350 SPIRV::AccessQualifier::None,
false);
357 uint32_t NumRowsA =
MI.getOperand(4).getImm();
358 uint32_t NumColsA =
MI.getOperand(5).getImm();
359 uint32_t NumColsB =
MI.getOperand(6).getImm();
366 SPIRVTypeInst SpvVecType = getDotProductVectorType(ResReg, NumColsA, GR);
368 extractColumns(BReg, NumColsB, SpvVecType, GR);
370 extractRows(AReg, NumRowsA, NumColsA, SpvVecType, GR);
372 computeDotProducts(RowsA, ColsB, SpvVecType, GR);
374 if (ResultScalars.
size() == 1)
375 Builder.buildCopy(ResReg, ResultScalars[0]);
377 Builder.buildBuildVector(ResReg, ResultScalars);
378 MI.eraseFromParent();
MachineInstrBuilder & UseMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
Declares convenience wrapper classes for interpreting MachineInstr instances as specific generic oper...
static std::pair< Value *, APInt > getMask(Value *WideMask, unsigned Factor, ElementCount LeafValueEC)
Contains matchers for matching SSA Machine Instructions.
Promote Memory to Register
Represent a constant reference to an array (0 or more elements consecutively in memory),...
size_t size() const
Get the array size.
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
@ FCMP_OLT
0 1 0 0 True if ordered and less than
@ FCMP_OGT
0 0 1 0 True if ordered and greater than
@ FCMP_ULT
1 1 0 0 True if unordered or less than
@ FCMP_UGT
1 0 1 0 True if unordered or greater than
MachineRegisterInfo & MRI
MachineDominatorTree * MDT
GISelChangeObserver & Observer
MachineIRBuilder & Builder
ConstantFP - Floating Point Values [float, double].
bool isZero() const
Return true if the value is positive or negative zero.
static LLVM_ABI FixedVectorType * get(Type *ElementType, unsigned NumElts)
Represents a G_BUILD_VECTOR.
Abstract class that contains various methods for clients to notify about changes.
LLT getElementType() const
Returns the vector's element type. Only valid for vector types.
DominatorTree Class - Concrete subclass of DominatorTreeBase that is used to compute a normal dominat...
Helper class to build MachineInstr.
Representation of each machine instruction.
unsigned getOpcode() const
Returns the opcode of this MachineInstr.
unsigned getNumOperands() const
Retuns the total number of operands.
const MachineOperand & getOperand(unsigned i) const
Register getReg() const
getReg - Returns the register number.
const MachineFunction & getMF() const
LLVM_ABI Register createGenericVirtualRegister(LLT Ty, StringRef Name="")
Create and return a new generic virtual register with low-level type Ty.
Wrapper class representing virtual and physical registers.
void applyMatrixMultiply(MachineInstr &MI) const
bool matchSelectToFaceForward(MachineInstr &MI) const
This match is part of a combine that rewrites select(fcmp(dot(I, Ng), 0), N, -N) to faceforward(N,...
void applyMatrixTranspose(MachineInstr &MI) const
bool matchFDivToNormalize(MachineInstr &MI) const
This match is part of a combine that rewrites X / length(X) to normalize(X) (vXf32 (g_fdiv (vXf32 X) ...
LLVM_ABI CombinerHelper(GISelChangeObserver &Observer, MachineIRBuilder &B, bool IsPreLegalize, GISelValueTracking *VT=nullptr, MachineDominatorTree *MDT=nullptr, const LegalizerInfo *LI=nullptr)
void applySPIRVFaceForward(MachineInstr &MI) const
SPIRVCombinerHelper(GISelChangeObserver &Observer, MachineIRBuilder &B, bool IsPreLegalize, GISelValueTracking *VT, MachineDominatorTree *MDT, const LegalizerInfo *LI, const SPIRVSubtarget &STI)
void applySPIRVNormalize(MachineInstr &MI) const
const SPIRVSubtarget & STI
LLT getRegType(SPIRVTypeInst SpvType) const
SPIRVTypeInst getScalarOrVectorComponentType(SPIRVTypeInst Type) const
SPIRVTypeInst getOrCreateSPIRVType(const Type *Type, MachineInstr &I, SPIRV::AccessQualifier::AccessQualifier AQ, bool EmitIR)
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
bool isVectorTy() const
True if this is an instance of VectorType.
bool isFloatingPointTy() const
Return true if this is one of the floating-point types.
bool isIntegerTy() const
True if this is an instance of IntegerType.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr std::underlying_type_t< E > Mask()
Get a bitmask with 1s in all places up to the high-order bit of E's largest value.
operand_type_match m_Reg()
GInstrBind< GBuildVector > m_GBuildVector(GBuildVector *&Inst)
operand_type_match m_Pred()
SpecificConstantMatch m_ZeroInt()
Convenience matchers for specific integer values.
TernaryOp_match< Src0Ty, Src1Ty, Src2Ty, TargetOpcode::G_SELECT > m_GISelect(const Src0Ty &Src0, const Src1Ty &Src1, const Src2Ty &Src2)
bool mi_match(Reg R, const MachineRegisterInfo &MRI, Pattern &&P)
SpecificRegisterMatch m_SpecificReg(Register RequestedReg)
Matches a register only if it is equal to RequestedReg.
UnaryOp_match< SrcTy, TargetOpcode::G_FNEG > m_GFNeg(const SrcTy &Src)
GFCstAndRegMatch m_GFCst(std::optional< FPValueAndVReg > &FPValReg)
GFCstOrSplatGFCstMatch m_GFCstOrSplat(std::optional< FPValueAndVReg > &FPValReg)
BinaryOp_match< LHS, RHS, TargetOpcode::G_FMUL, true > m_GFMul(const LHS &L, const RHS &R)
GInstrBind< GIntrinsic > m_GIntrinsic(GIntrinsic *&Inst)
Binds the defining instruction of Reg if it is a GIntrinsic (any of the four G_INTRINSIC* opcodes).
CompareOp_match< Pred, LHS, RHS, TargetOpcode::G_FCMP > m_GFCmp(const Pred &P, const LHS &L, const RHS &R)
This is an optimization pass for GlobalISel generic memory operations.
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
bool isVectorType(SPIRVTypeInst SPVTy)
void setRegClassType(Register Reg, SPIRVTypeInst SpvType, SPIRVGlobalRegistry *GR, MachineRegisterInfo *MRI, const MachineFunction &MF, bool Force)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Type * getMDOperandAsType(const MDNode *N, unsigned I)
bool isSpvIntrinsic(const MachineInstr &MI, Intrinsic::ID IntrinsicID)
void swap(llvm::BitVector &LHS, llvm::BitVector &RHS)
Implement std::swap in terms of BitVector swap.