Skip to content

Commit c3a63fc

Browse files
authored
Only emit incoming values for the phi when needed (#37285)
Fixes a crash when one of the incoming value for a union value phi node is of type `Union{}`. Incoming value of this type can be generated by try-catch. Fix #37265
1 parent f896006 commit c3a63fc

2 files changed

Lines changed: 44 additions & 5 deletions

File tree

src/codegen.cpp

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6836,19 +6836,25 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
68366836
}
68376837
else {
68386838
Value *RTindex;
6839-
Value *V;
6839+
// The branch below is a bit too complex for GCC to realize that
6840+
// `V` is always initialized when it is used.
6841+
// Ref https://gcc.gnu.org/bugzilla/show_bug.cgi?id=96629
6842+
Value *V = nullptr;
68406843
if (val.typ == (jl_value_t*)jl_bottom_type) {
6841-
V = undef_value_for_type(VN->getType());
6844+
if (VN)
6845+
V = undef_value_for_type(VN->getType());
68426846
RTindex = UndefValue::get(T_int8);
68436847
}
68446848
else if (jl_is_concrete_type(val.typ) || val.constant) {
68456849
size_t tindex = get_box_tindex((jl_datatype_t*)val.typ, phiType);
68466850
if (tindex == 0) {
6847-
V = boxed(ctx, val);
6851+
if (VN)
6852+
V = boxed(ctx, val);
68486853
RTindex = ConstantInt::get(T_int8, 0x80);
68496854
}
68506855
else {
6851-
V = ConstantPointerNull::get(cast<PointerType>(T_prjlvalue));
6856+
if (VN)
6857+
V = V_rnull;
68526858
Type *lty = julia_type_to_llvm(ctx, val.typ);
68536859
if (dest && !type_is_ghost(lty)) // basically, if !ghost union
68546860
emit_unbox(ctx, lty, val, val.typ, dest);
@@ -6868,7 +6874,8 @@ static std::pair<std::unique_ptr<Module>, jl_llvm_functions_t>
68686874
}
68696875
new_union.TIndex = RTindex;
68706876
}
6871-
V = new_union.Vboxed ? new_union.Vboxed : ConstantPointerNull::get(cast<PointerType>(T_prjlvalue));
6877+
if (VN)
6878+
V = new_union.Vboxed ? new_union.Vboxed : V_rnull;
68726879
if (dest) { // basically, if !ghost union
68736880
Value *skip = NULL;
68746881
if (new_union.Vboxed != nullptr)

test/core.jl

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7288,3 +7288,35 @@ function f37044(r)
72887288
end
72897289
r37044 = Ref37044(A37044{Int}.body)
72907290
@test f37044(r37044)[1] === Int
7291+
7292+
a37265() = 0
7293+
b37265() = 0
7294+
function c37265(d)
7295+
if d == 1
7296+
e = a37265
7297+
elseif d == 2
7298+
e = b37265
7299+
else
7300+
try
7301+
catch
7302+
end
7303+
end
7304+
e
7305+
end
7306+
@test_throws UndefVarError c37265(0)
7307+
@test c37265(1) === a37265
7308+
@test c37265(2) === b37265
7309+
7310+
function c37265_2(d)
7311+
if 0
7312+
e = a37265
7313+
elseif 0
7314+
e = b37265
7315+
else
7316+
try
7317+
catch
7318+
end
7319+
end
7320+
e
7321+
end
7322+
@test_throws TypeError c37265_2(0)

0 commit comments

Comments
 (0)