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(
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;
mlir::OpPrintingFlags flags;
if (preserveLocations)
@ -473,18 +471,17 @@ void outputCode(
#ifdef _WIN32
// copy original stderr file number
int stderrOrigin = _dup(_fileno(stderr));
#else
int stderrOrigin = dup(fileno(stderr));
#endif
freopen(tempFilename.c_str(), "w", stderr);
module->print(llvm::errs(), flags);
fflush(stderr);
// set modified stderr as original stderr
#ifdef _WIN32
_dup2(stderrOrigin, _fileno(stderr));
#else
if (fork() == 0) {
freopen(tempFilename.c_str(), "w", stderr);
module->print(llvm::errs(), flags);
fclose(stderr);
exit(0);
}
dup2(stderrOrigin, fileno(stderr));
#endif
if (printIR)
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::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::desc("Choose target to emit:"),
llvm::cl::values(
@ -46,9 +50,9 @@ int main(int argc, char *argv[]) {
mlir::OwningModuleRef module;
processInputFile(inputFilename, emissionTarget, context, module);
// Input file base name.
string outputBaseName =
inputFilename.substr(0, inputFilename.find_last_of("."));
// Input file base name, replace path if required.
if (outputBaseName == "")
outputBaseName = inputFilename.substr(0, inputFilename.find_last_of("."));
return compileModule(module, context, outputBaseName, emissionTarget);
}