Skip to content

Commit

Permalink
[Accelerator] Remove static NNPA libs by resolving circular dependenc…
Browse files Browse the repository at this point in the history
…ies (llvm#1269)

* initial code

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* Do not use static libs

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* clean up

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* Remove more static libs

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* Minor changes

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* Address comments

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* just to trigger CI tests

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* Address comments about cmake

Signed-off-by: Tung D. Le <tung@jp.ibm.com>

* Move some options from NNPAAccelerator.cpp into NNPACompilerUtils.cpp

Signed-off-by: Tung D. Le <tung@jp.ibm.com>
  • Loading branch information
tungld authored Mar 28, 2022
1 parent ee22509 commit 8666fe2
Show file tree
Hide file tree
Showing 26 changed files with 668 additions and 455 deletions.
27 changes: 17 additions & 10 deletions src/Accelerators/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,21 @@ else (ACCELERATORS_TO_BUILD)
endif (ACCELERATORS_TO_BUILD)
file(APPEND ${CMAKE_CURRENT_BINARY_DIR}/InitAccelerators.cpp "}\n")

add_onnx_mlir_library(Accelerator STATIC
Accelerator.cpp
InitAccelerators.cpp
DEPENDS
MLIRIR
INCLUDE_DIRS PUBLIC
${ONNX_MLIR_SRC_ROOT}/include
LINK_LIBS PUBLIC
LLVMSupport
${ACCEL_LIST}
add_onnx_mlir_library(InitAccelerators
InitAccelerators.cpp

EXCLUDE_FROM_OM_LIBS

LINK_LIBS PUBLIC
${ACCEL_LIST}
)

add_onnx_mlir_library(Accelerator
Accelerator.cpp
DEPENDS
MLIRIR
INCLUDE_DIRS PUBLIC
${ONNX_MLIR_SRC_ROOT}/include
LINK_LIBS PUBLIC
LLVMSupport
)
6 changes: 3 additions & 3 deletions src/Accelerators/NNPA/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,11 @@ add_subdirectory(Support)
add_subdirectory(Transform)
add_subdirectory(Compiler)

add_onnx_mlir_library(NNPAAccel STATIC
add_onnx_mlir_library(NNPAAccel
NNPAAccelerator.cpp

EXCLUDE_FROM_OM_LIBS

INCLUDE_DIRS PUBLIC
${ONNX_MLIR_SRC_ROOT}/include
${ONNX_MLIR_SRC_ROOT}
Expand All @@ -40,7 +40,7 @@ add_onnx_mlir_library(NNPAAccel STATIC
LINK_LIBS PUBLIC
onnx
NNPACompilerUtils
# Accelerator
Accelerator
OMZHighOps
OMZLowOps
)
Expand Down
9 changes: 5 additions & 4 deletions src/Accelerators/NNPA/Compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
get_property(OMLibs GLOBAL PROPERTY ONNX_MLIR_LIBS)

add_onnx_mlir_library(NNPACompilerUtils STATIC
EXCLUDE_FROM_OM_LIBS
add_onnx_mlir_library(NNPACompilerUtils
NNPACompilerUtils.cpp

EXCLUDE_FROM_OM_LIBS

INCLUDE_DIRS PRIVATE
${NNPA_SRC_ROOT}
${NNPA_BIN_ROOT}
${NNPA_ONNX_MLIR_SRC_ROOT}
${NNPA_ONNX_MLIR_BIN_ROOT}

LINK_LIBS PUBLIC
CompilerUtils
#${OMLibs}
${OMLibs}
CompilerPasses

ACCEL_INCLUDE_DIRS PRIVATE
${NNPA_ONNX_MLIR_SRC_ROOT}
Expand Down
27 changes: 17 additions & 10 deletions src/Accelerators/NNPA/Compiler/NNPACompilerUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@

#include "mlir/Conversion/AffineToStandard/AffineToStandard.h"
#include "mlir/Conversion/ReconcileUnrealizedCasts/ReconcileUnrealizedCasts.h"
#include "mlir/Conversion/SCFToControlFlow/SCFToControlFlow.h"
#include "mlir/Conversion/VectorToSCF/VectorToSCF.h"
#include "mlir/Dialect/Bufferization/Transforms/Passes.h"
#include "mlir/Dialect/MemRef/Transforms/Passes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/Pass/PassManager.h"
#include "mlir/Pass/PassRegistry.h"
#include "mlir/Transforms/Passes.h"
#include "llvm/CodeGen/TargetPassConfig.h"
#include "llvm/IR/DataLayout.h"
#include "llvm/MC/TargetRegistry.h"
Expand All @@ -28,23 +34,28 @@
#include "src/Accelerators/NNPA/Dialect/ZLow/ZLowOps.hpp"
#include "src/Accelerators/NNPA/Pass/NNPAPasses.hpp"
#include "src/Accelerators/NNPA/Support/OMNNPAOptions.hpp"
#include "src/Compiler/CompilerUtils.hpp"
#include "src/Compiler/CompilerOptions.hpp"
#include "src/Compiler/CompilerPasses.hpp"
#include "src/Pass/Passes.hpp"

#define DEBUG_TYPE "NNPACompiler"
#define DEBUG_TYPE "NNPACompilerUtils"

using namespace std;
using namespace mlir;
using namespace onnx_mlir;

namespace onnx_mlir {
extern llvm::cl::OptionCategory OnnxMlirOptions;
extern llvm::cl::opt<onnx_mlir::NNPAEmissionTargetType> nnpaEmissionTarget;
extern llvm::cl::list<std::string> execNodesOnCpu;

llvm::cl::opt<NNPAEmissionTargetType> nnpaEmissionTarget(
llvm::cl::desc("[Optional] Choose Z-related target to emit "
llvm::cl::desc("[Optional] Choose NNPA-related target to emit "
"(once selected it will cancel the other targets):"),
llvm::cl::values(
clEnumVal(EmitZHighIR, "Lower model to ZHigh IR (ZHigh dialect)"),
clEnumVal(EmitZLowIR, "Lower model to ZLow IR (ZLow dialect)"),
clEnumVal(EmitZNONE, "Do not emit Z-related target (default)")),
clEnumVal(EmitZNONE, "Do not emit NNPA-related target (default)")),
llvm::cl::init(EmitZNONE), llvm::cl::cat(OnnxMlirOptions));

llvm::cl::list<std::string> execNodesOnCpu{"execNodesOnCpu",
Expand All @@ -55,8 +66,6 @@ llvm::cl::list<std::string> execNodesOnCpu{"execNodesOnCpu",
llvm::cl::CommaSeparated, llvm::cl::ZeroOrMore,
llvm::cl::cat(OnnxMlirOptions)};

namespace onnx_mlir {

void addONNXToZHighPasses(
mlir::PassManager &pm, ArrayRef<std::string> execNodesOnCpu) {
pm.addPass(onnx_mlir::createRewriteONNXForZHighPass(execNodesOnCpu));
Expand Down Expand Up @@ -117,10 +126,8 @@ void addAllToLLVMPasses(mlir::PassManager &pm) {
pm.addPass(mlir::createCanonicalizerPass());
}

void addPassesNNPA(mlir::OwningOpRef<ModuleOp> &module, mlir::PassManager &pm,
EmissionTargetType &emissionTarget,
NNPAEmissionTargetType nnpaEmissionTarget,
ArrayRef<std::string> execNodesOnCpu) {
void addPassesNNPA(mlir::OwningOpRef<mlir::ModuleOp> &module,
mlir::PassManager &pm, EmissionTargetType &emissionTarget) {
// TODO: Develop and use determineInputIRLevel for NNPA
// InputIRLevelType inputIRLevel = determineInputIRLevel(module);

Expand Down
16 changes: 4 additions & 12 deletions src/Accelerators/NNPA/Compiler/NNPACompilerUtils.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@

#pragma once

#include "mlir/IR/BuiltinOps.h"
#include "mlir/Pass/PassManager.h"
#include "src/Compiler/CompilerUtils.hpp"
#include "src/Support/OMOptions.hpp"
#include "mlir/Pass/PassRegistry.h"
#include "onnx-mlir/Compiler/OMCompilerTypes.h"

namespace onnx_mlir {

Expand All @@ -34,15 +35,6 @@ void addZHighToZLowPasses(mlir::PassManager &pm);
void addAllToLLVMPasses(mlir::PassManager &pm);

void addPassesNNPA(mlir::OwningOpRef<mlir::ModuleOp> &module,
mlir::PassManager &pm, onnx_mlir::EmissionTargetType &emissionTarget,
NNPAEmissionTargetType nnpaEmissionTarget,
mlir::ArrayRef<std::string> execNodesOnCpu);

int compileModuleNNPA(mlir::OwningOpRef<mlir::ModuleOp> &module,
mlir::MLIRContext &context, std::string outputBaseName,
onnx_mlir::EmissionTargetType emissionTarget,
NNPAEmissionTargetType dlcEmissionTarget =
NNPAEmissionTargetType::EmitZNONE,
mlir::ArrayRef<std::string> execNodesOnCpu = mlir::ArrayRef<std::string>());
mlir::PassManager &pm, onnx_mlir::EmissionTargetType &emissionTarget);

} // namespace onnx_mlir
20 changes: 11 additions & 9 deletions src/Accelerators/NNPA/NNPAAccelerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,28 @@
//
//===----------------------------------------------------------------------===//

#include "src/Accelerators/NNPA/NNPAAccelerator.hpp"
#include "mlir/Transforms/Passes.h"
#include "llvm/Support/Debug.h"

#include "src/Accelerators/NNPA/Compiler/NNPACompilerUtils.hpp"
#include "src/Accelerators/NNPA/Dialect/ZHigh/ZHighOps.hpp"
#include "src/Accelerators/NNPA/Dialect/ZLow/ZLowOps.hpp"
#include "src/Accelerators/NNPA/NNPAAccelerator.hpp"
#include "src/Accelerators/NNPA/Pass/NNPAPasses.hpp"
#include "src/Support/OMOptions.hpp"
#include "llvm/Support/Debug.h"
#include "src/Compiler/CompilerOptions.hpp"

#include <memory>

#define DEBUG_TYPE "NNPAAccelerator"

extern llvm::cl::OptionCategory OMNNPAPassOptions;
extern llvm::cl::opt<onnx_mlir::NNPAEmissionTargetType> nnpaEmissionTarget;
extern llvm::cl::list<std::string> execNodesOnCpu;
extern llvm::cl::list<onnx_mlir::accel::Accelerator::Kind> maccel;

onnx_mlir::accel::nnpa::NNPAAccelerator *pnnpa = nullptr;

void createNNPA() { pnnpa = new onnx_mlir::accel::nnpa::NNPAAccelerator; }

namespace onnx_mlir {
extern llvm::cl::list<onnx_mlir::accel::Accelerator::Kind> maccel;

namespace accel {
namespace nnpa {

Expand Down Expand Up @@ -63,11 +65,11 @@ void NNPAAccelerator::getOrLoadDialects(mlir::MLIRContext &context) const {
context.getOrLoadDialect<zlow::ZLowDialect>();
}

void NNPAAccelerator::addPasses(mlir::OwningOpRef<ModuleOp> &module,
void NNPAAccelerator::addPasses(mlir::OwningOpRef<mlir::ModuleOp> &module,
mlir::PassManager &pm,
onnx_mlir::EmissionTargetType &emissionTarget) const {
LLVM_DEBUG(llvm::dbgs() << "adding passes for NNPA accelerator\n");
addPassesNNPA(module, pm, emissionTarget, nnpaEmissionTarget, execNodesOnCpu);
addPassesNNPA(module, pm, emissionTarget);
}

void NNPAAccelerator::registerDialects(mlir::DialectRegistry &registry) const {
Expand Down
43 changes: 40 additions & 3 deletions src/Compiler/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,31 @@ add_custom_target(ExternalUtil DEPENDS ${FILE_GENERATE_DIR}/ExternalUtil.hpp)

get_property(OMLibs GLOBAL PROPERTY ONNX_MLIR_LIBS)

add_onnx_mlir_library(CompilerUtils STATIC
CompilerUtils.cpp
add_onnx_mlir_library(CompilerOptions
CompilerOptions.cpp

EXCLUDE_FROM_OM_LIBS

DEPENDS
ExternalUtil

INCLUDE_DIRS PRIVATE
${FILE_GENERATE_DIR}

INCLUDE_DIRS PUBLIC
${ONNX_MLIR_SRC_ROOT}/include

LINK_LIBS PUBLIC
MLIRIR
)

add_onnx_mlir_library(CompilerPasses
CompilerPasses.cpp

EXCLUDE_FROM_OM_LIBS

DEPENDS
MLIRIR
OMVersion

INCLUDE_DIRS PRIVATE
${FILE_GENERATE_DIR}
Expand All @@ -42,9 +58,30 @@ add_onnx_mlir_library(CompilerUtils STATIC

LINK_LIBS PUBLIC
${OMLibs}
CompilerOptions
MLIRAffineTransforms
MLIRLinalgTransforms
MLIRLLVMToLLVMIRTranslation
)

add_onnx_mlir_library(CompilerUtils
CompilerUtils.cpp

DEPENDS
ExternalUtil
MLIRIR
OMVersion

INCLUDE_DIRS PRIVATE
${FILE_GENERATE_DIR}

INCLUDE_DIRS PUBLIC
${ONNX_MLIR_SRC_ROOT}/include

LINK_LIBS PUBLIC
${OMLibs}
CompilerPasses
InitAccelerators

# Link LLVM libraries necessary to query which target architectures are configured.
LINK_COMPONENTS PRIVATE
Expand Down
Loading

0 comments on commit 8666fe2

Please sign in to comment.