Vector alignment in LLVM amd64 output

I am trying to use vectors inside structures with LLVM. I have the following C definition of my structure:

struct Foo
{
    uint32_t len;
    uint32_t data[32] __attribute__ ((aligned (16)));
};

and here is some LLVM code to add 42 to element number 3 of the field data:

%Foo = type { i32, <32 x i32> }

define void @process(%Foo*) {
_L1:
  %data = getelementptr %Foo* %0, i32 0, i32 1
  %vec = load <32 x i32>* %data
  %x = extractelement <32 x i32> %vec, i32 3
  %xNew = add i32 42, %x
  %vecNew = insertelement <32 x i32> %vec, i32 %xNew, i32 3
  store <32 x i32> %vecNew, <32 x i32>* %data
  ret void
}

However, the output of llc looks as if the vectors should be aligned with 128 bytes, which seems wasteful as well as incorrect (AFAIK vectors should be aligned by 16 bytes):

    .file   "process.bc"
    .text
    .globl  process
    .align  16, 0x90
    .type   process,@function
process:                                # @process
.Leh_func_begin0:
# BB#0:                                 # %_L1
    movdqa  128(%rdi), %xmm0
    pextrd  $3, %xmm0, %eax
    addl    $42, %eax
    pinsrd  $3, %eax, %xmm0
    movdqa  %xmm0, 128(%rdi)
    ret
.Ltmp0:
    .size   process, .Ltmp0-process
.Leh_func_end0:

Of course, if I changed the definition of C to also align the data field with 128 bytes, it works, but spending 124 bytes (compared to 12 when using 16-byte alignment) just seems wrong. So what is going on here?

+3
source share
1 answer

, GEP . C-, - :

#include <stdint.h>

struct Foo
{
  uint32_t len;
  uint32_t data[32] __attribute__ ((aligned (16)));
};

void foo(struct Foo *F)
{
  F->data[3] = 4;
}

clang :

; ModuleID = 'foo.c'
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
target triple = "x86_64-apple-darwin10.0.0"

%struct.Foo = type { i32, [12 x i8], [32 x i32] }

define void @foo(%struct.Foo* %F) nounwind ssp {
  %1 = alloca %struct.Foo*, align 8
  store %struct.Foo* %F, %struct.Foo** %1, align 8
  %2 = load %struct.Foo** %1, align 8
  %3 = getelementptr inbounds %struct.Foo* %2, i32 0, i32 2
  %4 = getelementptr inbounds [32 x i32]* %3, i32 0, i64 3
  store i32 4, i32* %4
  ret void
}

, :

_foo:                                   ## @foo
Leh_func_begin0:
## BB#0:
    pushq   %rbp
Ltmp0:
    movq    %rsp, %rbp
Ltmp1:
    movl    $4, 28(%rdi)
    popq    %rbp
    ret
Leh_func_end0:

, , , :

_process:                               ## @process
Leh_func_begin1:
## BB#0:                                ## %_L1
        movaps  16(%rdi), %xmm0
        pextrd  $3, %xmm0, %eax
        addl    $42, %eax
        pinsrd  $3, %eax, %xmm0
        movaps  %xmm0, 16(%rdi)
        ret

ToT, .

+2

Source: https://habr.com/ru/post/1793585/


All Articles