Skip to content

Commit

Permalink
Allow .V to be null in emit_unionmove (JuliaLang#28248)
Browse files Browse the repository at this point in the history
We generally handle the case where the .V of a split union is as small
as the active element of the union. If the active element happens to be
a ghost is thus seems reasonable for the split union to not have any storage
at all. Such a union is generated e.g. if we widen from an all-ghost split
union to one that includes sized elements, e.g. in the following example:
```
function foo()
    x = (1, 2)
    if rand() < 0.5
        if rand() < 0.5
            y = nothing
        else
            y = missing
        end
        x = y
    end
    x
end
```

Fixes JuliaLang#28208
  • Loading branch information
Keno committed Jul 24, 2018
1 parent 69014c8 commit 978577f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/cgutils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2289,7 +2289,8 @@ static void emit_unionmove(jl_codectx_t &ctx, Value *dest, MDNode *tbaa_dst, con
Value *tindex = ctx.builder.CreateAnd(src.TIndex, ConstantInt::get(T_int8, 0x7f));
if (skip)
tindex = ctx.builder.CreateSelect(skip, ConstantInt::get(T_int8, 0), tindex);
Value *src_ptr = maybe_bitcast(ctx, data_pointer(ctx, src), T_pint8);
Value *src_ptr = data_pointer(ctx, src);
src_ptr = src_ptr ? maybe_bitcast(ctx, src_ptr, T_pint8) : src_ptr;
dest = maybe_bitcast(ctx, dest, T_pint8);
BasicBlock *defaultBB = BasicBlock::Create(jl_LLVMContext, "union_move_skip", ctx.f);
SwitchInst *switchInst = ctx.builder.CreateSwitch(tindex, defaultBB);
Expand All @@ -2302,8 +2303,18 @@ static void emit_unionmove(jl_codectx_t &ctx, Value *dest, MDNode *tbaa_dst, con
BasicBlock *tempBB = BasicBlock::Create(jl_LLVMContext, "union_move", ctx.f);
ctx.builder.SetInsertPoint(tempBB);
switchInst->addCase(ConstantInt::get(T_int8, idx), tempBB);
if (nb > 0)
emit_memcpy(ctx, dest, tbaa_dst, src_ptr, src.tbaa, nb, alignment, isVolatile);
if (nb > 0) {
if (!src_ptr) {
Function *trap_func =
Intrinsic::getDeclaration(ctx.f->getParent(), Intrinsic::trap);
ctx.builder.CreateCall(trap_func);
ctx.builder.CreateUnreachable();
return;
} else {
emit_memcpy(ctx, dest, tbaa_dst, src_ptr,
src.tbaa, nb, alignment, isVolatile);
}
}
ctx.builder.CreateBr(postBB);
},
src.typ,
Expand Down
17 changes: 17 additions & 0 deletions test/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6653,3 +6653,20 @@ function foo28224()
return z
end
@test foo28224() == 5

# Issue #28208
@noinline function foo28208(a::Bool, b::Bool)
x = (1, 2)
if a
if b
y = nothing
else
y = missing
end
x = y
end
x
end
@test isa(foo28208(false, true), Tuple)
@test foo28208(true, false) === missing
@test foo28208(true, true) === nothing

0 comments on commit 978577f

Please sign in to comment.