[LLVMdev] atomic operations for ARM

Robert Schuster theBohemian at gmx.net
Fri Mar 27 18:12:23 PDT 2009


Hi Evan,
thanks for your answer.

Evan Cheng schrieb:
>> What I want to achieve first is that llc picks this definition when it
>> finds an occurance of the @atomic.cmp.swap.i32 intrinsic.
>>
>> Therefore I wrote a basic .ll file containing only a call to this
>> function. I then let it run through "llvm-as | llc".
> 
> It would be useful if you post this.
Here is the code. It is basically a stripped down variant of
tests/Codegen/X86/Atomics-32.ll:

; RUN: llvm-as < %s | llc -march=x86

define void @test_compare_and_swap() nounwind {
entry:
  %a = malloc i32

  call i32 @llvm.atomic.cmp.swap.i32.p0i32( i32* %a, i32 100, i32 200  )

  br label %return

return:
  ret void
}

declare i32 @llvm.atomic.cmp.swap.i32.p0i32(i32*, i32, i32) nounwind

As an example. If I run 'llvm-as < Atomics-32.ll | llc -march=x86' to
generate x86 assembly I get

	.file	"<stdin>"


	.text
	.align	16
	.globl	test_compare_and_swap
	.type	test_compare_and_swap, at function
test_compare_and_swap:
	subl	$4, %esp
	movl	$4, (%esp)
	call	malloc
	movl	%eax, %ecx
	movl	$100, %eax
	movl	$200, %edx
	lock
	cmpxchgl	%edx, (%ecx)  ; <- !!!
.LBB1_1:	# return
	addl	$4, %esp
	ret
	.size	test_compare_and_swap, .-test_compare_and_swap

	.section	.note.GNU-stack,"", at progbits

Here the marked cmpxchgl instruction is the one that resulted from the
matching of atomic_cmp_swap. I know that for X86 a custom selection is
applied, since X86ISelLowering.cpp contains:

  setOperationAction(ISD::ATOMIC_CMP_SWAP, MVT::i32, Custom);

However such a line does not exist in the PowerPC implementation and as
such it is (AFAIU) being done using an properly written SDNode.

All the processor which have a proper implementation for the atomic
operation luckily have proper assembler instructions which help. In the
case of ARM it is different. AFAIK every ARM ISA below armv6 misses
proper atomic operations instructions. That is why they use this kernel
hack (switch to kernel mode, disable interrupts, make the cmp swp). So
in my case I just need to call a function and that it. There is also no
need to use a special argument format (like 8 bit immediate). Whatever
fits for function argument is OK.

For me as a beginner with LLVM these differences make it hard to find a
start.

>>With the above definition I expected "do_something" to appears in the
>> assembler output when -march=arm. Unfortunately atm I still get the
>> error, telling me that the selection failed:
> It's hard to guess what the problem is from this. Are you able to step  
> through the code in ARMGenDAGISel.inc to see why it fails to match?
I will do this and report back. However I thought that I just made a
mistake in the definition of ARM_ATOMIC_CMP_SWAP. Something which only
someone does who is not yet versed with these things. :)

Do you think the definition should work as expected by me?

Regards
Robert

-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 260 bytes
Desc: OpenPGP digital signature
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20090328/eff0c3ba/attachment.sig>


More information about the llvm-dev mailing list