MCPcopy Create free account
hub / github.com/NVIDIA/cuda-tile / parseConstantSection

Function parseConstantSection

lib/Bytecode/Reader/BytecodeReader.cpp:727–775  ·  view source on GitHub ↗

Parses the constant section and populates the constant table

Source from the content-addressed store, hash-verified

725
726/// Parses the constant section and populates the constant table
727static LogicalResult
728parseConstantSection(ArrayRef<uint8_t> payload,
729 std::vector<ArrayRef<uint8_t>> &constants,
730 MLIRContext &context) {
731 EncodingReader reader(payload, context);
732 uint64_t numConstants;
733 if (failed(reader.readVarInt(numConstants,
734 std::numeric_limits<uint32_t>::max() - 1)))
735 return failure();
736 // Handle empty constant section case
737 if (numConstants == 0)
738 return success();
739 // Ensure 8-byte alignment for the start indices array
740 if (failed(reader.skipPadding(alignof(uint64_t))))
741 return failure();
742 // Check if we have enough data to read the indices
743 if (reader.remaining() / sizeof(uint64_t) < numConstants)
744 return reader.emitError() << "insufficient data for constant indices";
745
746 // Read constant start indices as a contiguous array
747 const uint64_t *startIndicesPtr =
748 reinterpret_cast<const uint64_t *>(reader.getCurrentPtr());
749 if (!startIndicesPtr)
750 return failure();
751
752 ArrayRef<uint64_t> constantStartIndices(startIndicesPtr, numConstants);
753 if (failed(reader.skip(constantStartIndices.size() * sizeof(uint64_t))))
754 return failure();
755 ArrayRef<uint8_t> constantData = payload.slice(reader.currentOffset());
756 // Populate constants based on constantStartIndices
757 constants.reserve(numConstants);
758 for (uint64_t i = 0; i < numConstants; ++i) {
759 uint64_t start = constantStartIndices[i];
760 uint64_t end = (i + 1 < numConstants) ? constantStartIndices[i + 1]
761 : constantData.size();
762 if (end < start)
763 return reader.emitError()
764 << "invalid constant start indices: end (" << end
765 << ") is less than start (" << start << ") for constant " << i;
766 size_t constantSize = end - start;
767 if (constantSize + start > constantData.size())
768 return reader.emitError()
769 << "constant " << i << " extends beyond available data: "
770 << "size=" << constantSize << ", start=" << start
771 << ", total data size=" << constantData.size();
772 constants.push_back(constantData.slice(start, constantSize));
773 }
774 return success();
775}
776
777namespace {
778

Callers 1

readBytecodeMethod · 0.85

Calls 9

maxFunction · 0.85
readVarIntMethod · 0.80
skipPaddingMethod · 0.80
remainingMethod · 0.80
emitErrorMethod · 0.80
getCurrentPtrMethod · 0.80
skipMethod · 0.80
sizeMethod · 0.80
currentOffsetMethod · 0.80

Tested by

no test coverage detected