[LLVMdev] Wrong float value stored in LLVM IR code

Adrian Ortega elfus0.1 at gmail.com
Tue Jun 17 20:59:12 PDT 2014


Hi everyone,

I'm learning how to use LLVM API and JIT engine and I've come across 
with an issue I haven't been able to figure out.

The problem I'm having is that the wrong float is being stored  in a 
float global variable.

The code snippet I use to generate the float value is as follow:

     llvm::Type* type = // initialize with the global variable type;
     std::string real_value = // initialized with the float value from 
std input.
     ...
     return ConstantFP::get(type,real_value);

I am using the code snippet above to store a float and double value, the 
double value works, the float value not. This can be seen with the 
following LLVM IR I am generating:

What the functions do is just:
  1. backup current value of global variable
  2. store new value
  3. call a C function which returns the value of the global variable in 
that same module
  4. Compare against the value used in step 2
  5. restore the initial value
  6. return true or false if the values were equal.

/**
   float gfloat = 3.141592;
*/
define i32 @test_getgfloat_0() {
block_test_getgfloat_0:
   %0 = load float* @gfloat
   store float 0x400921FB00000000, float* @gfloat // wrong value stored
   %1 = call float @getgfloat()
   %2 = fcmp oeq float %1, 0x400921FA00000000 // wrong value stored
   %3 = zext i1 %2 to i32
   store float %0, float* @gfloat
   ret i32 %3
}

/**
   double gdouble = 2.52340;
  */
define i32 @test_getdouble_0() {
block_test_getdouble_0:
   %0 = load double* @gdouble
   store double 2.523400e+00, double* @gdouble // correct value stored
   %1 = call double @getdouble()
   %2 = fcmp oeq double %1, 2.523400e+00 // correct value stored
   %3 = zext i1 %2 to i32
   store double %0, double* @gdouble
   ret i32 %3
}

Do you have any idea of why I'm having this problem?

Thank you.



More information about the llvm-dev mailing list