├── ops ├── cpu │ ├── ops.h │ └── CMakeLists.txt └── CMakeLists.txt ├── .clang-format ├── include └── llcompiler │ ├── Dialect │ ├── LLH │ │ ├── IR │ │ │ ├── LLHUtility.h │ │ │ ├── CMakeLists.txt │ │ │ ├── LLHAttrs.h │ │ │ └── LLHEnums.h │ │ ├── CMakeLists.txt │ │ ├── SymbolInfer │ │ │ ├── CMakeLists.txt │ │ │ └── Utils │ │ │ │ └── InferSymbol.h │ │ ├── Transforms │ │ │ ├── CMakeLists.txt │ │ │ └── BufferizableOpInterfaceImpl.h │ │ └── Utils │ │ │ └── Broadcast.h │ ├── IRExtension │ │ ├── CMakeLists.txt │ │ └── IR │ │ │ ├── Ops.td │ │ │ ├── Constraints.td │ │ │ ├── CMakeLists.txt │ │ │ ├── Types.td │ │ │ ├── Ops.h │ │ │ ├── Types.h │ │ │ ├── Dialect.h │ │ │ ├── Attrs.h │ │ │ ├── Enums.h │ │ │ └── Dialect.td │ ├── LLVMExtension │ │ ├── CMakeLists.txt │ │ └── Transforms │ │ │ ├── CMakeLists.txt │ │ │ ├── Passes.td │ │ │ └── Passes.h │ ├── IndexExtension │ │ ├── CMakeLists.txt │ │ └── Transforms │ │ │ ├── CMakeLists.txt │ │ │ ├── Passes.td │ │ │ └── Passes.h │ ├── BufferizationExtension │ │ ├── CMakeLists.txt │ │ └── Transforms │ │ │ ├── CMakeLists.txt │ │ │ ├── Passes.td │ │ │ └── Passes.h │ ├── TosaExtension │ │ ├── CMakeLists.txt │ │ ├── Transforms │ │ │ ├── CMakeLists.txt │ │ │ ├── Passes.td │ │ │ └── Passes.h │ │ └── IR │ │ │ ├── TosaExEnums.td │ │ │ ├── TosaExInterfaces.td │ │ │ ├── TosaExConstraints.td │ │ │ ├── TosaExTypes.td │ │ │ ├── CMakeLists.txt │ │ │ ├── TosaExOps.td │ │ │ ├── TosaExDialect.h │ │ │ ├── TosaExOps.h │ │ │ └── TosaExTypes.h │ ├── CMakeLists.txt │ └── Utility │ │ ├── Tool.h │ │ ├── TensorPred.td │ │ ├── Benefit.h │ │ └── File.h │ ├── CMakeLists.txt │ ├── Support │ ├── CMakeLists.txt │ ├── Enums.h │ └── Enums_old.h │ ├── Compiler │ ├── CMakeLists.txt │ ├── ToolPath.h.in │ └── ToolPath.install.h.in │ ├── Conversion │ ├── CMakeLists.txt │ ├── LLHToHLO │ │ ├── LLHToHLO.h │ │ └── LLHPreprocessingForHLO.h │ ├── LLHToMath │ │ └── LLHToMath.h │ ├── LLHToTosa │ │ └── LLHToTosa.h │ ├── LLHToFunc │ │ └── LLHToFunc.h │ ├── LLHToShape │ │ └── LLHToShape.h │ ├── LLHToArith │ │ └── LLHToArith.h │ ├── LLHToLinalg │ │ └── LLHToLinalg.h │ ├── LLHToTensor │ │ └── LLHToTensor.h │ ├── TosaToLinalgExtension │ │ └── TosaToLinalgExtension.h │ └── StablehlotoLinalgExtension │ │ └── StablehlotoLinalgExtension.h │ ├── TransformLibrary │ ├── scf_include.mlir │ ├── CMakeLists.txt │ ├── tensor_include.mlir │ ├── LibraryPath.h.in │ └── LibraryPath.install.h.in │ ├── Interfaces │ ├── CMakeLists.txt │ ├── SymbolShapeOpInterfaces.h │ ├── BraodcastableOpInterfaces.h │ ├── BraodcastableOpInterfaces.td │ └── SymbolShapeOpInterfaces.td │ └── Frontend │ ├── Core │ ├── Macro.h │ ├── Utility.h │ ├── Base.h │ ├── Option.h │ └── Builder.h │ └── Onnx │ └── OnnxBuilder.h ├── doc ├── 记录 │ ├── Ai编译器开发日记-使用LLVM回归测试 -- to do.md │ └── Ai编译器开发日记-LLVM IR.md └── 其他 │ ├── 符号计算库-symengine-todo.md │ └── 异构混训要点速记.md ├── llcompiler ├── dialect │ └── __init__.py ├── __init__.py ├── utility │ ├── __init__.py │ └── utility.py ├── importer │ ├── onnx_op_translate │ │ └── __init__.py │ ├── __init__.py │ └── fx_op_translate │ │ ├── drop.py │ │ ├── layer_norm.py │ │ ├── dim.py │ │ ├── __init__.py │ │ ├── adaptive_avgpool.py │ │ └── convert_to.py ├── test_models │ ├── __init__.py │ ├── models │ │ ├── __init__.py │ │ └── resnet.py │ └── ops │ │ ├── matmul.py │ │ ├── where.py │ │ ├── unsqueeze.py │ │ ├── pooling.py │ │ ├── elewise_fusion.py │ │ ├── reduce_fusion.py │ │ ├── braodcast.py │ │ ├── slice.py │ │ ├── conv.py │ │ ├── __init__.py │ │ ├── linear.py │ │ ├── decompose.py │ │ ├── reduce.py │ │ └── batch_norm.py ├── compiler │ └── __init__.py └── executor │ └── __init__.py ├── src ├── Dialect │ ├── IRExtension │ │ ├── CMakeLists.txt │ │ └── IR │ │ │ ├── CMakeLists.txt │ │ │ ├── Enums.cpp │ │ │ ├── Ops.cpp │ │ │ ├── Types.cpp │ │ │ └── Dialect.cpp │ ├── LLVMExtension │ │ ├── CMakeLists.txt │ │ └── Transforms │ │ │ └── CMakeLists.txt │ ├── IndexExtension │ │ ├── CMakeLists.txt │ │ └── Transforms │ │ │ └── CMakeLists.txt │ ├── BufferizationExtension │ │ ├── CMakeLists.txt │ │ └── Transforms │ │ │ └── CMakeLists.txt │ ├── TosaExtension │ │ ├── CMakeLists.txt │ │ ├── Transforms │ │ │ └── CMakeLists.txt │ │ └── IR │ │ │ ├── CMakeLists.txt │ │ │ └── TosaExOps.cpp │ ├── LLH │ │ ├── CMakeLists.txt │ │ ├── Utils │ │ │ ├── CMakeLists.txt │ │ │ └── Broadcast.cpp │ │ ├── SymbolInfer │ │ │ └── CMakeLists.txt │ │ ├── Transforms │ │ │ ├── Aot.td │ │ │ └── CMakeLists.txt │ │ └── IR │ │ │ ├── CMakeLists.txt │ │ │ ├── LLHEnums.cpp │ │ │ ├── LLHCast.cpp │ │ │ └── LLHOps.cpp │ ├── CMakeLists.txt │ └── Utility │ │ ├── CMakeLists.txt │ │ ├── Benefit.cpp │ │ ├── RewritePattern.cpp │ │ └── Tool.cpp ├── Frontend │ ├── CMakeLists.txt │ ├── Core │ │ ├── CMakeLists.txt │ │ ├── Utility.cpp │ │ ├── Importer.cpp │ │ └── Base.cpp │ └── Onnx │ │ ├── CMakeLists.txt │ │ └── OnnxBuilder.cpp ├── Pybind │ └── CMakeLists.txt ├── Support │ ├── CMakeLists.txt │ ├── Core.cpp │ └── Enums.cpp ├── CMakeLists.txt ├── Conversion │ ├── LLHToArith │ │ └── CMakeLists.txt │ ├── LLHToShape │ │ └── CMakeLists.txt │ ├── LLHToTensor │ │ └── CMakeLists.txt │ ├── LLHToFunc │ │ └── CMakeLists.txt │ ├── LLHToMath │ │ └── CMakeLists.txt │ ├── LLHToLinalg │ │ └── CMakeLists.txt │ ├── TosaToLinalgExtension │ │ └── CMakeLists.txt │ ├── LLHToTosa │ │ └── CMakeLists.txt │ ├── CMakeLists.txt │ ├── StablehlotoLinalgExtension │ │ └── CMakeLists.txt │ └── LLHToHLO │ │ ├── CMakeLists.txt │ │ └── LLHToHLO.td ├── Interfaces │ ├── CMakeLists.txt │ ├── SymbolShapeOpInterfaces.cpp │ └── BraodcastableOpInterfaces.cpp └── Compiler │ └── CMakeLists.txt ├── tools ├── CMakeLists.txt ├── onnx-to-mlir │ └── CMakeLists.txt ├── llc-translate │ ├── CMakeLists.txt │ └── llc-translate.cpp ├── llc-opt │ └── CMakeLists.txt └── llcompiler │ └── CMakeLists.txt ├── readme.md ├── cmake ├── policy.cmake ├── third_party │ ├── build_ginac.cmake │ ├── build_pybind11.cmake │ ├── build_googlestest.cmake │ ├── build_spdlog.cmake │ ├── build_stablehlo.cmake │ ├── build_symengine.cmake │ ├── build_onnx.cmake │ ├── build_hlo.cmake │ └── build_llvm.cmake ├── LLCompilerConfig.cmake.in └── build_config.cmake ├── .gitignore ├── test ├── Conversion │ ├── LLHToShape │ │ └── llh_to_shape.mlir │ ├── LLHToMath │ │ └── llh_to_math.mlir │ ├── LLHToLinalg │ │ └── llh_to_linalg.mlir │ ├── LLHToArith │ │ └── llh_to_arith.mlir │ └── StablehlotoLinalgExtension │ │ └── stablehlo_to_linalg_extension.mlir ├── python │ ├── CMakeLists.txt │ ├── ops │ │ ├── max_pool_2d.py │ │ ├── conv2d_nchw_fchw.py │ │ ├── div.py │ │ ├── matmul.py │ │ ├── abs.py │ │ ├── add.py │ │ ├── mul.py │ │ ├── sub.py │ │ ├── batch_norm_2d_inference.py │ │ ├── linear.py │ │ ├── relu.py │ │ ├── sqrt.py │ │ ├── batch_matmul.py │ │ ├── broadcast.py │ │ ├── extract.py │ │ ├── unsqueeze.py │ │ ├── reduce_max.py │ │ ├── reduce_sum.py │ │ ├── batch_norm_1d_inference.py │ │ └── where.py │ └── lit.site.cfg.py.in ├── CMakeLists.txt ├── RunnerTest │ └── abs.mlir ├── Dialect │ ├── LLH │ │ ├── one-shot-bufferize.mlir │ │ ├── ops.mlir │ │ ├── remove_redundant_ops.mlir │ │ ├── layout_canonicalize.mlir │ │ ├── symbol_infer │ │ │ └── symbol_cse.mlir │ │ ├── reshape_before_braodcast.mlir │ │ └── decompose_ops.mlir │ ├── BufferizationExtension │ │ └── alloc_to_arg.mlir │ └── LLVMExtension │ │ └── adapt_entry_parms_for_engine.mlir └── lit.site.cfg.py.in ├── setup.cfg ├── .clangd ├── 前言.md ├── third_party └── CMakeLists.txt ├── main.cpp ├── check_code.py └── discard └── include ├── Support └── Option.h └── Compiler └── Utility.h /ops/cpu/ops.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /ops/cpu/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /.clang-format: -------------------------------------------------------------------------------- 1 | BasedOnStyle: Google 2 | -------------------------------------------------------------------------------- /ops/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(cpu) -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/IR/LLHUtility.h: -------------------------------------------------------------------------------- 1 | -------------------------------------------------------------------------------- /doc/记录/Ai编译器开发日记-使用LLVM回归测试 -- to do.md: -------------------------------------------------------------------------------- 1 | # LIT 2 | -------------------------------------------------------------------------------- /llcompiler/dialect/__init__.py: -------------------------------------------------------------------------------- 1 | from .llh import LLH -------------------------------------------------------------------------------- /src/Dialect/IRExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(IR) -------------------------------------------------------------------------------- /llcompiler/__init__.py: -------------------------------------------------------------------------------- 1 | from .compiler import LLCompiler 2 | -------------------------------------------------------------------------------- /src/Dialect/LLVMExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(Transforms) -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(IR) -------------------------------------------------------------------------------- /src/Dialect/IndexExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(Transforms) -------------------------------------------------------------------------------- /src/Dialect/BufferizationExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(Transforms) -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLVMExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(Transforms) -------------------------------------------------------------------------------- /llcompiler/utility/__init__.py: -------------------------------------------------------------------------------- 1 | from .utility import run_time, Dict_Registry 2 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IndexExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(Transforms) -------------------------------------------------------------------------------- /llcompiler/importer/onnx_op_translate/__init__.py: -------------------------------------------------------------------------------- 1 | from ..onnx_translate import * 2 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/BufferizationExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(Transforms) -------------------------------------------------------------------------------- /src/Dialect/TosaExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(IR) 2 | add_subdirectory(Transforms) -------------------------------------------------------------------------------- /doc/其他/符号计算库-symengine-todo.md: -------------------------------------------------------------------------------- 1 | # symengine 2 | symengine 是MIT协议的C++ 符号计算库。同时也有python的接口。 3 | 4 | 5 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(IR) 2 | add_subdirectory(Transforms) -------------------------------------------------------------------------------- /src/Frontend/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(Core) 2 | # TODO: will remove 3 | #add_subdirectory(Onnx) 4 | -------------------------------------------------------------------------------- /llcompiler/test_models/__init__.py: -------------------------------------------------------------------------------- 1 | from .ops import * 2 | from .models import * 3 | from .utility import * 4 | -------------------------------------------------------------------------------- /llcompiler/test_models/models/__init__.py: -------------------------------------------------------------------------------- 1 | from .resnet import Resnet 2 | from .multi_head_attention import MultiHeadedAttention 3 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(IR) 2 | add_subdirectory(Transforms) 3 | add_subdirectory(SymbolInfer) 4 | -------------------------------------------------------------------------------- /src/Dialect/LLH/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(IR) 2 | add_subdirectory(Transforms) 3 | add_subdirectory(Utils) 4 | add_subdirectory(SymbolInfer) -------------------------------------------------------------------------------- /llcompiler/importer/__init__.py: -------------------------------------------------------------------------------- 1 | from .mlir_gen import MLIR_Builder 2 | from .fx_translate import torch_build_func 3 | from .fx_op_translate import * 4 | -------------------------------------------------------------------------------- /tools/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | #add_subdirectory(llcompiler) 2 | #add_subdirectory(onnx-to-mlir) 3 | add_subdirectory(llc-opt) 4 | add_subdirectory(llc-translate) -------------------------------------------------------------------------------- /llcompiler/compiler/__init__.py: -------------------------------------------------------------------------------- 1 | from .torch_compile_config import ( 2 | LLC_DECOMPOSITIONS, 3 | LLC_SUPPORT_DICT, 4 | LLCOperatorSupport, 5 | ) 6 | from .compiler import LLCompiler 7 | -------------------------------------------------------------------------------- /src/Pybind/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | pybind11_add_module(entrance "${CMAKE_CURRENT_SOURCE_DIR}/Entrance.cpp") 2 | target_link_libraries(entrance PUBLIC LLCompiler) 3 | llcompiler_install_target(entrance) 4 | -------------------------------------------------------------------------------- /include/llcompiler/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(Interfaces) 2 | add_subdirectory(Dialect) 3 | add_subdirectory(Conversion) 4 | add_subdirectory(TransformLibrary) 5 | add_subdirectory(Compiler) 6 | add_subdirectory(Support) 7 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # install 2 | 3 | node:默认生成器是ninja ,其他生成器自行设置 4 | 5 | 安装 6 | ' pip install .' 7 | 8 | 开发模式: 9 | 10 | ' pip install -e .' 11 | 12 | 测试: 13 | ninja check-llcompiler 14 | ninja check-llc-python 15 | 16 | 17 | -------------------------------------------------------------------------------- /include/llcompiler/Support/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Enums.td) 2 | mlir_tablegen(Eunms.h.inc -gen-enum-decls) 3 | mlir_tablegen(Eunms.cpp.inc -gen-enum-defs) 4 | add_public_tablegen_target(LLCEnumIncGen) 5 | add_dependencies(mlir-headers LLCEnumIncGen) -------------------------------------------------------------------------------- /src/Support/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCSupport 2 | SRC_FILES 3 | Logger.cpp 4 | Enums.cpp 5 | 6 | DEPENDS 7 | LLCEnumIncGen 8 | 9 | LINKS PUBLIC 10 | LLVMOption 11 | 12 | PRIVATE 13 | spdlog 14 | ) 15 | -------------------------------------------------------------------------------- /src/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | add_subdirectory(Support) 3 | add_subdirectory(Frontend) 4 | add_subdirectory(Interfaces) 5 | add_subdirectory(Dialect) 6 | add_subdirectory(Conversion) 7 | add_subdirectory(Pipeline) 8 | add_subdirectory(Pybind) 9 | add_subdirectory(Compiler) 10 | -------------------------------------------------------------------------------- /src/Dialect/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(LLH) 2 | add_subdirectory(Utility) 3 | add_subdirectory(IndexExtension) 4 | add_subdirectory(LLVMExtension) 5 | add_subdirectory(BufferizationExtension) 6 | # this is discarded 7 | # add_subdirectory(TosaExtension) 8 | # add_subdirectory(IRExtension) -------------------------------------------------------------------------------- /src/Dialect/IndexExtension/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCIndexExtensionTransforms 2 | SRC_FILES 3 | FoldIndexCast.cpp 4 | 5 | DEPENDS 6 | LLCIndexExtensionPassIncGen 7 | 8 | 9 | LINKS PUBLIC 10 | MLIRIndexDialect 11 | MLIRPass 12 | ) 13 | 14 | -------------------------------------------------------------------------------- /cmake/policy.cmake: -------------------------------------------------------------------------------- 1 | function(new_policy policy) 2 | if(POLICY policy) 3 | cmake_policy(SET policy NEW) 4 | endif() 5 | endfunction(new_policy) 6 | 7 | new_policy(CMP0135) 8 | new_policy(CMP0148) 9 | new_policy(CMP0091) 10 | set(CMAKE_POLICY_DEFAULT_CMP0126 NEW) 11 | 12 | 13 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(LLH) 2 | add_subdirectory(IndexExtension) 3 | add_subdirectory(LLVMExtension) 4 | add_subdirectory(BufferizationExtension) 5 | 6 | # this is discarded 7 | #add_subdirectory(TosaExtension) 8 | #add_subdirectory(IRExtension) 9 | 10 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/matmul.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | class Matmul(nn.Module): 4 | def __init__(self): 5 | super().__init__() 6 | 7 | def forward(self, x: torch.Tensor): 8 | x = torch.matmul(x,x.transpose(-2, -1)) 9 | return x 10 | -------------------------------------------------------------------------------- /src/Dialect/LLVMExtension/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLVMExtensionTransforms 2 | SRC_FILES 3 | AdaptEntryParmsForEngine.cpp 4 | 5 | DEPENDS 6 | LLCLLVMExtensionPassIncGen 7 | 8 | 9 | LINKS PUBLIC 10 | MLIRIndexDialect 11 | MLIRPass 12 | ) 13 | 14 | -------------------------------------------------------------------------------- /src/Dialect/IRExtension/IR/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCIRExtension 2 | SRC_FILES 3 | Types.cpp 4 | Dialect.cpp 5 | Ops.cpp 6 | Enums.cpp 7 | Attrs.cpp 8 | 9 | DEPENDS 10 | LLCIRExtensionIncGen 11 | 12 | LINKS PUBLIC 13 | MLIRSupport 14 | ) 15 | -------------------------------------------------------------------------------- /src/Conversion/LLHToArith/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHToArith 2 | SRC_FILES 3 | LLHToArith.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | MLIRArithDialect 12 | LLCLLHDialect 13 | MLIRFuncDialect 14 | ) 15 | -------------------------------------------------------------------------------- /src/Conversion/LLHToShape/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHToShape 2 | SRC_FILES 3 | LLHToShape.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | LLCLLHDialect 12 | MLIRFuncDialect 13 | MLIRShapeDialect 14 | ) 15 | -------------------------------------------------------------------------------- /cmake/third_party/build_ginac.cmake: -------------------------------------------------------------------------------- 1 | function(build_ginac lib_path) 2 | message("******************************************") 3 | message("************* build ginac ***************") 4 | message("******************************************") 5 | add_subdirectory(${lib_path}) 6 | endfunction(build_ginac) -------------------------------------------------------------------------------- /cmake/third_party/build_pybind11.cmake: -------------------------------------------------------------------------------- 1 | function(build_pybind11 lib_path) 2 | message("******************************************") 3 | message("************* build pybind11 *****************") 4 | message("******************************************") 5 | add_subdirectory(${lib_path}) 6 | endfunction() -------------------------------------------------------------------------------- /src/Conversion/LLHToTensor/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHToTensor 2 | SRC_FILES 3 | LLHToTensor.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | MLIRTensorDialect 12 | LLCLLHDialect 13 | MLIRFuncDialect 14 | ) 15 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/where.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class Where(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | 9 | def forward(self, pre, x: torch.Tensor, y): 10 | x = x.where(pre, y) 11 | x = x + y 12 | return x 13 | -------------------------------------------------------------------------------- /src/Conversion/LLHToFunc/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHToFunc 2 | SRC_FILES 3 | LLHToFunc.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | MLIRArithDialect 12 | LLCLLHDialect 13 | MLIRFuncDialect 14 | MLIRMemRefDialect 15 | ) -------------------------------------------------------------------------------- /src/Conversion/LLHToMath/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHToMath 2 | SRC_FILES 3 | LLHToMath.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | MLIRArithDialect 12 | LLCLLHDialect 13 | MLIRFuncDialect 14 | MLIRMathDialect 15 | ) 16 | -------------------------------------------------------------------------------- /src/Conversion/LLHToLinalg/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHToLinalg 2 | SRC_FILES 3 | LLHToLinalg.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | MLIRArithDialect 12 | LLCLLHDialect 13 | MLIRFuncDialect 14 | MLIRLinalgDialect 15 | ) 16 | -------------------------------------------------------------------------------- /src/Conversion/TosaToLinalgExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(MLIRTosaToLinalgExtension 2 | SRC_FILES 3 | TosaToLinalgExtension.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | MLIRLinalgDialect 12 | LLCLLHDialect 13 | MLIRFuncDialect 14 | ) 15 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/unsqueeze.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class Unsqueeze(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | 9 | def forward(self, x: torch.Tensor): 10 | x1 = x.unsqueeze(1) 11 | x2 = x.unsqueeze(1) 12 | x = x1+x2 13 | return x 14 | -------------------------------------------------------------------------------- /src/Dialect/BufferizationExtension/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCBufferizationExtensionTransforms 2 | SRC_FILES 3 | AllocToArg.cpp 4 | 5 | DEPENDS 6 | LLCBufferizationExtensionPassIncGen 7 | 8 | 9 | LINKS PUBLIC 10 | MLIRFuncDialect 11 | MLIRBufferizationDialect 12 | MLIRPass 13 | ) 14 | 15 | 16 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | build 2 | .vscode 3 | log 4 | install 5 | .cache 6 | .clangd 7 | LLcompiler_weight_temp 8 | dist 9 | llcompiler.egg-info 10 | builtin_module_no-symbol-name 11 | ir_tree 12 | xxx 13 | llcompiler_.cpython-310-x86_64-linux-gnu.so 14 | __pycache__ 15 | log.mlir 16 | quick-command.txt 17 | test.py 18 | main.cpp 19 | test.mlir 20 | temp 21 | -------------------------------------------------------------------------------- /src/Dialect/LLH/Utils/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHUtils 2 | SRC_FILES 3 | Utils.cpp 4 | Broadcast.cpp 5 | InferSymbol.cpp 6 | SymbolAnalysis.cpp 7 | 8 | ADDITIONAL_INCLUDE_DIRS 9 | ${SYMENGINE_INCLUDE_DIRS} 10 | 11 | DEPENDS 12 | LLCLLHDialectIncGen 13 | 14 | 15 | LINKS PUBLIC 16 | LLCLLHDialect 17 | ) -------------------------------------------------------------------------------- /src/Conversion/LLHToTosa/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHToTosa 2 | SRC_FILES 3 | LLHToTosa.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | MLIRTosaDialect 12 | LLCLLHDialect 13 | MLIRFuncDialect 14 | MLIRPass 15 | MLIRTransformUtils 16 | ) 17 | 18 | 19 | -------------------------------------------------------------------------------- /include/llcompiler/Compiler/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | if(DEVELOPER_MODE) 3 | configure_file("${CMAKE_CURRENT_SOURCE_DIR}/ToolPath.h.in" 4 | "${CMAKE_CURRENT_BINARY_DIR}/ToolPath.h" 5 | ) 6 | else() 7 | configure_file("${CMAKE_CURRENT_SOURCE_DIR}/ToolPath.install.h.in" 8 | "${CMAKE_CURRENT_BINARY_DIR}/ToolPath.h" 9 | ) 10 | endif() 11 | -------------------------------------------------------------------------------- /src/Frontend/Core/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCFrontendCore 2 | SRC_FILES 3 | Base.cpp 4 | Builder.cpp 5 | Importer.cpp 6 | Option.cpp 7 | Utility.cpp 8 | 9 | LINKS PUBLIC 10 | LLVMOption 11 | LLCSupport 12 | MLIRTosaDialect 13 | LLCLLHDialect 14 | MLIRFuncDialect 15 | #LLCIRExtension 16 | ) 17 | -------------------------------------------------------------------------------- /src/Conversion/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(LLHToTosa) 2 | add_subdirectory(LLHToArith) 3 | add_subdirectory(LLHToTensor) 4 | add_subdirectory(LLHToHLO) 5 | add_subdirectory(LLHToShape) 6 | add_subdirectory(TosaToLinalgExtension) 7 | add_subdirectory(StablehlotoLinalgExtension) 8 | add_subdirectory(LLHToMath) 9 | add_subdirectory(LLHToLinalg) 10 | add_subdirectory(LLHToFunc) -------------------------------------------------------------------------------- /src/Conversion/StablehlotoLinalgExtension/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCStablehlotoLinalgExtension 2 | SRC_FILES 3 | StablehlotoLinalgExtension.cpp 4 | 5 | DEPENDS 6 | LLCConversionPassIncGen 7 | 8 | LINKS PUBLIC 9 | LLCMLIRUtility 10 | MLIRIR 11 | MLIRLinalgDialect 12 | LLCLLHDialect 13 | MLIRFuncDialect 14 | StablehloOps 15 | ) 16 | 17 | 18 | -------------------------------------------------------------------------------- /tools/onnx-to-mlir/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # #---------- compiler ----------## 2 | llcompiler_add_executable(onnx-to-mlir 3 | "${CMAKE_CURRENT_SOURCE_DIR}/onnx-to-mlir.cpp" 4 | 5 | ADDITIONAL_INCLUDE_DIRS 6 | ${ONNX_INCLUDE_DIR} 7 | ${ONNX_INCLUDE_GENERATE_DIR} 8 | ${PROTOBUF_INCLUDE_DIR} 9 | ${ABSL_INCLUDE_DIR} 10 | 11 | LINKS 12 | LLCompiler 13 | onnx 14 | ) 15 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/pooling.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class MaxPool2D(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | self.pool = nn.MaxPool2d( 9 | 5, 10 | 1, 11 | 2,2 12 | ) 13 | 14 | def forward(self, x: torch.Tensor): 15 | x = self.pool(x) 16 | x = self.pool(x) 17 | return x 18 | -------------------------------------------------------------------------------- /include/llcompiler/Conversion/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Passes.td) 2 | mlir_tablegen(Passes.h.inc -gen-pass-decls -name LLCConversion) 3 | mlir_tablegen(Passes.capi.h.inc -gen-pass-capi-header --prefix LLCConversion) 4 | mlir_tablegen(Passes.capi.cpp.inc -gen-pass-capi-impl --prefix LLCConversion) 5 | add_public_tablegen_target(LLCConversionPassIncGen) 6 | add_mlir_doc(LLCPasses LLCConversionPasses LLC/Conversion/ -gen-pass-doc) -------------------------------------------------------------------------------- /llcompiler/test_models/ops/elewise_fusion.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class ElewiseFusion1(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | self.rule = nn.ReLU() 9 | 10 | def forward(self, x: torch.Tensor): 11 | x = x + x 12 | x = x - 3 13 | x = x / 2 14 | x_max = self.rule(x) 15 | x = x_max * x 16 | return x 17 | 18 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Passes.td) 2 | mlir_tablegen(Passes.h.inc -gen-pass-decls -name TosaExOpt) 3 | mlir_tablegen(PassesEnums.h.inc -gen-enum-decls) 4 | mlir_tablegen(PassesEnums.cpp.inc -gen-enum-defs) 5 | add_public_tablegen_target(LLCTosaExPassIncGen) 6 | add_dependencies(mlir-headers LLCTosaExPassIncGen) 7 | 8 | add_mlir_doc(TosaExPasses TosaExPasses TosaExtension/ -gen-pass-doc) 9 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/SymbolInfer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Passes.td) 2 | mlir_tablegen(Passes.h.inc -gen-pass-decls -name LLCSymbolOpt) 3 | mlir_tablegen(PassesEnums.h.inc -gen-enum-decls) 4 | mlir_tablegen(PassesEnums.cpp.inc -gen-enum-defs) 5 | add_public_tablegen_target(LLCSymbolInferPassIncGen) 6 | add_dependencies(mlir-headers LLCSymbolInferPassIncGen) 7 | 8 | add_mlir_doc(SymbolInfer SymbolInfer LLH/ -gen-pass-doc) 9 | 10 | -------------------------------------------------------------------------------- /test/Conversion/LLHToShape/llh_to_shape.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --convert-llh-to-shape %s| FileCheck %s 2 | // 3 | 4 | // CHECK-LABEL: dim 5 | func.func @dim(%arg0: tensor) ->() attributes {entrance}{ 6 | %c3_i64 = arith.constant 3 : i64 7 | // CHECK-COUNT: index.casts 8 | // CHECK-COUNT: tensor.dim 9 | // CHECK: shape.dim 10 | %8 = "llh.dim"(%arg0, %c3_i64) : (tensor, i64) -> i64 11 | return 12 | } 13 | -------------------------------------------------------------------------------- /test/Conversion/LLHToMath/llh_to_math.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --convert-llh-to-math %s| FileCheck %s 2 | 3 | // CHECK-LABEL: sqrt 4 | func.func @sqrt(%arg0: i64) ->(i64) attributes {entrance}{ 5 | // CHECK: arith.muli 6 | %0 = "llh.sqrt"(%arg0) : (i64) -> i64 7 | return %0: i64 8 | } 9 | 10 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --convert-llh-to-math /home/lfr/LLCompiler/test/Conversion/LLHToMath/llh_to_math.mlir 11 | -------------------------------------------------------------------------------- /cmake/third_party/build_googlestest.cmake: -------------------------------------------------------------------------------- 1 | function(build_googletest lib_path) 2 | message("******************************************") 3 | message("************* build googletest ***************") 4 | message("******************************************") 5 | set(INSTALL_GTEST OFF) 6 | set(gtest_build_tests OFF) 7 | set(gtest_build_samples OFF) 8 | set(gmock_build_tests OFF) 9 | add_subdirectory(${lib_path}) 10 | endfunction(build_googletest) -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLVMExtension/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Passes.td) 2 | mlir_tablegen(Passes.h.inc -gen-pass-decls -name LLVMExtension) 3 | mlir_tablegen(PassesEnums.h.inc -gen-enum-decls) 4 | mlir_tablegen(PassesEnums.cpp.inc -gen-enum-defs) 5 | add_public_tablegen_target(LLCLLVMExtensionPassIncGen) 6 | add_dependencies(mlir-headers LLCLLVMExtensionPassIncGen) 7 | 8 | add_mlir_doc(LLVMExtensionPasses LLVMExtensionPasses LLH/ -gen-pass-doc) 9 | 10 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IndexExtension/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Passes.td) 2 | mlir_tablegen(Passes.h.inc -gen-pass-decls -name IndexExtension) 3 | mlir_tablegen(PassesEnums.h.inc -gen-enum-decls) 4 | mlir_tablegen(PassesEnums.cpp.inc -gen-enum-defs) 5 | add_public_tablegen_target(LLCIndexExtensionPassIncGen) 6 | add_dependencies(mlir-headers LLCIndexExtensionPassIncGen) 7 | 8 | add_mlir_doc(IndexExtensionPasses IndexExtensionPasses LLH/ -gen-pass-doc) 9 | 10 | -------------------------------------------------------------------------------- /src/Interfaces/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | llcompiler_add_library(MLIRSymbolShapeOpInterfaces 3 | SRC_FILES 4 | SymbolShapeOpInterfaces.cpp 5 | 6 | DEPENDS 7 | MLIRSymbolShapeOpInterfacesIncGen 8 | 9 | LINKS PUBLIC 10 | MLIRIR 11 | ) 12 | 13 | llcompiler_add_library(MLIRBraodcastableOpInterfaces 14 | SRC_FILES 15 | BraodcastableOpInterfaces.cpp 16 | 17 | DEPENDS 18 | MLIRBraodcastableOpInterfacesIncGen 19 | 20 | LINKS PUBLIC 21 | MLIRIR 22 | ) 23 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/reduce_fusion.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | 6 | class ReduceFusion1(nn.Module): 7 | def __init__(self): 8 | super().__init__() 9 | self.rule = nn.ReLU() 10 | 11 | def forward(self, x: torch.Tensor): 12 | x = x + x 13 | x = x - 3 14 | x = x / 2 15 | x= torch.softmax(x,1) 16 | x = torch.matmul(x,x.transpose(-2, -1)) 17 | x = x + x 18 | x = x - 3 19 | x = x / 2 20 | return x -------------------------------------------------------------------------------- /src/Dialect/Utility/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCMLIRUtility 2 | SRC_FILES 3 | Type.cpp 4 | Builder.cpp 5 | Attribute.cpp 6 | File.cpp 7 | Tool.cpp 8 | Benefit.cpp 9 | 10 | LINKS PUBLIC 11 | MLIRParser 12 | MLIRIR 13 | LLCSupport 14 | LLCLLHDialect 15 | 16 | ) 17 | 18 | llcompiler_add_library(LLCPatternRewriter 19 | SRC_FILES 20 | RewritePattern.cpp 21 | 22 | LINKS PUBLIC 23 | MLIRParser 24 | MLIRIR 25 | LLCSupport 26 | ) 27 | 28 | -------------------------------------------------------------------------------- /setup.cfg: -------------------------------------------------------------------------------- 1 | [metadata] 2 | name = llcompiler 3 | version = "0.0.1" 4 | author='Lfr' 5 | author_email='1733535832@qq.com' 6 | description='llcompiler' 7 | url='https://github.com/violetDelia/LLCompiler' 8 | license = Apache 2.0 9 | classifiers = 10 | Programming Language :: Python :: 3 11 | Topic :: Software Development :: Compilers 12 | 13 | [options] 14 | install_requires= 15 | xdsl>=0.22.0 16 | onnx>=1.16.2 17 | setuptools>=75.1.0 18 | pybind11>=2.10.3 19 | torch>=2.4.1 20 | 21 | packages = find: 22 | -------------------------------------------------------------------------------- /test/Conversion/LLHToLinalg/llh_to_linalg.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --convert-llh-to-linalg %s| FileCheck %s 2 | 3 | func.func @scalar_cast(%arg0: i64) ->(tensor<1xi64>) attributes {entrance}{ 4 | // CHECK: tensor.empty 5 | // CHECK: linalg.fill 6 | %0 = "llh.scalar_cast"(%arg0) : (i64) -> tensor<1xi64> 7 | return %0: tensor<1xi64> 8 | } 9 | 10 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --convert-llh-to-linalg /home/lfr/LLCompiler/test/Conversion/LLHToLinalg/llh_to_linalg.mlir 11 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/BufferizationExtension/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Passes.td) 2 | mlir_tablegen(Passes.h.inc -gen-pass-decls -name BufferizationExtension) 3 | mlir_tablegen(PassesEnums.h.inc -gen-enum-decls) 4 | mlir_tablegen(PassesEnums.cpp.inc -gen-enum-defs) 5 | add_public_tablegen_target(LLCBufferizationExtensionPassIncGen) 6 | add_dependencies(mlir-headers LLCBufferizationExtensionPassIncGen) 7 | 8 | add_mlir_doc(BufferizationExtensionPasses BufferizationExtensionPasses LLH/ -gen-pass-doc) 9 | 10 | -------------------------------------------------------------------------------- /src/Conversion/LLHToHLO/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS LLHToHLO.td) 2 | mlir_tablegen(LLHToHLO.inc -gen-rewriters EXTRA_INCLUDES ${STABLEHLO_INCLUDE_DIRS}) 3 | add_public_tablegen_target(LLCLLHToHLOIncGen) 4 | 5 | llcompiler_add_library(LLCLLHToHLO 6 | SRC_FILES 7 | LLHToHLO.cpp 8 | LLHPreprocessingForHLO.cpp 9 | 10 | DEPENDS 11 | LLCConversionPassIncGen 12 | LLCLLHToHLOIncGen 13 | 14 | LINKS PUBLIC 15 | LLCMLIRUtility 16 | MLIRIR 17 | MLIRTensorDialect 18 | LLCLLHDialect 19 | MLIRFuncDialect 20 | StablehloOps 21 | ) 22 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/braodcast.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class Braodcast(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | 9 | def forward(self, x: torch.Tensor): 10 | x1 = x.reshape(1, x.shape[0], x.shape[1]) 11 | x1 = x + x1 12 | x2 = x.reshape(x.shape[0], 1, x.shape[1]) 13 | x2 = x + x2 14 | x3 = x.reshape(x.shape[0], 1, x.shape[1]) 15 | x = x1 + x2 16 | # x4 = x[0] 17 | # x5 = x4[0] 18 | # x = x * x5 19 | return x 20 | -------------------------------------------------------------------------------- /test/python/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | configure_lit_site_cfg( 2 | ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in 3 | ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py 4 | MAIN_CONFIG 5 | ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py 6 | ) 7 | 8 | set(LLCOMPILER_TEST_DEPENDS 9 | FileCheck count not 10 | llc-opt 11 | ) 12 | 13 | add_lit_testsuite(check-llc-python "Running the llcompiler regression tests..." 14 | ${CMAKE_CURRENT_BINARY_DIR} 15 | DEPENDS ${LLCOMPILER_TEST_DEPENDS} 16 | ) 17 | set_target_properties(check-llc-python PROPERTIES FOLDER "Test") 18 | 19 | -------------------------------------------------------------------------------- /include/llcompiler/TransformLibrary/scf_include.mlir: -------------------------------------------------------------------------------- 1 | 2 | module @scf_include attributes { transform.with_named_sequence } { 3 | 4 | 5 | transform.named_sequence @loop_pipeline(%module: !transform.any_op {transform.readonly}) { 6 | %scf_for_ops = transform.structured.match ops{["scf.for"]} in %module : (!transform.any_op) -> !transform.op<"scf.for"> 7 | %1 = transform.loop.pipeline %scf_for_ops {iteration_interval = 1 : i64, read_latency = 5 : i64, scheduling_type = "full-loops"} : (!transform.op<"scf.for">) -> !transform.any_op 8 | transform.yield 9 | } 10 | } -------------------------------------------------------------------------------- /llcompiler/test_models/ops/slice.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class Extract(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | 9 | def forward(self, x: torch.Tensor): 10 | x1 = x[1] 11 | x2 = x[-2] 12 | x3 = x1+x2 13 | x3 += x[0] 14 | return x3 15 | 16 | class Slice(nn.Module): 17 | def __init__(self): 18 | super().__init__() 19 | 20 | def forward(self, x: torch.Tensor): 21 | x1 = x[0:3] 22 | x2 = x[3:6] 23 | x3 = x1+x2 24 | return x3 25 | -------------------------------------------------------------------------------- /src/Frontend/Onnx/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCFrontendOnnx 2 | SRC_FILES 3 | OnnxImport.cpp 4 | OnnxBuilder.cpp 5 | 6 | ADDITIONAL_INCLUDE_DIRS 7 | ${ONNX_INCLUDE_DIR} 8 | ${ONNX_INCLUDE_GENERATE_DIR} 9 | ${PROTOBUF_INCLUDE_DIR} 10 | ${ABSL_INCLUDE_DIR} 11 | 12 | LINKS PUBLIC 13 | MLIRSupport 14 | LLCSupport 15 | LLCFrontendCore 16 | LLCMLIRUtility 17 | 18 | PRIVATE 19 | onnx 20 | ) 21 | 22 | if(LLCOMPILER_BUILD_WITH_ONNX_ML) 23 | target_compile_definitions(LLCFrontendOnnx PRIVATE ONNX_ML) 24 | endif() 25 | -------------------------------------------------------------------------------- /tools/llc-translate/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # #---------- compiler ----------## 2 | get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) 3 | get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS) 4 | get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS) 5 | get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS) 6 | llcompiler_add_executable(llc-translate 7 | "${CMAKE_CURRENT_SOURCE_DIR}/llc-translate.cpp" 8 | 9 | LINKS 10 | LLCSupport 11 | LLCPipelines 12 | MLIROptLib 13 | MLIRFuncInlinerExtension 14 | ${translation_libs} 15 | ) 16 | -------------------------------------------------------------------------------- /src/Dialect/LLH/SymbolInfer/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | llcompiler_add_library(LLCSymbolInferTransforms 3 | SRC_FILES 4 | InferSymbolShape.cpp 5 | RemoveSymbol.cpp 6 | SinkBindEncoding.cpp 7 | UnloadAndBindEncoding.cpp 8 | SymbolCSE.cpp 9 | 10 | 11 | 12 | ADDITIONAL_INCLUDE_DIRS 13 | ${SYMENGINE_INCLUDE_DIRS} 14 | 15 | DEPENDS 16 | LLCLLHDialectIncGen 17 | LLCLLHPassIncGen 18 | LLCSymbolInferPassIncGen 19 | LLHTransformLayoutToNHWCIncGen 20 | 21 | LINKS PUBLIC 22 | LLCLLHDialect 23 | MLIRPass 24 | LLCLLHDialect 25 | MLIRArithDialect 26 | MLIRTensorDialect 27 | ) 28 | -------------------------------------------------------------------------------- /cmake/third_party/build_spdlog.cmake: -------------------------------------------------------------------------------- 1 | function(build_spdlog lib_path) 2 | message("******************************************") 3 | message("************* build spdlog ***************") 4 | message("******************************************") 5 | 6 | if(BUILD_SHARED_LIBS) 7 | set(SPDLOG_BUILD_SHARED ON) 8 | else() 9 | set(SPDLOG_BUILD_SHARED OFF) 10 | endif() 11 | set(SPDLOG_USE_STD_FORMAT OFF) 12 | set(SPDLOG_BUILD_TESTS OFF) 13 | set(SPDLOG_INSTALL ON) 14 | set(SPDLOG_FMT_EXTERNAL OFF) 15 | add_subdirectory(${lib_path}) 16 | endfunction(build_spdlog) -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Passes.td) 2 | mlir_tablegen(Passes.h.inc -gen-pass-decls -name LLHOpt) 3 | mlir_tablegen(PassesEnums.h.inc -gen-enum-decls) 4 | mlir_tablegen(PassesEnums.cpp.inc -gen-enum-defs) 5 | add_public_tablegen_target(LLCLLHPassIncGen) 6 | add_dependencies(mlir-headers LLCLLHPassIncGen) 7 | 8 | add_mlir_doc(LLHPasses LLHPasses LLH/ -gen-pass-doc) 9 | 10 | set(LLVM_TARGET_DEFINITIONS TransformLayoutToNHWC.td) 11 | mlir_tablegen(TransformLayoutToNHWC.inc -gen-rewriters) 12 | add_public_tablegen_target(LLHTransformLayoutToNHWCIncGen) 13 | -------------------------------------------------------------------------------- /include/llcompiler/TransformLibrary/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLCOMPILER_TANSFORMLIB_INSTALL_DIR "${LLCOMPILER_INSTALL_INCLUDE_DIR}/llcompiler/TransformLibrary") 2 | 3 | if(DEVELOPER_MODE) 4 | configure_file("${CMAKE_CURRENT_SOURCE_DIR}/LibraryPath.h.in" 5 | "${CMAKE_CURRENT_BINARY_DIR}/LibraryPath.h" 6 | ) 7 | else() 8 | configure_file("${CMAKE_CURRENT_SOURCE_DIR}/LibraryPath.install.h.in" 9 | "${CMAKE_CURRENT_BINARY_DIR}/LibraryPath.h" 10 | ) 11 | endif() 12 | 13 | configure_file("${CMAKE_CURRENT_SOURCE_DIR}/LibraryEntry.h.in" 14 | "${CMAKE_CURRENT_BINARY_DIR}/LibraryEntry.h" 15 | ) 16 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/conv.py: -------------------------------------------------------------------------------- 1 | 2 | import torch.nn as nn 3 | import torch 4 | 5 | 6 | class Conv2D_NCHW_FCHW(nn.Module): 7 | def __init__(self): 8 | super().__init__() 9 | self.conv = nn.Conv2d( 10 | 3, 11 | 3, 12 | kernel_size=5, 13 | stride=1, 14 | padding=2, 15 | groups=1, 16 | bias=False, 17 | dilation=1, 18 | ) 19 | 20 | def forward(self, x: torch.Tensor): 21 | #x = x.reshape(x.shape[0],x.shape[1],224,224) 22 | x = self.conv(x) 23 | x = self.conv(x) 24 | return x -------------------------------------------------------------------------------- /cmake/LLCompilerConfig.cmake.in: -------------------------------------------------------------------------------- 1 | @PACKGE_INIT@ 2 | set(LLCOMPILER_INCLUDE_DIR "@LLCOMPILER_INSTALL_INCLUDE_DIR@") 3 | set(LLCOMPILER_LIBRARY_DIR "@LLCOMPILER_INSTALL_LIBRARY_DIR@") 4 | set(LLCOMPILER_ALL_TARGETS "@LLCOMPILER_ALL_TARGETS@") 5 | set(LLCOMPILER_INSTALLED_TARGETS "@LLCOMPILER_INSTALLED_TARGETS@") 6 | set(LLCOMPILER_MLIR_TARGETS "@LLCOMPILER_MLIR_TARGETS@") 7 | set(LLCOMPILER_MLIR_DIALECT "@LLCOMPILER_MLIR_DIALECT@") 8 | set(LLCOMPILER_MLIR_TRANSFORM "@LLCOMPILER_MLIR_TRANSFORM@") 9 | set(LLCOMPILER_MLIR_CONVERSION_TARGETS "@LLCOMPILER_MLIR_CONVERSION_TARGETS@") 10 | set(LLCOMPILER_MLIR_PIPELINE "@LLCOMPILER_MLIR_PIPELINE@") -------------------------------------------------------------------------------- /llcompiler/test_models/ops/__init__.py: -------------------------------------------------------------------------------- 1 | from .elewise_ops import Abs, Add, Sub, Mul, Div, Relu, Sqrt, EQ, Exp, Drop 2 | from .batch_norm import BatchNorm1D_Inference, BatchNorm2D_Inference 3 | from .conv import Conv2D_NCHW_FCHW 4 | from .linear import Linear 5 | from .pooling import MaxPool2D 6 | from .elewise_fusion import ElewiseFusion1 7 | from .unsqueeze import Unsqueeze 8 | from .slice import Extract, Slice 9 | from .decompose import Decompose_BatchNorm 10 | from .braodcast import Braodcast 11 | from .matmul import Matmul 12 | from .where import Where 13 | from .reduce import RecudeMax, RecudeSum 14 | from .reduce_fusion import ReduceFusion1 15 | -------------------------------------------------------------------------------- /cmake/build_config.cmake: -------------------------------------------------------------------------------- 1 | set(CMAKE_C_STANDARD 17) 2 | set(CMAKE_CXX_STANDARD 20) 3 | 4 | set(CMAKE_INSTALL_PREFIX "${CMAKE_CURRENT_SOURCE_DIR}/install") 5 | 6 | if(MSVC) 7 | if(STATIC_WINDOWS_RUNTIME) 8 | set(CMAKE_MSVC_RUNTIME_LIBRARY "MultiThreaded$<$:Debug>") 9 | endif() 10 | endif() 11 | 12 | if(LLCOMPILER_BUILD_WARNINGS) 13 | if(MSVC) 14 | add_compile_options($<$:/Wall>) 15 | else() 16 | add_compile_options($<$:-w>) 17 | add_compile_options($<$:-w>) 18 | endif() 19 | endif() 20 | 21 | add_compile_options(-fPIC) 22 | 23 | -------------------------------------------------------------------------------- /src/Support/Core.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | -------------------------------------------------------------------------------- /src/Dialect/LLH/Transforms/Aot.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // -------------------------------------------------------------------------------- /src/Dialect/LLH/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | llcompiler_add_library(LLCLLHTransforms 3 | SRC_FILES 4 | TransformLayout.cpp 5 | LoadWeight.cpp 6 | Operationlegalization.cpp 7 | RemoveRedundantOps.cpp 8 | ReshapeBeforeBraodcast.cpp 9 | InsetBraodCastPass.cpp 10 | MarkAot.cpp 11 | DecomposeOps.cpp 12 | BufferizableOpInterfaceImpl.cpp 13 | 14 | ADDITIONAL_INCLUDE_DIRS 15 | ${SYMENGINE_INCLUDE_DIRS} 16 | 17 | DEPENDS 18 | LLCLLHDialectIncGen 19 | LLCLLHPassIncGen 20 | LLHTransformLayoutToNHWCIncGen 21 | 22 | LINKS PUBLIC 23 | LLCLLHDialect 24 | MLIRPass 25 | #LLCIRExtension 26 | LLCMLIRUtility 27 | LLCLLHUtils 28 | LLCPatternRewriter 29 | ) 30 | -------------------------------------------------------------------------------- /test/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | add_subdirectory(python) 2 | 3 | configure_lit_site_cfg( 4 | ${CMAKE_CURRENT_SOURCE_DIR}/lit.site.cfg.py.in 5 | ${CMAKE_CURRENT_BINARY_DIR}/lit.site.cfg.py 6 | MAIN_CONFIG 7 | ${CMAKE_CURRENT_SOURCE_DIR}/lit.cfg.py 8 | ) 9 | set(LLCOMPILER_TEST_DEPENDS 10 | FileCheck count not 11 | llc-opt 12 | mlir-runner 13 | mlir-opt 14 | mlir_c_runner_utils 15 | mlir_runner_utils 16 | ) 17 | 18 | add_lit_testsuite(check-llcompiler "Running the llcompiler regression tests..." 19 | ${CMAKE_CURRENT_BINARY_DIR} 20 | DEPENDS ${LLCOMPILER_TEST_DEPENDS} 21 | ) 22 | set_target_properties(check-llcompiler PROPERTIES FOLDER "Test") 23 | 24 | -------------------------------------------------------------------------------- /cmake/third_party/build_stablehlo.cmake: -------------------------------------------------------------------------------- 1 | function(build_stablehlo lib_path llvm_path) 2 | message("******************************************") 3 | message("************* build stablehlo ***************") 4 | message("******************************************") 5 | set(STABLEHLO_EXTERNAL_PROJECT_BUILD ON) 6 | set(LLVM_MAIN_SRC_DIR ${llvm_path}) 7 | list(APPEND CMAKE_MODULE_PATH ${MLIR_CMAKE_DIR}) 8 | list(APPEND CMAKE_MODULE_PATH ${LLVM_CMAKE_DIR}) 9 | include_directories(${LLVM_INCLUDE_DIRS}) 10 | include_directories(${MLIR_INCLUDE_DIRS}) 11 | message(${MLIR_CMAKE_DIR}) 12 | message(${LLVM_CMAKE_DIR}) 13 | add_subdirectory(${lib_path}) 14 | endfunction(build_stablehlo) -------------------------------------------------------------------------------- /llcompiler/test_models/ops/linear.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class Linear(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | self.linear1 = nn.Linear( 9 | 100000, 10 | 10, 11 | bias=False, 12 | ) 13 | self.linear2 = nn.Linear( 14 | 10, 15 | 100, 16 | bias=False, 17 | ) 18 | self.linear3 = nn.Linear( 19 | 100, 20 | 10, 21 | bias=False, 22 | ) 23 | 24 | def forward(self, x: torch.Tensor): 25 | x = self.linear1(x) 26 | x = self.linear2(x) 27 | x = self.linear3(x) 28 | return x 29 | -------------------------------------------------------------------------------- /test/RunnerTest/abs.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt %s -transform-pipeline | mlir-runner -e main -entry-point-result=void -shared-libs=%mlir_runner_utils,%mlir_c_runner_utils | FileCheck %s 2 | 3 | module{ 4 | func.func @main() -> () attributes {entrance} { 5 | %const = "llh.constant"() <{value = dense<-1.000000e+00> : tensor<2xf32>}> : () -> tensor<2xf32> 6 | %0 = "llh.abs"(%const) : (tensor<2xf32>) -> tensor<2xf32> 7 | // CHECK: abs 8 | // CHECK: Unranked Memref 9 | // CHECK-SAME: rank = 1 offset = 0 sizes = [2] strides = [1] data = 10 | // CHECK-NEXT: [1, 1] 11 | "llh.print"(%0) <{prefix_description = "abs"}>: (tensor<2xf32>) -> () 12 | return 13 | } 14 | } -------------------------------------------------------------------------------- /src/Dialect/TosaExtension/Transforms/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCTosaExtensionTransforms 2 | SRC_FILES 3 | TransformLayoutToNHWC.cpp 4 | 5 | DEPENDS 6 | LLCTosaExDialectIncGen 7 | LLCTosaExPassIncGen 8 | 9 | LINKS PUBLIC 10 | MLIRTosaExDialect 11 | MLIRPass 12 | ) 13 | 14 | # add_mlir_library(LLCTosaExtensionTransforms 15 | # TransformLayoutToNHWC.cpp 16 | 17 | # ADDITIONAL_HEADER_DIRS 18 | # ${PROJECT_SOURCE_DIR}/include/llcompiler/Dialect/TosaExtension/Transforms 19 | 20 | # DEPENDS 21 | # LLCTosaExDialectIncGen 22 | # LLCTosaExPassIncGen 23 | 24 | # LINK_LIBS PUBLIC 25 | # MLIRTosaExDialect 26 | # MLIRPass 27 | # ) 28 | # llcompiler_install_mlir_target(LLCTosaExtensionTransforms TRANSFORM) -------------------------------------------------------------------------------- /test/Dialect/LLH/one-shot-bufferize.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt %s -one-shot-bufferize="bufferize-function-boundaries" -canonicalize -buffer-loop-hoisting -drop-equivalent-buffer-results -split-input-file | FileCheck %s 2 | 3 | func.func @print(){ 4 | %const = arith.constant dense<1.0> : tensor<2xf32> 5 | // CHECK: llh.print 6 | // CHECK-SAME: memref<2xf32> 7 | "llh.print"(%const) <{prefix_description = "print const: "}>: (tensor<2xf32>) -> () 8 | return 9 | } 10 | 11 | // /home/lfr/LLCompiler/build/bin/llc-opt /home/lfr/LLCompiler/test/Dialect/LLH/one-shot-bufferize.mlir -one-shot-bufferize="bufferize-function-boundaries" -canonicalize -buffer-loop-hoisting -drop-equivalent-buffer-results -split-input-file 12 | -------------------------------------------------------------------------------- /src/Frontend/Core/Utility.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | namespace llc::front {} // namespace llc::front 16 | -------------------------------------------------------------------------------- /test/python/ops/max_pool_2d.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | import llcompiler.compiler as LLC 5 | import os.path 6 | import subprocess 7 | import torch 8 | import torchvision 9 | import torch.nn as nn 10 | import torch.nn.functional as F 11 | from torch._dynamo.backends.common import aot_autograd 12 | import torch.fx 13 | from llcompiler.utility import run_time 14 | import onnx 15 | import torchgen 16 | import torch._dynamo 17 | import os 18 | from llcompiler.test_models import * 19 | 20 | 21 | if __name__ == "__main__": 22 | model = MaxPool2D() 23 | input = [torch.randn(3, 3, 224, 224)] 24 | check_static_model(model, input) 25 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/decompose.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class Decompose_BatchNorm(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | 9 | self.linear1 = nn.Linear(224, 100) 10 | self.batch1 = nn.BatchNorm1d(100) 11 | self.linear2 = nn.Linear(100, 10) 12 | self.batch2 = nn.BatchNorm1d(10) 13 | self.rule = nn.ReLU() 14 | self.flaten = nn.Flatten() 15 | 16 | def forward(self, x: torch.Tensor): 17 | x = self.linear1(x) 18 | x = self.batch1(x) 19 | x = self.rule(x) 20 | x = self.linear2(x) 21 | x = self.batch2(x) 22 | x = self.rule(x) 23 | x = self.flaten(x) 24 | return x 25 | -------------------------------------------------------------------------------- /.clangd: -------------------------------------------------------------------------------- 1 | CompileFlags: 2 | Add: [-xc++,-Wall 3 | -I/home/lfr/LLCompiler/include/llcompiler/, 4 | -I/home/lfr/LLCompiler/src/, 5 | -I/home/lfr/LLCompiler/third_party/onnx/, 6 | -I/home/lfr/LLCompiler/third_party/spdlog/include/, 7 | -I/home/lfr/LLCompiler/third_party/pybind11/include/, 8 | -I/home/lfr/LLCompiler/third_party/llvm-project/llvm/include/, 9 | -I/home/lfr/LLCompiler/third_party/llvm-project/mlir/include/, 10 | -I/home/lfr/LLCompiler/third_party/symengine/, 11 | -I/home/lfr/LLCompiler/build/include/llcompiler/, 12 | -I/home/lfr/LLCompiler/build/src/, 13 | ] 14 | Diagnostics: 15 | UnusedIncludes: Strict 16 | 17 | InlayHints: 18 | Designators: Yes 19 | Enabled: Yes 20 | ParameterNames: Yes 21 | DeducedTypes: Yes 22 | -------------------------------------------------------------------------------- /test/python/ops/conv2d_nchw_fchw.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | import llcompiler.compiler as LLC 5 | import os.path 6 | import subprocess 7 | import torch 8 | import torchvision 9 | import torch.nn as nn 10 | import torch.nn.functional as F 11 | from torch._dynamo.backends.common import aot_autograd 12 | import torch.fx 13 | from llcompiler.utility import run_time 14 | import onnx 15 | import torchgen 16 | import torch._dynamo 17 | import os 18 | from llcompiler.test_models import * 19 | 20 | 21 | if __name__ == "__main__": 22 | model = Conv2D_NCHW_FCHW() 23 | input = [torch.randn(3, 3, 224, 224)] 24 | check_static_model(model, input) 25 | -------------------------------------------------------------------------------- /include/llcompiler/Interfaces/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | 2 | 3 | set(LLVM_TARGET_DEFINITIONS SymbolShapeOpInterfaces.td) 4 | mlir_tablegen(SymbolShapeOpInterfaces.h.inc -gen-op-interface-decls) 5 | mlir_tablegen(SymbolShapeOpInterfaces.cpp.inc -gen-op-interface-defs) 6 | add_public_tablegen_target(MLIRSymbolShapeOpInterfacesIncGen) 7 | add_dependencies(mlir-generic-headers MLIRSymbolShapeOpInterfacesIncGen) 8 | 9 | set(LLVM_TARGET_DEFINITIONS BraodcastableOpInterfaces.td) 10 | mlir_tablegen(BraodcastableOpInterfaces.h.inc -gen-op-interface-decls) 11 | mlir_tablegen(BraodcastableOpInterfaces.cpp.inc -gen-op-interface-defs) 12 | add_public_tablegen_target(MLIRBraodcastableOpInterfacesIncGen) 13 | add_dependencies(mlir-generic-headers MLIRBraodcastableOpInterfacesIncGen) 14 | -------------------------------------------------------------------------------- /src/Dialect/LLH/IR/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(LLCLLHDialect 2 | SRC_FILES 3 | LLHTypes.cpp 4 | LLHDialect.cpp 5 | LLHOps.cpp 6 | LLHFolder.cpp 7 | LLHCast.cpp 8 | LLHAttrs.cpp 9 | LLHinfersymbolShape.cpp 10 | 11 | # LLHBraodcastableOpInterfaces.cpp 12 | LLHEnums.cpp 13 | LLHVerfy.cpp 14 | LLHCanonicalize.cpp 15 | LLHSymbolUses.cpp 16 | LLHSymbolCanonicalize.cpp 17 | LLHUtilitys.cpp 18 | 19 | ADDITIONAL_INCLUDE_DIRS 20 | ${SYMENGINE_INCLUDE_DIRS} 21 | 22 | DEPENDS 23 | LLCLLHDialectIncGen 24 | LLHCanonicalizeIncGen 25 | 26 | LINKS PUBLIC 27 | MLIRIR 28 | LLCSupport 29 | MLIRSymbolShapeOpInterfaces 30 | MLIRBraodcastableOpInterfaces 31 | symengine 32 | ) 33 | -------------------------------------------------------------------------------- /src/Compiler/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | get_property(pipeline_depends GLOBAL PROPERTY LLCOMPILER_PIPELINE_DEPENDS) 2 | get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) 3 | get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS) 4 | get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS) 5 | get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS) 6 | 7 | llcompiler_add_library(LLCompiler 8 | SRC_FILES 9 | Init.cpp 10 | Compiler.cpp 11 | Tensor.cpp 12 | CompileOptions.cpp 13 | Command.cpp 14 | Execution.cpp 15 | 16 | ADDITIONAL_INCLUDE_DIRS 17 | 18 | LINKS PUBLIC 19 | MLIRIR 20 | LLCSupport 21 | LLCPipelines 22 | MLIRExecutionEngine 23 | LLVMX86AsmParser 24 | ${pipeline_depends} 25 | ) 26 | -------------------------------------------------------------------------------- /src/Support/Enums.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #include "llcompiler/Support/Enums.h" 17 | 18 | #include "llcompiler/Support/Eunms.cpp.inc" 19 | -------------------------------------------------------------------------------- /cmake/third_party/build_symengine.cmake: -------------------------------------------------------------------------------- 1 | function(build_symengine lib_path) 2 | message("******************************************") 3 | message("************* build symengine ***************") 4 | message("******************************************") 5 | set(WITH_SYMENGINE_ASSERT yes) 6 | set(WITH_SYMENGINE_RCP yes) 7 | set(WITH_SYMENGINE_THREAD_SAFE res) 8 | set(WITH_ECM no) 9 | set(WITH_PRIMESIEVE no) 10 | set(WITH_FLINT no) 11 | set(WITH_ARB no) 12 | set(WITH_TCMALLOC no) 13 | set(WITH_PIRANHA no) 14 | set(WITH_MPFR no) 15 | set(WITH_MPC no) 16 | set(WITH_LLVM no) 17 | set(WITH_OPENMP no) 18 | set(WITH_SYSTEM_CEREAL no) 19 | set(BUILD_TESTS no) 20 | add_subdirectory(${lib_path}) 21 | endfunction(build_symengine) -------------------------------------------------------------------------------- /llcompiler/executor/__init__.py: -------------------------------------------------------------------------------- 1 | # Copyright 2024 时光丶人爱 2 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | from ..importer.importer import Importer 15 | from .engine import Torch_ExecutionEngine 16 | from .out_put_call import GenOutput 17 | -------------------------------------------------------------------------------- /src/Dialect/LLH/IR/LLHEnums.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #include "llcompiler/Dialect/LLH/IR/LLHEnums.h" 17 | 18 | #include "llcompiler/Dialect/LLH/IR/LLHEunms.cpp.inc" 19 | -------------------------------------------------------------------------------- /src/Dialect/TosaExtension/IR/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | llcompiler_add_library(MLIRTosaExDialect 2 | SRC_FILES 3 | TosaExTypes.cpp 4 | TosaExDialect.cpp 5 | TosaExOps.cpp 6 | 7 | DEPENDS 8 | LLCTosaExDialectIncGen 9 | 10 | LINKS PUBLIC 11 | MLIRIR 12 | MLIRInferTypeOpInterface 13 | MLIRFuncDialect 14 | ) 15 | 16 | # add_mlir_dialect_library(MLIRTosaExDialect 17 | # TosaExTypes.cpp 18 | # TosaExDialect.cpp 19 | # TosaExOps.cpp 20 | 21 | # ADDITIONAL_HEADER_DIRS 22 | # ${PROJECT_SOURCE_DIR}/include/llcompiler/Dialect/TosaEx 23 | 24 | # DEPENDS 25 | # LLCTosaExDialectIncGen 26 | 27 | # LINK_LIBS PUBLIC 28 | # MLIRIR 29 | # MLIRInferTypeOpInterface 30 | # MLIRFuncDialect 31 | # ) 32 | # llcompiler_install_mlir_target(MLIRTosaExDialect DIALECT) -------------------------------------------------------------------------------- /test/python/ops/div.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Div() 25 | input = [torch.randn(3, 3, 100)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/matmul.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Matmul() 25 | input = [torch.randn(3,3)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/abs.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Abs() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/add.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Add() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/mul.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Mul() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/sub.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Sub() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/batch_norm_2d_inference.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: dynamic model inference are correct. 4 | import llcompiler.compiler as LLC 5 | import os.path 6 | import subprocess 7 | import torch 8 | import torchvision 9 | import torch.nn as nn 10 | import torch.nn.functional as F 11 | from torch._dynamo.backends.common import aot_autograd 12 | import torch.fx 13 | from llcompiler.utility import run_time 14 | import onnx 15 | import torchgen 16 | import torch._dynamo 17 | import os 18 | from llcompiler.test_models import * 19 | 20 | 21 | if __name__ == "__main__": 22 | model = BatchNorm2D_Inference() 23 | input = [torch.randn(3, 3, 224, 224)] 24 | check_static_model_inference(model, input) 25 | check_dynamic_model_inference(model, input) -------------------------------------------------------------------------------- /test/python/ops/linear.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Linear() 25 | input = [torch.randn(10,100000)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/relu.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Relu() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/sqrt.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Sqrt() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/TosaExEnums.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef LLH_TOSAEX_EUNMS 15 | #define LLH_TOSAEX_EUNMS 16 | include "mlir/IR/EnumAttr.td" 17 | #endif // LLH_TOSAEX_EUNMS -------------------------------------------------------------------------------- /src/Dialect/IRExtension/IR/Enums.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #include "llcompiler/Dialect/IRExtension/IR/Enums.h" 17 | 18 | #include "llcompiler/Dialect/IRExtension/IR/Eunms.cpp.inc" 19 | -------------------------------------------------------------------------------- /test/python/ops/batch_matmul.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Matmul() 25 | input = [torch.randn(3, 224, 112)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/broadcast.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Braodcast() 25 | input = [torch.randn(224, 112)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/extract.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Extract() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/unsqueeze.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Unsqueeze() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) -------------------------------------------------------------------------------- /src/Interfaces/SymbolShapeOpInterfaces.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | #include "llcompiler/Interfaces/SymbolShapeOpInterfaces.h" 15 | 16 | #include "llcompiler/Interfaces/SymbolShapeOpInterfaces.cpp.inc" 17 | -------------------------------------------------------------------------------- /test/python/ops/reduce_max.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = RecudeMax() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /test/python/ops/reduce_sum.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = RecudeSum() 25 | input = [torch.randn(3, 3, 224, 224)] 26 | check_model(model, input) 27 | -------------------------------------------------------------------------------- /src/Interfaces/BraodcastableOpInterfaces.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | #include "llcompiler/Interfaces/BraodcastableOpInterfaces.h" 15 | 16 | #include "llcompiler/Interfaces/BraodcastableOpInterfaces.cpp.inc" 17 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Ops.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef LLC_IREX_OPS 15 | #define LLC_IREX_OPS 16 | include "llcompiler/Dialect/IRExtension/IR/Attrs.td" 17 | 18 | #endif // LLC_IREX_OPS 19 | -------------------------------------------------------------------------------- /src/Dialect/IRExtension/IR/Ops.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "llcompiler/Dialect/IRExtension/IR/Ops.h" 16 | 17 | #define GET_OP_CLASSES 18 | #include "llcompiler/Dialect/IRExtension/IR/Ops.cpp.inc" 19 | -------------------------------------------------------------------------------- /test/python/ops/batch_norm_1d_inference.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: dynamic model inference are correct. 4 | import llcompiler.compiler as LLC 5 | import os.path 6 | import subprocess 7 | import torch 8 | import torchvision 9 | import torch.nn as nn 10 | import torch.nn.functional as F 11 | from torch._dynamo.backends.common import aot_autograd 12 | import torch.fx 13 | from llcompiler.utility import run_time 14 | import onnx 15 | import torchgen 16 | import torch._dynamo 17 | import os 18 | from llcompiler.test_models import * 19 | 20 | 21 | if __name__ == "__main__": 22 | model = BatchNorm1D_Inference() 23 | model.training = False 24 | input = [torch.randn(3, 3, 224)] 25 | check_static_model_inference(model, input) 26 | check_dynamic_model_inference(model, input) -------------------------------------------------------------------------------- /前言.md: -------------------------------------------------------------------------------- 1 | 这篇教程的目的是MLIR的基础教程,快速了解MLIR框架的用法和功能。 2 | 3 | ### 简介: 4 | 5 | MLIR它起源一方面是由于深度学习框架的兴起,因为不同的深度学习框架(TF,torch)都会有计算图的概念,于是为了优化计算图的运行,就需要图编译,于是每个框架都会自己实现图编译系统,甚至会为每一种后端去实现相应的编译软件,于是带来了软件碎片化的问题,因为每款”图编译器“都会有很多通用的地方,但是由于实现方式不同,导致无法复用。MLIR它就是提供了一套可复用的基础设施来尝试解决这个问题。 6 | 7 | 另一方面,传统LLVM IR表达的语义层次较低,并且是静态的。现代的高级语言往往是动态且具备很多高级特性。这使得从高级语言Lowing到LLVM IR较为困难且难以保持高级语言的特性。MLIR可以通过更高级的抽象表达将这些层次更高,更抽象的的信息在lowing中保留下来。 8 | 9 | MLIR更像是一套Dialect系统,包括定义Dialect内部的Operation,Type,Attribute,Interface以及Dialect之间的转换一套基础设施。 10 | 11 | ### 大纲: 12 | 13 | 主要内容是:定义一个名为”北极星“的方言,去完成一个softmax 算子数据并行下的编译与运行。考虑到没有相应的通讯库,以及多卡的条件,所以实际的通运和数据都只是模拟运行的。 14 | 15 | 第一阶段:定义”北极星”方言,包括类型,属性,以及接口。以及MLIR IR的结构。 16 | 17 | 第二阶段:实现IR的Pass以及变换。 18 | 19 | 第三个阶段:利用lit测试框架进行测试。 20 | 21 | 第四个阶段:进行方言的转换,将”北极星“方言下降到LLVM IR。 22 | 23 | 第五阶段:执行 “数据并行”条件下softmax算子。 24 | -------------------------------------------------------------------------------- /llcompiler/test_models/ops/reduce.py: -------------------------------------------------------------------------------- 1 | import torch.nn as nn 2 | import torch 3 | 4 | 5 | class RecudeMax(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | 9 | def forward(self, x: torch.Tensor): 10 | x2 = x.amax(x.shape[0] - 1, keepdim=False) 11 | x1 = x.amax(x.shape[0] - 1, keepdim=True) 12 | x1 = x1.squeeze(x.shape[0] - 1) 13 | x = x1 + x2 14 | x = x.amax(-1, keepdim=True) 15 | return x 16 | 17 | 18 | class RecudeSum(nn.Module): 19 | def __init__(self): 20 | super().__init__() 21 | 22 | def forward(self, x: torch.Tensor): 23 | x2 = x.sum(x.shape[0] - 1, keepdim=False) 24 | x1 = x.sum(x.shape[0] - 1, keepdim=True) 25 | x1 = x1.squeeze(x.shape[0] - 1) 26 | x = x1 + x2 27 | x = x.sum(-1, keepdim=True) 28 | return x 29 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Constraints.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef LLC_IREX_CONSTRAINTS 15 | #define LLC_IREX_CONSTRAINTS 16 | include "llcompiler/Dialect/IRExtension/IR/Types.td" 17 | 18 | #endif // LLC_IREX_CONSTRAINTS -------------------------------------------------------------------------------- /llcompiler/test_models/ops/batch_norm.py: -------------------------------------------------------------------------------- 1 | 2 | import torch.nn as nn 3 | import torch 4 | 5 | class BatchNorm2D_Inference(nn.Module): 6 | def __init__(self): 7 | super().__init__() 8 | self.batch = nn.BatchNorm2d(3) 9 | self.batch.training = False 10 | def forward(self, x: torch.Tensor): 11 | x = self.batch(x) 12 | 13 | 14 | x = self.batch(x) 15 | x = self.batch(x) 16 | x = self.batch(x) 17 | return x 18 | 19 | class BatchNorm1D_Inference(nn.Module): 20 | def __init__(self): 21 | super().__init__() 22 | self.batch = nn.BatchNorm1d(3) 23 | self.batch.training = False 24 | def forward(self, x: torch.Tensor): 25 | x = self.batch(x) 26 | x = self.batch(x) 27 | x = self.batch(x) 28 | x = self.batch(x) 29 | return x -------------------------------------------------------------------------------- /cmake/third_party/build_onnx.cmake: -------------------------------------------------------------------------------- 1 | function(build_onnx lib_path) 2 | message("******************************************") 3 | message("************* build onnx *****************") 4 | message("******************************************") 5 | 6 | if(STATIC_WINDOWS_RUNTIME) 7 | set(ONNX_USE_MSVC_STATIC_RUNTIME ON) 8 | endif() 9 | 10 | if(LLCOMPILER_BUILD_WITH_ONNX_ML) 11 | set(ONNX_ML ON) 12 | else() 13 | set(ONNX_ML OFF) 14 | endif() 15 | 16 | set(ONNX_BUILD_SHARED_LIBS OFF) 17 | set(ONNX_USE_PROTOBUF_SHARED_LIBS OFF) 18 | 19 | set(ONNX_DISABLE_EXCEPTIONS ON) 20 | set(ONNX_VERIFY_PROTO3 ON) 21 | set(BUILD_ONNX_PYTHON OFF) 22 | set(ONNX_BUILD_TESTS OFF) 23 | set(ONNX_WERROR OFF) 24 | set(ONNX_DISABLE_STATIC_REGISTRATION ON) 25 | add_subdirectory(${lib_path}) 26 | endfunction(build_onnx) -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS Ops.td) 2 | mlir_tablegen(Eunms.h.inc -gen-enum-decls) 3 | mlir_tablegen(Eunms.cpp.inc -gen-enum-defs) 4 | mlir_tablegen(Types.h.inc -gen-typedef-decls) 5 | mlir_tablegen(Types.cpp.inc -gen-typedef-defs) 6 | mlir_tablegen(Attrs.h.inc -gen-attrdef-decls) 7 | mlir_tablegen(Attrs.cpp.inc -gen-attrdef-defs) 8 | mlir_tablegen(Ops.h.inc -gen-op-decls) 9 | mlir_tablegen(Ops.cpp.inc -gen-op-defs) 10 | mlir_tablegen(Dialect.h.inc -gen-dialect-decls -dialect=mlir_ex) 11 | mlir_tablegen(Dialect.cpp.inc -gen-dialect-defs -dialect=mlir_ex) 12 | add_public_tablegen_target(LLCIRExtensionIncGen) 13 | add_dependencies(mlir-headers LLCIRExtensionIncGen) 14 | 15 | add_mlir_doc(IRExtensionDialect IRExtensionDialect IRExtension/ -gen-dialect-doc) 16 | add_mlir_doc(IRExtensionOps IRExtensionOps IRExtension/ -gen-op-doc) -------------------------------------------------------------------------------- /tools/llc-opt/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # #---------- compiler ----------## 2 | get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) 3 | get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS) 4 | get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS) 5 | get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS) 6 | get_property(pipeline_depends GLOBAL PROPERTY LLCOMPILER_PIPELINE_DEPENDS) 7 | llcompiler_add_executable(llc-opt 8 | "${CMAKE_CURRENT_SOURCE_DIR}/llc-opt.cpp" 9 | 10 | ADDITIONAL_INCLUDE_DIRS 11 | 12 | LINKS 13 | LLCSupport 14 | LLCPipelines 15 | MLIROptLib 16 | LLCompiler 17 | ${pipeline_depends} 18 | ${dialect_libs} 19 | ${conversion_libs} 20 | ${extension_libs} 21 | # AllMhloPasses 22 | # DeallocationPasses 23 | # MLIRHLOGPUTransforms 24 | # MhloRegisterDialects 25 | ) 26 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/TosaExInterfaces.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef LLH_TOSAEX_INTERFACES 16 | #define LLH_TOSAEX_INTERFACES 17 | include "mlir/Dialect/Tosa/IR/TosaInterfaces.td" 18 | 19 | #endif // LLH_TOSAEX_INTERFACES 20 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/TosaExConstraints.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef LLH_TOSAEX_CONSTRAINTS 15 | #define LLH_TOSAEX_CONSTRAINTS 16 | include "llcompiler/Dialect/TosaExtension/IR/TosaExTypes.td" 17 | 18 | #endif // LLH_TOSAEX_CONSTRAINTS -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Types.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef LLC_IREX_YPPES 16 | #define LLC_IREX_TYPES 17 | 18 | include "mlir/IR/DialectBase.td" 19 | include "llcompiler/Dialect/IRExtension/IR/Dialect.td" 20 | 21 | 22 | #endif // LLC_IREX_TYPES -------------------------------------------------------------------------------- /test/Conversion/LLHToArith/llh_to_arith.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --convert-llh-to-arith %s| FileCheck %s 2 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --convert-llh-to-arith /home/lfr/LLCompiler/test/Conversion/LLHToArith/llh_to_arith.mlir 3 | 4 | // CHECK-LABEL: constant 5 | func.func @constant() ->() attributes {entrance}{ 6 | // CHECK: llh.constant 7 | %98 = "llh.constant"() <{value = dense<1.000000e+00> : tensor<384xf32>}> : () -> tensor<384xf32> 8 | // CHECK: arith.constant 9 | %0 = "llh.constant"() <{value = 3 : i64}> : () -> i64 10 | return 11 | } 12 | // ----- 13 | 14 | // CHECK-LABEL: binary 15 | func.func @binary() ->(i64) attributes {entrance}{ 16 | // CHECK: arith.constant 17 | %98 = "llh.constant"() <{value = 3 : i64}> : () -> i64 18 | // CHECK: arith.muli 19 | %106 = "llh.mul"(%98, %98): (i64, i64) -> i64 20 | return %106: i64 21 | } -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/TosaExTypes.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef LLH_TOSAEX_TYPES 16 | #define LLH_TOSAEX_TYPES 17 | 18 | include "mlir/IR/OpBase.td" 19 | include "llcompiler/Dialect/TosaExtension/IR/TosaExDialect.td" 20 | 21 | #endif // LLH_TOSAEX_TYPES -------------------------------------------------------------------------------- /src/Frontend/Onnx/OnnxBuilder.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | #include "llcompiler/Frontend/Onnx/OnnxBuilder.h" 15 | #include "mlir/IR/MLIRContext.h" 16 | 17 | 18 | namespace llc::front { 19 | OnnxBuilder::OnnxBuilder(mlir::MLIRContext* context) : Builder(context) {} 20 | }; // namespace llc::front 21 | -------------------------------------------------------------------------------- /src/Dialect/TosaExtension/IR/TosaExOps.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExDialect.h" 16 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExOps.h" 17 | 18 | #define GET_OP_CLASSES 19 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExOps.cpp.inc" 20 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/Transforms/BufferizableOpInterfaceImpl.h: -------------------------------------------------------------------------------- 1 | //===- BufferizableOpInterfaceImpl.h - Impl. of BufferizableOpInterface ---===// 2 | // 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 | // See https://llvm.org/LICENSE.txt for license information. 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 | // 7 | //===----------------------------------------------------------------------===// 8 | 9 | #ifndef INCLUDE_LLCOMPILER_DIALECT_LLH_TRANSFORMS_BUFFERIZABLEOPINTERFACEIMPL_H_ 10 | #define INCLUDE_LLCOMPILER_DIALECT_LLH_TRANSFORMS_BUFFERIZABLEOPINTERFACEIMPL_H_ 11 | 12 | namespace mlir { 13 | class DialectRegistry; 14 | 15 | namespace llh{ 16 | void registerBufferizableOpInterfaceExternalModels(DialectRegistry ®istry); 17 | } // namespace cf 18 | } // namespace mlir 19 | 20 | #endif // INCLUDE_LLCOMPILER_DIALECT_LLH_TRANSFORMS_BUFFERIZABLEOPINTERFACEIMPL_H_ 21 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS TosaExOps.td) 2 | mlir_tablegen(TosaExEunms.h.inc -gen-enum-decls) 3 | mlir_tablegen(TosaExEunms.cpp.inc -gen-enum-defs) 4 | mlir_tablegen(TosaExTypes.h.inc -gen-typedef-decls) 5 | mlir_tablegen(TosaExTypes.cpp.inc -gen-typedef-defs) 6 | mlir_tablegen(TosaExAttrs.h.inc -gen-attrdef-decls) 7 | mlir_tablegen(TosaExAttrs.cpp.inc -gen-attrdef-defs) 8 | mlir_tablegen(TosaExOps.h.inc -gen-op-decls) 9 | mlir_tablegen(TosaExOps.cpp.inc -gen-op-defs) 10 | mlir_tablegen(TosaExDialect.h.inc -gen-dialect-decls -dialect=tosa_ex) 11 | mlir_tablegen(TosaExDialect.cpp.inc -gen-dialect-defs -dialect=tosa_ex) 12 | add_public_tablegen_target(LLCTosaExDialectIncGen) 13 | add_dependencies(mlir-headers LLCTosaExDialectIncGen) 14 | 15 | add_mlir_doc(TosaExDialect TosaExDialect TosaExtension/ -gen-dialect-doc) 16 | add_mlir_doc(TosaExOps TosaExOps TosaExtension/ -gen-op-doc) -------------------------------------------------------------------------------- /doc/其他/异构混训要点速记.md: -------------------------------------------------------------------------------- 1 | # 异构混合训练速记 2 | 3 | 某家厂商的的卡供应(英伟达)因为各种原因[不可描述]不满足大模型训练任务的算力需求。因此需要多种不同类型的芯片组成集群来进行大模型的训练。 4 | 5 | ## 难点 6 | 7 | 1. 多种类型的卡由于架构设计不同,计算性能各不相同。 8 | 通常来说,训练任务会被性能最差的卡拖累。 9 | 解决方案:负载均衡 10 | 2. 多卡之间通信困难。 11 | 芯片厂商的通讯库各不相同且不透明。导致无法实现不同芯片的通讯。 12 | 解决方案: 13 | 1. 不同芯片组成各自的小集群。小集群内部用芯片自己的通信协议,小集群与小集群之间用标准通用通信协议。[小集群之间如何配比?] 14 | 2. 借助CPU作为中间节点来进行通讯。[引入太多CPU与设备的数据拷贝] 15 | 3. 将某一个通讯库作为通信协议插入卡与卡之间进行通讯。[需要各个厂商认可、适配和优化] (无问芯穹 IHCCM) 16 | 3. 精度问题且难以定位 17 | 芯片厂商的内部计算单元对精度的支持度和实现不一致,导致模型训练精度有偏差。尤其是用fp16混精计算。 18 | 解决方案:用bf16 或者 精度敏感的计算用f32 19 | 4. 不同芯片算力不同,如何进行高效的配比 20 | 根据实际运行时间[芯片算力计算标准不一致,内部优化不一样,导致实际计算和理论值差距大],进行配比。 21 | 配比方法: 根据经验暴力搜。。。。。 22 | 23 | ## 训练方式 24 | 25 | 1. 流水线并行 (模型分层切)[好像更好一点] 26 | 1. 不同的卡计算的时候对形状敏感,所以从芯片A传输到芯片B时候如果能够支持Slice操作能够加快训练速度。 27 | 2. 数据并行 (batch维度切) [容易被性能最差的卡拖累.] 28 | 29 | ## 评价指标 30 | 吞吐(A,B)/吞吐(A)+吞吐(B) 31 | 32 | -------------------------------------------------------------------------------- /tools/llcompiler/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | # #---------- compiler ----------## 2 | get_property(dialect_libs GLOBAL PROPERTY MLIR_DIALECT_LIBS) 3 | get_property(conversion_libs GLOBAL PROPERTY MLIR_CONVERSION_LIBS) 4 | get_property(extension_libs GLOBAL PROPERTY MLIR_EXTENSION_LIBS) 5 | get_property(translation_libs GLOBAL PROPERTY MLIR_TRANSLATION_LIBS) 6 | llcompiler_add_executable(llcompiler 7 | "${CMAKE_CURRENT_SOURCE_DIR}/llcompilercpp" 8 | 9 | ADDITIONAL_INCLUDE_DIRS 10 | ${ONNX_INCLUDE_DIR} 11 | ${ONNX_INCLUDE_GENERATE_DIR} 12 | ${PROTOBUF_INCLUDE_DIR} 13 | ${ABSL_INCLUDE_DIR} 14 | 15 | LINKS 16 | MLIRAnalysis 17 | MLIRBuiltinToLLVMIRTranslation 18 | MLIRExecutionEngine 19 | MLIRIR 20 | MLIRJitRunner 21 | MLIRLLVMDialect 22 | MLIRLLVMToLLVMIRTranslation 23 | MLIRToLLVMIRTranslationRegistration 24 | MLIRParser 25 | MLIRTargetLLVMIRExport 26 | MLIRSupport 27 | LLVMX86AsmParser 28 | ) 29 | -------------------------------------------------------------------------------- /third_party/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | if(MSVC) 2 | add_compile_options($<$:/Wall>) 3 | else() 4 | add_compile_options($<$:-w>) 5 | endif() 6 | 7 | if(BUILD_LLCOMPILER_TEST) 8 | # build_googletest("${CMAKE_CURRENT_SOURCE_DIR}/googletest") 9 | endif() 10 | 11 | #build_abseil("${CMAKE_CURRENT_SOURCE_DIR}/abseil-cpp") 12 | build_spdlog("${CMAKE_CURRENT_SOURCE_DIR}/spdlog") 13 | #build_onnx("${CMAKE_CURRENT_SOURCE_DIR}/onnx") 14 | build_llvm("${CMAKE_CURRENT_SOURCE_DIR}/llvm-project/llvm") 15 | build_pybind11("${CMAKE_CURRENT_SOURCE_DIR}/pybind11") 16 | #unkown error in ../mlir-hlo/stablehlo 17 | build_stablehlo("${CMAKE_CURRENT_SOURCE_DIR}/stablehlo" "${CMAKE_CURRENT_SOURCE_DIR}/llvm-project/llvm") 18 | # build_hlo("${CMAKE_CURRENT_SOURCE_DIR}/mlir-hlo" "${CMAKE_CURRENT_SOURCE_DIR}/llvm-project/llvm") 19 | build_symengine("${CMAKE_CURRENT_SOURCE_DIR}/symengine") 20 | #build_ginac("${CMAKE_CURRENT_SOURCE_DIR}/ginac") 21 | -------------------------------------------------------------------------------- /src/Dialect/Utility/Benefit.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #include "llcompiler/Dialect/Utility/Benefit.h" 15 | namespace mlir::llh { 16 | const size_t ReshapeBenefit = 100; 17 | const size_t BroadcastBenefit = 99; 18 | const size_t RefineOpBenefit = 98; 19 | const size_t SinkOpBenfit = 97; 20 | const size_t RemoveBenfit = 0; 21 | } // namespace mlir::llh -------------------------------------------------------------------------------- /test/python/ops/where.py: -------------------------------------------------------------------------------- 1 | # RUN: python %s| FileCheck %s 2 | # CHECK: static model inference are correct. 3 | # CHECK: static model training are correct. 4 | # CHECK: dynamic model inference are correct. 5 | # CHECK: dynamic model training are correct. 6 | import llcompiler.compiler as LLC 7 | import os.path 8 | import subprocess 9 | import torch 10 | import torchvision 11 | import torch.nn as nn 12 | import torch.nn.functional as F 13 | from torch._dynamo.backends.common import aot_autograd 14 | import torch.fx 15 | from llcompiler.utility import run_time 16 | import onnx 17 | import torchgen 18 | import torch._dynamo 19 | import os 20 | from llcompiler.test_models import * 21 | 22 | 23 | if __name__ == "__main__": 24 | model = Where() 25 | input = [ 26 | torch.ones(1000, 224, dtype=torch.bool, device="cpu"), 27 | torch.randn(1000, 224, device="cpu"), 28 | torch.randn(1000, 224, device="cpu"), 29 | ] 30 | check_model(model, input) 31 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Ops.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_OPS_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_OPS_H_ 18 | 19 | #define GET_OP_CLASSES 20 | #include "llcompiler/Dialect/IRExtension/IR/Ops.h.inc" 21 | #endif // INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_OPS_H_ 22 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/Utils/Broadcast.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_LLH_UTILS_BROADCAST_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_LLH_UTILS_BROADCAST_H_ 18 | #include "mlir/IR/Operation.h" 19 | namespace mlir::llh { 20 | void checkBroadcast(Operation* op); 21 | 22 | } 23 | #endif // INCLUDE_LLCOMPILER_DIALECT_LLH_UTILS_BROADCAST_H_ 24 | -------------------------------------------------------------------------------- /include/llcompiler/Frontend/Core/Macro.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #ifndef INCLUDE_LLCOMPILER_FRONTEND_CORE_MACRO_H_ 16 | #define INCLUDE_LLCOMPILER_FRONTEND_CORE_MACRO_H_ 17 | 18 | // Constraint function formatting 19 | #define LLC_MLIR_GEN(Return, ...) \ 20 | Return mlir_gen(mlir::OpBuilder* builder, __VA_ARGS__) const; 21 | 22 | #endif // INCLUDE_LLCOMPILER_FRONTEND_CORE_MACRO_H_ 23 | -------------------------------------------------------------------------------- /tools/llc-translate/llc-translate.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #include "mlir/InitAllTranslations.h" 15 | #include "mlir/Support/LLVM.h" 16 | #include "mlir/Tools/mlir-translate/MlirTranslateMain.h" 17 | 18 | using namespace mlir; 19 | 20 | int main(int argc, char **argv) { 21 | registerAllTranslations(); 22 | return failed(mlirTranslateMain(argc, argv, "MLIR Translation Testing Tool")); 23 | } 24 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/Utility/Tool.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #ifndef INCLUDE_LLCOMPILER_DIALECT_UTILITY_TOOL_H_ 16 | #define INCLUDE_LLCOMPILER_DIALECT_UTILITY_TOOL_H_ 17 | #include "mlir/IR/BuiltinAttributes.h" 18 | namespace llc { 19 | mlir::DenseElementsAttr genDenseElementsFromArrayAttr( 20 | mlir::DenseI64ArrayAttr attr); 21 | } 22 | #endif // INCLUDE_LLCOMPILER_DIALECT_UTILITY_TOOL_H_ 23 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Types.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_TYPES_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_TYPES_H_ 18 | 19 | #include "mlir/IR/Dialect.h" 20 | 21 | #define GET_TYPEDEF_CLASSES 22 | #include "llcompiler/Dialect/IRExtension/IR/Types.h.inc" 23 | #endif // INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_TYPES_H_ 24 | -------------------------------------------------------------------------------- /include/llcompiler/TransformLibrary/tensor_include.mlir: -------------------------------------------------------------------------------- 1 | module @tensor_inlcude attributes { transform.with_named_sequence } { 2 | 3 | transform.named_sequence @tensor_basic_opt(%module: !transform.any_op {transform.readonly}) { 4 | %funcs = transform.structured.match ops{["func.func"]} in %module : (!transform.any_op) -> !transform.any_op 5 | transform.apply_patterns to %funcs { 6 | transform.apply_patterns.tensor.decompose_concat 7 | transform.apply_patterns.tensor.fold_tensor_empty 8 | transform.apply_patterns.tensor.fold_tensor_subset_ops 9 | transform.apply_patterns.tensor.rewrite_as_constant aggressive 10 | } : !transform.any_op 11 | transform.bufferization.buffer_loop_hoisting %funcs : !transform.any_op 12 | transform.bufferization.eliminate_empty_tensors %funcs : !transform.any_op 13 | transform.apply_patterns to %funcs { 14 | transform.apply_patterns.canonicalization 15 | } : !transform.any_op 16 | transform.apply_cse to %funcs : !transform.any_op 17 | transform.yield 18 | } 19 | 20 | } -------------------------------------------------------------------------------- /llcompiler/importer/fx_op_translate/drop.py: -------------------------------------------------------------------------------- 1 | from ..fx_translate import ( 2 | TORCH_FUNCTION_TRANSLATE, 3 | torch_fake_or_mate_tensor_translate, 4 | get_result_type, 5 | get_arg_value, 6 | commond_build_op, 7 | _expand_to_2_if_int, 8 | SPECIAL_RESULT_FAKE_INDEX_MAP, 9 | SPECIAL_GETITEM_IS_OPERAND_MAP, 10 | ) 11 | from xdsl.dialects.builtin import ( 12 | TensorType, 13 | IntegerType, 14 | i64, 15 | i32, 16 | i16, 17 | i1, 18 | f16, 19 | f32, 20 | f64, 21 | DYNAMIC_INDEX, 22 | DenseArrayBase, 23 | IntegerAttr, 24 | BoolAttr, 25 | DenseIntOrFPElementsAttr, 26 | FloatAttr, 27 | ) 28 | from ...dialect.llh_utility import build_llh_transpose, build_llh_constant 29 | import torch._ops as op 30 | import torch.fx 31 | import torch.nn.functional as F 32 | from xdsl.ir import SSAValue, Operation, OpResult, Attribute, Mapping, Block 33 | from torch._subclasses.fake_tensor import FakeTensor 34 | from ...dialect.llh import TorchSymbolicIntOp, DropOp 35 | from xdsl.irdl import IRDLOperation 36 | -------------------------------------------------------------------------------- /llcompiler/utility/utility.py: -------------------------------------------------------------------------------- 1 | from _collections_abc import dict_keys 2 | import time 3 | from typing import Any 4 | 5 | 6 | def run_time(func): 7 | def inner(*args, **kwargs): 8 | start_time = time.time() 9 | res = func(*args, **kwargs) 10 | end_time = time.time() 11 | result = end_time - start_time 12 | print(func.__name__, ": time is %.3fs" % result) 13 | return res 14 | 15 | return inner 16 | 17 | 18 | class Dict_Registry(dict): 19 | def __init__(self, *args, **kwargs): 20 | super().__init__(*args, **kwargs) 21 | 22 | def __call__(self, *keys) -> Any: 23 | return self.register(*keys) 24 | 25 | def register(self, *keys): 26 | def add_callable(func): 27 | if not callable(func): 28 | raise ValueError("Value must be callable") 29 | for key in keys: 30 | if key in self.keys(): 31 | raise ValueError(f"Key '{key}' already registered") 32 | self[key] = func 33 | 34 | return add_callable 35 | -------------------------------------------------------------------------------- /src/Frontend/Core/Importer.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #include 15 | 16 | #include "llcompiler/Frontend/Core/Base.h" 17 | #include "llcompiler/Frontend/Core/Importer.h" 18 | #include "mlir/IR/MLIRContext.h" 19 | 20 | namespace llc::front { 21 | 22 | Importer::Importer(Builder *builder, const FrontEndOption &option) 23 | : builder_(builder), option_(option) {} 24 | 25 | Importer::~Importer() {} 26 | 27 | }; // namespace llc::front 28 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/SymbolInfer/Utils/InferSymbol.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_LLH_SYMBOLINFER_UTILS_INFERSYMBOL_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_LLH_SYMBOLINFER_UTILS_INFERSYMBOL_H_ 18 | #include "mlir/IR/Operation.h" 19 | namespace mlir::llh { 20 | void checkAndInferSymbol(mlir::Operation* op); 21 | 22 | } 23 | #endif // INCLUDE_LLCOMPILER_DIALECT_LLH_SYMBOLINFER_UTILS_INFERSYMBOL_H_ 24 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/Utility/TensorPred.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef TENSORPRED 15 | #define TENSORPRED 16 | include "mlir/IR/Constraints.td" 17 | class Tensor_Rank_Is: 18 | Constraint< And<[ CPred<"::llvm::cast<::mlir::RankedTensorType>($_self).getRank() == " # n>] 19 | >,"tensor rank is " # n>; 20 | def Is_2D_Tensor :Tensor_Rank_Is<2>; 21 | def Is_3D_Tensor :Tensor_Rank_Is<3>; 22 | 23 | 24 | #endif // TENSORPRED -------------------------------------------------------------------------------- /cmake/third_party/build_hlo.cmake: -------------------------------------------------------------------------------- 1 | function(build_hlo lib_path llvm_path) 2 | message("******************************************") 3 | message("************* build hlo ***************") 4 | message("******************************************") 5 | set(LLVM_MAIN_SRC_DIR ${llvm_path}) 6 | set(LLVM_CMAKE_DIR "${LLCOMPILER_GENERATE_INCLUDE_DIR}/../third_party/llvm-project/llvm/lib/cmake/llvm") 7 | set(MLIR_CMAKE_DIR "${LLCOMPILER_GENERATE_INCLUDE_DIR}/../lib/cmake/mlir") 8 | list(APPEND CMAKE_MODULE_PATH "${MLIR_CMAKE_DIR}") 9 | list(APPEND CMAKE_MODULE_PATH "${LLVM_CMAKE_DIR}") 10 | set(MHLO_BUILD_EMBEDDED ON) 11 | set(MHLO_EXTERNAL_PROJECT_BUILD ON) 12 | include_directories(${LLVM_INCLUDE_DIR}) 13 | include_directories(${LLVM_INCLDE_GENERATE_DIR}) 14 | include_directories(${MLIR_INCLUDE_DIR}) 15 | include_directories(${MLIR_INCLUDE_GENERATE_DIR}) 16 | include_directories(${STABLEHLO_INCLUDE_DIR}) 17 | include_directories(${STABLEHLO_INCLUDE_GENERATE_DIR}) 18 | add_subdirectory(${lib_path}/stablehlo) 19 | endfunction(build_hlo) -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/IR/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | set(LLVM_TARGET_DEFINITIONS LLHOps.td) 2 | mlir_tablegen(LLHEunms.h.inc -gen-enum-decls) 3 | mlir_tablegen(LLHEunms.cpp.inc -gen-enum-defs) 4 | mlir_tablegen(LLHTypes.h.inc -gen-typedef-decls) 5 | mlir_tablegen(LLHTypes.cpp.inc -gen-typedef-defs) 6 | mlir_tablegen(LLHAttrs.h.inc -gen-attrdef-decls) 7 | mlir_tablegen(LLHAttrs.cpp.inc -gen-attrdef-defs) 8 | mlir_tablegen(LLHOps.h.inc -gen-op-decls) 9 | mlir_tablegen(LLHOps.cpp.inc -gen-op-defs) 10 | mlir_tablegen(LLHDialect.h.inc -gen-dialect-decls -dialect=llh) 11 | mlir_tablegen(LLHDialect.cpp.inc -gen-dialect-defs -dialect=llh) 12 | add_public_tablegen_target(LLCLLHDialectIncGen) 13 | add_dependencies(mlir-headers LLCLLHDialectIncGen) 14 | add_dependencies(LLCLLHDialectIncGen MLIRSymbolShapeOpInterfacesIncGen) 15 | 16 | set(LLVM_TARGET_DEFINITIONS LLHCanonicalize.td) 17 | mlir_tablegen(LLHCanonicalize.inc -gen-rewriters) 18 | add_public_tablegen_target(LLHCanonicalizeIncGen) 19 | 20 | add_mlir_doc(LLHDialect LLHDialect LLH/ -gen-dialect-doc) 21 | add_mlir_doc(LLHOps LLHOps LLH/ -gen-op-doc) 22 | -------------------------------------------------------------------------------- /main.cpp: -------------------------------------------------------------------------------- 1 | 2 | 3 | #include 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | 14 | #include "llcompiler/Compiler/CompileOption.h" 15 | #include "llcompiler/Compiler/Compiler.h" 16 | #include "llcompiler/Compiler/Execution.h" 17 | using namespace std; 18 | int main() { 19 | std::string mlir_module_file = "/home/lfr/LLCompiler/module.mlir"; 20 | auto options = llc::compiler::CompileOptions(); 21 | options.setLogRoot("/home/lfr/LLCompiler/ir_tree/test"); 22 | options.setLogLevel("debug"); 23 | options.setMode("training"); 24 | options.setTarget("x86_64"); 25 | options.setPipeline("transform"); 26 | options.setCpu("tigerlake"); 27 | options.setMtriple("x86_64-linux-gnu"); 28 | std::cout<<"a"; 29 | auto compiler = llc::compiler::LLCCompiler(); 30 | auto so_file = compiler.generateSharedLibFromMLIRFile(mlir_module_file, options); 31 | auto executor = llc::compiler::Execution(); 32 | executor.load(so_file); 33 | 34 | 35 | } -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Dialect.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_DIALECT_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_DIALECT_H_ 18 | 19 | #include "mlir/IR/Dialect.h" 20 | 21 | #define LLCOMPILER_MACRO_FOR_FIX_HEAD 22 | #include "llcompiler/Dialect/IRExtension/IR/Dialect.h.inc" 23 | #undef LLCOMPILER_MACRO_FOR_FIX_HEAD 24 | #endif // INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_DIALECT_H_ 25 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Attrs.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_ATTRS_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_ATTRS_H_ 18 | 19 | #include "mlir/IR/Attributes.h" 20 | #include "llcompiler/Dialect/IRExtension/IR/Enums.h" 21 | #define GET_ATTRDEF_CLASSES 22 | #include "llcompiler/Dialect/IRExtension/IR/Attrs.h.inc" 23 | 24 | #endif // INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_ATTRS_H_ 25 | 26 | -------------------------------------------------------------------------------- /llcompiler/importer/fx_op_translate/layer_norm.py: -------------------------------------------------------------------------------- 1 | from ..fx_translate import ( 2 | TORCH_FUNCTION_TRANSLATE, 3 | 4 | torch_fake_or_mate_tensor_translate, 5 | get_result_type, 6 | get_arg_value, 7 | commond_build_op, 8 | _expand_to_2_if_int, 9 | 10 | SPECIAL_RESULT_FAKE_INDEX_MAP, 11 | SPECIAL_GETITEM_IS_OPERAND_MAP, 12 | ) 13 | from xdsl.dialects.builtin import ( 14 | TensorType, 15 | IntegerType, 16 | i64, 17 | i32, 18 | i16, 19 | i1, 20 | f16, 21 | f32, 22 | f64, 23 | DYNAMIC_INDEX, 24 | DenseArrayBase, 25 | IntegerAttr, 26 | BoolAttr, 27 | DenseIntOrFPElementsAttr, 28 | FloatAttr, 29 | ) 30 | from ...dialect.llh_utility import build_llh_transpose, build_llh_constant 31 | import torch._ops as op 32 | import torch.fx 33 | import torch.nn.functional as F 34 | from xdsl.ir import SSAValue, Operation, OpResult, Attribute, Mapping, Block 35 | from torch._subclasses.fake_tensor import FakeTensor 36 | from ...dialect.llh import DimOp, TorchSymbolicIntOp, LayerNormOp 37 | from xdsl.irdl import IRDLOperation 38 | 39 | 40 | -------------------------------------------------------------------------------- /src/Dialect/LLH/Utils/Broadcast.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #include "llcompiler/Dialect/LLH/IR/LLHOps.h" 17 | #include "llcompiler/Dialect/LLH/Utils/Broadcast.h" 18 | namespace mlir::llh { 19 | 20 | void checkBroadcast(Operation* op) { 21 | auto broadcast_op = 22 | llvm::dyn_cast_or_null(op); 23 | if (broadcast_op) { 24 | broadcast_op.reshapeAndBrodcast(); 25 | return; 26 | } 27 | return; 28 | } 29 | } // namespace mlir::llh 30 | -------------------------------------------------------------------------------- /test/Dialect/LLH/ops.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt %s | llc-opt | FileCheck %s 2 | // RUN: llc-opt %s --mlir-print-op-generic | llc-opt | FileCheck %s 3 | 4 | // CHECK-LABEL: llh.symbolic_int 5 | "llh.symbolic_int"() <{sym_name = "c1000"}> : () -> () 6 | 7 | // CHECK-LABEL: torch_symbolic_int 8 | func.func @torch_symbolic_int() -> () { 9 | %6 = "llh.torch_symbolic_int"() <{sym_name = "s0"}> : () -> i64 10 | return 11 | } 12 | 13 | // CHECK-LABEL: aot 14 | func.func @aot(%arg0: i64, %arg1: i64, %arg2: tensor) ->(){ 15 | %1 = "llh.aot"(%arg0) <{name = "aot1"}> : (i64) -> i64 16 | %2 = "llh.aot"(%arg1,%arg2) <{name = "aot2"}> : (i64,tensor) -> tensor 17 | %3 = "llh.aot"() <{name = "aot3"}> : () -> i64 18 | %4 = "llh.aot"() <{name = "aot4"}> : () -> i64 19 | %5 = "llh.aot"() <{name = "aot5"}> : () -> i64 20 | return 21 | } 22 | 23 | // CHECK-LABEL: aot 24 | func.func @unary_op(%arg0: tensor) ->(){ 25 | // llh.abs 26 | %1 = "llh.abs"(%arg0): (tensor) -> tensor 27 | return 28 | } 29 | 30 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/IR/LLHAttrs.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_LLH_IR_LLHATTRS_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_LLH_IR_LLHATTRS_H_ 18 | #include "llcompiler/Dialect/LLH/IR/LLHEnums.h" 19 | #include "mlir/IR/Attributes.h" 20 | #include "mlir/IR/BuiltinAttributes.h" 21 | #define GET_ATTRDEF_CLASSES 22 | #include "llcompiler/Dialect/LLH/IR/LLHAttrs.h.inc" 23 | namespace mlir::llh {} 24 | 25 | #endif // INCLUDE_LLCOMPILER_DIALECT_LLH_IR_LLHATTRS_H_ 26 | -------------------------------------------------------------------------------- /src/Frontend/Core/Base.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #include 15 | 16 | #include "llcompiler/Frontend/Core/Base.h" 17 | 18 | namespace llc::front { 19 | const int64_t ONNX_ADAPTED_VERSION = 22; 20 | 21 | const char *frontend_type_to_str(const FRONTEND_TYPE type) { 22 | switch (type) { 23 | case FRONTEND_TYPE::ONNX_FILE: 24 | return "onnx_file"; 25 | case FRONTEND_TYPE::MLIR_FILE: 26 | return "mlir_file"; 27 | } 28 | return "unimplemented"; 29 | } 30 | 31 | } // namespace llc::front 32 | -------------------------------------------------------------------------------- /include/llcompiler/Frontend/Onnx/OnnxBuilder.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef INCLUDE_LLCOMPILER_FRONTEND_ONNX_ONNXBUILDER_H_ 15 | #define INCLUDE_LLCOMPILER_FRONTEND_ONNX_ONNXBUILDER_H_ 16 | #include "llcompiler/Frontend/Core/Builder.h" 17 | #include "mlir/IR/MLIRContext.h" 18 | 19 | namespace llc::front { 20 | class OnnxBuilder : public Builder { 21 | public: 22 | explicit OnnxBuilder(mlir::MLIRContext* context); 23 | }; 24 | } // namespace llc::front 25 | #endif // INCLUDE_LLCOMPILER_FRONTEND_ONNX_ONNXBUILDER_H_ 26 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IndexExtension/Transforms/Passes.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #ifndef INCLUDE_LLCOMPILER_DIALECT_INDEXEXTENSION_TRANSFORMS_PASSES_TD_ 16 | #define INCLUDE_LLCOMPILER_DIALECT_INDEXEXTENSION_TRANSFORMS_PASSES_TD_ 17 | 18 | include "mlir/Pass/PassBase.td" 19 | 20 | def FoldIndexCastPass: Pass<"fold-index-cast","ModuleOp"> { 21 | let summary = "折叠掉index.cast"; 22 | let description = [{ 23 | }]; 24 | let dependentDialects = [ "mlir::index::IndexDialect"]; 25 | } 26 | 27 | 28 | #endif // LLH_PASS -------------------------------------------------------------------------------- /include/llcompiler/Interfaces/SymbolShapeOpInterfaces.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_INTERFACES_SYMBOLSHAPEOPINTERFACES_H_ 17 | #define INCLUDE_LLCOMPILER_INTERFACES_SYMBOLSHAPEOPINTERFACES_H_ 18 | 19 | #include "mlir/IR/AffineMap.h" 20 | #include "mlir/IR/BuiltinTypes.h" 21 | #include "mlir/IR/OpDefinition.h" 22 | #define FIX_HEADER 23 | #include "llcompiler/Interfaces/SymbolShapeOpInterfaces.h.inc" 24 | #undef FIX_HEADER 25 | #endif // INCLUDE_LLCOMPILER_INTERFACES_SYMBOLSHAPEOPINTERFACES_H_ 26 | -------------------------------------------------------------------------------- /check_code.py: -------------------------------------------------------------------------------- 1 | import os 2 | import glob 3 | 4 | root_path = path = os.getcwd() 5 | include_dir = os.path.join(root_path, "include") 6 | src_dir = os.path.join(root_path, "src") 7 | tools_dir = os.path.join(root_path, "tools") 8 | check_dirs = [include_dir, src_dir, tools_dir] 9 | cpp_files = [] 10 | h_files = [] 11 | for root in check_dirs: 12 | cpp_files += glob.glob(root + "/**/*.cpp", recursive=True) 13 | h_files += glob.glob(root + "/**/*.h", recursive=True) 14 | 15 | 16 | def cpplint(): 17 | cpplint_filters = [ 18 | "-runtime/references", 19 | "-build/namespaces", 20 | "-build/include", 21 | "-build/c++11" 22 | # "-whitespace/comments", 23 | # "-whitespace/indent", 24 | ] 25 | command = "cpplint " 26 | if len(cpplint_filters) != 0: 27 | command += "--filter=" 28 | for filter in cpplint_filters: 29 | command += "{},".format(filter) 30 | for file in cpp_files: 31 | os.system("{} {}".format(command, file)) 32 | for file in h_files: 33 | os.system("{} {}".format(command, file)) 34 | 35 | 36 | if __name__ == "__main__": 37 | cpplint() 38 | -------------------------------------------------------------------------------- /include/llcompiler/Interfaces/BraodcastableOpInterfaces.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_INTERFACES_BRAODCASTABLEOPINTERFACES_H_ 17 | #define INCLUDE_LLCOMPILER_INTERFACES_BRAODCASTABLEOPINTERFACES_H_ 18 | 19 | #include "mlir/IR/AffineMap.h" 20 | #include "mlir/IR/BuiltinTypes.h" 21 | #include "mlir/IR/OpDefinition.h" 22 | #define FIX_HEADER 23 | #include "llcompiler/Interfaces/BraodcastableOpInterfaces.h.inc" 24 | #undef FIX_HEADER 25 | #endif // INCLUDE_LLCOMPILER_INTERFACES_BRAODCASTABLEOPINTERFACES_H_ 26 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/TosaExOps.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef LLH_TOSAEX_OPS 15 | #define LLH_TOSAEX_OPS 16 | include "llcompiler/Dialect/TosaExtension/IR/TosaExTypes.td" 17 | include "llcompiler/Dialect/TosaExtension/IR/TosaExConstraints.td" 18 | include "llcompiler/Dialect/TosaExtension/IR/TosaExEnums.td" 19 | include "llcompiler/Dialect/TosaExtension/IR/TosaExInterfaces.td" 20 | include "mlir/Interfaces/InferTypeOpInterface.td" 21 | include "mlir/Interfaces/SideEffectInterfaces.td" 22 | 23 | #endif // LLH_TOSAEX_OPS 24 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/Transforms/Passes.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #ifndef LLH_PASS 16 | #define LLH_PASS 17 | include "mlir/Pass/PassBase.td" 18 | 19 | def TransformLayoutToNHWC : Pass<"transform-layout-to-nhwc","ModuleOp"> { 20 | let summary = "trans some llh op layout to nhwc for lowing to tosa"; 21 | let description = [{ 22 | }]; 23 | let constructor = "mlir::tosa_ex::createTransformLayoutToNHWCPass()"; 24 | let dependentDialects = [ "::mlir::tosa::TosaDialect"]; 25 | } 26 | 27 | 28 | 29 | #endif // LLH_PASS -------------------------------------------------------------------------------- /include/llcompiler/Dialect/Utility/Benefit.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #ifndef INCLUDE_LLCOMPILER_DIALECT_UTILITY_BENEFIT_H_ 16 | #define INCLUDE_LLCOMPILER_DIALECT_UTILITY_BENEFIT_H_ 17 | 18 | #include 19 | 20 | namespace mlir::llh { 21 | extern const size_t ReshapeBenefit; 22 | extern const size_t BroadcastBenefit; 23 | extern const size_t RefineOpBenefit; 24 | extern const size_t SinkOpBenfit; 25 | extern const size_t RemoveBenfit; 26 | } // namespace mlir::llh 27 | 28 | #endif // INCLUDE_LLCOMPILER_DIALECT_UTILITY_BENEFIT_H_ 29 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLVMExtension/Transforms/Passes.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #ifndef INCLUDE_LLCOMPILER_DIALECT_LLVMXTENSION_TRANSFORMS_PASSES_TD_ 16 | #define INCLUDE_LLCOMPILER_DIALECT_LLVMXTENSION_TRANSFORMS_PASSES_TD_ 17 | 18 | include "mlir/Pass/PassBase.td" 19 | 20 | def AdaptEntryParmsForEnginePass: Pass<"adapt-entry-parms-for-engine","ModuleOp"> { 21 | let summary = "调整入口函数的参数, 方便执行引擎运行"; 22 | let description = [{ 23 | }]; 24 | let dependentDialects = [ "::mlir::LLVM::LLVMDialect"]; 25 | } 26 | 27 | 28 | #endif // LLH_PASS -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToHLO/LLHToHLO.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOHLO_LLHTOHLO_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOHLO_LLHTOHLO_H_ 17 | #include 18 | namespace mlir { 19 | class MLIRContext; 20 | class TypeConverter; 21 | class Pass; 22 | class RewritePatternSet; 23 | class ConversionTarget; 24 | #define GEN_PASS_DECL_CONVERTLLHTOHLOPASS 25 | #include "llcompiler/Conversion/Passes.h.inc" 26 | 27 | } // namespace mlir 28 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOHLO_LLHTOHLO_H_ 29 | -------------------------------------------------------------------------------- /cmake/third_party/build_llvm.cmake: -------------------------------------------------------------------------------- 1 | function(build_llvm lib_path) 2 | message("******************************************") 3 | message("************* build llvm *****************") 4 | message("******************************************") 5 | set(LLVM_TARGETS_TO_BUILD 6 | 7 | # AArch64 8 | # AMDGPU 9 | ARM 10 | AVR 11 | BPF 12 | Hexagon 13 | Lanai 14 | LoongArch 15 | Mips 16 | MSP430 17 | NVPTX 18 | PowerPC 19 | RISCV 20 | 21 | # Sparc 22 | SystemZ 23 | VE 24 | WebAssembly 25 | X86 26 | XCore) 27 | set(LLVM_ENABLE_PROJECTS "mlir;clang") 28 | # bolt;clang;clang-tools-extra;compiler-rt;cross-project-tests;libc;libclc;lld;lldb;mlir;openmp;polly;pstl;flang. 29 | set(LLVM_ENABLE_RTTI ON) 30 | set(LLVM_ENABLE_ASSERTIONS ON) 31 | set(LLVM_BUILD_TOOLS ON) 32 | set(LLVM_INCLUDE_TESTS ON) 33 | set(LLVM_INCLUDE_UTILS ON) 34 | set(LLVM_INCLUDE_BENCHMARKS OFF) 35 | set(LLVM_INSTALL_TOOLCHAIN_ONLY OFF) 36 | set(MLIR_ENABLE_BINDINGS_PYTHON OFF) 37 | add_subdirectory(${lib_path}) 38 | endfunction(build_llvm) -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToMath/LLHToMath.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOMATH_LLHTOMATH_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOMATH_LLHTOMATH_H_ 17 | #include 18 | namespace mlir { 19 | class MLIRContext; 20 | class TypeConverter; 21 | class Pass; 22 | class RewritePatternSet; 23 | class ConversionTarget; 24 | #define GEN_PASS_DECL_CONVERTLLHTOMATHPASS 25 | #include "llcompiler/Conversion/Passes.h.inc" 26 | 27 | } // namespace mlir 28 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOMATH_LLHTOMATH_H_ 29 | -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToTosa/LLHToTosa.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOTOSA_LLHTOTOSA_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOTOSA_LLHTOTOSA_H_ 17 | #include 18 | namespace mlir { 19 | class MLIRContext; 20 | class TypeConverter; 21 | class Pass; 22 | class RewritePatternSet; 23 | class ConversionTarget; 24 | #define GEN_PASS_DECL_CONVERTLLHTOTOSAPASS 25 | #include "llcompiler/Conversion/Passes.h.inc" 26 | 27 | } // namespace mlir 28 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOTOSA_LLHTOTOSA_H_ 29 | -------------------------------------------------------------------------------- /include/llcompiler/Support/Enums.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_SUPPORT_ENUMS_H_ 17 | #define INCLUDE_LLCOMPILER_SUPPORT_ENUMS_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "llvm/ADT/StringRef.h" 23 | #include "llvm/ADT/StringSwitch.h" 24 | #include "llvm/Support/raw_ostream.h" 25 | #include "mlir/Support/LLVM.h" 26 | 27 | #define LLCOMPILER_MACRO_FOR_FIX_HEAD 28 | #include "llcompiler/Support/Eunms.h.inc" 29 | #undef LLCOMPILER_MACRO_FOR_FIX_HEAD 30 | #endif // INCLUDE_LLCOMPILER_SUPPORT_ENUMS_H_ 31 | 32 | -------------------------------------------------------------------------------- /test/Dialect/LLH/remove_redundant_ops.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --remove-redundant-ops %s| FileCheck %s 2 | 3 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --remove-redundant-ops /home/lfr/LLCompiler/test/Dialect/LLH/remove_redundant_ops.mlir 4 | 5 | 6 | #map1 = affine_map<()[s0, s1, s2] -> (s0, s1, s2, s1)> 7 | module attributes {builtin.gloabal_layout = #llh.Layout} { 8 | // CHECK-LABEL: replaceTorchSymbolicIntOp 9 | func.func @replaceTorchSymbolicIntOp(%arg0: tensor {func.input_symbol_0 = "s0", func.input_symbol_1 = "s1", func.input_symbol_2 = "s2", func.input_symbol_3 = "s2"}) ->() attributes {entrance}{ 10 | // CHECK-NOT: llh.torch_symbolic_int 11 | // CHECK-NOT: llh.symbolic_bind 12 | %3 = "llh.torch_symbolic_int"() <{sym_name = "s0"}> : () -> i64 13 | %4 = "llh.torch_symbolic_int"() <{sym_name = "s1"}> : () -> i64 14 | %5 = "llh.torch_symbolic_int"() <{sym_name = "s2"}> : () -> i64 15 | %6 = "llh.reshape"(%arg0, %3, %5, %4, %5) : (tensor, i64, i64, i64, i64) -> tensor 16 | "llh.symbolic_bind"(%6, %3, %5, %4) <{expressions = #map1}> : (tensor, i64, i64, i64) -> () 17 | return 18 | } 19 | } -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToFunc/LLHToFunc.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOFUNC_LLHTOFUNC_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOFUNC_LLHTOFUNC_H_ 17 | 18 | #include 19 | namespace mlir { 20 | class MLIRContext; 21 | class TypeConverter; 22 | class Pass; 23 | class RewritePatternSet; 24 | class ConversionTarget; 25 | #define GEN_PASS_DECL_CONVERTLLHTOFUNCPASS 26 | #include "llcompiler/Conversion/Passes.h.inc" 27 | 28 | } // namespace mlir 29 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOFUNC_LLHTOFUNC_H_ 30 | -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToShape/LLHToShape.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOSHAPE_LLHTOSHAPE_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOSHAPE_LLHTOSHAPE_H_ 17 | #include 18 | namespace mlir { 19 | class MLIRContext; 20 | class TypeConverter; 21 | class Pass; 22 | class RewritePatternSet; 23 | class ConversionTarget; 24 | #define GEN_PASS_DECL_CONVERTLLHTOSHAPEPASS 25 | #include "llcompiler/Conversion/Passes.h.inc" 26 | 27 | } // namespace mlir 28 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOSHAPE_LLHTOSHAPE_H_ 29 | -------------------------------------------------------------------------------- /include/llcompiler/Frontend/Core/Utility.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef INCLUDE_LLCOMPILER_FRONTEND_CORE_UTILITY_H_ 15 | #define INCLUDE_LLCOMPILER_FRONTEND_CORE_UTILITY_H_ 16 | /** 17 | * @file Utility.h 18 | * @brief utility function about import 19 | * @author 时光丶人爱 (1733535832@qq.com) 20 | * @version 1.0 21 | * @date 2024-07-01 22 | * 23 | * @copyright Copyright (c) 2024 时光丶人爱 24 | * 25 | */ 26 | 27 | #include "llcompiler/Frontend/Core/Base.h" 28 | 29 | namespace llc::front {} // namespace llc::front 30 | 31 | #endif // INCLUDE_LLCOMPILER_FRONTEND_CORE_UTILITY_H_ 32 | -------------------------------------------------------------------------------- /src/Conversion/LLHToHLO/LLHToHLO.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef LLH_LLH_TO_HLO 17 | #define LLH_LLH_TO_HLO 18 | 19 | include "llcompiler/Dialect/LLH/IR/LLHOps.td" 20 | include "llcompiler/Dialect/Utility/CommonUtility.td" 21 | include "mlir/IR/PatternBase.td" 22 | include "stablehlo/dialect/StablehloOps.td" 23 | 24 | def LLHTransposeOpToHLO : Pat<(LLH_TransposeOp:$res $input, $perms), 25 | (StableHLO_TransposeOp $input, $perms), 26 | []>; 27 | 28 | 29 | #endif // LLH_LLH_TO_HLO 30 | -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToArith/LLHToArith.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOARITH_LLHTOARITH_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOARITH_LLHTOARITH_H_ 17 | 18 | #include 19 | namespace mlir { 20 | class MLIRContext; 21 | class TypeConverter; 22 | class Pass; 23 | class RewritePatternSet; 24 | class ConversionTarget; 25 | #define GEN_PASS_DECL_CONVERTLLHTOARITHPASS 26 | #include "llcompiler/Conversion/Passes.h.inc" 27 | 28 | } // namespace mlir 29 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOARITH_LLHTOARITH_H_ 30 | -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToLinalg/LLHToLinalg.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOLINALG_LLHTOLINALG_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOLINALG_LLHTOLINALG_H_ 17 | #include 18 | namespace mlir { 19 | class MLIRContext; 20 | class TypeConverter; 21 | class Pass; 22 | class RewritePatternSet; 23 | class ConversionTarget; 24 | #define GEN_PASS_DECL_CONVERTLLHTOLINALGPASS 25 | #include "llcompiler/Conversion/Passes.h.inc" 26 | 27 | } // namespace mlir 28 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOLINALG_LLHTOLINALG_H_ 29 | -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToTensor/LLHToTensor.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOTENSOR_LLHTOTENSOR_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOTENSOR_LLHTOTENSOR_H_ 17 | #include 18 | namespace mlir { 19 | class MLIRContext; 20 | class TypeConverter; 21 | class Pass; 22 | class RewritePatternSet; 23 | class ConversionTarget; 24 | #define GEN_PASS_DECL_CONVERTLLHTOTENSORPASS 25 | #include "llcompiler/Conversion/Passes.h.inc" 26 | 27 | } // namespace mlir 28 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOTENSOR_LLHTOTENSOR_H_ 29 | -------------------------------------------------------------------------------- /test/Dialect/BufferizationExtension/alloc_to_arg.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --alloc-to-arg -allow-unregistered-dialect %s| FileCheck %s 2 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --alloc-to-arg -allow-unregistered-dialect /home/lfr/LLCompiler/test/Dialect/BufferizationExtension/alloc_to_arg.mlir 3 | 4 | func.func @main(%arg0: memref {bufferization.access = "read", func.input_symbol_0 = "s0", func.input_symbol_1 = "s1", func.input_symbol_2 = "s2", func.input_symbol_3 = "s2"}, %arg1: memref {bufferize.result}) attributes {entrance} { 5 | %c3 = arith.constant 3 : index 6 | %c2 = arith.constant 2 : index 7 | %c1 = arith.constant 1 : index 8 | %c0 = arith.constant 0 : index 9 | %dim = memref.dim %arg0, %c0 : memref 10 | %dim_2 = memref.dim %arg0, %c1 : memref 11 | %dim_3 = memref.dim %arg0, %c2 : memref 12 | %dim_4 = memref.dim %arg0, %c3 : memref 13 | %alloc = memref.alloc(%dim, %dim_2, %dim_3, %dim_4) {alignment = 64 : i64} : memref 14 | // CHECK-NOT: memref.copy 15 | memref.copy %alloc, %arg1 : memref to memref 16 | return 17 | } 18 | 19 | 20 | -------------------------------------------------------------------------------- /include/llcompiler/Compiler/ToolPath.h.in: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_LLCOMPILER_COMPILER_TOOLPATH_H_ 2 | #define INCLUDE_LLCOMPILER_COMPILER_TOOLPATH_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | 8 | namespace llc::compiler { 9 | 10 | namespace { 11 | static const std::string OptPath = "@LLCOMPILER_LLVM_BUILD_TOOLS_DIR@/opt"; 12 | static const std::string LlcPath = "@LLCOMPILER_LLVM_BUILD_TOOLS_DIR@/llc"; 13 | static const std::string LlcOptPath = "@LLCOMPILER_BUILD_RUNTIME_DIR@/llc-opt"; 14 | static const std::string LlcTranslatePath = 15 | "@LLCOMPILER_BUILD_RUNTIME_DIR@/llc-translate"; 16 | static const std::string CXXPath = "@CMAKE_CXX_COMPILER@"; 17 | } // namespace 18 | 19 | static const std::map toolPathMap = { 20 | {"opt", OptPath}, 21 | {"llc", LlcPath}, 22 | {"llc-opt", LlcOptPath}, 23 | {"llc-translate", LlcTranslatePath}, 24 | {"cxx", CXXPath}}; 25 | 26 | static const std::vector defaultLibDirs = { 27 | "@LLCOMPILER_BUILD_LIBRARY_DIR@", "@LLCOMPILER_LLVM_BUILD_LIBS_DIR@"}; 28 | 29 | static const std::vector defaultLib = {"mlir_c_runner_utils"}; 30 | } // namespace llc::compiler 31 | #endif // INCLUDE_LLCOMPILER_COMPILER_TOOLPATH_H_ 32 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLH/IR/LLHEnums.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_LLH_IR_LLHENUMS_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_LLH_IR_LLHENUMS_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "llvm/ADT/StringRef.h" 23 | #include "llvm/ADT/StringSwitch.h" 24 | #include "llvm/Support/raw_ostream.h" 25 | #include "mlir/Support/LLVM.h" 26 | 27 | #define LLCOMPILER_MACRO_FOR_FIX_HEAD 28 | #include "llcompiler/Dialect/LLH/IR/LLHEunms.h.inc" 29 | #undef LLCOMPILER_MACRO_FOR_FIX_HEAD 30 | #endif // INCLUDE_LLCOMPILER_DIALECT_LLH_IR_LLHENUMS_H_ 31 | 32 | -------------------------------------------------------------------------------- /src/Dialect/Utility/RewritePattern.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #include "llcompiler/Dialect/Utility/RewritePattern.h" 16 | 17 | #include "llcompiler/Dialect/LLH/Utils/Broadcast.h" 18 | #include "llcompiler/Dialect/LLH/SymbolInfer/Utils/InferSymbol.h" 19 | #include "llcompiler/Support/Logger.h" 20 | namespace mlir { 21 | // 不要再这个方法里面创建非symbolOp 22 | void LLHPatternRewriter::processWileBuildOperation(Operation *op) { 23 | llh::checkAndInferSymbol(op); 24 | // llh::checkBroadcast(op); 25 | } 26 | 27 | bool LLHPatternRewriter::canRecoverFromRewriteFailure() const { return false; } 28 | 29 | } // namespace mlir 30 | -------------------------------------------------------------------------------- /include/llcompiler/Conversion/LLHToHLO/LLHPreprocessingForHLO.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_LLHTOHLO_LLHPREPROCESSINGFORHLO_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_LLHTOHLO_LLHPREPROCESSINGFORHLO_H_ 17 | #include 18 | namespace mlir { 19 | class MLIRContext; 20 | class TypeConverter; 21 | class Pass; 22 | class RewritePatternSet; 23 | class ConversionTarget; 24 | #define GEN_PASS_DECL_LLHPREPROCESSINGFORHLOPASS 25 | #include "llcompiler/Conversion/Passes.h.inc" 26 | 27 | } // namespace mlir 28 | #endif // INCLUDE_LLCOMPILER_CONVERSION_LLHTOHLO_LLHPREPROCESSINGFORHLO_H_ 29 | -------------------------------------------------------------------------------- /include/llcompiler/Compiler/ToolPath.install.h.in: -------------------------------------------------------------------------------- 1 | #ifndef INCLUDE_LLCOMPILER_COMPILER_TOOLPATH_INSTALL_H_ 2 | #define INCLUDE_LLCOMPILER_COMPILER_TOOLPATH_INSTALL_H_ 3 | 4 | #include 5 | #include 6 | #include 7 | #include 8 | 9 | namespace llc::compiler { 10 | 11 | namespace { 12 | static const std::string OptPath = "@CMAKE_INSTALL_PREFIX@/bin/opt"; 13 | static const std::string LlcPath = "@CMAKE_INSTALL_PREFIX@/bin/llc"; 14 | static const std::string LlcOptPath = "@CMAKE_INSTALL_PREFIX@/bin/llc-opt"; 15 | static const std::string LlcTranslatePath = 16 | "@CMAKE_INSTALL_PREFIX@/bin/llc-translate"; 17 | static const std::string CXXPath = "@CMAKE_CXX_COMPILER@"; 18 | 19 | } // namespace 20 | 21 | static const std::map toolPathMap = { 22 | {"opt", OptPath}, 23 | {"llc", LlcPath}, 24 | {"llc-opt", LlcOptPath}, 25 | {"llc-translate", LlcTranslatePath}, 26 | {"cxx", CXXPath}}; 27 | 28 | static const std::vector defaultLibDirs = { 29 | "@CMAKE_INSTALL_PREFIX@/lib", 30 | }; 31 | 32 | static const std::vector defaultLib = {"mlir_c_runner_utils"}; 33 | } // namespace llc::compiler 34 | #endif // INCLUDE_LLCOMPILER_COMPILER_TOOLPATH_INSTALL_H_ 35 | -------------------------------------------------------------------------------- /test/Dialect/LLH/layout_canonicalize.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --canonicalize %s| FileCheck %s 2 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --canonicalize /home/lfr/LLCompiler/test/Dialect/LLH/layout_canonicalize.mlir 3 | 4 | module attributes {builtin.gloabal_layout = #llh.Layout} { 5 | // CHECK-LABEL: add_layout_attr 6 | func.func @add_layout_attr(%arg0: tensor) ->(tensor) attributes {entrance}{ 7 | %0 = "llh.weight"() <{weight_file = "xxx"}> : () -> tensor<64x3x7x7xf32> 8 | // CHECK: llh.conv 9 | // CHECK-SAME: layout = #llh.Layout 10 | // CHECK-SAME: weight_layout = #llh.Layout 11 | %124 = "llh.conv"(%arg0, %0) <{dilation = array, group = 1 : i64, kernel_shape = array, pad = array, stride = array}> : (tensor, tensor<64x3x7x7xf32>) -> tensor 12 | // CHECK: llh.max_pool 13 | // CHECK-SAME: layout = #llh.Layout 14 | %129 = "llh.max_pool"(%124) <{ceil_mode = false, dilation = array, kernel_shape = array, pad = array, stride = array}> : (tensor) -> tensor 15 | return %129: tensor 16 | } 17 | } -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Enums.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_ENUMS_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_ENUMS_H_ 18 | 19 | #include 20 | #include 21 | 22 | #include "llvm/ADT/StringRef.h" 23 | #include "llvm/ADT/StringSwitch.h" 24 | #include "llvm/Support/raw_ostream.h" 25 | #include "mlir/Support/LLVM.h" 26 | 27 | #define LLCOMPILER_MACRO_FOR_FIX_HEAD 28 | #include "llcompiler/Dialect/IRExtension/IR/Eunms.h.inc" 29 | #undef LLCOMPILER_MACRO_FOR_FIX_HEAD 30 | #endif // INCLUDE_LLCOMPILER_DIALECT_IREXTENSION_IR_ENUMS_H_ 31 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/LLVMExtension/Transforms/Passes.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_LLVMEXTENSION_TRANSFORMS_PASSES_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_LLVMEXTENSION_TRANSFORMS_PASSES_H_ 18 | 19 | #include 20 | 21 | #include "mlir/Dialect/LLVMIR/LLVMDialect.h" 22 | #include "mlir/Pass/Pass.h" 23 | 24 | namespace mlir::LLVM::ex { 25 | 26 | #define GEN_PASS_DECL 27 | #define GEN_PASS_REGISTRATION 28 | #include "llcompiler/Dialect/LLVMExtension/Transforms/Passes.h.inc" 29 | 30 | } // namespace mlir::LLVM::ex 31 | 32 | #endif // INCLUDE_LLCOMPILER_DIALECT_LLVMEXTENSION_TRANSFORMS_PASSES_H_ 33 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/TosaExDialect.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXDIALECT_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXDIALECT_H_ 18 | 19 | #include "mlir/Bytecode/BytecodeOpInterface.h" 20 | #include "mlir/Dialect/Quant/IR/Quant.h" 21 | #include "mlir/Dialect/Tensor/IR/Tensor.h" 22 | #include "mlir/IR/Dialect.h" 23 | 24 | #define LLCOMPILER_MACRO_FOR_FIX_HEAD 25 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExDialect.h.inc" 26 | #undef LLCOMPILER_MACRO_FOR_FIX_HEAD 27 | 28 | #endif // INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXDIALECT_H_ 29 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/BufferizationExtension/Transforms/Passes.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #ifndef INCLUDE_LLCOMPILER_DIALECT_BUFFERIZATIONEXTENSION_TRANSFORMS_PASSES_TD_ 16 | #define INCLUDE_LLCOMPILER_DIALECT_BUFFERIZATIONEXTENSION_TRANSFORMS_PASSES_TD_ 17 | 18 | include "mlir/Pass/PassBase.td" 19 | 20 | def AllocToArgPass: Pass<"alloc-to-arg","ModuleOp"> { 21 | let summary = "内存复用"; 22 | let description = [{ 23 | }]; 24 | let dependentDialects = [ "mlir::func::FuncDialect", 25 | "mlir::bufferization::BufferizationDialect"]; 26 | } 27 | 28 | 29 | #endif // INCLUDE_LLCOMPILER_DIALECT_BUFFERIZATIONEXTENSION_TRANSFORMS_PASSES_TD_ -------------------------------------------------------------------------------- /doc/记录/Ai编译器开发日记-LLVM IR.md: -------------------------------------------------------------------------------- 1 | # LLVM IR 2 | 3 | LLVM IR 是LLVM的一种中间表示,一般我们会将LLVM IR 之前的优化和编译称为前端,LLVM IR 之后的优化和编译成为后端。 4 | 5 | 使用llc 可以将llvm IR 转为汇编,然后再转为相应后端的汇编指令。 6 | 7 | 所以LLVM IR是和后端关联性不大的最后一段IR,之后的IR以及优化就和硬件的特性以及指令集强相关了。 8 | 9 | ## LLVM IR 开头信息 10 | 11 | 以下是LLVM IR 的开头的部分: 12 | 13 | ``` 14 | ; ModuleID = 'LLVMDialectModule' 15 | source_filename = "LLVMDialectModule" 16 | target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" 17 | target triple = "x86_64-unknown-linux-gnu" 18 | ``` 19 | 20 | 其中source_filename 会注明 IR 的来源,如果是从c文件编译而来,则是c文件的路径,因为IR 是从MLIR 一路翻译而来,默认是LLVMDialectModule。 21 | 22 | target datalayout 描述的是机器数据的内存布局,类型大小,对其方式等。 23 | 24 | 以 `"e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128" 为例,` 25 | 26 | 第一个字母e代表机器数据存储是小端的,如果是E的话就是大端的。 27 | 28 | 之后的m:``代表输出IR的格式。m:e 就是elf 格式的。 29 | 30 | px1:x2:x3代表地址空间x1指针大小为x2比特,并且以x3比特对齐。 31 | 32 | 之后的i64:64 代表整数i64以64比特对齐。fxx:xx同理。 33 | 34 | 后面的nx :x:x 表示机器原生支持的整数位宽。 35 | 36 | 最后的Sxxx 表示栈的对齐大小,S128就代表栈是128比特对齐的。 37 | 38 | 注:一般对齐方式的格式是i32:32:64,它代表i32类型的最小对齐是32比特,最大对齐是64比特,但是一般最大对齐和最小对齐是一样的,如i32:32:32,可以缩写为i32:32。上面所看到的target都是缩写的形式。 39 | 40 | Target triple 则是目标机器是什么,结构是这样的:`arch-verdor-system-env`。如 `x86_64-unknown-linux-gnu`表示机器的arch是x86的,未知供应商,操作系统是linux,环境是gun的。 41 | -------------------------------------------------------------------------------- /include/llcompiler/Conversion/TosaToLinalgExtension/TosaToLinalgExtension.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_TOSATOLINALGEXTENSION_TOSATOLINALGEXTENSION_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_TOSATOLINALGEXTENSION_TOSATOLINALGEXTENSION_H_ 17 | 18 | #include 19 | namespace mlir { 20 | class MLIRContext; 21 | class TypeConverter; 22 | class Pass; 23 | class RewritePatternSet; 24 | class ConversionTarget; 25 | #define GEN_PASS_DECL_CONVERTTOSATOLINALGEXTENSIONPASS 26 | #include "llcompiler/Conversion/Passes.h.inc" 27 | 28 | } // namespace mlir 29 | #endif // INCLUDE_LLCOMPILER_CONVERSION_TOSATOLINALGEXTENSION_TOSATOLINALGEXTENSION_H_ 30 | -------------------------------------------------------------------------------- /test/Dialect/LLVMExtension/adapt_entry_parms_for_engine.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --adapt-entry-parms-for-engine %s| FileCheck %s 2 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --adapt-entry-parms-for-engine /home/lfr/LLCompiler/test/Dialect/LLVMExtension/adapt_entry_parms_for_engine.mlir 3 | 4 | 5 | // CHECK: llvm.func @main(%arg0: !llvm.ptr) attributes {entrance} 6 | // CHECK-COUNT-23: llvm.load 7 | llvm.func @main(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i64, %arg3: i64, %arg4: i64, %arg5: i64, %arg6: i64, %arg7: i64, %arg8: i64, %arg9: i64, %arg10: i64, %arg11: !llvm.ptr, %arg12: !llvm.ptr, %arg13: i64, %arg14: i64, %arg15: i64, %arg16: i64, %arg17: i64, %arg18: i64, %arg19: i64, %arg20: i64, %arg21: i64) attributes {entrance} { 8 | llvm.return 9 | } 10 | // ----- 11 | // CHECK: llvm.func @main(%arg0: !llvm.ptr) attributes {entrance} 12 | // CHECK-COUNT-28: llvm.getelementptr 13 | llvm.func @main(%arg0: !llvm.ptr, %arg1: !llvm.ptr, %arg2: i64, %arg3: i64, %arg4: i64, %arg5: i64, %arg6: i64, %arg7: i64, %arg8: i64, %arg9: i64, %arg10: i64, %arg11: !llvm.ptr, %arg12: !llvm.ptr, %arg13: i64, %arg14: i64, %arg15: i64, %arg16: i64, %arg17: i64, %arg18: i64, %arg19: i64, %arg20: i64, %arg21: i64) attributes {entrance} { 14 | llvm.return 15 | } 16 | 17 | 18 | 19 | -------------------------------------------------------------------------------- /src/Dialect/LLH/IR/LLHCast.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #include "llcompiler/Dialect/LLH/IR/LLHOps.h" 15 | #include "llvm/Support/Casting.h" 16 | #include "mlir/IR/BuiltinTypes.h" 17 | 18 | namespace mlir::llh { 19 | 20 | bool SymbolicCastOp::areCastCompatible(TypeRange inputs, TypeRange outputs) { 21 | if (inputs.size() != 1 || outputs.size() != 1) return false; 22 | RankedTensorType input = llvm::dyn_cast(inputs.front()); 23 | RankedTensorType output = llvm::dyn_cast(outputs.front()); 24 | if (!input || !output || (input.getElementType() != output.getElementType())) 25 | return false; 26 | return true; 27 | } 28 | 29 | } // namespace mlir::llh 30 | -------------------------------------------------------------------------------- /include/llcompiler/Interfaces/BraodcastableOpInterfaces.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef INCLUDE_LLCOMPILER_INTERFACES_BRAODCASTABLEOPINTERFACES_TD_ 15 | #define INCLUDE_LLCOMPILER_INTERFACES_BRAODCASTABLEOPINTERFACES_TD_ 16 | 17 | include "mlir/IR/OpBase.td" 18 | 19 | def BraodcastableOpInterface: OpInterface<"BraodcastableOpInterface"> { 20 | let description = [{ 21 | 广播Op的接口. 22 | }]; 23 | 24 | let cppNamespace = "::mlir"; 25 | 26 | let methods = [ 27 | InterfaceMethod<"reshape for brodcastable", 28 | /*retTy=*/"::llvm::LogicalResult",/*methodName=*/ "reshapeAndBrodcast"> 29 | ]; 30 | } 31 | 32 | #endif // INCLUDE_LLCOMPILER_INTERFACES_BRAODCASTABLEOPINTERFACES_TD_ -------------------------------------------------------------------------------- /llcompiler/test_models/models/resnet.py: -------------------------------------------------------------------------------- 1 | import llcompiler.compiler as LLC 2 | import os.path 3 | import subprocess 4 | import torch 5 | import torchvision 6 | import torch.nn as nn 7 | import torch.nn.functional as F 8 | from torch._dynamo.backends.common import aot_autograd 9 | import torch.fx 10 | from llcompiler.utility import run_time 11 | import onnx 12 | import torchgen 13 | import torch._dynamo 14 | import os 15 | from llcompiler.test_models import * 16 | 17 | 18 | class Resnet(nn.Module): 19 | def __init__(self): 20 | super().__init__() 21 | self.resnet = torchvision.models.resnet18(num_classes = 10) 22 | self.resnet.avgpool = nn.Sequential(nn.Flatten(1), nn.Linear(4096, 512)) 23 | for sub in self.modules(): 24 | sub.training = False 25 | 26 | def forward(self, x: torch.Tensor): 27 | #x = x.reshape(x.shape[0],x.shape[1],224,224) 28 | x = self.resnet.conv1(x) 29 | x = self.resnet.bn1(x) 30 | x = self.resnet.relu(x) 31 | x = self.resnet.maxpool(x) 32 | x = self.resnet.layer1(x) 33 | x = self.resnet.layer2(x) 34 | x = self.resnet.layer3(x) 35 | # x = self.resnet.layer4(x) 36 | x = self.resnet.avgpool(x) 37 | x = torch.flatten(x, 1) 38 | x = self.resnet.fc(x) 39 | return x 40 | -------------------------------------------------------------------------------- /src/Dialect/Utility/Tool.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #include "llcompiler/Dialect/Utility/Tool.h" 16 | 17 | #include "mlir/IR/Builders.h" 18 | #include "mlir/IR/BuiltinAttributes.h" 19 | #include "mlir/IR/BuiltinOps.h" 20 | #include "mlir/IR/Types.h" 21 | 22 | namespace llc { 23 | mlir::DenseElementsAttr genDenseElementsFromArrayAttr( 24 | mlir::DenseI64ArrayAttr attr) { 25 | auto size = attr.getSize(); 26 | auto element_type = attr.getElementType(); 27 | mlir::SmallVector shape; 28 | shape.push_back(size); 29 | auto tensor = mlir::RankedTensorType::get(shape, element_type); 30 | return mlir::DenseElementsAttr::get(tensor, attr.asArrayRef()); 31 | } 32 | 33 | } // namespace llc 34 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IndexExtension/Transforms/Passes.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_INDEXEXTENSION_TRANSFORMS_PASSES_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_INDEXEXTENSION_TRANSFORMS_PASSES_H_ 18 | 19 | #include 20 | 21 | #include "mlir/Dialect/Index/IR/IndexDialect.h" 22 | #include "mlir/Dialect/Index/IR/IndexOps.h" 23 | #include "mlir/Pass/Pass.h" 24 | 25 | namespace mlir::index::ex { 26 | 27 | #define GEN_PASS_DECL 28 | #define GEN_PASS_REGISTRATION 29 | #include "llcompiler/Dialect/IndexExtension/Transforms/Passes.h.inc" 30 | 31 | } // namespace mlir::index::ex 32 | 33 | #endif // INCLUDE_LLCOMPILER_DIALECT_INDEXEXTENSION_TRANSFORMS_PASSES_H_ 34 | -------------------------------------------------------------------------------- /include/llcompiler/Frontend/Core/Base.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef INCLUDE_LLCOMPILER_FRONTEND_CORE_BASE_H_ 15 | #define INCLUDE_LLCOMPILER_FRONTEND_CORE_BASE_H_ 16 | #include 17 | #include 18 | namespace llc::front { 19 | enum class FRONTEND_TYPE : int64_t { ONNX_FILE = 1, MLIR_FILE = 2 }; 20 | const char *frontend_type_to_str(const FRONTEND_TYPE type); 21 | extern const int64_t ONNX_ADAPTED_VERSION; 22 | 23 | struct FrontEndOption { 24 | std::string input_file; 25 | std::string output_file; 26 | bool onnx_convert; 27 | uint64_t onnx_convert_version; 28 | FRONTEND_TYPE frontend_type; 29 | }; 30 | 31 | } // namespace llc::front 32 | #endif // INCLUDE_LLCOMPILER_FRONTEND_CORE_BASE_H_ 33 | -------------------------------------------------------------------------------- /llcompiler/importer/fx_op_translate/dim.py: -------------------------------------------------------------------------------- 1 | from ..fx_translate import ( 2 | TORCH_FUNCTION_TRANSLATE, 3 | torch_fake_or_mate_tensor_translate, 4 | get_result_type, 5 | get_arg_value, 6 | commond_build_op, 7 | _expand_to_2_if_int, 8 | 9 | SPECIAL_RESULT_FAKE_INDEX_MAP, 10 | SPECIAL_GETITEM_IS_OPERAND_MAP, 11 | ) 12 | from xdsl.dialects.builtin import ( 13 | TensorType, 14 | IntegerType, 15 | i64, 16 | i32, 17 | i16, 18 | i1, 19 | f16, 20 | f32, 21 | f64, 22 | DYNAMIC_INDEX, 23 | DenseArrayBase, 24 | IntegerAttr, 25 | BoolAttr, 26 | DenseIntOrFPElementsAttr, 27 | FloatAttr, 28 | ) 29 | from ...dialect.llh_utility import build_llh_transpose, build_llh_constant 30 | import torch._ops as op 31 | import torch.fx 32 | import torch.nn.functional as F 33 | from xdsl.ir import SSAValue, Operation, OpResult, Attribute, Mapping, Block 34 | from torch._subclasses.fake_tensor import FakeTensor 35 | from ...dialect.llh import TorchSymbolicIntOp,DimOp 36 | from xdsl.irdl import IRDLOperation 37 | 38 | 39 | @TORCH_FUNCTION_TRANSLATE("aten::sym_size.int") 40 | def aten_sym_size_int_convert( 41 | node: torch.fx.node.Node, 42 | value_map: dict[str:[SSAValue]], 43 | 44 | block: Block, 45 | ): 46 | return commond_build_op(DimOp.build, 2, node, value_map, block) -------------------------------------------------------------------------------- /include/llcompiler/Conversion/StablehlotoLinalgExtension/StablehlotoLinalgExtension.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_CONVERSION_STABLEHLOTOLINALGEXTENSION_STABLEHLOTOLINALGEXTENSION_H_ 16 | #define INCLUDE_LLCOMPILER_CONVERSION_STABLEHLOTOLINALGEXTENSION_STABLEHLOTOLINALGEXTENSION_H_ 17 | 18 | #include 19 | namespace mlir { 20 | class MLIRContext; 21 | class TypeConverter; 22 | class Pass; 23 | class RewritePatternSet; 24 | class ConversionTarget; 25 | #define GEN_PASS_DECL_CONVERTSTABLEHLOTOLINALGEXTENSIONPASS 26 | #include "llcompiler/Conversion/Passes.h.inc" 27 | 28 | } // namespace mlir 29 | #endif // INCLUDE_LLCOMPILER_CONVERSION_STABLEHLOTOLINALGEXTENSION_STABLEHLOTOLINALGEXTENSION_H_ 30 | -------------------------------------------------------------------------------- /src/Dialect/IRExtension/IR/Types.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #include "llcompiler/Dialect/IRExtension/IR/Types.h" 16 | 17 | #include "llcompiler/Dialect/IRExtension/IR/Dialect.h" 18 | 19 | #define GET_TYPEDEF_CLASSES 20 | #include "llcompiler/Dialect/IRExtension/IR/Types.cpp.inc" 21 | namespace mlir::ex { 22 | //===----------------------------------------------------------------------===// 23 | // LLHDialect initialize method. 24 | //===----------------------------------------------------------------------===// 25 | void IRExtensionDialect::registerTypes() { 26 | addTypes< 27 | #define GET_TYPEDEF_LIST 28 | #include "llcompiler/Dialect/IRExtension/IR/Types.cpp.inc" 29 | >(); 30 | } 31 | } // namespace mlir::ex 32 | -------------------------------------------------------------------------------- /include/llcompiler/Interfaces/SymbolShapeOpInterfaces.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef INCLUDE_LLCOMPILER_INTERFACES_SYMBOLSHAPEOPINTERFACES_TD_ 15 | #define INCLUDE_LLCOMPILER_INTERFACES_SYMBOLSHAPEOPINTERFACES_TD_ 16 | include "mlir/IR/OpBase.td" 17 | 18 | def SymbolicInferShapeOpInterface: OpInterface<"SymbolicInferShapeOpInterface"> { 19 | let description = [{ 20 | 符号shepa推导的接口. 21 | }]; 22 | 23 | let cppNamespace = "::mlir"; 24 | 25 | let methods = [ 26 | InterfaceMethod<"Infer and set the output shape for the current operation.", 27 | /*retTy=*/"::llvm::LogicalResult",/*methodName=*/ "inferSymbolicShape"> 28 | ]; 29 | } 30 | 31 | #endif // INCLUDE_LLCOMPILER_INTERFACES_SYMBOLSHAPEOPINTERFACES_TD_ -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/TosaExOps.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXOPS_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXOPS_H_ 18 | 19 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExTypes.h" 20 | #include "mlir/Bytecode/BytecodeOpInterface.h" 21 | #include "mlir/IR/Dialect.h" 22 | #include "mlir/IR/OpDefinition.h" 23 | #include "mlir/IR/OpImplementation.h" 24 | #include "mlir/Interfaces/InferTypeOpInterface.h" 25 | #include "mlir/Interfaces/SideEffectInterfaces.h" 26 | 27 | #define GET_OP_CLASSES 28 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExOps.h.inc" 29 | #endif // INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXOPS_H_ 30 | -------------------------------------------------------------------------------- /llcompiler/importer/fx_op_translate/__init__.py: -------------------------------------------------------------------------------- 1 | from .adaptive_avgpool import ( 2 | adaptive_avg_pool2d_convert, 3 | ) 4 | from .binary_ops import mul_convert, div_convert, sub_convert, add_convert 5 | from .unary_ops import ( 6 | abs_convert, 7 | relu_convert, 8 | sqrt_convert, 9 | rsqrt_convert, 10 | exp_convert, 11 | reciprocal_convert, 12 | ) 13 | from .reshape_ops import ( 14 | flatten_convert, 15 | aten_view_convert, 16 | collapse_view_convert, 17 | inductor_force_stride_order_convert, 18 | ) 19 | from .alloc_ops import empty_convert, full_convert 20 | from .binary_ops import add_convert 21 | from .batch_norm import batch_norm_convert 22 | from .cat import cat_convert 23 | from .conv import convolution_convert 24 | from .dim import aten_sym_size_int_convert 25 | from .getitem import builtin_getitem_convert 26 | from .maxpool import max_pool2d_convert 27 | from .transpose import transpose_convert, permute_convert 28 | from .slice import aten_select_convert 29 | from .matmul import matmul_convert 30 | from .broadcast import broadcast_in_dim_convert 31 | from .identity import aten_clone_convert 32 | from .compare import eq_convert, le_convert 33 | from .alloc_ops import scalar_convert 34 | from .convert_to import aten_to_copy_convert 35 | from .where import where_convert 36 | from .reduce_ops import amax_convert, prims_sum_convert 37 | -------------------------------------------------------------------------------- /llcompiler/importer/fx_op_translate/adaptive_avgpool.py: -------------------------------------------------------------------------------- 1 | from ..fx_translate import ( 2 | TORCH_FUNCTION_TRANSLATE, 3 | torch_fake_or_mate_tensor_translate, 4 | get_result_type, 5 | get_arg_value, 6 | commond_build_op, 7 | _expand_to_2_if_int, 8 | SPECIAL_RESULT_FAKE_INDEX_MAP, 9 | SPECIAL_GETITEM_IS_OPERAND_MAP, 10 | ) 11 | from xdsl.irdl import IRDLOperation 12 | from xdsl.dialects.builtin import ( 13 | TensorType, 14 | IntegerType, 15 | i64, 16 | i32, 17 | i16, 18 | i1, 19 | f16, 20 | f32, 21 | f64, 22 | DYNAMIC_INDEX, 23 | DenseArrayBase, 24 | IntegerAttr, 25 | BoolAttr, 26 | DenseIntOrFPElementsAttr, 27 | FloatAttr, 28 | ) 29 | from ...dialect.llh_utility import build_llh_transpose, build_llh_constant 30 | import torch._ops as op 31 | import torch.fx 32 | import torch.nn.functional as F 33 | from xdsl.ir import SSAValue, Operation, OpResult, Attribute, Mapping, Block 34 | from torch._subclasses.fake_tensor import FakeTensor 35 | from ...dialect.llh import TorchSymbolicIntOp, AdaptiveAvgPoolOp 36 | 37 | 38 | @TORCH_FUNCTION_TRANSLATE(F.adaptive_avg_pool2d) 39 | def adaptive_avg_pool2d_convert( 40 | node: torch.fx.node.Node, 41 | value_map: dict[str:[SSAValue]], 42 | block: Block, 43 | ): 44 | return commond_build_op(AdaptiveAvgPoolOp.build, 1, node, value_map, block) 45 | -------------------------------------------------------------------------------- /llcompiler/importer/fx_op_translate/convert_to.py: -------------------------------------------------------------------------------- 1 | from ..fx_translate import ( 2 | TORCH_FUNCTION_TRANSLATE, 3 | 4 | torch_fake_or_mate_tensor_translate, 5 | get_result_type, 6 | get_arg_value, 7 | commond_build_op, 8 | _expand_to_2_if_int, 9 | 10 | SPECIAL_RESULT_FAKE_INDEX_MAP, 11 | SPECIAL_GETITEM_IS_OPERAND_MAP, 12 | ) 13 | from xdsl.dialects.builtin import ( 14 | TensorType, 15 | IntegerType, 16 | i64, 17 | i32, 18 | i16, 19 | i1, 20 | f16, 21 | f32, 22 | f64, 23 | DYNAMIC_INDEX, 24 | DenseArrayBase, 25 | IntegerAttr, 26 | BoolAttr, 27 | DenseIntOrFPElementsAttr, 28 | FloatAttr, 29 | ) 30 | from ...dialect.llh_utility import build_llh_transpose, build_llh_constant 31 | import torch._ops as op 32 | import torch.fx 33 | import torch.nn.functional as F 34 | from xdsl.ir import SSAValue, Operation, OpResult, Attribute, Mapping, Block 35 | from torch._subclasses.fake_tensor import FakeTensor 36 | from ...dialect.llh import TorchSymbolicIntOp, ConvertToOp 37 | from xdsl.irdl import IRDLOperation 38 | 39 | 40 | @TORCH_FUNCTION_TRANSLATE("aten::_to_copy") 41 | def aten_to_copy_convert( 42 | node: torch.fx.node.Node, 43 | value_map: dict[str:[SSAValue]], 44 | 45 | block: Block, 46 | ): 47 | return commond_build_op(ConvertToOp.build, 1, node, value_map, block) 48 | -------------------------------------------------------------------------------- /src/Dialect/IRExtension/IR/Dialect.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #include "llcompiler/Dialect/IRExtension/IR/Dialect.h" 17 | 18 | #include "llcompiler/Dialect/IRExtension/IR/Attrs.h" 19 | #include "llcompiler/Dialect/IRExtension/IR/Dialect.cpp.inc" 20 | 21 | namespace mlir::ex { 22 | //===----------------------------------------------------------------------===// 23 | // LLHDialect initialize method. 24 | //===----------------------------------------------------------------------===// 25 | void IRExtensionDialect::initialize() { 26 | addOperations< 27 | #define GET_OP_LIST 28 | #include "llcompiler/Dialect/IRExtension/IR/Ops.cpp.inc" 29 | >(); 30 | registerTypes(); 31 | registerAttributes(); 32 | } 33 | 34 | } // namespace mlir::ex 35 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/IR/TosaExTypes.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXTYPES_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXTYPES_H_ 18 | #include 19 | #include 20 | 21 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExEunms.h.inc" 22 | #include "mlir/IR/BuiltinTypes.h" 23 | #include "mlir/IR/DialectImplementation.h" 24 | #include "mlir/IR/ExtensibleDialect.h" 25 | 26 | #define GET_ATTRDEF_CLASSES 27 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExAttrs.h.inc" 28 | #define GET_TYPEDEF_CLASSES 29 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExTypes.h.inc" 30 | 31 | #endif // INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_IR_TOSAEXTYPES_H_ 32 | -------------------------------------------------------------------------------- /test/Dialect/LLH/symbol_infer/symbol_cse.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file -infer-symbol-shape="use-encoding=false" --symbol-cse %s| FileCheck %s 2 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file -infer-symbol-shape="use-encoding=false" --symbol-cse /home/lfr/LLCompiler/test/Dialect/LLH/symbol_infer/symbol_fold.mlir 3 | 4 | 5 | 6 | // CHECK-LABEL: tensor_dim_fold 7 | func.func @tensor_dim_fold(%arg0: tensor {func.input_symbol_0 = "s0", func.input_symbol_1 = "s0", func.input_symbol_2 = "s1", func.input_symbol_3 = "s1"}) -> index attributes {entrance} { 8 | %c3 = arith.constant 3 : index 9 | // CHECK-COUNT-1: tensor.dim 10 | %dim_1 = tensor.dim %arg0, %c3 : tensor 11 | %dim_5 = tensor.dim %arg0, %c3 : tensor 12 | %6 = arith.addi %dim_5, %dim_1 : index 13 | return %6: index 14 | } 15 | 16 | // ----- 17 | // CHECK-LABEL: arith_to_const 18 | func.func @arith_to_const(%arg0: tensor {func.input_symbol_0 = "s0", func.input_symbol_1 = "s0", func.input_symbol_2 = "s1", func.input_symbol_3 = "s1"}) -> index attributes {entrance} { 19 | %c3 = arith.constant 3 : index 20 | %dim_1 = tensor.dim %arg0, %c3 : tensor 21 | %dim_5 = tensor.dim %arg0, %c3 : tensor 22 | // CHECK: arith.constant 0 : index 23 | %6 = arith.subi %dim_5, %dim_1 : index 24 | return %6: index 25 | } -------------------------------------------------------------------------------- /include/llcompiler/Support/Enums_old.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | #ifndef INCLUDE_LLCOMPILER_SUPPORT_ENUMS_H_ 16 | #define INCLUDE_LLCOMPILER_SUPPORT_ENUMS_H_ 17 | #include 18 | #include 19 | 20 | namespace llc { 21 | enum MODE : int64_t { Training = 1, Inference = 2 }; 22 | const char *mode_to_str(const MODE level); 23 | MODE str_to_mode(const char *); 24 | 25 | enum TARGET : std::int64_t { 26 | CPU = 0, 27 | }; 28 | const char *target_to_str(const TARGET target); 29 | TARGET str_to_target(const char *str); 30 | 31 | enum FRONT : std::int64_t { 32 | Torch = 0, 33 | Onnx = 1, 34 | }; 35 | const char *front_to_str(const FRONT front); 36 | FRONT str_to_front(const char *str); 37 | } // namespace llc 38 | #endif // INCLUDE_LLCOMPILER_SUPPORT_ENUMS_H_ 39 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/TosaExtension/Transforms/Passes.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_TRANSFORMS_PASSES_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_TRANSFORMS_PASSES_H_ 18 | 19 | #include 20 | 21 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExDialect.h" 22 | #include "llcompiler/Dialect/TosaExtension/IR/TosaExOps.h" 23 | #include "mlir/Pass/Pass.h" 24 | 25 | namespace mlir::tosa_ex { 26 | std::unique_ptr<::mlir::Pass> createTransformLayoutToNHWCPass(); 27 | #define GEN_PASS_DECL 28 | #define GEN_PASS_REGISTRATION 29 | #include "llcompiler/Dialect/TosaExtension/Transforms/Passes.h.inc" 30 | } // namespace mlir::tosa_ex 31 | 32 | #endif // INCLUDE_LLCOMPILER_DIALECT_TOSAEXTENSION_TRANSFORMS_PASSES_H_ 33 | -------------------------------------------------------------------------------- /src/Dialect/LLH/IR/LLHOps.cpp: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | // Licensed under the Apache License, Version 2.0 (the "License"); 3 | // you may not use this file except in compliance with the License. 4 | // You may obtain a copy of the License at 5 | 6 | // http://www.apache.org/licenses/LICENSE-2.0 7 | 8 | // Unless required by applicable law or agreed to in writing, software 9 | // distributed under the License is distributed on an "AS IS" BASIS, 10 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 11 | // See the License for the specific language governing permissions and 12 | // limitations under the License. 13 | 14 | #include "llcompiler/Dialect/LLH/IR/LLHOps.h" 15 | 16 | #include "llcompiler/Support/Macro.h" 17 | #include "llcompiler/Support/Logger.h" 18 | #include "llvm/ADT/StringRef.h" 19 | #include "llvm/ADT/TypeSwitch.h" 20 | #include "llvm/Support/Casting.h" 21 | #include "llvm/Support/LogicalResult.h" 22 | #include "mlir/IR/Attributes.h" 23 | #include "mlir/IR/Builders.h" 24 | #include "mlir/IR/BuiltinAttributes.h" 25 | #include "mlir/IR/BuiltinOps.h" 26 | #include "mlir/IR/DialectImplementation.h" 27 | #include "mlir/IR/OpDefinition.h" 28 | #include "mlir/IR/OpImplementation.h" 29 | 30 | #define GET_OP_CLASSES 31 | #include "llcompiler/Dialect/LLH/IR/LLHOps.cpp.inc" 32 | 33 | namespace mlir::llh { 34 | 35 | 36 | } // namespace mlir::llh 37 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/BufferizationExtension/Transforms/Passes.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_DIALECT_BUFFERIZATIONEXTENSION_TRANSFORMS_PASSES_H_ 17 | #define INCLUDE_LLCOMPILER_DIALECT_BUFFERIZATIONEXTENSION_TRANSFORMS_PASSES_H_ 18 | 19 | #include 20 | 21 | #include "mlir/Dialect/Bufferization/IR/Bufferization.h" 22 | #include "mlir/Dialect/Func/IR/FuncOps.h" 23 | #include "mlir/Dialect/Index/IR/IndexOps.h" 24 | #include "mlir/Pass/Pass.h" 25 | namespace mlir::bufferization::ex { 26 | 27 | #define GEN_PASS_DECL 28 | #define GEN_PASS_REGISTRATION 29 | #include "llcompiler/Dialect/BufferizationExtension/Transforms/Passes.h.inc" 30 | 31 | } // namespace mlir::bufferization::ex 32 | 33 | #endif // INCLUDE_LLCOMPILER_DIALECT_BUFFERIZATIONEXTENSION_TRANSFORMS_PASSES_H_ 34 | -------------------------------------------------------------------------------- /include/llcompiler/Frontend/Core/Option.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef INCLUDE_LLCOMPILER_FRONTEND_CORE_OPTION_H_ 15 | #define INCLUDE_LLCOMPILER_FRONTEND_CORE_OPTION_H_ 16 | #include 17 | #include 18 | 19 | #include "llcompiler/Frontend/Core/Base.h" 20 | #include "llvm/Support/CommandLine.h" 21 | 22 | namespace llc::option { 23 | ::llc::front::FrontEndOption get_front_end_option(); 24 | 25 | extern llvm::cl::OptionCategory importingOptions; 26 | extern llvm::cl::opt frontendType; 27 | extern llvm::cl::opt inputFile; 28 | extern llvm::cl::opt outputFile; 29 | extern llvm::cl::opt onnxConvertVersion; 30 | extern llvm::cl::opt onnxConvert; 31 | } // namespace llc::option 32 | 33 | #endif // INCLUDE_LLCOMPILER_FRONTEND_CORE_OPTION_H_ 34 | -------------------------------------------------------------------------------- /discard/include/Support/Option.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef INCLUDE_LLCOMPILER_SUPPORT_OPTION_H_ 15 | #define INCLUDE_LLCOMPILER_SUPPORT_OPTION_H_ 16 | /** 17 | * @file Option.h 18 | * @brief global options define 19 | * @author 时光丶人爱 (1733535832@qq.com) 20 | * @version 1.0 21 | * @date 2024-07-01 22 | * 23 | * @copyright Copyright (c) 2024 时光丶人爱 24 | * 25 | */ 26 | 27 | #include 28 | 29 | #include "llcompiler/Support/Logger.h" 30 | #include "llvm/Support/CommandLine.h" 31 | 32 | namespace llc::option { 33 | 34 | ::llc::logger::LoggerOption get_logger_option(); 35 | extern llvm::cl::OptionCategory commonOption; 36 | extern llvm::cl::opt logRoot; 37 | extern llvm::cl::opt logLevel; 38 | } // namespace llc::option 39 | 40 | #endif // INCLUDE_LLCOMPILER_SUPPORT_OPTION_H_ 41 | -------------------------------------------------------------------------------- /test/Dialect/LLH/reshape_before_braodcast.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --canonicalize --cse %s| FileCheck %s 2 | 3 | 4 | module attributes {builtin.gloabal_layout = #llh.Layout} { 5 | "llh.symbolic_int"() <{sym_name = "c1"}> : () -> () 6 | // CHECK-LABEL: simplyBinary 7 | func.func @simplyBinary(%arg0: tensor,%arg1: tensor<1xf32>) -> (tensor) attributes {entrance} { 8 | // CHECK: llh.reshape 9 | // CHECK-SAME: -> tensor<1x1x1x1xf32> 10 | %29 = "llh.add"(%arg0, %arg1) : (tensor, tensor<1xf32>) -> tensor 11 | return %29: tensor 12 | } 13 | } 14 | 15 | // ----- 16 | // CHECK-LABEL: where 17 | func.func @where(%arg0: tensor,%arg1: tensor<1xf32>,%arg2: tensor<1xf32>) -> (tensor) attributes {entrance} { 18 | // CHECK: llh.reshape 19 | // CHECK-SAME: -> tensor<1x1x1x1xf32> 20 | // CHECK: llh.reshape 21 | // CHECK-SAME: -> tensor<1x1x1x1xf32> 22 | // CHECK: llh.broadcast_to 23 | // CHECK-SAME: -> tensor 24 | // CHECK: llh.broadcast_to 25 | // CHECK-SAME: -> tensor 26 | %0 = "llh.where"(%arg0, %arg1, %arg2) : (tensor, tensor<1xf32>, tensor<1xf32>) -> tensor 27 | return %0: tensor 28 | } 29 | 30 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --canonicalize --cse /home/lfr/LLCompiler/test/Dialect/LLH/reshape_before_braodcast.mlir 31 | -------------------------------------------------------------------------------- /include/llcompiler/Frontend/Core/Builder.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | /** 15 | * @file Opbuilder.h 16 | * @brief interface class OpBuilder that build Ops form inputs. 17 | * @author 时光丶人爱 (1733535832@qq.com) 18 | * @version 1.0 19 | * @date 2024-07-01 20 | * 21 | * @copyright Copyright (c) 2024 时光丶人爱 22 | * 23 | */ 24 | 25 | #include "mlir/IR/Builders.h" 26 | #include "mlir/IR/MLIRContext.h" 27 | 28 | #ifndef INCLUDE_LLCOMPILER_FRONTEND_CORE_BUILDER_H_ 29 | #define INCLUDE_LLCOMPILER_FRONTEND_CORE_BUILDER_H_ 30 | 31 | namespace llc::front { 32 | 33 | class Builder { 34 | public: 35 | explicit Builder(mlir::MLIRContext* context); 36 | virtual ~Builder(); 37 | mlir::OpBuilder& builder(); 38 | 39 | protected: 40 | mlir::OpBuilder builder_; 41 | }; 42 | 43 | } // namespace llc::front 44 | #endif // INCLUDE_LLCOMPILER_FRONTEND_CORE_BUILDER_H_ 45 | -------------------------------------------------------------------------------- /test/Dialect/LLH/decompose_ops.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --decompose-ops --canonicalize --cse %s| FileCheck %s 2 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --decompose-ops --cse /home/lfr/LLCompiler/test/Dialect/LLH/decompose_ops.mlir 3 | 4 | func.func @batch_norm(%arg0: tensor {func.input_symbol_0 = "s0", func.input_symbol_1 = "c3", func.input_symbol_2 = "s2", func.input_symbol_3 = "s2"}) -> tensor attributes {entrance} { 5 | %0 = "llh.weight"() <{weight_file = "xxx"}> : () -> tensor<3xf32> 6 | %1 = "llh.weight"() <{weight_file = "xxx"}> : () -> tensor<3xf32> 7 | %2 = "llh.weight"() <{weight_file = "xxx"}> : () -> tensor<3xf32> 8 | %3 = "llh.weight"() <{weight_file = "xxx"}> : () -> tensor<3xf32> 9 | %4 = "llh.weight"() <{weight_file = "xxx"}> : () -> tensor<1xi64> 10 | // CHECK: llh.add 11 | // CHECK: llh.sqrt 12 | // CHECK: llh.reshape 13 | // CHECK: llh.broadcast_to 14 | // CHECK: llh.reshape 15 | // CHECK: llh.broadcast_to 16 | // CHECK: llh.sub 17 | // CHECK: llh.mul 18 | // CHECK: llh.div 19 | // CHECK: llh.add 20 | // CHECK-NOT: llh.batch_norm_inference 21 | %5 = "llh.batch_norm_inference"(%arg0, %0, %1, %2, %3) <{epsilon = 1.000000e-05 : f64, feature_index = 1 : i64, momentum = 1.000000e-01 : f64}> : (tensor, tensor<3xf32>, tensor<3xf32>, tensor<3xf32>, tensor<3xf32>) -> tensor 22 | return %5 : tensor 23 | } 24 | 25 | 26 | -------------------------------------------------------------------------------- /include/llcompiler/TransformLibrary/LibraryPath.h.in: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | #ifndef INCLUDE_LLCOMPILER_TRANSFORMLIBRARY_LIBRARYPATH_H_ 16 | #define INCLUDE_LLCOMPILER_TRANSFORMLIBRARY_LIBRARYPATH_H_ 17 | #define __LLC_TRANSFORM_PATH__ "@CMAKE_CURRENT_SOURCE_DIR@" 18 | 19 | #define __LLC_TRANSFORM_LINALG_INCLUDE__ \ 20 | "@CMAKE_CURRENT_SOURCE_DIR@/linalg_include.mlir" 21 | 22 | #define __LLC_TRANSFORM_MHLO_INCLUDE__ \ 23 | "@CMAKE_CURRENT_SOURCE_DIR@/mhlo_include.mlir" 24 | 25 | #define __LLC_TRANSFORM_TENSOR_INCLUDE__ \ 26 | "@CMAKE_CURRENT_SOURCE_DIR@/tensor_include.mlir" 27 | 28 | #define __LLC_TRANSFORM_LLVM_INCLUDE__ \ 29 | "@CMAKE_CURRENT_SOURCE_DIR@/llvm_include.mlir" 30 | 31 | #define __LLC_TRANSFORM_MEMREF_INCLUDE__ \ 32 | "@CMAKE_CURRENT_SOURCE_DIR@/memref_include.mlir" 33 | 34 | #endif // INCLUDE_LLCOMPILER_TRANSFORMLIBRARY_LIBRARYPATH_H_ 35 | -------------------------------------------------------------------------------- /discard/include/Compiler/Utility.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | 15 | /** 16 | * @file Utility.h 17 | * @brief utility for compiler 18 | * @author 时光丶人爱 (1733535832@qq.com) 19 | * @version 1.0 20 | * @date 2024-07-01 21 | * 22 | * @copyright Copyright (c) 2024 时光丶人爱 23 | * 24 | */ 25 | 26 | #ifndef INCLUDE_LLCOMPILER_COMPILER_UTILITY_H_ 27 | #define INCLUDE_LLCOMPILER_COMPILER_UTILITY_H_ 28 | 29 | #include "Frontend/Core/Base.h" 30 | #include "mlir/IR/BuiltinOps.h" 31 | #include "mlir/IR/MLIRContext.h" 32 | #include "mlir/IR/OwningOpRef.h" 33 | 34 | namespace llc::compiler { 35 | 36 | mlir::OwningOpRef gen_mlir_from( 37 | mlir::MLIRContext *context, const front::FrontEndOption &option); 38 | 39 | void mlir_to_file(mlir::ModuleOp *module, const char *file); 40 | } // namespace llc::compiler 41 | 42 | #endif // INCLUDE_LLCOMPILER_COMPILER_UTILITY_H_ 43 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/IRExtension/IR/Dialect.td: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef LLC_IREX_DIALECT 15 | #define LLC_IREX_DIALECT 16 | 17 | include "mlir/IR/OpBase.td" 18 | 19 | //===----------------------------------------------------------------------===// 20 | // IR extension dialect definition. 21 | //===----------------------------------------------------------------------===// 22 | 23 | def IRExtension_Dialect : Dialect{ 24 | let name = "mlir_ex"; 25 | let summary = "mlir extension"; 26 | let description = [{ 27 | mlir extension. 28 | }]; 29 | let cppNamespace = "::mlir::ex"; 30 | let useDefaultAttributePrinterParser = 1; 31 | let useDefaultTypePrinterParser = 0; 32 | let extraClassDeclaration = [{ 33 | void registerTypes(); 34 | void registerAttributes(); 35 | }]; 36 | 37 | } 38 | 39 | 40 | #endif // LLC_IREX_ 41 | -------------------------------------------------------------------------------- /include/llcompiler/Dialect/Utility/File.h: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | #ifndef INCLUDE_LLCOMPILER_DIALECT_UTILITY_FILE_H_ 15 | #define INCLUDE_LLCOMPILER_DIALECT_UTILITY_FILE_H_ 16 | #include "llvm/IR/Module.h" 17 | #include "mlir/IR/BuiltinOps.h" 18 | #include "mlir/IR/MLIRContext.h" 19 | namespace llc::file { 20 | void mlir_to_file(mlir::OwningOpRef* module, const char* file); 21 | void str_to_mlir_module(mlir::MLIRContext& context, 22 | mlir::OwningOpRef& module, 23 | const char* str); 24 | void file_to_mlir_module(mlir::MLIRContext& context, 25 | mlir::OwningOpRef& module, 26 | const char* file); 27 | void llvm_module_to_file(llvm::Module* module, const char* file); 28 | } // namespace llc::file 29 | 30 | #endif // INCLUDE_LLCOMPILER_DIALECT_UTILITY_FILE_H_ 31 | -------------------------------------------------------------------------------- /test/Conversion/StablehlotoLinalgExtension/stablehlo_to_linalg_extension.mlir: -------------------------------------------------------------------------------- 1 | // RUN: llc-opt --split-input-file --convert-stablehlo-to-linalg-extension --canonicalize %s| FileCheck %s 2 | // /home/lfr/LLCompiler/build/bin/llc-opt --split-input-file --convert-stablehlo-to-linalg-extension --canonicalize /home/lfr/LLCompiler/test/Conversion/StablehlotoLinalgExtension/stablehlo_to_linalg_extension.mlir 3 | 4 | // CHECK-LABEL: real_dynamic_slice 5 | func.func @real_dynamic_slice(%arg0: tensor {func.input_symbol_0 = "s0", func.input_symbol_1 = "s0", func.input_symbol_2 = "s1", func.input_symbol_3 = "s1"}) -> tensor<1x?x?x?xf32> attributes {entrance} { 6 | %cst = arith.constant dense<0> : tensor<4xindex> 7 | %cst_0 = arith.constant dense<1> : tensor<4xindex> 8 | %cst_1 = arith.constant dense<[1, 0, 0, 0]> : tensor<4xindex> 9 | %c3 = arith.constant 3 : index 10 | %c2 = arith.constant 2 : index 11 | %c1 = arith.constant 1 : index 12 | %dim = tensor.dim {symbol = @s0} %arg0, %c1 : tensor 13 | %dim_2 = tensor.dim {symbol = @s1} %arg0, %c2 : tensor 14 | %dim_3 = tensor.dim {symbol = @s1} %arg0, %c3 : tensor 15 | %from_elements = tensor.from_elements %c2, %dim, %dim_2, %dim_3 : tensor<4xindex> 16 | //CHECK: tensor.extract_slice 17 | %0 = stablehlo.real_dynamic_slice %arg0, %cst_1, %from_elements, %cst_0 : (tensor, tensor<4xindex>, tensor<4xindex>, tensor<4xindex>) -> tensor<1x?x?x?xf32> 18 | return %0: tensor<1x?x?x?xf32> 19 | } -------------------------------------------------------------------------------- /test/lit.site.cfg.py.in: -------------------------------------------------------------------------------- 1 | # Copyright 2024 时光丶人爱 2 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | @LIT_SITE_CFG_IN_HEADER@ 16 | 17 | import sys 18 | 19 | config.llvm_src_root = "@LLVM_SOURCE_DIR@" 20 | config.llvm_obj_root = "@LLVM_BINARY_DIR@" 21 | config.llvm_tools_dir = "@LLVM_BINARY_DIR@/bin" 22 | config.llvm_lib_dir = "@LLVM_BINARY_DIR@/lib" 23 | config.llvm_shlib_dir = "@SHLIBDIR@" 24 | config.llvm_shlib_ext = "@SHLIBEXT@" 25 | config.llvm_exe_ext = "@EXEEXT@" 26 | config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" 27 | config.python_executable = "@Python3_EXECUTABLE@" 28 | config.mlir_lib_dir = "@MLIR_LIB_DIR@" or "@LLVM_LIBRARY_DIR@" 29 | config.mlir_binary_dir = "@MLIR_BINARY_DIR@" or "@LLVM_BINARY_DIR@" 30 | config.mlir_llcompiler_tools_dir = "@LLCOMPILER_BUILD_RUNTIME_DIR@" 31 | 32 | import lit.llvm 33 | lit.llvm.initialize(lit_config, config) 34 | 35 | # Let the main config do the real work. 36 | lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg.py") 37 | -------------------------------------------------------------------------------- /test/python/lit.site.cfg.py.in: -------------------------------------------------------------------------------- 1 | # Copyright 2024 时光丶人爱 2 | 3 | # Licensed under the Apache License, Version 2.0 (the "License"); 4 | # you may not use this file except in compliance with the License. 5 | # You may obtain a copy of the License at 6 | 7 | # http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | # Unless required by applicable law or agreed to in writing, software 10 | # distributed under the License is distributed on an "AS IS" BASIS, 11 | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | # See the License for the specific language governing permissions and 13 | # limitations under the License. 14 | 15 | @LIT_SITE_CFG_IN_HEADER@ 16 | 17 | import sys 18 | 19 | config.llvm_src_root = "@LLVM_SOURCE_DIR@" 20 | config.llvm_obj_root = "@LLVM_BINARY_DIR@" 21 | config.llvm_tools_dir = "@LLVM_BINARY_DIR@/bin" 22 | config.llvm_lib_dir = "@LLVM_BINARY_DIR@/lib" 23 | config.llvm_shlib_dir = "@SHLIBDIR@" 24 | config.llvm_shlib_ext = "@SHLIBEXT@" 25 | config.llvm_exe_ext = "@EXEEXT@" 26 | config.lit_tools_dir = "@LLVM_LIT_TOOLS_DIR@" 27 | config.python_executable = "@Python3_EXECUTABLE@" 28 | config.mlir_lib_dir = "@MLIR_LIB_DIR@" or "@LLVM_LIBRARY_DIR@" 29 | config.mlir_binary_dir = "@MLIR_BINARY_DIR@" or "@LLVM_BINARY_DIR@" 30 | config.mlir_llcompiler_tools_dir = "@LLCOMPILER_BUILD_RUNTIME_DIR@" 31 | 32 | import lit.llvm 33 | lit.llvm.initialize(lit_config, config) 34 | 35 | # Let the main config do the real work. 36 | lit_config.load_config(config, "@CMAKE_CURRENT_SOURCE_DIR@/lit.cfg.py") 37 | -------------------------------------------------------------------------------- /include/llcompiler/TransformLibrary/LibraryPath.install.h.in: -------------------------------------------------------------------------------- 1 | // Copyright 2024 时光丶人爱 2 | 3 | // Licensed under the Apache License, Version 2.0 (the "License"); 4 | // you may not use this file except in compliance with the License. 5 | // You may obtain a copy of the License at 6 | 7 | // http://www.apache.org/licenses/LICENSE-2.0 8 | 9 | // Unless required by applicable law or agreed to in writing, software 10 | // distributed under the License is distributed on an "AS IS" BASIS, 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 | // See the License for the specific language governing permissions and 13 | // limitations under the License. 14 | // 15 | 16 | #ifndef INCLUDE_LLCOMPILER_TRANSFORMLIBRARY_LIBRARYPATH_INSTALL_H_ 17 | #define INCLUDE_LLCOMPILER_TRANSFORMLIBRARY_LIBRARYPATH_INSTALL_H_ 18 | #define __LLC_TRANSFORM_PATH__ "@LLCOMPILER_TANSFORMLIB_INSTALL_DIR@" 19 | 20 | #define __LLC_TRANSFORM_LINALG_INCLUDE__ \ 21 | "@LLCOMPILER_TANSFORMLIB_INSTALL_DIR@/linalg_include.mlir" 22 | 23 | #define __LLC_TRANSFORM_MHLO_INCLUDE__ \ 24 | "@LLCOMPILER_TANSFORMLIB_INSTALL_DIR@/mhlo_include.mlir" 25 | 26 | #define __LLC_TRANSFORM_TENSOR_INCLUDE__ \ 27 | "@LLCOMPILER_TANSFORMLIB_INSTALL_DIR@/tensor_include.mlir" 28 | 29 | #define __LLC_TRANSFORM_LLVM_INCLUDE__ \ 30 | "@LLCOMPILER_TANSFORMLIB_INSTALL_DIR@/llvm_include.mlir" 31 | 32 | #define __LLC_TRANSFORM_MEMREF_INCLUDE__ \ 33 | "@LLCOMPILER_TANSFORMLIB_INSTALL_DIR@/memref_include.mlir" 34 | 35 | #endif // INCLUDE_LLCOMPILER_TRANSFORMLIBRARY_LIBRARYPATH_INSTALL_H_ 36 | --------------------------------------------------------------------------------