Skip to content

Commit

Permalink
[mpact][tools] set up the mpact-opt tool
Browse files Browse the repository at this point in the history
  • Loading branch information
Peiming Liu committed May 23, 2024
1 parent 983a8d8 commit 13603e4
Show file tree
Hide file tree
Showing 7 changed files with 93 additions and 2 deletions.
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
mpact_venv/
*_venv/
__pycache__
/build/
/build*/
29 changes: 29 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,35 @@ message(STATUS "Building the MPACT compiler at ${MPACT_SOURCE_DIR} (into ${MPACT

set(MPACT_PYTHON_PACKAGES_DIR "${MPACT_BINARY_DIR}/python_packages")

#-------------------------------------------------------------------------------
# Configure out-of-tree vs in-tree build
#-------------------------------------------------------------------------------

if(CMAKE_SOURCE_DIR STREQUAL CMAKE_CURRENT_SOURCE_DIR)
message(STATUS "Torch-MLIR out-of-tree build.")
message(FATAL_ERROR "TODO")
else()
message(STATUS "Torch-MLIR in-tree build.")
# In-tree build with LLVM_EXTERNAL_PROJECTS=torch-mlir

option(MLIR_ENABLE_BINDINGS_PYTHON "Enables MLIR Python Bindings" OFF)

# TODO: Fix this upstream so that global include directories are not needed.
set(MLIR_MAIN_SRC_DIR ${LLVM_MAIN_SRC_DIR}/../mlir)
set(MLIR_INCLUDE_DIR ${LLVM_MAIN_SRC_DIR}/../mlir/include)
set(MLIR_GENERATED_INCLUDE_DIR ${LLVM_BINARY_DIR}/tools/mlir/include)
set(MLIR_INCLUDE_DIRS "${MLIR_INCLUDE_DIR};${MLIR_GENERATED_INCLUDE_DIR}")
endif()

include_directories(${LLVM_INCLUDE_DIRS})
include_directories(${MLIR_INCLUDE_DIRS})
include_directories(${CMAKE_CURRENT_SOURCE_DIR}/include)
include_directories(${CMAKE_CURRENT_BINARY_DIR}/include)

add_subdirectory(include)
add_subdirectory(lib)
add_subdirectory(tools)

add_subdirectory(benchmark)
add_subdirectory(python)
add_subdirectory(test)
Empty file added include/CMakeLists.txt
Empty file.
Empty file added lib/CMakeLists.txt
Empty file.
1 change: 1 addition & 0 deletions tools/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
add_subdirectory(mpact-opt)
15 changes: 15 additions & 0 deletions tools/mpact-opt/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
add_llvm_executable(mpact-opt mpact_opt.cpp)

set(dependency_libraries)
if(TORCH_MLIR_ENABLE_STABLEHLO)
list(APPEND dependency_libraries StablehloRegister)
endif()

target_link_libraries(mpact-opt PRIVATE
MLIROptLib
MLIRTransforms
TorchMLIRInitAll
TorchMLIRTorchDialect
TorchMLIRTorchPasses
${dependency_libraries}
)
46 changes: 46 additions & 0 deletions tools/mpact-opt/mpact_opt.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
//===- mpact-opt.cpp - MLIR Optimizer Driver -------------------------===//
//
// Part of the MPACT 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
// Also available under a BSD-style license. See LICENSE.
//
//===----------------------------------------------------------------------===//

#include "mlir/Dialect/SCF/IR/SCF.h"
#include "mlir/Dialect/Tensor/IR/Tensor.h"
#include "mlir/Tools/mlir-opt/MlirOptMain.h"
#include "mlir/Transforms/Passes.h"
#include "torch-mlir/InitAll.h"

#ifdef TORCH_MLIR_ENABLE_STABLEHLO
#include "stablehlo/dialect/Register.h"
#endif

using namespace mlir;

int main(int argc, char **argv) {
mlir::torch::registerAllPasses();

// Core Transforms
registerCanonicalizerPass();
registerCSEPass();
registerInlinerPass();
registerLocationSnapshotPass();
registerLoopInvariantCodeMotionPass();
registerPrintOpStatsPass();
registerViewOpGraphPass();
registerStripDebugInfoPass();
registerSymbolDCEPass();

DialectRegistry registry;
mlir::torch::registerAllDialects(registry);
mlir::torch::registerOptionalInputDialects(registry);

#ifdef TORCH_MLIR_ENABLE_STABLEHLO
mlir::stablehlo::registerAllDialects(registry);
#endif
return mlir::asMainReturnCode(mlir::MlirOptMain(
argc, argv, "MLIR modular optimizer driver\n", registry));
}

0 comments on commit 13603e4

Please sign in to comment.