[LLVMdev] Incorrect execution of global constructor with JIT on ARM

Martins Mozeiko 49640f8a at gmail.com
Mon Feb 15 06:49:50 PST 2010


Hello, llvm developers!

I am running LLVM with JIT on ARM. For simple programs it runs ok, but for lager code I have stumbled upon some issues.
See following C++ code to which I have reduced the problem:

#include <stdio.h>
struct Global {
  typedef unsigned char ArrayType[4];
  ArrayType value;
  Global(const ArrayType& arg) {
    for (int i = 0; i < 4; i++) this->value[i] = arg[i];
  }
};
static const unsigned char arr[] = { 1, 2, 3, 4 };
static const Global Constant(arr);
int main() {
  for (int i=0; i<4; i++) printf("%i", Constant.value[i]);
}

I am compiling it with llvm-gcc (with -O3 or -O2 optimization), and am running it with llvm version 2.6. Instead of priniting out 1234, it prints out 4444.
I verified contents of Constant memory with this code:

        const llvm::GlobalValue* v = module->getGlobalVariable("_ZL8Constant", true);
        void* addr = EE->getPointerToGlobal(v);
        const unsigned char* ptr = (const unsigned char*)addr;
        for (int i=0; i<6; i++)
        {
            outs() << (int)ptr[i] << ','; 
        }

I really see that memory is filled with 4,4,4,4 and that is incorrect.
When I put global constant definition const Global Constant(arr); in main function as local variable then everything runs fine - program prints out 1234.
Is that some issue with LLVM JIT for ARM, or LLVM in general? Same code runs fine on Windows with same version of llvm.
Global constructor code looks like this:

define internal void @_GLOBAL__I_main() nounwind {
entry:
  store i8 1, i8* getelementptr inbounds (%struct.Global* @_ZL8Constant, i32 0, i32 0, i32 0), align 8
  store i8 2, i8* getelementptr inbounds (%struct.Global* @_ZL8Constant, i32 0, i32 0, i32 1), align 1
  store i8 3, i8* getelementptr inbounds (%struct.Global* @_ZL8Constant, i32 0, i32 0, i32 2), align 2
  store i8 4, i8* getelementptr inbounds (%struct.Global* @_ZL8Constant, i32 0, i32 0, i32 3), align 1
  ret void
}

I don't see any problems with it.
When I compile same bitcode file with llc.exe -march=arm, and use generated assembler file on my ARM, then code runs fine.
What else could I check in this situation to determine more about problem?

--
Martins Mozeiko





More information about the llvm-dev mailing list