6

I was looking at the documentation on the Atmel website and I came across this example where they explain some issues with reordering.

Here's the example code:

#define cli() __asm volatile( "cli" ::: "memory" )
#define sei() __asm volatile( "sei" ::: "memory" )

unsigned int ivar;

void test2( unsigned int val )
{
  val = 65535U / val;

  cli();

  ivar = val;

  sei();
}

In this example, they're implementing a critical region-like mechanism. The cli instruction disables interrupts and the sei instruction enables them. Normally, I would save the interrupt state and restore to that state, but I digress...

The problem which they note is that, with optimization enabled, the division on the first line actually gets moved to after the cli instruction. This can cause some issues when you're trying to be inside of the critical region for the shortest amount of time as possible.

How come this is possible if the cli() MACRO expands to inline asm which explicitly clobbers the memory? How is the compiler free to move things before or after this statement?

Also, I modified the code to include memory barriers before every statement in the form of __asm volatile("" ::: "memory"); and it doesn't seem to change anything.

I also removed the memory clobber from the cli() and sei() MACROs, and the generated code was identical.

Of course, if I declare the test2 function argument as volatile, there is no reordering, which I assume to be because volatile statements can't be reordered with respect to other volatile statements (which the inline asm technically is). Is my assumption correct?

Can volatile accesses be reordered with respect to volatile inline asm?

Can non-volatile accesses be reordered with respect to volatile inline asm?

What's weird is that Atmel claims they need the memory clobber just to enforce the ordering of volatile accesses with respect to the asm. That doesn't make any sense to me.

If the compiler barrier isn't the proper solution for this, then how could I go about preventing any outside code from "leaking" into the critical region?

If anyone could shed some light, I'd appreciate it.

Thanks

10
  • val's address is never taken, so it could have been declared with the storage-class specifier register. Consequently, the "memory" clobber doesn't have to apply to it.
    – EOF
    Jun 18, 2016 at 14:20
  • Indeed, when I tried compiling this on x86-64 with -O2, val actually did get put in a register (and the division was between the cli/sei). And in fact, the same occurred even after I added (void)&val; inside the function. Jun 18, 2016 at 14:26
  • @EOF That makes sense. The compiler probably kept it in a register as part of the optimization. So how would one go about preventing the division from being moved to after the cli instruction? Jun 18, 2016 at 14:27
  • Take unsigned int *ptr = &val;, pass the pointer as an input operand into the asm()?
    – EOF
    Jun 18, 2016 at 14:28
  • @EOF: I don't think you even need the pointer. In my test, simply adding val as an input operand to the asm() had the desired effect. Which makes sense - we don't really care about whether val is in memory or a register, we just want its value to be computed before the asm. Jun 18, 2016 at 14:33

1 Answer 1

3

How come this is possible if the cli() MACRO expands to inline asm which explicitly clobbers the memory? How is the compiler free to move things before or after this statement?

This is due to implementation details of avr-gcc: The compiler's support library, libgcc, provides many functions written in assembly for performance; including functions for integer division like __udivmodhi4. Not all of these functions clobber all of the callee-used registers as specified by the avr-gcc ABI. In particular, __udivmodhi4 does not clobber the Z register.

avr-gcc makes use of this as follows: On machines without 16-bit division instruction like AVR, GCC would issue a library call instead of generating code for it inline. avr-gcc however pretends that the architecture does have such division instruction and models it as having an effect on processor registers just like the library call. Finally, after all code analyzes and optimizations, the avr backend prints this instruction as [R]CALL __udivmodhi4. Let's call this a transparent call, i.e. a call which the compiler analysis does not see.

Example

int div (int a, int b, volatile const __flash char *z)
{
    int ab;

    (void) *z;
    asm volatile ("" : "+r" (a));
    ab = a / b;
    asm volatile ("" : "+r" (ab));
    (void) *z;

    return ab;
}

Compile this with avr-gcc -S -Os -mmcu=atmega8 ... to get assembly file *.s:

div:
    movw  r30,r20
    lpm   r18,Z
    rcall __divmodhi4
    movw  r24,r22
    lpm   r18,Z
    ret

Explanation

(void) *z reads one byte from flash, and in order to use lpm instruction, the address must be in the Z register accomplished by movw r30,r20. After reading via lpm, the compiler issues rcall __divmodhi4 to perform signed 16-bit division. If this was an ordinary (non-transparent) call, the compiler would know nothing about the internal working of the callee, but as the avr backend models the call by hand, the compiler knows that the instruction sequence does not change Z and hence may use Z again after the call without any further ado. This allows for better code generation due to less register pressure, in particular z need not be saved / restored around the division.

The asm just serves to order the code: It is volatile and hence must not be reordered against the volatile read *z. And the asm must not be reordered against the division because the asm changes a and ab – at least that's what we are pretending and telling the compiler by means of the constraints. (These variables are not actually changed, but that does not matter here.)

Also, I modified the code to include memory barriers before every statement in the form of __asm volatile("" ::: "memory"); and it doesn't seem to change anything.

The division does not touch memory (it's a transparent call without memory clobber) hence the compiler machinery may reorder it against memory clobber / accesses.

If you need a specific order, then you'll have to introduce artificial dependencies like in in my example above.

In order to tell apart ordinary calls from transparent ones, you can dump the generated assembly in the .s file by means of -save-temps -dp where -dp prints insn names:

void func0 (void);

int func1 (int a, int b)
{
    return a / b;
}

void func2 (void)
{
    func0();
}

Every call that's neither call_insn nor call_value_insn is a transparent call, *divmodhi4_call in this case:

func1:
    rcall __divmodhi4    ;  17  [c=0 l=1]  *divmodhi4_call
    movw r24,r22         ;  18  [c=4 l=1]  *movhi/0
    ret                  ;  23  [c=0 l=1]  return

func2:
    rjmp func0           ;  5   [c=0 l=1]  call_insn/3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.