46#define DEBUG_TYPE "x86-pre-tile-config"
52 ": Failed to config tile register, please define the shape earlier");
58 MachineInstr *MI =
nullptr;
59 MachineBasicBlock *MBB =
nullptr;
63 MIRef(MachineBasicBlock *MBB) : MBB(MBB) {
64 for (
auto I = MBB->begin(),
E = MBB->end();
I !=
E &&
I->isPHI();
68 MIRef(MachineInstr *MI)
70 Pos(std::distance(MBB->instr_begin(), ++MI->getIterator())) {}
71 MIRef(MachineInstr *MI, MachineBasicBlock *MBB)
73 Pos(std::distance(MBB->instr_begin(), ++MI->getIterator())) {}
74 MIRef(MachineInstr *MI, MachineBasicBlock *MBB,
size_t Pos)
75 : MI(MI), MBB(MBB), Pos(Pos) {}
76 operator bool()
const {
return MBB !=
nullptr; }
78 return MI ==
RHS.MI && MBB ==
RHS.MBB;
84 return std::tie(MBB, Pos) < std::tie(
RHS.MBB,
RHS.Pos);
89 return std::tie(MBB, Pos) > std::tie(
RHS.MBB,
RHS.Pos);
96 bool HasAMXRegLiveIn =
false;
97 bool TileCfgForbidden =
false;
98 bool NeedTileCfgLiveIn =
false;
101class X86PreTileConfigImpl {
102 std::function<MachineLoopInfo *()> GetMLI;
103 MachineRegisterInfo *MRI =
nullptr;
104 const MachineLoopInfo *MLI =
nullptr;
105 SmallPtrSet<MachineInstr *, 8> DefVisited;
106 DenseMap<MachineBasicBlock *, BBInfo> BBVisitedInfo;
107 DenseMap<MachineBasicBlock *, SmallVector<MIRef, 8>> ShapeBBs;
110 bool isDestructiveCall(MachineInstr &
MI, BitVector UsableRegs) {
112 MI.operands(), [](MachineOperand &MO) { return MO.isRegMask(); });
113 if (Iter ==
MI.operands_end())
116 return !UsableRegs.
none();
120 bool isAMXInstruction(MachineInstr &
MI) {
121 if (
MI.isPHI() ||
MI.isDebugInstr() ||
MI.getNumOperands() < 3)
123 switch (
MI.getOpcode()) {
124 case X86::PTILESTOREDV:
125 case X86::PTCVTROWD2PSrteV:
126 case X86::PTCVTROWD2PSrtiV:
127 case X86::PTCVTROWPS2BF16HrteV:
128 case X86::PTCVTROWPS2BF16HrtiV:
129 case X86::PTCVTROWPS2BF16LrteV:
130 case X86::PTCVTROWPS2BF16LrtiV:
131 case X86::PTCVTROWPS2PHHrteV:
132 case X86::PTCVTROWPS2PHHrtiV:
133 case X86::PTCVTROWPS2PHLrteV:
134 case X86::PTCVTROWPS2PHLrtiV:
135 case X86::PTILEMOVROWrteV:
136 case X86::PTILEMOVROWrtiV:
137 case X86::ACE_PTCVTROWD2PSrteV:
138 case X86::ACE_PTCVTROWD2PSrtiV:
139 case X86::ACE_PTCVTROWPS2BF16HrteV:
140 case X86::ACE_PTCVTROWPS2BF16HrtiV:
141 case X86::ACE_PTCVTROWPS2BF16LrteV:
142 case X86::ACE_PTCVTROWPS2BF16LrtiV:
143 case X86::ACE_PTCVTROWPS2PHHrteV:
144 case X86::ACE_PTCVTROWPS2PHHrtiV:
145 case X86::ACE_PTCVTROWPS2PHLrteV:
146 case X86::ACE_PTCVTROWPS2PHLrtiV:
147 case X86::ACE_PTILEMOVROWrteV:
148 case X86::ACE_PTILEMOVROWrtiV:
154 MachineOperand &MO =
MI.getOperand(0);
158 if (MRI->getRegClass(MO.
getReg())->getID() != X86::TILERegClassID)
161 collectShapeInfo(
MI);
166 bool isLoopBackEdge(MachineBasicBlock *Header, MachineBasicBlock *Bottom) {
167 if (!MLI->isLoopHeader(Header))
169 auto *
ML = MLI->getLoopFor(Header);
170 if (
ML->contains(Bottom) &&
ML->isLoopLatch(Bottom))
177 void collectShapeInfo(MachineInstr &
MI);
180 bool hoistShapesInBB(MachineBasicBlock *
MBB, SmallVectorImpl<MIRef> &Shapes) {
181 MIRef &FirstAMX = BBVisitedInfo[
MBB].FirstAMX;
184 for (
auto I = FirstShapeBelowAMX,
E = Shapes.
end();
I !=
E; ++
I) {
186 if (
I->MI->mayLoadOrStore())
188 for (
auto &MO :
I->MI->operands()) {
193 if (MO.
isReg() && MIRef(MRI->getVRegDef(MO.
getReg())) > FirstAMX)
197 MBB->
insert(InsertPoint,
I->MI->removeFromParent());
206 void releaseMemory() {
209 BBVisitedInfo.clear();
213 X86PreTileConfigImpl(std::function<MachineLoopInfo *()> GetMLI)
220 X86PreTileConfigLegacy() : MachineFunctionPass(ID) {}
223 StringRef getPassName()
const override {
224 return "Tile Register Pre-configure";
228 void getAnalysisUsage(AnalysisUsage &AU)
const override {
242char X86PreTileConfigLegacy::ID = 0;
245 "Tile Register Pre-configure",
false,
false)
253 auto &Refs = ShapeBBs[
MBB];
255 if (
I == Refs.end() || *
I != MIR)
260 {
MI.getOperand(1).getReg(),
MI.getOperand(2).getReg()});
261 while (!WorkList.empty()) {
262 Register R = WorkList.pop_back_val();
264 assert(
DefMI &&
"R must has one define instruction");
269 if (
DefMI->isPHI()) {
270 for (
unsigned I = 1;
I <
DefMI->getNumOperands();
I += 2)
271 if (isLoopBackEdge(DefMBB,
DefMI->getOperand(
I + 1).getMBB()))
272 RecordShape(
DefMI, DefMBB);
274 WorkList.push_back(
DefMI->getOperand(
I).getReg());
276 RecordShape(
DefMI, DefMBB);
282 scope_exit ClearStateOnExit([
this] { releaseMemory(); });
284 X86MachineFunctionInfo *X86FI = MF.
getInfo<X86MachineFunctionInfo>();
286 if (X86FI->getAMXProgModel() != AMXProgModelEnum::ManagedRA &&
287 X86FI->getACEProgModel() != ACEProgModelEnum::ACE_ManagedRA)
291 const TargetInstrInfo *
TII =
ST.getInstrInfo();
292 const TargetRegisterInfo *
TRI =
ST.getRegisterInfo();
295 BitVector AMXRegs(
TRI->getNumRegs());
297 AMXRegs.set(X86::TMM0 +
I);
302 SmallSet<MIRef, 8> CfgNeedInsert;
304 for (
auto &
MBB : MF) {
306 auto &
Info = BBVisitedInfo[&
MBB];
307 for (
auto &
MI :
MBB) {
309 if (isAMXInstruction(
MI)) {
314 Info.NeedTileCfgLiveIn =
true;
318 }
else if (
MI.isCall() && isDestructiveCall(
MI, AMXRegs)) {
323 if (
Info.NeedTileCfgLiveIn) {
329 if (
Info.FirstAMX ||
Info.HasAMXRegLiveIn)
331 if (!isLoopBackEdge(Succ, &
MBB))
332 BBVisitedInfo[Succ].HasAMXRegLiveIn =
true;
336 while (!CfgLiveInBBs.
empty()) {
342 }
else if (!
Info.NeedTileCfgLiveIn) {
343 Info.NeedTileCfgLiveIn =
true;
344 if (
Pred == &MF.front())
353 if (CfgNeedInsert.
empty())
358 for (
auto &
I : ShapeBBs) {
359 auto &
Info = BBVisitedInfo[
I.first];
361 if (
Info.HasAMXRegLiveIn) {
368 if (
Info.FirstAMX &&
Info.FirstAMX <
I.second.back() &&
369 !hoistShapesInBB(
I.first,
I.second)) {
375 while (!WorkList.
empty()) {
379 if (!
Info.TileCfgForbidden && !isLoopBackEdge(
MBB,
Pred)) {
380 Info.TileCfgForbidden =
true;
387 SmallSet<MIRef, 8> VisitedOrInserted;
388 int SS = MF.getFrameInfo().CreateStackObject(
389 ST.getTileConfigSize(),
ST.getTileConfigAlignment(),
false);
392 for (
const auto &
I : CfgNeedInsert) {
393 SmallSet<MIRef, 8> InsertPoints;
395 while (!WorkList.
empty()) {
397 if (!VisitedOrInserted.
count(
I)) {
398 if (!BBVisitedInfo[
I.MBB].TileCfgForbidden) {
406 for (
auto *Succ :
I.MBB->successors())
407 if (BBVisitedInfo[Succ].NeedTileCfgLiveIn)
414 for (MIRef
I : InsertPoints) {
416 auto It = ShapeBBs.find(
I.MBB);
417 if (It != ShapeBBs.end() &&
I < It->second.back())
418 I = It->second.back();
421 if (VisitedOrInserted.
insert(
I).second) {
423 I.MI ? std::next(
I.MI->getIterator()) :
I.
MBB->getFirstNonPHI();
432 MachineBasicBlock &
MBB = MF.
front();
434 if (
ST.hasAVX512()) {
443 }
else if (
ST.hasAVX2()) {
455 assert(
ST.hasSSE2() &&
"AMX should assume SSE2 enabled");
456 unsigned StoreOpc =
ST.hasAVX() ? X86::VMOVUPSmr : X86::MOVUPSmr;
470 X86FI->getACEProgModel() == ACEProgModelEnum::ACE_ManagedRA ? 2 : 1;
478 return new X86PreTileConfigLegacy();
481bool X86PreTileConfigLegacy::runOnMachineFunction(
MachineFunction &MF) {
482 X86PreTileConfigImpl Impl(
483 [
this]() {
return &getAnalysis<MachineLoopInfoWrapperPass>().getLI(); });
484 return Impl.runOnMachineFunction(MF);
490 X86PreTileConfigImpl Impl(
492 return Impl.runOnMachineFunction(MF)
MachineInstrBuilder MachineInstrBuilder & DefMI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
const AArch64InstrInfo & TII
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static const Function * getParent(const Value *V)
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
Module.h This file contains the declarations for the Module class.
Register const TargetRegisterInfo * TRI
Promote Memory to Register
uint64_t IntrinsicInst * II
#define INITIALIZE_PASS_DEPENDENCY(depName)
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
MachineRegisterInfo & MRI
This file defines the scope_exit class, which executes user-defined cleanup logic at scope exit.
This file defines the SmallSet class.
static void emitErrorMsg(MachineFunction &MF)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
void clearBitsInMask(const uint32_t *Mask, unsigned MaskWords=~0u)
Clear any bits in this vector that are set in Mask.
bool none() const
Returns true if none of the bits are set.
Represents analyses that only rely on functions' control flow.
FunctionPass class - This class is used to implement most global optimizations.
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.
LLVM_ABI void emitError(const Instruction *I, const Twine &ErrorStr)
emitError - Emit an error message to the currently installed error handler with optional location inf...
unsigned getNumRegs() const
getNumRegs - Return the number of registers in this class.
LLVM_ABI instr_iterator insert(instr_iterator I, MachineInstr *M)
Insert MI into the instruction list before I, possibly inside a bundle.
iterator_range< succ_iterator > successors()
iterator_range< pred_iterator > predecessors()
MachineInstrBundleIterator< MachineInstr > iterator
MachineFunctionPass - This class adapts the FunctionPass interface to allow convenient creation of pa...
void getAnalysisUsage(AnalysisUsage &AU) const override
getAnalysisUsage - Subclasses that override getAnalysisUsage must call this.
const TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
StringRef getName() const
getName - Return the name of the corresponding LLVM function.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
Function & getFunction()
Return the LLVM function that this machine code represents.
Ty * getInfo()
getInfo - Keep track of various per-function pieces of information for backends that would like to do...
const MachineInstrBuilder & addReg(Register RegNo, RegState Flags={}, unsigned SubReg=0) const
Add a new virtual register operand.
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
Representation of each machine instruction.
void setImplicitPhysRegDefsDead()
Mark the implicit physreg defs named by the instruction description as dead.
Analysis pass that exposes the MachineLoopInfo for a machine function.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
LLVM_ABI Register createVirtualRegister(const TargetRegisterClass *RegClass, StringRef Name="")
createVirtualRegister - Create and return a new virtual register in the function with the specified r...
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Wrapper class representing virtual and physical registers.
constexpr bool isVirtual() const
Return true if the specified register number is in the virtual register namespace.
std::pair< iterator, bool > insert(PtrType Ptr)
Inserts Ptr if and only if there is no element in the container equal to Ptr.
size_type count(const T &V) const
count - Return 1 if the element is in the set, 0 otherwise.
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
self_iterator getIterator()
This is an optimization pass for GlobalISel generic memory operations.
bool operator<(int64_t V1, const APSInt &V2)
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
scope_exit(Callable) -> scope_exit< Callable >
bool operator!=(uint64_t V1, const APInt &V2)
static const MachineInstrBuilder & addFrameReference(const MachineInstrBuilder &MIB, int FI, int Offset=0, bool mem=true)
addFrameReference - This function is used to add a reference to the base of an abstract object on the...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
bool operator>(int64_t V1, const APSInt &V2)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
FunctionPass * createX86PreTileConfigLegacyPass()
auto lower_bound(R &&Range, T &&Value)
Provide wrappers to std::lower_bound which take ranges instead of having to pass begin/end explicitly...
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
MCRegisterClass TargetRegisterClass