[API][v0.3] Introduce Struct Data Type to HeteroCL #157
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
In this PR, we introduce the C-like struct to HeteroCL.
Python Interface
Users can define a HeteroCL type by using
hcl.struct
with a Python dictionary. The following shows an example.Note that currently, we only support single-level structs. Namely, we cannot have a struct inside another struct. Since HeteroCL does not support pointers intrinsically, we will not have pointer members or self-referential structs.
Users can print the struct simply using Python
print
function. The following are some examples of getting information of a struct.With the support of a struct, we can create tensors having struct type. For example,
To access the elements, we provide C-like semantics (i.e., by using
.
). For example,We can also initialize a tensor with struct type by using tuples. For example,
Alternatively, we can use the imperative DSL provided by HeteroCL. For example,
More examples can be seen in the tests.
CPU Simulation (LLVM JIT)
The struct can be run with CPU simulation. This is realized by using LLVM intrinsic
bitcast
. Note that forbitcast
to work, we need to make sure the bitwidths are the same before and after typecast. Currently, this is guaranteed by the HeteroCL compiler. Thus, users only need to consider this requirement when they want to call the intrinsic directly from Python.The struct type is treated as "unsigned int" for all internal representation.
HLS C Code Generation
For code generation, we consider two different conditions. First, if floating-point numbers are involved, we use the C++ union. Here is an example of a generated code, where A is a floating-point tensor while B is a struct with a floating-point field.
If floating-point is not involved, we simply use the bit-selection provided by the HLS tool. Here, A is a tensor with type
ap_fixed<8, 5>
while B is a struct type.