1- // ===-- SPIRVDuplicatesTracker .h - SPIR-V Duplicates Tracker ----*- C++ -*-===//
1+ // ===------------ SPIRVMapping .h - SPIR-V Duplicates Tracker ----*- C++ -*-===//
22//
33// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
44// See https://llvm.org/LICENSE.txt for license information.
2626
2727namespace llvm {
2828namespace SPIRV {
29- /*
30- inline size_t to_hash(const MachineInstr *MI,
31- std::unordered_set<const MachineInstr *> &Visited) {
32- if (!MI || !Visited.insert(MI).second)
33- return 0;
34- const MachineRegisterInfo &MRI = MI->getMF()->getRegInfo();
35- SmallVector<size_t, 16> Codes{MI->getOpcode()};
36- size_t H;
37- for (unsigned I = MI->getNumDefs(); I < MI->getNumOperands(); ++I) {
38- const MachineOperand &MO = MI->getOperand(I);
39- H = MO.isReg() ? to_hash(getDef(MO, &MRI), Visited)
40- : size_t(llvm::hash_value(MO));
41- Codes.push_back(H);
42- }
43- return llvm::hash_combine(Codes.begin(), Codes.end());
44- }
45-
46- inline size_t to_hash(const MachineInstr *MI) {
47- std::unordered_set<const MachineInstr *> Visited;
48- return to_hash(MI, Visited);
49- }
50- */
5129
5230inline size_t to_hash (const MachineInstr *MI) {
5331 hash_code H = llvm::hash_combine (MI->getOpcode (), MI->getNumOperands ());
@@ -63,10 +41,10 @@ inline size_t to_hash(const MachineInstr *MI) {
6341 return H;
6442}
6543
66- using MIHandle = std::pair <const MachineInstr *, size_t >;
44+ using MIHandle = std::tuple <const MachineInstr *, Register , size_t >;
6745
6846inline MIHandle getMIKey (const MachineInstr *MI) {
69- return std::make_pair (MI, SPIRV::to_hash (MI));
47+ return std::make_tuple (MI, MI-> getOperand ( 0 ). getReg () , SPIRV::to_hash (MI));
7048}
7149
7250using IRHandle = std::tuple<const void *, unsigned , unsigned >;
@@ -200,10 +178,8 @@ class SPIRVIRMapping {
200178 SPIRV::MIHandle MIKey = SPIRV::getMIKey (MI);
201179 auto It1 = Vregs.try_emplace (HandleMF, MIKey);
202180 if (!It1.second ) {
203- // there is an expired record
204- auto [ExistMI, _] = It1.first ->second ;
205- // invalidate the record
206- Defs.erase (ExistMI);
181+ // there is an expired record that we need to invalidate
182+ Defs.erase (std::get<0 >(It1.first ->second ));
207183 // update the record
208184 It1.first ->second = MIKey;
209185 }
@@ -225,13 +201,14 @@ class SPIRVIRMapping {
225201 auto It = Vregs.find (HandleMF);
226202 if (It == Vregs.end ())
227203 return nullptr ;
228- auto [MI, Hash] = It->second ;
229- assert ( SPIRV::to_hash (MI) == Hash );
230- assert (Defs. find (MI) != Defs. end () && Defs. find (MI)-> second == HandleMF);
231- /* if (SPIRV::to_hash(MI) != Hash) {
204+ auto [MI, Reg, Hash] = It->second ;
205+ const MachineInstr *Def = MF-> getRegInfo (). getVRegDef (Reg );
206+ if (!Def || Def != MI || SPIRV::to_hash (MI) != Hash) {
207+ // there is an expired record that we need to invalidate
232208 erase (MI);
233209 return nullptr ;
234- }*/
210+ }
211+ assert (Defs.find (MI) != Defs.end () && Defs.find (MI)->second == HandleMF);
235212 return MI;
236213 }
237214 Register find (SPIRV::IRHandle Handle, const MachineFunction *MF) {
0 commit comments