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.
124
125// How to apply register bank on register operand.
126// In most cases, this serves as a LLT and register bank assert.
127// Can change operands and insert copies, extends, truncs, and read-any-lanes.
128// Anything more complicated requires LoweringMethod.
212
213// Instruction needs to be replaced with sequence of instructions. Lowering was
214// not done by legalizer since instructions is available in either sgpr or vgpr.
215// For example S64 AND is available on sgpr, for that reason S64 AND is legal in
216// context of Legalizer that only checks LLT. But S64 AND is not available on
217// vgpr. Lower it to two S32 vgpr ANDs.
238
241 Standard, // S16, S32, S64, V2S16
242 StandardB, // B32, B64, B96, B128
243 Vector, // S32, V2S32, V3S32, V4S32
244};
245
251 std::initializer_list<RegBankLLTMappingApplyID> DstOpMappingList,
252 std::initializer_list<RegBankLLTMappingApplyID> SrcOpMappingList,
254};
255
258 std::function<bool(const MachineInstr &)> TestFunc;
260 std::initializer_list<UniformityLLTOpPredicateID> OpList,
261 std::function<bool(const MachineInstr &)> TestFunc = nullptr);
262
263 bool match(const MachineInstr &MI, const MachineUniformityInfo &MUI,
264 const MachineRegisterInfo &MRI) const;
265};
266
271
273 // "Slow Rules". More complex 'Rules[i].Predicate', check them one by one.
275
276 // "Fast Rules"
277 // Instead of testing each 'Rules[i].Predicate' we do direct access to
278 // RegBankLLTMapping using getFastPredicateSlot. For example if:
279 // - FastTypes == Standard Uni[0] holds Mapping in case Op 0 is uniform S32
280 // - FastTypes == Vector Div[3] holds Mapping in case Op 0 is divergent V4S32
281 FastRulesTypes FastTypes = NoFastRules;
282#define InvMapping RegBankLLTMapping({InvalidMapping}, {InvalidMapping})
283 RegBankLLTMapping Uni[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
284 RegBankLLTMapping Div[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
285
286public:
289
290 const RegBankLLTMapping &
292 const MachineUniformityInfo &MUI) const;
293
294 void addRule(RegBankLegalizeRule Rule);
295
297 RegBankLLTMapping RuleApplyIDs);
299 RegBankLLTMapping RuleApplyIDs);
300
301private:
302 int getFastPredicateSlot(UniformityLLTOpPredicateID Ty) const;
303};
304
305// Essentially 'map<Opcode(or intrinsic_opcode), SetOfRulesForOpcode>' but a
306// little more efficient.
308 const GCNSubtarget *ST;
310 // Separate maps for G-opcodes and instrinsics since they are in different
311 // enums. Multiple opcodes can share same set of rules.
312 // RulesAlias = map<Opcode, KeyOpcode>
313 // Rules = map<KeyOpcode, SetOfRulesForOpcode>
318 class RuleSetInitializer {
319 SetOfRulesForOpcode *RuleSet;
320
321 public:
322 // Used for clang-format line breaks and to force writing all rules for
323 // opcode in same place.
324 template <class AliasMap, class RulesMap>
325 RuleSetInitializer(std::initializer_list<unsigned> OpcList,
326 AliasMap &RulesAlias, RulesMap &Rules,
327 FastRulesTypes FastTypes = NoFastRules) {
328 unsigned KeyOpcode = *OpcList.begin();
329 for (unsigned Opc : OpcList) {
330 [[maybe_unused]] auto [_, NewInput] =
331 RulesAlias.try_emplace(Opc, KeyOpcode);
332 assert(NewInput && "Can't redefine existing Rules");
333 }
334
335 auto [DenseMapIter, NewInput] = Rules.try_emplace(KeyOpcode, FastTypes);
336 assert(NewInput && "Can't redefine existing Rules");
337
338 RuleSet = &DenseMapIter->second;
339 }
340
341 RuleSetInitializer(const RuleSetInitializer &) = delete;
342 RuleSetInitializer &operator=(const RuleSetInitializer &) = delete;
343 RuleSetInitializer(RuleSetInitializer &&) = delete;
344 RuleSetInitializer &operator=(RuleSetInitializer &&) = delete;
345 ~RuleSetInitializer() = default;
346
347 RuleSetInitializer &Div(UniformityLLTOpPredicateID Ty,
348 RegBankLLTMapping RuleApplyIDs,
349 bool STPred = true) {
350 if (STPred)
351 RuleSet->addFastRuleDivergent(Ty, RuleApplyIDs);
352 return *this;
353 }
354
355 RuleSetInitializer &Uni(UniformityLLTOpPredicateID Ty,
356 RegBankLLTMapping RuleApplyIDs,
357 bool STPred = true) {
358 if (STPred)
359 RuleSet->addFastRuleUniform(Ty, RuleApplyIDs);
360 return *this;
361 }
362
363 RuleSetInitializer &Any(RegBankLegalizeRule Init, bool STPred = true) {
364 if (STPred)
365 RuleSet->addRule(Init);
366 return *this;
367 }
368 };
369
370 RuleSetInitializer addRulesForGOpcs(std::initializer_list<unsigned> OpcList,
371 FastRulesTypes FastTypes = NoFastRules);
372
373 RuleSetInitializer addRulesForIOpcs(std::initializer_list<unsigned> OpcList,
374 FastRulesTypes FastTypes = NoFastRules);
375
376public:
377 // Initialize rules for all opcodes.
379
380 // In case we don't want to regenerate same rules, we can use already
381 // generated rules but need to refresh references to objects that are
382 // created for this run.
384 ST = &_ST;
385 MRI = &_MRI;
386 };
387
389};
390
391} // end namespace AMDGPU
392} // end namespace llvm
393
394#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