lli : external functions and target datalayout

Hi All,

I have a C code:
//////////////////////////////
#include "stdio.h"

int main () {
  putchar('a');
  return 0;
}

llvm-gcc -emit-llvm, I got
////////////////////////////////////////
; ModuleID = 't1.bc'
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"
target triple = "i386-pc-linux-gnu"

define i32 @main() nounwind {
entry:
  %retval = alloca i32 ; <i32*> [#uses=2]
  %0 = alloca i32 ; <i32*> [#uses=2]
  %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
  %1 = call i32 @putchar(i32 97) nounwind ; <i32> [#uses=0]
  store i32 0, i32* %0, align 4
  %2 = load i32* %0, align 4 ; <i32> [#uses=1]
  store i32 %2, i32* %retval, align 4
  br label %return

return: ; preds = %entry
  %retval1 = load i32* %retval ; <i32> [#uses=1]
  ret i32 %retval1
}

declare i32 @putchar(i32)
//////////////////////////////////////////////

Is there a way to tell 'lli' where to load the external function
'putchar'? "lli -force-interpreter t1.bc" reports "LLVM ERROR: Tried
to execute an unknown external function: i32 (i32)* putchar".

The other question is about
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"

If I use this string to creat a TargetData object directly, I got an
assertion error:

~/llvm-2.6/lib/Target/TargetData.cpp:246: void
llvm::TargetData::setAlignment(llvm::AlignTypeEnum, unsigned char,
unsigned char, uint32_t): Assertion `abi_align <= pref_align &&
"Preferred alignment worse than ABI!"' failed.
Aborted

This is because of the i64:32:64. It seems to be i64:64:64. 'lli' is
able to fix this problem automatically, but why does llvm-gcc output
i64:32:64 rather than i64:64:64?

Thanks

Jianzhou Zhao <jianzhou@seas.upenn.edu> writes:

[snip]

The other question is about
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"

If I use this string to creat a TargetData object directly, I got an
assertion error:

~/llvm-2.6/lib/Target/TargetData.cpp:246: void
llvm::TargetData::setAlignment(llvm::AlignTypeEnum, unsigned char,
unsigned char, uint32_t): Assertion `abi_align <= pref_align &&
"Preferred alignment worse than ABI!"' failed.
Aborted

This is because of the i64:32:64. It seems to be i64:64:64. 'lli' is
able to fix this problem automatically, but why does llvm-gcc output
i64:32:64 rather than i64:64:64?

FYI: the X86 data layout is hard-coded in
lib/Target/X86/X86TargetMachine.cpp, so that assert is mostly bogus, and
the existence of "target datalayout" IR setting and
Module::setDataLayout are deceiving.

Jianzhou Zhao <jianzhou@seas.upenn.edu> writes:

[snip]

The other question is about
target datalayout =
"e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:32:32"

If I use this string to creat a TargetData object directly, I got an
assertion error:

~/llvm-2.6/lib/Target/TargetData.cpp:246: void
llvm::TargetData::setAlignment(llvm::AlignTypeEnum, unsigned char,
unsigned char, uint32_t): Assertion `abi_align <= pref_align &&
"Preferred alignment worse than ABI!"' failed.
Aborted

This is because of the i64:32:64. It seems to be i64:64:64. 'lli' is
able to fix this problem automatically, but why does llvm-gcc output
i64:32:64 rather than i64:64:64?

FYI: the X86 data layout is hard-coded in
lib/Target/X86/X86TargetMachine.cpp, so that assert is mostly bogus, and

Thanks. I should get the TargetData object from the ExecutionEngine class.

the existence of "target datalayout" IR setting and
Module::setDataLayout are deceiving.

Do you know if the latest release fixed this assertion? I actually
dont quite understand how this assertion affects the program. Can I
use setDataLayout for other targets?