From 50c545539b6ccce38a1ece1f4f302fc160682f7c Mon Sep 17 00:00:00 2001 From: NiccoloN Date: Mon, 23 Mar 2026 16:39:14 +0100 Subject: [PATCH] clean up PIM CMake update README.md --- README.md | 10 +++- src/PIM/CMakeLists.txt | 58 ++++++++++++------- src/PIM/Common/CMakeLists.txt | 12 +--- src/PIM/Compiler/CMakeLists.txt | 31 ++++------ src/PIM/Compiler/PimCodeGen.cpp | 1 - src/PIM/Compiler/PimCodeGen.hpp | 3 +- src/PIM/Compiler/PimCompilerUtils.cpp | 1 - .../Conversion/ONNXToSpatial/CMakeLists.txt | 7 ++- .../SpatialToGraphviz/CMakeLists.txt | 7 ++- .../Conversion/SpatialToPim/CMakeLists.txt | 7 ++- src/PIM/Dialect/Pim/CMakeLists.txt | 4 +- .../Transforms/Bufferization/CMakeLists.txt | 6 +- src/PIM/Dialect/Spatial/CMakeLists.txt | 12 +++- .../SpatialBufferizableOpInterface.cpp | 52 ++++++++++++++++- src/PIM/Pass/CMakeLists.txt | 15 +++++ src/PIM/Pass/CountInstructionPass.cpp | 1 - src/PIM/Pass/MessagePass.cpp | 8 ++- 17 files changed, 166 insertions(+), 69 deletions(-) create mode 100644 src/PIM/Pass/CMakeLists.txt diff --git a/README.md b/README.md index e3a833a..3f9ac1b 100644 --- a/README.md +++ b/README.md @@ -29,7 +29,7 @@ Remember to set ```-DCMAKE_BUILD_TYPE=Debug``` for developing on Raptor Moreover, if compiling with build type debug, it is also suggested to use mold as linker (you will need to install it if you don't have it already) -to reduce memory usage during linking. You can use it with: +to reduce memory usage during linking. You can use it by setting the options: ``` -DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold" \ -DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=mold" @@ -38,8 +38,16 @@ to reduce memory usage during linking. You can use it with: ### Raptor Use the following commands to build Raptor. + Remember to set ```-DCMAKE_BUILD_TYPE=Debug``` for developing on Raptor. +Also in this case, it is suggested to use mold as linker to reduce link time and memory usage, +setting the options: +``` +-DCMAKE_EXE_LINKER_FLAGS="-fuse-ld=mold" \ +-DCMAKE_SHARED_LINKER_FLAGS="-fuse-ld=mold" +``` + ``` git submodule update --init --recursive diff --git a/src/PIM/CMakeLists.txt b/src/PIM/CMakeLists.txt index 1d310d3..3ed0853 100644 --- a/src/PIM/CMakeLists.txt +++ b/src/PIM/CMakeLists.txt @@ -10,37 +10,54 @@ set(PIM_INCLUDE_PATH ${CMAKE_INCLUDE_OUTPUT_DIRECTORY}) set(PIM_ONNX_MLIR_SRC_ROOT ${ONNX_MLIR_SRC_ROOT}) set(PIM_ONNX_MLIR_BIN_ROOT ${ONNX_MLIR_BIN_ROOT}) -add_subdirectory(Common) -add_subdirectory(Compiler) -add_subdirectory(Conversion) -add_subdirectory(Dialect) - -add_onnx_mlir_library(OMPIMAccel - PimAccelerator.cpp - Pass/CountInstructionPass.cpp - Pass/EmitPimJsonPass.cpp - Pass/MessagePass.cpp - Pass/PimConstantFolding/Common.cpp - Pass/PimConstantFolding/Patterns/ConstantPatterns.cpp - Pass/PimConstantFolding/PimConstantFoldingPass.cpp - Pass/PimConstantFolding/Patterns/SubviewPatterns.cpp - Pass/PimHostVerificationPass.cpp - - EXCLUDE_FROM_OM_LIBS - - INCLUDE_DIRS PUBLIC +set(PIM_PUBLIC_INCLUDE_DIRS ${ONNX_MLIR_SRC_ROOT}/include ${ONNX_MLIR_SRC_ROOT} ${PIM_ONNX_MLIR_SRC_ROOT} ${PIM_SRC_ROOT} ${PIM_BIN_ROOT} ${PIM_INCLUDE_PATH} +) + +set(PIM_COMPILER_INCLUDE_DIRS + ${PIM_SRC_ROOT} + ${PIM_BIN_ROOT} + ${PIM_ONNX_MLIR_SRC_ROOT} + ${PIM_ONNX_MLIR_BIN_ROOT} +) + +set(PIM_ACCEL_INCLUDE_DIRS + ${PIM_ONNX_MLIR_SRC_ROOT} + ${PIM_ONNX_MLIR_BIN_ROOT} +) + +set(PIM_GENERATED_INCLUDE_DIRS + ${PIM_INCLUDE_PATH} +) + +function(add_pim_library name) + add_onnx_mlir_library(${name} STATIC ${ARGN}) +endfunction() + +add_subdirectory(Dialect) +add_subdirectory(Common) +add_subdirectory(Pass) +add_subdirectory(Compiler) +add_subdirectory(Conversion) + +add_pim_library(OMPIMAccel + PimAccelerator.cpp + + EXCLUDE_FROM_OM_LIBS + + INCLUDE_DIRS PUBLIC + ${PIM_PUBLIC_INCLUDE_DIRS} LINK_LIBS PUBLIC onnx OMAccelerator OMPimCompilerUtils - OMCompilerUtils + OMPimPasses OMONNXOps SpatialOps PimOps @@ -48,5 +65,6 @@ add_onnx_mlir_library(OMPIMAccel OMSpatialToGraphviz OMSpatialToPim OMPimCommon + OMPimBufferization MLIRTensorInferTypeOpInterfaceImpl ) diff --git a/src/PIM/Common/CMakeLists.txt b/src/PIM/Common/CMakeLists.txt index 57c79f2..7c370d2 100644 --- a/src/PIM/Common/CMakeLists.txt +++ b/src/PIM/Common/CMakeLists.txt @@ -1,19 +1,13 @@ -add_onnx_mlir_library(OMPimCommon +add_pim_library(OMPimCommon PimCommon.cpp EXCLUDE_FROM_OM_LIBS INCLUDE_DIRS PUBLIC - ${ONNX_MLIR_SRC_ROOT}/include - ${ONNX_MLIR_SRC_ROOT} - ${PIM_ONNX_MLIR_SRC_ROOT} - ${PIM_SRC_ROOT} - ${PIM_BIN_ROOT} - ${PIM_INCLUDE_PATH} + ${PIM_PUBLIC_INCLUDE_DIRS} LINK_LIBS PUBLIC onnx - OMPimCompilerUtils SpatialOps PimOps -) \ No newline at end of file +) diff --git a/src/PIM/Compiler/CMakeLists.txt b/src/PIM/Compiler/CMakeLists.txt index fe4c290..bd50dbe 100644 --- a/src/PIM/Compiler/CMakeLists.txt +++ b/src/PIM/Compiler/CMakeLists.txt @@ -1,44 +1,37 @@ -get_property(OMLibs GLOBAL PROPERTY ONNX_MLIR_LIBS) - -add_onnx_mlir_library(OMPimCompilerOptions +add_pim_library(OMPimCompilerOptions PimCompilerOptions.cpp EXCLUDE_FROM_OM_LIBS INCLUDE_DIRS PRIVATE - ${PIM_SRC_ROOT} - ${PIM_BIN_ROOT} - ${PIM_ONNX_MLIR_SRC_ROOT} - ${PIM_ONNX_MLIR_BIN_ROOT} + ${PIM_COMPILER_INCLUDE_DIRS} LINK_LIBS PUBLIC - ${OMLibs} OMCompilerOptions ACCEL_INCLUDE_DIRS PRIVATE - ${PIM_ONNX_MLIR_SRC_ROOT} - ${PIM_ONNX_MLIR_BIN_ROOT} + ${PIM_ACCEL_INCLUDE_DIRS} ) -add_onnx_mlir_library(OMPimCompilerUtils +add_pim_library(OMPimCompilerUtils PimCompilerUtils.cpp PimCodeGen.cpp + ../Pass/EmitPimJsonPass.cpp EXCLUDE_FROM_OM_LIBS INCLUDE_DIRS PRIVATE - ${PIM_SRC_ROOT} - ${PIM_BIN_ROOT} - ${PIM_ONNX_MLIR_SRC_ROOT} - ${PIM_ONNX_MLIR_BIN_ROOT} + ${PIM_COMPILER_INCLUDE_DIRS} LINK_LIBS PUBLIC - ${OMLibs} - OMCompilerUtils OMPimCompilerOptions + OMPimCommon + OMPimBufferization + OMPimPasses + OMONNXToSpatial + OMSpatialToPim OMCompilerPasses ACCEL_INCLUDE_DIRS PRIVATE - ${PIM_ONNX_MLIR_SRC_ROOT} - ${PIM_ONNX_MLIR_BIN_ROOT} + ${PIM_ACCEL_INCLUDE_DIRS} ) diff --git a/src/PIM/Compiler/PimCodeGen.cpp b/src/PIM/Compiler/PimCodeGen.cpp index c50f897..50c539f 100644 --- a/src/PIM/Compiler/PimCodeGen.cpp +++ b/src/PIM/Compiler/PimCodeGen.cpp @@ -19,7 +19,6 @@ #include "src/Accelerators/PIM/Compiler/PimCompilerOptions.hpp" #include "src/Accelerators/PIM/Dialect/Pim/PimOps.hpp" #include "src/Compiler/CompilerPasses.hpp" -#include "src/Compiler/CompilerUtils.hpp" using namespace llvm; using namespace mlir; diff --git a/src/PIM/Compiler/PimCodeGen.hpp b/src/PIM/Compiler/PimCodeGen.hpp index 7d520b1..df5b170 100644 --- a/src/PIM/Compiler/PimCodeGen.hpp +++ b/src/PIM/Compiler/PimCodeGen.hpp @@ -3,8 +3,9 @@ #include "llvm-project/clang/include/clang/Basic/LLVM.h" #include "llvm/Support/JSON.h" +#include "onnx-mlir/Compiler/OMCompilerTypes.h" + #include "Common/ValueMap.hpp" -#include "src/Accelerators/PIM/Compiler/PimCompilerUtils.hpp" #include "src/Accelerators/PIM/Dialect/Pim/PimOps.hpp" namespace onnx_mlir { diff --git a/src/PIM/Compiler/PimCompilerUtils.cpp b/src/PIM/Compiler/PimCompilerUtils.cpp index c12f3ce..b346454 100644 --- a/src/PIM/Compiler/PimCompilerUtils.cpp +++ b/src/PIM/Compiler/PimCompilerUtils.cpp @@ -5,7 +5,6 @@ #include "src/Accelerators/PIM/Dialect/Pim/PimOps.hpp" #include "src/Accelerators/PIM/Pass/PimPasses.hpp" #include "src/Compiler/CompilerPasses.hpp" -#include "src/Compiler/CompilerUtils.hpp" #define DEBUG_TYPE "PimCompilerUtils" diff --git a/src/PIM/Conversion/ONNXToSpatial/CMakeLists.txt b/src/PIM/Conversion/ONNXToSpatial/CMakeLists.txt index a77cde2..ce76d78 100644 --- a/src/PIM/Conversion/ONNXToSpatial/CMakeLists.txt +++ b/src/PIM/Conversion/ONNXToSpatial/CMakeLists.txt @@ -2,7 +2,7 @@ set(LLVM_TARGET_DEFINITIONS ONNXToSpatial.td) mlir_tablegen(ONNXToSpatial.hpp.inc -gen-rewriters "-I${ONNX_MLIR_SRC_ROOT}") add_public_tablegen_target(ONNXToSpatialIncGen) -add_onnx_mlir_library(OMONNXToSpatial +add_pim_library(OMONNXToSpatial Patterns/Math/Gemm.cpp Patterns/Math/Conv.cpp Patterns/Math/MatMul.cpp @@ -17,10 +17,13 @@ add_onnx_mlir_library(OMONNXToSpatial ONNXToSpatialPass.cpp Common.cpp + EXCLUDE_FROM_OM_LIBS + DEPENDS ONNXToSpatialIncGen LINK_LIBS PUBLIC + MLIRTosaDialect OMCompilerOptions OMPimCompilerOptions OMONNXOps @@ -28,5 +31,5 @@ add_onnx_mlir_library(OMONNXToSpatial OMPimCommon ACCEL_INCLUDE_DIRS PRIVATE - ${PIM_INCLUDE_PATH} + ${PIM_GENERATED_INCLUDE_DIRS} ) diff --git a/src/PIM/Conversion/SpatialToGraphviz/CMakeLists.txt b/src/PIM/Conversion/SpatialToGraphviz/CMakeLists.txt index cf79a3c..dea0370 100644 --- a/src/PIM/Conversion/SpatialToGraphviz/CMakeLists.txt +++ b/src/PIM/Conversion/SpatialToGraphviz/CMakeLists.txt @@ -1,14 +1,17 @@ add_onnx_mlir_rewriter(SpatialToGraphviz) -add_onnx_mlir_library(OMSpatialToGraphviz +add_pim_library(OMSpatialToGraphviz SpatialToGraphviz.cpp + EXCLUDE_FROM_OM_LIBS + LINK_LIBS PUBLIC + MLIRTosaDialect OMCompilerOptions OMPimCommon OMONNXOps SpatialOps ACCEL_INCLUDE_DIRS PRIVATE - ${PIM_INCLUDE_PATH} + ${PIM_GENERATED_INCLUDE_DIRS} ) diff --git a/src/PIM/Conversion/SpatialToPim/CMakeLists.txt b/src/PIM/Conversion/SpatialToPim/CMakeLists.txt index 171109b..166110a 100644 --- a/src/PIM/Conversion/SpatialToPim/CMakeLists.txt +++ b/src/PIM/Conversion/SpatialToPim/CMakeLists.txt @@ -2,19 +2,22 @@ set(LLVM_TARGET_DEFINITIONS SpatialToPim.td) mlir_tablegen(SpatialToPim.hpp.inc -gen-rewriters "-I${ONNX_MLIR_SRC_ROOT}") add_public_tablegen_target(SpatialToPimIncGen) -add_onnx_mlir_library(OMSpatialToPim +add_pim_library(OMSpatialToPim SpatialToPimPass.cpp Common.cpp + EXCLUDE_FROM_OM_LIBS + DEPENDS SpatialToPimIncGen LINK_LIBS PUBLIC + MLIRTosaDialect OMCompilerOptions OMPimCommon SpatialOps PimOps ACCEL_INCLUDE_DIRS PRIVATE - ${PIM_INCLUDE_PATH} + ${PIM_GENERATED_INCLUDE_DIRS} ) diff --git a/src/PIM/Dialect/Pim/CMakeLists.txt b/src/PIM/Dialect/Pim/CMakeLists.txt index e46ed57..80373ca 100644 --- a/src/PIM/Dialect/Pim/CMakeLists.txt +++ b/src/PIM/Dialect/Pim/CMakeLists.txt @@ -3,10 +3,12 @@ add_onnx_mlir_dialect_doc(pim Pim.td) add_subdirectory(Transforms/Bufferization) -add_onnx_mlir_library(PimOps +add_pim_library(PimOps PimOps.hpp PimOps.cpp + EXCLUDE_FROM_OM_LIBS + DEPENDS OMPimIncGen diff --git a/src/PIM/Dialect/Pim/Transforms/Bufferization/CMakeLists.txt b/src/PIM/Dialect/Pim/Transforms/Bufferization/CMakeLists.txt index 277306f..cb74785 100644 --- a/src/PIM/Dialect/Pim/Transforms/Bufferization/CMakeLists.txt +++ b/src/PIM/Dialect/Pim/Transforms/Bufferization/CMakeLists.txt @@ -2,13 +2,15 @@ set(LLVM_TARGET_DEFINITIONS PimBufferization.td) mlir_tablegen(PimBufferization.hpp.inc -gen-rewriters "-I${ONNX_MLIR_SRC_ROOT}") add_public_tablegen_target(PimBufferizationIncGen) -add_onnx_mlir_library(OMPimBufferization +add_pim_library(OMPimBufferization PimBufferizationPass.cpp OpBufferizationInterfaces.hpp OpBufferizationInterfaces.cpp Common.hpp Common.cpp + EXCLUDE_FROM_OM_LIBS + DEPENDS PimBufferizationIncGen @@ -17,5 +19,5 @@ add_onnx_mlir_library(OMPimBufferization PimOps ACCEL_INCLUDE_DIRS PRIVATE - ${PIM_INCLUDE_PATH} + ${PIM_GENERATED_INCLUDE_DIRS} ) diff --git a/src/PIM/Dialect/Spatial/CMakeLists.txt b/src/PIM/Dialect/Spatial/CMakeLists.txt index 21266c4..ba07c16 100644 --- a/src/PIM/Dialect/Spatial/CMakeLists.txt +++ b/src/PIM/Dialect/Spatial/CMakeLists.txt @@ -1,16 +1,22 @@ add_onnx_mlir_dialect(Spatial spat) add_onnx_mlir_dialect_doc(spat Spatial.td) - -add_onnx_mlir_library(SpatialOps +add_pim_library(SpatialOps SpatialOps.cpp Transforms/SpatialBufferizableOpInterface.cpp + EXCLUDE_FROM_OM_LIBS + DEPENDS OMONNXIncGen OMSpatialIncGen - + LINK_LIBS PUBLIC MLIRIR + MLIRBufferizationDialect + MLIRBufferizationTransforms OMMlirDialects + OMONNXOps + OMPimCompilerOptions + PimOps ) diff --git a/src/PIM/Dialect/Spatial/Transforms/SpatialBufferizableOpInterface.cpp b/src/PIM/Dialect/Spatial/Transforms/SpatialBufferizableOpInterface.cpp index cc56c90..51cdc10 100644 --- a/src/PIM/Dialect/Spatial/Transforms/SpatialBufferizableOpInterface.cpp +++ b/src/PIM/Dialect/Spatial/Transforms/SpatialBufferizableOpInterface.cpp @@ -17,7 +17,6 @@ #include -#include "src/Accelerators/PIM/Common/PimCommon.hpp" #include "src/Accelerators/PIM/Dialect/Pim/PimOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/Transforms/SpatialBufferizableOpInterface.hpp" @@ -39,6 +38,55 @@ memref::AllocOp createEmptyFromType(Type resultType, Location loc, RewriterBase& const llvm::StringRef PRECOMPUTED_OTHER_CORE_ID_ATTR_NAME("precomp_other_core_id"); +static FailureOr getOtherEndOfChannel(Operation* op, bool opIsReceive) { + auto channelNewOp = op->getOperand(0).getDefiningOp(); + if (!channelNewOp) { + op->emitError("User of Channel must have the first operand created by ChannelNewOp."); + return failure(); + } + + auto channelUsers = channelNewOp->getUsers(); + auto usersIterator = channelUsers.begin(); + auto firstUser = *usersIterator; + ++usersIterator; + if (usersIterator == channelUsers.end()) { + op->emitError("Operand generated by ChannelNewOp must have two users, only one found."); + channelNewOp->dump(); + op->dump(); + channelNewOp->getParentOp()->dump(); + return failure(); + } + + auto secondUser = *usersIterator; + ++usersIterator; + if (usersIterator != channelUsers.end()) { + op->emitError("Operand generated by ChannelNewOp must have two users, more than two found."); + return failure(); + } + + Operation* otherUser = nullptr; + if (firstUser == op) + otherUser = secondUser; + else if (secondUser == op) + otherUser = firstUser; + else { + op->emitError("Operand generated by ChannelNewOp must have two users and one of them must be the current op."); + return failure(); + } + + if (opIsReceive && !isa(otherUser)) { + op->emitError("Operand generated by ChannelNewOp has two users, but the other one is not a ChannelSendOp."); + return failure(); + } + + if (!opIsReceive && !isa(otherUser)) { + op->emitError("Operand generated by ChannelNewOp has two users, but the other one is not a ChannelReceiveOp."); + return failure(); + } + + return otherUser; +} + llvm::FailureOr getCoreIdOfOtherEndOfChannel(Operation* op, bool opIsReceive, RewriterBase& rewriter) { // This function requires the existence of ChannelNewOp and the other @@ -49,7 +97,7 @@ llvm::FailureOr getCoreIdOfOtherEndOfChannel(Operation* op, bool opIsR if (precomputedOtherCoreId) return cast(precomputedOtherCoreId).getInt(); - auto notOpUserOpt = getOtherEndOfChannel(op, opIsReceive, rewriter); + auto notOpUserOpt = getOtherEndOfChannel(op, opIsReceive); if (failed(notOpUserOpt)) return failure(); Operation* notOpUser = *notOpUserOpt; diff --git a/src/PIM/Pass/CMakeLists.txt b/src/PIM/Pass/CMakeLists.txt new file mode 100644 index 0000000..48c66ec --- /dev/null +++ b/src/PIM/Pass/CMakeLists.txt @@ -0,0 +1,15 @@ +add_pim_library(OMPimPasses + CountInstructionPass.cpp + MessagePass.cpp + PimConstantFolding/Common.cpp + PimConstantFolding/Patterns/ConstantPatterns.cpp + PimConstantFolding/PimConstantFoldingPass.cpp + PimConstantFolding/Patterns/SubviewPatterns.cpp + PimHostVerificationPass.cpp + + EXCLUDE_FROM_OM_LIBS + + LINK_LIBS PUBLIC + MLIRLinalgDialect + OMPimCommon +) diff --git a/src/PIM/Pass/CountInstructionPass.cpp b/src/PIM/Pass/CountInstructionPass.cpp index 1ae5ef3..6d0d35b 100644 --- a/src/PIM/Pass/CountInstructionPass.cpp +++ b/src/PIM/Pass/CountInstructionPass.cpp @@ -3,7 +3,6 @@ #include "src/Accelerators/PIM/Dialect/Pim/PimOps.hpp" #include "src/Accelerators/PIM/Dialect/Spatial/SpatialOps.hpp" -#include "src/Compiler/CompilerUtils.hpp" using namespace mlir; diff --git a/src/PIM/Pass/MessagePass.cpp b/src/PIM/Pass/MessagePass.cpp index 0ac5c29..01224fd 100644 --- a/src/PIM/Pass/MessagePass.cpp +++ b/src/PIM/Pass/MessagePass.cpp @@ -1,6 +1,7 @@ +#include "mlir/IR/BuiltinOps.h" #include "mlir/Pass/Pass.h" -#include "src/Compiler/CompilerUtils.hpp" +#include "llvm/Support/raw_ostream.h" using namespace mlir; @@ -17,7 +18,10 @@ struct MessagePass : PassWrapper> { : message(message) {} MessagePass(const MessagePass& pass) {} - void runOnOperation() final { showCompilePhase(message); } + void runOnOperation() final { + llvm::outs() << message << "\n"; + llvm::outs().flush(); + } private: std::string message;