I made a macro that checks if some code is vectorized or not, by getting the LLVM code and checking if “vector.body” is there or not, and then it actually runs the code. Here it is:
macro vec_check(expr) f = gensym() llvm = gensym() io = gensym() vec = gensym() return esc(quote function $f() $expr end $io = IOBuffer() code_llvm($io, $f, ()) $llvm = String(take!($io)) $vec = all(contains.($llvm, ("\nvector.body:", "\nvector.ph:"))) if $vec println("Vectorized") else println("Not vectorized") end $expr end) end
This is useful when you have a big algorithm and you want to check if each loop which you are trying to vectorize is really vectorized or not. Example:
julia> @vec_check begin n = 100000; a = rand(n); @inbounds @simd for i in 1:n a[i] = 1 end a; end; Vectorized julia> @vec_check begin n = 100000; a = rand(n); @simd for i in 1:n a[i] = 1 end a; end; Not vectorized
Is this a welcome PR, and if so to which package, Base?