Skip to content

Commit dbed061

Browse files
committed
[MCA] Moving the target specific CustomBehaviour impl. from /tools/llvm-mca/ to /lib/Target/.
Differential Revision: https://reviews.llvm.org/D106775
1 parent c4cb9b6 commit dbed061

18 files changed

+263
-126
lines changed

llvm/CMakeLists.txt

+9
Original file line numberDiff line numberDiff line change
@@ -718,6 +718,7 @@ set(LLVM_ENUM_TARGETS "")
718718
set(LLVM_ENUM_ASM_PRINTERS "")
719719
set(LLVM_ENUM_ASM_PARSERS "")
720720
set(LLVM_ENUM_DISASSEMBLERS "")
721+
set(LLVM_ENUM_TARGETMCAS "")
721722
foreach(t ${LLVM_TARGETS_TO_BUILD})
722723
set( td ${LLVM_MAIN_SRC_DIR}/lib/Target/${t} )
723724

@@ -747,6 +748,10 @@ foreach(t ${LLVM_TARGETS_TO_BUILD})
747748
set(LLVM_ENUM_DISASSEMBLERS
748749
"${LLVM_ENUM_DISASSEMBLERS}LLVM_DISASSEMBLER(${t})\n")
749750
endif()
751+
if( EXISTS ${td}/MCA/CMakeLists.txt )
752+
set(LLVM_ENUM_TARGETMCAS
753+
"${LLVM_ENUM_TARGETMCAS}LLVM_TARGETMCA(${t})\n")
754+
endif()
750755
endforeach(t)
751756

752757
# Produce the target definition files, which provide a way for clients to easily
@@ -767,6 +772,10 @@ configure_file(
767772
${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/Targets.def.in
768773
${LLVM_INCLUDE_DIR}/llvm/Config/Targets.def
769774
)
775+
configure_file(
776+
${LLVM_MAIN_INCLUDE_DIR}/llvm/Config/TargetMCAs.def.in
777+
${LLVM_INCLUDE_DIR}/llvm/Config/TargetMCAs.def
778+
)
770779

771780
# They are not referenced. See set_output_directory().
772781
set( CMAKE_RUNTIME_OUTPUT_DIRECTORY ${LLVM_BINARY_DIR}/bin )

llvm/cmake/modules/LLVM-Config.cmake

+7
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,13 @@ function(llvm_expand_pseudo_components out_components)
201201
list(APPEND expanded_components "${t}Info")
202202
endif()
203203
endforeach(t)
204+
elseif( c STREQUAL "AllTargetsMCAs" )
205+
# Link all the TargetMCAs from all the targets
206+
foreach(t ${LLVM_TARGETS_TO_BUILD})
207+
if( TARGET LLVM${t}TargetMCA )
208+
list(APPEND expanded_components "${t}TargetMCA")
209+
endif()
210+
endforeach(t)
204211
else()
205212
list(APPEND expanded_components "${c}")
206213
endif()

llvm/docs/CommandGuide/llvm-mca.rst

+6-3
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,10 @@ option specifies "``-``", then the output will also be sent to standard output.
217217

218218
.. option:: -disable-cb
219219

220-
Force usage of the generic CustomBehaviour class rather than using the target
221-
specific class. The generic class never detects any custom hazards.
220+
Force usage of the generic CustomBehaviour and InstrPostProcess classes rather
221+
than using the target specific implementation. The generic classes never
222+
detect any custom hazards or make any post processing modifications to
223+
instructions.
222224

223225

224226
EXIT STATUS
@@ -1013,4 +1015,5 @@ if you don't know the exact number and a value of 0 represents no stall).
10131015

10141016
If you'd like to add a CustomBehaviour class for a target that doesn't
10151017
already have one, refer to an existing implementation to see how to set it
1016-
up. Remember to look at (and add to) `/llvm-mca/lib/CMakeLists.txt`.
1018+
up. The classes are implemented within the target specific backend (for
1019+
example `/llvm/lib/Target/AMDGPU/MCA/`) so that they can access backend symbols.
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*===------ llvm/Config/TargetMCAs.def - LLVM Target MCAs -------*- C++ -*-===*\
2+
|* *|
3+
|* Part of the LLVM Project, under the Apache License v2.0 with LLVM *|
4+
|* Exceptions. *|
5+
|* See https://llvm.org/LICENSE.txt for license information. *|
6+
|* SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception *|
7+
|* *|
8+
|*===----------------------------------------------------------------------===*|
9+
|* *|
10+
|* This file enumerates all of the target MCAs *|
11+
|* supported by this build of LLVM. Clients of this file should define *|
12+
|* the LLVM_TARGETMCA macro to be a function-like macro with a *|
13+
|* single parameter (the name of the target whose assembly can be *|
14+
|* generated); including this file will then enumerate all of the *|
15+
|* targets with target MCAs. *|
16+
|* *|
17+
|* The set of targets supported by LLVM is generated at configuration *|
18+
|* time, at which point this header is generated. Do not modify this *|
19+
|* header directly. *|
20+
|* *|
21+
\*===----------------------------------------------------------------------===*/
22+
23+
#ifndef LLVM_TARGETMCA
24+
# error Please define the macro LLVM_TARGETMCA(TargetName)
25+
#endif
26+
27+
@LLVM_ENUM_TARGETMCAS@
28+
29+
#undef LLVM_TARGETMCA

llvm/include/llvm/Config/llvm-config.h.cmake

+3
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,9 @@
5050
/* LLVM name for the native target MC init function, if available */
5151
#cmakedefine LLVM_NATIVE_TARGETMC LLVMInitialize${LLVM_NATIVE_ARCH}TargetMC
5252

53+
/* LLVM name for the native target MCA init function, if available */
54+
#cmakedefine LLVM_NATIVE_TARGETMCA LLVMInitialize${LLVM_NATIVE_ARCH}TargetMCA
55+
5356
/* Define if this is Unixish platform */
5457
#cmakedefine LLVM_ON_UNIX ${LLVM_ON_UNIX}
5558

llvm/include/llvm/MCA/CustomBehaviour.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,11 +55,11 @@ class InstrPostProcess {
5555
class CustomBehaviour {
5656
protected:
5757
const MCSubtargetInfo &STI;
58-
const SourceMgr &SrcMgr;
58+
const mca::SourceMgr &SrcMgr;
5959
const MCInstrInfo &MCII;
6060

6161
public:
62-
CustomBehaviour(const MCSubtargetInfo &STI, const SourceMgr &SrcMgr,
62+
CustomBehaviour(const MCSubtargetInfo &STI, const mca::SourceMgr &SrcMgr,
6363
const MCInstrInfo &MCII)
6464
: STI(STI), SrcMgr(SrcMgr), MCII(MCII) {}
6565

llvm/include/llvm/Support/TargetRegistry.h

+76
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ class raw_ostream;
5959
class raw_pwrite_stream;
6060
class TargetMachine;
6161
class TargetOptions;
62+
namespace mca {
63+
class CustomBehaviour;
64+
class InstrPostProcess;
65+
class SourceMgr;
66+
} // namespace mca
6267

6368
MCStreamer *createNullStreamer(MCContext &Ctx);
6469
// Takes ownership of \p TAB and \p CE.
@@ -114,6 +119,13 @@ MCSymbolizer *createMCSymbolizer(const Triple &TT, LLVMOpInfoCallback GetOpInfo,
114119
void *DisInfo, MCContext *Ctx,
115120
std::unique_ptr<MCRelocationInfo> &&RelInfo);
116121

122+
mca::CustomBehaviour *createCustomBehaviour(const MCSubtargetInfo &STI,
123+
const mca::SourceMgr &SrcMgr,
124+
const MCInstrInfo &MCII);
125+
126+
mca::InstrPostProcess *createInstrPostProcess(const MCSubtargetInfo &STI,
127+
const MCInstrInfo &MCII);
128+
117129
/// Target - Wrapper for Target specific information.
118130
///
119131
/// For registration purposes, this is a POD type so that targets can be
@@ -206,6 +218,15 @@ class Target {
206218
LLVMSymbolLookupCallback SymbolLookUp, void *DisInfo, MCContext *Ctx,
207219
std::unique_ptr<MCRelocationInfo> &&RelInfo);
208220

221+
using CustomBehaviourCtorTy =
222+
mca::CustomBehaviour *(*)(const MCSubtargetInfo &STI,
223+
const mca::SourceMgr &SrcMgr,
224+
const MCInstrInfo &MCII);
225+
226+
using InstrPostProcessCtorTy =
227+
mca::InstrPostProcess *(*)(const MCSubtargetInfo &STI,
228+
const MCInstrInfo &MCII);
229+
209230
private:
210231
/// Next - The next registered target in the linked list, maintained by the
211232
/// TargetRegistry.
@@ -305,6 +326,14 @@ class Target {
305326
/// MCSymbolizer, if registered (default = llvm::createMCSymbolizer)
306327
MCSymbolizerCtorTy MCSymbolizerCtorFn = nullptr;
307328

329+
/// CustomBehaviourCtorFn - Construction function for this target's
330+
/// CustomBehaviour, if registered (default = nullptr).
331+
CustomBehaviourCtorTy CustomBehaviourCtorFn = nullptr;
332+
333+
/// InstrPostProcessCtorFn - Construction function for this target's
334+
/// InstrPostProcess, if registered (default = nullptr).
335+
InstrPostProcessCtorTy InstrPostProcessCtorFn = nullptr;
336+
308337
public:
309338
Target() = default;
310339

@@ -623,6 +652,25 @@ class Target {
623652
std::move(RelInfo));
624653
}
625654

655+
/// createCustomBehaviour - Create a target specific CustomBehaviour.
656+
/// This class is used by llvm-mca and requires backend functionality.
657+
mca::CustomBehaviour *createCustomBehaviour(const MCSubtargetInfo &STI,
658+
const mca::SourceMgr &SrcMgr,
659+
const MCInstrInfo &MCII) const {
660+
if (CustomBehaviourCtorFn)
661+
return CustomBehaviourCtorFn(STI, SrcMgr, MCII);
662+
return nullptr;
663+
}
664+
665+
/// createInstrPostProcess - Create a target specific InstrPostProcess.
666+
/// This class is used by llvm-mca and requires backend functionality.
667+
mca::InstrPostProcess *createInstrPostProcess(const MCSubtargetInfo &STI,
668+
const MCInstrInfo &MCII) const {
669+
if (InstrPostProcessCtorFn)
670+
return InstrPostProcessCtorFn(STI, MCII);
671+
return nullptr;
672+
}
673+
626674
/// @}
627675
};
628676

@@ -959,6 +1007,34 @@ struct TargetRegistry {
9591007
T.MCSymbolizerCtorFn = Fn;
9601008
}
9611009

1010+
/// RegisterCustomBehaviour - Register a CustomBehaviour
1011+
/// implementation for the given target.
1012+
///
1013+
/// Clients are responsible for ensuring that registration doesn't occur
1014+
/// while another thread is attempting to access the registry. Typically
1015+
/// this is done by initializing all targets at program startup.
1016+
///
1017+
/// @param T - The target being registered.
1018+
/// @param Fn - A function to construct a CustomBehaviour for the target.
1019+
static void RegisterCustomBehaviour(Target &T,
1020+
Target::CustomBehaviourCtorTy Fn) {
1021+
T.CustomBehaviourCtorFn = Fn;
1022+
}
1023+
1024+
/// RegisterInstrPostProcess - Register an InstrPostProcess
1025+
/// implementation for the given target.
1026+
///
1027+
/// Clients are responsible for ensuring that registration doesn't occur
1028+
/// while another thread is attempting to access the registry. Typically
1029+
/// this is done by initializing all targets at program startup.
1030+
///
1031+
/// @param T - The target being registered.
1032+
/// @param Fn - A function to construct an InstrPostProcess for the target.
1033+
static void RegisterInstrPostProcess(Target &T,
1034+
Target::InstrPostProcessCtorTy Fn) {
1035+
T.InstrPostProcessCtorFn = Fn;
1036+
}
1037+
9621038
/// @}
9631039
};
9641040

llvm/include/llvm/Support/TargetSelect.h

+12
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,10 @@ extern "C" {
4141
#define LLVM_DISASSEMBLER(TargetName) \
4242
void LLVMInitialize##TargetName##Disassembler();
4343
#include "llvm/Config/Disassemblers.def"
44+
45+
// Declare all of the available TargetMCA initialization functions.
46+
#define LLVM_TARGETMCA(TargetName) void LLVMInitialize##TargetName##TargetMCA();
47+
#include "llvm/Config/TargetMCAs.def"
4448
}
4549

4650
namespace llvm {
@@ -159,6 +163,14 @@ namespace llvm {
159163
return true;
160164
#endif
161165
}
166+
167+
/// InitializeAllTargetMCAs - The main program should call
168+
/// this function to initialize the target CustomBehaviour and
169+
/// InstrPostProcess classes.
170+
inline void InitializeAllTargetMCAs() {
171+
#define LLVM_TARGETMCA(TargetName) LLVMInitialize##TargetName##TargetMCA();
172+
#include "llvm/Config/TargetMCAs.def"
173+
}
162174
}
163175

164176
#endif

llvm/lib/Target/AMDGPU/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ add_llvm_target(AMDGPUCodeGen
175175

176176
add_subdirectory(AsmParser)
177177
add_subdirectory(Disassembler)
178+
add_subdirectory(MCA)
178179
add_subdirectory(MCTargetDesc)
179180
add_subdirectory(TargetInfo)
180181
add_subdirectory(Utils)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//===------------------ AMDGPUCustomBehaviour.cpp ---------------*-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 implements methods from the AMDGPUCustomBehaviour class.
11+
///
12+
//===----------------------------------------------------------------------===//
13+
14+
#include "AMDGPUCustomBehaviour.h"
15+
#include "MCTargetDesc/AMDGPUMCTargetDesc.h"
16+
#include "SIInstrInfo.h"
17+
#include "TargetInfo/AMDGPUTargetInfo.h"
18+
#include "llvm/Support/TargetRegistry.h"
19+
#include "llvm/Support/WithColor.h"
20+
21+
namespace llvm {
22+
namespace mca {
23+
24+
AMDGPUCustomBehaviour::AMDGPUCustomBehaviour(const MCSubtargetInfo &STI,
25+
const mca::SourceMgr &SrcMgr,
26+
const MCInstrInfo &MCII)
27+
: CustomBehaviour(STI, SrcMgr, MCII) {}
28+
29+
unsigned
30+
AMDGPUCustomBehaviour::checkCustomHazard(ArrayRef<mca::InstRef> IssuedInst,
31+
const mca::InstRef &IR) {
32+
return 0;
33+
}
34+
35+
} // namespace mca
36+
} // namespace llvm
37+
38+
using namespace llvm;
39+
using namespace mca;
40+
41+
static CustomBehaviour *
42+
createAMDGPUCustomBehaviour(const MCSubtargetInfo &STI,
43+
const mca::SourceMgr &SrcMgr,
44+
const MCInstrInfo &MCII) {
45+
return new AMDGPUCustomBehaviour(STI, SrcMgr, MCII);
46+
}
47+
48+
static InstrPostProcess *
49+
createAMDGPUInstrPostProcess(const MCSubtargetInfo &STI,
50+
const MCInstrInfo &MCII) {
51+
return new AMDGPUInstrPostProcess(STI, MCII);
52+
}
53+
54+
/// Extern function to initialize the targets for the AMDGPU backend
55+
56+
extern "C" LLVM_EXTERNAL_VISIBILITY void LLVMInitializeAMDGPUTargetMCA() {
57+
TargetRegistry::RegisterCustomBehaviour(getTheAMDGPUTarget(),
58+
createAMDGPUCustomBehaviour);
59+
TargetRegistry::RegisterInstrPostProcess(getTheAMDGPUTarget(),
60+
createAMDGPUInstrPostProcess);
61+
62+
TargetRegistry::RegisterCustomBehaviour(getTheGCNTarget(),
63+
createAMDGPUCustomBehaviour);
64+
TargetRegistry::RegisterInstrPostProcess(getTheGCNTarget(),
65+
createAMDGPUInstrPostProcess);
66+
}

llvm/tools/llvm-mca/lib/AMDGPU/AMDGPUCustomBehaviour.h llvm/lib/Target/AMDGPU/MCA/AMDGPUCustomBehaviour.h

+11-9
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
/// \file
99
///
1010
/// This file defines the AMDGPUCustomBehaviour class which inherits from
11-
/// CustomBehaviour.
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.
1214
///
1315
//===----------------------------------------------------------------------===//
1416

15-
#ifndef LLVM_TOOLS_LLVM_MCA_LIB_AMDGPU_AMDGPUCUSTOMBEHAVIOUR_H
16-
#define LLVM_TOOLS_LLVM_MCA_LIB_AMDGPU_AMDGPUCUSTOMBEHAVIOUR_H
17+
#ifndef LLVM_LIB_TARGET_AMDGPU_MCA_AMDGPUCUSTOMBEHAVIOUR_H
18+
#define LLVM_LIB_TARGET_AMDGPU_MCA_AMDGPUCUSTOMBEHAVIOUR_H
1719

1820
#include "llvm/ADT/SmallVector.h"
1921
#include "llvm/MCA/CustomBehaviour.h"
@@ -29,14 +31,14 @@ class AMDGPUInstrPostProcess : public InstrPostProcess {
2931

3032
~AMDGPUInstrPostProcess() {}
3133

32-
void postProcessInstruction(std::unique_ptr<Instruction> &Inst,
34+
void postProcessInstruction(std::unique_ptr<mca::Instruction> &Inst,
3335
const MCInst &MCI) override {}
3436
};
3537

3638
class AMDGPUCustomBehaviour : public CustomBehaviour {
3739
public:
38-
AMDGPUCustomBehaviour(const MCSubtargetInfo &STI, const SourceMgr &SrcMgr,
39-
const MCInstrInfo &MCII);
40+
AMDGPUCustomBehaviour(const MCSubtargetInfo &STI,
41+
const mca::SourceMgr &SrcMgr, const MCInstrInfo &MCII);
4042

4143
~AMDGPUCustomBehaviour() {}
4244

@@ -47,11 +49,11 @@ class AMDGPUCustomBehaviour : public CustomBehaviour {
4749
/// register and hardware dependencies so this method should only
4850
/// implement custom behaviour and dependencies that are not picked up
4951
/// by MCA naturally.
50-
unsigned checkCustomHazard(ArrayRef<InstRef> IssuedInst,
51-
const InstRef &IR) override;
52+
unsigned checkCustomHazard(ArrayRef<mca::InstRef> IssuedInst,
53+
const mca::InstRef &IR) override;
5254
};
5355

5456
} // namespace mca
5557
} // namespace llvm
5658

57-
#endif /* LLVM_TOOLS_LLVM_MCA_LIB_AMDGPU_AMDGPUCUSTOMBEHAVIOUR_H */
59+
#endif

0 commit comments

Comments
 (0)