/* * The MIT License (MIT) * * Copyright (c) 2015-2022 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 * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in * all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN * THE SOFTWARE. */ #ifndef MIGRAPHX_GUARD_MIGRAPHLIB_MODULE_HPP #define MIGRAPHX_GUARD_MIGRAPHLIB_MODULE_HPP #include #include #include #include #include #include #include #include #include #include #include #include #include #include namespace migraphx { inline namespace MIGRAPHX_INLINE_NS { const operation& get_operation(instruction_ref ins); struct module_impl; using parameter_map = std::unordered_map; using ins_dep_map = std::unordered_map>; /** * @brief Stores the instruction stream */ struct module { module(const std::string& name = ""); // move constructor module(module&&) noexcept; // copy constructor module(const module&); // copy assignment operator module& operator=(module); ~module() noexcept; std::string name() const; bool bypass() const; void set_bypass(bool b = true); template {}...)> instruction_ref add_instruction(operation op, Ts... args) { return add_instruction(op, {args...}); } instruction_ref add_instruction(const operation& op, std::vector args); instruction_ref add_instruction(const operation& op, std::vector args, std::vector module_args); template {}...)> instruction_ref insert_instruction(instruction_ref ins, operation op, Ts... args) { return insert_instruction(ins, op, {args...}); } instruction_ref insert_instruction(instruction_ref ins, const operation& op, std::vector args); instruction_ref insert_instruction(instruction_ref ins, const operation& op, std::vector args, std::vector module_args); template {}...)> instruction_ref replace_instruction(instruction_ref ins, operation op, Ts... args) { return replace_instruction(ins, op, {args...}); } instruction_ref replace_instruction(instruction_ref ins, const operation& op, std::vector args) MIGRAPHX_TIDY_CONST; instruction_ref replace_instruction(instruction_ref ins, const operation& op, std::vector args, std::vector module_args) MIGRAPHX_TIDY_CONST; instruction_ref replace_instruction(instruction_ref ins, instruction_ref rep); instruction_ref remove_instruction(instruction_ref ins); instruction_ref remove_instructions(instruction_ref first, instruction_ref last); instruction_ref move_instruction(instruction_ref src, instruction_ref dst); instruction_ref move_instructions(instruction_ref src, instruction_ref dst); std::vector add_instructions(const std::vector& instructions, std::unordered_map map_ins = {}); std::vector add_instructions(module_ref m, std::unordered_map map_ins = {}); std::vector add_instructions(instruction_ref start, instruction_ref last, std::unordered_map map_ins = {}); std::vector insert_instructions(instruction_ref ins, const std::vector& instructions, std::unordered_map map_ins = {}); std::vector insert_instructions(instruction_ref ins, module_ref m, std::unordered_map map_ins = {}); std::vector insert_instructions(instruction_ref ins, instruction_ref start, instruction_ref last, std::unordered_map map_ins = {}); template instruction_ref add_literal(Ts&&... xs) { return add_literal(literal{std::forward(xs)...}); } instruction_ref add_literal(literal l); instruction_ref add_outline(const shape& s); instruction_ref add_parameter(std::string name, shape s); instruction_ref add_return(std::vector args); instruction_ref replace_return(std::vector args); std::vector get_parameter_names() const; shape get_parameter_shape(std::string name) const; instruction_ref get_parameter(std::string name) const; std::unordered_map get_parameter_shapes() const; bool has_instruction(instruction_ref ins) const; std::size_t size() const; instruction_ref begin() const; instruction_ref end() const; std::vector get_output_shapes() const; instruction_ref validate() const; instruction_ref find_dangling_reference() const; instruction_ref find_division_by_zero() const; void finalize(context& ctx); void debug_print() const; void debug_print(instruction_ref ins) const; void debug_print(instruction_ref ins, std::unordered_map& names) const; void debug_print(const std::vector& inss) const; std::unordered_map print( const std::function&)>& print_func, std::unordered_map names) const; void print(const std::function&)>& print_func) const; void print_graph(std::ostream& os, bool brief = false) const; void print_cpp(std::ostream& os) const; std::unordered_map print_cpp(std::ostream& os, const std::string& mname, std::unordered_map names) const; void annotate(std::ostream& os, std::function a) const; std::vector get_sub_modules(bool shallow = false) const; module& sort(); ins_dep_map calc_implicit_deps() const; friend std::ostream& operator<<(std::ostream& os, const module& m); friend bool operator==(const module& x, const module& y); friend bool operator!=(const module& x, const module& y) { return !(x == y); } private: void assign(const module& m); void calc_implicit_deps(const module& smod, const module& pmod, instruction_ref ins, ins_dep_map& deps) const; std::unique_ptr impl; }; } // namespace MIGRAPHX_INLINE_NS } // namespace migraphx #endif