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

Code before:
            // existance of Load instructions when the entry parameters are replaced with the
            // builtin variables.
            auto loadInstType = loadInst->getDataType();
            switch (loadInstType->getOp())
            {
            case kIROp_Int8Type:
            case kIROp_Int16Type:
            case kIROp_IntType:
            case kIROp_Int64Type:
            case kIROp_UInt8Type:
            case kIROp_UInt16Type:
            case kIROp_UIntType:
            case kIROp_UInt64Type:
            case kIROp_IntPtrType:
            case kIROp_UIntPtrType:
            case kIROp_FloatType:
            case kIROp_DoubleType:
            case kIROp_HalfType:
            case kIROp_BoolType:
                continue;
            }

            auto loadedPtr = loadInst->getPtr();

            // Optimize only when the type is immutable (ConstRef).
            auto loadedPtrType = loadedPtr->getDataType();
            if (!as<IRConstRefType>(loadedPtrType) && !as<IRParameterBlockType>(loadedPtrType) &&
                !as<IRConstantBufferType>(loadedPtrType))
                continue;

            // We cannot optimize if the variable is passed to a function call that treats the
            // parameter as mutable.
            bool isSafeToOptimize = true;

            // Check if variable is only used in function calls with ConstRef parameters
            for (auto use = destPtr->firstUse; use; use = use->nextUse)
            {
                auto user = use->getUser();
                if (user == storeInst)
                    continue; // Skip the store itself

                auto call = as<IRCall>(user);
                if (!call)
                {
                    isSafeToOptimize = false;
                    break;
                }

                auto callee = call->getCallee();
                auto funcInst = as<IRFunc>(callee);
                if (!funcInst)

Review comment (refactor):
This is too restrictive. You need to call getRootAddr(loadedPtr) to get the address root of loadedPtr, and see if that is immutable.

You need a helper that is isAddressImmutable() to implement all cases where an address is immutable. These include parameter block, ConstantBuffer, StructuredBufferGetElementPtr, ConstRef and any Pointer types whose access is Read.

Code after:
    // leaves many linakge decorations in place that we seemingly
    // don't need/want, so this option currently can't be enabled
    // unconditionally.
    //
    if (options.keepExportsAlive)
    {
        bool isImported = false;
        bool shouldKeptAliveIfImported = false;
        IRInst* innerInst = inst;
        if (auto genInst = as<IRGeneric>(inst))
        {
            innerInst = findInnerMostGenericReturnVal(genInst);
        }
        for (auto decor : inst->getDecorations())
        {
            switch (decor->getOp())
            {
            case kIROp_ExportDecoration:
                return true;
            case kIROp_ImportDecoration:
                isImported = true;
                break;
            }
        }
        for (auto decor : innerInst->getDecorations())
        {
            switch (decor->getOp())
            {
            case kIROp_ForwardDerivativeDecoration:
            case kIROp_UserDefinedBackwardDerivativeDecoration:
            case kIROp_PrimalSubstituteDecoration:
                shouldKeptAliveIfImported = true;
                break;
            }
        }
        if (isImported && shouldKeptAliveIfImported)
            return true;
    }

    if (options.keepLayoutsAlive && inst->findDecoration<IRLayoutDecoration>())
    {
        return true;
    }

    // A basic block is an interesting case. Knowing that a function
    // is live means that its entry block is live, but the liveness
    // of any other blocks is determined by whether they are referenced
    // by other instructions (e.g., a branch from one block to
    // another).
    //
    if (auto block = as<IRBlock>(inst))