| 660 | } |
| 661 | |
| 662 | MlirStringRef mlirCudaTileWriteBytecodeToBuffer(MlirOperation moduleOp) { |
| 663 | auto *op = unwrap(moduleOp); |
| 664 | |
| 665 | // Extract cuda_tile::ModuleOp (handles both direct and nested cases) |
| 666 | auto cudaTileModule = extractCudaTileModuleOp(op); |
| 667 | if (!cudaTileModule) |
| 668 | return mlirStringRefCreateFromCString(""); |
| 669 | |
| 670 | // Allocate buffer that caller must free |
| 671 | std::string temp; |
| 672 | llvm::raw_string_ostream stream(temp); |
| 673 | if (failed(cuda_tile::writeBytecode(stream, cudaTileModule, |
| 674 | BytecodeVersion::kCurrentVersion))) |
| 675 | return mlirStringRefCreateFromCString(""); |
| 676 | |
| 677 | stream.flush(); |
| 678 | |
| 679 | // Allocate persistent buffer |
| 680 | char *buffer = static_cast<char *>(malloc(temp.size())); |
| 681 | if (!buffer) |
| 682 | return mlirStringRefCreateFromCString(""); |
| 683 | |
| 684 | memcpy(buffer, temp.data(), temp.size()); |
| 685 | return mlirStringRefCreate(buffer, temp.size()); |
| 686 | } |
| 687 | |
| 688 | void mlirCudaTileFreeBuffer(MlirStringRef buffer) { |
| 689 | if (buffer.data && buffer.length > 0) |