comp_node_env.h 22.5 KB
Newer Older
1 2 3 4
/**
 * \file src/core/include/megbrain/comp_node_env.h
 * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
 *
5
 * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
 *
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 */

#pragma once

#include "megbrain/common.h"
#include "megbrain/comp_node.h"
#include "megbrain/utils/metahelper.h"
#include "megbrain/utils/thread.h"
#include "megbrain_build_config.h"

#include "megdnn/handle.h"


#if MGB_CUDA
#include <cuda_runtime.h>
25
#include <cuda.h>
26 27 28 29 30 31 32 33 34 35

#if MGB_ENABLE_LOGGING
#define MGB_CUDA_CHECK(expr)                                          \
    do {                                                              \
        cudaError_t __cuda_check_code = (expr);                       \
        if (!mgb_likely(__cuda_check_code == cudaSuccess)) {          \
            ::mgb::_on_cuda_error(#expr, __cuda_check_code, __FILE__, \
                                  __func__, __LINE__);                \
        }                                                             \
    } while (0)
36 37 38 39 40 41 42 43 44 45

#define MGB_CUDA_CU_CHECK(expr)                                          \
    do {                                                                 \
        CUresult __cuda_check_code = (expr);                             \
        if (!mgb_likely(__cuda_check_code == CUDA_SUCCESS)) {            \
            ::mgb::_on_cuda_cu_error(#expr, __cuda_check_code, __FILE__, \
                                     __func__, __LINE__);                \
        }                                                                \
    } while (0)

46 47 48 49 50 51 52 53 54
#else
#define MGB_CUDA_CHECK(expr)                                            \
    do {                                                                \
        cudaError_t __cuda_check_code = (expr);                         \
        if (!mgb_likely(__cuda_check_code == cudaSuccess)) {            \
            ::mgb::_on_cuda_error(#expr, __cuda_check_code, "", "", 1); \
        }                                                               \
    } while (0)

55 56 57 58 59 60 61 62
#define MGB_CUDA_CU_CHECK(expr)                                            \
    do {                                                                   \
        CUresult __cuda_check_code = (expr);                               \
        if (!mgb_likely(__cuda_check_code == CUDA_SUCCESS)) {              \
            ::mgb::_on_cuda_cu_error(#expr, __cuda_check_code, "", "", 1); \
        }                                                                  \
    } while (0)

63 64
#endif //MGB_ENABLE_LOGGING
#endif //MGB_CUDA
65

66
#if MGB_ATLAS
67
#include "megcore_atlas.h"
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
#include <atomic>

#if MGB_ENABLE_LOGGING
#define MGB_ATLAS_CHECK(expr)                                         \
    do {                                                              \
        aclError __acl_check_code = (expr);                           \
        if (!mgb_likely(__acl_check_code == ACL_ERROR_NONE)) {        \
            ::mgb::_on_atlas_error(#expr, __acl_check_code, __FILE__, \
                                  __func__, __LINE__);                \
        }                                                             \
    } while (0)
#else
#define MGB_ATLAS_CHECK(expr)                                           \
    do {                                                                \
        aclError __acl_check_code = (expr);                             \
        if (!mgb_likely(__acl_check_code == ACL_ERROR_NONE)) {          \
            ::mgb::_on_atlas_error(#expr, __acl_check_code, "", "", 1); \
        }                                                               \
    } while (0)

#endif //MGB_ENABLE_LOGGING

#endif // MGB_ATLAS
91

92

93

94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
#if MGB_ROCM
#include "hcc_detail/hcc_defs_prologue.h"
#include "megcore_rocm.h"

#if MGB_ENABLE_LOGGING
#define MGB_ROCM_CHECK(expr)                                                  \
    do {                                                                      \
        hipError_t __hip_check_code = (expr);                                 \
        if (!mgb_likely(__hip_check_code == hipSuccess)) {                    \
            ::mgb::_on_hip_error(#expr, __hip_check_code, __FILE__, __func__, \
                                 __LINE__);                                   \
        }                                                                     \
    } while (0)
#else
#define MGB_ROCM_CHECK(expr)                                          \
    do {                                                              \
        hipError_t __hip_check_code = (expr);                         \
        if (!mgb_likely(__hip_check_code == hipSuccess)) {            \
            ::mgb::_on_hip_error(#expr, __hip_check_code, "", "", 1); \
        }                                                             \
    } while (0)

#endif  // MGB_ENABLE_LOGGING

#endif

#if MGB_CAMBRICON
#include <cnrt.h>
#include <cndev.h>
#include <cnml.h>

#if MGB_ENABLE_LOGGING
#define MGB_CNRT_CHECK(expr)                                          \
    do {                                                              \
        cnrtRet_t __cnrt_check_code = (expr);                         \
        if (mgb_unlikely(__cnrt_check_code != CNRT_RET_SUCCESS)) {    \
            ::mgb::_on_cnrt_error(#expr, __cnrt_check_code, __FILE__, \
                                  __func__, __LINE__);                \
        }                                                             \
    } while (0)
#define MGB_CNDEV_CHECK(expr)                                           \
    do {                                                                \
        cndevRet_t __cndev_check_code = (expr);                         \
        if (mgb_unlikely(__cndev_check_code != CNDEV_SUCCESS)) {        \
            ::mgb::_on_cndev_error(#expr, __cndev_check_code, __FILE__, \
                                   __func__, __LINE__);                 \
        }                                                               \
    } while (0)
#define MGB_CNML_CHECK(expr)                                          \
    do {                                                              \
        cnmlStatus_t __cnml_check_code = (expr);                      \
        if (mgb_unlikely(__cnml_check_code != CNML_STATUS_SUCCESS)) { \
            ::mgb::_on_cnml_error(#expr, __cnml_check_code, __FILE__, \
                                  __func__, __LINE__);                \
        }                                                             \
    } while (0)
#else
#define MGB_CNRT_CHECK(expr)                                       \
    do {                                                                \
        cnrtRet_t __cnrt_check_code = (expr);                           \
        if (mgb_unlikely(__cnrt_check_code != CNRT_RET_SUCCESS)) {      \
            ::mgb::_on_cnrt_error(#expr, __cnrt_check_code, "", "", 1); \
        }                                                               \
    } while (0)
#define MGB_CNDEV_CHECK(expr)                                               \
    do {                                                                    \
        cndevRet_t __cndev_check_code = (expr);                             \
        if (mgb_unlikely(__cndev_check_code != CNDEV_SUCCESS)) {            \
            ::mgb::_on_cndev_error(#expr, __cndev_check_code, __FILE__, "", \
                                   "", 1);                                  \
        }                                                                   \
    } while (0)
#define MGB_CNML_CHECK(expr)                                                  \
    do {                                                                      \
        cnmlStatus_t __cnml_check_code = (expr);                              \
        if (mgb_unlikely(__cnml_check_code != CNML_STATUS_SUCCESS)) {         \
            ::mgb::_on_cnml_error(#expr, __cnml_check_code, __FILE__, "", "", \
                                  1);                                         \
        }                                                                     \
    } while (0)
#endif  // MGB_ENABLE_LOGGING
#endif  // MGB_CAMBRICON

177
//! whether to enable asynchronous initialization for CompNode and CompNodeEnv
178
#define MGB_ENABLE_COMP_NODE_ASYNC_INIT (MGB_CUDA || MGB_ROCM)
179 180

//! whether AsyncErrorInfo is needed
181
#define MGB_NEED_MEGDNN_ASYNC_ERROR (MGB_CUDA || MGB_ROCM)
182 183 184 185 186 187 188 189 190 191 192

#if MGB_ENABLE_COMP_NODE_ASYNC_INIT
#include <atomic>
#include <future>
#endif

#include <memory>
#include <type_traits>
#include "megbrain/utils/thin/function.h"

namespace mgb {
193 194 195 196
#if MGB_ATLAS
[[noreturn]] void _on_atlas_error(const char* expr, aclError err,
                                  const char* file, const char* func, int line);
#endif
197

198

199 200 201
#if MGB_CUDA
[[noreturn]] void _on_cuda_error(const char* expr, cudaError_t err,
                                 const char* file, const char* func, int line);
202 203 204
[[noreturn]] void _on_cuda_cu_error(const char* expr, CUresult err,
                                    const char* file, const char* func,
                                    int line);
205 206 207
#endif


208 209 210 211 212
#if MGB_ROCM
[[noreturn]] void _on_hip_error(const char* expr, hipError_t err,
                                const char* file, const char* func, int line);
#endif

213 214 215 216 217 218 219 220 221 222
#if MGB_CAMBRICON
const char* cnml_get_error_string(cnmlStatus_t err);
[[noreturn]] void _on_cnrt_error(const char* expr, cnrtRet_t err,
                                 const char* file, const char* func, int line);
[[noreturn]] void _on_cndev_error(const char* expr, cndevRet_t err,
                                  const char* file, const char* func, int line);
[[noreturn]] void _on_cnml_error(const char* expr, cnmlStatus_t err,
                                 const char* file, const char* func, int line);
#endif

223 224 225 226 227 228 229 230 231 232 233 234
class CPUDispatcher : public MegcoreCPUDispatcher {
public:
    using AffinityCallBack = thin_function<void(size_t)>;
    //! get number of tasks already dispatched
    virtual size_t get_nr_dispatched_tasks() const = 0;
    //! set the cpu affinity callback, the callback is
    //! thin_function<void(size_t)>
    virtual void set_affinity(AffinityCallBack&& /*affinity_cb*/) {
        mgb_assert(0, "The CompNode set_affinity is not implement");
    }
};

235
using AtlasDispatcher = CPUDispatcher;
236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313

/*!
 * \brief CompNode environment
 *
 * CompNodeEnv contains necessary information to launch a kernel on a comp node,
 * or calling other libraries on a comp node. It has common fields for all comp
 * nodes and also specific fields for a given comp node type.
 *
 * Each CompNode is associated with a CompNodeEnv that could be retrieved by
 * CompNodeEnv::from_comp_node.
 *
 * Note: CUDA CompNodeEnv is initialized asynchronously. The env and property is
 * set synchronously, but m_lib_handle_manager would be initialized in the
 * future.
 */
class CompNodeEnv final : public NonCopyableObj {
public:
    using DeviceType = CompNode::DeviceType;
    using MemEventHandler =
            thin_function<void(size_t alloc_size, bool is_host, void* ptr)>;

    //! extra properties for a CompNodeEnv
    struct Property {
        //! type of the underlying device
        DeviceType type;

        //! alignment requirement in bytes, for memory allocating
        size_t mem_alignment = 0;
    };

    //! get user data by calling UserDataContainer::get_user_data_or_create;
    //! this method is thread-safe
    template <typename T, typename Maker>
    T& get_user_data(Maker&& maker) const {
        ensure_async_init_finished();
        MGB_LOCK_GUARD(m_user_data_container_mtx);
        return *m_user_data_container->get_user_data_or_create<T>(
                std::forward<Maker>(maker));
    }

    template <typename T>
    T& get_user_data() const {
        ensure_async_init_finished();
        MGB_LOCK_GUARD(m_user_data_container_mtx);
        return *m_user_data_container->get_user_data_or_create<T>(
                std::make_shared<T>);
    }

    //! check whether a user data object has been registered
    template <typename T>
    bool has_user_data() const {
        ensure_async_init_finished();
        MGB_LOCK_GUARD(m_user_data_container_mtx);
        return m_user_data_container->get_user_data<T>().second;
    }

    //! get property
    const Property& property() const { return m_property; }

    //! get the comp node to which this env belongs
    CompNode comp_node() const { return m_comp_node; }

    /*!
     * \brief create CompNodeEnv from comp_node
     */
    static inline const CompNodeEnv& from_comp_node(const CompNode& node);

    /*!
     * \brief activate this env for current thread
     *
     * Currently only calls cuda_env().activate() if type is cuda
     */
    void activate() const {
#if MGB_CUDA
        if (m_property.type == DeviceType::CUDA) {
            m_cuda_env.activate();
        }
#endif
314 315 316 317 318
#if MGB_ROCM
        if (m_property.type == DeviceType::ROCM) {
            m_rocm_env.activate();
        }
#endif
319 320 321 322 323 324 325 326 327 328 329
#if MGB_CAMBRICON
        if (m_property.type == DeviceType::CAMBRICON) {
            m_cnrt_env.activate();
        }
#endif
#if MGB_ATLAS
        if (m_property.type == DeviceType::ATLAS) {
            m_atlas_env.activate();
        }
#endif

330

331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
    }

    /*!
     * \brief set a callback to be invoked on alloc/free events
     * \param[in,out] handler the new handler to be set; the previous handler
     *      would be returned
     */
    void mem_event_handler(MemEventHandler& handler) {
        m_mem_event_handler.swap(handler);
    }

    //! invoke mem event handler on a mem event; only be called from CompNode
    void on_mem_event(size_t alloc_size, bool is_host, void* ptr) {
        if (m_mem_event_handler) {
            m_mem_event_handler(alloc_size, is_host, ptr);
        }
    }

        // following are impls for various envs

#if MGB_CUDA
    struct CudaEnv {
        int device = -1;
        cudaStream_t stream = 0;
        cudaDeviceProp device_prop;

        void activate() const { MGB_CUDA_CHECK(cudaSetDevice(device)); }
    };

    const CudaEnv& cuda_env() const {
        if (mgb_unlikely(m_property.type != DeviceType::CUDA))
            on_bad_device_type(DeviceType::CUDA);
        ensure_async_init_finished();
        return m_cuda_env;
    }

    //! init this as a cuda env asynchronously
    void init_cuda_async(int dev, CompNode comp_node,
                         const ContinuationCtx<cudaStream_t>& cont);
#endif

372

373 374 375 376 377 378 379 380 381 382 383 384
#if MGB_ATLAS
    struct AtlasEnv {
        int device = -1;
        aclrtStream stream = 0;

        struct InitStatus {
            bool initialized;
            Spinlock mtx;
            InitStatus() : initialized{false} {}
            void init() {
                MGB_LOCK_GUARD(mtx);
                if (!initialized) {
385 386 387
                    const char* config_path =
                            MGB_GETENV("MGB_ATLAS_PROFILE_JSON");
                    auto acl_err = aclInit(config_path);
388 389
                    initialized = acl_err == ACL_ERROR_NONE;
                    mgb_throw_if(!initialized, AtlasError,
390 391
                                 "acl initialize failed: (acl: %s)",
                                 megcore::atlas::get_error_str(acl_err));
392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408
                }
            }
            ~InitStatus() {
                MGB_LOCK_GUARD(mtx);
                if (initialized) {
                    initialized = false;
                }
            }
        };
        static InitStatus init_status;

        static void init() {
            init_status.init();
        }

        void activate() const {
            init();
409 410 411 412 413 414 415 416 417 418
            int32_t device_id = -1;
            auto err = aclrtGetDevice(&device_id);
            if (err == ACL_ERROR_INVALID_DEVICE || device != device_id) {
                MGB_ATLAS_CHECK(aclrtSetDevice(device));
            } else {
                MGB_ATLAS_CHECK(err);
                mgb_assert(err == ACL_ERROR_NONE,
                           "Failed to invoke aclrtGetDevice, get %s(%d)",
                           megcore::atlas::get_error_str(err), err);
            }
419 420 421 422 423 424 425 426 427 428 429 430 431 432
        }
    };

    const AtlasEnv& atlas_env() const {
        if (mgb_unlikely(m_property.type != DeviceType::ATLAS))
            on_bad_device_type(DeviceType::ATLAS);
        ensure_async_init_finished();
        return m_atlas_env;
    }

    //! init this as a atlas env synchronously
    void init_atlas(CompNode comp_node, const AtlasEnv& env);
#endif

433 434


435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456
#if MGB_ROCM
    struct ROCmEnv {
        int device = -1;
        hipStream_t stream = 0;
        hipDeviceProp_t device_prop;

        void activate() const { MGB_ROCM_CHECK(hipSetDevice(device)); }
    };

    const ROCmEnv& rocm_env() const {
        if (mgb_unlikely(m_property.type != DeviceType::ROCM))
            on_bad_device_type(DeviceType::ROCM);
        ensure_async_init_finished();
        return m_rocm_env;
    }

    //! init this as a rocm env asynchronously
    void init_rocm_async(int dev, CompNode comp_node,
                         const ContinuationCtx<hipStream_t>& cont);

#endif

457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493
#if MGB_CAMBRICON
    struct CnrtEnv {
        int device = -1;
        cnrtQueue_t queue = nullptr;
        cnrtDeviceInfo_t device_info;
        struct InitStatus {
            bool initialized;
            Spinlock mtx;
            InitStatus() : initialized{false} {}
            void init() {
                MGB_LOCK_GUARD(mtx);
                if (!initialized) {
                    auto cnrt_err = cnrtInit(0);
                    initialized = cnrt_err == CNRT_RET_SUCCESS;
                    auto cndev_err = cndevInit(0);
                    initialized &= cndev_err == CNDEV_SUCCESS;
                    auto cnml_err = cnmlInit(0);
                    initialized &= cnml_err == CNML_STATUS_SUCCESS;
                    mgb_throw_if(!initialized, CnrtError,
                                 "cnrt/cndev/cnml initialize failed: (cnrt:%d, "
                                 "cndev:%d, cnml: %d)",
                                 static_cast<int>(cnrt_err),
                                 static_cast<int>(cndev_err),
                                 static_cast<int>(cnml_err));
                }
            }
            ~InitStatus() {
                if (initialized) {
                    MGB_CNML_CHECK(cnmlExit());
                    MGB_CNDEV_CHECK(cndevRelease());
                    cnrtDestroy();
                    initialized = false;
                }
            }
        };
        static InitStatus init_status;

494
        static void init() { init_status.init(); }
495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512

        void activate() const {
            init();
            cnrtDev_t dev;
            MGB_CNRT_CHECK(cnrtGetDeviceHandle(&dev, device));
            MGB_CNRT_CHECK(cnrtSetCurrentDevice(dev));
        }
    };

    const CnrtEnv& cnrt_env() const {
        if (mgb_unlikely(m_property.type != DeviceType::CAMBRICON))
            on_bad_device_type(DeviceType::CAMBRICON);
        return m_cnrt_env;
    }

    void init_cnrt(int dev, CompNode comp_node,
                   const ContinuationCtx<cnrtQueue_t>& cont);
#endif
513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551

    struct CpuEnv {
        using Task = CPUDispatcher::Task;
        using MultiThreadingTask = CPUDispatcher::MultiThreadingTask;
        using AffinityCallBack = thin_function<void(size_t)>;

        std::shared_ptr<CPUDispatcher> dispatcher;

        void dispatch(Task&& task) const {
            dispatcher->dispatch(std::move(task));
        }

        void dispatch(MultiThreadingTask&& task, size_t parallelism) const {
            dispatcher->dispatch(std::move(task), parallelism);
        }

        void set_affinity(AffinityCallBack&& cb) const {
            dispatcher->set_affinity(std::move(cb));
        }
    };

    const CpuEnv& cpu_env() const {
        if (mgb_unlikely(m_property.type != DeviceType::CPU))
            on_bad_device_type(DeviceType::CPU);
        return m_cpu_env;
    }

    //! init this as a cpu env
    void init_cpu(const CpuEnv& env, CompNode comp_node);

    void fini();

private:
    CompNode m_comp_node;
    Property m_property;
    MemEventHandler m_mem_event_handler;

#if MGB_CUDA
    CudaEnv m_cuda_env;
552 553 554 555
#endif
#if MGB_ATLAS
    AtlasEnv m_atlas_env;
#endif
556 557 558
#if MGB_ROCM
    ROCmEnv m_rocm_env;
#endif
559 560
#if MGB_CAMBRICON
    CnrtEnv m_cnrt_env;
561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650
#endif
    CpuEnv m_cpu_env;

    std::unique_ptr<UserDataContainer> m_user_data_container;
    mutable RecursiveSpinlock m_user_data_container_mtx;

    [[noreturn]] void on_bad_device_type(DeviceType expected) const;

#if MGB_ENABLE_COMP_NODE_ASYNC_INIT
    //! whether async init is in future; set by init*_async methods
    std::atomic_bool m_async_init_need_wait{false};
    std::mutex m_async_init_mtx;
    std::future<void> m_async_init_future;
    std::thread::id m_async_init_tid;

    void ensure_async_init_finished() const {
        if (m_async_init_need_wait.load()) {
            const_cast<CompNodeEnv*>(this)->wait_async_init();
        }
    }

    void wait_async_init();
#else
    void ensure_async_init_finished() const {}
#endif
};

//! megdnn handle stored in a CompNodeEnv
class MegDNNHandle final : public UserDataContainer::UserData,
                           public std::enable_shared_from_this<MegDNNHandle> {
    MGB_TYPEINFO_OBJ_DECL;

    static int sm_default_dbg_level;
    megcoreDeviceHandle_t m_dev_hdl = nullptr;
    megcoreComputingHandle_t m_comp_hdl = nullptr;
    std::unique_ptr<megdnn::Handle> m_megdnn_handle;

#if MGB_NEED_MEGDNN_ASYNC_ERROR
    std::shared_ptr<megcore::AsyncErrorInfo> m_async_error_info_devptr;
    megcore::AsyncErrorInfo* make_async_error_info(const CompNodeEnv& env);
#endif

public:
    MegDNNHandle(const CompNodeEnv& env);
    ~MegDNNHandle() noexcept;

    static MegDNNHandle& get(const CompNodeEnv& env);

    megdnn::Handle* operator->() const { return handle(); }

    megdnn::Handle* handle() const { return m_megdnn_handle.get(); }

    //! set the default debug level; return original setting
    static int exchange_default_dbg_level(int level) {
        auto ret = sm_default_dbg_level;
        sm_default_dbg_level = level;
        return ret;
    }

#if MGB_NEED_MEGDNN_ASYNC_ERROR
    /*!
     * \brief get pointer to underlying AsyncErrorInfo
     *
     * return nullptr if the device does not need async error report.
     */
    megcore::AsyncErrorInfo* async_error_info_devptr() const {
        return m_async_error_info_devptr.get();
    }
#endif
};

class CompNode::Impl : public CompNode::ImplBase {
protected:
    CompNodeEnv m_env;

    using ImplBase::ImplBase;
    ~Impl() = default;

public:
    CompNodeEnv& env() { return m_env; }
};

const CompNodeEnv& CompNodeEnv::from_comp_node(const CompNode& node) {
    mgb_assert(node.valid());
    return static_cast<CompNode::Impl*>(node.m_impl)->env();
}

}  // namespace mgb

// vim: syntax=cpp.doxygen foldmethod=marker foldmarker=f{{{,f}}}