Loads/produces a ModuleOp into `outMod` based on cfg.input.kind. Returns success() on success, failure() on any error.
| 176 | // Loads/produces a ModuleOp into `outMod` based on cfg.input.kind. |
| 177 | // Returns success() on success, failure() on any error. |
| 178 | static LogicalResult loadInputModule(TileIROptimizerConfig &cfg, |
| 179 | MLIRContext &context, |
| 180 | OwningOpRef<mlir::ModuleOp> &outMod) { |
| 181 | // The values of cfg.input.buffer & cfg.input.filename are already checked |
| 182 | // during the call of validateConfig() |
| 183 | // 1) Materialize a MemoryBuffer + MemoryBufferRef regardless of source |
| 184 | std::unique_ptr<llvm::MemoryBuffer> owned; |
| 185 | llvm::MemoryBufferRef ref; |
| 186 | StringRef *buf = nullptr; |
| 187 | |
| 188 | if (auto fname = std::get_if<TileIROptInput::FileT>(&cfg.input.value)) { |
| 189 | // Read raw bytes (no text-mode CRLF translation), so detection is reliable. |
| 190 | auto bufOrErr = llvm::MemoryBuffer::getFile(*fname, /*IsText=*/false); |
| 191 | if (!bufOrErr) |
| 192 | return emitConfigError( |
| 193 | &context, (llvm::Twine("Failed to read file: ") + *fname).str()); |
| 194 | |
| 195 | owned = std::move(*bufOrErr); |
| 196 | ref = owned->getMemBufferRef(); |
| 197 | } else { |
| 198 | buf = std::get_if<TileIROptInput::BufferT>(&cfg.input.value); |
| 199 | // No copy here. Build a non-owning view onto caller's memory. |
| 200 | ref = llvm::MemoryBufferRef(*buf, ""); |
| 201 | } |
| 202 | |
| 203 | // Parse depending on detected type |
| 204 | if (cuda_tile::isTileIRBytecode(ref)) { |
| 205 | // CUDA Tile IR bytecode |
| 206 | outMod = parseTileIRBytecode(ref, context); |
| 207 | } else { |
| 208 | // MLIR textual IR |
| 209 | llvm::SourceMgr sm; |
| 210 | if (buf) { |
| 211 | // Create an owned, null-terminated copy ONLY for the Buffer path. |
| 212 | // This guarantees ownership + '\0' for SourceMgr. |
| 213 | owned = llvm::MemoryBuffer::getMemBufferCopy(*buf, ""); |
| 214 | if (!owned) |
| 215 | return emitConfigError(&context, |
| 216 | "Failed to allocate buffer copy for MLIR text."); |
| 217 | } |
| 218 | // If cfg.input.kind == K::File, 'owned' was already set from getFile() |
| 219 | // above. |
| 220 | sm.AddNewSourceBuffer(std::move(owned), llvm::SMLoc()); |
| 221 | outMod = parseSourceFile<mlir::ModuleOp>(sm, &context); |
| 222 | } |
| 223 | |
| 224 | if (!outMod) |
| 225 | return emitConfigError(&context, "Failed to parse input"); |
| 226 | return success(); |
| 227 | } |
| 228 | |
| 229 | static LogicalResult emitOutputs(TileIROptimizerConfig &cfg, |
| 230 | OwningOpRef<mlir::ModuleOp> &parentModule) { |
no test coverage detected