Skip to content

Commit be049fa

Browse files
maleadtKristofferC
authored andcommitted
Handle union of immutables in codegen of GC.@preserve (#39520)
(cherry picked from commit fb0287f)
1 parent 74cf90d commit be049fa

File tree

2 files changed

+30
-1
lines changed

2 files changed

+30
-1
lines changed

src/codegen.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4542,7 +4542,7 @@ static jl_cgval_t emit_expr(jl_codectx_t &ctx, jl_value_t *expr, ssize_t ssaval)
45424542
if (ai.isboxed) {
45434543
vals.push_back(ai.Vboxed);
45444544
}
4545-
else if (!jl_is_pointerfree(ai.typ)) {
4545+
else if (jl_is_concrete_immutable(ai.typ) && !jl_is_pointerfree(ai.typ)) {
45464546
Type *at = julia_type_to_llvm(ctx, ai.typ);
45474547
vals.push_back(emit_unbox(ctx, at, ai, ai.typ));
45484548
}

test/compiler/codegen.jl

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,3 +522,32 @@ function f39232(a)
522522
return z
523523
end
524524
@test f39232((+, -)) == Any[+, -]
525+
526+
@testset "GC.@preserve" begin
527+
# main use case
528+
function f1(cond)
529+
val = [1]
530+
GC.@preserve val begin end
531+
end
532+
@test occursin("llvm.julia.gc_preserve_begin", get_llvm(f1, Tuple{Bool}, true, false, false))
533+
534+
# stack allocated objects (JuliaLang/julia#34241)
535+
function f3(cond)
536+
val = ([1],)
537+
GC.@preserve val begin end
538+
end
539+
@test occursin("llvm.julia.gc_preserve_begin", get_llvm(f3, Tuple{Bool}, true, false, false))
540+
541+
# unions of immutables (JuliaLang/julia#39501)
542+
function f2(cond)
543+
val = cond ? 1 : 1f0
544+
GC.@preserve val begin end
545+
end
546+
@test !occursin("llvm.julia.gc_preserve_begin", get_llvm(f2, Tuple{Bool}, true, false, false))
547+
# make sure the fix for the above doesn't regress #34241
548+
function f4(cond)
549+
val = cond ? ([1],) : ([1f0],)
550+
GC.@preserve val begin end
551+
end
552+
@test occursin("llvm.julia.gc_preserve_begin", get_llvm(f4, Tuple{Bool}, true, false, false))
553+
end

0 commit comments

Comments
 (0)