|
| 1 | +const CompositeIndex = Vector{Int} |
| 2 | + |
| 3 | +struct ArgumentMap |
| 4 | + variables::Vector{CompositeIndex} # index into argument tuple type |
| 5 | + equations::Vector{CompositeIndex} # index into argument tuple type |
| 6 | +end |
| 7 | +ArgumentMap() = ArgumentMap(CompositeIndex[], CompositeIndex[]) |
| 8 | + |
| 9 | +function ArgumentMap(argtypes::Vector{Any}) |
| 10 | + map = ArgumentMap() |
| 11 | + index = CompositeIndex() |
| 12 | + fill_argument_map!(map, index, argtypes) |
| 13 | + return map |
| 14 | +end |
| 15 | + |
| 16 | +function fill_argument_map!(map::ArgumentMap, index::CompositeIndex, types::Vector{Any}) |
| 17 | + for (i, type) in enumerate(types) |
| 18 | + push!(index, i) |
| 19 | + fill_argument_map!(map, index, type) |
| 20 | + pop!(index) |
| 21 | + end |
| 22 | +end |
| 23 | + |
| 24 | +function fill_argument_map!(map::ArgumentMap, index::CompositeIndex, @nospecialize(type)) |
| 25 | + if isprimitivetype(type) || isa(type, Incidence) |
| 26 | + push!(map.variables, copy(index)) |
| 27 | + elseif type === equation |
| 28 | + push!(map.equations, copy(index)) |
| 29 | + elseif isa(type, PartialStruct) || isstructtype(type) |
| 30 | + fields = isa(type, PartialStruct) ? type.fields : collect(Any, fieldtypes(type)) |
| 31 | + fill_argument_map!(map, index, fields) |
| 32 | + end |
| 33 | +end |
| 34 | + |
| 35 | +struct FlatteningState |
| 36 | + compact::IncrementalCompact |
| 37 | + settings::Settings |
| 38 | + map::ArgumentMap |
| 39 | + nvariables::Int |
| 40 | + nequations::Int |
| 41 | + new_argtypes::Vector{Any} |
| 42 | +end |
| 43 | + |
| 44 | +function FlatteningState(compact::IncrementalCompact, settings::Settings, map::ArgumentMap) |
| 45 | + FlatteningState(compact, settings, deepcopy(map), length(map.variables), length(map.equations), Any[]) |
| 46 | +end |
| 47 | + |
| 48 | +function next_variable!(state::FlatteningState) |
| 49 | + popfirst!(state.map.variables) |
| 50 | + return state.nvariables - length(state.map.variables) |
| 51 | +end |
| 52 | + |
| 53 | +function next_equation!(state::FlatteningState) |
| 54 | + popfirst!(state.map.equations) |
| 55 | + return state.nequations - length(state.map.equations) |
| 56 | +end |
| 57 | + |
| 58 | +function flatten_arguments!(state::FlatteningState, argtypes::Vector{Any}) |
| 59 | + args = Any[] |
| 60 | + # push!(state.new_argtypes, argtypes[1]) |
| 61 | + for argt in argtypes |
| 62 | + arg = flatten_argument!(state, argt) |
| 63 | + arg === nothing && return nothing |
| 64 | + push!(args, arg) |
| 65 | + end |
| 66 | + @assert isempty(state.map.variables) |
| 67 | + @assert isempty(state.map.equations) |
| 68 | + return args |
| 69 | +end |
| 70 | + |
| 71 | +function flatten_argument!(state::FlatteningState, @nospecialize(argt)) |
| 72 | + @assert !isa(argt, Incidence) && !isa(argt, Eq) |
| 73 | + (; compact, settings) = state |
| 74 | + if isa(argt, Const) |
| 75 | + return argt.val |
| 76 | + elseif Base.issingletontype(argt) |
| 77 | + return argt.instance |
| 78 | + elseif isprimitivetype(argt) |
| 79 | + push!(state.new_argtypes, argt) |
| 80 | + return Argument(next_variable!(state)) |
| 81 | + elseif argt === equation |
| 82 | + eq = next_equation!(state) |
| 83 | + line = compact[Compiler.OldSSAValue(1)][:line] |
| 84 | + ssa = @insert_instruction_here(compact, line, settings, (:invoke)(nothing, InternalIntrinsics.external_equation)::Eq(eq)) |
| 85 | + return ssa |
| 86 | + elseif isabstracttype(argt) || ismutabletype(argt) || (!isa(argt, DataType) && !isa(argt, PartialStruct)) |
| 87 | + line = compact[Compiler.OldSSAValue(1)][:line] |
| 88 | + ssa = @insert_instruction_here(compact, line, settings, error("Cannot IPO model arg type $argt")::Union{}) |
| 89 | + return nothing |
| 90 | + else |
| 91 | + if !isa(argt, PartialStruct) && Base.datatype_fieldcount(argt) === nothing |
| 92 | + line = compact[Compiler.OldSSAValue(1)][:line] |
| 93 | + ssa = @insert_instruction_here(compact, line, settings, error("Cannot IPO model arg type $argt")::Union{}) |
| 94 | + return nothing |
| 95 | + end |
| 96 | + fields = isa(argt, PartialStruct) ? argt.fields : collect(Any, fieldtypes(argt)) |
| 97 | + args = flatten_arguments!(state, fields) |
| 98 | + args === nothing && return nothing |
| 99 | + this = Expr(:new, isa(argt, PartialStruct) ? argt.typ : argt, args...) |
| 100 | + line = compact[Compiler.OldSSAValue(1)][:line] |
| 101 | + ssa = @insert_instruction_here(compact, line, settings, this::argt) |
| 102 | + return ssa |
| 103 | + end |
| 104 | +end |
| 105 | + |
| 106 | +function flatten_arguments_for_callee!(compact::IncrementalCompact, map::ArgumentMap, argtypes, 𝕃, line, settings) |
| 107 | + list = Any[] |
| 108 | + this = nothing |
| 109 | + last_index = Int[] |
| 110 | + for index in map.variables |
| 111 | + from = findfirst(j -> get(last_index, j, -1) !== index[j], eachindex(index))::Int |
| 112 | + for i in from:length(index) |
| 113 | + field = index[i] |
| 114 | + if i == 1 |
| 115 | + this = Argument(2 + field) |
| 116 | + else |
| 117 | + thistype = argextype(this, compact) |
| 118 | + fieldtype = Compiler.getfield_tfunc(𝕃, Const(field)) |
| 119 | + this = @insert_instruction_here(compact, line, settings, getfield(this, field)::fieldtype) |
| 120 | + end |
| 121 | + end |
| 122 | + push!(list, this) |
| 123 | + end |
| 124 | + return list |
| 125 | +end |
| 126 | + |
1 | 127 | function _flatten_parameter!(𝕃, compact, argtypes, ntharg, line, settings) |
2 | 128 | list = Any[] |
3 | 129 | for (argn, argt) in enumerate(argtypes) |
@@ -67,51 +193,55 @@ function process_template!(𝕃, coeffs, eq_mapping, applied_scopes, argtypes, t |
67 | 193 | return Pair{Int, Int}(offset, eqoffset) |
68 | 194 | end |
69 | 195 |
|
70 | | -struct TransformedArg |
71 | | - ssa::Any |
72 | | - offset::Int |
73 | | - eqoffset::Int |
74 | | - TransformedArg(@nospecialize(arg), new_offset::Int, new_eqoffset::Int) = new(arg, new_offset, new_eqoffset) |
75 | | -end |
76 | 196 |
|
77 | | -function flatten_argument!(compact::Compiler.IncrementalCompact, settings::Settings, @nospecialize(argt), offset::Int, eqoffset::Int, argtypes::Vector{Any})::TransformedArg |
78 | | - @assert !isa(argt, Incidence) && !isa(argt, Eq) |
79 | | - if isa(argt, Const) |
80 | | - return TransformedArg(argt.val, offset, eqoffset) |
81 | | - elseif Base.issingletontype(argt) |
82 | | - return TransformedArg(argt.instance, offset, eqoffset) |
83 | | - elseif Base.isprimitivetype(argt) |
84 | | - push!(argtypes, argt) |
85 | | - return TransformedArg(Argument(offset+1), offset+1, eqoffset) |
86 | | - elseif argt === equation |
87 | | - line = compact[Compiler.OldSSAValue(1)][:line] |
88 | | - ssa = @insert_instruction_here(compact, line, settings, (:invoke)(nothing, InternalIntrinsics.external_equation)::Eq(eqoffset+1)) |
89 | | - return TransformedArg(ssa, offset, eqoffset+1) |
90 | | - elseif isabstracttype(argt) || ismutabletype(argt) || (!isa(argt, DataType) && !isa(argt, PartialStruct)) |
91 | | - line = compact[Compiler.OldSSAValue(1)][:line] |
92 | | - ssa = @insert_instruction_here(compact, line, settings, error("Cannot IPO model arg type $argt")::Union{}) |
93 | | - return TransformedArg(ssa, -1, eqoffset) |
94 | | - else |
95 | | - if !isa(argt, PartialStruct) && Base.datatype_fieldcount(argt) === nothing |
96 | | - line = compact[Compiler.OldSSAValue(1)][:line] |
97 | | - ssa = @insert_instruction_here(compact, line, settings, error("Cannot IPO model arg type $argt")::Union{}) |
98 | | - return TransformedArg(ssa, -1, eqoffset) |
| 197 | +remove_variable_and_equation_annotations(argtypes) = Any[widenconst(T) for T in argtypes] |
| 198 | + |
| 199 | +function annotate_variables_and_equations(argtypes::Vector{Any}, map::ArgumentMap) |
| 200 | + argtypes_annotated = Any[] |
| 201 | + pstructs = Dict{CompositeIndex,PartialStruct}() |
| 202 | + for (i, arg) in enumerate(argtypes) |
| 203 | + if arg !== equation && arg !== Incidence && isstructtype(arg) && (any(==(i) ∘ first, map.variables) || any(==(i) ∘ first, map.equations)) |
| 204 | + arg = init_partialstruct(arg) |
| 205 | + pstructs[[i]] = arg |
99 | 206 | end |
100 | | - (args, _, offset) = flatten_arguments!(compact, settings, isa(argt, PartialStruct) ? argt.fields : collect(Any, fieldtypes(argt)), offset, eqoffset, argtypes) |
101 | | - offset == -1 && return TransformedArg(ssa, -1, eqoffset) |
102 | | - this = Expr(:new, isa(argt, PartialStruct) ? argt.typ : argt, args...) |
103 | | - line = compact[Compiler.OldSSAValue(1)][:line] |
104 | | - ssa = @insert_instruction_here(compact, line, settings, this::argt) |
105 | | - return TransformedArg(ssa, offset, eqoffset) |
| 207 | + push!(argtypes_annotated, arg) |
106 | 208 | end |
| 209 | + |
| 210 | + function fields_for_index(index) |
| 211 | + length(index) > 1 || return argtypes_annotated |
| 212 | + # Find the parent `PartialStruct` that holds the variable field, |
| 213 | + # creating any further `PartialStruct` going down if necessary. |
| 214 | + i, base = find_base(pstructs, index) |
| 215 | + local fields = base.fields |
| 216 | + for j in @view index[(i + 1):(end - 1)] |
| 217 | + pstruct = init_partialstruct(fields[j]) |
| 218 | + fields[j] = pstruct |
| 219 | + fields = pstruct.fields |
| 220 | + end |
| 221 | + return fields |
| 222 | + end |
| 223 | + |
| 224 | + # Populate `PartialStruct` variable fields with an `Incidence` lattice element. |
| 225 | + for (variable, index) in enumerate(map.variables) |
| 226 | + fields = fields_for_index(index) |
| 227 | + type = get_fieldtype(argtypes, index) |
| 228 | + fields[index[end]] = Incidence(type, variable) |
| 229 | + end |
| 230 | + |
| 231 | + # Do the same for equations with an `Eq` lattice element. |
| 232 | + for (equation, index) in enumerate(map.equations) |
| 233 | + fields = fields_for_index(index) |
| 234 | + fields[index[end]] = Eq(equation) |
| 235 | + end |
| 236 | + |
| 237 | + return argtypes_annotated |
107 | 238 | end |
108 | 239 |
|
109 | | -function flatten_arguments!(compact::Compiler.IncrementalCompact, settings::Settings, argtypes::Vector{Any}, offset::Int=0, eqoffset::Int=0, new_argtypes::Vector{Any} = Any[]) |
110 | | - args = Any[] |
111 | | - for argt in argtypes |
112 | | - (; ssa, offset, eqoffset) = flatten_argument!(compact, settings, argt, offset, eqoffset, new_argtypes) |
113 | | - offset == -1 && break |
114 | | - push!(args, ssa) |
| 240 | +init_partialstruct(@nospecialize(T)) = PartialStruct(T, collect(Any, fieldtypes(T))) |
| 241 | + |
| 242 | +function find_base(dict::Dict{CompositeIndex}, index::CompositeIndex) |
| 243 | + for i in reverse(eachindex(index)) |
| 244 | + base = get(dict, @view(index[1:i]), nothing) |
| 245 | + base !== nothing && return i, base |
115 | 246 | end |
116 | | - return (args, new_argtypes, offset, eqoffset) |
117 | 247 | end |
0 commit comments