LLVM 23.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.
136
137// How to apply register bank on register operand.
138// In most cases, this serves as a LLT and register bank assert.
139// Can change operands and insert copies, extends, truncs, and read-any-lanes.
140// Anything more complicated requires LoweringMethod.
147
148 // sgpr scalars, pointers, vectors and B-types
173
174 // vgpr scalars, pointers, vectors and B-types
201
202 // Dst only modifiers: read-any-lane and truncs
218
220
221 // Src only modifiers: execute in waterfall loop if divergent
224
225 // Src only modifiers: execute in waterfall loop for calls
228
229 // Src only modifiers: for operands that must end up in M0. If divergent,
230 // readfirstlane to SGPR. The result can then be copied to M0 in ISel.
232
233 // Src only modifiers: extends
241};
242
243// Instruction needs to be replaced with sequence of instructions. Lowering was
244// not done by legalizer since instructions is available in either sgpr or vgpr.
245// For example S64 AND is available on sgpr, for that reason S64 AND is legal in
246// context of Legalizer that only checks LLT. But S64 AND is not available on
247// vgpr. Lower it to two S32 vgpr ANDs.
279
282 Standard, // S16, S32, S64, V2S16
283 StandardB, // B32, B64, B96, B128
284 Vector, // S32, V2S32, V3S32, V4S32
285};
286
292 std::initializer_list<RegBankLLTMappingApplyID> DstOpMappingList,
293 std::initializer_list<RegBankLLTMappingApplyID> SrcOpMappingList,
295};
296
299 std::function<bool(const MachineInstr &)> TestFunc;
301 std::initializer_list<UniformityLLTOpPredicateID> OpList,
302 std::function<bool(const MachineInstr &)> TestFunc = nullptr);
303
304 bool match(const MachineInstr &MI, const MachineUniformityInfo &MUI,
305 const MachineRegisterInfo &MRI) const;
306};
307
312
314 // "Slow Rules". More complex 'Rules[i].Predicate', check them one by one.
316
317 // "Fast Rules"
318 // Instead of testing each 'Rules[i].Predicate' we do direct access to
319 // RegBankLLTMapping using getFastPredicateSlot. For example if:
320 // - FastTypes == Standard Uni[0] holds Mapping in case Op 0 is uniform S32
321 // - FastTypes == Vector Div[3] holds Mapping in case Op 0 is divergent V4S32
322 FastRulesTypes FastTypes = NoFastRules;
323#define InvMapping RegBankLLTMapping({InvalidMapping}, {InvalidMapping})
324 RegBankLLTMapping Uni[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
325 RegBankLLTMapping Div[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
326
327public:
330
331 const RegBankLLTMapping *
333 const MachineUniformityInfo &MUI) const;
334
335 void addRule(RegBankLegalizeRule Rule);
336
338 RegBankLLTMapping RuleApplyIDs);
340 RegBankLLTMapping RuleApplyIDs);
341
342private:
343 int getFastPredicateSlot(UniformityLLTOpPredicateID Ty) const;
344};
345
346// Essentially 'map<Opcode(or intrinsic_opcode), SetOfRulesForOpcode>' but a
347// little more efficient.
349 const GCNSubtarget *ST;
351 // Separate maps for G-opcodes and intrinsics since they are in different
352 // enums. Multiple opcodes can share same set of rules.
353 // RulesAlias = map<Opcode, KeyOpcode>
354 // Rules = map<KeyOpcode, SetOfRulesForOpcode>
359 class RuleSetInitializer {
360 SetOfRulesForOpcode *RuleSet;
361
362 public:
363 // Used for clang-format line breaks and to force writing all rules for
364 // opcode in same place.
365 template <class AliasMap, class RulesMap>
366 RuleSetInitializer(std::initializer_list<unsigned> OpcList,
367 AliasMap &RulesAlias, RulesMap &Rules,
368 FastRulesTypes FastTypes = NoFastRules) {
369 unsigned KeyOpcode = *OpcList.begin();
370 for (unsigned Opc : OpcList) {
371 [[maybe_unused]] auto [_, NewInput] =
372 RulesAlias.try_emplace(Opc, KeyOpcode);
373 assert(NewInput && "Can't redefine existing Rules");
374 }
375
376 auto [DenseMapIter, NewInput] = Rules.try_emplace(KeyOpcode, FastTypes);
377 assert(NewInput && "Can't redefine existing Rules");
378
379 RuleSet = &DenseMapIter->second;
380 }
381
382 RuleSetInitializer(const RuleSetInitializer &) = delete;
383 RuleSetInitializer &operator=(const RuleSetInitializer &) = delete;
384 RuleSetInitializer(RuleSetInitializer &&) = delete;
385 RuleSetInitializer &operator=(RuleSetInitializer &&) = delete;
386 ~RuleSetInitializer() = default;
387
388 RuleSetInitializer &Div(UniformityLLTOpPredicateID Ty,
389 RegBankLLTMapping RuleApplyIDs,
390 bool STPred = true) {
391 if (STPred)
392 RuleSet->addFastRuleDivergent(Ty, RuleApplyIDs);
393 return *this;
394 }
395
396 RuleSetInitializer &Uni(UniformityLLTOpPredicateID Ty,
397 RegBankLLTMapping RuleApplyIDs,
398 bool STPred = true) {
399 if (STPred)
400 RuleSet->addFastRuleUniform(Ty, RuleApplyIDs);
401 return *this;
402 }
403
404 RuleSetInitializer &Any(RegBankLegalizeRule Init, bool STPred = true) {
405 if (STPred)
406 RuleSet->addRule(Init);
407 return *this;
408 }
409 };
410
411 RuleSetInitializer addRulesForGOpcs(std::initializer_list<unsigned> OpcList,
412 FastRulesTypes FastTypes = NoFastRules);
413
414 RuleSetInitializer addRulesForIOpcs(std::initializer_list<unsigned> OpcList,
415 FastRulesTypes FastTypes = NoFastRules);
416
417public:
418 // Initialize rules for all opcodes.
420
421 // In case we don't want to regenerate same rules, we can use already
422 // generated rules but need to refresh references to objects that are
423 // created for this run.
425 ST = &_ST;
426 MRI = &_MRI;
427 };
428
430};
431
432} // end namespace AMDGPU
433} // end namespace llvm
434
435#endif
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.
Definition Types.h:26
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