Commit 884d3eb1 authored by umangyadav's avatar umangyadav
Browse files

fix errors and add another environment variable for dump_passes

parent 751fd21a
......@@ -17,8 +17,8 @@
namespace migraphx {
inline namespace MIGRAPHX_INLINE_NS {
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_COMPILE)
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_DUMP_PASSES_TO_FILE)
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_EVAL)
struct program_impl;
......
......@@ -2,6 +2,9 @@
#define MIGRAPHX_GUARD_RTGLIB_TRACER_HPP
#include <fstream>
#include <iostream>
#include <ostream>
#include <migraphx/requires.hpp>
#include <migraphx/functional.hpp>
#include <migraphx/config.hpp>
#include <migraphx/filesystem.hpp>
......@@ -12,36 +15,58 @@ struct tracer
{
tracer() {}
tracer(std::ostream& s) : os(&s) {}
tracer(const std::string& dump_directory)
: dump_dir(dump_directory), counter(0), dir_path(fs::current_path() / dump_directory)
{
if(fs::exists(dir_path))
if(!dump_dir.empty() && fs::exists(dir_path))
{
fs::remove_all(dir_path);
}
fs::create_directories(dir_path);
}
// file_stream
bool fs_enabled() const { return !dump_dir.empty() && !os_enabled(); }
// output_stream
bool os_enabled() const { return os && !fs_enabled();}
bool enabled() const {return fs_enabled() or os_enabled();};
bool enabled() const { return !dump_dir.empty(); }
/*
Dump any string to ostream, used for debug build or debugging purposes.
*/
void operator()(const std::string& s="") const {
std::cout << s << std::endl;
}
template <class... Ts>
void operator()(const std::string& program_name, const Ts&... xs)
/*
Based on user's envrionment flags, either dump IR passes' output to a file or ostream i.e. cout or cerr,
:param pass_file_name : file_name to be used when dumping IR pass to a file, this param is not used when IR is
dumped to ostream.
*/
template <class... Ts, MIGRAPHX_REQUIRES((sizeof...(Ts) > 0))>
void operator()(const std::string& pass_file_name, const Ts&... xs)
{
if(this->enabled())
if(fs_enabled())
{
fs::path ir_file_path =
this->dir_path / (std::to_string(this->counter++) + "_" + program_name + ".mxr");
dir_path / (std::to_string(counter++) + "_" + pass_file_name + ".mxr");
std::ofstream ofs(ir_file_path);
swallow{ofs << xs...};
ofs << std::endl;
ofs.close();
} else if (os_enabled()) {
swallow{*os << xs...};
*os << std::endl;
}
}
std::string dump_dir;
std::string dump_dir = "";
private:
uint counter;
fs::path dir_path;
uint counter = 0;
std::ostream* os = nullptr;
fs::path dir_path = "";
};
} // namespace MIGRAPHX_INLINE_NS
......
......@@ -18,12 +18,12 @@ inline namespace MIGRAPHX_INLINE_NS {
MIGRAPHX_DECLARE_ENV_VAR(MIGRAPHX_TRACE_PASSES);
void validate_pass(module& mod, const pass& p)
void validate_pass(module& mod, const pass& p, tracer trace)
{
(void)mod;
(void)p;
#ifndef NDEBUG
std::cout << "Validate..." << std::endl;
trace("Validate...");
auto invalid = mod.validate();
if(invalid != mod.end())
{
......@@ -31,13 +31,17 @@ void validate_pass(module& mod, const pass& p)
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)
{
auto t_start = std::chrono::high_resolution_clock::now();
p.apply(prog);
trace(p.name(), prog);
auto t_end = std::chrono::high_resolution_clock::now();
double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();
trace(p.name(), "Pass: ", p.name(), "\n", prog, "Elapsed Wall Time (ms): ", elapsed_time_ms, "\n");
}
struct module_pm : module_pass_manager
......@@ -72,9 +76,12 @@ struct module_pm : module_pass_manager
{
assert(mod);
assert(mod->validate() == mod->end());
auto t_start = std::chrono::high_resolution_clock::now();
p.apply(*this);
trace(p.name(), *mod);
validate_pass(*mod, p);
auto t_end = std::chrono::high_resolution_clock::now();
double elapsed_time_ms = std::chrono::duration<double, std::milli>(t_end-t_start).count();
trace(p.name(), "Module: ", mod->name(), ", Pass: ", p.name(), "\n", *mod, "Elapsed Wall Time (ms): ", elapsed_time_ms);
validate_pass(*mod, p, *t);
}
};
......@@ -83,7 +90,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{}) and not trace.enabled())
trace = tracer{mod.name() + "_passes"};
trace = tracer{std::cout};
for(const auto& p : passes)
{
module_pm{&mod, nullptr, &trace}.run_pass(p);
......@@ -93,17 +100,18 @@ 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{}) and not trace.enabled())
trace = tracer{"passes"};
trace = tracer{std::cout};
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.find(mod->name()) == module_tracer_map.end())
{
module_tracer_map[mod->name()] =
tracer{trace.enabled() ? trace.dump_dir + "/" + mod->name() : ""};
// Set tracer for module passes, if tracer is set to output to file stream then set name of the dump directory.
// For file dumps, tracer object internally sets the counter for the individual passes' file dumps.
if(module_tracer_map.find(mod->name()) == module_tracer_map.end()) {
module_tracer_map[mod->name()] = trace.fs_enabled() ? tracer{trace.dump_dir + "/" + mod->name()} : trace;
}
if(mod->bypass())
continue;
......
......@@ -143,8 +143,10 @@ void program::compile(const target& t, compile_options options, const std::strin
assert(not this->is_compiled());
this->impl->target_name = t.name();
this->impl->ctx = t.get_context();
if(enabled(MIGRAPHX_TRACE_COMPILE{}))
if(enabled(MIGRAPHX_DUMP_PASSES_TO_FILE{}))
options.trace = tracer{t.name() + "_" + ir_dump_path};
else if(enabled(MIGRAPHX_TRACE_COMPILE{}))
options.trace = tracer{std::cout};
options.trace("input_program", *this);
......
......@@ -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{t.name() + "_passes"};
options.trace = migraphx::tracer{std::cout};
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