LLVM 22.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 MachineFunction *MF = nullptr;
45 const TargetInstrInfo *TII = nullptr;
46 MachineRegisterInfo *MRI = nullptr;
47 const TargetRegisterInfo *TRI = nullptr;
48 X86MachineFunctionInfo *X86FI = nullptr;
49
50 bool configBasicBlock(MachineBasicBlock &MBB);
51};
52
53class X86FastTileConfigLegacy : public MachineFunctionPass {
54public:
55 X86FastTileConfigLegacy() : MachineFunctionPass(ID) {}
56
57 /// Return the pass name.
58 StringRef getPassName() const override {
59 return "Fast Tile Register Configure";
60 }
61
62 void getAnalysisUsage(AnalysisUsage &AU) const override {
63 AU.setPreservesAll();
65 }
66
67 /// Perform register allocation.
68 bool runOnMachineFunction(MachineFunction &MFunc) override;
69
70 MachineFunctionProperties getRequiredProperties() const override {
71 return MachineFunctionProperties().setNoPHIs();
72 }
73
74 static char ID;
75};
76
77} // end anonymous namespace
78
79char X86FastTileConfigLegacy::ID = 0;
80
81INITIALIZE_PASS_BEGIN(X86FastTileConfigLegacy, DEBUG_TYPE,
82 "Fast Tile Register Configure", false, false)
83INITIALIZE_PASS_END(X86FastTileConfigLegacy, DEBUG_TYPE,
84 "Fast Tile Register Configure", false, false)
85
87 // There is no phi instruction after register allocation.
88 assert(MI.isPHI() == false);
89 // The instruction must have 3 operands: tile def, row, col.
90 // It should be AMX pseudo instruction that have shape operand.
91 if (MI.isDebugInstr() || MI.isCopy() || MI.getNumOperands() < 3 ||
92 !MI.isPseudo())
93 return false;
94 MachineOperand &MO = MI.getOperand(0);
95
96 if (MO.isReg()) {
97 Register Reg = MO.getReg();
98 // FIXME: It may be used after Greedy RA and the physical
99 // register is not rewritten yet.
100 if (Reg.isVirtual()) {
101 if (MRI->getRegClass(Reg)->getID() == X86::TILERegClassID)
102 return true;
103 }
104 if (Reg >= X86::TMM0 && Reg <= X86::TMM7)
105 return true;
106 }
107
108 return false;
109}
110
111static unsigned getTMMIndex(Register Reg) {
112 if (Reg >= X86::TMM0 && Reg <= X86::TMM7)
113 return Reg - X86::TMM0;
114 llvm_unreachable("Invalid Tmm Reg!");
115}
116
117// PreTileConfig should configure the tile registers based on basic
118// block.
119bool X86FastTileConfigImpl::configBasicBlock(MachineBasicBlock &MBB) {
120 bool Change = false;
122 for (MachineInstr &MI : reverse(MBB)) {
123 if (!isTileDef(MRI, MI) && MI.getOpcode() != X86::PLDTILECFGV)
124 continue;
125 // AMX instructions that define tile register.
126 if (MI.getOpcode() != X86::PLDTILECFGV) {
127 MachineOperand &Row = MI.getOperand(1);
128 unsigned TMMIdx = getTMMIndex(MI.getOperand(0).getReg());
129 MachineOperand &Col = MI.getOperand(2);
130 ShapeInfos.push_back({TMMIdx, ShapeT(&Row, &Col)});
131 } else { // PLDTILECFGV
132 // Rewrite the shape information to memory. Stack slot should have
133 // been initialized to zero in pre config.
134 int SS = MI.getOperand(0).getIndex(); // tile config stack slot.
135 for (auto &ShapeInfo : ShapeInfos) {
136 DebugLoc DL;
137 unsigned TMMIdx = ShapeInfo.first;
138 Register RowReg = ShapeInfo.second.getRow()->getReg();
139 Register ColReg = ShapeInfo.second.getCol()->getReg();
140 // Here is the data format for the tile config.
141 // 0 palette
142 // 1 start_row
143 // 2-15 reserved, must be zero
144 // 16-17 tile0.colsb Tile 0 bytes per row.
145 // 18-19 tile1.colsb Tile 1 bytes per row.
146 // 20-21 tile2.colsb Tile 2 bytes per row.
147 // ... (sequence continues)
148 // 30-31 tile7.colsb Tile 7 bytes per row.
149 // 32-47 reserved, must be zero
150 // 48 tile0.rows Tile 0 rows.
151 // 49 tile1.rows Tile 1 rows.
152 // 50 tile2.rows Tile 2 rows.
153 // ... (sequence continues)
154 // 55 tile7.rows Tile 7 rows.
155 // 56-63 reserved, must be zero
156 int RowOffset = 48 + TMMIdx;
157 int ColOffset = 16 + TMMIdx * 2;
158
159 Register SubRowReg = TRI->getSubReg(RowReg, X86::sub_8bit);
160 BuildMI(MBB, MI, DL, TII->get(X86::IMPLICIT_DEF), SubRowReg);
161 MachineInstrBuilder StoreRow =
162 BuildMI(MBB, MI, DL, TII->get(X86::MOV8mr));
163 addFrameReference(StoreRow, SS, RowOffset).addReg(SubRowReg);
164
165 MachineInstrBuilder StoreCol =
166 BuildMI(MBB, MI, DL, TII->get(X86::MOV16mr));
167 addFrameReference(StoreCol, SS, ColOffset).addReg(ColReg);
168 }
169 ShapeInfos.clear();
170 Change = true;
171 }
172 }
173
174 return Change;
175}
176
177bool X86FastTileConfigImpl::runOnMachineFunction(MachineFunction &MFunc) {
178 X86FI = MFunc.getInfo<X86MachineFunctionInfo>();
179 // Early exit in the common case of non-AMX code.
180 if (X86FI->getAMXProgModel() != AMXProgModelEnum::ManagedRA)
181 return false;
182
183 MF = &MFunc;
184 MRI = &MFunc.getRegInfo();
185 const TargetSubtargetInfo *ST = &MFunc.getSubtarget<X86Subtarget>();
186 TRI = ST->getRegisterInfo();
187 TII = MFunc.getSubtarget().getInstrInfo();
188 bool Change = false;
189
190 // Loop over all of the basic blocks, eliminating virtual register references
191 for (MachineBasicBlock &MBB : MFunc)
192 Change |= configBasicBlock(MBB);
193
194 return Change;
195}
196
198 return new X86FastTileConfigLegacy();
199}
200
201bool X86FastTileConfigLegacy::runOnMachineFunction(MachineFunction &MF) {
202 X86FastTileConfigImpl Impl;
203 return Impl.runOnMachineFunction(MF);
204}
205
206PreservedAnalyses
209 X86FastTileConfigImpl Impl;
210 Impl.runOnMachineFunction(MF);
212}
unsigned const MachineRegisterInfo * MRI
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
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:314
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, unsigned flags=0, 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:151
Wrapper class representing virtual and physical registers.
Definition Register.h:20
void push_back(const T &Elt)
StringRef - Represent a constant reference to a string, i.e.
Definition StringRef.h:55
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.
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 Types.h:26
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:406
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...