From d30d2b8aff3e6bd9c98f9906ca6add6730e7b47b 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 7190f9db81b741..860695e140cbd0 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() == 1) // 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)