Skip to content
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

Try to fix Undefined Behavior in DataLayouts #2034

Merged
merged 1 commit into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ ClimaCore.jl Release Notes
main
-------

- Fixed world-age issue on Julia 1.11 issue [Julia#54780](https://github.com/JuliaLang/julia/issues/54780), PR [#2034](https://github.com/CliMA/ClimaCore.jl/pull/2034).

v0.14.18
-------

Expand Down
25 changes: 17 additions & 8 deletions src/DataLayouts/DataLayouts.jl
Original file line number Diff line number Diff line change
Expand Up @@ -235,27 +235,29 @@ Base.similar(data::AbstractData{S}) where {S} = similar(data, S)
typesize(eltype(parent(data)), S)
end

@inline _getproperty(data::AbstractData, ::Val{Name}) where {Name} =
_getproperty(data, Val(Name), Name)

@generated function _getproperty(
data::AbstractData{S},
::Val{Name},
name,
) where {S, Name}
errorstring = "Invalid field name $(Name) for type $(S)"
i = findfirst(isequal(Name), fieldnames(S))
if i === nothing
return :(error($errorstring))
end
static_idx = Val{i}()
return :(Base.@_inline_meta; DataLayouts._property_view(data, $static_idx))
return :(
Base.@_inline_meta; DataLayouts._property_view(data, $static_idx, name)
)
end

@inline function Base.getproperty(data::AbstractData{S}, name::Symbol) where {S}
_getproperty(data, Val{name}())
_getproperty(data, Val{name}(), name)
end
@inline function Base.dotgetproperty(
data::AbstractData{S},
name::Symbol,
) where {S}
_getproperty(data, Val{name}())
_getproperty(data, Val{name}(), name)
end

Base.@propagate_inbounds function Base.getproperty(
Expand All @@ -275,11 +277,18 @@ Base.@propagate_inbounds function Base.getproperty(
union_all(data){SS, Base.tail(type_params(data))...}(dataview)
end

@noinline _property_view(
data::AbstractData{S},
::Val{Nothing},
name,
) where {S} = error("Invalid field name $name for type $(S)")

# In the past, we've sometimes needed a generated function
# for inference and constant propagation:
Base.@propagate_inbounds @generated function _property_view(
data::AD,
::Val{Idx},
name,
) where {S, Idx, AD <: AbstractData{S}}
SS = fieldtype(S, Idx)
T = eltype(parent_array_type(AD))
Expand Down Expand Up @@ -855,7 +864,7 @@ Base.lastindex(data::VF) = length(data)
nlevels(::VF{S, Nv}) where {S, Nv} = Nv

Base.@propagate_inbounds Base.getproperty(data::VF, i::Integer) =
_property_view(data, Val(i))
_property_view(data, Val(i), i)

Base.@propagate_inbounds column(data::VF, i, h) = column(data, i, 1, h)

Expand Down
Loading