Skip to content

Commit

Permalink
Merge pull request #2034 from CliMA/ck/attempt_to_fix_UB
Browse files Browse the repository at this point in the history
Try to fix Undefined Behavior in DataLayouts
  • Loading branch information
charleskawczynski authored Oct 11, 2024
2 parents 49d7e80 + e5f1dac commit 5f096c2
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 8 deletions.
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

0 comments on commit 5f096c2

Please sign in to comment.