--- lib/SPIRV/libSPIRV/SPIRVStream.h.orig 2026-09-02 08:54:13 UTC +++ lib/SPIRV/libSPIRV/SPIRVStream.h @@ -98,10 +98,20 @@ class SPIRVNL { friend spv_ostream &operator<<(spv_ostream &O, const SPIRVNL &E); }; +/// Read a single SPIR-V word from a binary stream, byte-swapping from +/// little-endian on big-endian hosts. +inline uint32_t readSPIRVWord(std::istream &IS) { + uint32_t W; + IS.read(reinterpret_cast(&W), sizeof(W)); +#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + W = __builtin_bswap32(W); +#endif + return W; +} + template const SPIRVDecoder &decodeBinary(const SPIRVDecoder &I, T &V) { - uint32_t W; - I.IS.read(reinterpret_cast(&W), sizeof(W)); + uint32_t W = readSPIRVWord(I.IS); V = static_cast(W); SPIRVDBG(spvdbgs() << "Read word: W = " << W << " V = " << V << '\n'); return I; @@ -186,6 +196,10 @@ const SPIRVEncoder &operator<<(const SPIRVEncoder &O, } #endif uint32_t W = static_cast(V); +#if defined(__BYTE_ORDER__) && __BYTE_ORDER__ == __ORDER_BIG_ENDIAN__ + // SPIR-V binary format is little-endian; byte-swap on big-endian hosts. + W = __builtin_bswap32(W); +#endif O.OS.write(reinterpret_cast(&W), sizeof(W)); return O; }