| 52 | } |
| 53 | |
| 54 | Compiler* Compiler::get(ComputingGraph& graph, CompNode comp_node) { |
| 55 | static EmptyCompiler empty_compiler; |
| 56 | if (comp_node == CompNode::default_cpu()) { |
| 57 | // oprs in the internal graph are on default cpu; this case handles |
| 58 | // nested JITExecutor |
| 59 | return &empty_compiler; |
| 60 | } |
| 61 | |
| 62 | CompilerHolder* holder; |
| 63 | { |
| 64 | static std::mutex mtx; |
| 65 | MGB_LOCK_GUARD(mtx); |
| 66 | holder = graph.options().user_data.get_user_data_or_create<CompilerHolder>(); |
| 67 | } |
| 68 | MGB_LOCK_GUARD(holder->mtx); |
| 69 | auto&& compiler = holder->dev2compiler[comp_node.device_type()]; |
| 70 | std::string backend = gopt::JITFusionPass::get_jit_backend_str(); |
| 71 | mgb_assert( |
| 72 | !backend.empty(), |
| 73 | "code issue happened, need call config_jit_backends before get compiler"); |
| 74 | //! please keep logic with JITFusionPass::Impl::config_jit_backends |
| 75 | mgb_log_debug("Compiler: JIT backend: %s", backend.c_str()); |
| 76 | if (!compiler) { |
| 77 | switch (comp_node.device_type()) { |
| 78 | #if MGB_CUDA |
| 79 | case CompNode::DeviceType::CUDA: |
| 80 | #if MGB_JIT_HALIDE |
| 81 | if (!strcmp(backend.c_str(), "HALIDE")) { |
| 82 | compiler = std::make_unique<HalideCudaCompiler>(); |
| 83 | break; |
| 84 | } |
| 85 | #endif |
| 86 | #if MGB_JIT_MLIR |
| 87 | if (!strcmp(backend.c_str(), "MLIR")) { |
| 88 | compiler = |
| 89 | std::make_unique<MLIRCompiler>(CompNode::DeviceType::CUDA); |
| 90 | break; |
| 91 | } |
| 92 | #endif |
| 93 | if (!strcmp(backend.c_str(), "NVRTC")) { |
| 94 | compiler = std::make_unique<CudaCompiler>(); |
| 95 | break; |
| 96 | } |
| 97 | mgb_throw( |
| 98 | InternalError, |
| 99 | "No compiler support for cuda, may caused by build not enable " |
| 100 | "MLIR/HALIDE module or error config jit backend env"); |
| 101 | break; |
| 102 | #endif |
| 103 | case CompNode::DeviceType::CPU: |
| 104 | #if MGB_JIT_MLIR |
| 105 | if (!strcmp(backend.c_str(), "MLIR")) { |
| 106 | compiler = |
| 107 | std::make_unique<MLIRCompiler>(CompNode::DeviceType::CPU); |
| 108 | break; |
| 109 | } |
| 110 | #endif |
| 111 | mgb_throw( |