[LLVMdev] Structure Alignment

Manoel Teixeira manoel at fonetica.com.br
Thu Jun 4 06:39:24 PDT 2009


Hi,
I have a problem using structs, when calling a function written in C from
a LLVM function or calling a LLVM function from a C one.
I noticed that the LLVM align double with 4 bytes and our run time routines
use 8, in MS Windows.
If a use #pragma pack(4) in my C programs, the routines run fine, but
I must ensure that all external call should use the #pragma directive.

If I change the llvm layout f64:64:64, the LLVM calls C functions and
the struct is OK, in the C side, but the getelemntptr fails in LLVM side.
Is there on way to ensure that the LLVM reads the correct memory position
in this case?

//#pragma pack(4)
struct xfirstStru
{
    int                var1;
    double            var2;
    int                var3;
    char    *var4;
};
typedef struct xfirstStru osX ;
typedef struct xfirstStru* PosX ;
//#pragma pack()
//
extern "C" int testOSX(    PosX osx    )
{
    printf("\nChamada C %d %f %d %s",++osx->var1,++osx->var2,++osx->var3,osx->var4);
    return 999;
}

it was
... f64:32:64 ...
and I changed it to
... f64:64:64 ...

%struct.VAR1. = getelementptr %FIRSTSTRU.* %OUTRASTRU., i32 0, i32 0        ; <i32*> [#uses=2]    %loadX = load i32* %struct.el.VAR1., align 4        ; <i32> [#uses=1]
here the first element is OK
...
%struct.el.VAR2. = getelementptr %FIRSTSTRU.* %OUTRASTRU., i32 0, i32 1        ; <i32*> [#uses=2]    %loadX = load double* %struct.el.VAR2., align 8        ; <double> [#uses=1]
here, the second is wrong
...


Thanks,
Manoel Teixeira





More information about the llvm-dev mailing list