llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

calls to old-style functions possibly wrong

Open ahatanak opened this issue 15 years ago • 11 comments

Bugzilla Link 10613
Resolution FIXED
Resolved on Nov 30, 2011 19:38
Version trunk
OS Linux
CC @asl,@efriedma-quic

Extended Description

clang seems to treat calls to functions defined in the old-style differently when optimization level is -O0 and -O3: at level -O3, calls to f1 and f2 in main look identical while at level -O3, the pointer to f2 is being cast to a function with ellipsis before it is called. This results in generation of incorrect code for Mips (and possibly for other targets which have different calling conventions for functions that have a variable argument list and those that don't).

// oldstl2.c // adapted from gcc.c-torture/execute/960513-1.c #include <stdio.h> #include <stdlib.h>

void attribute((noinline)) f1(double f, int i) { printf("%f\n", f + i); }

void attribute((noinline)) f2(f, i) double f; int i; { printf("%f\n", f + i); }

int main(int argc, char** argv) { f1(2.0, 1); f2(2.0, 1);

return 0; }

$ clang oldstl2.c -o oldstl2.ll -emit-llvm -O3 -S ; ModuleID = 'oldstl2.c' target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64" target triple = "x86_64-unknown-linux-gnu"

@.str = private unnamed_addr constant [4 x i8] c"%f\0A\00", align 1

define void @​f1(double %f, i32 %i) nounwind uwtable noinline { entry: %conv = sitofp i32 %i to double %add = fadd double %conv, %f %call = tail call i32 (i8*, ...)* @​printf(i8* getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), double %add) nounwind ret void }

declare i32 @​printf(i8* nocapture, ...) nounwind

define void @​f2(double %f, i32 %i) nounwind uwtable noinline { entry: %conv = sitofp i32 %i to double %add = fadd double %conv, %f %call = tail call i32 (i8*, ...)* @​printf(i8* getelementptr inbounds ([4 x i8]* @.str, i64 0, i64 0), double %add) nounwind ret void }

define i32 @​main(i32 %argc, i8** nocapture %argv) nounwind uwtable { entry: tail call void @​f1(double 2.000000e+00, i32 1) tail call void @​f2(double 2.000000e+00, i32 1) nounwind ret i32 0 }

$ clang oldstl2.c -o oldstl2.ll -emit-llvm -O0 -S // definition of f1 and f2 omitted. define i32 @​main(i32 %argc, i8** %argv) nounwind uwtable { entry: %retval = alloca i32, align 4 %argc.addr = alloca i32, align 4 %argv.addr = alloca i8**, align 8 store i32 0, i32* %retval store i32 %argc, i32* %argc.addr, align 4 store i8** %argv, i8*** %argv.addr, align 8 call void @​f1(double 2.000000e+00, i32 1) call void (...)* bitcast (void (double, i32)* @​f2 to void (...)*)(double 2.000000e+00, i32 1) ret i32 0 }

ahatanak avatar Aug 09 '11 03:08 ahatanak

This was fixed in r140241.

efriedma-quic avatar Dec 01 '11 03:12 efriedma-quic

Okay... sorry about asking so many questions, but I wanted to make sure this wasn't something that would come back to bite you later.

Again, this should be a relatively easy change to CodeGenFunction::EmitCall.

Okay, thank you.

ahatanak avatar Aug 09 '11 18:08 ahatanak

Okay... sorry about asking so many questions, but I wanted to make sure this wasn't something that would come back to bite you later.

Again, this should be a relatively easy change to CodeGenFunction::EmitCall.

efriedma-quic avatar Aug 09 '11 05:08 efriedma-quic

Have you tried something like the following (which would be the case where the codegen is actually different)?

void a(); void b() { return a(1.0,2.0); } This is what I get with gcc:

$ mips-linux-gnu-gcc foo.c -mips32r2 -O3 -EL -o - -S lui $2,%hi($LC0) ldc1 $f12,%lo($LC0)($2) lui $2,%hi($LC1) j a ldc1 $f14,%lo($LC1)($2)

Both arguments are passed in floating point registers (f12 and f14).

I think gcc follows the rule mentioned in the document:

"A function without a prototype or without an ellipsis in a prototype is called using the normal argument passing conventions."

There is no prototype for function "a" in your example, therefore it follows the non-vararg function's argument passing convention.

ahatanak avatar Aug 09 '11 05:08 ahatanak

Have you tried something like the following (which would be the case where the codegen is actually different)?

void a(); void b() { return a(1.0,2.0); } This is what I get with gcc:

$ mips-linux-gnu-gcc foo.c -mips32r2 -O3 -EL -o - -S lui $2,%hi($LC0) ldc1 $f12,%lo($LC0)($2) lui $2,%hi($LC1) j a ldc1 $f14,%lo($LC1)($2)

Both arguments are passed in floating point registers (f12 and f14).

ahatanak avatar Aug 09 '11 05:08 ahatanak

Have you tried something like the following (which would be the case where the codegen is actually different)?

void a(); void b() { return a(1.0,2.0); }

efriedma-quic avatar Aug 09 '11 05:08 efriedma-quic

Hmm... strictly speaking, it isn't impossible for a varargs call to call a non-varargs function if you put the relevant arguments into both the integer and floating-point registers (you end up with something rather close to the Windows 64-bit calling convention). You might want to double-check that gcc doesn't do that.

gcc (4.5.1) produces code like this for call to f2:

mov.d $f12,$f20 # f20 contains the 1st arg (constant 2.0) lw $25,%call16(f2)($28) jalr $25 li $6,1 # second arg, 0x1

It is possible to use integer registers to pass floating point arguments ($4 and $5, in this example) in addition to floating point registers, but I have never seen gcc do that.

I meant to say floating point registers in addition to integer registers.

ahatanak avatar Aug 09 '11 05:08 ahatanak

Hmm... strictly speaking, it isn't impossible for a varargs call to call a non-varargs function if you put the relevant arguments into both the integer and floating-point registers (you end up with something rather close to the Windows 64-bit calling convention). You might want to double-check that gcc doesn't do that.

gcc (4.5.1) produces code like this for call to f2:

mov.d $f12,$f20 # f20 contains the 1st arg (constant 2.0) lw $25,%call16(f2)($28) jalr $25 li $6,1 # second arg, 0x1

It is possible to use integer registers to pass floating point arguments ($4 and $5, in this example) in addition to floating point registers, but I have never seen gcc do that.

ahatanak avatar Aug 09 '11 05:08 ahatanak

Hmm... strictly speaking, it isn't impossible for a varargs call to call a non-varargs function if you put the relevant arguments into both the integer and floating-point registers (you end up with something rather close to the Windows 64-bit calling convention). You might want to double-check that gcc doesn't do that.

efriedma-quic avatar Aug 09 '11 04:08 efriedma-quic

Does the Mips calling convention truly have requirements for varargs functions that are incompatible with those for non-varargs functions? Our current behavior is an attempt to remain compatible with C code with incorrect declarations of varargs functions, something like the following:

int fun(); /* fun is actually defined as int fun(int, ...) */ int bar() { fun(2, "a", "b"); }

Of course, code like that isn't strictly conforming.

If we truly cannot emit a varargs call to a non-varargs function on Mips, it would not be too hard to whack CodeGenFunction::EmitCall to do the right thing.

Yes. In O32, arguments that are passed in floating points registers for non-vararg functions are passed in integer registers when vararg functions are called. Please see 3-18 of the following document.

www.sco.com/developers/devspecs/mipsabi.pdf

ahatanak avatar Aug 09 '11 04:08 ahatanak

Does the Mips calling convention truly have requirements for varargs functions that are incompatible with those for non-varargs functions? Our current behavior is an attempt to remain compatible with C code with incorrect declarations of varargs functions, something like the following:

int fun(); /* fun is actually defined as int fun(int, ...) */ int bar() { fun(2, "a", "b"); }

Of course, code like that isn't strictly conforming.

If we truly cannot emit a varargs call to a non-varargs function on Mips, it would not be too hard to whack CodeGenFunction::EmitCall to do the right thing.

efriedma-quic avatar Aug 09 '11 03:08 efriedma-quic