1//===- AMDGPUMemoryUtils.h - Memory related helper functions -*- 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_UTILS_AMDGPUMEMORYUTILS_H
10#define LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUMEMORYUTILS_H
11
12#include "llvm/ADT/ArrayRef.h"
13#include "llvm/ADT/BitmaskEnum.h"
14#include "llvm/ADT/DenseMap.h"
15#include "llvm/ADT/DenseSet.h"
16#include "llvm/ADT/MapVector.h"
17#include "llvm/IR/DebugLoc.h"
18
19namespace llvm {
20
21struct Align;
22class AAResults;
23class DataLayout;
24class GlobalVariable;
25class Instruction;
26class LoadInst;
27class MemoryDef;
28class MemorySSA;
29class MMRAMetadata;
30class Value;
31class Function;
32class CallGraph;
33class Module;
34class TargetExtType;
35
36namespace AMDGPU {
37
38static constexpr unsigned NamedBarrierTypeSizeInBytes = 1;
39
40using FunctionVariableMap = MapVector<Function *, DenseSet<GlobalVariable *>>;
41using VariableFunctionMap = DenseMap<GlobalVariable *, DenseSet<Function *>>;
42
43Align getAlign(const DataLayout &DL, const GlobalVariable *GV);
44
45// Get the synthetic aperture number for the given address space, or None (0)
46// if the address space does not have one.
47unsigned getSyntheticApertureNumber(unsigned AS);
48
49// Copy metadata onto a load widened to read a superset of Source's bytes. Only
50// value-independent metadata is copied; metadata describing the loaded value
51// (!range, !noundef, !nofpclass, !tbaa, ...) is dropped.
52void copyMetadataForWidenedLoad(LoadInst &Dest, const LoadInst &Source);
53
54// If GV is a named-barrier return its type. Otherwise return nullptr.
55TargetExtType *isNamedBarrier(const GlobalVariable &GV);
56
57/// \returns how many named barriers are declared by \p GV.
58unsigned getNumNamedBarriersDeclared(const DataLayout &DL,
59 const GlobalVariable &GV);
60
61bool isDynamicLDS(const GlobalVariable &GV);
62bool isLDSVariableToLower(const GlobalVariable &GV);
63
64struct GVUsesInfoTy {
65 FunctionVariableMap DirectAccess;
66 FunctionVariableMap IndirectAccess;
67};
68
69/// Iterates over all GlobalVariables in \p M, and whenever \p Filter returns
70/// true, replace all constant users of the GV with instructions.
71bool eliminateGVConstantExprUsesFromAllInstructions(
72 Module &M, function_ref<bool(const GlobalVariable &)> Filter);
73
74/// Finds uses of Global Variables on a per-function basis.
75/// \param CG \p M Call Graph
76/// \param M Module
77/// \param Filter Function that returns true for GVs that need to be considered.
78/// \param Kernels[out] Maps kernels to global variables used by that kernel.
79/// \param Functions[out] Maps functions to global variables used by that
80/// function.
81void getUsesOfGVByFunction(const CallGraph &CG, Module &M,
82 function_ref<bool(const GlobalVariable &)> Filter,
83 FunctionVariableMap &Kernels,
84 FunctionVariableMap &Functions);
85
86/// Collects all uses of Global Variables in \p M using
87/// \ref getUsesOfGVByFunction.
88/// \param CG \p M Call Graph
89/// \param M Module
90/// \param Filter Filter for \ref getUsesOfGVByFunction - only GVs for which the
91/// filter returns true will be considered.
92/// \returns Uses of GVs that were found within each function, sorted by
93/// direct and indirect accesses.
94GVUsesInfoTy
95getTransitiveUsesOfGV(const CallGraph &CG, Module &M,
96 function_ref<bool(const GlobalVariable &)> Filter);
97
98/// Collects all uses of LDS Global Variables in \p M using
99/// \ref getUsesOfGVByFunction, with \ref isLDSVariableToLower as the filter.
100/// \param CG \p M Call Graph
101/// \param M Module
102/// \returns Uses of LDS GVs that need lowering that were found within each
103/// function, sorted by direct and indirect accesses.
104GVUsesInfoTy getTransitiveUsesOfLDSForLowering(const CallGraph &CG, Module &M);
105
106/// Strip FnAttr attribute from any functions where we may have
107/// introduced its use.
108void removeFnAttrFromReachable(CallGraph &CG, Function *KernelRoot,
109 ArrayRef<StringRef> FnAttrs);
110
111/// Given a \p Def clobbering a load from \p Ptr according to the MSSA check
112/// if this is actually a memory update or an artificial clobber to facilitate
113/// ordering constraints.
114bool isReallyAClobber(const Value *Ptr, MemoryDef *Def, AAResults *AA);
115
116/// Check is a \p Load is clobbered in its function.
117bool isClobberedInFunction(const LoadInst *Load, MemorySSA *MSSA,
118 AAResults *AA);
119
120/// The distinct address spaces supported by the AMDGPU target for
121/// atomic memory operation. Can be ORed together.
122enum class SIAtomicAddrSpace {
123 NONE = 0u,
124 GLOBAL = 1u << 0,
125 LDS = 1u << 1,
126 SCRATCH = 1u << 2,
127 GDS = 1u << 3,
128 OTHER = 1u << 4,
129
130 /// The address spaces that can be accessed by a FLAT instruction.
131 FLAT = GLOBAL | LDS | SCRATCH,
132
133 /// The address spaces that support atomic instructions.
134 ATOMIC = GLOBAL | LDS | SCRATCH | GDS,
135
136 /// All address spaces.
137 ALL = GLOBAL | LDS | SCRATCH | GDS | OTHER,
138
139 LLVM_MARK_AS_BITMASK_ENUM(/* LargestFlag = */ ALL)
140};
141
142/// \return Return a bit set of the address spaces accessed by \p AS.
143SIAtomicAddrSpace toSIAtomicAddrSpace(unsigned AS);
144
145/// Reads \p I's MMRAs to parse the "amdgpu-synchronize-as" MMRA.
146/// If this tag isn't present, or if it has no meaningful values, returns
147/// \p ATOMIC, otherwise returns the address spaces specified by the MD.
148/// Does not diagnose unexpected MMRA tag suffixes.
149SIAtomicAddrSpace getSynchronizeAddrSpace(const Instruction &I);
150
151/// Parses the values for the "amdgpu-synchronize-as" MMRA.
152/// If this tag isn't present, or if it has no meaningful values, returns
153/// \p ATOMIC, otherwise returns the address spaces specified by the MD.
154/// If \p DiagFn is non-null, unexpected suffixes for the MMRA tag are reported
155/// as warnings to \p DiagFn's context, located at \p DL.
156SIAtomicAddrSpace getSynchronizeAddrSpace(const MMRAMetadata &MMRA,
157 const Function *DiagFn = nullptr,
158 const DebugLoc &DL = {});
159
160} // end namespace AMDGPU
161
162} // end namespace llvm
163
164#endif // LLVM_LIB_TARGET_AMDGPU_UTILS_AMDGPUMEMORYUTILS_H
165