1 #ifndef HALIDE_SPIRV_IR_H
2 #define HALIDE_SPIRV_IR_H
20 #include <unordered_map>
26 #include <spirv/1.6/GLSL.std.450.h>
27 #include <spirv/1.6/spirv.h>
40 SpvCrossDeviceScope = 0,
42 SpvWorkgroupScope = 2,
44 SpvInvocationScope = 4
48 enum SpvPredefinedConstant {
65 SpvRuntimeArrayTypeId,
76 SpvCompositeConstantId,
103 using SpvBinary = std::vector<uint32_t>;
105 static constexpr SpvStorageClass SpvInvalidStorageClass = SpvStorageClassMax;
106 static constexpr SpvId SpvInvalidId = SpvId(-1);
107 static constexpr SpvId SpvNoResult = 0;
108 static constexpr SpvId SpvNoType = 0;
114 class SpvInstruction;
119 struct SpvModuleContents;
120 struct SpvFunctionContents;
121 struct SpvBlockContents;
122 struct SpvInstructionContents;
125 using SpvModuleContentsPtr = IntrusivePtr<SpvModuleContents>;
126 using SpvFunctionContentsPtr = IntrusivePtr<SpvFunctionContents>;
127 using SpvBlockContentsPtr = IntrusivePtr<SpvBlockContents>;
128 using SpvInstructionContentsPtr = IntrusivePtr<SpvInstructionContents>;
131 class SpvInstruction {
133 using LiteralValue = std::pair<uint32_t, SpvValueType>;
134 using Immediates = std::vector<LiteralValue>;
135 using Operands = std::vector<SpvId>;
136 using ValueTypes = std::vector<SpvValueType>;
138 SpvInstruction() =
default;
139 ~SpvInstruction() =
default;
141 SpvInstruction(
const SpvInstruction &) =
default;
142 SpvInstruction &operator=(
const SpvInstruction &) =
default;
143 SpvInstruction(SpvInstruction &&) =
default;
144 SpvInstruction &operator=(SpvInstruction &&) =
default;
146 void set_block(SpvBlock block);
147 void set_result_id(SpvId
id);
148 void set_type_id(SpvId
id);
149 void set_op_code(SpvOp opcode);
150 void add_operand(SpvId
id);
151 void add_operands(
const Operands &operands);
152 void add_immediate(SpvId
id, SpvValueType type);
153 void add_immediates(
const Immediates &Immediates);
154 void add_data(
uint32_t bytes,
const void *data, SpvValueType type);
155 void add_string(
const std::string &str);
158 void append(
const T &operands_or_immediates_or_strings);
160 SpvId result_id()
const;
161 SpvId type_id()
const;
162 SpvOp op_code()
const;
163 SpvId operand(
uint32_t index)
const;
164 const void *data(
uint32_t index = 0)
const;
165 SpvValueType value_type(
uint32_t index)
const;
166 const Operands &operands()
const;
168 bool has_type()
const;
169 bool has_result()
const;
170 bool is_defined()
const;
171 bool is_immediate(
uint32_t index)
const;
173 SpvBlock block()
const;
174 void check_defined()
const;
176 void encode(SpvBinary &binary)
const;
178 static SpvInstruction make(SpvOp op_code);
181 SpvInstructionContentsPtr contents;
187 using Instructions = std::vector<SpvInstruction>;
188 using Variables = std::vector<SpvInstruction>;
189 using Blocks = std::vector<SpvBlock>;
191 SpvBlock() =
default;
192 ~SpvBlock() =
default;
194 SpvBlock(
const SpvBlock &) =
default;
195 SpvBlock &operator=(
const SpvBlock &) =
default;
196 SpvBlock(SpvBlock &&) =
default;
197 SpvBlock &operator=(SpvBlock &&) =
default;
199 void add_instruction(SpvInstruction inst);
200 void add_variable(SpvInstruction var);
201 void set_function(SpvFunction func);
202 const Instructions &instructions()
const;
203 const Variables &variables()
const;
204 SpvFunction
function()
const;
205 bool is_reachable()
const;
206 bool is_terminated()
const;
207 bool is_defined()
const;
209 void check_defined()
const;
211 void encode(SpvBinary &binary)
const;
213 static SpvBlock make(SpvFunction func, SpvId
id);
216 SpvBlockContentsPtr contents;
222 using Blocks = std::vector<SpvBlock>;
223 using Parameters = std::vector<SpvInstruction>;
225 SpvFunction() =
default;
226 ~SpvFunction() =
default;
228 SpvFunction(
const SpvFunction &) =
default;
229 SpvFunction &operator=(
const SpvFunction &) =
default;
230 SpvFunction(SpvFunction &&) =
default;
231 SpvFunction &operator=(SpvFunction &&) =
default;
233 SpvBlock create_block(SpvId block_id);
234 void add_block(
const SpvBlock &block);
235 void add_parameter(
const SpvInstruction ¶m);
236 void set_module(SpvModule module);
237 void set_return_precision(SpvPrecision precision);
238 void set_parameter_precision(
uint32_t index, SpvPrecision precision);
239 bool is_defined()
const;
241 const Blocks &blocks()
const;
242 SpvBlock entry_block()
const;
243 SpvBlock tail_block()
const;
244 SpvPrecision return_precision()
const;
245 const Parameters ¶meters()
const;
246 SpvPrecision parameter_precision(
uint32_t index)
const;
249 SpvInstruction declaration()
const;
250 SpvModule module()
const;
251 SpvId return_type_id()
const;
252 SpvId type_id()
const;
254 void check_defined()
const;
256 void encode(SpvBinary &binary)
const;
258 static SpvFunction make(SpvId func_id, SpvId func_type_id, SpvId return_type_id,
uint32_t control_mask = SpvFunctionControlMaskNone);
261 SpvFunctionContentsPtr contents;
267 using ImportDefinition = std::pair<SpvId, std::string>;
268 using ImportNames = std::vector<std::string>;
269 using EntryPointNames = std::vector<std::string>;
270 using Instructions = std::vector<SpvInstruction>;
271 using Functions = std::vector<SpvFunction>;
272 using Capabilities = std::vector<SpvCapability>;
273 using Extensions = std::vector<std::string>;
274 using Imports = std::vector<ImportDefinition>;
276 SpvModule() =
default;
277 ~SpvModule() =
default;
279 SpvModule(
const SpvModule &) =
default;
280 SpvModule &operator=(
const SpvModule &) =
default;
281 SpvModule(SpvModule &&) =
default;
282 SpvModule &operator=(SpvModule &&) =
default;
284 void add_debug_string(SpvId result_id,
const std::string &
string);
285 void add_debug_symbol(SpvId
id,
const std::string &symbol);
286 void add_annotation(
const SpvInstruction &val);
287 void add_type(
const SpvInstruction &val);
288 void add_constant(
const SpvInstruction &val);
289 void add_global(
const SpvInstruction &val);
290 void add_execution_mode(
const SpvInstruction &val);
291 void add_function(SpvFunction val);
292 void add_instruction(
const SpvInstruction &val);
293 void add_entry_point(
const std::string &name, SpvInstruction entry_point);
295 void import_instruction_set(SpvId
id,
const std::string &instruction_set);
296 void require_capability(SpvCapability val);
297 void require_extension(
const std::string &val);
299 void set_version_format(
uint32_t version);
300 void set_source_language(SpvSourceLanguage val);
301 void set_addressing_model(SpvAddressingModel val);
302 void set_memory_model(SpvMemoryModel val);
303 void set_binding_count(SpvId count);
306 SpvSourceLanguage source_language()
const;
307 SpvAddressingModel addressing_model()
const;
308 SpvMemoryModel memory_model()
const;
309 SpvInstruction entry_point(
const std::string &name)
const;
310 EntryPointNames entry_point_names()
const;
311 ImportNames import_names()
const;
312 SpvId lookup_import(
const std::string &Instruction_set)
const;
315 Imports imports()
const;
316 Extensions extensions()
const;
317 Capabilities capabilities()
const;
318 Instructions entry_points()
const;
319 const Instructions &execution_modes()
const;
320 const Instructions &debug_source()
const;
321 const Instructions &debug_symbols()
const;
322 const Instructions &annotations()
const;
323 const Instructions &type_definitions()
const;
324 const Instructions &global_constants()
const;
325 const Instructions &global_variables()
const;
326 const Functions &function_definitions()
const;
329 SpvModule module()
const;
331 bool is_imported(
const std::string &instruction_set)
const;
332 bool is_capability_required(SpvCapability val)
const;
333 bool is_extension_required(
const std::string &val)
const;
334 bool is_defined()
const;
336 void check_defined()
const;
338 void encode(SpvBinary &binary)
const;
340 static SpvModule make(SpvId module_id,
341 SpvSourceLanguage source_language = SpvSourceLanguageUnknown,
342 SpvAddressingModel addressing_model = SpvAddressingModelLogical,
343 SpvMemoryModel memory_model = SpvMemoryModelSimple);
346 SpvModuleContentsPtr contents;
354 using ParamTypes = std::vector<SpvId>;
355 using Components = std::vector<SpvId>;
356 using StructMemberTypes = std::vector<SpvId>;
357 using Variables = std::vector<SpvId>;
358 using Indices = std::vector<uint32_t>;
359 using Literals = std::vector<uint32_t>;
362 ~SpvBuilder() =
default;
364 SpvBuilder(
const SpvBuilder &) =
delete;
365 SpvBuilder &operator=(
const SpvBuilder &) =
delete;
368 SpvId reserve_id(SpvKind = SpvResultId);
371 SpvKind kind_of(SpvId
id)
const;
374 std::string kind_name(SpvKind kind)
const;
377 SpvId
type_of(SpvId variable_id)
const;
386 SpvId declare_void_type();
387 SpvId declare_type(
const Type &type,
uint32_t array_size = 1);
388 SpvId declare_pointer_type(
const Type &type, SpvStorageClass storage_class);
389 SpvId declare_pointer_type(SpvId type_id, SpvStorageClass storage_class);
390 SpvId declare_constant(
const Type &type,
const void *data,
bool is_specialization =
false);
391 SpvId declare_null_constant(
const Type &type);
392 SpvId declare_bool_constant(
bool value);
393 SpvId declare_string_constant(
const std::string &str);
394 SpvId declare_integer_constant(
const Type &type,
int64_t value);
395 SpvId declare_float_constant(
const Type &type,
double value);
396 SpvId declare_scalar_constant(
const Type &type,
const void *data);
397 SpvId declare_vector_constant(
const Type &type,
const void *data);
398 SpvId declare_specialization_constant(
const Type &type,
const void *data);
399 SpvId declare_access_chain(SpvId ptr_type_id, SpvId base_id,
const Indices &indices);
400 SpvId declare_pointer_access_chain(SpvId ptr_type_id, SpvId base_id, SpvId element_id,
const Indices &indices);
401 SpvId declare_function_type(SpvId return_type,
const ParamTypes ¶m_types = {});
402 SpvId declare_function(
const std::string &name, SpvId function_type);
403 SpvId declare_struct(
const std::string &name,
const StructMemberTypes &member_types);
404 SpvId declare_variable(
const std::string &name, SpvId type_id, SpvStorageClass storage_class, SpvId initializer_id = SpvInvalidId);
405 SpvId declare_global_variable(
const std::string &name, SpvId type_id, SpvStorageClass storage_class, SpvId initializer_id = SpvInvalidId);
406 SpvId declare_symbol(
const std::string &symbol, SpvId
id, SpvId scope_id);
410 SpvId add_type(
const Type &type,
uint32_t array_size = 1);
411 SpvId add_struct(
const std::string &name,
const StructMemberTypes &member_types);
412 SpvId add_array_with_default_size(SpvId base_type_id, SpvId array_size_id);
413 SpvId add_runtime_array(SpvId base_type_id);
414 SpvId add_pointer_type(
const Type &type, SpvStorageClass storage_class);
415 SpvId add_pointer_type(SpvId base_type_id, SpvStorageClass storage_class);
416 SpvId add_constant(
const Type &type,
const void *data,
bool is_specialization =
false);
417 SpvId add_function_type(SpvId return_type_id,
const ParamTypes ¶m_type_ids);
418 SpvId add_function(
const std::string &name, SpvId return_type,
const ParamTypes ¶m_types = {});
419 SpvId add_instruction(SpvInstruction val);
421 void add_annotation(SpvId target_id, SpvDecoration decoration_type,
const Literals &literals = {});
422 void add_struct_annotation(SpvId struct_type_id,
uint32_t member_index, SpvDecoration decoration_type,
const Literals &literals = {});
423 void add_symbol(
const std::string &symbol, SpvId
id, SpvId scope_id);
425 void add_entry_point(SpvId func_id, SpvExecutionModel exec_model,
426 const Variables &variables = {});
429 void add_execution_mode_local_size(SpvId entry_point_id,
uint32_t local_size_x,
uint32_t local_size_y,
uint32_t local_size_z);
432 void add_execution_mode_local_size_id(SpvId entry_point_id, SpvId local_size_x, SpvId local_size_y, SpvId local_size_z);
435 void set_version_format(
uint32_t version);
438 void set_source_language(SpvSourceLanguage val);
441 void set_addressing_model(SpvAddressingModel val);
444 void set_memory_model(SpvMemoryModel val);
447 SpvSourceLanguage source_language()
const;
450 SpvAddressingModel addressing_model()
const;
453 SpvMemoryModel memory_model()
const;
456 SpvId import_glsl_intrinsics();
459 SpvId import_instruction_set(
const std::string &instruction_set);
462 void require_extension(
const std::string &extension);
465 void require_capability(SpvCapability);
468 bool is_imported(
const std::string &instruction_set)
const;
471 bool is_extension_required(
const std::string &extension)
const;
474 bool is_capability_required(SpvCapability)
const;
478 void enter_block(
const SpvBlock &block);
481 SpvBlock create_block(SpvId block_id);
484 SpvBlock current_block()
const;
487 SpvBlock leave_block();
490 void enter_function(
const SpvFunction &func);
493 SpvFunction lookup_function(SpvId func_id)
const;
496 SpvFunction current_function()
const;
499 SpvFunction leave_function();
502 SpvId current_id()
const;
505 void update_id(SpvId
id);
508 bool is_pointer_type(SpvId
id)
const;
509 bool is_struct_type(SpvId
id)
const;
510 bool is_vector_type(SpvId
id)
const;
511 bool is_scalar_type(SpvId
id)
const;
512 bool is_array_type(SpvId
id)
const;
513 bool is_constant(SpvId
id)
const;
516 SpvId lookup_base_type(SpvId pointer_type)
const;
519 SpvStorageClass lookup_storage_class(SpvId
id)
const;
522 SpvId lookup_id(
const std::string &symbol)
const;
525 SpvId lookup_scope(SpvId
id)
const;
528 SpvId lookup_import(
const std::string &instruction_set)
const;
534 SpvModule current_module()
const;
537 void append(SpvInstruction inst);
543 void encode(SpvBinary &binary)
const;
550 using TypeMap = std::unordered_map<TypeKey, SpvId>;
551 using KindMap = std::unordered_map<SpvId, SpvKind>;
552 using PointerTypeKey = std::pair<SpvId, SpvStorageClass>;
553 using PointerTypeMap = std::map<PointerTypeKey, SpvId>;
554 using BaseTypeMap = std::unordered_map<SpvId, SpvId>;
555 using VariableTypeMap = std::unordered_map<SpvId, SpvId>;
556 using StorageClassMap = std::unordered_map<SpvId, SpvStorageClass>;
558 using ConstantMap = std::unordered_map<ConstantKey, SpvId>;
559 using StringMap = std::unordered_map<ConstantKey, SpvId>;
560 using ScopeMap = std::unordered_map<SpvId, SpvId>;
561 using IdSymbolMap = std::unordered_map<SpvId, std::string>;
562 using SymbolIdMap = std::unordered_map<std::string, SpvId>;
564 using FunctionTypeMap = std::unordered_map<FunctionTypeKey, SpvId>;
565 using FunctionMap = std::unordered_map<SpvId, SpvFunction>;
569 SpvId make_id(SpvKind kind);
571 TypeKey make_type_key(
const Type &type,
uint32_t array_size = 1)
const;
572 SpvId lookup_type(
const Type &type,
uint32_t array_size = 1)
const;
574 TypeKey make_struct_type_key(
const StructMemberTypes &member_types)
const;
575 SpvId lookup_struct(
const std::string &name,
const StructMemberTypes &member_types)
const;
577 PointerTypeKey make_pointer_type_key(
const Type &type, SpvStorageClass storage_class)
const;
578 SpvId lookup_pointer_type(
const Type &type, SpvStorageClass storage_class)
const;
580 PointerTypeKey make_pointer_type_key(SpvId base_type_id, SpvStorageClass storage_class)
const;
581 SpvId lookup_pointer_type(SpvId base_type_id, SpvStorageClass storage_class)
const;
584 SpvId declare_scalar_constant_of_type(
const Type &scalar_type,
const T *data);
587 SpvId declare_specialization_constant_of_type(
const Type &scalar_type,
const T *data);
590 SpvBuilder::Components declare_constants_for_each_lane(Type type,
const void *data);
592 ConstantKey make_bool_constant_key(
bool value)
const;
593 ConstantKey make_string_constant_key(
const std::string &value)
const;
594 ConstantKey make_constant_key(
uint8_t code,
uint8_t bits,
int lanes,
size_t bytes,
const void *data,
bool is_specialization =
false)
const;
595 ConstantKey make_constant_key(
const Type &type,
const void *data,
bool is_specialization =
false)
const;
596 SpvId lookup_constant(
const Type &type,
const void *data,
bool is_specialization =
false)
const;
598 ConstantKey make_null_constant_key(
const Type &type)
const;
599 SpvId lookup_null_constant(
const Type &type)
const;
601 SpvId lookup_variable(
const std::string &name, SpvId type_id, SpvStorageClass storage_class, SpvId scope_id)
const;
602 bool has_variable(
const std::string &name, SpvId type_id, SpvStorageClass storage_class, SpvId scope_id)
const;
604 FunctionTypeKey make_function_type_key(SpvId return_type_id,
const ParamTypes ¶m_type_ids)
const;
605 SpvId lookup_function_type(SpvId return_type_id,
const ParamTypes ¶m_type_ids)
const;
607 SpvId active_id = SpvInvalidId;
608 SpvFunction active_function;
609 SpvBlock active_block;
615 StringMap string_map;
616 ConstantMap constant_map;
617 FunctionMap function_map;
618 IdSymbolMap id_symbol_map;
619 SymbolIdMap symbol_id_map;
620 BaseTypeMap base_type_map;
621 StorageClassMap storage_class_map;
622 PointerTypeMap pointer_type_map;
623 VariableTypeMap variable_type_map;
624 FunctionTypeMap function_type_map;
629 using Indices = std::vector<uint32_t>;
630 using Literals = std::vector<uint32_t>;
631 using BranchWeights = std::vector<uint32_t>;
632 using Components = std::vector<SpvId>;
633 using ParamTypes = std::vector<SpvId>;
634 using MemberTypeIds = std::vector<SpvId>;
635 using Operands = std::vector<SpvId>;
636 using Variables = std::vector<SpvId>;
637 using VariableBlockIdPair = std::pair<SpvId, SpvId>;
638 using BlockVariables = std::vector<VariableBlockIdPair>;
640 static SpvInstruction no_op(SpvId result_id);
641 static SpvInstruction capability(
const SpvCapability &capability);
642 static SpvInstruction extension(
const std::string &extension);
643 static SpvInstruction
import(SpvId instruction_set_id,
const std::string &instruction_set_name);
644 static SpvInstruction label(SpvId result_id);
645 static SpvInstruction debug_line(SpvId string_id,
uint32_t line,
uint32_t column);
646 static SpvInstruction debug_string(SpvId result_id,
const std::string &
string);
647 static SpvInstruction debug_symbol(SpvId target_id,
const std::string &symbol);
648 static SpvInstruction decorate(SpvId target_id, SpvDecoration decoration_type,
const Literals &literals = {});
649 static SpvInstruction decorate_member(SpvId struct_type_id,
uint32_t member_index, SpvDecoration decoration_type,
const Literals &literals = {});
650 static SpvInstruction void_type(SpvId void_type_id);
651 static SpvInstruction bool_type(SpvId bool_type_id);
652 static SpvInstruction integer_type(SpvId int_type_id,
uint32_t bits,
uint32_t signedness);
653 static SpvInstruction float_type(SpvId float_type_id,
uint32_t bits);
654 static SpvInstruction vector_type(SpvId vector_type_id, SpvId element_type_id,
uint32_t vector_size);
655 static SpvInstruction array_type(SpvId array_type_id, SpvId element_type_id, SpvId array_size_id);
656 static SpvInstruction struct_type(SpvId result_id,
const MemberTypeIds &member_type_ids);
657 static SpvInstruction runtime_array_type(SpvId result_type_id, SpvId base_type_id);
658 static SpvInstruction pointer_type(SpvId pointer_type_id, SpvStorageClass storage_class, SpvId base_type_id);
659 static SpvInstruction function_type(SpvId function_type_id, SpvId return_type_id,
const ParamTypes ¶m_type_ids);
660 static SpvInstruction constant(SpvId result_id, SpvId type_id,
size_t bytes,
const void *data, SpvValueType value_type);
661 static SpvInstruction null_constant(SpvId result_id, SpvId type_id);
662 static SpvInstruction bool_constant(SpvId result_id, SpvId type_id,
bool value);
663 static SpvInstruction string_constant(SpvId result_id,
const std::string &value);
664 static SpvInstruction composite_constant(SpvId result_id, SpvId type_id,
const Components &components);
665 static SpvInstruction specialization_constant(SpvId result_id, SpvId type_id,
size_t bytes,
const void *data, SpvValueType value_type);
666 static SpvInstruction variable(SpvId result_id, SpvId result_type_id,
uint32_t storage_class, SpvId initializer_id = SpvInvalidId);
667 static SpvInstruction
function(SpvId return_type_id, SpvId func_id,
uint32_t control_mask, SpvId func_type_id);
668 static SpvInstruction function_parameter(SpvId param_type_id, SpvId param_id);
669 static SpvInstruction function_end();
670 static SpvInstruction return_stmt(SpvId return_value_id = SpvInvalidId);
671 static SpvInstruction entry_point(SpvId exec_model, SpvId func_id,
const std::string &name,
const Variables &variables);
672 static SpvInstruction memory_model(SpvAddressingModel addressing_model, SpvMemoryModel memory_model);
673 static SpvInstruction exec_mode_local_size(SpvId function_id,
uint32_t local_size_size_x,
uint32_t local_size_size_y,
uint32_t local_size_size_z);
674 static SpvInstruction exec_mode_local_size_id(SpvId function_id, SpvId local_size_x_id, SpvId local_size_y_id, SpvId local_size_z_id);
675 static SpvInstruction memory_barrier(SpvId memory_scope_id, SpvId semantics_mask_id);
676 static SpvInstruction control_barrier(SpvId execution_scope_id, SpvId memory_scope_id, SpvId semantics_mask_id);
677 static SpvInstruction bitwise_not(SpvId type_id, SpvId result_id, SpvId src_id);
678 static SpvInstruction bitwise_and(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
679 static SpvInstruction logical_not(SpvId type_id, SpvId result_id, SpvId src_id);
680 static SpvInstruction logical_and(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
681 static SpvInstruction shift_right_logical(SpvId type_id, SpvId result_id, SpvId src_id, SpvId shift_id);
682 static SpvInstruction shift_right_arithmetic(SpvId type_id, SpvId result_id, SpvId src_id, SpvId shift_id);
683 static SpvInstruction multiply_extended(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
684 static SpvInstruction
select(SpvId type_id, SpvId result_id, SpvId condition_id, SpvId true_id, SpvId false_id);
685 static SpvInstruction in_bounds_access_chain(SpvId type_id, SpvId result_id, SpvId base_id,
const Indices &indices);
686 static SpvInstruction pointer_access_chain(SpvId type_id, SpvId result_id, SpvId base_id, SpvId element_id,
const Indices &indices);
687 static SpvInstruction load(SpvId type_id, SpvId result_id, SpvId ptr_id,
uint32_t access_mask = 0x0);
688 static SpvInstruction store(SpvId ptr_id, SpvId obj_id,
uint32_t access_mask = 0x0);
689 static SpvInstruction vector_insert_dynamic(SpvId type_id, SpvId result_id, SpvId vector_id, SpvId value_id, SpvId index_id);
690 static SpvInstruction vector_extract_dynamic(SpvId type_id, SpvId result_id, SpvId vector_id, SpvId value_id, SpvId index_id);
691 static SpvInstruction vector_shuffle(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
const Indices &indices);
692 static SpvInstruction composite_insert(SpvId type_id, SpvId result_id, SpvId object_id, SpvId composite_id,
const SpvFactory::Indices &indices);
693 static SpvInstruction composite_extract(SpvId type_id, SpvId result_id, SpvId composite_id,
const Indices &indices);
694 static SpvInstruction composite_construct(SpvId type_id, SpvId result_id,
const Components &constituents);
695 static SpvInstruction
is_inf(SpvId type_id, SpvId result_id, SpvId src_id);
696 static SpvInstruction
is_nan(SpvId type_id, SpvId result_id, SpvId src_id);
697 static SpvInstruction bitcast(SpvId type_id, SpvId result_id, SpvId src_id);
698 static SpvInstruction float_add(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
699 static SpvInstruction integer_add(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
700 static SpvInstruction integer_equal(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
701 static SpvInstruction integer_not_equal(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
702 static SpvInstruction integer_less_than(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
703 static SpvInstruction integer_less_than_equal(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
704 static SpvInstruction integer_greater_than(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
705 static SpvInstruction integer_greater_than_equal(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
706 static SpvInstruction branch(SpvId target_label_id);
707 static SpvInstruction conditional_branch(SpvId condition_label_id, SpvId true_label_id, SpvId false_label_id,
const BranchWeights &weights = {});
708 static SpvInstruction loop_merge(SpvId merge_label_id, SpvId continue_label_id,
uint32_t loop_control_mask = SpvLoopControlMaskNone);
709 static SpvInstruction selection_merge(SpvId merge_label_id,
uint32_t selection_control_mask = SpvSelectionControlMaskNone);
710 static SpvInstruction phi(SpvId type_id, SpvId result_id,
const BlockVariables &block_vars);
711 static SpvInstruction unary_op(SpvOp op_code, SpvId type_id, SpvId result_id, SpvId src_id);
712 static SpvInstruction binary_op(SpvOp op_code, SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
713 static SpvInstruction convert(SpvOp op_code, SpvId type_id, SpvId result_id, SpvId src_id);
714 static SpvInstruction extended(SpvId instruction_set_id, SpvId instruction_number, SpvId type_id, SpvId result_id,
const SpvFactory::Operands &operands);
718 struct SpvInstructionContents {
719 using Operands = std::vector<SpvId>;
720 using ValueTypes = std::vector<SpvValueType>;
722 SpvOp op_code = SpvOpNop;
723 SpvId result_id = SpvNoResult;
724 SpvId type_id = SpvNoType;
726 ValueTypes value_types;
731 struct SpvBlockContents {
732 using Instructions = std::vector<SpvInstruction>;
733 using Variables = std::vector<SpvInstruction>;
734 using Blocks = std::vector<SpvBlock>;
736 SpvId block_id = SpvInvalidId;
738 Instructions instructions;
742 bool reachable =
true;
746 struct SpvFunctionContents {
747 using PrecisionMap = std::unordered_map<SpvId, SpvPrecision>;
748 using Parameters = std::vector<SpvInstruction>;
749 using Blocks = std::vector<SpvBlock>;
753 SpvId function_type_id;
754 SpvId return_type_id;
756 SpvInstruction declaration;
757 Parameters parameters;
758 PrecisionMap precision;
763 struct SpvModuleContents {
764 using Capabilities = std::set<SpvCapability>;
765 using Extensions = std::set<std::string>;
766 using Imports = std::unordered_map<std::string, SpvId>;
767 using Functions = std::vector<SpvFunction>;
768 using Instructions = std::vector<SpvInstruction>;
769 using EntryPoints = std::unordered_map<std::string, SpvInstruction>;
772 SpvId module_id = SpvInvalidId;
773 SpvId version_format = SpvVersion;
774 SpvId binding_count = 0;
775 SpvSourceLanguage source_language = SpvSourceLanguageUnknown;
776 SpvAddressingModel addressing_model = SpvAddressingModelLogical;
777 SpvMemoryModel memory_model = SpvMemoryModelSimple;
778 Capabilities capabilities;
779 Extensions extensions;
781 EntryPoints entry_points;
782 Instructions execution_modes;
783 Instructions debug_source;
784 Instructions debug_symbols;
785 Instructions annotations;
787 Instructions constants;
788 Instructions globals;
790 Instructions instructions;
794 bool is_glsl_unary_op(SpvId glsl_op_code);
795 bool is_glsl_binary_op(SpvId glsl_op_code);
796 uint32_t glsl_operand_count(SpvId glsl_op_code);
799 std::ostream &
operator<<(std::ostream &stream,
const SpvModule &);
802 std::ostream &
operator<<(std::ostream &stream,
const SpvFunction &);
805 std::ostream &
operator<<(std::ostream &stream,
const SpvBlock &);
808 std::ostream &
operator<<(std::ostream &stream,
const SpvInstruction &);
824 #endif // HALIDE_SPIRV_IR_H