Not sure if this belongs to llvmlite
channel or the main numba
channel, but it seems like IRBuilder.if_then
and IRBuilder.if_else
aren’t working.
I am trying to write a very simple numba
intrinsic
function that will return 1
if the supplied value x
is greater than 0
or return 1
otherwise. The intrinsic
function looks like below code snippet, it’s called check_value()
:
@intrinsic
def check_value(typingctx, src):
# create the expected type signature
result_type = types.Integer('int64')
sig = result_type(types.int64)
# defines the custom code generation
def codegen(context, builder, signature, args):
# llvm IRBuilder code here
gtz = builder.icmp_signed(">", args[0], args[0].type(0))
with builder.if_else(gtz) as (then, otherwise):
with then:
print("gtz = True")
a = context.get_constant(signature.args[0], 1)
with otherwise:
print("gtz = False")
a = context.get_constant(signature.args[0], 0)
return a
return sig, codegen
and the function to be njit
ted looks like this:
@njit('int64(int64)')
def func(x):
y = check_value(x)
return y
and this is how it’s been used:
a = 10
b = func(a)
print("b =", b)
and when I run it, I am getting this output:
gtz = True
gtz = False
b = 0
But I was expecting this output:
gtz = True
b = 1
Why is it hitting both branches? i.e. printing both gtz = True
and gtz = False
statements? and why it is not returning 1
?
Even it doesn’t work with the else
case:
a = -10
b = func(a)
print("b =", b)
the output is same like when a = 10
:
gtz = True
gtz = False
b = 0
How do I make it work?