diff --git a/ethosu/regor/test/util.cpp b/ethosu/regor/test/util.cpp index 6842308c1fcb22259362fe188253690ec3a28e9d..617f495cc5305b1fc1d33270e4e003dee539bf71 100644 --- a/ethosu/regor/test/util.cpp +++ b/ethosu/regor/test/util.cpp @@ -1,5 +1,5 @@ // -// SPDX-FileCopyrightText: Copyright 2024 Arm Limited and/or its affiliates +// SPDX-FileCopyrightText: Copyright 2024-2025 Arm Limited and/or its affiliates // // SPDX-License-Identifier: Apache-2.0 // @@ -21,6 +21,9 @@ #include "common/data_type.hpp" #include "common/ini_reader.hpp" +#include +#include + using namespace regor; // Helpers for Architecture @@ -182,14 +185,38 @@ std::shared_ptr CreateSchedulerTensor(const std::string &name, return schedTensor; } +static struct temporary_op_scope_t +{ + std::vector> _ops; + std::mutex _lock; + + void add_op(const std::shared_ptr &op) + { + std::lock_guard lock(_lock); + _ops.push_back(op); + } + + ~temporary_op_scope_t() + { + try + { + for ( const auto &op : _ops ) + op->Disconnect(); + } + catch ( std::bad_weak_ptr & ) + { + } + } +} s_ops; + + // Create a SchedulerOperation with unary input std::unique_ptr CreateSchedulerOperation(OpType opType, TensorUsage ifmUsage, std::shared_ptr &ifm, TensorUsage ofmUsage, std::shared_ptr &ofm) { // use static vector to keep operation reference alive - static std::vector> ops; auto op = CreateOperation(opType, ifmUsage, ifm->srcTensor, ofmUsage, ofm->srcTensor); - ops.push_back(op); + s_ops.add_op(op); auto schedOp = std::make_unique(opType); schedOp->_srcKey = static_cast(op.get());