Mapping LLVM type i1 to Julia type

I am trying to use llvmcall and want to map a return value of LLVM type i1 to a Julia Bool value.

using Base

function isodd(a)
    Base.llvmcall("""%mod = srem i64 %0, 2\n%cmp = icmp eq i64 %mod, 1\nret i1 %cmp""", Bool, Tuple{Int64,}, a)
end

print(isodd(13))

The following error is thrown when executing the program:

ERROR: LoadError: Failed to parse LLVM assembly: 
llvmcall:5:5: error: value doesn't match function result type 'i8'
ret i1 %cmp

Could anyone help? Is there an alternative type I can use instead of Bool to match to i1?

You need to zero extend it to i8. See

https://github.com/eschnett/SIMD.jl/blob/24016b1790cf105e20fa10e374206589f8f9bb4c/src/LLVM_intrinsics.jl#L349

3 Likes

Thanks for the answer. But would it be possible to do this without changing the LLVM code, and by adding some compatible Julia type?

I don’t think so. Even primitive type restricts the size to be a multiple of 8.

3 Likes

FWIW, I haven’t seen an example where LLVM fails to get rid of the zext/truncs.

3 Likes