LLVM 19.0.0git
X86TileConfig.cpp
Go to the documentation of this file.
1//===-- X86TileConfig.cpp - Tile Register Configure----------------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9/// \file Pass to config the shape of AMX physical registers
10/// AMX register need to be configured before use. In X86PreTileConfig pass
11/// the pldtilecfg instruction is inserted, however at that time we don't
12/// know the shape of each physical tile registers, because the register
13/// allocation is not done yet. This pass runs after egister allocation
14/// pass. It collects the shape information of each physical tile register
15/// and store the shape in the stack slot that is allocated for load config
16/// to tile config register.
17//
18//===----------------------------------------------------------------------===//
19
20#include "X86.h"
21#include "X86InstrBuilder.h"
23#include "X86RegisterInfo.h"
24#include "X86Subtarget.h"
30#include "llvm/CodeGen/Passes.h"
36
37using namespace llvm;
38
39#define DEBUG_TYPE "tileconfig"
40
41namespace {
42
43struct X86TileConfig : public MachineFunctionPass {
44
45 X86TileConfig() : MachineFunctionPass(ID) {}
46
47 /// Return the pass name.
48 StringRef getPassName() const override { return "Tile Register Configure"; }
49
50 /// X86TileConfig analysis usage.
51 void getAnalysisUsage(AnalysisUsage &AU) const override {
52 AU.setPreservesAll();
56 }
57
58 /// Perform register allocation.
59 bool runOnMachineFunction(MachineFunction &mf) override;
60
63 MachineFunctionProperties::Property::NoPHIs);
64 }
65
66 static char ID;
67};
68
69} // end anonymous namespace
70
71char X86TileConfig::ID = 0;
72
73INITIALIZE_PASS_BEGIN(X86TileConfig, DEBUG_TYPE, "Tile Register Configure",
74 false, false)
77 false)
78
79bool X86TileConfig::runOnMachineFunction(MachineFunction &MF) {
81 // Early exit in the common case of non-AMX code.
82 if (X86FI->getAMXProgModel() != AMXProgModelEnum::ManagedRA)
83 return false;
84
85 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
86 const TargetRegisterInfo *TRI = ST.getRegisterInfo();
87 const TargetInstrInfo *TII = ST.getInstrInfo();
88 MachineRegisterInfo &MRI = MF.getRegInfo();
89 LiveIntervals &LIS = getAnalysis<LiveIntervalsWrapperPass>().getLIS();
90 VirtRegMap &VRM = getAnalysis<VirtRegMap>();
91
92 if (VRM.isShapeMapEmpty())
93 return false;
94
95 int SS = INT_MAX;
96 for (MachineBasicBlock &MBB : MF) {
97 for (MachineInstr &MI : MBB) {
98 if (MI.getOpcode() == X86::PLDTILECFGV) {
99 SS = MI.getOperand(0).getIndex();
100 break;
101 }
102 }
103 if (SS != INT_MAX)
104 break;
105 }
106 // Didn't find PLDTILECFGV, just return false;
107 if (SS == INT_MAX)
108 return false;
109
110 // Try to find a point to insert MIs for constant shapes.
111 // Here we are leveraging the palette id inserted in PreRA pass.
112 unsigned ConstPos = 0;
113 MachineInstr *ConstMI = nullptr;
114 for (MachineInstr &MI : MF.front()) {
115 if (MI.getOpcode() == X86::MOV8mi && SS == MI.getOperand(0).getIndex()) {
116 ConstMI = &MI;
117 break;
118 }
119 ++ConstPos;
120 }
121 assert(ConstMI && "Cannot find an insertion point");
122
123 unsigned AMXRegNum = TRI->getRegClass(X86::TILERegClassID)->getNumRegs();
124 SmallVector<Register, 8> Phys2Virt(AMXRegNum, 0);
125 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
127 if (MRI.reg_nodbg_empty(VirtReg))
128 continue;
129 if (MRI.getRegClass(VirtReg)->getID() != X86::TILERegClassID)
130 continue;
131 if (VRM.getPhys(VirtReg) == VirtRegMap::NO_PHYS_REG)
132 continue;
133 unsigned Index = VRM.getPhys(VirtReg) - X86::TMM0;
134 if (!Phys2Virt[Index])
135 Phys2Virt[Index] = VirtReg;
136 }
137
138 // Fill in the shape of each tile physical register.
139 for (unsigned I = 0; I < AMXRegNum; ++I) {
140 if (!Phys2Virt[I])
141 continue;
142 DebugLoc DL;
143 bool IsRow = true;
144 MachineInstr *NewMI = nullptr;
145 ShapeT Shape = VRM.getShape(Phys2Virt[I]);
146 for (auto &R : {Shape.getRow()->getReg(), Shape.getCol()->getReg()}) {
147 // Here is the data format for the tile config.
148 // 0 palette
149 // 1 start_row
150 // 2-15 reserved, must be zero
151 // 16-17 tile0.colsb Tile 0 bytes per row.
152 // 18-19 tile1.colsb Tile 1 bytes per row.
153 // 20-21 tile2.colsb Tile 2 bytes per row.
154 // ... (sequence continues)
155 // 30-31 tile7.colsb Tile 7 bytes per row.
156 // 32-47 reserved, must be zero
157 // 48 tile0.rows Tile 0 rows.
158 // 49 tile1.rows Tile 1 rows.
159 // 50 tile2.rows Tile 2 rows.
160 // ... (sequence continues)
161 // 55 tile7.rows Tile 7 rows.
162 // 56-63 reserved, must be zero
163 int64_t Imm = INT64_MAX;
164 int Offset = IsRow ? 48 + I : 16 + I * 2;
165 for (auto &DefMI : MRI.def_instructions(R)) {
166 MachineBasicBlock &MBB = *DefMI.getParent();
167 if (DefMI.isMoveImmediate()) {
168 if (Imm != INT64_MAX) {
169 // FIXME: We should handle this case in future.
170 assert(Imm == DefMI.getOperand(1).getImm() &&
171 "Cannot initialize with different shapes");
172 continue;
173 }
174 Imm = DefMI.getOperand(1).getImm();
175 NewMI = addFrameReference(
176 BuildMI(MF.front(), ++ConstMI->getIterator(), DL,
177 TII->get(IsRow ? X86::MOV8mi : X86::MOV16mi)),
178 SS, Offset)
179 .addImm(Imm);
180 ConstMI = NewMI;
181 LIS.InsertMachineInstrInMaps(*NewMI);
182 } else {
183 unsigned SubIdx = IsRow ? X86::sub_8bit : X86::sub_16bit;
184 unsigned RegSize = TRI->getRegSizeInBits(*MRI.getRegClass(R));
185 if ((IsRow && RegSize == 8) || (!IsRow && RegSize == 16))
186 SubIdx = 0;
187 auto Iter = DefMI.getIterator();
188 if (&MBB == &MF.front() &&
189 (unsigned)std::distance(MBB.instr_begin(), Iter) < ConstPos)
190 Iter = ConstMI->getIterator();
191 NewMI = addFrameReference(
192 BuildMI(MBB, ++Iter, DL,
193 TII->get(IsRow ? X86::MOV8mr : X86::MOV16mr)),
194 SS, Offset)
195 .addReg(R, 0, SubIdx);
196 SlotIndex SIdx = LIS.InsertMachineInstrInMaps(*NewMI);
197 LIS.extendToIndices(LIS.getInterval(R), {SIdx.getRegSlot()});
198 }
199 }
200 IsRow = false;
201 }
202 }
203 return true;
204}
205
206FunctionPass *llvm::createX86TileConfigPass() { return new X86TileConfig(); }
unsigned const MachineRegisterInfo * MRI
MachineInstrBuilder MachineInstrBuilder & DefMI
unsigned RegSize
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition: MD5.cpp:58
unsigned const TargetRegisterInfo * TRI
static unsigned getReg(const MCDisassembler *D, unsigned RC, unsigned RegNo)
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition: PassSupport.h:55
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:59
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:52
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Tile Register Configure
#define DEBUG_TYPE
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
A debug info location.
Definition: DebugLoc.h:33
FunctionPass class - This class is used to implement most global optimizations.
Definition: Pass.h:311
SlotIndex InsertMachineInstrInMaps(MachineInstr &MI)
LiveInterval & getInterval(Register Reg)
void extendToIndices(LiveRange &LR, ArrayRef< SlotIndex > Indices, ArrayRef< SlotIndex > Undefs)
Extend the live range LR to reach all points in Indices.
instr_iterator instr_begin()
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.
virtual bool runOnMachineFunction(MachineFunction &MF)=0
runOnMachineFunction - This method must be overloaded to perform the desired machine code transformat...
virtual MachineFunctionProperties getRequiredProperties() const
Properties which a MachineFunction may have at a given point in time.
MachineFunctionProperties & set(Property P)
const MachineInstrBuilder & addImm(int64_t Val) const
Add a new immediate operand.
const MachineInstrBuilder & addReg(Register RegNo, unsigned flags=0, unsigned SubReg=0) const
Add a new virtual register operand.
Representation of each machine instruction.
Definition: MachineInstr.h:69
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
Wrapper class representing virtual and physical registers.
Definition: Register.h:19
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition: Register.h:84
MachineOperand * getRow() const
Definition: TileShapeInfo.h:56
MachineOperand * getCol() const
Definition: TileShapeInfo.h:58
SlotIndex - An opaque wrapper around machine indexes.
Definition: SlotIndexes.h:65
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
ShapeT getShape(Register virtReg) const
Definition: VirtRegMap.h:120
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition: VirtRegMap.h:105
bool isShapeMapEmpty() const
Definition: VirtRegMap.h:114
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
AMXProgModelEnum getAMXProgModel() const
self_iterator getIterator()
Definition: ilist_node.h:132
#define INT64_MAX
Definition: DataTypes.h:71
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
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...
FunctionPass * createX86TileConfigPass()
Return a pass that config the tile registers.