Skip to content

[SPIR-V] Duplicates Tracker accounts for possible changes in Constant usage after optimization #110835

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 25 additions & 24 deletions llvm/lib/Target/SPIRV/SPIRVDuplicatesTracker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,37 +12,39 @@
//===----------------------------------------------------------------------===//

#include "SPIRVDuplicatesTracker.h"
#include "SPIRVInstrInfo.h"

#define DEBUG_TYPE "build-dep-graph"

using namespace llvm;

template <typename T>
void SPIRVGeneralDuplicatesTracker::prebuildReg2Entry(
SPIRVDuplicatesTracker<T> &DT, SPIRVReg2EntryTy &Reg2Entry) {
SPIRVDuplicatesTracker<T> &DT, SPIRVReg2EntryTy &Reg2Entry,
const SPIRVInstrInfo *TII) {
for (auto &TPair : DT.getAllUses()) {
for (auto &RegPair : TPair.second) {
const MachineFunction *MF = RegPair.first;
Register R = RegPair.second;
MachineInstr *MI = MF->getRegInfo().getUniqueVRegDef(R);
if (!MI)
if (!MI || (TPair.second.getIsConst() && !TII->isConstantInstr(*MI)))
continue;
Reg2Entry[&MI->getOperand(0)] = &TPair.second;
}
}
}

void SPIRVGeneralDuplicatesTracker::buildDepsGraph(
std::vector<SPIRV::DTSortableEntry *> &Graph,
std::vector<SPIRV::DTSortableEntry *> &Graph, const SPIRVInstrInfo *TII,
MachineModuleInfo *MMI = nullptr) {
SPIRVReg2EntryTy Reg2Entry;
prebuildReg2Entry(TT, Reg2Entry);
prebuildReg2Entry(CT, Reg2Entry);
prebuildReg2Entry(GT, Reg2Entry);
prebuildReg2Entry(FT, Reg2Entry);
prebuildReg2Entry(AT, Reg2Entry);
prebuildReg2Entry(MT, Reg2Entry);
prebuildReg2Entry(ST, Reg2Entry);
prebuildReg2Entry(TT, Reg2Entry, TII);
prebuildReg2Entry(CT, Reg2Entry, TII);
prebuildReg2Entry(GT, Reg2Entry, TII);
prebuildReg2Entry(FT, Reg2Entry, TII);
prebuildReg2Entry(AT, Reg2Entry, TII);
prebuildReg2Entry(MT, Reg2Entry, TII);
prebuildReg2Entry(ST, Reg2Entry, TII);

for (auto &Op2E : Reg2Entry) {
SPIRV::DTSortableEntry *E = Op2E.second;
Expand All @@ -65,20 +67,19 @@ void SPIRVGeneralDuplicatesTracker::buildDepsGraph(
if (MI->getOpcode() == SPIRV::OpConstantFunctionPointerINTEL && i == 2)
continue;
MachineOperand *RegOp = &VRegDef->getOperand(0);
LLVM_DEBUG({
if (Reg2Entry.count(RegOp) == 0 &&
(MI->getOpcode() != SPIRV::OpVariable || i != 3)) {
dbgs() << "Unexpected pattern while building a dependency "
"graph.\nInstruction: ";
MI->print(dbgs());
dbgs() << "Operand: ";
Op.print(dbgs());
dbgs() << "\nOperand definition: ";
VRegDef->print(dbgs());
}
});
assert((MI->getOpcode() == SPIRV::OpVariable && i == 3) ||
Reg2Entry.count(RegOp));
if (Reg2Entry.count(RegOp) == 0 &&
(MI->getOpcode() != SPIRV::OpVariable || i != 3)) {
std::string DiagMsg;
raw_string_ostream OS(DiagMsg);
OS << "Unexpected pattern while building a dependency "
"graph.\nInstruction: ";
MI->print(OS);
OS << "Operand: ";
Op.print(OS);
OS << "\nOperand definition: ";
VRegDef->print(OS);
report_fatal_error(DiagMsg.c_str());
}
if (Reg2Entry.count(RegOp))
E->addDep(Reg2Entry[RegOp]);
}
Expand Down
15 changes: 12 additions & 3 deletions llvm/lib/Target/SPIRV/SPIRVDuplicatesTracker.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

namespace llvm {
namespace SPIRV {
class SPIRVInstrInfo;
// 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.
Expand All @@ -35,8 +36,9 @@ class DTSortableEntry : public MapVector<const MachineFunction *, Register> {
struct FlagsTy {
unsigned IsFunc : 1;
unsigned IsGV : 1;
unsigned IsConst : 1;
// NOTE: bit-field default init is a C++20 feature.
FlagsTy() : IsFunc(0), IsGV(0) {}
FlagsTy() : IsFunc(0), IsGV(0), IsConst(0) {}
};
FlagsTy Flags;

Expand All @@ -45,8 +47,10 @@ class DTSortableEntry : public MapVector<const MachineFunction *, Register> {
// require hoisting of params as well.
bool getIsFunc() const { return Flags.IsFunc; }
bool getIsGV() const { return Flags.IsGV; }
bool getIsConst() const { return Flags.IsConst; }
void setIsFunc(bool V) { Flags.IsFunc = V; }
void setIsGV(bool V) { Flags.IsGV = V; }
void setIsConst(bool V) { Flags.IsConst = V; }

const SmallVector<DTSortableEntry *, 2> &getDeps() const { return Deps; }
void addDep(DTSortableEntry *E) { Deps.push_back(E); }
Expand Down Expand Up @@ -160,6 +164,10 @@ template <typename KeyTy> class SPIRVDuplicatesTrackerBase {
typename std::remove_const<
typename std::remove_pointer<KeyTy>::type>::type>())
Storage[V].setIsGV(true);
if (std::is_same<Constant,
typename std::remove_const<
typename std::remove_pointer<KeyTy>::type>::type>())
Storage[V].setIsConst(true);
}

Register find(KeyTy V, const MachineFunction *MF) const {
Expand Down Expand Up @@ -211,11 +219,12 @@ class SPIRVGeneralDuplicatesTracker {

template <typename T>
void prebuildReg2Entry(SPIRVDuplicatesTracker<T> &DT,
SPIRVReg2EntryTy &Reg2Entry);
SPIRVReg2EntryTy &Reg2Entry,
const SPIRVInstrInfo *TII);

public:
void buildDepsGraph(std::vector<SPIRV::DTSortableEntry *> &Graph,
MachineModuleInfo *MMI);
const SPIRVInstrInfo *TII, MachineModuleInfo *MMI);

void add(const Type *Ty, const MachineFunction *MF, Register R) {
TT.add(unifyPtrType(Ty), MF, R);
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/Target/SPIRV/SPIRVGlobalRegistry.h
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,9 @@ class SPIRVGlobalRegistry {
}

void buildDepsGraph(std::vector<SPIRV::DTSortableEntry *> &Graph,
const SPIRVInstrInfo *TII,
MachineModuleInfo *MMI = nullptr) {
DT.buildDepsGraph(Graph, MMI);
DT.buildDepsGraph(Graph, TII, MMI);
}

void setBound(unsigned V) { Bound = V; }
Expand Down
2 changes: 1 addition & 1 deletion llvm/lib/Target/SPIRV/SPIRVModuleAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ void SPIRVModuleAnalysis::collectGlobalEntities(
void SPIRVModuleAnalysis::processDefInstrs(const Module &M) {
std::vector<SPIRV::DTSortableEntry *> DepsGraph;

GR->buildDepsGraph(DepsGraph, SPVDumpDeps ? MMI : nullptr);
GR->buildDepsGraph(DepsGraph, TII, SPVDumpDeps ? MMI : nullptr);

collectGlobalEntities(
DepsGraph, SPIRV::MB_TypeConstVars,
Expand Down
Loading