LLVM 18.0.0git
AMDGPURemoveIncompatibleFunctions.cpp
Go to the documentation of this file.
1//===-- AMDGPURemoveIncompatibleFunctions.cpp -----------------------------===//
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
10/// This pass replaces all uses of functions that use GPU features
11/// incompatible with the current GPU with null then deletes the function.
12//
13//===----------------------------------------------------------------------===//
14
15#include "AMDGPU.h"
16#include "GCNSubtarget.h"
18#include "llvm/IR/Function.h"
19#include "llvm/IR/IRBuilder.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Pass.h"
23
24#define DEBUG_TYPE "amdgpu-remove-incompatible-functions"
25
26using namespace llvm;
27
28namespace llvm {
29extern const SubtargetFeatureKV
30 AMDGPUFeatureKV[AMDGPU::NumSubtargetFeatures - 1];
31}
32
33namespace {
34
35using Generation = AMDGPUSubtarget::Generation;
36
37class AMDGPURemoveIncompatibleFunctions : public ModulePass {
38public:
39 static char ID;
40
41 AMDGPURemoveIncompatibleFunctions(const TargetMachine *TM = nullptr)
42 : ModulePass(ID), TM(TM) {
43 assert(TM && "No TargetMachine!");
44 }
45
46 StringRef getPassName() const override {
47 return "AMDGPU Remove Incompatible Functions";
48 }
49
50 void getAnalysisUsage(AnalysisUsage &AU) const override {}
51
52 /// Checks a single function, returns true if the function must be deleted.
53 bool checkFunction(Function &F);
54
55 bool runOnModule(Module &M) override {
56 assert(TM->getTargetTriple().isAMDGCN());
57
59 for (Function &F : M) {
60 if (checkFunction(F))
61 FnsToDelete.push_back(&F);
62 }
63
64 for (Function *F : FnsToDelete) {
65 F->replaceAllUsesWith(ConstantPointerNull::get(F->getType()));
66 F->eraseFromParent();
67 }
68 return !FnsToDelete.empty();
69 }
70
71private:
72 const TargetMachine *TM = nullptr;
73};
74
75StringRef getFeatureName(unsigned Feature) {
76 for (const SubtargetFeatureKV &KV : AMDGPUFeatureKV)
77 if (Feature == KV.Value)
78 return KV.Key;
79
80 llvm_unreachable("Unknown Target feature");
81}
82
83const SubtargetSubTypeKV *getGPUInfo(const GCNSubtarget &ST,
84 StringRef GPUName) {
85 for (const SubtargetSubTypeKV &KV : ST.getAllProcessorDescriptions())
86 if (StringRef(KV.Key) == GPUName)
87 return &KV;
88
89 return nullptr;
90}
91
92constexpr unsigned FeaturesToCheck[] = {
93 AMDGPU::FeatureGFX11Insts,
94 AMDGPU::FeatureGFX10Insts,
95 AMDGPU::FeatureGFX9Insts,
96 AMDGPU::FeatureGFX8Insts,
97 AMDGPU::FeatureDPP,
98 AMDGPU::Feature16BitInsts,
99 AMDGPU::FeatureDot1Insts,
100 AMDGPU::FeatureDot2Insts,
101 AMDGPU::FeatureDot3Insts,
102 AMDGPU::FeatureDot4Insts,
103 AMDGPU::FeatureDot5Insts,
104 AMDGPU::FeatureDot6Insts,
105 AMDGPU::FeatureDot7Insts,
106 AMDGPU::FeatureDot8Insts,
107 AMDGPU::FeatureExtendedImageInsts,
108 AMDGPU::FeatureSMemRealTime,
109 AMDGPU::FeatureSMemTimeInst
110};
111
112FeatureBitset expandImpliedFeatures(const FeatureBitset &Features) {
113 FeatureBitset Result = Features;
114 for (const SubtargetFeatureKV &FE : AMDGPUFeatureKV) {
115 if (Features.test(FE.Value) && FE.Implies.any())
116 Result |= expandImpliedFeatures(FE.Implies.getAsBitset());
117 }
118 return Result;
119}
120
121void reportFunctionRemoved(Function &F, unsigned Feature) {
123 ORE.emit([&]() {
124 // Note: we print the function name as part of the diagnostic because if
125 // debug info is not present, users get "<unknown>:0:0" as the debug
126 // loc. If we didn't print the function name there would be no way to
127 // tell which function got removed.
128 return OptimizationRemark(DEBUG_TYPE, "AMDGPUIncompatibleFnRemoved", &F)
129 << "removing function '" << F.getName() << "': +"
130 << getFeatureName(Feature)
131 << " is not supported on the current target";
132 });
133}
134} // end anonymous namespace
135
136bool AMDGPURemoveIncompatibleFunctions::checkFunction(Function &F) {
137 if (F.isDeclaration())
138 return false;
139
140 const GCNSubtarget *ST =
141 static_cast<const GCNSubtarget *>(TM->getSubtargetImpl(F));
142
143 // Check the GPU isn't generic. Generic is used for testing only
144 // and we don't want this pass to interfere with it.
145 StringRef GPUName = ST->getCPU();
146 if (GPUName.empty() || GPUName.contains("generic"))
147 return false;
148
149 // Try to fetch the GPU's info. If we can't, it's likely an unknown processor
150 // so just bail out.
151 const SubtargetSubTypeKV *GPUInfo = getGPUInfo(*ST, GPUName);
152 if (!GPUInfo)
153 return false;
154
155 // Get all the features implied by the current GPU, and recursively expand
156 // the features that imply other features.
157 //
158 // e.g. GFX90A implies FeatureGFX9, and FeatureGFX9 implies a whole set of
159 // other features.
160 const FeatureBitset GPUFeatureBits =
161 expandImpliedFeatures(GPUInfo->Implies.getAsBitset());
162
163 // Now that the have a FeatureBitset containing all possible features for
164 // the chosen GPU, check our list of "suspicious" features.
165
166 // Check that the user didn't enable any features that aren't part of that
167 // GPU's feature set. We only check a predetermined set of features.
168 for (unsigned Feature : FeaturesToCheck) {
169 if (ST->hasFeature(Feature) && !GPUFeatureBits.test(Feature)) {
170 reportFunctionRemoved(F, Feature);
171 return true;
172 }
173 }
174
175 // Delete FeatureWavefrontSize32 functions for
176 // gfx9 and below targets that don't support the mode.
177 // gfx10+ is implied to support both wave32 and 64 features.
178 // They are not in the feature set. So, we need a separate check
179 if (ST->getGeneration() < AMDGPUSubtarget::GFX10 &&
180 ST->hasFeature(AMDGPU::FeatureWavefrontSize32)) {
181 reportFunctionRemoved(F, AMDGPU::FeatureWavefrontSize32);
182 return true;
183 }
184 return false;
185}
186
187INITIALIZE_PASS(AMDGPURemoveIncompatibleFunctions, DEBUG_TYPE,
188 "AMDGPU Remove Incompatible Functions", false, false)
189
190char AMDGPURemoveIncompatibleFunctions::ID = 0;
191
194 return new AMDGPURemoveIncompatibleFunctions(TM);
195}
aarch64 promote const
AMD GCN specific subclass of TargetSubtarget.
#define F(x, y, z)
Definition: MD5.cpp:55
Module.h This file contains the declarations for the Module class.
const char LLVMTargetMachineRef TM
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:38
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
Represent the analysis usage information of a pass.
static ConstantPointerNull * get(PointerType *T)
Static factory methods - Return objects of the specified value.
Definition: Constants.cpp:1691
const FeatureBitset & getAsBitset() const
Container class for subtarget features.
constexpr bool test(unsigned I) const
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:251
virtual bool runOnModule(Module &M)=0
runOnModule - Virtual method overriden by subclasses to process the module being operated on.
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
The optimization diagnostic interface.
Diagnostic information for applied optimization remarks.
virtual void getAnalysisUsage(AnalysisUsage &) const
getAnalysisUsage - This function should be overriden by passes that need analysis information to do t...
Definition: Pass.cpp:98
virtual StringRef getPassName() const
getPassName - Return a nice clean name for a pass.
Definition: Pass.cpp:81
void push_back(const T &Elt)
Definition: SmallVector.h:416
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1200
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:50
constexpr bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:134
bool contains(StringRef Other) const
Return true if the given string is a substring of *this, and false otherwise.
Definition: StringRef.h:428
Primary interface to the complete machine description for the target machine.
Definition: TargetMachine.h:78
#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: AddressRanges.h:18
const SubtargetFeatureKV AMDGPUFeatureKV[AMDGPU::NumSubtargetFeatures - 1]
ModulePass * createAMDGPURemoveIncompatibleFunctionsPass(const TargetMachine *)
Used to provide key value pairs for feature and CPU bit flags.
Used to provide key value pairs for feature and CPU bit flags.
FeatureBitArray Implies
K-V bit mask.