From 06134f618f9add361a590099a448c8b6494151ff Mon Sep 17 00:00:00 2001 From: Gabriel Baraldi Date: Thu, 25 Apr 2024 09:29:35 -0300 Subject: [PATCH] Utilize correct tbaa when emitting stores of unions. (#54222) Also remove unused argument from union_store (cherry picked from commit 37f848c79f16d85aa680b8e04c026cbb64f2b1ae) --- src/cgutils.cpp | 6 +++--- src/codegen.cpp | 2 +- test/compiler/codegen.jl | 14 ++++++++++++++ 3 files changed, 18 insertions(+), 4 deletions(-) diff --git a/src/cgutils.cpp b/src/cgutils.cpp index 007e4c5936a4f..8b539ce7dc016 100644 --- a/src/cgutils.cpp +++ b/src/cgutils.cpp @@ -3625,7 +3625,7 @@ static void emit_write_multibarrier(jl_codectx_t &ctx, Value *parent, Value *agg static jl_cgval_t union_store(jl_codectx_t &ctx, Value *ptr, Value *ptindex, jl_cgval_t rhs, jl_cgval_t cmp, - jl_value_t *jltype, MDNode *tbaa, MDNode *aliasscope, MDNode *tbaa_tindex, + jl_value_t *jltype, MDNode *tbaa, MDNode *tbaa_tindex, AtomicOrdering Order, AtomicOrdering FailOrder, Value *needlock, bool issetfield, bool isreplacefield, bool isswapfield, bool ismodifyfield, bool issetfieldonce, const jl_cgval_t *modifyop, const Twine &fname) @@ -3683,7 +3683,7 @@ static jl_cgval_t union_store(jl_codectx_t &ctx, } Value *tindex = compute_tindex_unboxed(ctx, rhs_union, jltype); tindex = ctx.builder.CreateNUWSub(tindex, ConstantInt::get(getInt8Ty(ctx.builder.getContext()), 1)); - jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, ctx.tbaa().tbaa_unionselbyte); + jl_aliasinfo_t ai = jl_aliasinfo_t::fromTBAA(ctx, tbaa_tindex); ai.decorateInst(ctx.builder.CreateAlignedStore(tindex, ptindex, Align(1))); // copy data if (!rhs.isghost) { @@ -3739,7 +3739,7 @@ static jl_cgval_t emit_setfield(jl_codectx_t &ctx, emit_bitcast(ctx, addr, getInt8PtrTy(ctx.builder.getContext())), ConstantInt::get(ctx.types().T_size, fsz1)); setNameWithField(ctx.emission_context, ptindex, get_objname, sty, idx0, Twine(".tindex_ptr")); - return union_store(ctx, addr, ptindex, rhs, cmp, jfty, tbaa, nullptr, ctx.tbaa().tbaa_unionselbyte, + return union_store(ctx, addr, ptindex, rhs, cmp, jfty, tbaa, ctx.tbaa().tbaa_unionselbyte, Order, FailOrder, needlock, issetfield, isreplacefield, isswapfield, ismodifyfield, issetfieldonce, modifyop, fname); diff --git a/src/codegen.cpp b/src/codegen.cpp index cdd847c38901c..bc5e30233db01 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -3870,7 +3870,7 @@ static bool emit_f_opmemory(jl_codectx_t &ctx, jl_cgval_t *ret, jl_value_t *f, ptindex = emit_bitcast(ctx, ptindex, getInt8PtrTy(ctx.builder.getContext())); ptindex = ctx.builder.CreateInBoundsGEP(getInt8Ty(ctx.builder.getContext()), ptindex, idx0); *ret = union_store(ctx, data, ptindex, val, cmp, ety, - ctx.tbaa().tbaa_arraybuf, nullptr, ctx.tbaa().tbaa_arrayselbyte, + ctx.tbaa().tbaa_arraybuf, ctx.tbaa().tbaa_arrayselbyte, Order, FailOrder, nullptr, issetmemory, isreplacememory, isswapmemory, ismodifymemory, issetmemoryonce, modifyop, fname); diff --git a/test/compiler/codegen.jl b/test/compiler/codegen.jl index 805e5c7acc817..a95ef3225c3f3 100644 --- a/test/compiler/codegen.jl +++ b/test/compiler/codegen.jl @@ -873,3 +873,17 @@ if Sys.ARCH === :x86_64 end end end + +#Check if we aren't emitting the store with the wrong TBAA metadata + +foo54166(x,i,y) = x[i] = y +let io = IOBuffer() + code_llvm(io,foo54166, (Vector{Union{Missing,Int}}, Int, Int), dump_module=true, raw=true) + str = String(take!(io)) + @test !occursin("jtbaa_unionselbyte", str) + @test occursin("jtbaa_arrayselbyte", str) +end + +ex54166 = Union{Missing, Int64}[missing -2; missing -2]; +dims54166 = (1,2) +@test (minimum(ex54166; dims=dims54166)[1] === missing)