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

Code before:
            m_targetProgram->getOptionSet(),
            elementType,
            &elementLayout));
        IRIntegerValue elementStride = elementLayout.getStride();

        auto indexType = m_builder.getIntType();
        for (IRIntegerValue ii = 0; ii < elementCount; ++ii)
        {
            auto elementIndex = m_builder.getIntValue(indexType, ii);
            auto elementVal = m_builder.emitElementExtract(elementType, value, elementIndex);
            SLANG_RETURN_ON_FAIL(emitLegalStore(
                elementType,
                buffer,
                baseOffset,
                immediateOffset + ii * elementStride,
                alignment,
                elementVal));
        }

        return SLANG_OK;
    }

    // Process legalized parameters and propagate type changes to their uses.
    // This handles all parameters that were converted from ByteAddressBuffer
    // to StructuredBuffer, updating their "use"-s.
    void processParamTypes()
    {
        List<IRInst*> workList;

        for (auto& [babIR, structuredBufferType] : byteAddrBufferToReplace)
        {
            workList.add(babIR);
        }

        while (workList.getCount() > 0)
        {
            auto babIR = workList.getLast();
            workList.removeLast();

            // iterate all "use"-s of `babIR` and change their type
            // to `validBufferType`.
            auto validBufferType = byteAddrBufferToReplace[babIR];

            auto babIRType = babIR->getDataType();
            if (isTypeLegalForByteAddressLoadStore(babIRType))
            {
                // Element type should be same.
                SLANG_ASSERT(babIRType == validBufferType);
                continue; // skip if already structuredbuffer
            }


Review comment (bug):
I think we shouldn't need this function. This pass seems to miss the logic to replace all `ByteAddressBuffer` types to `getEquivalentStructuredBufferParamType(babType)` after processing all the ByteAddressBufferLoad/Store instructions. Simply replace the types at the end of `processModule` should fix any crashes.

We should also check and see whether ByteAddressBuffers work through bindless handles, e.g. whether or not ByteAddressBuffer.Handle works correctly.

Code after:
        {
            auto elementIndex = m_builder.getIntValue(indexType, ii);
            auto elementVal = m_builder.emitElementExtract(elementType, value, elementIndex);
            SLANG_RETURN_ON_FAIL(emitLegalStore(
                elementType,
                buffer,
                baseOffset,
                immediateOffset + ii * elementStride,
                alignment,
                elementVal));
        }

        return SLANG_OK;
    }
};


void legalizeByteAddressBufferOps(
    Session* session,
    TargetProgram* program,
    IRModule* module,
    DiagnosticSink* sink,
    ByteAddressBufferLegalizationOptions const& options)
{
    ByteAddressBufferLegalizationContext context;
    context.m_session = session;
    context.m_target = program->getTargetReq();
    context.m_options = options;
    context.m_targetProgram = program;
    context.m_sink = sink;
    context.processModule(module);
}

} // namespace Slang