Repository: shader-slang/slang
File: source/slang/slang-emit-spirv.cpp (C++)

Code before:
                for (auto prev = inst->getPrevInst(); prev; prev = prev->getPrevInst())
                {
                    if (auto debugScope = as<IRDebugScope>(prev))
                    {
                        return ensureInst(debugScope->getScope());
                    }
                }
            }
            else if (as<IRFunc>(parent) || as<IRModuleInst>(parent))
            {
                SpvInst* spvInst = nullptr;
                if (m_mapIRInstToSpvDebugInst.tryGetValue(parent, spvInst))
                    return spvInst;

                // If we reached the module inst and the debug scope isn't found yet,
                // ensure the debug compilation unit is created.
                if (as<IRModuleInst>(parent))
                {
                    if (auto module = parent->getModule())
                    {
                        ensureDebugCompilationUnit(module);
                        // Try again after ensuring the compilation unit
                        if (m_mapIRInstToSpvDebugInst.tryGetValue(parent, spvInst))
                            return spvInst;
                    }
                }
            }

            inst = parent;
        }
        return nullptr;
    }

    /// Get or reserve a SpvID for an IR value.
    SpvWord getIRInstSpvID(IRInst* inst)
    {
        // If we have already emitted an SpvInst for `inst`, return its ID.
        SpvInst* spvInst = nullptr;
        if (m_mapIRInstToSpvInst.tryGetValue(inst, spvInst))
            return getID(spvInst);
        // Check if we have reserved an ID for `inst`.
        SpvWord result = 0;
        if (m_mapIRInstToSpvID.tryGetValue(inst, result))
            return result;
        // Otherwise, reserve a new ID for inst, and register it in `m_mapIRInstToSpvID`.
        result = m_nextID;
        ++m_nextID;
        m_mapIRInstToSpvID[inst] = result;
        return result;
    }


Review comment (bug):
I think it will be a bit cleaner if we split two cases:
```
            else if (as<IRFunc>(parent))
            {
                SpvInst* spvInst = nullptr;
                if (m_mapIRInstToSpvDebugInst.tryGetValue(parent, spvInst))
                    return spvInst;
            }
            else if (auto module = as<IRModuleInst>(parent))
            {
                SpvInst* spvInst = nullptr;
                if (m_mapIRInstToSpvDebugInst.tryGetValue(parent, spvInst))
                    return spvInst;

                // If we reached the module inst and the debug scope isn't found yet,
                // ensure the debug compilation unit is created.
                ensureDebugCompilationUnit(module);

                // Try again after ensuring the compilation unit
                if (m_mapIRInstToSpvDebugInst.tryGetValue(parent, spvInst))
                    return spvInst;
            }
```

Code after:
                for (auto prev = inst->getPrevInst(); prev; prev = prev->getPrevInst())
                {
                    if (auto debugScope = as<IRDebugScope>(prev))
                    {
                        return ensureInst(debugScope->getScope());
                    }
                }
            }
            else if (as<IRFunc>(parent) || as<IRModuleInst>(parent))
            {
                SpvInst* spvInst = nullptr;
                if (m_mapIRInstToSpvDebugInst.tryGetValue(parent, spvInst))
                    return spvInst;
            }

            inst = parent;
        }
        return nullptr;
    }

    /// Get or reserve a SpvID for an IR value.
    SpvWord getIRInstSpvID(IRInst* inst)
    {
        // If we have already emitted an SpvInst for `inst`, return its ID.
        SpvInst* spvInst = nullptr;
        if (m_mapIRInstToSpvInst.tryGetValue(inst, spvInst))
            return getID(spvInst);
        // Check if we have reserved an ID for `inst`.
        SpvWord result = 0;
        if (m_mapIRInstToSpvID.tryGetValue(inst, result))
            return result;
        // Otherwise, reserve a new ID for inst, and register it in `m_mapIRInstToSpvID`.
        result = m_nextID;
        ++m_nextID;
        m_mapIRInstToSpvID[inst] = result;
        return result;
    }

    // When we are emitting an instruction that can produce
    // a result, we will allocate an <id> to it so that other
    // instructions can refer to it.
    //
    // We will allocate <id>s on emand as they are needed.

    SpvWord freshID() { return m_nextID++; }

    /// Get the <id> for `inst`, or assign one if it doesn't have one yet
    SpvWord getID(SpvInst* inst)
    {
        auto id = inst->id;
        if (!id)