Skip to content
Merged
Changes from 6 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
51 changes: 46 additions & 5 deletions src/targets/gpu/mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
* THE SOFTWARE.
*/
#include <algorithm>
#include <atomic>
#include <cstdint>
#include <migraphx/shape.hpp>
#include <migraphx/algorithm.hpp>
Expand Down Expand Up @@ -163,6 +164,13 @@ using mlir_tuning_space = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirRockTuningSpace,
using mlir_tuning_param = MIGRAPHX_MANAGE_MLIR_HANDLE(MlirRockTuningParam,
mlirRockTuningParamDestroy);

static std::atomic<int>& dump_counter()
{
// NOLINTNEXTLINE
static std::atomic<int> c = 0;
return c;
}

static std::string_view to_string_view(MlirStringRef s) { return {s.data, s.length}; }

static MlirStringRef make_mlir_string_ref(const std::string_view& s)
Expand Down Expand Up @@ -1094,6 +1102,20 @@ std::string dump_mlir(module m, const std::vector<shape>& inputs)
return mlir_print(&mlirOperationPrint, mod_op);
}

static void abbreviate_symbol_names(std::string& n)
{
static const std::map<std::string, std::string> abbrs = {
{"reduce_max_reshape_sub_exp_reshape_reduce_sum_reshape_div", "softmax"},
{"reshape", "rsp"},
{"transpose", "trp"},
{"slice", "slc"}};

for(auto const& [key, val] : abbrs)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is a bug lurking here, should the loop iterate over the string reshape before the longer string (for softmax) which also has the same substring inside it. It is just that the std::map would be in an order which works here. But change it to a another data structure, e.g. a hash, and the renaming scheme could be a surprise for us :-)

{
replace_string_inplace(n, key, val);
}
}

static std::string compute_dump_name(const module& m, const std::string& ext)
{
std::vector<instruction_ref> sizes;
Expand All @@ -1102,11 +1124,30 @@ static std::string compute_dump_name(const module& m, const std::string& ext)
if(contains({"quant_convolution", "quant_dot", "convolution", "dot"}, ins->name()))
sizes.insert(sizes.end(), ins->inputs().begin(), ins->inputs().end());
}
auto name =
mlir_program::get_symbol_name(m) + "_" + shape::to_sizes_string(to_shapes(sizes)) + ext;
replace_string_inplace(name, ", ", "_");
replace_string_inplace(name, ":", "s");
return name;
auto shape_str = "_" + shape::to_sizes_string(to_shapes(sizes));
auto sym_names = mlir_program::get_symbol_name(m);
abbreviate_symbol_names(sym_names);

const int max_file_length = 255;
std::string fname;
if(sym_names.length() + shape_str.length() + ext.length() > max_file_length)
{
auto cnt = "_" + std::to_string(dump_counter()++);
auto cutoff = max_file_length - shape_str.length() - ext.length() - cnt.length();
Copy link

Copilot AI Aug 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cutoff calculation could result in a negative value if the combined length of shape_str, ext, and cnt exceeds max_file_length. This could cause undefined behavior in the substr call on line 1137.

Copilot uses AI. Check for mistakes.

// If the shapes are too big, the filename will be unparsable anyways so simply name it
// mlir_x to avoid erroring out
fname =
cutoff > 0 ? sym_names.substr(0, cutoff) + shape_str + cnt + ext : "mlir" + cnt + ext;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If a file with a name mlir0 or mlir1 is acceptable, then why not just dump every file with that consistent scheme: mlir_1, mlir_2? That was my point earlier.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because if you use this flag with a large model you might get 10s of mlir_ files, you would rather open every single one to see what they are about? I think if would be nice to see straight away see what kind of kernel each file is representing by a quick look at the name. As I mentioned in the other comment, I'm happy to do this if the mlir team would prefer this :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please have a consistent naming scheme. The MLIR team is okay with either naming scheme, but we shouldn't have two naming schemes. You can either take the first approach, but you can truncate the long name to the appropriate length and add a numeral to it, if you want to see it straight away, a very personal approach that I find very subjective. So either please truncate the full name, or have a simple naming scheme. We should not add confusion while debugging, IMHO!

else
{
fname = sym_names + shape_str + ext;
}

replace_string_inplace(fname, ", ", "_");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is shortening the file name by replacing 2 chars , with a single _, then this should be handled while accounting with new logic of name truncation.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, totally missed the space after the comma

replace_string_inplace(fname, ":", "s");
return fname;
}

void dump_mlir_to_file(module m, const std::vector<shape>& inputs, const fs::path& location)
Expand Down
Loading