| 2414 | //===----------------------------------------------------------------------===// |
| 2415 | |
| 2416 | std::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 | |
| 2461 | OwningOpRef<cuda_tile::ModuleOp> |
| 2462 | cuda_tile::readBytecode(llvm::MemoryBufferRef bytecodeBuffer, |
nothing calls this directly
no test coverage detected