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

Method getBytecodeSize

lib/Bytecode/Reader/BytecodeReader.cpp:2416–2459  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2414//===----------------------------------------------------------------------===//
2415
2416std::optional<size_t> cuda_tile::getBytecodeSize(const char *bytecodeBuffer) {
2417 if (!isTileIRBytecode(bytecodeBuffer))
2418 return std::nullopt;
2419
2420 auto charBuffer = reinterpret_cast<const unsigned char*>(bytecodeBuffer);
2421 if (charBuffer[sizeof(kTileIRBytecodeMagic)] == 0)
2422 return std::nullopt;
2423
2424 // Build a buffer assuming we have the maximum size of the bytecode, we'll
2425 // infer the actual size as we parse the bytecode.
2426 ArrayRef<uint8_t> bytecodeData(
2427 reinterpret_cast<const uint8_t *>(bytecodeBuffer),
2428 SIZE_MAX - reinterpret_cast<uintptr_t>(bytecodeBuffer));
2429
2430 // Set up the reader and context.
2431 MLIRContext context(MLIRContext::Threading::DISABLED);
2432 EncodingReader reader(bytecodeData, context);
2433 ScopedDiagnosticHandler handler(&context, [](Diagnostic &diag) {
2434 // Ignore all errors.
2435 });
2436
2437 // Parse the header of the bytecode.
2438 BytecodeVersion version;
2439 if (failed(parseHeader(reader, context, version)))
2440 return std::nullopt;
2441
2442 // Parse the sections until we reach the end of the bytecode. We don't
2443 // actually try to reason about the section data, we just want to know the
2444 // sizes.
2445 std::array<bool, Section::NumSections> seenSections;
2446 seenSections.fill(false);
2447 while (true) {
2448 // Parse the next section.
2449 SectionHeader header;
2450 if (failed(parseSectionHeader(reader, header, context)) ||
2451 failed(reader.skip(header.length)) ||
2452 std::exchange(seenSections[header.sectionID], true))
2453 return std::nullopt;
2454
2455 // Check for the end of the bytecode stream.
2456 if (header.sectionID == Section::EndOfBytecode)
2457 return reader.currentOffset();
2458 }
2459}
2460
2461OwningOpRef<cuda_tile::ModuleOp>
2462cuda_tile::readBytecode(llvm::MemoryBufferRef bytecodeBuffer,

Callers

nothing calls this directly

Calls 4

parseHeaderFunction · 0.85
parseSectionHeaderFunction · 0.85
skipMethod · 0.80
currentOffsetMethod · 0.80

Tested by

no test coverage detected