X86TileConfig.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. #include "X86.h"
  20. #include "X86InstrBuilder.h"
  21. #include "X86MachineFunctionInfo.h"
  22. #include "X86RegisterInfo.h"
  23. #include "X86Subtarget.h"
  24. #include "llvm/CodeGen/LiveIntervals.h"
  25. #include "llvm/CodeGen/MachineFrameInfo.h"
  26. #include "llvm/CodeGen/MachineFunctionPass.h"
  27. #include "llvm/CodeGen/MachineInstr.h"
  28. #include "llvm/CodeGen/MachineRegisterInfo.h"
  29. #include "llvm/CodeGen/Passes.h"
  30. #include "llvm/CodeGen/TargetInstrInfo.h"
  31. #include "llvm/CodeGen/TargetRegisterInfo.h"
  32. #include "llvm/CodeGen/TileShapeInfo.h"
  33. #include "llvm/CodeGen/VirtRegMap.h"
  34. #include "llvm/InitializePasses.h"
  35. using namespace llvm;
  36. #define DEBUG_TYPE "tile-config"
  37. namespace {
  38. struct X86TileConfig : public MachineFunctionPass {
  39. X86TileConfig() : MachineFunctionPass(ID) {}
  40. /// Return the pass name.
  41. StringRef getPassName() const override { return "Tile Register Configure"; }
  42. /// X86TileConfig analysis usage.
  43. void getAnalysisUsage(AnalysisUsage &AU) const override {
  44. AU.setPreservesAll();
  45. AU.addRequired<VirtRegMap>();
  46. AU.addRequired<LiveIntervals>();
  47. MachineFunctionPass::getAnalysisUsage(AU);
  48. }
  49. /// Perform register allocation.
  50. bool runOnMachineFunction(MachineFunction &mf) override;
  51. MachineFunctionProperties getRequiredProperties() const override {
  52. return MachineFunctionProperties().set(
  53. MachineFunctionProperties::Property::NoPHIs);
  54. }
  55. static char ID;
  56. };
  57. } // end anonymous namespace
  58. char X86TileConfig::ID = 0;
  59. INITIALIZE_PASS_BEGIN(X86TileConfig, "tileconfig", "Tile Register Configure",
  60. false, false)
  61. INITIALIZE_PASS_DEPENDENCY(VirtRegMap)
  62. INITIALIZE_PASS_END(X86TileConfig, "tileconfig", "Tile Register Configure",
  63. false, false)
  64. bool X86TileConfig::runOnMachineFunction(MachineFunction &MF) {
  65. const X86Subtarget &ST = MF.getSubtarget<X86Subtarget>();
  66. const TargetRegisterInfo *TRI = ST.getRegisterInfo();
  67. const TargetInstrInfo *TII = ST.getInstrInfo();
  68. MachineRegisterInfo &MRI = MF.getRegInfo();
  69. LiveIntervals &LIS = getAnalysis<LiveIntervals>();
  70. VirtRegMap &VRM = getAnalysis<VirtRegMap>();
  71. if (VRM.isShapeMapEmpty())
  72. return false;
  73. int SS = INT_MAX;
  74. for (MachineBasicBlock &MBB : MF) {
  75. for (MachineInstr &MI : MBB) {
  76. if (MI.getOpcode() == X86::LDTILECFG) {
  77. SS = MI.getOperand(0).getIndex();
  78. break;
  79. }
  80. }
  81. if (SS != INT_MAX)
  82. break;
  83. }
  84. // Try to find a point to insert MIs for constant shapes.
  85. // Here we are leveraging the palette id inserted in PreRA pass.
  86. unsigned ConstPos = 0;
  87. MachineInstr *ConstMI = nullptr;
  88. for (MachineInstr &MI : MF.front()) {
  89. if (MI.getOpcode() == X86::MOV8mi && SS == MI.getOperand(0).getIndex()) {
  90. ConstMI = &MI;
  91. break;
  92. }
  93. ++ConstPos;
  94. }
  95. assert(ConstMI && "Cannot find an insertion point");
  96. unsigned AMXRegNum = TRI->getRegClass(X86::TILERegClassID)->getNumRegs();
  97. SmallVector<Register, 8> Phys2Virt(AMXRegNum, 0);
  98. for (unsigned I = 0, E = MRI.getNumVirtRegs(); I != E; ++I) {
  99. Register VirtReg = Register::index2VirtReg(I);
  100. if (MRI.reg_nodbg_empty(VirtReg))
  101. continue;
  102. if (MRI.getRegClass(VirtReg)->getID() != X86::TILERegClassID)
  103. continue;
  104. unsigned Index = VRM.getPhys(VirtReg) - X86::TMM0;
  105. if (!Phys2Virt[Index])
  106. Phys2Virt[Index] = VirtReg;
  107. }
  108. // Fill in the shape of each tile physical register.
  109. for (unsigned I = 0; I < AMXRegNum; ++I) {
  110. if (!Phys2Virt[I])
  111. continue;
  112. DebugLoc DL;
  113. bool IsRow = true;
  114. MachineInstr *NewMI = nullptr;
  115. ShapeT Shape = VRM.getShape(Phys2Virt[I]);
  116. for (auto &R : {Shape.getRow()->getReg(), Shape.getCol()->getReg()}) {
  117. // Here is the data format for the tile config.
  118. // 0 palette
  119. // 1 start_row
  120. // 2-15 reserved, must be zero
  121. // 16-17 tile0.colsb Tile 0 bytes per row.
  122. // 18-19 tile1.colsb Tile 1 bytes per row.
  123. // 20-21 tile2.colsb Tile 2 bytes per row.
  124. // ... (sequence continues)
  125. // 30-31 tile7.colsb Tile 7 bytes per row.
  126. // 32-47 reserved, must be zero
  127. // 48 tile0.rows Tile 0 rows.
  128. // 49 tile1.rows Tile 1 rows.
  129. // 50 tile2.rows Tile 2 rows.
  130. // ... (sequence continues)
  131. // 55 tile7.rows Tile 7 rows.
  132. // 56-63 reserved, must be zero
  133. int64_t Imm = INT64_MAX;
  134. int Offset = IsRow ? 48 + I : 16 + I * 2;
  135. for (auto &DefMI : MRI.def_instructions(R)) {
  136. MachineBasicBlock &MBB = *DefMI.getParent();
  137. if (DefMI.isMoveImmediate()) {
  138. if (Imm != INT64_MAX) {
  139. // FIXME: We should handle this case in future.
  140. assert(Imm == DefMI.getOperand(1).getImm() &&
  141. "Cannot initialize with different shapes");
  142. continue;
  143. }
  144. Imm = DefMI.getOperand(1).getImm();
  145. NewMI = addFrameReference(
  146. BuildMI(MF.front(), ++ConstMI->getIterator(), DL,
  147. TII->get(IsRow ? X86::MOV8mi : X86::MOV16mi)),
  148. SS, Offset)
  149. .addImm(Imm);
  150. ConstMI = NewMI;
  151. LIS.InsertMachineInstrInMaps(*NewMI);
  152. } else {
  153. unsigned SubIdx = IsRow ? X86::sub_8bit : X86::sub_16bit;
  154. unsigned RegSize = TRI->getRegSizeInBits(*MRI.getRegClass(R));
  155. if ((IsRow && RegSize == 8) || (!IsRow && RegSize == 16))
  156. SubIdx = 0;
  157. auto Iter = DefMI.getIterator();
  158. if (&MBB == &MF.front() &&
  159. (unsigned)std::distance(MBB.instr_begin(), Iter) < ConstPos)
  160. Iter = ConstMI->getIterator();
  161. NewMI = addFrameReference(
  162. BuildMI(MBB, ++Iter, DL,
  163. TII->get(IsRow ? X86::MOV8mr : X86::MOV16mr)),
  164. SS, Offset)
  165. .addReg(R, 0, SubIdx);
  166. SlotIndex SIdx = LIS.InsertMachineInstrInMaps(*NewMI);
  167. LIS.extendToIndices(LIS.getInterval(R), {SIdx.getRegSlot()});
  168. }
  169. }
  170. IsRow = false;
  171. }
  172. }
  173. return true;
  174. }
  175. FunctionPass *llvm::createX86TileConfigPass() { return new X86TileConfig(); }