Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 1 | // Copyright 2022 The Dawn & Tint Authors |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 2 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 3 | // Redistribution and use in source and binary forms, with or without |
| 4 | // modification, are permitted provided that the following conditions are met: |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 5 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 6 | // 1. Redistributions of source code must retain the above copyright notice, this |
| 7 | // list of conditions and the following disclaimer. |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 8 | // |
Austin Eng | cc2516a | 2023-10-17 20:57:54 +0000 | [diff] [blame] | 9 | // 2. Redistributions in binary form must reproduce the above copyright notice, |
| 10 | // this list of conditions and the following disclaimer in the documentation |
| 11 | // and/or other materials provided with the distribution. |
| 12 | // |
| 13 | // 3. Neither the name of the copyright holder nor the names of its |
| 14 | // contributors may be used to endorse or promote products derived from |
| 15 | // this software without specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 20 | // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
| 21 | // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
| 22 | // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
| 23 | // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
| 24 | // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
| 25 | // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 27 | |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 28 | #ifndef SRC_TINT_LANG_CORE_IR_MODULE_H_ |
| 29 | #define SRC_TINT_LANG_CORE_IR_MODULE_H_ |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 30 | |
dan sinclair | b984643 | 2023-06-07 21:52:57 +0000 | [diff] [blame] | 31 | #include <memory> |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 32 | #include <string> |
| 33 | |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 34 | #include "src/tint/lang/core/constant/manager.h" |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 35 | #include "src/tint/lang/core/ir/block.h" |
| 36 | #include "src/tint/lang/core/ir/constant.h" |
| 37 | #include "src/tint/lang/core/ir/function.h" |
| 38 | #include "src/tint/lang/core/ir/instruction.h" |
| 39 | #include "src/tint/lang/core/ir/value.h" |
dan sinclair | 352f8c8 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 40 | #include "src/tint/lang/core/type/manager.h" |
Ben Clayton | acef310 | 2023-11-20 17:09:35 +0000 | [diff] [blame] | 41 | #include "src/tint/utils/containers/const_propagating_ptr.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 42 | #include "src/tint/utils/containers/vector.h" |
Ben Clayton | f848af2 | 2023-07-28 16:37:32 +0000 | [diff] [blame] | 43 | #include "src/tint/utils/diagnostic/source.h" |
Ben Clayton | 67fc590 | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 44 | #include "src/tint/utils/id/generation_id.h" |
dan sinclair | 22b4dd2 | 2023-07-21 00:40:07 +0000 | [diff] [blame] | 45 | #include "src/tint/utils/memory/block_allocator.h" |
| 46 | #include "src/tint/utils/result/result.h" |
Ben Clayton | dc68d5c | 2023-08-01 00:37:35 +0000 | [diff] [blame] | 47 | #include "src/tint/utils/symbol/symbol_table.h" |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 48 | |
dan sinclair | 6f138fe | 2023-08-15 21:29:34 +0000 | [diff] [blame] | 49 | namespace tint::core::ir { |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 50 | |
| 51 | /// Main module class for the IR. |
| 52 | class Module { |
Ben Clayton | 8841768 | 2023-05-10 09:44:10 +0000 | [diff] [blame] | 53 | /// Program Id required to create other components |
dan sinclair | 637a2fe | 2023-07-24 21:11:41 +0000 | [diff] [blame] | 54 | GenerationID prog_id_; |
Ben Clayton | 8841768 | 2023-05-10 09:44:10 +0000 | [diff] [blame] | 55 | |
Ben Clayton | 8041661 | 2023-07-24 14:03:34 +0000 | [diff] [blame] | 56 | /// Map of value to name |
Ben Clayton | ead8a04 | 2023-11-18 09:45:32 +0000 | [diff] [blame] | 57 | Hashmap<const Value*, Symbol, 32> value_to_name_; |
Ben Clayton | 8841768 | 2023-05-10 09:44:10 +0000 | [diff] [blame] | 58 | |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 59 | public: |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 60 | /// Constructor |
dan sinclair | 8bcb4e9 | 2023-01-06 19:45:13 +0000 | [diff] [blame] | 61 | Module(); |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 62 | /// Move constructor |
| 63 | /// @param o the module to move from |
| 64 | Module(Module&& o); |
| 65 | /// Destructor |
| 66 | ~Module(); |
| 67 | |
| 68 | /// Move assign |
| 69 | /// @param o the module to assign from |
| 70 | /// @returns a reference to this module |
| 71 | Module& operator=(Module&& o); |
| 72 | |
dan sinclair | 0d80c3d | 2023-06-19 13:32:03 +0000 | [diff] [blame] | 73 | /// @param inst the instruction |
| 74 | /// @return the name of the given instruction, or an invalid symbol if the instruction is not |
Ben Clayton | a89d664 | 2023-11-17 22:41:32 +0000 | [diff] [blame] | 75 | /// named or does not have a single return value. |
Ben Clayton | ead8a04 | 2023-11-18 09:45:32 +0000 | [diff] [blame] | 76 | Symbol NameOf(const Instruction* inst) const; |
dan sinclair | 0d80c3d | 2023-06-19 13:32:03 +0000 | [diff] [blame] | 77 | |
Ben Clayton | 8841768 | 2023-05-10 09:44:10 +0000 | [diff] [blame] | 78 | /// @param value the value |
| 79 | /// @return the name of the given value, or an invalid symbol if the value is not named. |
Ben Clayton | ead8a04 | 2023-11-18 09:45:32 +0000 | [diff] [blame] | 80 | Symbol NameOf(const Value* value) const; |
dan sinclair | f8abdc7 | 2023-01-05 21:07:15 +0000 | [diff] [blame] | 81 | |
dan sinclair | 0d80c3d | 2023-06-19 13:32:03 +0000 | [diff] [blame] | 82 | /// @param inst the instruction to set the name of |
| 83 | /// @param name the desired name of the value. May be suffixed on collision. |
dan sinclair | 0d80c3d | 2023-06-19 13:32:03 +0000 | [diff] [blame] | 84 | /// @note requires the instruction be a single result instruction. |
Ben Clayton | 8041661 | 2023-07-24 14:03:34 +0000 | [diff] [blame] | 85 | void SetName(Instruction* inst, std::string_view name); |
dan sinclair | 0d80c3d | 2023-06-19 13:32:03 +0000 | [diff] [blame] | 86 | |
Ben Clayton | 8841768 | 2023-05-10 09:44:10 +0000 | [diff] [blame] | 87 | /// @param value the value to name. |
| 88 | /// @param name the desired name of the value. May be suffixed on collision. |
Ben Clayton | 8041661 | 2023-07-24 14:03:34 +0000 | [diff] [blame] | 89 | void SetName(Value* value, std::string_view name); |
James Price | aa78dcd | 2023-06-07 10:17:41 +0000 | [diff] [blame] | 90 | |
James Price | 128a766 | 2023-07-12 23:45:55 +0000 | [diff] [blame] | 91 | /// @param value the value to name |
| 92 | /// @param name the desired name of the value |
| 93 | void SetName(Value* value, Symbol name); |
| 94 | |
Ben Clayton | d9ffa54 | 2023-11-28 20:36:55 +0000 | [diff] [blame] | 95 | /// Removes the name from @p value |
| 96 | /// @param value the value to remove the name from |
| 97 | void ClearName(Value* value); |
| 98 | |
James Price | aa78dcd | 2023-06-07 10:17:41 +0000 | [diff] [blame] | 99 | /// @return the type manager for the module |
dan sinclair | cedcdf3 | 2023-08-10 02:39:48 +0000 | [diff] [blame] | 100 | core::type::Manager& Types() { return constant_values.types; } |
Ben Clayton | 1e67e53 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 101 | |
Ben Clayton | ead8a04 | 2023-11-18 09:45:32 +0000 | [diff] [blame] | 102 | /// @return the type manager for the module |
| 103 | const core::type::Manager& Types() const { return constant_values.types; } |
| 104 | |
dan sinclair | a00fe39 | 2023-05-25 04:56:11 +0000 | [diff] [blame] | 105 | /// The block allocator |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 106 | BlockAllocator<Block> blocks; |
Ben Clayton | 1e67e53 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 107 | |
| 108 | /// The constant value manager |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 109 | core::constant::Manager constant_values; |
Ben Clayton | 1e67e53 | 2023-05-24 23:07:36 +0000 | [diff] [blame] | 110 | |
dan sinclair | 367fad8 | 2023-06-19 12:58:53 +0000 | [diff] [blame] | 111 | /// The instruction allocator |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 112 | BlockAllocator<Instruction> instructions; |
dan sinclair | 367fad8 | 2023-06-19 12:58:53 +0000 | [diff] [blame] | 113 | |
dan sinclair | 2d108ae | 2022-11-29 20:36:59 +0000 | [diff] [blame] | 114 | /// The value allocator |
dan sinclair | bae54e7 | 2023-07-28 15:01:54 +0000 | [diff] [blame] | 115 | BlockAllocator<Value> values; |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 116 | |
| 117 | /// List of functions in the program |
Ben Clayton | acef310 | 2023-11-20 17:09:35 +0000 | [diff] [blame] | 118 | Vector<ConstPropagatingPtr<Function>, 8> functions; |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 119 | |
dan sinclair | c970e80 | 2023-05-02 15:47:29 +0000 | [diff] [blame] | 120 | /// The block containing module level declarations, if any exist. |
Ben Clayton | acef310 | 2023-11-20 17:09:35 +0000 | [diff] [blame] | 121 | ConstPropagatingPtr<Block> root_block; |
dan sinclair | c970e80 | 2023-05-02 15:47:29 +0000 | [diff] [blame] | 122 | |
dan sinclair | f8abdc7 | 2023-01-05 21:07:15 +0000 | [diff] [blame] | 123 | /// The symbol table for the module |
| 124 | SymbolTable symbols{prog_id_}; |
dan sinclair | 9774483 | 2023-05-18 14:51:54 +0000 | [diff] [blame] | 125 | |
dan sinclair | 464b3b8 | 2023-08-09 14:14:28 +0000 | [diff] [blame] | 126 | /// The map of core::constant::Value to their ir::Constant. |
| 127 | Hashmap<const core::constant::Value*, ir::Constant*, 16> constants; |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 128 | }; |
| 129 | |
dan sinclair | 6f138fe | 2023-08-15 21:29:34 +0000 | [diff] [blame] | 130 | } // namespace tint::core::ir |
dan sinclair | eee8d88 | 2022-10-28 01:22:58 +0000 | [diff] [blame] | 131 | |
dan sinclair | 97c3727 | 2023-07-24 17:11:53 +0000 | [diff] [blame] | 132 | #endif // SRC_TINT_LANG_CORE_IR_MODULE_H_ |