LLVM 24.0.0git
X86FastTileConfig.cpp
Go to the documentation of this file.
1//===-- X86FastTileConfig.cpp - Fast 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. Before FastRegAllocation pass
11/// the ldtilecfg 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 register 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"
28#include "llvm/CodeGen/Passes.h"
31
32using namespace llvm;
33
34#define DEBUG_TYPE "x86-fast-tile-config"
35
36namespace {
37
38class X86FastTileConfigImpl {
39public:
40 bool runOnMachineFunction(MachineFunction &MF);
41
42private:
43 // context
44 const TargetInstrInfo *TII = nullptr;
45 MachineRegisterInfo *MRI = nullptr;
46 const TargetRegisterInfo *TRI = nullptr;
47 X86MachineFunctionInfo *X86FI = nullptr;
48
49 bool configBasicBlock(MachineBasicBlock &MBB);
50};
51
52class X86FastTileConfigLegacy : public MachineFunctionPass {
53public:
54 X86FastTileConfigLegacy() : MachineFunctionPass(ID) {}
55
56 /// Return the pass name.
57 StringRef getPassName() const override {
58 return "Fast Tile Register Configure";
59 }
60
61 void getAnalysisUsage(AnalysisUsage &AU) const override {
62 AU.setPreservesAll();
64 }
65
66 /// Perform register allocation.
67 bool runOnMachineFunction(MachineFunction &MFunc) override;
68
69 MachineFunctionProperties getRequiredProperties() const override {
70 return MachineFunctionProperties().setNoPHIs();
71 }
72
73 static char ID;
74};
75
76} // end anonymous namespace
77
78char X86FastTileConfigLegacy::ID = 0;
79
80INITIALIZE_PASS_BEGIN(X86FastTileConfigLegacy, DEBUG_TYPE,
81 "Fast Tile Register Configure", false, false)
82INITIALIZE_PASS_END(X86FastTileConfigLegacy, DEBUG_TYPE,
83 "Fast Tile Register Configure", false, false)
84
86 // There is no phi instruction after register allocation.
87 assert(MI.isPHI() == false);
88 // The instruction must have 3 operands: tile def, row, col.
89 // It should be AMX pseudo instruction that have shape operand.
90 if (MI.isDebugInstr() || MI.isCopy() || MI.getNumOperands() < 3 ||
91 !MI.isPseudo())
92 return false;
93 MachineOperand &MO = MI.getOperand(0);
94
95 if (MO.isReg()) {
96 Register Reg = MO.getReg();
97 // FIXME: It may be used after Greedy RA and the physical
98 // register is not rewritten yet.
99 if (Reg.isVirtual()) {
100 if (MRI->getRegClass(Reg)->getID() == X86::TILERegClassID)
101 return true;
102 }
103 if (Reg >= X86::TMM0 && Reg <= X86::TMM7)
104 return true;
105 }
106
107 return false;
108}
109
110static unsigned getTMMIndex(Register Reg) {
111 if (Reg >= X86::TMM0 && Reg <= X86::TMM7)
112 return Reg - X86::TMM0;
113 llvm_unreachable("Invalid Tmm Reg!");
114}
115
116// PreTileConfig should configure the tile registers based on basic
117// block.
118bool X86FastTileConfigImpl::configBasicBlock(MachineBasicBlock &MBB) {
119 bool Change = false;
121 for (MachineInstr &MI : reverse(MBB)) {
122 if (!isTileDef(MRI, MI) && MI.getOpcode() != X86::PLDTILECFGV)
123 continue;
124 // AMX instructions that define tile register.
125 if (MI.getOpcode() != X86::PLDTILECFGV) {
126 MachineOperand &Row = MI.getOperand(1);
127 unsigned TMMIdx = getTMMIndex(MI.getOperand(0).getReg());
128 MachineOperand &Col = MI.getOperand(2);
129 ShapeInfos.push_back({TMMIdx, ShapeT(&Row, &Col)});
130 } else { // PLDTILECFGV
131 // Rewrite the shape information to memory. Stack slot should have
132 // been initialized to zero in pre config.
133 int SS = MI.getOperand(0).getIndex(); // tile config stack slot.
134 for (auto &ShapeInfo : ShapeInfos) {
135 DebugLoc DL;
136 unsigned TMMIdx = ShapeInfo.first;
137 Register RowReg = ShapeInfo.second.getRow()->getReg();
138 Register ColReg = ShapeInfo.second.getCol()->getReg();
139 // Here is the data format for the tile config.
140 // 0 palette
141 // 1 start_row
142 // 2-15 reserved, must be zero
143 // 16-17 tile0.colsb Tile 0 bytes per row.
144 // 18-19 tile1.colsb Tile 1 bytes per row.
145 // 20-21 tile2.colsb Tile 2 bytes per row.
146 // ... (sequence continues)
147 // 30-31 tile7.colsb Tile 7 bytes per row.
148 // 32-47 reserved, must be zero
149 // 48 tile0.rows Tile 0 rows.
150 // 49 tile1.rows Tile 1 rows.
151 // 50 tile2.rows Tile 2 rows.
152 // ... (sequence continues)
153 // 55 tile7.rows Tile 7 rows.
154 // 56-63 reserved, must be zero
155 int RowOffset = 48 + TMMIdx;
156 int ColOffset = 16 + TMMIdx * 2;
157
158 Register SubRowReg = TRI->getSubReg(RowReg, X86::sub_8bit);
159 BuildMI(MBB, MI, DL, TII->get(X86::IMPLICIT_DEF), SubRowReg);
160 MachineInstrBuilder StoreRow =
161 BuildMI(MBB, MI, DL, TII->get(X86::MOV8mr));
162 addFrameReference(StoreRow, SS, RowOffset).addReg(SubRowReg);
163
164 MachineInstrBuilder StoreCol =
165 BuildMI(MBB, MI, DL, TII->get(X86::MOV16mr));
166 addFrameReference(StoreCol, SS, ColOffset).addReg(ColReg);
167 }
168 ShapeInfos.clear();
169 Change = true;
170 }
171 }
172
173 return Change;
174}
175
176bool X86FastTileConfigImpl::runOnMachineFunction(MachineFunction &MFunc) {
177 X86FI = MFunc.getInfo<X86MachineFunctionInfo>();
178 // Early exit in the common case of non-AMX code.
179 if (X86FI->getAMXProgModel() != AMXProgModelEnum::ManagedRA)
180 return false;
181
182 MRI = &MFunc.getRegInfo();
183 const TargetSubtargetInfo *ST = &MFunc.getSubtarget<X86Subtarget>();
184 TRI = ST->getRegisterInfo();
185 TII = MFunc.getSubtarget().getInstrInfo();
186 bool Change = false;
187
188 // Loop over all of the basic blocks, eliminating virtual register references
189 for (MachineBasicBlock &MBB : MFunc)
190 Change |= configBasicBlock(MBB);
191
192 return Change;
193}
194
196 return new X86FastTileConfigLegacy();
197}
198
199bool X86FastTileConfigLegacy::runOnMachineFunction(MachineFunction &MF) {
200 X86FastTileConfigImpl Impl;
201 return Impl.runOnMachineFunction(MF);
202}
203
204PreservedAnalyses
207 X86FastTileConfigImpl Impl;
208 Impl.runOnMachineFunction(MF);
210}
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
MachineBasicBlock & MBB
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
#define DEBUG_TYPE
const HexagonInstrInfo * TII
IRTranslator LLVM IR MI
Register Reg
Register const TargetRegisterInfo * TRI
Promote Memory to Register
Definition Mem2Reg.cpp:110
#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
static bool isTileDef(MachineRegisterInfo *MRI, MachineInstr &MI)
static unsigned getTMMIndex(Register Reg)
Represent the analysis usage information of a pass.
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
FunctionPass class - This class is used to implement most global optimizations.
Definition Pass.h:312
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 TargetSubtargetInfo & getSubtarget() const
getSubtarget - Return the subtarget for which this machine code is being compiled.
MachineRegisterInfo & getRegInfo()
getRegInfo - Return information about the registers currently in use.
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.
Representation of each machine instruction.
MachineOperand class - Representation of each machine instruction operand.
bool isReg() const
isReg - Tests if this is a MO_Register operand.
Register getReg() const
getReg - Returns the register number.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
PreservedAnalyses & preserveSet()
Mark an analysis set as preserved.
Definition Analysis.h:152
Wrapper class representing virtual and physical registers.
Definition Register.h:20
void push_back(const T &Elt)
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
TargetInstrInfo - Interface to description of machine instruction set.
TargetRegisterInfo base class - We assume that the target defines a static array of TargetRegisterDes...
virtual const TargetInstrInfo * getInstrInfo() const
PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &MFAM)
X86MachineFunctionInfo - This class is derived from MachineFunction and contains private X86 target-s...
AMXProgModelEnum getAMXProgModel() const
#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.
FunctionPass * createX86FastTileConfigLegacyPass()
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...
AnalysisManager< MachineFunction > MachineFunctionAnalysisManager
LLVM_ABI PreservedAnalyses getMachineFunctionPassPreservedAnalyses()
Returns the minimum set of Analyses that all machine function passes must preserve.
auto reverse(ContainerTy &&C)
Definition STLExtras.h:408
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...