llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Loading/storing an 8 aligned i128 uses two `movq` instead of a single `movups`

Open LingMan opened this issue 3 years ago • 10 comments

Consider this example:

target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"

define void @foo(ptr noalias %slice.0, ptr noalias %value) unnamed_addr {
start:
  %0 = load i128, ptr %value, align 8
  store i128 %0, ptr %slice.0, align 16
  ret void
}

Godbolt: https://llc.godbolt.org/z/fMMPbacGx

Expected asm output:

foo:                                    # @foo
        movups  xmm0, xmmword ptr [rsi]
        movaps  xmmword ptr [rdi], xmm0
        ret

Actual asm output:

foo:                                    # @foo
        mov     rax, qword ptr [rsi]
        mov     rcx, qword ptr [rsi + 8]
        mov     qword ptr [rdi + 8], rcx
        mov     qword ptr [rdi], rax
        ret

Replacing the two occurrences of i128 with <2 x i64> gives the expected output instead, so the issue appears to be specific to i128. Alternatively, changing the align 8 to align 16 causes LLVM to correctly output movaps instructions.

Real world background:

I noticed that filling a slice of u128 in Rust doesn't optimize as well as filling a slice of [u64; 2].

//type T = u128;
type T = [u64; 2];
pub fn foo(slice: &mut [T], value: &T) {
    slice.fill(*value)
}

LingMan avatar Nov 17 '23 11:11 LingMan

@llvm/issue-subscribers-backend-x86

Author: None (LingMan)

Consider this example: ```llvm target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"

define void @foo(ptr noalias %slice.0, ptr noalias %value) unnamed_addr { start: %0 = load i128, ptr %value, align 8 store i128 %0, ptr %slice.0, align 16 ret void }

Godbolt: https://llc.godbolt.org/z/fMMPbacGx

Expected asm output:
```asm
foo:                                    # @<!-- -->foo
        movups  xmm0, xmmword ptr [rsi]
        movaps  xmmword ptr [rdi], xmm0
        ret

Actual asm output:

foo:                                    # @<!-- -->foo
        mov     rax, qword ptr [rsi]
        mov     rcx, qword ptr [rsi + 8]
        mov     qword ptr [rdi + 8], rcx
        mov     qword ptr [rdi], rax
        ret

Replacing the two occurrences of i128 with &lt;2 x i64&gt; gives the expected output instead, so the issue appears to be specific to i128. Alternatively, changing the align 8 to align 16 causes LLVM to correctly output movaps instructions.

Real world background:

I noticed that filling a slice of u128 in Rust doesn't optimize as well as filling a slice of [u64; 2].

//type T = u128;
type T = [u64; 2];
pub fn foo(slice: &amp;mut [T], value: &amp;T) {
    slice.fill(*value)
}

llvmbot avatar Nov 17 '23 16:11 llvmbot

Hi!

This issue may be a good introductory issue for people new to working on LLVM. If you would like to work on this issue, your first steps are:

  1. In the comments of the issue, request for it to be assigned to you.
  2. Fix the issue locally.
  3. Run the test suite locally. Remember that the subdirectories under test/ create fine-grained testing targets, so you can e.g. use make check-clang-ast to only run Clang's AST tests.
  4. Create a Git commit.
  5. Run git clang-format HEAD~1 to format your changes.
  6. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation.

If you have any further questions about this issue, don't hesitate to ask via a comment in the thread below.

llvmbot avatar Mar 22 '24 17:03 llvmbot

@llvm/issue-subscribers-good-first-issue

Author: None (LingMan)

Consider this example: ```llvm target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128" target triple = "x86_64-unknown-linux-gnu"

define void @foo(ptr noalias %slice.0, ptr noalias %value) unnamed_addr { start: %0 = load i128, ptr %value, align 8 store i128 %0, ptr %slice.0, align 16 ret void }

Godbolt: https://llc.godbolt.org/z/fMMPbacGx

Expected asm output:
```asm
foo:                                    # @<!-- -->foo
        movups  xmm0, xmmword ptr [rsi]
        movaps  xmmword ptr [rdi], xmm0
        ret

Actual asm output:

foo:                                    # @<!-- -->foo
        mov     rax, qword ptr [rsi]
        mov     rcx, qword ptr [rsi + 8]
        mov     qword ptr [rdi + 8], rcx
        mov     qword ptr [rdi], rax
        ret

Replacing the two occurrences of i128 with &lt;2 x i64&gt; gives the expected output instead, so the issue appears to be specific to i128. Alternatively, changing the align 8 to align 16 causes LLVM to correctly output movaps instructions.

Real world background:

I noticed that filling a slice of u128 in Rust doesn't optimize as well as filling a slice of [u64; 2].

//type T = u128;
type T = [u64; 2];
pub fn foo(slice: &amp;mut [T], value: &amp;T) {
    slice.fill(*value)
}

llvmbot avatar Mar 22 '24 17:03 llvmbot

@LingMan, I am newbie to the LLVM community, is this something which I can work on ? Thank you.

Unique-Usman avatar Mar 26 '24 11:03 Unique-Usman

It requires someone with some familiarity with the SelectionDAG and its legalization stages.

RKSimon avatar Mar 26 '24 11:03 RKSimon

@Unique-Usman Don't ask me. This report is the only interaction I ever had with LLVM :)

LingMan avatar Mar 26 '24 13:03 LingMan

Hi @RKSimon, I’ve been trying this issue and wanted to ask for your input.

My thinking is that although the load/store of the i128 will be broken up/expanded into two i64 load/stores during legalization, the DAG combiner pass should recognize those two load/stores are adjacent and thus combine them. I initially was looking at the DagCombiner.cpp and specifically thought these load/store DAG nodes should be combining here:

https://github.com/llvm/llvm-project/blob/f87bde2962dded71599034880f57f742ea371888/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp#L20656-L20666

But the check was failing on the isFastLd flag (in the given example) since the unaligned memory access for 16 bytes was considered “slow” for CPUs without SSE4.2 or SSE4a:

https://github.com/llvm/llvm-project/blob/ca68a14eb822d88bc32d511258f261e88dd6b7d6/llvm/lib/Target/X86/X86ISelLoweringCall.cpp#L339-L352

https://github.com/llvm/llvm-project/blob/ca68a14eb822d88bc32d511258f261e88dd6b7d6/llvm/lib/Target/X86/X86Subtarget.cpp#L284-L289

But given SSE4.2 or SSE4a, the code now correctly generates:

https://llc.godbolt.org/z/nhP9T7Gv5

Do you have any tips on how to proceed/if I’m thinking about this correctly? My next thought is that I should handle this specific case of load/storing an unaligned i128 in CombineLoad and CombineStore in X86ISelLowering if at least SSE1 is given since I don’t think I should change allowsMemoryAccess() or isMemoryAccessFast() for this case?

js324 avatar Apr 01 '24 01:04 js324

Hi everyone, I'd like to work on this issue. If it's currently unassigned, could someone please assign it to me? Thanks!

Gonzalosilvalde avatar Feb 27 '25 20:02 Gonzalosilvalde

Hi everyone , could i work on this issue ? thanks

harshasr60 avatar Oct 08 '26 11:10 harshasr60

Hello If noone is working on this issue I wolud like to work on it ? Thanks

kulkarni-aditya-01 avatar Oct 28 '26 04:10 kulkarni-aditya-01