From ac65834c4071d6b14d38dc14ef0d282ed7d75ddb Mon Sep 17 00:00:00 2001 From: Ahsan Saghir Date: Tue, 30 Jun 2026 09:03:01 -0500 Subject: [PATCH 1/5] Change benchmark mxr file name to use __ instead of :: --- src/targets/gpu/compile_ops.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/targets/gpu/compile_ops.cpp b/src/targets/gpu/compile_ops.cpp index 91d817765f4..8edb48631ef 100644 --- a/src/targets/gpu/compile_ops.cpp +++ b/src/targets/gpu/compile_ops.cpp @@ -516,7 +516,9 @@ struct compile_plan mm->add_instruction(builtin::comment{comment_text}, {}); auto problem_hash = std::hash{}(to_string(config->problem)); - auto mxr_file = mxr_dir / (preop.name() + "_" + std::to_string(i) + "_" + + // Sanitize the op name for use in a filename. Replace "::" with "__". + auto op_filename = replace_string(preop.name(), "::", "__"); + auto mxr_file = mxr_dir / (op_filename + "_" + std::to_string(i) + "_" + std::to_string(problem_hash) + ".mxr"); log::info() << "Saving benchmark binary: " << mxr_file; save(bench_prog, mxr_file.string()); From b13c7498bbf375f93c1e2ee2b2626ce61d836515 Mon Sep 17 00:00:00 2001 From: Ahsan Saghir Date: Tue, 30 Jun 2026 10:35:20 -0500 Subject: [PATCH 2/5] Use sanitize_filename function to replace invalid Windows characters --- src/include/migraphx/filesystem.hpp | 14 +++++++++++++- src/pass_manager.cpp | 12 +----------- src/targets/gpu/compile_ops.cpp | 5 ++--- 3 files changed, 16 insertions(+), 15 deletions(-) diff --git a/src/include/migraphx/filesystem.hpp b/src/include/migraphx/filesystem.hpp index 555ef18a82e..f090962b89b 100644 --- a/src/include/migraphx/filesystem.hpp +++ b/src/include/migraphx/filesystem.hpp @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. + * Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,6 +25,9 @@ #define MIGRAPHX_GUARD_RTGLIB_FILESYSTEM_HPP #include +#include +#include +#include #if defined(CPPCHECK) #define MIGRAPHX_HAS_FILESYSTEM 1 @@ -73,6 +76,15 @@ namespace fs = ::std::filesystem; namespace fs = ::std::experimental::filesystem; #endif +// Replace invalid characters for Windows with '_'. +inline std::string sanitize_filename(std::string s) +{ + static constexpr std::string_view invalid = "<>:\"/\\|?*"; + std::replace_if( + s.begin(), s.end(), [](char c) { return invalid.find(c) != std::string_view::npos; }, '_'); + return s; +} + } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx diff --git a/src/pass_manager.cpp b/src/pass_manager.cpp index 999465ede7c..45f70de319f 100644 --- a/src/pass_manager.cpp +++ b/src/pass_manager.cpp @@ -35,10 +35,8 @@ #include #include #include -#include #include #include -#include namespace migraphx { inline namespace MIGRAPHX_INLINE_NS { @@ -159,14 +157,6 @@ struct module_pm : module_pass_manager validate_pass(*mod, p, *t); } - static void sanitize(std::string& s) - { - static constexpr std::string_view invalid = "<>:\"/\\|?*"; - - std::replace_if( - s.begin(), s.end(), [](char c) { return invalid.find(c) != std::string::npos; }, '_'); - } - template void try_and_dump_on_error(const pass& p, F f) const { @@ -189,7 +179,7 @@ struct module_pm : module_pass_manager std::string base = p.name() + std::to_string(clk) + ".mxr"; #if defined(_WIN32) // On Windows, some pass names may contain invalid characters for filenames - sanitize(base); + base = sanitize_filename(base); #endif fs::path fname = dirname / base; log::error() << "Dump: " << fname; diff --git a/src/targets/gpu/compile_ops.cpp b/src/targets/gpu/compile_ops.cpp index 8edb48631ef..30adf0fa660 100644 --- a/src/targets/gpu/compile_ops.cpp +++ b/src/targets/gpu/compile_ops.cpp @@ -516,9 +516,8 @@ struct compile_plan mm->add_instruction(builtin::comment{comment_text}, {}); auto problem_hash = std::hash{}(to_string(config->problem)); - // Sanitize the op name for use in a filename. Replace "::" with "__". - auto op_filename = replace_string(preop.name(), "::", "__"); - auto mxr_file = mxr_dir / (op_filename + "_" + std::to_string(i) + "_" + + auto op_filename = sanitize_filename(preop.name()); + auto mxr_file = mxr_dir / (op_filename + "_" + std::to_string(i) + "_" + std::to_string(problem_hash) + ".mxr"); log::info() << "Saving benchmark binary: " << mxr_file; save(bench_prog, mxr_file.string()); From eb954871b10563d479b0cf629e82f124ef89c2cf Mon Sep 17 00:00:00 2001 From: Ahsan Saghir Date: Tue, 30 Jun 2026 11:02:35 -0500 Subject: [PATCH 3/5] Move sanitize_filename() to fileutils.hpp --- src/fileutils.cpp | 13 ++++++++++++- src/include/migraphx/filesystem.hpp | 14 +------------- src/include/migraphx/fileutils.hpp | 1 + src/pass_manager.cpp | 1 + src/targets/gpu/compile_ops.cpp | 1 + 5 files changed, 16 insertions(+), 14 deletions(-) diff --git a/src/fileutils.cpp b/src/fileutils.cpp index 402e0113ef5..0987e682d99 100644 --- a/src/fileutils.cpp +++ b/src/fileutils.cpp @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -23,6 +23,9 @@ */ #include +#include +#include +#include namespace migraphx { inline namespace MIGRAPHX_INLINE_NS { @@ -66,5 +69,13 @@ fs::path append_extension(const fs::path& path, std::string_view ext) return fs::path{path}.replace_extension(path.extension().string().append(ext)); } +std::string sanitize_filename(std::string s) +{ + static constexpr std::string_view invalid = "<>:\"/\\|?*"; + std::replace_if( + s.begin(), s.end(), [](char c) { return invalid.find(c) != std::string_view::npos; }, '_'); + return s; +} + } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx diff --git a/src/include/migraphx/filesystem.hpp b/src/include/migraphx/filesystem.hpp index f090962b89b..555ef18a82e 100644 --- a/src/include/migraphx/filesystem.hpp +++ b/src/include/migraphx/filesystem.hpp @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved. + * Copyright (c) 2015-2023 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -25,9 +25,6 @@ #define MIGRAPHX_GUARD_RTGLIB_FILESYSTEM_HPP #include -#include -#include -#include #if defined(CPPCHECK) #define MIGRAPHX_HAS_FILESYSTEM 1 @@ -76,15 +73,6 @@ namespace fs = ::std::filesystem; namespace fs = ::std::experimental::filesystem; #endif -// Replace invalid characters for Windows with '_'. -inline std::string sanitize_filename(std::string s) -{ - static constexpr std::string_view invalid = "<>:\"/\\|?*"; - std::replace_if( - s.begin(), s.end(), [](char c) { return invalid.find(c) != std::string_view::npos; }, '_'); - return s; -} - } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx diff --git a/src/include/migraphx/fileutils.hpp b/src/include/migraphx/fileutils.hpp index 64bf98442cf..b63fde56372 100644 --- a/src/include/migraphx/fileutils.hpp +++ b/src/include/migraphx/fileutils.hpp @@ -36,6 +36,7 @@ MIGRAPHX_EXPORT fs::path make_shared_object_filename(std::string_view name); MIGRAPHX_EXPORT fs::path make_object_file_filename(std::string_view name); MIGRAPHX_EXPORT fs::path make_static_library_filename(std::string_view name); MIGRAPHX_EXPORT fs::path append_extension(const fs::path& path, std::string_view ext); +MIGRAPHX_EXPORT std::string sanitize_filename(std::string s); inline std::string operator+(std::string l, const fs::path& r) { return std::move(l) + r.string(); } diff --git a/src/pass_manager.cpp b/src/pass_manager.cpp index 45f70de319f..37931b6d2fe 100644 --- a/src/pass_manager.cpp +++ b/src/pass_manager.cpp @@ -31,6 +31,7 @@ #include #include #include +#include #include #include #include diff --git a/src/targets/gpu/compile_ops.cpp b/src/targets/gpu/compile_ops.cpp index 30adf0fa660..1c8f9a428e6 100644 --- a/src/targets/gpu/compile_ops.cpp +++ b/src/targets/gpu/compile_ops.cpp @@ -38,6 +38,7 @@ #include #include #include +#include #include #include #include From 9c1d6dcf651d5087435cdc731e76977bca65b8bc Mon Sep 17 00:00:00 2001 From: Ahsan Saghir Date: Tue, 30 Jun 2026 11:15:58 -0500 Subject: [PATCH 4/5] Address co-pilot comments, add tests --- src/include/migraphx/fileutils.hpp | 1 + src/pass_manager.cpp | 2 +- test/fileutils.cpp | 26 +++++++++++++++++++++++++- 3 files changed, 27 insertions(+), 2 deletions(-) diff --git a/src/include/migraphx/fileutils.hpp b/src/include/migraphx/fileutils.hpp index b63fde56372..383dd2054e1 100644 --- a/src/include/migraphx/fileutils.hpp +++ b/src/include/migraphx/fileutils.hpp @@ -26,6 +26,7 @@ #define MIGRAPHX_GUARD_MIGRAPHLIB_FILEUTILS_HPP #include +#include #include namespace migraphx { diff --git a/src/pass_manager.cpp b/src/pass_manager.cpp index 37931b6d2fe..20cc82ad87a 100644 --- a/src/pass_manager.cpp +++ b/src/pass_manager.cpp @@ -180,7 +180,7 @@ struct module_pm : module_pass_manager std::string base = p.name() + std::to_string(clk) + ".mxr"; #if defined(_WIN32) // On Windows, some pass names may contain invalid characters for filenames - base = sanitize_filename(base); + base = sanitize_filename(std::move(base)); #endif fs::path fname = dirname / base; log::error() << "Dump: " << fname; diff --git a/test/fileutils.cpp b/test/fileutils.cpp index cb737a10777..d939e720cef 100644 --- a/test/fileutils.cpp +++ b/test/fileutils.cpp @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -90,4 +90,28 @@ TEST_CASE(append_file_extension) EXPECT(updated == std::string{baze_name}.append(txt).append(bz2)); } +TEST_CASE(sanitize_filename_replaces_invalid_chars) +{ + // Each Windows-invalid character maps to a single '_'. + EXPECT(migraphx::sanitize_filename("a:b") == "a_b"); + EXPECT(migraphx::sanitize_filename("a*b") == "a_b"); + EXPECT(migraphx::sanitize_filename("a?b") == "a_b"); + EXPECT(migraphx::sanitize_filename("a\\b") == "a_b"); + EXPECT(migraphx::sanitize_filename("a/b") == "a_b"); + EXPECT(migraphx::sanitize_filename("ac|d\"e") == "a_b_c_d_e"); +} + +TEST_CASE(sanitize_filename_double_colon) +{ + // "::" in op names (e.g. "gpu::mlir_op") becomes "__". + EXPECT(migraphx::sanitize_filename("gpu::mlir_op") == "gpu__mlir_op"); +} + +TEST_CASE(sanitize_filename_no_invalid_chars) +{ + // Valid names are returned unchanged. + EXPECT(migraphx::sanitize_filename("gpu_mlir_op_8_123.mxr") == "gpu_mlir_op_8_123.mxr"); + EXPECT(migraphx::sanitize_filename("") == ""); +} + int main(int argc, const char* argv[]) { test::run(argc, argv); } From 96d63f01e8dd83953af6efe3212ed70cb0543db0 Mon Sep 17 00:00:00 2001 From: Ahsan Saghir Date: Tue, 30 Jun 2026 11:51:08 -0500 Subject: [PATCH 5/5] Fix license and add header --- src/include/migraphx/fileutils.hpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/include/migraphx/fileutils.hpp b/src/include/migraphx/fileutils.hpp index 383dd2054e1..df03378a952 100644 --- a/src/include/migraphx/fileutils.hpp +++ b/src/include/migraphx/fileutils.hpp @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2015-2024 Advanced Micro Devices, Inc. All rights reserved. + * Copyright (c) 2015-2026 Advanced Micro Devices, Inc. All rights reserved. * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -28,6 +28,7 @@ #include #include #include +#include namespace migraphx { inline namespace MIGRAPHX_INLINE_NS {