#include "mlir/Rewrite/FrozenRewritePatternSet.h"
#include "mlir/Support/FileUtilities.h"
#include "mlir/Support/LogicalResult.h"
+#include "mlir/Tools/ParseUtilties.h"
#include "llvm/Support/InitLLVM.h"
+#include "llvm/Support/SourceMgr.h"
#include "llvm/Support/ToolOutputFile.h"
using namespace mlir;
-// Parse and verify the input MLIR file.
-static LogicalResult loadModule(MLIRContext &context,
- OwningOpRef<ModuleOp> &module,
- StringRef inputFilename) {
- module = parseSourceFile<ModuleOp>(inputFilename, &context);
- if (!module)
- return failure();
+// Parse and verify the input MLIR file. Returns null on error.
+OwningOpRef<Operation *> loadModule(MLIRContext &context,
+ StringRef inputFilename,
+ bool insertImplictModule) {
+ // Set up the input file.
+ std::string errorMessage;
+ auto file = openInputFile(inputFilename, &errorMessage);
+ if (!file) {
+ llvm::errs() << errorMessage << "\n";
+ return nullptr;
+ }
- return success();
+ llvm::SourceMgr sourceMgr;
+ sourceMgr.AddNewSourceBuffer(std::move(file), SMLoc());
+ return parseSourceFileForTool(sourceMgr, &context, insertImplictModule);
}
LogicalResult mlir::mlirReduceMain(int argc, char **argv,
"o", llvm::cl::desc("Output filename for the reduced test case"),
llvm::cl::init("-"), llvm::cl::cat(mlirReduceCategory));
+ static llvm::cl::opt<bool> noImplicitModule{
+ "no-implicit-module",
+ llvm::cl::desc(
+ "Disable implicit addition of a top-level module op during parsing"),
+ llvm::cl::init(false)};
+
llvm::cl::HideUnrelatedOptions(mlirReduceCategory);
llvm::InitLLVM y(argc, argv);
if (!output)
return failure();
- OwningOpRef<ModuleOp> moduleRef;
- if (failed(loadModule(context, moduleRef, inputFilename)))
+ OwningOpRef<Operation *> opRef =
+ loadModule(context, inputFilename, !noImplicitModule);
+ if (!opRef)
return failure();
auto errorHandler = [&](const Twine &msg) {
};
// Reduction pass pipeline.
- PassManager pm(&context);
+ PassManager pm(&context, opRef.get()->getName().getStringRef());
if (failed(parser.addToPipeline(pm, errorHandler)))
return failure();
- OwningOpRef<ModuleOp> m = moduleRef.get().clone();
+ OwningOpRef<Operation *> op = opRef.get()->clone();
- if (failed(pm.run(m.get())))
+ if (failed(pm.run(op.get())))
return failure();
- m->print(output->os());
+ op.get()->print(output->os());
output->keep();
return success();