1 //===- mlir-cat.cpp ---------------------------------------------*- C++ -*-===//
3 // This file is licensed under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 #include "mlir/IR/MLIRContext.h"
10 #include "mlir/IR/Operation.h"
11 #include "mlir/IR/OwningOpRef.h"
12 #include "mlir/Parser/Parser.h"
13 #include "mlir/Support/FileUtilities.h"
14 #include "mlir/Support/LLVM.h"
15 #include "llvm/Support/SourceMgr.h"
16 #include "llvm/Support/ToolOutputFile.h"
17 #include "llvm/Support/raw_ostream.h"
24 /// This example parse its input, either from a file or its standard input (in
25 /// bytecode or textual assembly) and print it back.
27 int main(int argc
, char **argv
) {
28 // Set up the input file.
31 llvm::errs() << "Reading from stdin...\n";
36 std::string errorMessage
;
37 auto file
= openInputFile(inputFile
, &errorMessage
);
39 llvm::errs() << errorMessage
<< "\n";
42 llvm::SourceMgr sourceMgr
;
43 sourceMgr
.AddNewSourceBuffer(std::move(file
), SMLoc());
45 auto output
= openOutputFile("-", &errorMessage
);
47 llvm::errs() << errorMessage
<< "\n";
51 DialectRegistry registry
;
52 MLIRContext
context(registry
, MLIRContext::Threading::DISABLED
);
53 context
.allowUnregisteredDialects(true);
54 OwningOpRef
<Operation
*> op
= parseSourceFile(sourceMgr
, &context
);
56 llvm::errs() << "Failed to parse input file";
59 output
->os() << *(op
.get()) << "\n";