mirror of https://github.com/llvm/torch-mlir
Add npcomp-opt and lit runner.
parent
d3b6e1767a
commit
953ef89a30
|
@ -13,7 +13,7 @@ if(POLICY CMP0077)
|
|||
cmake_policy(SET CMP0077 NEW)
|
||||
endif()
|
||||
|
||||
project(standalone-dialect LANGUAGES CXX C)
|
||||
project(npcomp LANGUAGES CXX C)
|
||||
|
||||
find_package(MLIR REQUIRED CONFIG)
|
||||
|
||||
|
@ -34,10 +34,6 @@ include_directories(${PROJECT_BINARY_DIR}/include)
|
|||
link_directories(${LLVM_BUILD_LIBRARY_DIR})
|
||||
add_definitions(${LLVM_DEFINITIONS})
|
||||
|
||||
# MLIR npcomp project.
|
||||
#set(MLIR_NPCOMP_MAIN_SRC_DIR ${CMAKE_CURRENT_SOURCE_DIR}) # --src-root
|
||||
#set(MLIR_NPCOMP_INCLUDE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/include) # --includedir
|
||||
|
||||
set(MLIR_NPCOMP_SOURCE_DIR ${CMAKE_CURRENT_SOURCE_DIR})
|
||||
set(MLIR_NPCOMP_BINARY_DIR ${CMAKE_CURRENT_BINARY_DIR})
|
||||
|
||||
|
@ -49,13 +45,8 @@ message(STATUS "Found ppython libraries: ${PYTHON_LIBRARIES}")
|
|||
find_package(pybind11 CONFIG REQUIRED)
|
||||
message(STATUS "Found pybind11 v${pybind11_VERSION}: ${pybind11_INCLUDE_DIRS}")
|
||||
|
||||
# TODO(laurenzo): What is the right way to get include directories for
|
||||
# cross project dependencies?
|
||||
#include_directories(${MLIR_NPCOMP_INCLUDE_DIR})
|
||||
#include_directories(${CMAKE_SOURCE_DIR}/../mlir/include)
|
||||
#include_directories(${CMAKE_BINARY_DIR}/tools/mlir/include)
|
||||
|
||||
add_subdirectory(include/npcomp)
|
||||
add_subdirectory(lib)
|
||||
add_subdirectory(python)
|
||||
|
||||
add_subdirectory(tools)
|
||||
add_subdirectory(test)
|
||||
|
|
|
@ -12,10 +12,9 @@
|
|||
|
||||
namespace mlir {
|
||||
namespace npcomp {
|
||||
namespace numpy {
|
||||
|
||||
namespace NUMPY {
|
||||
#define GET_OP_CLASSES
|
||||
#include "npcomp/Dialect/Numpy/NumpyOps.cpp.inc"
|
||||
|
||||
} // namespace numpy
|
||||
} // namespace NUMPY
|
||||
} // namespace npcomp
|
||||
} // namespace mlir
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
configure_lit_site_cfg(
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in
|
||||
${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py
|
||||
MAIN_CONFIG
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py
|
||||
)
|
||||
|
||||
set(NPCOMP_OPT_TEST_DEPENDS
|
||||
FileCheck count not
|
||||
npcomp-opt
|
||||
)
|
||||
|
||||
add_lit_testsuite(check-npcomp-opt "Running the npcomp-opt regression tests"
|
||||
${CMAKE_CURRENT_BINARY_DIR}
|
||||
DEPENDS ${NPCOMP_OPT_TEST_DEPENDS}
|
||||
)
|
||||
set_target_properties(check-npcomp-opt PROPERTIES FOLDER "Tests")
|
||||
|
||||
add_lit_testsuites(NPCOMP_OPT ${CMAKE_CURRENT_SOURCE_DIR} DEPENDS ${NPCOMP_OPT_TEST_DEPENDS})
|
|
@ -0,0 +1,9 @@
|
|||
// RUN: npcomp-opt %s | npcomp-opt | FileCheck %s
|
||||
|
||||
// CHECK-LABEL: func @foo()
|
||||
func @foo() -> i32 {
|
||||
%0 = constant 1 : i32
|
||||
// CHECK: %{{.*}} = numpy.foo %{{.*}} : i32
|
||||
%res = numpy.foo %0 : i32
|
||||
return %res : i32
|
||||
}
|
|
@ -0,0 +1,61 @@
|
|||
# -*- Python -*-
|
||||
|
||||
import os
|
||||
import platform
|
||||
import re
|
||||
import subprocess
|
||||
import tempfile
|
||||
|
||||
import lit.formats
|
||||
import lit.util
|
||||
|
||||
from lit.llvm import llvm_config
|
||||
from lit.llvm.subst import ToolSubst
|
||||
from lit.llvm.subst import FindTool
|
||||
|
||||
# Configuration file for the 'lit' test runner.
|
||||
|
||||
# name: The name of this test suite.
|
||||
config.name = 'NPCOMP_OPT'
|
||||
|
||||
config.test_format = lit.formats.ShTest(not llvm_config.use_lit_shell)
|
||||
|
||||
# suffixes: A list of file extensions to treat as test files.
|
||||
config.suffixes = ['.mlir']
|
||||
|
||||
# test_source_root: The root path where tests are located.
|
||||
config.test_source_root = os.path.dirname(__file__)
|
||||
|
||||
# test_exec_root: The root path where tests should be run.
|
||||
config.test_exec_root = os.path.join(config.npcomp_obj_root, 'test')
|
||||
|
||||
config.substitutions.append(('%PATH%', config.environment['PATH']))
|
||||
config.substitutions.append(('%shlibext', config.llvm_shlib_ext))
|
||||
|
||||
llvm_config.with_system_environment(
|
||||
['HOME', 'INCLUDE', 'LIB', 'TMP', 'TEMP'])
|
||||
|
||||
llvm_config.use_default_substitutions()
|
||||
|
||||
# excludes: A list of directories to exclude from the testsuite. The 'Inputs'
|
||||
# subdirectories contain auxiliary inputs for various tests in their parent
|
||||
# directories.
|
||||
config.excludes = ['Inputs', 'Examples', 'CMakeLists.txt', 'README.txt', 'LICENSE.txt']
|
||||
|
||||
# test_source_root: The root path where tests are located.
|
||||
config.test_source_root = os.path.dirname(__file__)
|
||||
|
||||
# test_exec_root: The root path where tests should be run.
|
||||
config.test_exec_root = os.path.join(config.npcomp_obj_root, 'test')
|
||||
config.npcomp_tools_dir = os.path.join(
|
||||
config.npcomp_obj_root, 'tools', 'npcomp-opt')
|
||||
|
||||
# Tweak the PATH to include the tools dir.
|
||||
llvm_config.with_environment('PATH', config.llvm_tools_dir, append_path=True)
|
||||
|
||||
tool_dirs = [config.npcomp_tools_dir, config.llvm_tools_dir]
|
||||
tools = [
|
||||
'npcomp-opt'
|
||||
]
|
||||
|
||||
llvm_config.add_tool_substitutions(tools, tool_dirs)
|
|
@ -0,0 +1,49 @@
|
|||
@LIT_SITE_CFG_IN_HEADER@
|
||||
|
||||
import sys
|
||||
|
||||
config.host_triple = "@LLVM_HOST_TRIPLE@"
|
||||
config.target_triple = "@TARGET_TRIPLE@"
|
||||
config.llvm_src_root = "@LLVM_SOURCE_DIR@"
|
||||
config.llvm_obj_root = "@LLVM_BINARY_DIR@"
|
||||
config.llvm_tools_dir = "@LLVM_TOOLS_DIR@"
|
||||
config.llvm_lib_dir = "@LLVM_LIBRARY_DIR@"
|
||||
config.llvm_shlib_dir = "@SHLIBDIR@"
|
||||
config.llvm_shlib_ext = "@SHLIBEXT@"
|
||||
config.llvm_exe_ext = "@EXEEXT@"
|
||||
config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@"
|
||||
config.python_executable = "@PYTHON_EXECUTABLE@"
|
||||
config.gold_executable = "@GOLD_EXECUTABLE@"
|
||||
config.ld64_executable = "@LD64_EXECUTABLE@"
|
||||
config.enable_shared = @ENABLE_SHARED@
|
||||
config.enable_assertions = @ENABLE_ASSERTIONS@
|
||||
config.targets_to_build = "@TARGETS_TO_BUILD@"
|
||||
config.native_target = "@LLVM_NATIVE_ARCH@"
|
||||
config.llvm_bindings = "@LLVM_BINDINGS@".split(' ')
|
||||
config.host_os = "@HOST_OS@"
|
||||
config.host_cc = "@HOST_CC@"
|
||||
config.host_cxx = "@HOST_CXX@"
|
||||
# Note: ldflags can contain double-quoted paths, so must use single quotes here.
|
||||
config.host_ldflags = '@HOST_LDFLAGS@'
|
||||
config.llvm_use_sanitizer = "@LLVM_USE_SANITIZER@"
|
||||
config.llvm_host_triple = '@LLVM_HOST_TRIPLE@'
|
||||
config.host_arch = "@HOST_ARCH@"
|
||||
config.npcomp_src_root = "@CMAKE_SOURCE_DIR@"
|
||||
config.npcomp_obj_root = "@CMAKE_BINARY_DIR@"
|
||||
|
||||
# Support substitution of the tools_dir with user parameters. This is
|
||||
# used when we can't determine the tool dir at configuration time.
|
||||
try:
|
||||
config.llvm_tools_dir = config.llvm_tools_dir % lit_config.params
|
||||
config.llvm_shlib_dir = config.llvm_shlib_dir % lit_config.params
|
||||
except KeyError:
|
||||
e = sys.exc_info()[1]
|
||||
key, = e.args
|
||||
lit_config.fatal("unable to find %r parameter, use '--param=%s=VALUE'" % (key,key))
|
||||
|
||||
|
||||
import lit.llvm
|
||||
lit.llvm.initialize(lit_config, config)
|
||||
|
||||
# Let the main config do the real work.
|
||||
lit_config.load_config(config, "@CMAKE_SOURCE_DIR@/test/lit.cfg.py")
|
|
@ -0,0 +1 @@
|
|||
add_subdirectory(npcomp-opt)
|
|
@ -5,6 +5,7 @@ set -e
|
|||
td="$(realpath $(dirname $0)/..)"
|
||||
build_dir="$td/build"
|
||||
install_mlir="$td/install-mlir"
|
||||
build_mlir="$td/build-mlir"
|
||||
|
||||
if ! [ -d "$install_mlir/include/mlir" ]; then
|
||||
echo "MLIR install path does not appear valid: $install_mlir"
|
||||
|
@ -26,5 +27,5 @@ cmake -GNinja \
|
|||
"-B$build_dir" \
|
||||
"-DPYTHON_EXECUTABLE=$python_exe" \
|
||||
"-DMLIR_DIR=$install_mlir/lib/cmake/mlir" \
|
||||
"-DLLVM_EXTERNAL_LIT=$build_dir/bin/llvm-lit" \
|
||||
"-DLLVM_EXTERNAL_LIT=$build_mlir/bin/llvm-lit" \
|
||||
"$@"
|
||||
|
|
|
@ -0,0 +1,12 @@
|
|||
get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS)
|
||||
get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS)
|
||||
set(LIBS
|
||||
${dialect_libs}
|
||||
${conversion_libs}
|
||||
MLIROptLib
|
||||
NPCOMPNumpyDialect
|
||||
)
|
||||
add_llvm_executable(npcomp-opt npcomp-opt.cpp)
|
||||
|
||||
llvm_update_compile_flags(npcomp-opt)
|
||||
target_link_libraries(npcomp-opt PRIVATE ${LIBS})
|
|
@ -0,0 +1,107 @@
|
|||
//===- npcomp-opt.cpp -------------------------------------------*- C++ -*-===//
|
||||
//
|
||||
// This file is licensed 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
|
||||
//
|
||||
//===----------------------------------------------------------------------===//
|
||||
|
||||
#include "mlir/IR/Dialect.h"
|
||||
#include "mlir/IR/MLIRContext.h"
|
||||
#include "mlir/InitAllDialects.h"
|
||||
#include "mlir/InitAllPasses.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"
|
||||
|
||||
#include "npcomp/Dialect/Numpy/NumpyDialect.h"
|
||||
|
||||
static llvm::cl::opt<std::string> inputFilename(llvm::cl::Positional,
|
||||
llvm::cl::desc("<input file>"),
|
||||
llvm::cl::init("-"));
|
||||
|
||||
static llvm::cl::opt<std::string>
|
||||
outputFilename("o", llvm::cl::desc("Output filename"),
|
||||
llvm::cl::value_desc("filename"), llvm::cl::init("-"));
|
||||
|
||||
static llvm::cl::opt<bool> splitInputFile(
|
||||
"split-input-file",
|
||||
llvm::cl::desc("Split the input file into pieces and process each "
|
||||
"chunk independently"),
|
||||
llvm::cl::init(false));
|
||||
|
||||
static llvm::cl::opt<bool> verifyDiagnostics(
|
||||
"verify-diagnostics",
|
||||
llvm::cl::desc("Check that emitted diagnostics match "
|
||||
"expected-* lines on the corresponding line"),
|
||||
llvm::cl::init(false));
|
||||
|
||||
static llvm::cl::opt<bool> verifyPasses(
|
||||
"verify-each",
|
||||
llvm::cl::desc("Run the verifier after each transformation pass"),
|
||||
llvm::cl::init(true));
|
||||
|
||||
static llvm::cl::opt<bool> allowUnregisteredDialects(
|
||||
"allow-unregistered-dialect",
|
||||
llvm::cl::desc("Allow operation with no registered dialects"),
|
||||
llvm::cl::init(false));
|
||||
|
||||
static llvm::cl::opt<bool>
|
||||
showDialects("show-dialects",
|
||||
llvm::cl::desc("Print the list of registered dialects"),
|
||||
llvm::cl::init(false));
|
||||
|
||||
int main(int argc, char **argv) {
|
||||
mlir::registerAllDialects();
|
||||
mlir::registerAllPasses();
|
||||
|
||||
mlir::registerDialect<mlir::npcomp::NUMPY::NumpyDialect>();
|
||||
// TODO: Register standalone passes here.
|
||||
|
||||
llvm::InitLLVM y(argc, argv);
|
||||
|
||||
// Register any pass manager command line options.
|
||||
mlir::registerPassManagerCLOptions();
|
||||
mlir::PassPipelineCLParser passPipeline("", "Compiler passes to run");
|
||||
|
||||
// Parse pass names in main to ensure static initialization completed.
|
||||
llvm::cl::ParseCommandLineOptions(argc, argv,
|
||||
"MLIR modular optimizer driver\n");
|
||||
|
||||
mlir::MLIRContext context;
|
||||
if (showDialects) {
|
||||
llvm::outs() << "Registered Dialects:\n";
|
||||
for (mlir::Dialect *dialect : context.getRegisteredDialects()) {
|
||||
llvm::outs() << dialect->getNamespace() << "\n";
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Set up the input file.
|
||||
std::string errorMessage;
|
||||
auto file = mlir::openInputFile(inputFilename, &errorMessage);
|
||||
if (!file) {
|
||||
llvm::errs() << errorMessage << "\n";
|
||||
return 1;
|
||||
}
|
||||
|
||||
auto output = mlir::openOutputFile(outputFilename, &errorMessage);
|
||||
if (!output) {
|
||||
llvm::errs() << errorMessage << "\n";
|
||||
exit(1);
|
||||
}
|
||||
|
||||
if (failed(MlirOptMain(output->os(), std::move(file), passPipeline,
|
||||
splitInputFile, verifyDiagnostics, verifyPasses,
|
||||
allowUnregisteredDialects))) {
|
||||
return 1;
|
||||
}
|
||||
// Keep the output file if the invocation of MlirOptMain was successful.
|
||||
output->keep();
|
||||
return 0;
|
||||
}
|
Loading…
Reference in New Issue