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.
139
140// How to apply register bank on register operand.
141// In most cases, this serves as a LLT and register bank assert.
142// Can change operands and insert copies, extends, truncs, and read-any-lanes.
143// Anything more complicated requires LoweringMethod.
150
151 // sgpr scalars, pointers, vectors and B-types
176
177 // vgpr scalars, pointers, vectors and B-types
206
207 // Dst only modifiers: read-any-lane and truncs
223
225
226 // Dst only modifiers: dst was assigned VGPR by RegBankSelect but the
227 // instruction result must be in SGPR. Replace dst with SGPR, then copy the
228 // result back to the original VGPR.
231
232 // Src only modifiers: execute in waterfall loop if divergent
235
236 // Src only modifiers: execute in waterfall loop for calls
239
240 // Src only modifiers: for operands that must end up in M0. If divergent,
241 // readfirstlane to SGPR. The result can then be copied to M0 in ISel.
243
244 // Src only modifiers: operand must be SGPR, if in VGPR, insert readfirstlane
245 // to move to SGPR.
248
249 // Src only modifiers: extends
257};
258
259// Instruction needs to be replaced with sequence of instructions. Lowering was
260// not done by legalizer since instructions is available in either sgpr or vgpr.
261// For example S64 AND is available on sgpr, for that reason S64 AND is legal in
262// context of Legalizer that only checks LLT. But S64 AND is not available on
263// vgpr. Lower it to two S32 vgpr ANDs.
298
301 Standard, // S16, S32, S64, V2S16
302 StandardB, // B32, B64, B96, B128
303 Vector, // S32, V2S32, V3S32, V4S32
304};
305
311 std::initializer_list<RegBankLLTMappingApplyID> DstOpMappingList,
312 std::initializer_list<RegBankLLTMappingApplyID> SrcOpMappingList,
314};
315
318 std::function<bool(const MachineInstr &)> TestFunc;
320 std::initializer_list<UniformityLLTOpPredicateID> OpList,
321 std::function<bool(const MachineInstr &)> TestFunc = nullptr);
322
323 bool match(const MachineInstr &MI, const MachineUniformityInfo &MUI,
324 const MachineRegisterInfo &MRI) const;
325};
326
331
333 // "Slow Rules". More complex 'Rules[i].Predicate', check them one by one.
335
336 // "Fast Rules"
337 // Instead of testing each 'Rules[i].Predicate' we do direct access to
338 // RegBankLLTMapping using getFastPredicateSlot. For example if:
339 // - FastTypes == Standard Uni[0] holds Mapping in case Op 0 is uniform S32
340 // - FastTypes == Vector Div[3] holds Mapping in case Op 0 is divergent V4S32
341 FastRulesTypes FastTypes = NoFastRules;
342#define InvMapping RegBankLLTMapping({InvalidMapping}, {InvalidMapping})
343 RegBankLLTMapping Uni[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
344 RegBankLLTMapping Div[4] = {InvMapping, InvMapping, InvMapping, InvMapping};
345
346public:
349
350 const RegBankLLTMapping *
352 const MachineUniformityInfo &MUI) const;
353
354 void addRule(RegBankLegalizeRule Rule);
355
357 RegBankLLTMapping RuleApplyIDs);
359 RegBankLLTMapping RuleApplyIDs);
360
361private:
362 int getFastPredicateSlot(UniformityLLTOpPredicateID Ty) const;
363};
364
365// Essentially 'map<Opcode(or intrinsic_opcode), SetOfRulesForOpcode>' but a
366// little more efficient.
368 const GCNSubtarget *ST;
370 // Separate maps for G-opcodes and intrinsics since they are in different
371 // enums. Multiple opcodes can share same set of rules.
372 // RulesAlias = map<Opcode, KeyOpcode>
373 // Rules = map<KeyOpcode, SetOfRulesForOpcode>
378 class RuleSetInitializer {
379 SetOfRulesForOpcode *RuleSet;
380
381 public:
382 // Used for clang-format line breaks and to force writing all rules for
383 // opcode in same place.
384 template <class AliasMap, class RulesMap>
385 RuleSetInitializer(std::initializer_list<unsigned> OpcList,
386 AliasMap &RulesAlias, RulesMap &Rules,
387 FastRulesTypes FastTypes = NoFastRules) {
388 unsigned KeyOpcode = *OpcList.begin();
389 for (unsigned Opc : OpcList) {
390 [[maybe_unused]] auto [_, NewInput] =
391 RulesAlias.try_emplace(Opc, KeyOpcode);
392 assert(NewInput && "Can't redefine existing Rules");
393 }
394
395 auto [DenseMapIter, NewInput] = Rules.try_emplace(KeyOpcode, FastTypes);
396 assert(NewInput && "Can't redefine existing Rules");
397
398 RuleSet = &DenseMapIter->second;
399 }
400
401 RuleSetInitializer(const RuleSetInitializer &) = delete;
402 RuleSetInitializer &operator=(const RuleSetInitializer &) = delete;
403 RuleSetInitializer(RuleSetInitializer &&) = delete;
404 RuleSetInitializer &operator=(RuleSetInitializer &&) = delete;
405 ~RuleSetInitializer() = default;
406
407 RuleSetInitializer &Div(UniformityLLTOpPredicateID Ty,
408 RegBankLLTMapping RuleApplyIDs,
409 bool STPred = true) {
410 if (STPred)
411 RuleSet->addFastRuleDivergent(Ty, RuleApplyIDs);
412 return *this;
413 }
414
415 RuleSetInitializer &Uni(UniformityLLTOpPredicateID Ty,
416 RegBankLLTMapping RuleApplyIDs,
417 bool STPred = true) {
418 if (STPred)
419 RuleSet->addFastRuleUniform(Ty, RuleApplyIDs);
420 return *this;
421 }
422
423 RuleSetInitializer &Any(RegBankLegalizeRule Init, bool STPred = true) {
424 if (STPred)
425 RuleSet->addRule(Init);
426 return *this;
427 }
428 };
429
430 RuleSetInitializer addRulesForGOpcs(std::initializer_list<unsigned> OpcList,
431 FastRulesTypes FastTypes = NoFastRules);
432
433 RuleSetInitializer addRulesForIOpcs(std::initializer_list<unsigned> OpcList,
434 FastRulesTypes FastTypes = NoFastRules);
435
436public:
437 // Initialize rules for all opcodes.
439
440 // In case we don't want to regenerate same rules, we can use already
441 // generated rules but need to refresh references to objects that are
442 // created for this run.
444 ST = &_ST;
445 MRI = &_MRI;
446 };
447
449};
450
451} // end namespace AMDGPU
452} // end namespace llvm
453
454#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.
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