From e84eecd2e35408cb83c2ae191d88e0d8ff48b9eb Mon Sep 17 00:00:00 2001 From: Jameson Nash Date: Sat, 14 Sep 2024 13:10:43 +0000 Subject: [PATCH] fix SROA --- src/codegen.cpp | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/src/codegen.cpp b/src/codegen.cpp index 7190f9db81b74..4677fd8c189f1 100644 --- a/src/codegen.cpp +++ b/src/codegen.cpp @@ -2225,9 +2225,13 @@ static AllocaInst *emit_static_alloca(jl_codectx_t &ctx, Type *lty, Align align) static AllocaInst *emit_static_alloca(jl_codectx_t &ctx, unsigned nb, Align align) { - if (nb == 1 && align == Align(1)) - return emit_static_alloca(ctx, ctx.builder.getInt8Ty(), align); - return emit_static_alloca(ctx, ArrayType::get(ctx.builder.getInt8Ty(), alignTo(nb, align)), align); + // Stupid hack: SROA takes hints from the element type, and will happily split this allocation into lots of unaligned bits + // if it cannot find something better to do, which is terrible for performance. + // However, if we emit this with an element size equal to the alignment, it will instead split it into aligned chunks + // which is great for performance and vectorization. + if (alignTo(nb, align) == align.value()) // don't bother with making an array of length 1 + return emit_static_alloca(ctx, ctx.builder.getIntNTy(align.value() * 8), align); + return emit_static_alloca(ctx, ArrayType::get(ctx.builder.getIntNTy(align.value() * 8), alignTo(nb, align) / align.value()), align); } static AllocaInst *emit_static_roots(jl_codectx_t &ctx, unsigned nroots)