# Autovectorization in Julia 101

**URL:** <https://discourse.julialang.org/t/autovectorization-in-julia-101/123472>\
**Category:** Internals & Design\
**Tags:** simd, loopvectorization\
**Created:** [December 4, 2024, 10:15pm UTC](https://discourse.julialang.org/t/autovectorization-in-julia-101/123472 "2024-12-04T22:15:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Benny](https://avatars.discourse-cdn.com/v4/letter/b/49beb7/32.png) [@Benny](https://discourse.julialang.org/u/Benny)\
**Post date:** [December 4, 2024, 10:15pm UTC](https://discourse.julialang.org/t/autovectorization-in-julia-101/123472/1 "2024-12-04T22:15:40Z")

</div>

I’m trying to read up on autovectorization but I might be looking in the wrong place:  
[Auto-Vectorization in LLVM — LLVM 20.0.0git documentation](https://llvm.org/docs/Vectorizers.html)  
For one, the whole thing talks about Clang and C code, so I have doubts it’s relevant to Julia. I don’t know the LLVM compiler pipeline all that well to begin with.

If I am indeed looking in the right place and this is relevant to Julia, then I have a few questions on top of the big “what even is autovectorization doing in Julia 101”:

> Many loops cannot be vectorized including loops with complicated control flow, unvectorizable types, and unvectorizable calls.

This seems consistent with `@simd`, but…

> The Loop Vectorizer is able to “flatten” the IF statement in the code and generate a single stream of instructions. The Loop Vectorizer supports any control flow in the innermost loop. The innermost loop may contain complex nesting of IFs, ELSEs and even GOTOs.

…it sounds like _somewhat_ complex branching can be flattened. Why is `@simd` stricter?

> The loop vectorizer uses a cost model to decide on the optimal vectorization factor and unroll factor.

Isn’t this what LoopVectorization.jl does? I assumed LLVM wasn’t doing this for us, hence our need for that package.

---

<div class="post-metadata">

**Author:** ![Tortar](https://avatars.discourse-cdn.com/v4/letter/t/6bbea6/32.png) [@Tortar](https://discourse.julialang.org/u/Tortar)\
**Post date:** [December 4, 2024, 10:47pm UTC](https://discourse.julialang.org/t/autovectorization-in-julia-101/123472/2 "2024-12-04T22:47:01Z")

</div>

> [@Benny](#):
>
> The loop vectorizer uses a cost model to decide on the optimal vectorization factor and unroll factor.

If I remember correctly, I think the author of LoopVectorization.jl one time on this forum said that the cost model of LLVM is simplistic and LoopVectorization.jl does a lot more for assessing the cost better

---

<div class="post-metadata">

**Author:** ![abraemer](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/abraemer/32/51403_2.png) [@abraemer](https://discourse.julialang.org/u/abraemer)\
**Post date:** [December 5, 2024, 11:43am UTC](https://discourse.julialang.org/t/autovectorization-in-julia-101/123472/3 "2024-12-05T11:43:22Z")

</div>

> [@Tortar](#):
>
> I think the author of [LoopVectorization.jl](https://juliahub.com/ui/Packages/General/LoopVectorization) one time on this forum said

cc @Elrod
