1//===------------------- AMDGPUCustomBehaviour.h ----------------*-C++ -* -===//
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/// \file
9///
10/// This file defines the AMDGPUCustomBehaviour class which inherits from
11/// CustomBehaviour. This class is used by the tool llvm-mca to enforce
12/// target specific behaviour that is not expressed well enough in the
13/// scheduling model for mca to enforce it automatically.
14///
15//===----------------------------------------------------------------------===//
16
17#ifndef LLVM_LIB_TARGET_AMDGPU_MCA_AMDGPUCUSTOMBEHAVIOUR_H
18#define LLVM_LIB_TARGET_AMDGPU_MCA_AMDGPUCUSTOMBEHAVIOUR_H
19
20#include "Utils/AMDGPUBaseInfo.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/MCA/CustomBehaviour.h"
23
24namespace llvm {
25namespace mca {
26
27class AMDGPUInstrPostProcess : public InstrPostProcess {
28 void processWaitCnt(Instruction &Inst, const MCInst &MCI);
29
30public:
31 AMDGPUInstrPostProcess(const MCSubtargetInfo &STI, const MCInstrInfo &MCII)
32 : InstrPostProcess(STI, MCII) {}
33
34 ~AMDGPUInstrPostProcess() override = default;
35
36 void postProcessInstruction(Instruction &Inst, const MCInst &MCI) override;
37};
38
39struct WaitCntInfo {
40 bool VmCnt = false;
41 bool ExpCnt = false;
42 bool LgkmCnt = false;
43 bool VsCnt = false;
44};
45
46class AMDGPUCustomBehaviour : public CustomBehaviour {
47 /// Whenever MCA would like to dispatch an s_waitcnt instructions,
48 /// we must check all the instruction that are still executing to see if
49 /// they modify the same CNT as we need to wait for. This vector
50 /// gets built in the constructor and contains 1 WaitCntInfo struct
51 /// for each instruction within the SrcManager. Each element
52 /// tells us which CNTs that instruction may interact with.
53 /// We conservatively assume some instructions interact with more
54 /// CNTs than they do in reality, so we will occasionally wait
55 /// longer than necessary, but we shouldn't ever wait for shorter.
56 std::vector<WaitCntInfo> InstrWaitCntInfo;
57
58 /// This method gets called from the constructor and is
59 /// where we setup the InstrWaitCntInfo vector.
60 /// The core logic for determining which CNTs an instruction
61 /// interacts with is taken from SIInsertWaitcnts::updateEventWaitcntAfter().
62 /// Unfortunately, some of the logic from that function is not available to us
63 /// in this scope so we conservatively end up assuming that some
64 /// instructions interact with more CNTs than they do in reality.
65 void generateWaitCntInfo();
66 /// Helper function used in generateWaitCntInfo()
67 bool hasModifiersSet(const std::unique_ptr<Instruction> &Inst,
68 AMDGPU::OpName OpName) const;
69 /// Helper function used in generateWaitCntInfo()
70 bool isAlwaysGDS(uint32_t Opcode) const;
71 /// This method gets called from checkCustomHazard when mca is attempting to
72 /// dispatch an s_waitcnt instruction (or one of its variants). The method
73 /// looks at each of the instructions that are still executing in the pipeline
74 /// to determine if the waitcnt should force a wait.
75 unsigned handleWaitCnt(ArrayRef<InstRef> IssuedInst, const InstRef &IR);
76 /// Based on the type of s_waitcnt instruction we are looking at, and what its
77 /// operands are, this method will set the values for each of the cnt
78 /// references provided as arguments.
79 void computeWaitCnt(const InstRef &IR, unsigned &Vmcnt, unsigned &Expcnt,
80 unsigned &Lgkmcnt, unsigned &Vscnt);
81
82public:
83 AMDGPUCustomBehaviour(const MCSubtargetInfo &STI,
84 const mca::SourceMgr &SrcMgr, const MCInstrInfo &MCII);
85
86 ~AMDGPUCustomBehaviour() override = default;
87 /// This method is used to determine if an instruction
88 /// should be allowed to be dispatched. The return value is
89 /// how many cycles until the instruction can be dispatched.
90 /// This method is called after MCA has already checked for
91 /// register and hardware dependencies so this method should only
92 /// implement custom behaviour and dependencies that are not picked up
93 /// by MCA naturally.
94 unsigned checkCustomHazard(ArrayRef<InstRef> IssuedInst,
95 const InstRef &IR) override;
96};
97} // namespace mca
98} // namespace llvm
99
100#endif
101