docc 0.8.3 (0d44999b)
C++ API Reference
Loading...
Searching...
No Matches
type.h
Go to the documentation of this file.
1
8
9#pragma once
10
11#include <cassert>
12#include <concepts>
13#include <memory>
14#include <nlohmann/json.hpp>
15#include <string>
16#include <string_view>
17#include <type_traits>
18
19#include "sdfg/exceptions.h"
21
22using json = nlohmann::json;
23
24namespace sdfg {
25
26namespace types {
27
56
65public:
74
75private:
76 std::string value_;
77 symbolic::Expression allocation_size_;
78 AllocationType allocation_;
79 AllocationType deallocation_;
80 std::vector<symbolic::Expression> args_;
81
82public:
87 StorageType(const std::string& value)
88 : value_(value), allocation_size_(SymEngine::null), allocation_(Unmanaged), deallocation_(Unmanaged) {
89 }
90
99 const std::string& value,
103 )
104 : value_(value), allocation_size_(allocation_size), allocation_(allocation), deallocation_(deallocation) {
105 }
106
108 std::string value() const {
109 return value_;
110 }
111
113 void value(const std::string& value) {
114 value_ = value;
115 }
116
119 return allocation_size_;
120 }
121
124 allocation_size_ = allocation_size;
125 }
126
128 [[deprecated("use args")]]
129 const symbolic::Expression& arg1() const {
130 return args_.at(0);
131 }
132
134 [[deprecated("use args")]]
135 void arg1(const symbolic::Expression& arg) {
136 if (args_.size() > 1) {
137 args_.at(0) = arg;
138 } else {
139 args_.push_back(arg);
140 }
141 }
142
143 std::vector<symbolic::Expression>& args() {
144 return args_;
145 }
146
147 const std::vector<symbolic::Expression>& args() const {
148 return args_;
149 }
150
151 void args(const std::vector<symbolic::Expression>& args) {
152 args_ = args;
153 }
154
155 void args(std::vector<symbolic::Expression>&& args) {
156 args_ = std::move(args);
157 }
158
161 return allocation_;
162 }
163
166 allocation_ = allocation;
167 }
168
171 return deallocation_;
172 }
173
176 deallocation_ = deallocation;
177 }
178
184 bool operator==(const StorageType& other) const {
185 if (value_ != other.value_) {
186 return false;
187 }
188 if (allocation_ != other.allocation_) {
189 return false;
190 }
191 if (deallocation_ != other.deallocation_) {
192 return false;
193 }
194 if (!symbolic::null_safe_eq(allocation_size_, other.allocation_size_)) {
195 return false;
196 }
197 if (args_.size() != other.args_.size()) {
198 return false;
199 } else {
200 for (size_t i = 0; i < args_.size(); ++i) {
201 if (!symbolic::null_safe_eq(args_[i], other.args_[i])) {
202 return false;
203 }
204 }
205 }
206 return true;
207 }
208
210 bool is_cpu_stack() const {
211 return value_ == "CPU_Stack";
212 }
213
215 bool is_cpu_heap() const {
216 return value_ == "CPU_Heap";
217 }
218
220 bool is_device() const {
221 return !is_cpu_stack() && !is_cpu_heap();
222 }
223
225 bool is_nv_generic() const {
226 return value_ == "NV_Generic";
227 }
228
230 bool is_amd_generic() const {
231 return value_ == "AMD_Generic";
232 }
233
235 bool is_nv_global() const {
236 return value_ == "NV_Global";
237 }
238
240 bool is_nv_shared() const {
241 return value_ == "NV_Shared";
242 }
243
245 bool is_nv_constant() const {
246 return value_ == "NV_Constant";
247 }
248
250 bool is_nv_symbol() const {
251 return value_ == "NV_Symbol";
252 }
253
262 bool is_gpu_static() const {
263 return value_ == "GPU_Static";
264 }
265
271 return StorageType("CPU_Stack", SymEngine::null, StorageType::Unmanaged, StorageType::Unmanaged);
272 }
273
279 return StorageType("CPU_Heap", SymEngine::null, StorageType::Unmanaged, StorageType::Unmanaged);
280 }
281
296
299 return StorageType("NV_Generic");
300 }
301
304 return StorageType("NV_Global");
305 }
306
309 return StorageType("NV_Shared");
310 }
311
314 return StorageType("NV_Constant");
315 }
316
319 return StorageType("NV_Symbol");
320 }
321
324 return StorageType("GPU_Static");
325 }
326};
327
334constexpr const char* primitive_type_to_string(PrimitiveType e) {
335 switch (e) {
337 return "Void";
339 return "Bool";
341 return "Int8";
343 return "Int16";
345 return "Int32";
347 return "Int64";
349 return "Int128";
351 return "UInt8";
353 return "UInt16";
355 return "UInt32";
357 return "UInt64";
359 return "UInt128";
361 return "Half";
363 return "BFloat";
365 return "Float";
367 return "Double";
369 return "X86_FP80";
371 return "FP128";
373 return "PPC_FP128";
374 }
375 throw std::invalid_argument("Invalid primitive type");
376};
377
384constexpr PrimitiveType primitive_type_from_string(std::string_view e) {
385 if (e == "Void") {
386 return PrimitiveType::Void;
387 } else if (e == "Bool") {
388 return PrimitiveType::Bool;
389 } else if (e == "Int8") {
390 return PrimitiveType::Int8;
391 } else if (e == "Int16") {
393 } else if (e == "Int32") {
395 } else if (e == "Int64") {
397 } else if (e == "Int128") {
399 } else if (e == "UInt8") {
401 } else if (e == "UInt16") {
403 } else if (e == "UInt32") {
405 } else if (e == "UInt64") {
407 } else if (e == "UInt128") {
409 } else if (e == "Half") {
410 return PrimitiveType::Half;
411 } else if (e == "BFloat") {
413 } else if (e == "Float") {
415 } else if (e == "Double") {
417 } else if (e == "X86_FP80") {
419 } else if (e == "FP128") {
421 } else if (e == "PPC_FP128") {
423 }
424 throw std::invalid_argument("Invalid primitive type");
425};
426
433constexpr size_t bit_width(PrimitiveType e) {
434 switch (e) {
436 return 0;
438 return 1;
440 return 8;
442 return 16;
444 return 32;
446 return 64;
448 return 128;
450 return 8;
452 return 16;
454 return 32;
456 return 64;
458 return 128;
460 return 16;
462 return 16;
464 return 32;
466 return 64;
468 return 80;
470 return 128;
472 return 128;
473 }
474 throw std::invalid_argument("Invalid primitive type");
475};
476
482constexpr bool is_floating_point(PrimitiveType e) noexcept {
483 switch (e) {
491 return true;
492 default:
493 return false;
494 }
495};
496
502constexpr bool is_integer(PrimitiveType e) noexcept {
503 switch (e) {
515 return true;
516 default:
517 return false;
518 }
519};
520
526constexpr bool is_signed(PrimitiveType e) noexcept {
527 switch (e) {
533 return true;
534 default:
535 return false;
536 }
537};
538
544constexpr bool is_unsigned(PrimitiveType e) noexcept {
545 switch (e) {
551 return true;
552 default:
553 return false;
554 }
555};
556
562constexpr PrimitiveType as_signed(PrimitiveType e) noexcept {
563 switch (e) {
565 return PrimitiveType::Int8;
574 default:
575 return e;
576 }
577};
578
585 switch (e) {
596 default:
597 return e;
598 }
599};
600
616
625class IType {
626protected:
628 size_t alignment_;
629 std::string initializer_;
630
631public:
640
642 virtual ~IType() = default;
643
648 virtual TypeID type_id() const = 0;
649
652 return storage_type_;
653 }
654
660 virtual bool is_pointer_like() const = 0;
661
664 return storage_type_;
665 };
666
671
673 size_t alignment() const {
674 return alignment_;
675 };
676
678 void alignment(size_t alignment) {
680 };
681
683 std::string initializer() const {
684 return initializer_;
685 };
686
688 void initializer(const std::string& initializer) {
690 };
691
701 virtual PrimitiveType primitive_type() const = 0;
702
711 virtual bool is_symbol() const = 0;
712
718 virtual bool operator==(const IType& other) const = 0;
719
724 virtual std::unique_ptr<IType> clone() const = 0;
725
730 virtual std::string print() const = 0;
731
738 friend std::ostream& operator<<(std::ostream& os, const IType& type) {
739 os << type.print();
740 return os;
741 };
742
750 virtual void replace_symbols(const symbolic::Expression old_expression, const symbolic::Expression new_expression) = 0;
751 virtual void replace_symbols(const symbolic::ExpressionMapping& replacements) = 0;
752};
753
754} // namespace types
755} // namespace sdfg
Represents a c-style array type.
Definition array.h:33
Represents a function type.
Definition function.h:31
virtual std::unique_ptr< IType > clone() const =0
Creates a deep copy of this type.
size_t alignment_
Memory alignment requirement in bytes.
Definition type.h:628
void storage_type(const StorageType &storage_type)
Sets the storage type.
Definition type.h:668
IType(StorageType storage_type=StorageType::CPU_Stack(), size_t alignment=0, const std::string &initializer="")
Constructs an IType with optional storage, alignment, and initializer.
Definition type.h:638
virtual void replace_symbols(const symbolic::ExpressionMapping &replacements)=0
virtual bool operator==(const IType &other) const =0
Compares two types for equality.
void alignment(size_t alignment)
Sets the alignment requirement in bytes.
Definition type.h:678
virtual TypeID type_id() const =0
Returns the TypeID for runtime type identification.
virtual PrimitiveType primitive_type() const =0
Returns the primitive type for this type.
std::string initializer_
Optional initializer expression.
Definition type.h:629
virtual bool is_pointer_like() const =0
Checks if this is a pointer or a structure behaves like a pointer.
StorageType storage_type_
Where and how the data is stored.
Definition type.h:627
virtual void replace_symbols(const symbolic::Expression old_expression, const symbolic::Expression new_expression)=0
Replace symbolic expressions on this type.
virtual bool is_symbol() const =0
Checks if this type represents a symbolic/integer type.
StorageType storage_type() const
Gets the storage type (const version).
Definition type.h:651
std::string initializer() const
Gets the initializer expression.
Definition type.h:683
virtual ~IType()=default
Virtual destructor for proper cleanup of derived classes.
friend std::ostream & operator<<(std::ostream &os, const IType &type)
Stream output operator for types.
Definition type.h:738
StorageType & storage_type()
Gets the storage type (mutable reference).
Definition type.h:663
void initializer(const std::string &initializer)
Sets the initializer expression.
Definition type.h:688
size_t alignment() const
Gets the alignment requirement in bytes.
Definition type.h:673
virtual std::string print() const =0
Returns a string representation of this type.
Represents a pointer type.
Definition pointer.h:32
Represents a scalar primitive type.
Definition scalar.h:27
Represents the storage location and management characteristics of data.
Definition type.h:64
StorageType(const std::string &value)
Constructs a StorageType with default unmanaged allocation.
Definition type.h:87
bool is_amd_generic() const
Checks if this storage is AMD generic memory.
Definition type.h:230
symbolic::Expression allocation_size() const
Gets the allocation size.
Definition type.h:118
void args(const std::vector< symbolic::Expression > &args)
Definition type.h:151
void arg1(const symbolic::Expression &arg)
Sets the additional argument.
Definition type.h:135
bool is_gpu_static() const
Checks if this storage is a static GPU buffer.
Definition type.h:262
static StorageType CPU_Stack()
Creates a CPU stack storage type.
Definition type.h:270
static StorageType NV_Generic()
Creates an NVIDIA generic memory storage type.
Definition type.h:298
bool is_nv_symbol() const
Checks if this storage is an NVIDIA symbol.
Definition type.h:250
static StorageType GPU_Static()
Creates a static GPU buffer storage type (see is_gpu_static).
Definition type.h:323
bool is_cpu_stack() const
Checks if this storage is CPU stack storage.
Definition type.h:210
std::string value() const
Gets the storage location identifier.
Definition type.h:108
const symbolic::Expression & arg1() const
Gets the additional argument.
Definition type.h:129
bool is_nv_global() const
Checks if this storage is NVIDIA global memory.
Definition type.h:235
const std::vector< symbolic::Expression > & args() const
Definition type.h:147
AllocationType
Defines how memory allocation/deallocation is managed.
Definition type.h:70
@ Managed
Memory is automatically managed.
Definition type.h:72
@ Unmanaged
Memory is not automatically managed.
Definition type.h:71
bool is_nv_shared() const
Checks if this storage is NVIDIA shared memory.
Definition type.h:240
void allocation_size(const symbolic::Expression &allocation_size)
Sets the allocation size.
Definition type.h:123
bool is_nv_generic() const
Checks if this storage is NVIDIA generic memory.
Definition type.h:225
std::vector< symbolic::Expression > & args()
Definition type.h:143
AllocationType deallocation() const
Gets the deallocation management type.
Definition type.h:170
static StorageType CPU_Heap(symbolic::Expression allocation_size, StorageType::AllocationType allocation, StorageType::AllocationType deallocation)
Creates a CPU heap storage type with explicit allocation management.
Definition type.h:289
void args(std::vector< symbolic::Expression > &&args)
Definition type.h:155
static StorageType NV_Shared()
Creates an NVIDIA shared memory storage type.
Definition type.h:308
static StorageType NV_Constant()
Creates an NVIDIA constant memory storage type.
Definition type.h:313
void value(const std::string &value)
Sets the storage location identifier.
Definition type.h:113
bool is_cpu_heap() const
Checks if this storage is CPU heap storage.
Definition type.h:215
void deallocation(AllocationType deallocation)
Sets the deallocation management type.
Definition type.h:175
StorageType(const std::string &value, const symbolic::Expression &allocation_size, AllocationType allocation, AllocationType deallocation)
Constructs a StorageType with explicit allocation management.
Definition type.h:98
static StorageType NV_Symbol()
Creates an NVIDIA symbol storage type.
Definition type.h:318
static StorageType NV_Global()
Creates an NVIDIA global memory storage type.
Definition type.h:303
bool is_nv_constant() const
Checks if this storage is NVIDIA constant memory.
Definition type.h:245
bool operator==(const StorageType &other) const
Compares two StorageType objects for equality.
Definition type.h:184
bool is_device() const
Checks if this storage resides on a device (i.e. not CPU stack/heap).
Definition type.h:220
AllocationType allocation() const
Gets the allocation management type.
Definition type.h:160
static StorageType CPU_Heap()
Creates a CPU heap storage type with default unmanaged allocation.
Definition type.h:278
void allocation(AllocationType allocation)
Sets the allocation management type.
Definition type.h:165
Represents a structure type (similar to C struct).
Definition structure.h:37
Definition tensor.h:13
bool null_safe_eq(const Expression lhs, const Expression rhs)
Tests equality of expressions with null-safety.
SymEngine::RCP< const SymEngine::Basic > Expression
Reference-counted pointer to a symbolic expression (SymEngine Basic).
Definition symbolic.h:83
SymEngine::map_basic_basic ExpressionMapping
Definition symbolic.h:109
nlohmann::json json
Definition interstate_edge.h:14
Definition symbolic.h:57
constexpr bool is_signed(PrimitiveType e) noexcept
Checks if a PrimitiveType is a signed integer type.
Definition type.h:526
constexpr size_t bit_width(PrimitiveType e)
Returns the bit width of a PrimitiveType.
Definition type.h:433
constexpr PrimitiveType as_unsigned(PrimitiveType e) noexcept
Converts a signed integer type to its unsigned equivalent.
Definition type.h:584
constexpr PrimitiveType as_signed(PrimitiveType e) noexcept
Converts an unsigned integer type to its signed equivalent.
Definition type.h:562
PrimitiveType
Enumeration of all primitive data types supported by sdfglib.
Definition type.h:35
@ Int32
32-bit signed integer
Definition type.h:40
@ UInt64
64-bit unsigned integer
Definition type.h:46
@ Bool
Boolean type (1 bit).
Definition type.h:37
@ Int16
16-bit signed integer
Definition type.h:39
@ UInt16
16-bit unsigned integer
Definition type.h:44
@ Half
16-bit floating-point (IEEE 754 half precision)
Definition type.h:48
@ FP128
128-bit floating-point (IEEE 754 quadruple precision)
Definition type.h:53
@ Double
64-bit floating-point (IEEE 754 double precision)
Definition type.h:51
@ Int64
64-bit signed integer
Definition type.h:41
@ UInt32
32-bit unsigned integer
Definition type.h:45
@ Int8
8-bit signed integer
Definition type.h:38
@ Int128
128-bit signed integer
Definition type.h:42
@ PPC_FP128
128-bit floating-point (PowerPC double-double)
Definition type.h:54
@ UInt8
8-bit unsigned integer
Definition type.h:43
@ Void
Void type (0 bits).
Definition type.h:36
@ BFloat
16-bit brain floating-point
Definition type.h:49
@ UInt128
128-bit unsigned integer
Definition type.h:47
@ X86_FP80
80-bit extended precision floating-point (x86)
Definition type.h:52
@ Float
32-bit floating-point (IEEE 754 single precision)
Definition type.h:50
constexpr bool is_integer(PrimitiveType e) noexcept
Checks if a PrimitiveType is an integer type.
Definition type.h:502
constexpr const char * primitive_type_to_string(PrimitiveType e)
Converts a PrimitiveType to its string representation.
Definition type.h:334
TypeID
Enumeration of high-level type categories.
Definition type.h:607
@ Reference
Reference type.
Definition type.h:612
constexpr bool is_unsigned(PrimitiveType e) noexcept
Checks if a PrimitiveType is an unsigned integer type.
Definition type.h:544
constexpr PrimitiveType primitive_type_from_string(std::string_view e)
Converts a string to its corresponding PrimitiveType.
Definition type.h:384
constexpr bool is_floating_point(PrimitiveType e) noexcept
Checks if a PrimitiveType is a floating-point type.
Definition type.h:482
Definition analysis.h:7
Symbolic system for integer-based symbol analysis.