Skip to content
This repository has been archived by the owner on Jan 13, 2025. It is now read-only.

-mpatmos-subfunction-align flag doesn't seem to work #32

Closed
Emoun opened this issue Mar 10, 2021 · 1 comment
Closed

-mpatmos-subfunction-align flag doesn't seem to work #32

Emoun opened this issue Mar 10, 2021 · 1 comment

Comments

@Emoun
Copy link
Member

Emoun commented Mar 10, 2021

Take the following LLVM program (main.s):

@_7 = global i32 7

define i32 @main()  {
entry:
  %0 = load volatile i32* @_7
  %tobool = icmp eq i32 %0, 7
  br i1 %tobool, label %if.else, label %if.then

if.else:                                          ; preds = %entry
  %1 = sub nsw i32 %0, 7
  br label %if.end
  
if.then:                                          ; preds = %entry
  %2 = add nsw i32 %0, 2
  br label %if.end

if.end:                                           ; preds = %if.else, %if.then
  %result = phi i32 [ %2, %if.then ], [ %1, %if.else ]
  ret i32 %result
}

Compiling it (patmos-llc main.s -filetype=obj -o main.o) and then inspecting the code (patmos-llvm-objdump main.o -d) will give the following:

.text:
       0:       00 00 00 38                                             li      $r0 = 56

main:
       4:       87 c2 00 00 00 00 00 00                                 li      $r1 = 0
       c:       02 82 11 00                                             lwc     $r1 = [$r1]
      10:       02 44 00 30                                             mfs     $r2 = $s0
      14:       02 02 13 e1                                             cmpneq  $p1 = $r1, 7
      18:       0c 80 00 00                                    ( $p1)   brnd    0
      1c:       06 40 00 00                                             ret
      20:       00 40 00 00                                             nop
      24:       00 42 10 07                                             sub     $r1 = $r1, 7
      28:       02 40 20 20                                             mts     $s0 = $r2

.LBB0_2:
      2c:       06 40 00 00                                             ret
      30:       00 40 00 00                                             nop
      34:       00 02 10 02                                             add     $r1 = $r1, 2
      38:       02 40 20 20                                             mts     $s0 = $r2

Notice how the first instruction in main is at address 4. This is wrong, as the default subfunction alignment is 16 (since we have not specified the alignment using -mpatmos-subfunction-align).
Trying other alignments, and trying to make additional subfunctions by using -mpatmos-max-subfunction-size, gives the same results: the alignment is ignored and always set to a 4 byte boundary.

This issue does not appear if we output assembly (patmos-llc main.s -mpatmos-subfunction-align=8). the alignment assigned to the function will be correct:

	.file	"main.s"
	.text
	.globl	main
	.align	8
	.type	main,@function
	.fstart	main, .Ltmp0-main, 8
main:                                        # @main
# BB#0:                                      # %entry
         	li	$r1 = _7
         	lwc	$r1 = [$r1]
         	mfs	$r2 = $s0
         	cmpneq	$p1 = $r1, 7
  ( $p1) 	brnd	.LBB0_2
# BB#1:                                      # %if.else
         	ret	
         	nop	
         	sub	$r1 = $r1, 7
         	mts	$s0 = $r2
.LBB0_2:                                     # %if.then
         	ret	
         	nop	
         	add	$r1 = $r1, 2
         	mts	$s0 = $r2
.Ltmp0:
.Ltmp1:
	.size	main, .Ltmp1-main

	.type	_7,@object                   # @_7
	.data
	.globl	_7
	.align	4
_7:
	.word	7                            # 0x7
	.size	_7, 4
@Emoun
Copy link
Member Author

Emoun commented Mar 29, 2021

After some investigation I found out that the flag does have an effect: It aligns a subfunction including the .size declaration.
This means the word preceding the first instruction (which declared the subfunction's size) is included in the subfunction and as such is what gets aligned. The first instruction is therefore always 4 bytes offset from the given alignment.

After talking with @schoeberl it was decided this functionality will be preserved as-is.

@Emoun Emoun closed this as completed Mar 29, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant