Repository: shader-slang/slang
File: source/slang/slang-ir-cleanup-void.cpp (C++)

Code before:
                                    debugVar->setArgIndex(
                                        builder.getIntValue(builder.getUIntType(), newArgIndex));
                                }
                            }
                        }
                    }
                }

                // Detect any VoidType DebugVars, e.g. from locally declared
                // variables
                auto debugVarType = debugVar->getDataType();
                if (auto ptrType = as<IRPtrTypeBase>(debugVarType))
                {
                    debugVarType = ptrType->getValueType();
                }
                if (debugVarType->getOp() == kIROp_VoidType)
                {
                    removeDebugVar = true;
                }

                // Remove the VoidType DebugVar and any DebugValue users of the DebugVar
                if (removeDebugVar)
                {
                    List<IRInst*> debugValueUsersToRemove;
                    bool hasNonDebugValueUsers =
                        false; // Keep the DebugVar if there are non-DebugValue users
                    for (auto use = debugVar->firstUse; use; use = use->nextUse)
                    {
                        if (auto debugValue = as<IRDebugValue>(use->getUser()))
                        {
                            debugValueUsersToRemove.add(debugValue);
                        }
                        else
                        {
                            hasNonDebugValueUsers = true;
                        }
                    }

                    for (auto debugValue : debugValueUsersToRemove)
                    {
                        debugValue->removeFromParent();
                        m_instsToRemove.add(debugValue);
                    }
                    if (!hasNonDebugValueUsers)
                    {
                        debugVar->removeFromParent();
                        m_instsToRemove.add(debugVar);
                    }
                }
            }
            break;

Review comment (bug):
It is a bit confusing with double-negative.
Can we rename it to `mayHaveDebugValueUsers = true;`?

Code after:
                                    builder.setInsertBefore(debugVar);
                                    debugVar->setArgIndex(
                                        builder.getIntValue(builder.getUIntType(), newArgIndex));
                                }
                            }
                        }
                    }
                }

                // Detect any VoidType DebugVars, e.g. from locally declared
                // variables
                auto debugVarType = debugVar->getDataType();
                while (auto ptrType = as<IRPtrTypeBase>(debugVarType))
                {
                    debugVarType = ptrType->getValueType();
                }
                if (debugVarType && debugVarType->getOp() == kIROp_VoidType)
                {
                    removeDebugVar = true;
                }

                // Remove the VoidType DebugVar and any DebugValue users of the DebugVar
                if (removeDebugVar)
                {
                    List<IRInst*> debugValueUsersToRemove;
                    bool hasOnlyDebugValueUsers =
                        true; // Keep the DebugVar if there are non-DebugValue users
                    for (auto use = debugVar->firstUse; use; use = use->nextUse)
                    {
                        if (auto debugValue = as<IRDebugValue>(use->getUser()))
                        {
                            debugValueUsersToRemove.add(debugValue);
                        }
                        else
                        {
                            hasOnlyDebugValueUsers = false;
                        }
                    }

                    for (auto debugValue : debugValueUsersToRemove)
                    {
                        m_instsToRemove.add(debugValue);
                    }
                    if (hasOnlyDebugValueUsers)
                    {
                        m_instsToRemove.add(debugVar);
                    }
                }
            }
            break;
        case kIROp_FuncType: