You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

lower_to_llvm_pass.cpp 2.6 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /**
  2. * \file src/jit/impl/mlir/ir/lower_to_llvm_pass.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or
  10. * implied.
  11. */
  12. #include "megbrain_build_config.h"
  13. #if MGB_JIT && MGB_JIT_MLIR
  14. #include "megbrain/common.h"
  15. #include "megbrain/jit/mlir/ir/dialect.h"
  16. #include "megbrain/jit/mlir/ir/passes.h"
  17. #include <mlir/Conversion/AffineToStandard/AffineToStandard.h>
  18. #include <mlir/Conversion/SCFToStandard/SCFToStandard.h>
  19. #include <mlir/Conversion/StandardToLLVM/ConvertStandardToLLVM.h>
  20. #include <mlir/Conversion/StandardToLLVM/ConvertStandardToLLVMPass.h>
  21. #include <mlir/Dialect/LLVMIR/LLVMDialect.h>
  22. #include <mlir/Dialect/SCF/SCF.h>
  23. #include <mlir/Dialect/StandardOps/Transforms/Passes.h>
  24. using namespace mgb;
  25. using namespace jit;
  26. namespace {
  27. class AffineToLLVMLoweringPass : public PassWrapper<AffineToLLVMLoweringPass,
  28. OperationPass<ModuleOp>> {
  29. public:
  30. void getDependentDialects(mlir::DialectRegistry& registry) const override {
  31. registry.insert<mlir::LLVM::LLVMDialect>();
  32. registry.insert<mlir::scf::SCFDialect>();
  33. }
  34. void runOnOperation() final {
  35. LLVMConversionTarget target(getContext());
  36. target.addLegalOp<ModuleOp, ModuleTerminatorOp>();
  37. LLVMTypeConverter typeConverter(&getContext());
  38. OwningRewritePatternList patterns;
  39. populateAffineToStdConversionPatterns(patterns, &getContext());
  40. populateLoopToStdConversionPatterns(patterns, &getContext());
  41. populateStdToLLVMConversionPatterns(typeConverter, patterns);
  42. populateExpandTanhPattern(patterns, &getContext());
  43. auto module = getOperation();
  44. if (failed(applyFullConversion(module, target, std::move(patterns))))
  45. signalPassFailure();
  46. }
  47. };
  48. } // namespace
  49. std::unique_ptr<mlir::Pass> mgb::jit::create_lower_to_llvm_pass() {
  50. return std::make_unique<AffineToLLVMLoweringPass>();
  51. }
  52. namespace mgb {
  53. namespace jit {
  54. void register_test_affine_to_llvm_lowering_pass() {
  55. PassRegistration<AffineToLLVMLoweringPass>(
  56. "mgb-codegen-convert-affine-to-llvm",
  57. "Perform final conversion from Affine to LLVMIR ",
  58. [] { return std::make_unique<AffineToLLVMLoweringPass>(); });
  59. }
  60. } // namespace jit
  61. } // namespace mgb
  62. #endif // MGB_JIT && MGB_JIT_MLIR
  63. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台