Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion src/fileutils.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -23,6 +23,9 @@
*/

#include <migraphx/fileutils.hpp>
#include <algorithm>
#include <string>
#include <string_view>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Expand Down Expand Up @@ -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;
}
Comment thread
ahsan-ca marked this conversation as resolved.

} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
5 changes: 4 additions & 1 deletion src/include/migraphx/fileutils.hpp
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -26,7 +26,9 @@
#define MIGRAPHX_GUARD_MIGRAPHLIB_FILEUTILS_HPP

#include <migraphx/filesystem.hpp>
#include <string>
#include <string_view>
#include <utility>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Expand All @@ -36,6 +38,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);
Comment thread
ahsan-ca marked this conversation as resolved.

inline std::string operator+(std::string l, const fs::path& r) { return std::move(l) + r.string(); }

Expand Down
13 changes: 2 additions & 11 deletions src/pass_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,13 @@
#include <migraphx/time.hpp>
#include <migraphx/iterator_for.hpp>
#include <migraphx/filesystem.hpp>
#include <migraphx/fileutils.hpp>
#include <migraphx/load_save.hpp>
#include <migraphx/logger.hpp>
#include <iostream>
#include <sstream>
#include <algorithm>
#include <utility>
#include <string>
#include <string_view>

namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
Expand Down Expand Up @@ -159,14 +158,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 <class F>
void try_and_dump_on_error(const pass& p, F f) const
{
Expand All @@ -189,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
sanitize(base);
base = sanitize_filename(std::move(base));
#endif
fs::path fname = dirname / base;
log::error() << "Dump: " << fname;
Expand Down
4 changes: 3 additions & 1 deletion src/targets/gpu/compile_ops.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
#include <migraphx/builtin.hpp>
#include <migraphx/load_save.hpp>
#include <migraphx/filesystem.hpp>
#include <migraphx/fileutils.hpp>
#include <migraphx/json.hpp>
#include <migraphx/gpu/compiler.hpp>
#include <migraphx/gpu/compile_ops.hpp>
Expand Down Expand Up @@ -516,7 +517,8 @@ struct compile_plan

mm->add_instruction(builtin::comment{comment_text}, {});
auto problem_hash = std::hash<std::string>{}(to_string(config->problem));
auto mxr_file = mxr_dir / (preop.name() + "_" + 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");
Comment on lines +520 to 522
log::info() << "Saving benchmark binary: " << mxr_file;
save(bench_prog, mxr_file.string());
Expand Down
26 changes: 25 additions & 1 deletion test/fileutils.cpp
Original file line number Diff line number Diff line change
@@ -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
Expand Down Expand Up @@ -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("a<b>c|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); }
Loading