Remove fork when redirecting stderr in onnx-mlir (#310)

Remove fork and add filenamebase option

Signed-off-by: Nathaniel McVicar <namcvica@microsoft.com>

Co-authored-by: Gheorghe-Teodor Bercea <gt.bercea@gmail.com>
This commit is contained in:
NathanielMcVicar 2020-09-25 15:39:46 -07:00 committed by GitHub
parent 278f10c3a4
commit aa2eed411f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 11 deletions

View File

@ -463,8 +463,6 @@ llvm::cl::opt<bool> printIR("printIR",
void outputCode( void outputCode(
mlir::OwningModuleRef &module, string filename, string extension) { mlir::OwningModuleRef &module, string filename, string extension) {
// Start a separate process to redirect the model output. I/O redirection
// changes will not be visible to the parent process.
string tempFilename = filename + extension; string tempFilename = filename + extension;
mlir::OpPrintingFlags flags; mlir::OpPrintingFlags flags;
if (preserveLocations) if (preserveLocations)
@ -473,18 +471,17 @@ void outputCode(
#ifdef _WIN32 #ifdef _WIN32
// copy original stderr file number // copy original stderr file number
int stderrOrigin = _dup(_fileno(stderr)); int stderrOrigin = _dup(_fileno(stderr));
#else
int stderrOrigin = dup(fileno(stderr));
#endif
freopen(tempFilename.c_str(), "w", stderr); freopen(tempFilename.c_str(), "w", stderr);
module->print(llvm::errs(), flags); module->print(llvm::errs(), flags);
fflush(stderr); fflush(stderr);
// set modified stderr as original stderr // set modified stderr as original stderr
#ifdef _WIN32
_dup2(stderrOrigin, _fileno(stderr)); _dup2(stderrOrigin, _fileno(stderr));
#else #else
if (fork() == 0) { dup2(stderrOrigin, fileno(stderr));
freopen(tempFilename.c_str(), "w", stderr);
module->print(llvm::errs(), flags);
fclose(stderr);
exit(0);
}
#endif #endif
if (printIR) if (printIR)
module->print(llvm::outs(), flags); module->print(llvm::outs(), flags);

View File

@ -21,6 +21,10 @@ int main(int argc, char *argv[]) {
llvm::cl::desc("<input file>"), llvm::cl::init("-"), llvm::cl::desc("<input file>"), llvm::cl::init("-"),
llvm::cl::cat(OnnxMlirOptions)); llvm::cl::cat(OnnxMlirOptions));
llvm::cl::opt<string> outputBaseName("o",
llvm::cl::desc("Base path for output files, extensions will be added."),
llvm::cl::value_desc("path"), llvm::cl::cat(OnnxMlirOptions),
llvm::cl::ValueRequired);
llvm::cl::opt<EmissionTargetType> emissionTarget( llvm::cl::opt<EmissionTargetType> emissionTarget(
llvm::cl::desc("Choose target to emit:"), llvm::cl::desc("Choose target to emit:"),
llvm::cl::values( llvm::cl::values(
@ -46,9 +50,9 @@ int main(int argc, char *argv[]) {
mlir::OwningModuleRef module; mlir::OwningModuleRef module;
processInputFile(inputFilename, emissionTarget, context, module); processInputFile(inputFilename, emissionTarget, context, module);
// Input file base name. // Input file base name, replace path if required.
string outputBaseName = if (outputBaseName == "")
inputFilename.substr(0, inputFilename.find_last_of(".")); outputBaseName = inputFilename.substr(0, inputFilename.find_last_of("."));
return compileModule(module, context, outputBaseName, emissionTarget); return compileModule(module, context, outputBaseName, emissionTarget);
} }