Skip to content

Commit

Permalink
first pass
Browse files Browse the repository at this point in the history
  • Loading branch information
richagadgil committed Oct 23, 2024
1 parent 0fb79b4 commit bb33f43
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/targets/gpu/include/migraphx/gpu/mlir.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ namespace gpu {

MIGRAPHX_GPU_EXPORT std::string dump_mlir(module m);
MIGRAPHX_GPU_EXPORT std::string dump_mlir(module m, const std::vector<shape>& inputs);
MIGRAPHX_GPU_EXPORT std::string dump_mlir(module m, const std::vector<shape>& inputs, const fs::path& location);

MIGRAPHX_GPU_EXPORT bool
is_module_fusible(const module& m, const context& migraphx_ctx, const value& solution);
Expand Down
7 changes: 7 additions & 0 deletions src/targets/gpu/jit/mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ inline namespace MIGRAPHX_INLINE_NS {
namespace gpu {

MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_MLIR_DUMP_TO_MXR);
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_MLIR_DUMP_TO_FILE);

static module create_pointwise_module(module_ref in_mod)
{
Expand Down Expand Up @@ -212,18 +213,24 @@ struct mlir_compiler : compiler<mlir_compiler>
bool exhaustive) const
{
static const auto mxr_loc = string_value_of(MIGRAPHX_MLIR_DUMP_TO_MXR{});
static const auto mlir_loc = string_value_of(MIGRAPHX_MLIR_DUMP_TO_FILE{});

auto shapes = to_shapes(ins->inputs());
auto* smod = ins->module_inputs().front();
if(not mxr_loc.empty())
{
dump_mlir_to_mxr(*smod, ins->inputs(), mxr_loc);
}
if(not mlir_loc.empty())
{
dump_mlir(*smod, shapes, mlir_loc);
}
return get_tuning_config_mlir(ctx, *smod, shapes, exhaustive);
}

static void trace(std::ostream& os, instruction_ref ins)
{
std::cout << "tracing" << std::endl;
auto shapes = to_shapes(ins->inputs());
auto* smod = ins->module_inputs().front();
os << dump_mlir(*smod, shapes);
Expand Down
32 changes: 32 additions & 0 deletions src/targets/gpu/mlir.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1096,6 +1096,37 @@ std::string dump_mlir(module m, const std::vector<shape>& inputs)
return mlir_print(&mlirOperationPrint, mod_op);
}

std::string dump_mlir(module m, const std::vector<shape>& inputs, const fs::path& location)
{
auto name =
mlir_program::get_symbol_name(m) + ".mlir";
replace_string_inplace(name, ", ", "_");
replace_string_inplace(name, ":", "s");
auto f = location / name;

const_module_ref mr = &m;
if(not inputs.empty())
{
adjust_param_shapes(m, inputs);
}
rewrite_reduce(m);
mlir_program mp;
mp.parse(*mr);
auto mod_op = mlirModuleGetOperation(mp.mmodule.get());

std::string mlir_str = mlir_print(&mlirOperationPrint, mod_op);

std::ofstream outfile(f);
if (outfile.is_open()) {
outfile << mlir_str;
outfile.close();
} else {
throw std::runtime_error("Unable to open file");
}

return mlir_str;
}

std::string dump_mlir(module m) { return dump_mlir(std::move(m), {}); }

mlir_code_object compile_mlir(const context& migraphx_ctx,
Expand All @@ -1122,6 +1153,7 @@ mlir_code_object compile_mlir(const context& migraphx_ctx,
if(trace)
{
const std::lock_guard<std::mutex> lock(mutex);
std::cout << "printing mlir" << std::endl;
std::cout << mlir_print(&mlirOperationPrint, mod_op) << std::endl;
}
auto co = mp.compile(solution);
Expand Down

0 comments on commit bb33f43

Please sign in to comment.