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 "AMDGPURemoveIncompatibleFunctions.h"
16#include "AMDGPU.h"
17#include "GCNSubtarget.h"
18#include "llvm/Analysis/OptimizationRemarkEmitter.h"
19#include "llvm/IR/Function.h"
20#include "llvm/IR/Module.h"
21#include "llvm/Pass.h"
22#include "llvm/Target/TargetMachine.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} // namespace llvm
32
33namespace {
34
35using Generation = AMDGPUSubtarget::Generation;
36
37class AMDGPURemoveIncompatibleFunctions {
38public:
39 AMDGPURemoveIncompatibleFunctions(const TargetMachine *TM = nullptr)
40 : TM(TM) {
41 assert(TM && "No TargetMachine!");
42 }
43 /// Checks a single function, returns true if the function must be deleted.
44 bool checkFunction(Function &F);
45
46 bool run(Module &M) {
47 assert(TM->getTargetTriple().isAMDGCN());
48
49 SmallVector<Function *, 4> FnsToDelete;
50 for (Function &F : M) {
51 if (checkFunction(F))
52 FnsToDelete.push_back(Elt: &F);
53 }
54
55 for (Function *F : FnsToDelete) {
56 F->replaceAllUsesWith(V: ConstantPointerNull::get(T: F->getType()));
57 F->eraseFromParent();
58 }
59 return !FnsToDelete.empty();
60 }
61
62private:
63 const TargetMachine *TM = nullptr;
64};
65
66class AMDGPURemoveIncompatibleFunctionsLegacy : public ModulePass {
67public:
68 static char ID;
69
70 AMDGPURemoveIncompatibleFunctionsLegacy(const TargetMachine *TM)
71 : ModulePass(ID), TM(TM) {}
72
73 bool runOnModule(Module &M) override {
74 AMDGPURemoveIncompatibleFunctions Pass(TM);
75 return Pass.run(M);
76 }
77
78 StringRef getPassName() const override {
79 return "AMDGPU Remove Incompatible Functions";
80 }
81
82 void getAnalysisUsage(AnalysisUsage &AU) const override {}
83
84private:
85 const TargetMachine *TM = nullptr;
86};
87
88StringRef getFeatureName(unsigned Feature) {
89 for (const SubtargetFeatureKV &KV : AMDGPUFeatureKV)
90 if (Feature == KV.Value)
91 return KV.Key;
92
93 llvm_unreachable("Unknown Target feature");
94}
95
96const SubtargetSubTypeKV *getGPUInfo(const GCNSubtarget &ST,
97 StringRef GPUName) {
98 for (const SubtargetSubTypeKV &KV : ST.getAllProcessorDescriptions())
99 if (StringRef(KV.Key) == GPUName)
100 return &KV;
101
102 return nullptr;
103}
104
105constexpr unsigned FeaturesToCheck[] = {AMDGPU::FeatureGFX11Insts,
106 AMDGPU::FeatureGFX10Insts,
107 AMDGPU::FeatureGFX9Insts,
108 AMDGPU::FeatureGFX8Insts,
109 AMDGPU::FeatureDPP,
110 AMDGPU::FeatureDPPWavefrontShifts,
111 AMDGPU::FeatureDPPBroadcasts,
112 AMDGPU::Feature16BitInsts,
113 AMDGPU::FeatureDot1Insts,
114 AMDGPU::FeatureDot2Insts,
115 AMDGPU::FeatureDot3Insts,
116 AMDGPU::FeatureDot4Insts,
117 AMDGPU::FeatureDot5Insts,
118 AMDGPU::FeatureDot6Insts,
119 AMDGPU::FeatureDot7Insts,
120 AMDGPU::FeatureDot8Insts,
121 AMDGPU::FeatureExtendedImageInsts,
122 AMDGPU::FeatureSMemRealTime,
123 AMDGPU::FeatureSMemTimeInst,
124 AMDGPU::FeatureGWS};
125
126FeatureBitset expandImpliedFeatures(const FeatureBitset &Features) {
127 FeatureBitset Result = Features;
128 for (const SubtargetFeatureKV &FE : AMDGPUFeatureKV) {
129 if (Features.test(I: FE.Value) && FE.Implies.any())
130 Result |= expandImpliedFeatures(Features: FE.Implies.getAsBitset());
131 }
132 return Result;
133}
134
135void reportFunctionRemoved(Function &F, unsigned Feature) {
136 OptimizationRemarkEmitter ORE(&F);
137 ORE.emit(RemarkBuilder: [&]() {
138 // Note: we print the function name as part of the diagnostic because if
139 // debug info is not present, users get "<unknown>:0:0" as the debug
140 // loc. If we didn't print the function name there would be no way to
141 // tell which function got removed.
142 return OptimizationRemark(DEBUG_TYPE, "AMDGPUIncompatibleFnRemoved", &F)
143 << "removing function '" << F.getName() << "': +"
144 << getFeatureName(Feature)
145 << " is not supported on the current target";
146 });
147}
148} // end anonymous namespace
149
150PreservedAnalyses
151AMDGPURemoveIncompatibleFunctionsPass::run(Module &M,
152 ModuleAnalysisManager &MAM) {
153 AMDGPURemoveIncompatibleFunctions Impl(TM);
154 if (Impl.run(M))
155 return PreservedAnalyses::none();
156 return PreservedAnalyses::all();
157}
158
159bool AMDGPURemoveIncompatibleFunctions::checkFunction(Function &F) {
160 if (F.isDeclaration())
161 return false;
162
163 const GCNSubtarget *ST =
164 static_cast<const GCNSubtarget *>(TM->getSubtargetImpl(F));
165
166 // Check the GPU isn't generic or generic-hsa. Generic is used for testing
167 // only and we don't want this pass to interfere with it.
168 StringRef GPUName = ST->getCPU();
169 if (GPUName.empty() || GPUName.starts_with(Prefix: "generic"))
170 return false;
171
172 // Try to fetch the GPU's info. If we can't, it's likely an unknown processor
173 // so just bail out.
174 const SubtargetSubTypeKV *GPUInfo = getGPUInfo(ST: *ST, GPUName);
175 if (!GPUInfo)
176 return false;
177
178 // Get all the features implied by the current GPU, and recursively expand
179 // the features that imply other features.
180 //
181 // e.g. GFX90A implies FeatureGFX9, and FeatureGFX9 implies a whole set of
182 // other features.
183 const FeatureBitset GPUFeatureBits =
184 expandImpliedFeatures(Features: GPUInfo->Implies.getAsBitset());
185
186 // Now that the have a FeatureBitset containing all possible features for
187 // the chosen GPU, check our list of "suspicious" features.
188
189 // Check that the user didn't enable any features that aren't part of that
190 // GPU's feature set. We only check a predetermined set of features.
191 for (unsigned Feature : FeaturesToCheck) {
192 if (ST->hasFeature(Feature) && !GPUFeatureBits.test(I: Feature)) {
193 reportFunctionRemoved(F, Feature);
194 return true;
195 }
196 }
197
198 // Delete FeatureWavefrontSize32 functions for
199 // gfx9 and below targets that don't support the mode.
200 // gfx10, gfx11, gfx12 are implied to support both wave32 and 64 features.
201 // They are not in the feature set. So, we need a separate check
202 if (!ST->supportsWave32() && ST->hasFeature(Feature: AMDGPU::FeatureWavefrontSize32)) {
203 reportFunctionRemoved(F, Feature: AMDGPU::FeatureWavefrontSize32);
204 return true;
205 }
206 // gfx125x only support FeatureWavefrontSize32.
207 if (!ST->supportsWave64() && ST->hasFeature(Feature: AMDGPU::FeatureWavefrontSize64)) {
208 reportFunctionRemoved(F, Feature: AMDGPU::FeatureWavefrontSize64);
209 return true;
210 }
211 return false;
212}
213
214INITIALIZE_PASS(AMDGPURemoveIncompatibleFunctionsLegacy, DEBUG_TYPE,
215 "AMDGPU Remove Incompatible Functions", false, false)
216
217char AMDGPURemoveIncompatibleFunctionsLegacy::ID = 0;
218
219ModulePass *
220llvm::createAMDGPURemoveIncompatibleFunctionsPass(const TargetMachine *TM) {
221 return new AMDGPURemoveIncompatibleFunctionsLegacy(TM);
222}
223