Skip to contentSkip to conversation

const versus __attribute__((const))

DISCUSSION Arnd Bergmann 5 participants 16 messages Dec 8, 2003 – Dec 9, 2003

lore
Share & tools
lore
#0 lore raw
Quoted 4 lines · H. Peter Anvin

I have made a patch against the current tree defining __attribute_const__ in <linux/compiler.h> and using it in the above cases; does anyone know any reason why I should *NOT* submit this to Linus?

I noticed before that gcc appearantly ignores __attribute__((const)) for inline functions, so both the original and your proposed code is rather pointless as an optimization, except for extern declarations.

I'd rather remove the 'const' completely where it causes warnings for inline functions.

	Arnd <><
#1 lore raw
Quoted 8 lines · Arnd Bergmann
Quoted 4 lines · H. Peter Anvin

I have made a patch against the current tree defining __attribute_const__ in <linux/compiler.h> and using it in the above cases; does anyone know any reason why I should *NOT* submit this to Linus?

Quoted 7 lines

I noticed before that gcc appearantly ignores __attribute__((const)) for inline functions, so both the original and your proposed code is rather pointless as an optimization, except for extern declarations.

I'd rather remove the 'const' completely where it causes warnings for inline functions.

These functions are available to userspace, though, and can be compiled with -O0; thus not inlined.

-hpa

#2 lore raw
Quoted 13 lines · H. Peter Anvin
Quoted 12 lines · Arnd Bergmann
Quoted 4 lines · H. Peter Anvin

I have made a patch against the current tree defining __attribute_const__ in <linux/compiler.h> and using it in the above cases; does anyone know any reason why I should *NOT* submit this to Linus?

I noticed before that gcc appearantly ignores __attribute__((const)) for inline functions, so both the original and your proposed code is rather pointless as an optimization, except for extern declarations.

Quoted 6 lines
Quoted 3 lines

I'd rather remove the 'const' completely where it causes warnings for inline functions.

These functions are available to userspace, though, and can be compiled with -O0; thus not inlined.

And future versions of gcc can be smarter.

Quoted 2 lines

-hpa

Nikita.

#3 lore raw
Quoted 6 lines · Nikita Danilov
Quoted 3 lines

These functions are available to userspace, though, and can be compiled with -O0; thus not inlined.

And future versions of gcc can be smarter.

Actually, the reason it doesn't use it for the inlines is because it doesn't need to -- it already has full visibility, so it doesn't need it to be spelled out.

So it would be an issue if gcc got dumber, not smarter.

-hpa

#4 lore raw
Quoted 3 lines · H. Peter Anvin

Actually, the reason it doesn't use it for the inlines is because it doesn't need to -- it already has full visibility, so it doesn't need it to be spelled out.

Until a few minutes ago, I disagreed. I thought GCC wouldn't eliminate calls to inline functions which contain an inline asm, such as current_thread_info().

But having just looked, multiple calls to current_thread_info() are eliminated. GCC inspects the arguments of the inline asm which is the body of this function, and concludes that the asm itself is to be optimised like a const function, depending only on its input operands.

In fact to get GCC to _not_ optimise away inline asms, or even to behave like "pure" (allowing memory to be read) instead of "const", they must be declared volatile, or have no outputs. Putting "memory" in the clobber list says only that the asm clobbers memory, not that it reads memory.

It would be nice to have a way to declare an asm like "pure" not "const", so that it's allowed to read memory but multiple calls can be eliminated; I don't know of a way to express that.

-- Jamie

#5 lore raw
Quoted 5 lines · Jamie Lokier

It would be nice to have a way to declare an asm like "pure" not "const", so that it's allowed to read memory but multiple calls can be eliminated; I don't know of a way to express that.

Just specify memory input operands.

-hpa

#6 lore raw
Quoted 5 lines · H. Peter Anvin
Quoted 3 lines

It would be nice to have a way to declare an asm like "pure" not "const", so that it's allowed to read memory but multiple calls can be eliminated; I don't know of a way to express that.

Just specify memory input operands.

Thanks. That's even more useful than "pure" because it implies the asm only reads the explicitly passed memory operands.

Memory input operands don't work if you want the asm to read arbitrary memory not mentioned in the inputs (like "pure" allows) or traverse linked lists.

(A long time ago there was a question about whether GCC could ever copy the value associated with an "m" operand to a stack slot, and pass the address of the stack slot. After all, GCC _will_ copy the value if the operand is an "r", and presumably gives mixed results with "rm". We seem to have concluded that it never will).

-- Jamie

#7 lore raw
Quoted 16 lines · Jamie Lokier
Quoted 5 lines · H. Peter Anvin
Quoted 3 lines

It would be nice to have a way to declare an asm like "pure" not "const", so that it's allowed to read memory but multiple calls can be eliminated; I don't know of a way to express that.

Just specify memory input operands.

Thanks. That's even more useful than "pure" because it implies the asm only reads the explicitly passed memory operands.

Memory input operands don't work if you want the asm to read arbitrary memory not mentioned in the inputs (like "pure" allows) or traverse linked lists.

Quoted 6 lines

(A long time ago there was a question about whether GCC could ever copy the value associated with an "m" operand to a stack slot, and pass the address of the stack slot. After all, GCC _will_ copy the value if the operand is an "r", and presumably gives mixed results with "rm". We seem to have concluded that it never will).

Sure it will:

int foo(int x)
{
int y, z;

  asm("movl %1,%0" : "=r" (y) : "r" (x));
  asm("movl %1,%0" : "=r" (z) : "m" (y));

return z;
}

: smyrno 21 ; gcc -O2 -c testme.c
: smyrno 22 ; objdump -dr testme.o

testme.o: file format elf32-i386

Disassembly of section .text:

00000000 <foo>:

   0:   55                      push   %ebp			; Make stack frame
   1:   89 e5                   mov    %esp,%ebp		; d:o
   3:   50                      push   %eax			; Allocate stack slot
   4:   8b 45 08                mov    0x8(%ebp),%eax		; Copy to register
   7:   89 c0                   mov    %eax,%eax		; First asm()
   9:   89 45 fc                mov    %eax,0xfffffffc(%ebp) 	; Copy to stack slot
   c:   8b 45 fc                mov    0xfffffffc(%ebp),%eax	; Second asm()
   f:   c9                      leave				; Destroy stack frame
  10:   c3                      ret
#8 lore raw
Quoted 2 lines · H. Peter Anvin

Sure it will:

No, Jamie was thinking of a variable that _already_ has a memory location having its value copied around to _another_ memory location (ie a temporary stack slot).

What your example shows is that gcc will create a stack slot for an automatic variable if it doesn't have one already, in order to make it an lvalue with a memory address.

So what's interesting is if gcc would take something like

	extern int variable;

	asm("..." : :"m" (variable));

and cause this to create a _new_ temporary on the stack slot, and pass the asm the pointer to that temporary rather than the "real" address of 'variable'.

And as far as I know, it won't. Newer gcc's will _require_ memory arguments to be lvalues (well, it will warn if they aren't), and will always turn them into addressables of that particular lvalue.

This is actually an issue, because some asm code depends on getting the proper address - not just the proper value. Things like locks etc care _deeply_ about more than just the value.

Linus

#9 lore raw
Quoted 5 lines · Linus Torvalds

And as far as I know, it won't. Newer gcc's will _require_ memory arguments to be lvalues (well, it will warn if they aren't), and will always turn them into addressables of that particular lvalue.

Well, I get no warning from the following code, with gcc 3.2.2 and -W -Wall:

int foo(int x)
{
int y, z, w;

  asm("movl %1,%0" : "=r" (y) : "r" (x));
  asm("movl %1,%0" : "=r" (z) : "m" (y+1));
  asm("movl %1,%0" : "=r" (w) : "m" (33));

return z+w;
}

The memory operand is definitely not an lvalue. gcc allocates a stack slot for the y+1 value as a temporary automatic and passes it in. The value 33 is put in the .rodata segment:

00000000 <foo>:

   0:   55                      push   %ebp
   1:   89 e5                   mov    %esp,%ebp
   3:   50                      push   %eax
   4:   8b 45 08                mov    0x8(%ebp),%eax
   7:   89 c0                   mov    %eax,%eax
   9:   40                      inc    %eax
   a:   89 45 fc                mov    %eax,0xfffffffc(%ebp)
   d:   8b 15 00 00 00 00       mov    0x0,%edx
                        f: R_386_32     .rodata.cst4
  13:   8b 45 fc                mov    0xfffffffc(%ebp),%eax
  16:   01 d0                   add    %edx,%eax
  18:   c9                      leave
  19:   c3                      ret
Quoted 3 lines

This is actually an issue, because some asm code depends on getting the proper address - not just the proper value. Things like locks etc care _deeply_ about more than just the value.

That would be a very bad thing indeed. Fortunately I think it would take some rather odd contortions on the part of the compiler for that situation to occur at all, I would think, and if the gcc people have since been made aware of it it should be pretty easy for them to make sure it will not happen.

-hpa

#10 lore raw
Quoted 7 lines · H. Peter Anvin

Well, I get no warning from the following code, with gcc 3.2.2 and -W -Wall:

int foo(int x)
{
   int y, z, w;

Quoted 6 lines

   asm("movl %1,%0" : "=r" (y) : "r" (x));
   asm("movl %1,%0" : "=r" (z) : "m" (y+1));
   asm("movl %1,%0" : "=r" (w) : "m" (33));

   return z+w;
}

For reference, both gcc-3.3 and gcc-3.4 (snapshot) give produce the same assembly as gcc-3.2 for your code, but give this warning:

test.c:6: warning: use of memory input without lvalue in asm operand 1 is deprecated test.c:7: warning: use of memory input without lvalue in asm operand 1 is deprecated

	Arnd <><
#11 lore raw
Quoted 6 lines · Arnd Bergmann

For reference, both gcc-3.3 and gcc-3.4 (snapshot) give produce the same assembly as gcc-3.2 for your code, but give this warning:

test.c:6: warning: use of memory input without lvalue in asm operand 1 is deprecated test.c:7: warning: use of memory input without lvalue in asm operand 1 is deprecated

In some ways, this is rather unfortunate, too. What it really means is that the gcc "m" constraint is overloaded; it would have been better if they would have created a new modifier (say "*") for "must be lvalue."

-hpa

#12 lore raw
Quoted 4 lines · H. Peter Anvin

In some ways, this is rather unfortunate, too. What it really means is that the gcc "m" constraint is overloaded; it would have been better if they would have created a new modifier (say "*") for "must be lvalue."

The thing is, most users of "m" (like 99%) actually mean "_THIS_ memory location". So just fixing the "m" modifier was an easy way to make sure that users get the behaviour they expect.

Also, I have this dim memory of there actually being a potential bug in "m" handling inside gcc, and requiring the entry to be a lvalue was the easiest way to fix it. Richard Henderson would have the details. I think it was the liveness analysis that got confused or something.

And the thing is, if you have a non-lvalue right now, you will (a) get a nice warnign that tells you so, and (b) it will be trivial to fix. So something like

	asm("xxxx" : :"m" (1+x));

can be trivially fixed to be

	{
		int tmp = 1+x;
		asm("xxxx" : : "m" (tmp));
	}

so it's not like it's a horribly undue burden on the programmer.

In the kernel, I don't think we had a _single_ case that needed this, but I might remember that wrong. Anyway, it wasn't a problem - and the kernel tends to be the single most active user of inline asm's of all gcc-compiled projects.

Linus

#13 lore raw
Quoted 12 lines · Linus Torvalds
Quoted 3 lines · H. Peter Anvin

In some ways, this is rather unfortunate, too. What it really means is that the gcc "m" constraint is overloaded; it would have been better if they would have created a new modifier (say "*") for "must be lvalue."

The thing is, most users of "m" (like 99%) actually mean "_THIS_ memory location". So just fixing the "m" modifier was an easy way to make sure that users get the behaviour they expect.

Agreed. It's just a bit ugly that the "m" in "rm" has a different meaning than just "m".

-hpa

#14 lore raw
Quoted 2 lines · H. Peter Anvin

Agreed. It's just a bit ugly that the "m" in "rm" has a different meaning than just "m".

Oh? What does the "m" in "m,m" mean then?

(I don't have GCC 3.3 to try it here).

Sometimes that's useful, when used with differing multi-alternative constraints on the other arguments, and 99% of the time you _do_ want the "m,m" constraint to use the actual lvalue's address just like "m".

-- Jamie

#15 lore raw
In reply to Jamie Lokier
Quoted 6 lines · Jamie Lokier

(A long time ago there was a question about whether GCC could ever copy the value associated with an "m" operand to a stack slot, and pass the address of the stack slot. After all, GCC _will_ copy the value if the operand is an "r", and presumably gives mixed results with "rm". We seem to have concluded that it never will).

We never never concluded that they never would, but we did (I think) convince the gcc people that a memory operand to an asm should always be considered a lvalue. That will effectively mean that we know a memory op will never be moved around - because then it wouldn't be the same lvalue any more (a lvalue is literally defined by its address).

So yes, I think we can depend on it now, although we historically couldn't.

Linus