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

Code before:
        // when computing the dispatch thread ID and group thread index.
        //
        // TODO: If we ever wanted to support flexible thread-group sizes for our
        // CPU target, we'd need to change it so that the thread-group size can
        // be passed in as part of `ComputeVaryingThreadInput`.
        //
        groupExtents = emitCalcGroupExtents(builder, m_entryPointFunc, uint3Type);

        if (!groupExtents)
        {
            m_sink->diagnose(
                m_entryPointFunc,
                Diagnostics::unsupportedSpecializationConstantForNumThreads);

            // Fill in placeholder values.
            static const int kAxisCount = 3;
            IRInst* groupExtentAlongAxis[kAxisCount] = {};
            for (int axis = 0; axis < kAxisCount; axis++)
                groupExtentAlongAxis[axis] = builder.getIntValue(uint3Type->getElementType(), 1);
            groupExtents = builder.emitMakeVector(uint3Type, kAxisCount, groupExtentAlongAxis);
        }

        dispatchThreadID =
            emitCalcDispatchThreadID(builder, uint3Type, groupID, groupThreadID, groupExtents);
        UInt idx = 0;
        dispatchThreadID1D = builder.emitSwizzle(uintType, dispatchThreadID, 1, &idx);

        groupThreadIndex = emitCalcGroupIndex(builder, groupThreadID, groupExtents);
    }

    static bool isVectorParam(IRType* type)
    {
        if (as<IRVectorType>(type))
            return true;
        else if (auto ptr = as<IRPtrTypeBase>(type))
            return isVectorParam(ptr->getValueType());
        return false;
    }

    LegalizedVaryingVal createLegalSystemVaryingValImpl(VaryingParamInfo const& info) SLANG_OVERRIDE
    {
        // Because all of the relvant system values were synthesized
        // into the first block of the entry-point function, we can
        // just return them wherever they are referenced.
        //
        // Note that any values that were synthesized but then are
        // not referened will simply be eliminated as dead code
        // in later passes.

        switch (info.systemValueSemanticName)
        {

Review comment (suggestion):
This isn't general enough to handle the cases like `int2 tid : SV_DispatchThreadID` or 
`vector<uint8_t, 2> tid : SV_DispatchThreadID`.

We will need the same type adaptor mechanism implemented for other targets here, and same applies to group id and other builtins as well.

Code after:
        // to tell us the size of the compute thread group, which we will then use
        // when computing the dispatch thread ID and group thread index.
        //
        // TODO: If we ever wanted to support flexible thread-group sizes for our
        // CPU target, we'd need to change it so that the thread-group size can
        // be passed in as part of `ComputeVaryingThreadInput`.
        //
        groupExtents = emitCalcGroupExtents(builder, m_entryPointFunc, uint3Type);

        if (!groupExtents)
        {
            m_sink->diagnose(
                m_entryPointFunc,
                Diagnostics::unsupportedSpecializationConstantForNumThreads);

            // Fill in placeholder values.
            static const int kAxisCount = 3;
            IRInst* groupExtentAlongAxis[kAxisCount] = {};
            for (int axis = 0; axis < kAxisCount; axis++)
                groupExtentAlongAxis[axis] = builder.getIntValue(uint3Type->getElementType(), 1);
            groupExtents = builder.emitMakeVector(uint3Type, kAxisCount, groupExtentAlongAxis);
        }

        dispatchThreadID =
            emitCalcDispatchThreadID(builder, uint3Type, groupID, groupThreadID, groupExtents);

        groupThreadIndex = emitCalcGroupIndex(builder, groupThreadID, groupExtents);
    }

    LegalizedVaryingVal createLegalSystemVaryingValImpl(VaryingParamInfo const& info) SLANG_OVERRIDE
    {
        // Because all of the relvant system values were synthesized
        // into the first block of the entry-point function, we can
        // just return them wherever they are referenced.
        //
        // Note that any values that were synthesized but then are
        // not referened will simply be eliminated as dead code
        // in later passes.

        switch (info.systemValueSemanticName)
        {
        case SystemValueSemanticName::GroupID:
            return createLegalizedSystemVaryingValInst(info, groupID);
        case SystemValueSemanticName::GroupThreadID:
            return createLegalizedSystemVaryingValInst(info, groupThreadID);
        case SystemValueSemanticName::GroupIndex:
            return createLegalizedSystemVaryingValInst(info, groupThreadIndex);
        case SystemValueSemanticName::DispatchThreadID:
            return createLegalizedSystemVaryingValInst(info, dispatchThreadID);
        default:
            return diagnoseUnsupportedSystemVal(info);