1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318
|
//===-- SPIRVDuplicatesTracker.h - SPIR-V Duplicates Tracker ----*- C++ -*-===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// General infrastructure for keeping track of the values that according to
// the SPIR-V binary layout should be global to the whole module.
//
//===----------------------------------------------------------------------===//
#ifndef LLVM_LIB_TARGET_SPIRV_SPIRVDUPLICATESTRACKER_H
#define LLVM_LIB_TARGET_SPIRV_SPIRVDUPLICATESTRACKER_H
#include "MCTargetDesc/SPIRVBaseInfo.h"
#include "MCTargetDesc/SPIRVMCTargetDesc.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/MapVector.h"
#include "llvm/CodeGen/GlobalISel/MachineIRBuilder.h"
#include "llvm/CodeGen/MachineModuleInfo.h"
#include <type_traits>
namespace llvm {
namespace SPIRV {
// NOTE: using MapVector instead of DenseMap because it helps getting
// everything ordered in a stable manner for a price of extra (NumKeys)*PtrSize
// memory and expensive removals which do not happen anyway.
class DTSortableEntry : public MapVector<const MachineFunction *, Register> {
SmallVector<DTSortableEntry *, 2> Deps;
struct FlagsTy {
unsigned IsFunc : 1;
unsigned IsGV : 1;
// NOTE: bit-field default init is a C++20 feature.
FlagsTy() : IsFunc(0), IsGV(0) {}
};
FlagsTy Flags;
public:
// Common hoisting utility doesn't support function, because their hoisting
// require hoisting of params as well.
bool getIsFunc() const { return Flags.IsFunc; }
bool getIsGV() const { return Flags.IsGV; }
void setIsFunc(bool V) { Flags.IsFunc = V; }
void setIsGV(bool V) { Flags.IsGV = V; }
const SmallVector<DTSortableEntry *, 2> &getDeps() const { return Deps; }
void addDep(DTSortableEntry *E) { Deps.push_back(E); }
};
struct SpecialTypeDescriptor {
enum SpecialTypeKind {
STK_Empty = 0,
STK_Image,
STK_SampledImage,
STK_Sampler,
STK_Pipe,
STK_DeviceEvent,
STK_Last = -1
};
SpecialTypeKind Kind;
unsigned Hash;
SpecialTypeDescriptor() = delete;
SpecialTypeDescriptor(SpecialTypeKind K) : Kind(K) { Hash = Kind; }
unsigned getHash() const { return Hash; }
virtual ~SpecialTypeDescriptor() {}
};
struct ImageTypeDescriptor : public SpecialTypeDescriptor {
union ImageAttrs {
struct BitFlags {
unsigned Dim : 3;
unsigned Depth : 2;
unsigned Arrayed : 1;
unsigned MS : 1;
unsigned Sampled : 2;
unsigned ImageFormat : 6;
unsigned AQ : 2;
} Flags;
unsigned Val;
};
ImageTypeDescriptor(const Type *SampledTy, unsigned Dim, unsigned Depth,
unsigned Arrayed, unsigned MS, unsigned Sampled,
unsigned ImageFormat, unsigned AQ = 0)
: SpecialTypeDescriptor(SpecialTypeKind::STK_Image) {
ImageAttrs Attrs;
Attrs.Val = 0;
Attrs.Flags.Dim = Dim;
Attrs.Flags.Depth = Depth;
Attrs.Flags.Arrayed = Arrayed;
Attrs.Flags.MS = MS;
Attrs.Flags.Sampled = Sampled;
Attrs.Flags.ImageFormat = ImageFormat;
Attrs.Flags.AQ = AQ;
Hash = (DenseMapInfo<Type *>().getHashValue(SampledTy) & 0xffff) ^
((Attrs.Val << 8) | Kind);
}
static bool classof(const SpecialTypeDescriptor *TD) {
return TD->Kind == SpecialTypeKind::STK_Image;
}
};
struct SampledImageTypeDescriptor : public SpecialTypeDescriptor {
SampledImageTypeDescriptor(const Type *SampledTy, const MachineInstr *ImageTy)
: SpecialTypeDescriptor(SpecialTypeKind::STK_SampledImage) {
assert(ImageTy->getOpcode() == SPIRV::OpTypeImage);
ImageTypeDescriptor TD(
SampledTy, ImageTy->getOperand(2).getImm(),
ImageTy->getOperand(3).getImm(), ImageTy->getOperand(4).getImm(),
ImageTy->getOperand(5).getImm(), ImageTy->getOperand(6).getImm(),
ImageTy->getOperand(7).getImm(), ImageTy->getOperand(8).getImm());
Hash = TD.getHash() ^ Kind;
}
static bool classof(const SpecialTypeDescriptor *TD) {
return TD->Kind == SpecialTypeKind::STK_SampledImage;
}
};
struct SamplerTypeDescriptor : public SpecialTypeDescriptor {
SamplerTypeDescriptor()
: SpecialTypeDescriptor(SpecialTypeKind::STK_Sampler) {
Hash = Kind;
}
static bool classof(const SpecialTypeDescriptor *TD) {
return TD->Kind == SpecialTypeKind::STK_Sampler;
}
};
struct PipeTypeDescriptor : public SpecialTypeDescriptor {
PipeTypeDescriptor(uint8_t AQ)
: SpecialTypeDescriptor(SpecialTypeKind::STK_Pipe) {
Hash = (AQ << 8) | Kind;
}
static bool classof(const SpecialTypeDescriptor *TD) {
return TD->Kind == SpecialTypeKind::STK_Pipe;
}
};
struct DeviceEventTypeDescriptor : public SpecialTypeDescriptor {
DeviceEventTypeDescriptor()
: SpecialTypeDescriptor(SpecialTypeKind::STK_DeviceEvent) {
Hash = Kind;
}
static bool classof(const SpecialTypeDescriptor *TD) {
return TD->Kind == SpecialTypeKind::STK_DeviceEvent;
}
};
} // namespace SPIRV
template <> struct DenseMapInfo<SPIRV::SpecialTypeDescriptor> {
static inline SPIRV::SpecialTypeDescriptor getEmptyKey() {
return SPIRV::SpecialTypeDescriptor(
SPIRV::SpecialTypeDescriptor::STK_Empty);
}
static inline SPIRV::SpecialTypeDescriptor getTombstoneKey() {
return SPIRV::SpecialTypeDescriptor(SPIRV::SpecialTypeDescriptor::STK_Last);
}
static unsigned getHashValue(SPIRV::SpecialTypeDescriptor Val) {
return Val.getHash();
}
static bool isEqual(SPIRV::SpecialTypeDescriptor LHS,
SPIRV::SpecialTypeDescriptor RHS) {
return getHashValue(LHS) == getHashValue(RHS);
}
};
template <typename KeyTy> class SPIRVDuplicatesTrackerBase {
public:
// NOTE: using MapVector instead of DenseMap helps getting everything ordered
// in a stable manner for a price of extra (NumKeys)*PtrSize memory and
// expensive removals which don't happen anyway.
using StorageTy = MapVector<KeyTy, SPIRV::DTSortableEntry>;
private:
StorageTy Storage;
public:
void add(KeyTy V, const MachineFunction *MF, Register R) {
if (find(V, MF).isValid())
return;
Storage[V][MF] = R;
if (std::is_same<Function,
typename std::remove_const<
typename std::remove_pointer<KeyTy>::type>::type>() ||
std::is_same<Argument,
typename std::remove_const<
typename std::remove_pointer<KeyTy>::type>::type>())
Storage[V].setIsFunc(true);
if (std::is_same<GlobalVariable,
typename std::remove_const<
typename std::remove_pointer<KeyTy>::type>::type>())
Storage[V].setIsGV(true);
}
Register find(KeyTy V, const MachineFunction *MF) const {
auto iter = Storage.find(V);
if (iter != Storage.end()) {
auto Map = iter->second;
auto iter2 = Map.find(MF);
if (iter2 != Map.end())
return iter2->second;
}
return Register();
}
const StorageTy &getAllUses() const { return Storage; }
private:
StorageTy &getAllUses() { return Storage; }
// The friend class needs to have access to the internal storage
// to be able to build dependency graph, can't declare only one
// function a 'friend' due to the incomplete declaration at this point
// and mutual dependency problems.
friend class SPIRVGeneralDuplicatesTracker;
};
template <typename T>
class SPIRVDuplicatesTracker : public SPIRVDuplicatesTrackerBase<const T *> {};
template <>
class SPIRVDuplicatesTracker<SPIRV::SpecialTypeDescriptor>
: public SPIRVDuplicatesTrackerBase<SPIRV::SpecialTypeDescriptor> {};
class SPIRVGeneralDuplicatesTracker {
SPIRVDuplicatesTracker<Type> TT;
SPIRVDuplicatesTracker<Constant> CT;
SPIRVDuplicatesTracker<GlobalVariable> GT;
SPIRVDuplicatesTracker<Function> FT;
SPIRVDuplicatesTracker<Argument> AT;
SPIRVDuplicatesTracker<SPIRV::SpecialTypeDescriptor> ST;
// NOTE: using MOs instead of regs to get rid of MF dependency to be able
// to use flat data structure.
// NOTE: replacing DenseMap with MapVector doesn't affect overall correctness
// but makes LITs more stable, should prefer DenseMap still due to
// significant perf difference.
using SPIRVReg2EntryTy =
MapVector<MachineOperand *, SPIRV::DTSortableEntry *>;
template <typename T>
void prebuildReg2Entry(SPIRVDuplicatesTracker<T> &DT,
SPIRVReg2EntryTy &Reg2Entry);
public:
void buildDepsGraph(std::vector<SPIRV::DTSortableEntry *> &Graph,
MachineModuleInfo *MMI);
void add(const Type *T, const MachineFunction *MF, Register R) {
TT.add(T, MF, R);
}
void add(const Constant *C, const MachineFunction *MF, Register R) {
CT.add(C, MF, R);
}
void add(const GlobalVariable *GV, const MachineFunction *MF, Register R) {
GT.add(GV, MF, R);
}
void add(const Function *F, const MachineFunction *MF, Register R) {
FT.add(F, MF, R);
}
void add(const Argument *Arg, const MachineFunction *MF, Register R) {
AT.add(Arg, MF, R);
}
void add(const SPIRV::SpecialTypeDescriptor &TD, const MachineFunction *MF,
Register R) {
ST.add(TD, MF, R);
}
Register find(const Type *T, const MachineFunction *MF) {
return TT.find(const_cast<Type *>(T), MF);
}
Register find(const Constant *C, const MachineFunction *MF) {
return CT.find(const_cast<Constant *>(C), MF);
}
Register find(const GlobalVariable *GV, const MachineFunction *MF) {
return GT.find(const_cast<GlobalVariable *>(GV), MF);
}
Register find(const Function *F, const MachineFunction *MF) {
return FT.find(const_cast<Function *>(F), MF);
}
Register find(const Argument *Arg, const MachineFunction *MF) {
return AT.find(const_cast<Argument *>(Arg), MF);
}
Register find(const SPIRV::SpecialTypeDescriptor &TD,
const MachineFunction *MF) {
return ST.find(TD, MF);
}
const SPIRVDuplicatesTracker<Type> *getTypes() { return &TT; }
};
} // namespace llvm
#endif // LLVM_LIB_TARGET_SPIRV_SPIRVDUPLICATESTRACKER_H
|