LLVM 22.0.0git
AMDGPURegBankLegalizeRules.h
Go to the documentation of this file.
1//===- AMDGPURegBankLegalizeRules --------------------------------*- 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
9#ifndef LLVM_LIB_TARGET_AMDGPU_AMDGPUREGBANKLEGALIZERULES_H
10#define LLVM_LIB_TARGET_AMDGPU_AMDGPUREGBANKLEGALIZERULES_H
11
12#include "llvm/ADT/DenseMap.h"
14#include <functional>
15
16namespace llvm {
17
18class LLT;
20class MachineInstr;
21class GCNSubtarget;
22class MachineFunction;
23template <typename T> class GenericUniformityInfo;
24template <typename T> class GenericSSAContext;
27
28namespace AMDGPU {
29
30/// \returns true if \p Ty is a pointer type with size \p Width.
31bool isAnyPtr(LLT Ty, unsigned Width);
32
33// IDs used to build predicate for RegBankLegalizeRule. Predicate can have one
34// or more IDs and each represents a check for 'uniform or divergent' + LLT or
35// just LLT on register operand.
36// Most often checking one operand is enough to decide which RegBankLLTMapping
37// to apply (see Fast Rules), IDs are useful when two or more operands need to
38// be checked.
120
121// How to apply register bank on register operand.
122// In most cases, this serves as a LLT and register bank assert.
123// Can change operands and insert copies, extends, truncs, and read-any-lanes.
124// Anything more complicated requires LoweringMethod.
204
205// Instruction needs to be replaced with sequence of instructions. Lowering was
206// not done by legalizer since instructions is available in either sgpr or vgpr.
207// For example S64 AND is available on sgpr, for that reason S64 AND is legal in
208// context of Legalizer that only checks LLT. But S64 AND is not available on
209// vgpr. Lower it to two S32 vgpr ANDs.
228
231 Standard, // S16, S32, S64, V2S16
232 StandardB, // B32, B64, B96, B128
233 Vector, // S32, V2S32, V3S32, V4S32
234};
235
241 std::initializer_list<RegBankLLTMappingApplyID> DstOpMappingList,
242 std::initializer_list<RegBankLLTMappingApplyID> SrcOpMappingList,
244};
245
248 std::function<bool(const MachineInstr &)> TestFunc;
250 std::initializer_list<UniformityLLTOpPredicateID> OpList,
251 std::function<bool(const MachineInstr &)> TestFunc = nullptr);
252
253 bool match(const MachineInstr &MI, const MachineUniformityInfo &MUI,
254 const MachineRegisterInfo &MRI) const;
255};
256
261
263 // "Slow Rules". More complex 'Rules[i].Predicate', check them one by one.
265
266 // "Fast Rules"
267 // Instead of testing each 'Rules[i].Predicate' we do direct access to
268 // RegBankLLTMapping using getFastPredicateSlot. For example if:
269 // - FastTypes == Standard Uni[0] holds Mapping in case Op 0 is uniform S32
270 // - FastTypes == Vector Div[3] holds Mapping in case Op 0 is divergent V4S32
271 FastRulesTypes FastTypes = NoFastRules;
272#define InvMapping RegBankLLTMapping({InvalidMapping}, {InvalidMapping})
273 RegBankLLTMapping Uni[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
274 RegBankLLTMapping Div[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
275
276public:
279
280 const RegBankLLTMapping &
281 findMappingForMI(const MachineInstr &MI, const MachineRegisterInfo &MRI,
282 const MachineUniformityInfo &MUI) const;
283
284 void addRule(RegBankLegalizeRule Rule);
285
287 RegBankLLTMapping RuleApplyIDs);
289 RegBankLLTMapping RuleApplyIDs);
290
291private:
292 int getFastPredicateSlot(UniformityLLTOpPredicateID Ty) const;
293};
294
295// Essentially 'map<Opcode(or intrinsic_opcode), SetOfRulesForOpcode>' but a
296// little more efficient.
298 const GCNSubtarget *ST;
300 // Separate maps for G-opcodes and instrinsics since they are in different
301 // enums. Multiple opcodes can share same set of rules.
302 // RulesAlias = map<Opcode, KeyOpcode>
303 // Rules = map<KeyOpcode, SetOfRulesForOpcode>
308 class RuleSetInitializer {
309 SetOfRulesForOpcode *RuleSet;
310
311 public:
312 // Used for clang-format line breaks and to force writing all rules for
313 // opcode in same place.
314 template <class AliasMap, class RulesMap>
315 RuleSetInitializer(std::initializer_list<unsigned> OpcList,
316 AliasMap &RulesAlias, RulesMap &Rules,
317 FastRulesTypes FastTypes = NoFastRules) {
318 unsigned KeyOpcode = *OpcList.begin();
319 for (unsigned Opc : OpcList) {
320 [[maybe_unused]] auto [_, NewInput] =
321 RulesAlias.try_emplace(Opc, KeyOpcode);
322 assert(NewInput && "Can't redefine existing Rules");
323 }
324
325 auto [DenseMapIter, NewInput] = Rules.try_emplace(KeyOpcode, FastTypes);
326 assert(NewInput && "Can't redefine existing Rules");
327
328 RuleSet = &DenseMapIter->second;
329 }
330
331 RuleSetInitializer(const RuleSetInitializer &) = delete;
332 RuleSetInitializer &operator=(const RuleSetInitializer &) = delete;
333 RuleSetInitializer(RuleSetInitializer &&) = delete;
334 RuleSetInitializer &operator=(RuleSetInitializer &&) = delete;
335 ~RuleSetInitializer() = default;
336
337 RuleSetInitializer &Div(UniformityLLTOpPredicateID Ty,
338 RegBankLLTMapping RuleApplyIDs,
339 bool STPred = true) {
340 if (STPred)
341 RuleSet->addFastRuleDivergent(Ty, RuleApplyIDs);
342 return *this;
343 }
344
345 RuleSetInitializer &Uni(UniformityLLTOpPredicateID Ty,
346 RegBankLLTMapping RuleApplyIDs,
347 bool STPred = true) {
348 if (STPred)
349 RuleSet->addFastRuleUniform(Ty, RuleApplyIDs);
350 return *this;
351 }
352
353 RuleSetInitializer &Any(RegBankLegalizeRule Init, bool STPred = true) {
354 if (STPred)
355 RuleSet->addRule(Init);
356 return *this;
357 }
358 };
359
360 RuleSetInitializer addRulesForGOpcs(std::initializer_list<unsigned> OpcList,
361 FastRulesTypes FastTypes = NoFastRules);
362
363 RuleSetInitializer addRulesForIOpcs(std::initializer_list<unsigned> OpcList,
364 FastRulesTypes FastTypes = NoFastRules);
365
366public:
367 // Initialize rules for all opcodes.
369
370 // In case we don't want to regenerate same rules, we can use already
371 // generated rules but need to refresh references to objects that are
372 // created for this run.
374 ST = &_ST;
375 MRI = &_MRI;
376 };
377
379};
380
381} // end namespace AMDGPU
382} // end namespace llvm
383
384#endif
unsigned const MachineRegisterInfo * MRI
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
#define InvMapping
This file defines the DenseMap class.
IRTranslator LLVM IR MI
This file defines the SmallVector class.
RegBankLegalizeRules(const GCNSubtarget &ST, MachineRegisterInfo &MRI)
const SetOfRulesForOpcode & getRulesForOpc(MachineInstr &MI) const
void refreshRefs(const GCNSubtarget &_ST, MachineRegisterInfo &_MRI)
const RegBankLLTMapping & findMappingForMI(const MachineInstr &MI, const MachineRegisterInfo &MRI, const MachineUniformityInfo &MUI) const
void addFastRuleDivergent(UniformityLLTOpPredicateID Ty, RegBankLLTMapping RuleApplyIDs)
void addFastRuleUniform(UniformityLLTOpPredicateID Ty, RegBankLLTMapping RuleApplyIDs)
Representation of each machine instruction.
MachineRegisterInfo - Keep track of information for virtual and physical registers,...
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
bool isAnyPtr(LLT Ty, unsigned Width)
This is an optimization pass for GlobalISel generic memory operations.
GenericUniformityInfo< MachineSSAContext > MachineUniformityInfo
GenericSSAContext< MachineFunction > MachineSSAContext
SmallVector< UniformityLLTOpPredicateID, 4 > OpUniformityAndTypes
PredicateMapping(std::initializer_list< UniformityLLTOpPredicateID > OpList, std::function< bool(const MachineInstr &)> TestFunc=nullptr)
bool match(const MachineInstr &MI, const MachineUniformityInfo &MUI, const MachineRegisterInfo &MRI) const
std::function< bool(const MachineInstr &)> TestFunc
RegBankLLTMapping(std::initializer_list< RegBankLLTMappingApplyID > DstOpMappingList, std::initializer_list< RegBankLLTMappingApplyID > SrcOpMappingList, LoweringMethodID LoweringMethod=DoNotLower)
SmallVector< RegBankLLTMappingApplyID, 2 > DstOpMapping
SmallVector< RegBankLLTMappingApplyID, 4 > SrcOpMapping