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;
120 struct SpvModuleContents;
121 struct SpvFunctionContents;
122 struct SpvBlockContents;
123 struct SpvInstructionContents;
126 using SpvModuleContentsPtr = IntrusivePtr<SpvModuleContents>;
127 using SpvFunctionContentsPtr = IntrusivePtr<SpvFunctionContents>;
128 using SpvBlockContentsPtr = IntrusivePtr<SpvBlockContents>;
129 using SpvInstructionContentsPtr = IntrusivePtr<SpvInstructionContents>;
132 class SpvInstruction {
134 using LiteralValue = std::pair<uint32_t, SpvValueType>;
135 using Immediates = std::vector<LiteralValue>;
136 using Operands = std::vector<SpvId>;
137 using ValueTypes = std::vector<SpvValueType>;
139 SpvInstruction() =
default;
142 SpvInstruction(
const SpvInstruction &) =
default;
143 SpvInstruction &operator=(
const SpvInstruction &) =
default;
144 SpvInstruction(SpvInstruction &&) =
default;
145 SpvInstruction &operator=(SpvInstruction &&) =
default;
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 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;
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 const Instructions &instructions()
const;
202 const Variables &variables()
const;
203 bool is_reachable()
const;
204 bool is_terminated()
const;
205 bool is_defined()
const;
207 void check_defined()
const;
210 void encode(SpvBinary &binary)
const;
212 static SpvBlock make(SpvId block_id);
215 SpvBlockContentsPtr contents;
221 using Blocks = std::vector<SpvBlock>;
222 using Parameters = std::vector<SpvInstruction>;
224 SpvFunction() =
default;
227 SpvFunction(
const SpvFunction &) =
default;
228 SpvFunction &operator=(
const SpvFunction &) =
default;
229 SpvFunction(SpvFunction &&) =
default;
230 SpvFunction &operator=(SpvFunction &&) =
default;
232 SpvBlock create_block(SpvId block_id);
233 void add_block(SpvBlock block);
234 void add_parameter(SpvInstruction param);
235 void set_return_precision(SpvPrecision precision);
236 void set_parameter_precision(
uint32_t index, SpvPrecision precision);
237 bool is_defined()
const;
240 const Blocks &blocks()
const;
241 SpvBlock entry_block()
const;
242 SpvBlock tail_block()
const;
243 SpvPrecision return_precision()
const;
244 const Parameters ¶meters()
const;
245 SpvPrecision parameter_precision(
uint32_t index)
const;
248 SpvInstruction declaration()
const;
249 SpvId return_type_id()
const;
250 SpvId type_id()
const;
252 void check_defined()
const;
254 void encode(SpvBinary &binary)
const;
256 static SpvFunction make(SpvId func_id, SpvId func_type_id, SpvId return_type_id,
uint32_t control_mask = SpvFunctionControlMaskNone);
259 SpvFunctionContentsPtr contents;
265 using ImportDefinition = std::pair<SpvId, std::string>;
266 using ImportNames = std::vector<std::string>;
267 using EntryPointNames = std::vector<std::string>;
268 using Instructions = std::vector<SpvInstruction>;
269 using Functions = std::vector<SpvFunction>;
270 using Capabilities = std::vector<SpvCapability>;
271 using Extensions = std::vector<std::string>;
272 using Imports = std::vector<ImportDefinition>;
274 SpvModule() =
default;
277 SpvModule(
const SpvModule &) =
default;
278 SpvModule &operator=(
const SpvModule &) =
default;
279 SpvModule(SpvModule &&) =
default;
280 SpvModule &operator=(SpvModule &&) =
default;
282 void add_debug_string(SpvId result_id,
const std::string &
string);
283 void add_debug_symbol(SpvId
id,
const std::string &symbol);
284 void add_annotation(SpvInstruction val);
285 void add_type(SpvInstruction val);
286 void add_constant(SpvInstruction val);
287 void add_global(SpvInstruction val);
288 void add_execution_mode(SpvInstruction val);
289 void add_function(SpvFunction val);
290 void add_instruction(SpvInstruction val);
291 void add_entry_point(
const std::string &name, SpvInstruction entry_point);
293 void import_instruction_set(SpvId
id,
const std::string &instruction_set);
294 void require_capability(SpvCapability val);
295 void require_extension(
const std::string &val);
297 void set_version_format(
uint32_t version);
298 void set_source_language(SpvSourceLanguage val);
299 void set_addressing_model(SpvAddressingModel val);
300 void set_memory_model(SpvMemoryModel val);
301 void set_binding_count(SpvId count);
304 SpvSourceLanguage source_language()
const;
305 SpvAddressingModel addressing_model()
const;
306 SpvMemoryModel memory_model()
const;
307 SpvInstruction entry_point(
const std::string &name)
const;
308 EntryPointNames entry_point_names()
const;
309 ImportNames import_names()
const;
310 SpvId lookup_import(
const std::string &Instruction_set)
const;
313 Imports imports()
const;
314 Extensions extensions()
const;
315 Capabilities capabilities()
const;
316 Instructions entry_points()
const;
317 const Instructions &execution_modes()
const;
318 const Instructions &debug_source()
const;
319 const Instructions &debug_symbols()
const;
320 const Instructions &annotations()
const;
321 const Instructions &type_definitions()
const;
322 const Instructions &global_constants()
const;
323 const Instructions &global_variables()
const;
324 const Functions &function_definitions()
const;
327 SpvModule module()
const;
329 bool is_imported(
const std::string &instruction_set)
const;
330 bool is_capability_required(SpvCapability val)
const;
331 bool is_extension_required(
const std::string &val)
const;
332 bool is_defined()
const;
334 void check_defined()
const;
337 void encode(SpvBinary &binary)
const;
339 static SpvModule make(SpvId module_id,
340 SpvSourceLanguage source_language = SpvSourceLanguageUnknown,
341 SpvAddressingModel addressing_model = SpvAddressingModelLogical,
342 SpvMemoryModel memory_model = SpvMemoryModelSimple);
345 SpvModuleContentsPtr contents;
353 using ParamTypes = std::vector<SpvId>;
354 using Components = std::vector<SpvId>;
355 using StructMemberTypes = std::vector<SpvId>;
356 using Variables = std::vector<SpvId>;
357 using Indices = std::vector<uint32_t>;
358 using Literals = std::vector<uint32_t>;
361 ~SpvBuilder() =
default;
363 SpvBuilder(
const SpvBuilder &) =
delete;
364 SpvBuilder &operator=(
const SpvBuilder &) =
delete;
367 SpvId reserve_id(SpvKind = SpvResultId);
370 SpvKind kind_of(SpvId
id)
const;
373 std::string kind_name(SpvKind kind)
const;
376 SpvId
type_of(SpvId variable_id)
const;
385 SpvId declare_void_type();
386 SpvId declare_type(
const Type &type,
uint32_t array_size = 1);
387 SpvId declare_pointer_type(
const Type &type, SpvStorageClass storage_class);
388 SpvId declare_pointer_type(SpvId type_id, SpvStorageClass storage_class);
389 SpvId declare_constant(
const Type &type,
const void *data,
bool is_specialization =
false);
390 SpvId declare_null_constant(
const Type &type);
391 SpvId declare_bool_constant(
bool value);
392 SpvId declare_string_constant(
const std::string &str);
393 SpvId declare_integer_constant(
const Type &type,
int64_t value);
394 SpvId declare_float_constant(
const Type &type,
double value);
395 SpvId declare_scalar_constant(
const Type &type,
const void *data);
396 SpvId declare_vector_constant(
const Type &type,
const void *data);
397 SpvId declare_specialization_constant(
const Type &type,
const void *data);
398 SpvId declare_access_chain(SpvId ptr_type_id, SpvId base_id,
const Indices &indices);
399 SpvId declare_pointer_access_chain(SpvId ptr_type_id, SpvId base_id, SpvId element_id,
const Indices &indices);
400 SpvId declare_function_type(SpvId return_type,
const ParamTypes ¶m_types = {});
401 SpvId declare_function(
const std::string &name, SpvId function_type);
402 SpvId declare_struct(
const std::string &name,
const StructMemberTypes &member_types);
403 SpvId declare_variable(
const std::string &name, SpvId type_id, SpvStorageClass storage_class, SpvId initializer_id = SpvInvalidId);
404 SpvId declare_global_variable(
const std::string &name, SpvId type_id, SpvStorageClass storage_class, SpvId initializer_id = SpvInvalidId);
405 SpvId declare_symbol(
const std::string &symbol, SpvId
id, SpvId scope_id);
409 SpvId add_type(
const Type &type,
uint32_t array_size = 1);
410 SpvId add_struct(
const std::string &name,
const StructMemberTypes &member_types);
411 SpvId add_array_with_default_size(SpvId base_type_id, SpvId array_size_id);
412 SpvId add_runtime_array(SpvId base_type_id);
413 SpvId add_pointer_type(
const Type &type, SpvStorageClass storage_class);
414 SpvId add_pointer_type(SpvId base_type_id, SpvStorageClass storage_class);
415 SpvId add_constant(
const Type &type,
const void *data,
bool is_specialization =
false);
416 SpvId add_function_type(SpvId return_type_id,
const ParamTypes ¶m_type_ids);
417 SpvId add_function(
const std::string &name, SpvId return_type,
const ParamTypes ¶m_types = {});
418 SpvId add_instruction(SpvInstruction val);
420 void add_annotation(SpvId target_id, SpvDecoration decoration_type,
const Literals &literals = {});
421 void add_struct_annotation(SpvId struct_type_id,
uint32_t member_index, SpvDecoration decoration_type,
const Literals &literals = {});
422 void add_symbol(
const std::string &symbol, SpvId
id, SpvId scope_id);
424 void add_entry_point(SpvId func_id, SpvExecutionModel exec_model,
425 const Variables &variables = {});
428 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);
431 void add_execution_mode_local_size_id(SpvId entry_point_id, SpvId local_size_x, SpvId local_size_y, SpvId local_size_z);
434 void set_version_format(
uint32_t version);
437 void set_source_language(SpvSourceLanguage val);
440 void set_addressing_model(SpvAddressingModel val);
443 void set_memory_model(SpvMemoryModel val);
446 SpvSourceLanguage source_language()
const;
449 SpvAddressingModel addressing_model()
const;
452 SpvMemoryModel memory_model()
const;
455 SpvId import_glsl_intrinsics();
458 SpvId import_instruction_set(
const std::string &instruction_set);
461 void require_extension(
const std::string &extension);
464 void require_capability(SpvCapability);
467 bool is_imported(
const std::string &instruction_set)
const;
470 bool is_extension_required(
const std::string &extension)
const;
473 bool is_capability_required(SpvCapability)
const;
477 void enter_block(
const SpvBlock &block);
480 SpvBlock create_block(SpvId block_id);
483 SpvBlock current_block()
const;
486 SpvBlock leave_block();
489 void enter_function(
const SpvFunction &func);
492 SpvFunction lookup_function(SpvId func_id)
const;
495 SpvFunction current_function()
const;
498 SpvFunction leave_function();
501 SpvId current_id()
const;
504 void update_id(SpvId
id);
507 bool is_pointer_type(SpvId
id)
const;
508 bool is_struct_type(SpvId
id)
const;
509 bool is_vector_type(SpvId
id)
const;
510 bool is_scalar_type(SpvId
id)
const;
511 bool is_array_type(SpvId
id)
const;
512 bool is_constant(SpvId
id)
const;
515 SpvId lookup_base_type(SpvId pointer_type)
const;
518 SpvStorageClass lookup_storage_class(SpvId
id)
const;
521 SpvId lookup_id(
const std::string &symbol)
const;
524 SpvId lookup_scope(SpvId
id)
const;
527 SpvId lookup_import(
const std::string &instruction_set)
const;
533 SpvModule current_module()
const;
536 void append(SpvInstruction inst);
542 void encode(SpvBinary &binary)
const;
549 using TypeMap = std::unordered_map<TypeKey, SpvId>;
550 using KindMap = std::unordered_map<SpvId, SpvKind>;
551 using PointerTypeKey = std::pair<SpvId, SpvStorageClass>;
552 using PointerTypeMap = std::map<PointerTypeKey, SpvId>;
553 using BaseTypeMap = std::unordered_map<SpvId, SpvId>;
554 using VariableTypeMap = std::unordered_map<SpvId, SpvId>;
555 using StorageClassMap = std::unordered_map<SpvId, SpvStorageClass>;
557 using ConstantMap = std::unordered_map<ConstantKey, SpvId>;
558 using StringMap = std::unordered_map<ConstantKey, SpvId>;
559 using ScopeMap = std::unordered_map<SpvId, SpvId>;
560 using IdSymbolMap = std::unordered_map<SpvId, std::string>;
561 using SymbolIdMap = std::unordered_map<std::string, SpvId>;
563 using FunctionTypeMap = std::unordered_map<FunctionTypeKey, SpvId>;
564 using FunctionMap = std::unordered_map<SpvId, SpvFunction>;
568 SpvId make_id(SpvKind kind);
570 TypeKey make_type_key(
const Type &type,
uint32_t array_size = 1)
const;
571 SpvId lookup_type(
const Type &type,
uint32_t array_size = 1)
const;
573 TypeKey make_struct_type_key(
const StructMemberTypes &member_types)
const;
574 SpvId lookup_struct(
const std::string &name,
const StructMemberTypes &member_types)
const;
576 PointerTypeKey make_pointer_type_key(
const Type &type, SpvStorageClass storage_class)
const;
577 SpvId lookup_pointer_type(
const Type &type, SpvStorageClass storage_class)
const;
579 PointerTypeKey make_pointer_type_key(SpvId base_type_id, SpvStorageClass storage_class)
const;
580 SpvId lookup_pointer_type(SpvId base_type_id, SpvStorageClass storage_class)
const;
583 SpvId declare_scalar_constant_of_type(
const Type &scalar_type,
const T *data);
586 SpvId declare_specialization_constant_of_type(
const Type &scalar_type,
const T *data);
589 SpvBuilder::Components declare_constants_for_each_lane(Type type,
const void *data);
591 ConstantKey make_bool_constant_key(
bool value)
const;
592 ConstantKey make_string_constant_key(
const std::string &value)
const;
593 ConstantKey make_constant_key(
uint8_t code,
uint8_t bits,
int lanes,
size_t bytes,
const void *data,
bool is_specialization =
false)
const;
594 ConstantKey make_constant_key(
const Type &type,
const void *data,
bool is_specialization =
false)
const;
595 SpvId lookup_constant(
const Type &type,
const void *data,
bool is_specialization =
false)
const;
597 ConstantKey make_null_constant_key(
const Type &type)
const;
598 SpvId lookup_null_constant(
const Type &type)
const;
600 SpvId lookup_variable(
const std::string &name, SpvId type_id, SpvStorageClass storage_class, SpvId scope_id)
const;
601 bool has_variable(
const std::string &name, SpvId type_id, SpvStorageClass storage_class, SpvId scope_id)
const;
603 FunctionTypeKey make_function_type_key(SpvId return_type_id,
const ParamTypes ¶m_type_ids)
const;
604 SpvId lookup_function_type(SpvId return_type_id,
const ParamTypes ¶m_type_ids)
const;
606 SpvId active_id = SpvInvalidId;
607 SpvFunction active_function;
608 SpvBlock active_block;
614 StringMap string_map;
615 ConstantMap constant_map;
616 FunctionMap function_map;
617 IdSymbolMap id_symbol_map;
618 SymbolIdMap symbol_id_map;
619 BaseTypeMap base_type_map;
620 StorageClassMap storage_class_map;
621 PointerTypeMap pointer_type_map;
622 VariableTypeMap variable_type_map;
623 FunctionTypeMap function_type_map;
628 using Indices = std::vector<uint32_t>;
629 using Literals = std::vector<uint32_t>;
630 using BranchWeights = std::vector<uint32_t>;
631 using Components = std::vector<SpvId>;
632 using ParamTypes = std::vector<SpvId>;
633 using MemberTypeIds = std::vector<SpvId>;
634 using Operands = std::vector<SpvId>;
635 using Variables = std::vector<SpvId>;
636 using VariableBlockIdPair = std::pair<SpvId, SpvId>;
637 using BlockVariables = std::vector<VariableBlockIdPair>;
639 static SpvInstruction no_op(SpvId result_id);
640 static SpvInstruction capability(
const SpvCapability &capability);
641 static SpvInstruction extension(
const std::string &extension);
642 static SpvInstruction
import(SpvId instruction_set_id,
const std::string &instruction_set_name);
643 static SpvInstruction label(SpvId result_id);
644 static SpvInstruction debug_line(SpvId string_id,
uint32_t line,
uint32_t column);
645 static SpvInstruction debug_string(SpvId result_id,
const std::string &
string);
646 static SpvInstruction debug_symbol(SpvId target_id,
const std::string &symbol);
647 static SpvInstruction decorate(SpvId target_id, SpvDecoration decoration_type,
const Literals &literals = {});
648 static SpvInstruction decorate_member(SpvId struct_type_id,
uint32_t member_index, SpvDecoration decoration_type,
const Literals &literals = {});
649 static SpvInstruction void_type(SpvId void_type_id);
650 static SpvInstruction bool_type(SpvId bool_type_id);
651 static SpvInstruction integer_type(SpvId int_type_id,
uint32_t bits,
uint32_t signedness);
652 static SpvInstruction float_type(SpvId float_type_id,
uint32_t bits);
653 static SpvInstruction vector_type(SpvId vector_type_id, SpvId element_type_id,
uint32_t vector_size);
654 static SpvInstruction array_type(SpvId array_type_id, SpvId element_type_id, SpvId array_size_id);
655 static SpvInstruction struct_type(SpvId result_id,
const MemberTypeIds &member_type_ids);
656 static SpvInstruction runtime_array_type(SpvId result_type_id, SpvId base_type_id);
657 static SpvInstruction pointer_type(SpvId pointer_type_id, SpvStorageClass storage_class, SpvId base_type_id);
658 static SpvInstruction function_type(SpvId function_type_id, SpvId return_type_id,
const ParamTypes ¶m_type_ids);
659 static SpvInstruction constant(SpvId result_id, SpvId type_id,
size_t bytes,
const void *data, SpvValueType value_type);
660 static SpvInstruction null_constant(SpvId result_id, SpvId type_id);
661 static SpvInstruction bool_constant(SpvId result_id, SpvId type_id,
bool value);
662 static SpvInstruction string_constant(SpvId result_id,
const std::string &value);
663 static SpvInstruction composite_constant(SpvId result_id, SpvId type_id,
const Components &components);
664 static SpvInstruction specialization_constant(SpvId result_id, SpvId type_id,
size_t bytes,
const void *data, SpvValueType value_type);
665 static SpvInstruction variable(SpvId result_id, SpvId result_type_id,
uint32_t storage_class, SpvId initializer_id = SpvInvalidId);
666 static SpvInstruction
function(SpvId return_type_id, SpvId func_id,
uint32_t control_mask, SpvId func_type_id);
667 static SpvInstruction function_parameter(SpvId param_type_id, SpvId param_id);
668 static SpvInstruction function_end();
669 static SpvInstruction return_stmt(SpvId return_value_id = SpvInvalidId);
670 static SpvInstruction entry_point(SpvId exec_model, SpvId func_id,
const std::string &name,
const Variables &variables);
671 static SpvInstruction memory_model(SpvAddressingModel addressing_model, SpvMemoryModel memory_model);
672 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);
673 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);
674 static SpvInstruction memory_barrier(SpvId memory_scope_id, SpvId semantics_mask_id);
675 static SpvInstruction control_barrier(SpvId execution_scope_id, SpvId memory_scope_id, SpvId semantics_mask_id);
676 static SpvInstruction bitwise_not(SpvId type_id, SpvId result_id, SpvId src_id);
677 static SpvInstruction bitwise_and(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
678 static SpvInstruction logical_not(SpvId type_id, SpvId result_id, SpvId src_id);
679 static SpvInstruction logical_and(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
680 static SpvInstruction shift_right_logical(SpvId type_id, SpvId result_id, SpvId src_id, SpvId shift_id);
681 static SpvInstruction shift_right_arithmetic(SpvId type_id, SpvId result_id, SpvId src_id, SpvId shift_id);
682 static SpvInstruction multiply_extended(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
683 static SpvInstruction
select(SpvId type_id, SpvId result_id, SpvId condition_id, SpvId true_id, SpvId false_id);
684 static SpvInstruction in_bounds_access_chain(SpvId type_id, SpvId result_id, SpvId base_id,
const Indices &indices);
685 static SpvInstruction pointer_access_chain(SpvId type_id, SpvId result_id, SpvId base_id, SpvId element_id,
const Indices &indices);
686 static SpvInstruction load(SpvId type_id, SpvId result_id, SpvId ptr_id,
uint32_t access_mask = 0x0);
687 static SpvInstruction store(SpvId ptr_id, SpvId obj_id,
uint32_t access_mask = 0x0);
688 static SpvInstruction vector_insert_dynamic(SpvId type_id, SpvId result_id, SpvId vector_id, SpvId value_id, SpvId index_id);
689 static SpvInstruction vector_extract_dynamic(SpvId type_id, SpvId result_id, SpvId vector_id, SpvId value_id, SpvId index_id);
690 static SpvInstruction vector_shuffle(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
const Indices &indices);
691 static SpvInstruction composite_insert(SpvId type_id, SpvId result_id, SpvId object_id, SpvId composite_id,
const SpvFactory::Indices &indices);
692 static SpvInstruction composite_extract(SpvId type_id, SpvId result_id, SpvId composite_id,
const Indices &indices);
693 static SpvInstruction composite_construct(SpvId type_id, SpvId result_id,
const Components &constituents);
694 static SpvInstruction
is_inf(SpvId type_id, SpvId result_id, SpvId src_id);
695 static SpvInstruction
is_nan(SpvId type_id, SpvId result_id, SpvId src_id);
696 static SpvInstruction bitcast(SpvId type_id, SpvId result_id, SpvId src_id);
697 static SpvInstruction float_add(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
698 static SpvInstruction integer_add(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
699 static SpvInstruction integer_equal(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
700 static SpvInstruction integer_not_equal(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
701 static SpvInstruction integer_less_than(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
702 static SpvInstruction integer_less_than_equal(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
703 static SpvInstruction integer_greater_than(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
704 static SpvInstruction integer_greater_than_equal(SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id,
bool is_signed);
705 static SpvInstruction branch(SpvId target_label_id);
706 static SpvInstruction conditional_branch(SpvId condition_label_id, SpvId true_label_id, SpvId false_label_id,
const BranchWeights &weights = {});
707 static SpvInstruction loop_merge(SpvId merge_label_id, SpvId continue_label_id,
uint32_t loop_control_mask = SpvLoopControlMaskNone);
708 static SpvInstruction selection_merge(SpvId merge_label_id,
uint32_t selection_control_mask = SpvSelectionControlMaskNone);
709 static SpvInstruction phi(SpvId type_id, SpvId result_id,
const BlockVariables &block_vars);
710 static SpvInstruction unary_op(SpvOp op_code, SpvId type_id, SpvId result_id, SpvId src_id);
711 static SpvInstruction binary_op(SpvOp op_code, SpvId type_id, SpvId result_id, SpvId src_a_id, SpvId src_b_id);
712 static SpvInstruction convert(SpvOp op_code, SpvId type_id, SpvId result_id, SpvId src_id);
713 static SpvInstruction extended(SpvId instruction_set_id, SpvId instruction_number, SpvId type_id, SpvId result_id,
const SpvFactory::Operands &operands);
717 struct SpvInstructionContents {
718 using Operands = std::vector<SpvId>;
719 using ValueTypes = std::vector<SpvValueType>;
721 SpvOp op_code = SpvOpNop;
722 SpvId result_id = SpvNoResult;
723 SpvId type_id = SpvNoType;
725 ValueTypes value_types;
729 struct SpvBlockContents {
730 using Instructions = std::vector<SpvInstruction>;
731 using Variables = std::vector<SpvInstruction>;
732 using Blocks = std::vector<SpvBlock>;
734 SpvId block_id = SpvInvalidId;
735 Instructions instructions;
739 bool reachable =
true;
743 struct SpvFunctionContents {
744 using PrecisionMap = std::unordered_map<SpvId, SpvPrecision>;
745 using Parameters = std::vector<SpvInstruction>;
746 using Blocks = std::vector<SpvBlock>;
749 SpvId function_type_id;
750 SpvId return_type_id;
752 SpvInstruction declaration;
753 Parameters parameters;
754 PrecisionMap precision;
759 struct SpvModuleContents {
760 using Capabilities = std::set<SpvCapability>;
761 using Extensions = std::set<std::string>;
762 using Imports = std::unordered_map<std::string, SpvId>;
763 using Functions = std::vector<SpvFunction>;
764 using Instructions = std::vector<SpvInstruction>;
765 using EntryPoints = std::unordered_map<std::string, SpvInstruction>;
768 SpvId module_id = SpvInvalidId;
769 SpvId version_format = SpvVersion;
770 SpvId binding_count = 0;
771 SpvSourceLanguage source_language = SpvSourceLanguageUnknown;
772 SpvAddressingModel addressing_model = SpvAddressingModelLogical;
773 SpvMemoryModel memory_model = SpvMemoryModelSimple;
774 Capabilities capabilities;
775 Extensions extensions;
777 EntryPoints entry_points;
778 Instructions execution_modes;
779 Instructions debug_source;
780 Instructions debug_symbols;
781 Instructions annotations;
783 Instructions constants;
784 Instructions globals;
786 Instructions instructions;
790 bool is_glsl_unary_op(SpvId glsl_op_code);
791 bool is_glsl_binary_op(SpvId glsl_op_code);
792 uint32_t glsl_operand_count(SpvId glsl_op_code);
795 std::ostream &
operator<<(std::ostream &stream,
const SpvModule &);
798 std::ostream &
operator<<(std::ostream &stream,
const SpvFunction &);
801 std::ostream &
operator<<(std::ostream &stream,
const SpvBlock &);
804 std::ostream &
operator<<(std::ostream &stream,
const SpvInstruction &);
Support classes for reference-counting via intrusive shared pointers.
void * lookup_symbol(const char *sym, const known_symbol *map)
void spirv_ir_test()
Internal test for SPIR-V IR.
std::ostream & operator<<(std::ostream &stream, const Stmt &)
Emit a halide statement on an output stream (such as std::cout) in a human-readable form.
RefCount & ref_count(const T *t) noexcept
Because in this header we don't yet know how client classes store their RefCount (and we don't want t...
This file defines the class FunctionDAG, which is our representation of a Halide pipeline,...
@ Internal
Not visible externally, similar to 'static' linkage in C.
Type type_of()
Construct the halide equivalent of a C type.
Expr select(Expr condition, Expr true_value, Expr false_value)
Returns an expression similar to the ternary operator in C, except that it always evaluates all argum...
Expr is_nan(Expr x)
Returns true if the argument is a Not a Number (NaN).
Expr is_inf(Expr x)
Returns true if the argument is Inf or -Inf.
unsigned __INT64_TYPE__ uint64_t
signed __INT64_TYPE__ int64_t
unsigned __INT8_TYPE__ uint8_t
unsigned __INT32_TYPE__ uint32_t