File: AMDGPUInsertSingleUseVDST.cpp

package info (click to toggle)
llvm-toolchain-19 1%3A19.1.7-6
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 1,998,552 kB
  • sloc: cpp: 6,951,682; ansic: 1,486,157; asm: 913,598; python: 232,024; f90: 80,126; objc: 75,281; lisp: 37,276; pascal: 16,990; sh: 10,009; ml: 5,058; perl: 4,724; awk: 3,523; makefile: 3,177; javascript: 2,504; xml: 892; fortran: 664; cs: 573
file content (245 lines) | stat: -rw-r--r-- 8,957 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
//===- AMDGPUInsertSingleUseVDST.cpp - Insert s_singleuse_vdst instructions ==//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
/// \file
/// Insert s_singleuse_vdst instructions on GFX11.5+ to mark regions of VALU
/// instructions that produce single-use VGPR values. If the value is forwarded
/// to the consumer instruction prior to VGPR writeback, the hardware can
/// then skip (kill) the VGPR write.
//
//===----------------------------------------------------------------------===//

#include "AMDGPU.h"
#include "AMDGPUGenSearchableTables.inc"
#include "GCNSubtarget.h"
#include "SIInstrInfo.h"
#include "SIRegisterInfo.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/CodeGen/MachineFunction.h"
#include "llvm/CodeGen/MachineFunctionPass.h"
#include "llvm/CodeGen/MachineInstr.h"
#include "llvm/CodeGen/MachineInstrBuilder.h"
#include "llvm/CodeGen/MachineOperand.h"
#include "llvm/CodeGen/Register.h"
#include "llvm/IR/DebugLoc.h"
#include "llvm/MC/MCRegister.h"
#include "llvm/MC/MCRegisterInfo.h"
#include "llvm/Pass.h"
#include <array>

using namespace llvm;

#define DEBUG_TYPE "amdgpu-insert-single-use-vdst"

namespace {
class AMDGPUInsertSingleUseVDST : public MachineFunctionPass {
private:
  const SIInstrInfo *SII;
  class SingleUseInstruction {
  private:
    static const unsigned MaxSkipRange = 0b111;
    static const unsigned MaxNumberOfSkipRegions = 2;

    unsigned LastEncodedPositionEnd;
    MachineInstr *ProducerInstr;

    std::array<unsigned, MaxNumberOfSkipRegions + 1> SingleUseRegions;
    SmallVector<unsigned, MaxNumberOfSkipRegions> SkipRegions;

    // Adds a skip region into the instruction.
    void skip(const unsigned ProducerPosition) {
      while (LastEncodedPositionEnd + MaxSkipRange < ProducerPosition) {
        SkipRegions.push_back(MaxSkipRange);
        LastEncodedPositionEnd += MaxSkipRange;
      }
      SkipRegions.push_back(ProducerPosition - LastEncodedPositionEnd);
      LastEncodedPositionEnd = ProducerPosition;
    }

    bool currentRegionHasSpace() {
      const auto Region = SkipRegions.size();
      // The first region has an extra bit of encoding space.
      return SingleUseRegions[Region] <
             ((Region == MaxNumberOfSkipRegions) ? 0b1111U : 0b111U);
    }

    unsigned encodeImm() {
      // Handle the first Single Use Region separately as it has an extra bit
      // of encoding space.
      unsigned Imm = SingleUseRegions[SkipRegions.size()];
      unsigned ShiftAmount = 4;
      for (unsigned i = SkipRegions.size(); i > 0; i--) {
        Imm |= SkipRegions[i - 1] << ShiftAmount;
        ShiftAmount += 3;
        Imm |= SingleUseRegions[i - 1] << ShiftAmount;
        ShiftAmount += 3;
      }
      return Imm;
    }

  public:
    SingleUseInstruction(const unsigned ProducerPosition,
                         MachineInstr *Producer)
        : LastEncodedPositionEnd(ProducerPosition + 1), ProducerInstr(Producer),
          SingleUseRegions({1, 0, 0}) {}

    // Returns false if adding a new single use producer failed. This happens
    // because it could not be encoded, either because there is no room to
    // encode another single use producer region or that this single use
    // producer is too far away to encode the amount of instructions to skip.
    bool tryAddProducer(const unsigned ProducerPosition, MachineInstr *MI) {
      // Producer is too far away to encode into this instruction or another
      // skip region is needed and SkipRegions.size() = 2 so there's no room for
      // another skip region, therefore a new instruction is needed.
      if (LastEncodedPositionEnd +
              (MaxSkipRange * (MaxNumberOfSkipRegions - SkipRegions.size())) <
          ProducerPosition)
        return false;

      // If a skip region is needed.
      if (LastEncodedPositionEnd != ProducerPosition ||
          !currentRegionHasSpace()) {
        // If the current region is out of space therefore a skip region would
        // be needed, but there is no room for another skip region.
        if (SkipRegions.size() == MaxNumberOfSkipRegions)
          return false;
        skip(ProducerPosition);
      }

      SingleUseRegions[SkipRegions.size()]++;
      LastEncodedPositionEnd = ProducerPosition + 1;
      ProducerInstr = MI;
      return true;
    }

    auto emit(const SIInstrInfo *SII) {
      return BuildMI(*ProducerInstr->getParent(), ProducerInstr, DebugLoc(),
                     SII->get(AMDGPU::S_SINGLEUSE_VDST))
          .addImm(encodeImm());
    }
  };

public:
  static char ID;

  AMDGPUInsertSingleUseVDST() : MachineFunctionPass(ID) {}

  void insertSingleUseInstructions(
      ArrayRef<std::pair<unsigned, MachineInstr *>> SingleUseProducers) const {
    SmallVector<SingleUseInstruction> Instructions;

    for (auto &[Position, MI] : SingleUseProducers) {
      // Encode this position into the last single use instruction if possible.
      if (Instructions.empty() ||
          !Instructions.back().tryAddProducer(Position, MI)) {
        // If not, add a new instruction.
        Instructions.push_back(SingleUseInstruction(Position, MI));
      }
    }

    for (auto &Instruction : Instructions)
      Instruction.emit(SII);
  }

  bool runOnMachineFunction(MachineFunction &MF) override {
    const auto &ST = MF.getSubtarget<GCNSubtarget>();
    if (!ST.hasVGPRSingleUseHintInsts())
      return false;

    SII = ST.getInstrInfo();
    const auto *TRI = &SII->getRegisterInfo();
    bool InstructionEmitted = false;

    for (MachineBasicBlock &MBB : MF) {
      DenseMap<MCRegUnit, unsigned> RegisterUseCount;

      // Handle boundaries at the end of basic block separately to avoid
      // false positives. If they are live at the end of a basic block then
      // assume it has more uses later on.
      for (const auto &Liveout : MBB.liveouts()) {
        for (MCRegUnitMaskIterator Units(Liveout.PhysReg, TRI); Units.isValid();
             ++Units) {
          const auto [Unit, Mask] = *Units;
          if ((Mask & Liveout.LaneMask).any())
            RegisterUseCount[Unit] = 2;
        }
      }

      SmallVector<std::pair<unsigned, MachineInstr *>>
          SingleUseProducerPositions;

      unsigned VALUInstrCount = 0;
      for (MachineInstr &MI : reverse(MBB.instrs())) {
        // All registers in all operands need to be single use for an
        // instruction to be marked as a single use producer.
        bool AllProducerOperandsAreSingleUse = true;

        // Gather a list of Registers used before updating use counts to avoid
        // double counting registers that appear multiple times in a single
        // MachineInstr.
        SmallVector<MCRegUnit> RegistersUsed;

        for (const auto &Operand : MI.all_defs()) {
          const auto Reg = Operand.getReg();

          const auto RegUnits = TRI->regunits(Reg);
          if (any_of(RegUnits, [&RegisterUseCount](const MCRegUnit Unit) {
                return RegisterUseCount[Unit] > 1;
              }))
            AllProducerOperandsAreSingleUse = false;

          // Reset uses count when a register is no longer live.
          for (const MCRegUnit Unit : RegUnits)
            RegisterUseCount.erase(Unit);
        }

        for (const auto &Operand : MI.all_uses()) {
          const auto Reg = Operand.getReg();

          // Count the number of times each register is read.
          for (const MCRegUnit Unit : TRI->regunits(Reg)) {
            if (!is_contained(RegistersUsed, Unit))
              RegistersUsed.push_back(Unit);
          }
        }
        for (const MCRegUnit Unit : RegistersUsed)
          RegisterUseCount[Unit]++;

        // Do not attempt to optimise across exec mask changes.
        if (MI.modifiesRegister(AMDGPU::EXEC, TRI) ||
            AMDGPU::isInvalidSingleUseConsumerInst(MI.getOpcode())) {
          for (auto &UsedReg : RegisterUseCount)
            UsedReg.second = 2;
        }

        if (!SIInstrInfo::isVALU(MI) ||
            AMDGPU::isInvalidSingleUseProducerInst(MI.getOpcode()))
          continue;
        if (AllProducerOperandsAreSingleUse) {
          SingleUseProducerPositions.push_back({VALUInstrCount, &MI});
          InstructionEmitted = true;
        }
        VALUInstrCount++;
      }
      insertSingleUseInstructions(SingleUseProducerPositions);
    }
    return InstructionEmitted;
  }
};
} // namespace

char AMDGPUInsertSingleUseVDST::ID = 0;

char &llvm::AMDGPUInsertSingleUseVDSTID = AMDGPUInsertSingleUseVDST::ID;

INITIALIZE_PASS(AMDGPUInsertSingleUseVDST, DEBUG_TYPE,
                "AMDGPU Insert SingleUseVDST", false, false)