blob: fba89fb7f458aa417e2e46ce32eab85d337726f0 [file] [log] [blame]
Austin Engcc2516a2023-10-17 20:57:54 +00001// Copyright 2022 The Dawn & Tint Authors
dan sinclaireee8d882022-10-28 01:22:58 +00002//
Austin Engcc2516a2023-10-17 20:57:54 +00003// Redistribution and use in source and binary forms, with or without
4// modification, are permitted provided that the following conditions are met:
dan sinclaireee8d882022-10-28 01:22:58 +00005//
Austin Engcc2516a2023-10-17 20:57:54 +00006// 1. Redistributions of source code must retain the above copyright notice, this
7// list of conditions and the following disclaimer.
dan sinclaireee8d882022-10-28 01:22:58 +00008//
Austin Engcc2516a2023-10-17 20:57:54 +00009// 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 sinclaireee8d882022-10-28 01:22:58 +000027
dan sinclair97c37272023-07-24 17:11:53 +000028#ifndef SRC_TINT_LANG_CORE_IR_MODULE_H_
29#define SRC_TINT_LANG_CORE_IR_MODULE_H_
dan sinclaireee8d882022-10-28 01:22:58 +000030
dan sinclairb9846432023-06-07 21:52:57 +000031#include <memory>
dan sinclaireee8d882022-10-28 01:22:58 +000032#include <string>
33
dan sinclair352f8c82023-07-21 00:40:07 +000034#include "src/tint/lang/core/constant/manager.h"
dan sinclair97c37272023-07-24 17:11:53 +000035#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 sinclair352f8c82023-07-21 00:40:07 +000040#include "src/tint/lang/core/type/manager.h"
Ben Claytonacef3102023-11-20 17:09:35 +000041#include "src/tint/utils/containers/const_propagating_ptr.h"
dan sinclair22b4dd22023-07-21 00:40:07 +000042#include "src/tint/utils/containers/vector.h"
Ben Claytonf848af22023-07-28 16:37:32 +000043#include "src/tint/utils/diagnostic/source.h"
Ben Clayton67fc5902023-08-01 00:37:35 +000044#include "src/tint/utils/id/generation_id.h"
dan sinclair22b4dd22023-07-21 00:40:07 +000045#include "src/tint/utils/memory/block_allocator.h"
46#include "src/tint/utils/result/result.h"
Ben Claytondc68d5c2023-08-01 00:37:35 +000047#include "src/tint/utils/symbol/symbol_table.h"
dan sinclaireee8d882022-10-28 01:22:58 +000048
dan sinclair6f138fe2023-08-15 21:29:34 +000049namespace tint::core::ir {
dan sinclaireee8d882022-10-28 01:22:58 +000050
51/// Main module class for the IR.
52class Module {
Ben Clayton88417682023-05-10 09:44:10 +000053 /// Program Id required to create other components
dan sinclair637a2fe2023-07-24 21:11:41 +000054 GenerationID prog_id_;
Ben Clayton88417682023-05-10 09:44:10 +000055
Ben Clayton80416612023-07-24 14:03:34 +000056 /// Map of value to name
Ben Claytonead8a042023-11-18 09:45:32 +000057 Hashmap<const Value*, Symbol, 32> value_to_name_;
Ben Clayton88417682023-05-10 09:44:10 +000058
dan sinclaireee8d882022-10-28 01:22:58 +000059 public:
dan sinclaireee8d882022-10-28 01:22:58 +000060 /// Constructor
dan sinclair8bcb4e92023-01-06 19:45:13 +000061 Module();
dan sinclaireee8d882022-10-28 01:22:58 +000062 /// 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 sinclair0d80c3d2023-06-19 13:32:03 +000073 /// @param inst the instruction
74 /// @return the name of the given instruction, or an invalid symbol if the instruction is not
Ben Claytona89d6642023-11-17 22:41:32 +000075 /// named or does not have a single return value.
Ben Claytonead8a042023-11-18 09:45:32 +000076 Symbol NameOf(const Instruction* inst) const;
dan sinclair0d80c3d2023-06-19 13:32:03 +000077
Ben Clayton88417682023-05-10 09:44:10 +000078 /// @param value the value
79 /// @return the name of the given value, or an invalid symbol if the value is not named.
Ben Claytonead8a042023-11-18 09:45:32 +000080 Symbol NameOf(const Value* value) const;
dan sinclairf8abdc72023-01-05 21:07:15 +000081
dan sinclair0d80c3d2023-06-19 13:32:03 +000082 /// @param inst the instruction to set the name of
83 /// @param name the desired name of the value. May be suffixed on collision.
dan sinclair0d80c3d2023-06-19 13:32:03 +000084 /// @note requires the instruction be a single result instruction.
Ben Clayton80416612023-07-24 14:03:34 +000085 void SetName(Instruction* inst, std::string_view name);
dan sinclair0d80c3d2023-06-19 13:32:03 +000086
Ben Clayton88417682023-05-10 09:44:10 +000087 /// @param value the value to name.
88 /// @param name the desired name of the value. May be suffixed on collision.
Ben Clayton80416612023-07-24 14:03:34 +000089 void SetName(Value* value, std::string_view name);
James Priceaa78dcd2023-06-07 10:17:41 +000090
James Price128a7662023-07-12 23:45:55 +000091 /// @param value the value to name
92 /// @param name the desired name of the value
93 void SetName(Value* value, Symbol name);
94
Ben Claytond9ffa542023-11-28 20:36:55 +000095 /// Removes the name from @p value
96 /// @param value the value to remove the name from
97 void ClearName(Value* value);
98
James Priceaa78dcd2023-06-07 10:17:41 +000099 /// @return the type manager for the module
dan sinclaircedcdf32023-08-10 02:39:48 +0000100 core::type::Manager& Types() { return constant_values.types; }
Ben Clayton1e67e532023-05-24 23:07:36 +0000101
Ben Claytonead8a042023-11-18 09:45:32 +0000102 /// @return the type manager for the module
103 const core::type::Manager& Types() const { return constant_values.types; }
104
dan sinclaira00fe392023-05-25 04:56:11 +0000105 /// The block allocator
dan sinclairbae54e72023-07-28 15:01:54 +0000106 BlockAllocator<Block> blocks;
Ben Clayton1e67e532023-05-24 23:07:36 +0000107
108 /// The constant value manager
dan sinclair464b3b82023-08-09 14:14:28 +0000109 core::constant::Manager constant_values;
Ben Clayton1e67e532023-05-24 23:07:36 +0000110
dan sinclair367fad82023-06-19 12:58:53 +0000111 /// The instruction allocator
dan sinclairbae54e72023-07-28 15:01:54 +0000112 BlockAllocator<Instruction> instructions;
dan sinclair367fad82023-06-19 12:58:53 +0000113
dan sinclair2d108ae2022-11-29 20:36:59 +0000114 /// The value allocator
dan sinclairbae54e72023-07-28 15:01:54 +0000115 BlockAllocator<Value> values;
dan sinclaireee8d882022-10-28 01:22:58 +0000116
117 /// List of functions in the program
Ben Claytonacef3102023-11-20 17:09:35 +0000118 Vector<ConstPropagatingPtr<Function>, 8> functions;
dan sinclaireee8d882022-10-28 01:22:58 +0000119
dan sinclairc970e802023-05-02 15:47:29 +0000120 /// The block containing module level declarations, if any exist.
Ben Claytonacef3102023-11-20 17:09:35 +0000121 ConstPropagatingPtr<Block> root_block;
dan sinclairc970e802023-05-02 15:47:29 +0000122
dan sinclairf8abdc72023-01-05 21:07:15 +0000123 /// The symbol table for the module
124 SymbolTable symbols{prog_id_};
dan sinclair97744832023-05-18 14:51:54 +0000125
dan sinclair464b3b82023-08-09 14:14:28 +0000126 /// The map of core::constant::Value to their ir::Constant.
127 Hashmap<const core::constant::Value*, ir::Constant*, 16> constants;
dan sinclaireee8d882022-10-28 01:22:58 +0000128};
129
dan sinclair6f138fe2023-08-15 21:29:34 +0000130} // namespace tint::core::ir
dan sinclaireee8d882022-10-28 01:22:58 +0000131
dan sinclair97c37272023-07-24 17:11:53 +0000132#endif // SRC_TINT_LANG_CORE_IR_MODULE_H_