Commit aff10174 authored by umangyadav's avatar umangyadav
Browse files

dump IR passes into seperate files, dump modules passes into seperate directories

parent 671f24be
......@@ -201,10 +201,7 @@ rocm_install_targets(
)
check_cxx_linker_flag(-lstdc++fs HAS_LIB_STD_FILESYSTEM)
if(HAS_LIB_STD_FILESYSTEM)
target_link_libraries(migraphx PRIVATE -lstdc++fs)
endif()
target_link_libraries(migraphx PRIVATE -ldl)
......
......@@ -61,7 +61,7 @@ struct program
instruction_ref validate() const;
void compile(const target& t, compile_options options = compile_options{});
void compile(const target& t, compile_options options = compile_options{}, std::string ir_dump_path="passes");
bool is_compiled() const;
......
#ifndef MIGRAPHX_GUARD_RTGLIB_TRACER_HPP
#define MIGRAPHX_GUARD_RTGLIB_TRACER_HPP
#include <ostream>
#include <fstream>
#include <migraphx/functional.hpp>
#include <migraphx/config.hpp>
#include <migraphx/filesystem.hpp>
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
......@@ -12,24 +12,31 @@ struct tracer
{
tracer() {}
tracer(std::ostream& s) : os(&s) {}
tracer(std::string dump_directory) : dump_dir(dump_directory), counter(0) {}
bool enabled() const { return os != nullptr; }
bool enabled() const { return !dump_dir.empty(); }
template <class... Ts>
void operator()(const Ts&... xs) const
void operator()(const std::string& program_name, const Ts&... xs)
{
if(os != nullptr)
{
swallow{*os << xs...};
*os << std::endl;
if(this->enabled()) {
fs::path dir_path = fs::current_path() / this->dump_dir;
if(not fs::exists(dir_path)) {
fs::create_directories(dir_path);
}
fs::path ir_file_path = dir_path / (std::to_string(this->counter++)+"_"+program_name+".mxr");
std::ofstream ofs(ir_file_path);
swallow{ofs<<xs...};
ofs<<std::endl;
ofs.close();
}
}
std::string dump_dir;
private:
std::ostream* os = nullptr;
uint counter;
};
} // namespace MIGRAPHX_INLINE_NS
} // namespace migraphx
......
......@@ -10,6 +10,7 @@
#include <iostream>
#include <sstream>
#include <algorithm>
#include <unordered_map>
#include <utility>
namespace migraphx {
......@@ -17,13 +18,12 @@ inline namespace MIGRAPHX_INLINE_NS {
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_PASSES);
void validate_pass(module& mod, const pass& p, tracer trace)
void validate_pass(module& mod, const pass& p)
{
(void)mod;
(void)p;
(void)trace;
#ifndef NDEBUG
trace("Validate ...");
std::cout << "Validate..." << std::endl;
auto invalid = mod.validate();
if(invalid != mod.end())
{
......@@ -31,14 +31,13 @@ void validate_pass(module& mod, const pass& p, tracer trace)
MIGRAPHX_THROW(p.name() + " pass produces invalid program at instruction " +
std::to_string(index) + ": " + invalid->name());
}
trace();
#endif
}
void run_pass(program& prog, const pass& p, tracer trace)
void run_pass(program& prog, const pass& p, tracer& trace)
{
trace("Pass: ", p.name());
p.apply(prog);
trace(prog);
trace(p.name(), prog);
}
struct module_pm : module_pass_manager
......@@ -72,11 +71,10 @@ struct module_pm : module_pass_manager
virtual void run_pass(const pass& p) override
{
assert(mod);
trace("Module: ", mod->name(), ", Pass: ", p.name());
assert(mod->validate() == mod->end());
p.apply(*this);
trace(*mod);
validate_pass(*mod, p, *t);
trace(p.name(), *mod);
validate_pass(*mod, p);
}
};
......@@ -85,7 +83,7 @@ module& get_module(module_pass_manager& mpm) { return mpm.get_module(); }
void run_passes(module& mod, const std::vector<pass>& passes, tracer trace)
{
if(enabled(MIGRAPHX_TRACE_PASSES{}))
trace = tracer{std::cout};
trace = tracer{mod.name()+"_passes"};
for(const auto& p : passes)
{
module_pm{&mod, nullptr, &trace}.run_pass(p);
......@@ -94,16 +92,22 @@ void run_passes(module& mod, const std::vector<pass>& passes, tracer trace)
void run_passes(program& prog, const std::vector<pass>& passes, tracer trace)
{
if(enabled(MIGRAPHX_TRACE_PASSES{}))
trace = tracer{std::cout};
if(enabled(MIGRAPHX_TRACE_PASSES{}) and not trace.enabled())
trace = tracer{"passes"};
auto module_trace = trace;
std::unordered_map<std::string, tracer> module_tracer_map;
for(const auto& p : passes)
{
auto mods = prog.get_modules();
for(const auto& mod : reverse(mods))
{
if(!module_tracer_map.count(mod->name())) {
module_tracer_map[mod->name()] = module_trace;
module_tracer_map[mod->name()].dump_dir += "/"+mod->name();
}
if(mod->bypass())
continue;
module_pm{mod, &prog, &trace}.run_pass(p);
module_pm{mod, &prog, &module_tracer_map[mod->name()]}.run_pass(p);
}
run_pass(prog, p, trace);
}
......
......@@ -138,16 +138,15 @@ instruction_ref program::validate() const
bool program::is_compiled() const { return not this->impl->target_name.empty(); }
void program::compile(const target& t, compile_options options)
void program::compile(const target& t, compile_options options, std::string ir_dump_path)
{
assert(not this->is_compiled());
this->impl->target_name = t.name();
this->impl->ctx = t.get_context();
if(enabled(MIGRAPHX_TRACE_COMPILE{}))
options.trace = tracer{std::cout};
options.trace = tracer{t.name()+"_"+ir_dump_path};
options.trace(*this);
options.trace();
options.trace("input_program", *this);
auto&& passes = t.get_passes(this->impl->ctx, options);
run_passes(*this, passes, options.trace);
......
......@@ -90,7 +90,7 @@ void quantize_int8(program& prog,
// use the calibration data to compute the quantization scale
auto capture_prog = prog;
capture_prog.compile(t);
capture_prog.compile(t, compile_options{}, "quantization");
// use all calibration data to run the program to calculate the
// quantization scale and shift
......
......@@ -13,7 +13,7 @@ namespace cpu {
struct lowering
{
std::string name() const { return "cpu::lowering"; }
std::string name() const { return "lowering"; }
void apply(module& m) const;
};
......
......@@ -14,7 +14,7 @@ struct lowering
{
context* ctx;
bool offload_copy;
std::string name() const { return "gpu::lowering"; }
std::string name() const { return "lowering"; }
void apply(module& m) const;
};
......
......@@ -10,7 +10,7 @@ namespace ref {
struct lowering
{
std::string name() const { return "ref::lowering"; }
std::string name() const { return "lowering"; }
void apply(module& m) const;
};
......
......@@ -41,7 +41,7 @@ inline void compile_check(migraphx::program& p, const migraphx::target& t, bool
std::stringstream ss;
migraphx::compile_options options;
if(show_trace)
options.trace = migraphx::tracer{std::cout};
options.trace = migraphx::tracer{t.name()+"_passes"};
p.compile(t, options);
if(shapes.size() != p.get_output_shapes().size())
{
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment