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

Code before:
                        return ensureInst(debugScope->getScope());
                    }
                }
            }
            else if (as<IRFunc>(parent))
            {
                SpvInst* spvInst = nullptr;
                if (m_mapIRInstToSpvDebugInst.tryGetValue(parent, spvInst))
                    return spvInst;
            }
            else if (auto moduleInst = 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 (auto module = moduleInst->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):
The tricky part of this issue is that we don't have a unit test case when the following line is added.
```
    translationUnit->addIncludedSourceFileIfNotExist(sourceFile);
```
The issue was observed only by the nightly CI on slang-rhi repo.
I think we need to add a new slang-test test case that can reproduce the issue and the test should go through this new lines of code.

When I ran, `tests/modules/simple-module-debug-info.slang` test with this PR on my local machine, I am not hitting a break-point set here.

We need a new slang-test test case that goes through this new lines of implementation.

Code after:
                        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)
        {
            id = freshID();
            inst->id = id;
        }