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

Code before:
        {
            // The uses had better all be uses of this
            // instruction, or invariants are broken.
            SLANG_ASSERT(uu->get() == thisInst);

            auto user = uu->getUser();
            bool userIsHoistable = getIROpInfo(user->getOp()).isHoistable();

            // We want to de-duplicate WitnessTable but we don't really want to hoist them.
            bool userNeedToBeHoisted = userIsHoistable && (user->getOp() != kIROp_WitnessTable);

            if (userNeedToBeHoisted)
            {
                if (!dedupContext)
                {
                    SLANG_ASSERT(user->getModule());
                    dedupContext = user->getModule()->getDeduplicationContext();
                }
                dedupContext->_removeGlobalNumberingEntry(user);
            }

            // Swap this use over to use the other value.
            uu->usedValue = other;

            if (as<IRSetBase>(uu->getUser()) != nullptr)
                setsToUpdate.add(as<IRSetBase>(user));

            // If `other` is hoistable, then we need to make sure `other` is hoisted
            // to a point before `user`, if it is not already so.
            _maybeHoistOperand(uu);

            if (userNeedToBeHoisted)
            {
                // Is the updated inst already exists in the global numbering map?
                // If so, we need to continue work on replacing the updated inst with the existing
                // value.
                IRInst* existingVal = nullptr;
                if (dedupContext->getGlobalValueNumberingMap().tryGetValue(
                        IRInstKey{user},
                        existingVal))
                {
                    // If existingVal has been replaced by something else, use that.
                    dedupContext->getInstReplacementMap().tryGetValue(existingVal, existingVal);
                    addToWorkList(user, existingVal);
                }
                else
                {
                    dedupContext->_addGlobalNumberingEntry(user);
                }
            }


Review comment (suggestion):
It looks like these two lines are casting `as<IRSetBase>(user)` twice, if you look at line 8036, you can probably create an `auto` var in the `if` statement.

Code after:

    addToWorkList(thisInst, other);

    List<IRSetBase*> setsToUpdate;
    for (Index i = 0; i < workList.getCount(); i++)
    {
        auto workItem = workList[i];
        thisInst = workItem.thisInst;
        other = workItem.otherInst;

        SLANG_ASSERT(other);

        // Safety check: don't try to replace something with itself.
        if (other == thisInst)
            continue;

        if (getIROpInfo(thisInst->getOp()).isHoistable())
        {
            if (!dedupContext)
            {
                SLANG_ASSERT(thisInst->getModule());
                dedupContext = thisInst->getModule()->getDeduplicationContext();
            }
            dedupContext->getInstReplacementMap()[thisInst] = other;
        }

        // We will walk through the list of uses for the current
        // instruction, and make them point to the other inst.
        IRUse* ff = thisInst->firstUse;

        // No uses? Nothing to do.
        if (!ff)
            continue;

        // ff->debugValidate();

        IRUse* uu = ff;
        for (;;)
        {
            // The uses had better all be uses of this
            // instruction, or invariants are broken.
            SLANG_ASSERT(uu->get() == thisInst);

            auto user = uu->getUser();
            bool userIsHoistable = getIROpInfo(user->getOp()).isHoistable();

            // We want to de-duplicate WitnessTable but we don't really want to hoist them.
            bool userNeedToBeHoisted = userIsHoistable && (user->getOp() != kIROp_WitnessTable);

            if (userNeedToBeHoisted)
            {