-
Notifications
You must be signed in to change notification settings - Fork 154
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add struct tracing #1045
Merged
Merged
Add struct tracing #1045
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
const TypeT = UInt32 | ||
const ISINTEGER = TypeT(0) | ||
const SIGNED_OFFSET = TypeT(1) | ||
const SIZE_OFFSET = TypeT(2) | ||
|
||
const EMPTY_DIMS = Int[] | ||
|
||
struct StructElement | ||
typ::TypeT | ||
name::Symbol | ||
size::Vector{Int} | ||
function StructElement(::Type{T}, name, size = EMPTY_DIMS) where {T} | ||
c = encodetyp(T) | ||
c == typemax(TypeT) && error("Cannot handle type $T") | ||
new(c, name, size) | ||
end | ||
end | ||
|
||
_sizeofrepr(typ::TypeT) = typ >> SIZE_OFFSET | ||
sizeofrepr(s::StructElement) = _sizeofrepr(s.typ) | ||
Base.size(s::StructElement) = s.size | ||
Base.length(s::StructElement) = prod(size(s)) | ||
Base.nameof(s::StructElement) = s.name | ||
function Base.show(io::IO, s::StructElement) | ||
print(io, nameof(s), "::", decodetyp(s.typ)) | ||
if length(s) > 1 | ||
print(io, "::(", join(size(s), " × "), ")") | ||
end | ||
end | ||
|
||
function encodetyp(::Type{T}) where {T} | ||
typ = zero(UInt32) | ||
if T <: Integer | ||
typ |= TypeT(1) << ISINTEGER | ||
if T <: Signed | ||
typ |= TypeT(1) << SIGNED_OFFSET | ||
elseif !(T <: Unsigned) | ||
return typemax(TypeT) | ||
end | ||
elseif !(T <: AbstractFloat) | ||
return typemax(TypeT) | ||
end | ||
typ |= TypeT(sizeof(T)) << SIZE_OFFSET | ||
end | ||
|
||
function decodetyp(typ::TypeT) | ||
siz = TypeT(8) * (typ >> SIZE_OFFSET) | ||
if !iszero(typ & (TypeT(1) << ISINTEGER)) | ||
if !iszero(typ & TypeT(1) << SIGNED_OFFSET) | ||
siz == 8 ? Int8 : | ||
siz == 16 ? Int16 : | ||
siz == 32 ? Int32 : | ||
siz == 64 ? Int64 : | ||
error("invalid type $(typ)!") | ||
else # unsigned | ||
siz == 8 ? UInt8 : | ||
siz == 16 ? UInt16 : | ||
siz == 32 ? UInt32 : | ||
siz == 64 ? UInt64 : | ||
error("invalid type $(typ)!") | ||
end | ||
else # float | ||
siz == 16 ? Float16 : | ||
siz == 32 ? Float32 : | ||
siz == 64 ? Float64 : | ||
error("invalid type $(typ)!") | ||
end | ||
end | ||
|
||
struct Struct | ||
v::Vector{StructElement} | ||
end | ||
|
||
function Base.getproperty(s::Struct, name::Symbol) | ||
v = getfield(s, :v) | ||
idx = findfirst(x -> nameof(x) == name, v) | ||
idx === nothing && error("no field $name in struct") | ||
SymbolicUtils.term(getfield, s, idx, type = Real) | ||
end | ||
|
||
function Base.setproperty!(s::Struct, name::Symbol, x) | ||
v = getfield(s, :v) | ||
idx = findfirst(x -> nameof(x) == name, v) | ||
idx === nothing && error("no field $name in struct") | ||
type = SymbolicUtils.symtype(x) | ||
SymbolicUtils.term(setfield!, s, idx, x; type) | ||
end | ||
|
||
# We cannot precisely derive the type after `getfield` due to SU limitations, | ||
# so give up and just say Real. | ||
SymbolicUtils.promote_symtype(::typeof(getfield), ::Type{<:Struct}, _...) = Real | ||
SymbolicUtils.promote_symtype(::typeof(setfield!), ::Type{<:Struct}, _, ::Type{T}) where T = T |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
using Test, Symbolics | ||
using Symbolics: StructElement, Struct, operation, arguments | ||
|
||
handledtypes = [Int8, | ||
Int16, | ||
Int32, | ||
Int64, | ||
UInt8, | ||
UInt16, | ||
UInt32, | ||
UInt64, | ||
Float16, | ||
Float32, | ||
Float64] | ||
for t in handledtypes | ||
@test Symbolics.decodetyp(Symbolics.encodetyp(t)) === t | ||
end | ||
|
||
@variables t x(t) | ||
a = StructElement(Int8, :a) | ||
b = StructElement(Int, :b) | ||
s = Struct([a, b]) | ||
sa = s.a | ||
sb = s.b | ||
@test operation(sa) === getfield | ||
@test arguments(sa) == Any[s, 1] | ||
@test arguments(sa) isa Any | ||
@test operation(sb) === getfield | ||
@test arguments(sb) == Any[s, 2] | ||
@test arguments(sb) isa Any | ||
|
||
sa1 = (setproperty!(s, :a, UInt8(1))) | ||
@test operation(sa1) === setfield! | ||
@test arguments(sa1) == Any[s, 1, UInt8(1)] | ||
@test arguments(sa1) isa Any | ||
|
||
sb1 = (setproperty!(s, :b, "hi")) | ||
@test operation(sb1) === setfield! | ||
@test arguments(sb1) == Any[s, 2, "hi"] | ||
@test arguments(sb1) isa Any |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
shouldn't this error? Or what's the purpose of assigning a string to the Int field here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Type checking is compiler's responsibility. We don't need to check it on the frontend level.