diff --git a/mlir/test/CMakeLists.txt b/mlir/test/CMakeLists.txt --- a/mlir/test/CMakeLists.txt +++ b/mlir/test/CMakeLists.txt @@ -3,6 +3,7 @@ add_subdirectory(mlir-cpu-runner) add_subdirectory(SDBM) add_subdirectory(lib) +add_subdirectory(OutOfTree/mlir-opt) llvm_canonicalize_cmake_booleans( LLVM_BUILD_EXAMPLES diff --git a/mlir/test/OutOfTree/mlir-opt/CMakeLists.txt b/mlir/test/OutOfTree/mlir-opt/CMakeLists.txt new file mode 100644 --- /dev/null +++ b/mlir/test/OutOfTree/mlir-opt/CMakeLists.txt @@ -0,0 +1,83 @@ +if(LLVM_BUILD_LLVM_DYLIB) +add_executable(mlir-opt-test + mlir-opt.cpp + ) +target_link_libraries(mlir-opt-test MLIR LLVM) +whole_archive_link(mlir-opt-test + MLIRSPIRVTestPasses + MLIRTestDialect + MLIRTestIR + MLIRTestPass + MLIRTestTransforms +) +llvm_update_compile_flags(mlir-opt-test) +endif() + +# set(LLVM_OPTIONAL_SOURCES +# null.cpp +# ) + +# set(LIB_LIBS +# MLIRAnalysis +# MLIRLLVMIR +# MLIROptMain +# MLIRParser +# MLIRPass +# MLIRTransforms +# MLIRSupport +# ) +# add_llvm_library(MLIRMlirOptLib BUILDTREE_ONLY +# mlir-opt.cpp +# LINK_LIBS ${LIB_LIBS} +# ) + +# set(LIBS +# MLIRAnalysis +# MLIRAffineOps +# MLIRAffineToStandard +# MLIRLoopsToGPU +# MLIRLinalgToLLVM + +# MLIRLoopToStandard +# MLIREDSC +# MLIRFxpMathOps +# MLIRGPU +# MLIRGPUtoNVVMTransforms +# MLIRGPUtoROCDLTransforms +# MLIRGPUtoSPIRVTransforms +# MLIRLinalg +# MLIRLLVMIR +# MLIRLoopOps +# MLIRNVVMIR +# MLIROptMain +# MLIRParser +# MLIRPass +# MLIRQuantizerTransforms +# MLIRQuantOps +# MLIRROCDLIR +# MLIRSPIRV +# MLIRStandardToSPIRVTransforms +# MLIRSPIRVTransforms +# MLIRStandardOps +# MLIRStandardToLLVM +# MLIRTransforms +# MLIRTestDialect +# MLIRTestIR +# MLIRTestPass +# MLIRTestTransforms +# MLIRSupport +# MLIRVectorOps +# MLIRVectorToLLVM +# MLIRVectorToLoops +# ) +# if(MLIR_CUDA_CONVERSIONS_ENABLED) +# list(APPEND LIBS +# MLIRGPUtoCUDATransforms +# ) +# endif() +# add_llvm_tool(mlir-opt +# mlir-opt.cpp +# ) +# llvm_update_compile_flags(mlir-opt) +# whole_archive_link(mlir-opt ${LIBS}) +# target_link_libraries(mlir-opt PRIVATE MLIRIR MLIROptMain MLIRMlirOptLib ${LIBS} LLVMSupport) diff --git a/mlir/test/OutOfTree/mlir-opt/mlir-opt.cpp b/mlir/test/OutOfTree/mlir-opt/mlir-opt.cpp new file mode 100644 --- /dev/null +++ b/mlir/test/OutOfTree/mlir-opt/mlir-opt.cpp @@ -0,0 +1,92 @@ +//===- mlir-opt.cpp - MLIR Optimizer Driver -------------------------------===// +// +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. +// See https://llvm.org/LICENSE.txt for license information. +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +// +//===----------------------------------------------------------------------===// +// +// Main entry function for mlir-opt for when built as standalone binary. +// +//===----------------------------------------------------------------------===// + +#include "mlir/Analysis/Passes.h" +#include "mlir/IR/Dialect.h" +#include "mlir/IR/MLIRContext.h" +#include "mlir/Pass/Pass.h" +#include "mlir/Pass/PassManager.h" +#include "mlir/Support/FileUtilities.h" +#include "mlir/Support/MlirOptMain.h" +#include "llvm/Support/CommandLine.h" +#include "llvm/Support/InitLLVM.h" +#include "llvm/Support/SourceMgr.h" +#include "llvm/Support/ToolOutputFile.h" + +using namespace llvm; +using namespace mlir; + +static cl::opt + inputFilename(cl::Positional, cl::desc(""), cl::init("-")); + +static cl::opt outputFilename("o", cl::desc("Output filename"), + cl::value_desc("filename"), + cl::init("-")); + +static cl::opt + splitInputFile("split-input-file", + cl::desc("Split the input file into pieces and process each " + "chunk independently"), + cl::init(false)); + +static cl::opt + verifyDiagnostics("verify-diagnostics", + cl::desc("Check that emitted diagnostics match " + "expected-* lines on the corresponding line"), + cl::init(false)); + +static cl::opt + verifyPasses("verify-each", + cl::desc("Run the verifier after each transformation pass"), + cl::init(true)); + +static cl::opt + showDialects("show-dialects", + cl::desc("Print the list of registered dialects"), + cl::init(false)); + +int main(int argc, char **argv) { + InitLLVM y(argc, argv); + + // Register any pass manager command line options. + registerPassManagerCLOptions(); + PassPipelineCLParser passPipeline("", "Compiler passes to run"); + + // Parse pass names in main to ensure static initialization completed. + cl::ParseCommandLineOptions(argc, argv, "MLIR modular optimizer driver\n"); + + MLIRContext context; + if(showDialects) { + llvm::outs() << "Registered Dialects:\n"; + for(Dialect *dialect : context.getRegisteredDialects()) { + llvm::outs() << dialect->getNamespace() << "\n"; + } + return 0; + } + + // Set up the input file. + std::string errorMessage; + auto file = openInputFile(inputFilename, &errorMessage); + if (!file) { + llvm::errs() << errorMessage << "\n"; + return 1; + } + + auto output = openOutputFile(outputFilename, &errorMessage); + if (!output) { + llvm::errs() << errorMessage << "\n"; + exit(1); + } + + return failed(MlirOptMain(output->os(), std::move(file), passPipeline, + splitInputFile, verifyDiagnostics, verifyPasses)); +}