Skip to content

allocations when performing no-op convert on struct instance with Union fieldtype #28087

@jrevels

Description

@jrevels

There might be a better MWE for this, I'm not sure why it happens (codegen perf bug, I'm guessing?):

julia> using BenchmarkTools

julia> struct FooA{X}
           x::X
       end

julia> F, f = FooA{Int}, FooA{Int}(1)
(FooA{Int64}, FooA{Int64}(1))

julia> @btime convert($F, $f)
  0.035 ns (0 allocations: 0 bytes)
FooA{Int64}(1)

julia> @code_lowered convert(F, f)
CodeInfo(
123 1return %%x                                                                                            │
)

julia> @code_llvm convert(F, f)

; Function convert
; Location: essentials.jl:123
define { i64 } @julia_convert_32884(%jl_value_t addrspace(10)*, { i64 } addrspace(11)* nocapture nonnull readonly dereferenceable(8)) {
top:
  %2 = getelementptr inbounds { i64 }, { i64 } addrspace(11)* %1, i64 0, i32 0
  %.unpack = load i64, i64 addrspace(11)* %2, align 8
  %3 = insertvalue { i64 } undef, i64 %.unpack, 0
  ret { i64 } %3
}

julia> struct FooB{X}
           x::Union{X,Nothing}
       end

julia> F, f = FooB{Int}, FooB{Int}(1)
(FooB{Int64}, FooB{Int64}(1))

julia> @btime convert($F, $f)
  56.547 ns (2 allocations: 64 bytes)
FooB{Int64}(1)

julia> @code_lowered convert(F, f)
CodeInfo(
123 1return %%x                                                                                            │
)

julia> @code_llvm convert(F, f)

; Function convert
; Location: essentials.jl:123
define void @julia_convert_33024({ i64, i8 }* noalias nocapture sret, %jl_value_t addrspace(10)*, { i64, i8 } addrspace(11)* nocapture nonnull readonly dereferenceable(16)) {
top:
  %3 = bitcast { i64, i8 }* %0 to i8*
  %4 = bitcast { i64, i8 } addrspace(11)* %2 to i8 addrspace(11)*
  call void @llvm.memcpy.p0i8.p11i8.i64(i8* %3, i8 addrspace(11)* %4, i64 16, i32 8, i1 false)
  ret void
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions