From d2b317f5dd969967ed003e25c930ead27e1f44fd Mon Sep 17 00:00:00 2001 From: tqchen Date: Mon, 27 Apr 2026 15:17:10 +0000 Subject: [PATCH 1/2] [REFACTOR] Move source_utils.h into runtime/opencl `src/runtime/source_utils.h` is only used by OpenCL backend code. Move it into the opencl subdir, inline `SplitKernels` (single short function), and delete the now-empty `.cc`. --- src/runtime/opencl/opencl_module.cc | 2 +- src/runtime/opencl/opencl_module_spirv.cc | 2 +- .../source_utils.h} | 23 +++++++--- src/runtime/source_utils.h | 44 ------------------- 4 files changed, 20 insertions(+), 51 deletions(-) rename src/runtime/{source_utils.cc => opencl/source_utils.h} (69%) delete mode 100644 src/runtime/source_utils.h diff --git a/src/runtime/opencl/opencl_module.cc b/src/runtime/opencl/opencl_module.cc index c5afaeba7d32..c8ccbfa09f18 100644 --- a/src/runtime/opencl/opencl_module.cc +++ b/src/runtime/opencl/opencl_module.cc @@ -30,8 +30,8 @@ #include #include "../../support/bytes_io.h" -#include "../source_utils.h" #include "opencl_common.h" +#include "source_utils.h" namespace tvm { namespace runtime { diff --git a/src/runtime/opencl/opencl_module_spirv.cc b/src/runtime/opencl/opencl_module_spirv.cc index 12b6df9aa1a5..67e7b1c1d5d7 100644 --- a/src/runtime/opencl/opencl_module_spirv.cc +++ b/src/runtime/opencl/opencl_module_spirv.cc @@ -25,10 +25,10 @@ #include #include "../../support/bytes_io.h" -#include "../source_utils.h" #include "../spirv/spirv_shader.h" #include "opencl_common.h" #include "opencl_module.h" +#include "source_utils.h" namespace tvm { namespace runtime { diff --git a/src/runtime/source_utils.cc b/src/runtime/opencl/source_utils.h similarity index 69% rename from src/runtime/source_utils.cc rename to src/runtime/opencl/source_utils.h index e1cf94e52e18..708b76a9c580 100644 --- a/src/runtime/source_utils.cc +++ b/src/runtime/opencl/source_utils.h @@ -18,15 +18,26 @@ */ /*! - * \file source_utils.cc + * \file source_utils.h + * \brief Minimum source manipulation utils for the OpenCL runtime. */ -#include "source_utils.h" + +#ifndef TVM_RUNTIME_OPENCL_SOURCE_UTILS_H_ +#define TVM_RUNTIME_OPENCL_SOURCE_UTILS_H_ + +#include +#include namespace tvm { namespace runtime { - -std::unordered_map SplitKernels(std::string source, - std::string delimiter) { +/*! + * \brief Split the source file on separate kernels by specified delimiter. + * \param source The source code of the kernels. + * \param delimiter The delimiter which is using for splitting kernels. + * \return Mapping from primitive name to kernel source + */ +inline std::unordered_map SplitKernels( + std::string source, std::string delimiter = "// Function: ") { std::unordered_map split_kernels; if (source.size()) { size_t begin = source.find(delimiter); @@ -47,3 +58,5 @@ std::unordered_map SplitKernels(std::string source, } } // namespace runtime } // namespace tvm + +#endif // TVM_RUNTIME_OPENCL_SOURCE_UTILS_H_ diff --git a/src/runtime/source_utils.h b/src/runtime/source_utils.h deleted file mode 100644 index 5476585b945c..000000000000 --- a/src/runtime/source_utils.h +++ /dev/null @@ -1,44 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one - * or more contributor license agreements. See the NOTICE file - * distributed with this work for additional information - * regarding copyright ownership. The ASF licenses this file - * to you under the Apache License, Version 2.0 (the - * "License"); you may not use this file except in compliance - * with the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, - * software distributed under the License is distributed on an - * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY - * KIND, either express or implied. See the License for the - * specific language governing permissions and limitations - * under the License. - */ - -/*! - * \file source_utils.h - * \brief Minimum source manipulation utils for runtime. - */ - -#ifndef TVM_RUNTIME_SOURCE_UTILS_H_ -#define TVM_RUNTIME_SOURCE_UTILS_H_ - -#include -#include - -namespace tvm { -namespace runtime { -/*! - * \brief Split the source file on separate kernels by specified delimiter. - * \param source The source code of the kernels. - * \param delimiter The delimiter which is using for splitting kernels. - * \return Mapping from primitive name to kernel source - */ -std::unordered_map SplitKernels(std::string source, - std::string delimiter = "// Function: "); -} // namespace runtime -} // namespace tvm - -#endif // TVM_RUNTIME_SOURCE_UTILS_H_ From f0641f4663f6aaab8d8bab36be679af10f0065d7 Mon Sep 17 00:00:00 2001 From: tqchen Date: Mon, 27 Apr 2026 16:53:22 +0000 Subject: [PATCH 2/2] [REFACTOR][RUNTIME] Pass SplitKernels source by const reference The header-only inline form copies the OpenCL source string on every call; const reference avoids the copy. The function only reads the parameters. --- src/runtime/opencl/source_utils.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/runtime/opencl/source_utils.h b/src/runtime/opencl/source_utils.h index 708b76a9c580..9f17d670128d 100644 --- a/src/runtime/opencl/source_utils.h +++ b/src/runtime/opencl/source_utils.h @@ -37,7 +37,7 @@ namespace runtime { * \return Mapping from primitive name to kernel source */ inline std::unordered_map SplitKernels( - std::string source, std::string delimiter = "// Function: ") { + const std::string& source, const std::string& delimiter = "// Function: ") { std::unordered_map split_kernels; if (source.size()) { size_t begin = source.find(delimiter);