I've been experimenting with code generation with llvm. I'm currently trying to generate IR for variable declarations. Here is an example:
let sum: float = 8 * 3 * 4 + 6 * 9;
When I generate the IR for this, this is what I get:
%sum = alloca float
%sum1 = alloca float
%sum2 = alloca float
store float mul (float 8.000000e+00, float 3.000000e+00), float* %sum2
%sum3 = load float, float* %sum2
%sum4 = alloca float
%sum5 = mul float %sum3, 4.000000e+00
store float %sum5, float* %sum4
%sum6 = load float, float* %sum4
store float %sum6, float* %sum
%sum_loaded = load float, float* %sum
%sum7 = alloca float
%sum8 = alloca float
store float mul (float 6.000000e+00, float 9.000000e+00), float* %sum8
%sum9 = load float, float* %sum8
store float %sum9, float* %sum
%sum_loaded10 = load float, float* %sum
Firstly, this looks quite messy, and I'm sure this can be done more efficiently. But i think i can fix it with optimizations later. My problem, is in testing to see if it works. I tried:
llc my_ir
Which throws:
llc: error: llc: my_ir:4:15: error: constexpr requires integer operands
store float mul (float 8.000000e+00, float 3.000000e+00), float* %sum2
I don't really understand this error message, but it sounds like it can't multiply floats? What's going on here?
I think it's just that you need to use fmul
to multiply floats.