LLVM 24.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 "X86Subtarget.h"
29#include "llvm/CodeGen/Passes.h"
35
36using namespace llvm;
37
38#define DEBUG_TYPE "x86-tile-config"
39
40namespace {
41
42struct X86TileConfigLegacy : public MachineFunctionPass {
43
44 X86TileConfigLegacy() : MachineFunctionPass(ID) {}
45
46 /// Return the pass name.
47 StringRef getPassName() const override { return "Tile Register Configure"; }
48
49 /// X86TileConfig analysis usage.
50 void getAnalysisUsage(AnalysisUsage &AU) const override {
51 AU.setPreservesAll();
55 }
56
57 /// Perform register allocation.
58 bool runOnMachineFunction(MachineFunction &MF) override;
59
60 MachineFunctionProperties getRequiredProperties() const override {
61 return MachineFunctionProperties().setNoPHIs();
62 }
63
64 static char ID;
65};
66
67} // end anonymous namespace
68
69char X86TileConfigLegacy::ID = 0;
70
71INITIALIZE_PASS_BEGIN(X86TileConfigLegacy, DEBUG_TYPE,
72 "Tile Register Configure", false, false)
74INITIALIZE_PASS_END(X86TileConfigLegacy, DEBUG_TYPE, "Tile Register Configure",
76
78 llvm::function_ref<LiveIntervals *()> GetLIs,
79 llvm::function_ref<VirtRegMap *()> GetVRM) {
81 // Early exit in the common case of non-AMX/ACE code.
84 return false;
85
86 const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
87 const X86RegisterInfo *TRI = ST.getRegisterInfo();
88 const TargetInstrInfo *TII = ST.getInstrInfo();
89 MachineRegisterInfo &MRI = MF.getRegInfo();
90 LiveIntervals &LIS = *GetLIs();
91 VirtRegMap &VRM = *GetVRM();
92
93 if (VRM.isShapeMapEmpty())
94 return false;
95
96 int SS = INT_MAX;
97 for (MachineBasicBlock &MBB : MF) {
98 for (MachineInstr &MI : MBB) {
99 if (MI.getOpcode() == X86::PLDTILECFGV) {
100 SS = MI.getOperand(0).getIndex();
101 break;
102 }
103 }
104 if (SS != INT_MAX)
105 break;
106 }
107 // Didn't find PLDTILECFGV, just return false;
108 if (SS == INT_MAX)
109 return false;
110
111 // Try to find a point to insert MIs for constant shapes.
112 // Here we are leveraging the palette id inserted in PreRA pass.
113 unsigned ConstPos = 0;
114 MachineInstr *ConstMI = nullptr;
115 for (MachineInstr &MI : MF.front()) {
116 if (MI.getOpcode() == X86::MOV8mi && SS == MI.getOperand(0).getIndex()) {
117 ConstMI = &MI;
118 break;
119 }
120 ++ConstPos;
121 }
122 assert(ConstMI && "Cannot find an insertion point");
123
124 unsigned AMXRegNum = TRI->getRegClass(X86::TILERegClassID)->getNumRegs();
125 SmallVector<Register, 8> Phys2Virt(AMXRegNum, 0);
126 for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
128 if (MRI.reg_nodbg_empty(VirtReg))
129 continue;
130 if (!TRI->isTileRegisterClass(MRI.getRegClass(VirtReg)))
131 continue;
132 MCRegister PhysReg = VRM.getPhys(VirtReg);
133 if (!PhysReg)
134 continue;
135 unsigned Index = PhysReg - X86::TMM0;
136 if (!Phys2Virt[Index])
137 Phys2Virt[Index] = VirtReg;
138 }
139
140 // Fill in the shape of each tile physical register.
141 // ACE Palette 2 uses fixed tile dimensions (16x64), so skip writing
142 // shapes - bytes 1-63 must remain zero for ACE.
144 return true;
145
146 for (unsigned I = 0; I < AMXRegNum; ++I) {
147 if (!Phys2Virt[I])
148 continue;
149 DebugLoc DL;
150 bool IsRow = true;
151 MachineInstr *NewMI = nullptr;
152 ShapeT Shape = VRM.getShape(Phys2Virt[I]);
153 for (auto &R : {Shape.getRow()->getReg(), Shape.getCol()->getReg()}) {
154 // Here is the data format for the tile config.
155 // 0 palette
156 // 1 start_row
157 // 2-15 reserved, must be zero
158 // 16-17 tile0.colsb Tile 0 bytes per row.
159 // 18-19 tile1.colsb Tile 1 bytes per row.
160 // 20-21 tile2.colsb Tile 2 bytes per row.
161 // ... (sequence continues)
162 // 30-31 tile7.colsb Tile 7 bytes per row.
163 // 32-47 reserved, must be zero
164 // 48 tile0.rows Tile 0 rows.
165 // 49 tile1.rows Tile 1 rows.
166 // 50 tile2.rows Tile 2 rows.
167 // ... (sequence continues)
168 // 55 tile7.rows Tile 7 rows.
169 // 56-63 reserved, must be zero
170 int64_t Imm = INT64_MAX;
171 int Offset = IsRow ? 48 + I : 16 + I * 2;
172 for (auto &DefMI : MRI.def_instructions(R)) {
173 MachineBasicBlock &MBB = *DefMI.getParent();
174 if (DefMI.isMoveImmediate()) {
175 if (Imm != INT64_MAX) {
176 // FIXME: We should handle this case in future.
177 assert(Imm == DefMI.getOperand(1).getImm() &&
178 "Cannot initialize with different shapes");
179 continue;
180 }
181 if (DefMI.getOperand(1).isImm()) {
182 Imm = DefMI.getOperand(1).getImm();
183 } else {
184 assert(DefMI.getOpcode() == X86::MOV32r0 &&
185 "The opcode is assumed to be MOV32r0 if the operand is not "
186 "immediate.");
187 Imm = 0;
188 }
189
190 NewMI = addFrameReference(
191 BuildMI(MF.front(), ++ConstMI->getIterator(), DL,
192 TII->get(IsRow ? X86::MOV8mi : X86::MOV16mi)),
193 SS, Offset)
194 .addImm(Imm);
195 ConstMI = NewMI;
196 LIS.InsertMachineInstrInMaps(*NewMI);
197 } else {
198 unsigned SubIdx = IsRow ? X86::sub_8bit : X86::sub_16bit;
199 unsigned RegSize = TRI->getRegSizeInBits(*MRI.getRegClass(R));
200 if ((IsRow && RegSize == 8) || (!IsRow && RegSize == 16))
201 SubIdx = 0;
202 auto Iter = DefMI.getIterator();
203 if (&MBB == &MF.front() &&
204 (unsigned)std::distance(MBB.instr_begin(), Iter) < ConstPos)
205 Iter = ConstMI->getIterator();
206 NewMI = addFrameReference(
207 BuildMI(MBB, ++Iter, DL,
208 TII->get(IsRow ? X86::MOV8mr : X86::MOV16mr)),
209 SS, Offset)
210 .addReg(R, {}, SubIdx);
211 SlotIndex SIdx = LIS.InsertMachineInstrInMaps(*NewMI);
212 LIS.extendToIndices(LIS.getInterval(R), {SIdx.getRegSlot()});
213 }
214 }
215 IsRow = false;
216 }
217 }
218 return true;
219}
220
222 return new X86TileConfigLegacy();
223}
224
225bool X86TileConfigLegacy::runOnMachineFunction(MachineFunction &MF) {
226 return tileConfig(
227 MF,
228 [this]() { return &getAnalysis<LiveIntervalsWrapperPass>().getLIS(); },
229 [this]() { return &getAnalysis<VirtRegMapWrapperLegacy>().getVRM(); });
230}
231
234 bool Changed = tileConfig(
235 MF, [&MFAM, &MF]() { return &MFAM.getResult<LiveIntervalsAnalysis>(MF); },
236 [&MFAM, &MF]() { return &MFAM.getResult<VirtRegMapAnalysis>(MF); });
240}
MachineInstrBuilder MachineInstrBuilder & DefMI
unsigned RegSize
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
uint64_t Imm
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
static GCRegistry::Add< CoreCLRGC > E("coreclr", "CoreCLR-compatible GC")
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
#define I(x, y, z)
Definition MD5.cpp:57
Register const TargetRegisterInfo * TRI
#define INITIALIZE_PASS_DEPENDENCY(depName)
Definition PassSupport.h:42
#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)
Definition PassSupport.h:44
#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)
Definition PassSupport.h:39
MachineRegisterInfo & MRI
Tile Register static false bool tileConfig(MachineFunction &MF, llvm::function_ref< LiveIntervals *()> GetLIs, llvm::function_ref< VirtRegMap *()> GetVRM)
PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)
Get the result of an analysis pass for a given IR unit.
Represent the analysis usage information of a pass.
AnalysisUsage & addRequired()
void setPreservesAll()
Set by analyses that do not transform their input at all.
Represents analyses that only rely on functions' control flow.
Definition Analysis.h:73
A debug info location.
Definition DebugLoc.h:126
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
Wrapper class representing physical registers and target specific virtual registers.
Definition MCRegister.h:42
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.
Properties which a MachineFunction may have at a given point in time.
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.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
static PreservedAnalyses all()
Construct a special preserved set that preserves all passes.
Definition Analysis.h:118
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
static Register index2VirtReg(unsigned Index)
Convert a 0-based index to a virtual register number.
Definition Register.h:72
MachineOperand * getRow() const
MachineOperand * getCol() const
SlotIndex - An opaque wrapper around machine indexes.
Definition SlotIndexes.h:70
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
ShapeT getShape(Register virtReg) const
Definition VirtRegMap.h:104
MCRegister getPhys(Register virtReg) const
returns the physical register mapped to the specified virtual register
Definition VirtRegMap.h:89
bool isShapeMapEmpty() const
Definition VirtRegMap.h:98
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
ACEProgModelEnum getACEProgModel() const
AMXProgModelEnum getAMXProgModel() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
An efficient, type-erasing, non-owning reference to a callable.
self_iterator getIterator()
Definition ilist_node.h:123
#define INT64_MAX
Definition DataTypes.h:71
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
MachineInstrBuilder BuildMI(MachineFunction &MF, const MIMetadata &MIMD, const MCInstrDesc &MCID)
Builder interface. Specify how to create the initial instruction itself.
FunctionPass * createX86TileConfigLegacyPass()
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
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.