2003-09-13 23:57:49

by Andi Kleen

[permalink] [raw]
Subject: stack alignment in the kernel was Re: nasm over gas?

Jamie Lokier <[email protected]> writes:

> Obvious the _intent_ of -O2 is to compile for speed, but it's clear
> that GCC often emits trivially redundant instructions (like stack
> adjustments) that don't serve to speed up the program at all.

The stack adjustments are for getting good performance with floating
point code. Most x86 CPUs require 16 byte alignment for floating point
stores/loads on the stack. It can make a dramatic difference in some
FP intensive programs.

But obviously that's completely useless for the kernel which never
uses floating point.

A compiler option to turn it off would make sense to save .text space
and eliminate these useless instructions. Especially since the kernel
entry points make no attempt to align the stack to 16 byte anyways,
so most likely the stack adjustments do not even work.

(this option could also warn for floating point usage which is usually illegal,
although you can already get the same effect by compiling with -msoft-float)

-Andi


2003-09-14 13:55:08

by Jamie Lokier

[permalink] [raw]
Subject: Re: stack alignment in the kernel was Re: nasm over gas?

Andi Kleen wrote:
> The stack adjustments are for getting good performance with floating
> point code. Most x86 CPUs require 16 byte alignment for floating point
> stores/loads on the stack. It can make a dramatic difference in some
> FP intensive programs.

You're right.

> A compiler option to turn it off would make sense to save .text space
> and eliminate these useless instructions. Especially since the kernel
> entry points make no attempt to align the stack to 16 byte anyways,
> so most likely the stack adjustments do not even work.

There is an option:

-mpreferred-stack-boundary=2

-- Jamie

2003-09-14 14:14:01

by Andi Kleen

[permalink] [raw]
Subject: Re: stack alignment in the kernel was Re: nasm over gas?

On Sun, Sep 14, 2003 at 02:54:31PM +0100, Jamie Lokier wrote:
> > A compiler option to turn it off would make sense to save .text space
> > and eliminate these useless instructions. Especially since the kernel
> > entry points make no attempt to align the stack to 16 byte anyways,
> > so most likely the stack adjustments do not even work.
>
> There is an option:
>
> -mpreferred-stack-boundary=2

Hmm. The i386 Makefile sets that already. Where exactly did you see
bogus stack adjustments in kernel code?

-Andi

2003-09-14 15:57:25

by Jamie Lokier

[permalink] [raw]
Subject: Re: stack alignment in the kernel was Re: nasm over gas?

Andi Kleen wrote:
> Hmm. The i386 Makefile sets that already. Where exactly did you see
> bogus stack adjustments in kernel code?

I didn't. I saw them in a test program for __builtin_expect() in the
"oops_in_progress is unlikely()" thread.

I'm used to seeing redundant "mov" instructions and such from GCC, so
when I saw the stack adjustments with -O2 go away with -Os I thought
they were more of the same - not realising that -Os turns off the stack
alignment. My error.

-- Jamie

2003-09-14 22:27:01

by Jan Hubicka

[permalink] [raw]
Subject: Re: stack alignment in the kernel was Re: nasm over gas?

>
> > A compiler option to turn it off would make sense to save .text space
> > and eliminate these useless instructions. Especially since the kernel
> > entry points make no attempt to align the stack to 16 byte anyways,
> > so most likely the stack adjustments do not even work.
>
> There is an option:
>
> -mpreferred-stack-boundary=2

Note that this won't work for x86-64 where ABI compliant varargs require
it.

Honza
>
> -- Jamie