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

[CIR] Upstream handling of AArch64 (Arm) Neon builtins

Open banach-space opened this issue 8 months ago • 97 comments

Overview

This is an umbrella issue for upstreaming all AArch64 builtins to ClangIR.

There are enough AArch64-specific builtins that creating separate issues for each logical group is not practical. Multiple contributors can work on this in parallel, provided we coordinate to minimize overlap. To help with that, a partial list of builtins that need to be upstreamed is included below.

If you would like to contribute, please comment indicating which builtin(s) you plan to work on.

Builtins will be removed from this list as they are upstreamed.

Before beginning work, please check the table below to ensure no one else is already working on the same intrinsics. If you have any questions, please comment here and tag @banach-space.

Needed builtins

Intrinsic Group Common prefix Assignee PR(s) Status
Vector shift right and narrow vsrhn* @E00N777

(*) Special cases that are implemented in CIRGenFunction::emitAArch64BuiltinExpr in the ClangIR incubator repository - these are usually the easiest starting point.

Completed builtins

Click to expand ...
Intrinsic Group Common prefix Assignee PR(s) Status
Split vectors (BFloat16) vget{q}_lane_bf16 @fileho #186866 ✓
Set all lanes to the same value (BFloat16) vduph_lane{q}_bf16 @E00N777 #185852 #187460 ✓
Set all lanes to the same value (FP16) vdup*_f16 @neonetizen #186955 ✓
Widening multiplication + Polynomial Multiply vmull_*, vmull_high_* @pau-sum #188371 ✓
Maximum vmax_*, vmaxq_* @Xinlong-Chen #188503 ✓
Bitwise select vbsl_* @E00N777 #188449 ✓
Vector shift right vshr_* @alowqie https://github.com/llvm/llvm-project/pull/186693 ✓
Minimum vmin_*, vminq_* @YGGkk #187935 ✓
Square root vsqrt_*, vsqrtq_* @Kouunnn https://github.com/llvm/llvm-project/pull/192282 ✓
Pairwise minimum vpmin_*, vpminq_* @iamvickynguyen https://github.com/llvm/llvm-project/pull/191759 ✓
Minimum across vector vminv_*, vminvq_* @E00N777 https://github.com/llvm/llvm-project/pull/192901 ✓
Vector rounding shift right and accumulate vrsra_* @E00N777 https://github.com/llvm/llvm-project/pull/191129 ✓
Pairwise addition and widen vpaddl_*, vpaddlq_* @xiongzile https://github.com/llvm/llvm-project/pull/191845 ✓
Vector rounding shift right vrshr_* @ArfiH #185992 + https://github.com/llvm/llvm-project/pull/194229 ✓
Absolute difference vabd_*, vabdq_* @banach-space #183595 ✓
Addition across vector + Addition across vector widening vaddv_*, vaddvq_* @iamvickynguyen https://github.com/llvm/llvm-project/pull/193396 ✓
Zip elements vzip_*, vzipq_* @E00N777 #193658 https://github.com/llvm/llvm-project/pull/194311 ✓
Unzip elements vuzp_*, vuzpq_* @E00N777 https://github.com/llvm/llvm-project/pull/195591/ https://github.com/llvm/llvm-project/pull/195527 ✓
Transpose elements vtrn_*, vtrnq_* @E00N777 https://github.com/llvm/llvm-project/pull/197112 https://github.com/llvm/llvm-project/pull/197651 ✓
Vector shift left vshl_* @albertbolt1 https://github.com/llvm/llvm-project/pull/186406 https://github.com/llvm/llvm-project/pull/187516 https://github.com/llvm/llvm-project/pull/191655 ✓
Maximum across vector vmaxv_*, vmaxvq_* @Ko496-glitch https://github.com/llvm/llvm-project/pull/194401 https://github.com/llvm/llvm-project/pull/197095 ✓
Vector shift right and insert vsri_*, vsriq_* @iamvickynguyen #196776 ✓
Vector shift right and accumulate vsra_*, vsraq_* @iamvickynguyen https://github.com/llvm/llvm-project/pull/200630 ✓
Vector shift left and insert vsli_*, vsliq_* @E00N777 https://github.com/llvm/llvm-project/pull/199415 https://github.com/llvm/llvm-project/pull/198309 ✓
Maximum across vector (IEEE754 vmaxnmv_f32 @Ko496-glitch https://github.com/llvm/llvm-project/pull/199779 ✓
Pairwise maximum vpmax_*, vpmaxq_* @E00N777 https://github.com/llvm/llvm-project/pull/201495 ✓
Add @iamvickynguyen https://github.com/llvm/llvm-project/pull/202005 ✓
Polynomial Addition @iamvickynguyen https://github.com/llvm/llvm-project/pull/202005 ✓
Vector ShiftLeft And Widen @iamvickynguyen https://github.com/llvm/llvm-project/pull/204285 ✓
Widening Addition @iamvickynguyen https://github.com/llvm/llvm-project/pull/204285 ✓
Narrowing Addition @iamvickynguyen https://github.com/llvm/llvm-project/pull/204989 ✓
Narrowing Subtraction @iamvickynguyen https://github.com/llvm/llvm-project/pull/207115 ✓
Store (vst1 + vst1q) vst1 + vst1q @iamvickynguyen https://github.com/llvm/llvm-project/pull/209347 ✓
Vector arithmetic (FP16) v*_f16 @rafe-murray + @banach-space https://github.com/llvm/llvm-project/pull/190310 + https://github.com/llvm/llvm-project/pull/194865 + https://github.com/llvm/llvm-project/pull/207511 + https://github.com/llvm/llvm-project/pull/209222 ✓
Vector saturating shift right and narrow vqshrn_*, vqshrn_high_*, vqshrun_* @E00N777 https://github.com/llvm/llvm-project/pull/195085 + https://github.com/llvm/llvm-project/pull/195080 + https://github.com/llvm/llvm-project/pull/195040 + https://github.com/llvm/llvm-project/pull/209389/ ✓
vstrq_p128 @iamvickynguyen https://github.com/llvm/llvm-project/pull/212677 ✓
Fused multiply-accumulate + Fused multiply-accumulate - fp16 vfma_* @yairbenavraham #188190 https://github.com/llvm/llvm-project/pull/195602 https://github.com/llvm/llvm-project/pull/197084 https://github.com/llvm/llvm-project/pull/202337 https://github.com/llvm/llvm-project/pull/204819 https://github.com/llvm/llvm-project/pull/210359 ✓
vst2 + vst3 + vst4 @iamvickynguyen https://github.com/llvm/llvm-project/pull/212040 ✓
Saturating Addition @iamvickynguyen https://github.com/llvm/llvm-project/pull/213755 ✓
Subtract @E00N777 https://github.com/llvm/llvm-project/pull/202857 + https://github.com/llvm/llvm-project/pull/204088/ https://github.com/llvm/llvm-project/pull/207189 + https://github.com/llvm/llvm-project/pull/213658 https://github.com/llvm/llvm-project/pull/205508 ✓
Load (vld1 + vld1q) vld1 + vld1q @iamvickynguyen https://github.com/llvm/llvm-project/pull/218319 ✓
Set vector lane + Set vector lane (BF16) vset_lane_*, vsetq_lane_* @E00N777 https://github.com/llvm/llvm-project/pull/217198 ✓
Widen @E00N777 https://github.com/llvm/llvm-project/pull/204088/ + https://github.com/llvm/llvm-project/pull/217950 ✓
Conversions vcvt_*, vcvtq_* @banach-space https://github.com/llvm/llvm-project/pull/190961 https://github.com/llvm/llvm-project/pull/193273 https://github.com/llvm/llvm-project/pull/199990 https://github.com/llvm/llvm-project/pull/217263 ✓
Extract one element from vector vget_lane_*, vgetq_lane_* @Ayush3941 #186119 ✓
Vector saturating rounding shift right and narrow vqrshrn_*, vqrshrn_high_*, vqrshrun_* @Ko496-glitch + @banach-space https://github.com/llvm/llvm-project/pull/198216 https://github.com/llvm/llvm-project/pull/200113 https://github.com/llvm/llvm-project/pull/198947 https://github.com/llvm/llvm-project/pull/219241 ✓
Rounding + Rounding-v8.5 vrnd*_ *, vrndq*_ *, ... @AbdallahRashed + @iamvickynguyen https://github.com/llvm/llvm-project/pull/195021 https://github.com/llvm/llvm-project/pull/221389 WIP (v8.5: ✓)
Vector saturating shift left vqshl_* @Ko496-glitch + @iamvickynguyen https://github.com/llvm/llvm-project/pull/190728 https://github.com/llvm/llvm-project/pull/199153 https://github.com/llvm/llvm-project/pull/221507 ✓
Load (vld2 + vld3 + vld4) vld2 + vld3 + vld4 @iamvickynguyen https://github.com/llvm/llvm-project/pull/220476 ✓

Implementation requirements

  1. For each intrinsic group listed in the Arm Neon Intrinsics Reference, ensure that all variants are supported and tested (*). If some variants are missing in the ClangIR incubator repository, please implement them.

  2. Reuse the existing AArch64 builtin tests located in clang/test/CodeGen/AArch64. These tests will need to be moved to the neon subdirectory, which enables ClangIR testing. For more context, see: https://github.com/llvm/llvm-project/issues/179952.

  3. Prefer to preserve the high-level structure of CIRGenBuiltinAArch64.cpp so that switch cases and handling remain visibly consistent with ARM.cpp; limited refactors are allowed when they improve maintainability but must be explained in PR descriptions.

  4. Format tests using the pre-existing style, see intrinsics.c for reference.

References

  • Previous umbrella tickets for X86: https://github.com/llvm/llvm-project/issues/167752 + https://github.com/llvm/llvm-project/issues/167765
  • Neon overviews: https://www.arm.com/technologies/neon + https://developer.arm.com/Architectures/Neon
  • Neon ACLE: https://arm-software.github.io/acle/neon_intrinsics/

CC @andykaylor


(*) Every variant listed in the ACLE Neon reference for each intrinsic group, including all element types and vector widths (e.g., i8/16/32/64, q/duph/lanes, immediate and non-immediate forms).

banach-space avatar Mar 09 '26 09:03 banach-space

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. Check that no other contributor is working on this issue. If someone is assigned to the issue or claimed to be working on it, ping the person. After one week without a response, the assignee may be changed.
  2. Leave a comment indicating that you are working on the issue, or just create a pull request after following the steps below. Mention this issue in the description of the pull request.
  3. Fix the issue locally.
  4. 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.
  5. Create a Git commit.
  6. Run git clang-format HEAD~1 to format your changes.
  7. Open a pull request to the upstream repository on GitHub. Detailed instructions can be found in GitHub's documentation. Mention this issue in the description of the pull request.

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

llvmbot avatar Mar 09 '26 09:03 llvmbot

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

Author: Andrzej Warzyński (banach-space)

### Overview

This is an umbrella issue for upstreaming all AArch64 builtins to ClangIR.

There are enough AArch64-specific builtins that creating separate issues for each logical group is not practical. Multiple contributors can work on this in parallel, provided we coordinate to minimize overlap. To help with that, a partial list of builtins that need to be upstreamed is included below.

If you would like to contribute, please comment indicating which builtin(s) you plan to work on.

Builtins will be removed from this list as they are upstreamed.

Before beginning work, please check the table below to ensure no one else is already working on the same intrinsics. If you have any questions, please comment here and tag @banach-space.

Needed builtins

Intrinsic Group Common prefix Assignee PR(s) Status
Set vector lane vset_lane_*, vsetq_lane_* (*) vset_lane_i8 ...
Extract one element from vector vget_lane_*, vgetq_lane_* (*) vget_lane_i8 ...
Vector saturating shift left vqshl_* (*) vqshlud_n_s64, vqshld_n_u64, vqshld_n_s64
Vector rounding shift right vrshr_* (*) vrshrd_n_u64, vrshrd_n_s64
Vector rounding shift right and accumulate vrsra_* (*) vrsrad_n_u64, vrsrad_n_s64
Vector shift left vshl_* (*) vshld_n_s64, vshld_n_u64
Vector shift right vshr_* (*) vshrd_n_s64, vshrd_n_u64
Split vectors (BFloat16) vget{q}_lane_bf16 (*) vget_lane_bf16, vgetq_lane_bf16
Set all lanes to the same value (BFloat16) vduph_lane{q}_bf16 (*) vduph_lane_bf16, vduph_laneq_bf16
Set all lanes to the same value (FP16) vdup*_f16 (*) vduph_lane_f16, vduph_laneq_f16
Scalar & vector intrinsics (FP16) v*_f16 (*) vaddh_f16, vsubh_f16, vmulh_f16, vdivh_f16

(*) Special cases are implemented in CIRGenFunction::emitAArch64BuiltinExpr in the ClangIR incubator repository - these are usually the easiest starting point.

Implementation requirements

  1. For each intrinsic group listed in the Arm Neon Intrinsics Reference, ensure that all variants are supported and tested. If some variants are missing in the ClangIR incubator repository, please implement them.

  2. Reuse the existing AArch64 builtin tests located in clang/test/CodeGen/AArch64. These tests will need to be moved to the neon subdirectory, which enables ClangIR testing. For more context, see: https://github.com/llvm/llvm-project/issues/179952.

  3. Tests should follow the format used in intrinsics.c.

References

  • Previous umbrella tickets for X86: https://github.com/llvm/llvm-project/issues/167752 + https://github.com/llvm/llvm-project/issues/167765
  • Neon overviews: https://www.arm.com/technologies/neon + https://developer.arm.com/Architectures/Neon
  • Neon ACLE: https://arm-software.github.io/acle/neon_intrinsics/

llvmbot avatar Mar 09 '26 09:03 llvmbot

I have marked this as a "good first issue", but interested contributors should be aware that working on ClangIR requires some additional setup.


1. Build Clang with ClangIR enabled.

By default, Clang does not build ClangIR support. Instructions for building with ClangIR enabled can be found here.


2. After building, but before starting any other work.

Verify that you see an error when compiling a minimal test case with ClangIR enabled (-fclangir):

ClangIR code gen Not Yet Implemented

The error may be followed by additional diagnostics or a stack dump. If you do not see this error, then one of the following is likely true:

  1. Someone has already implemented this feature.
  2. You are using the wrong repository (e.g. you built the ClangIR incubator repository).
  3. The selected builtin is handled by existing shared code (for example, EmitCommonNeonSISDBuiltinExpr). In such cases, only the missing tests may need to be added.

3. Reuse and adapt the existing implementation.

The implementation already exists in the ClangIR incubator repository.

The task here is to locate the relevant code, migrate it into the main LLVM repository, and submit a pull request.

During migration, you may need to:

You will also need to create or migrate the appropriate LIT tests to exercise the new functionality.


4. How to write tests?

  1. Test ACLE intrinsics rather than builtins. Most of the intrinsics expand to calls to builtins. Since intrinsics are the user-facing interface, tests should use ACLE intrinsics (as listed in the Arm Neon Intrinsics Reference) instead of the underlying builtins.

  2. Follow the existing format. Use the same structure as the tests in intrinsics.c:


// LLVM-LABEL: @test_vduph_lane_bf16
// CIR-LABEL: @vduph_lane_bf16(
bfloat16_t test_vduph_lane_bf16(bfloat16x4_t v) {
  // CIR:

  // LLVM:
  return (...)
}
  1. Reuse and port existing AArch64 codegen tests. Existing tests for AArch64 builtins (e.g. in neon-intrinsics.c) should be reused where possible.

    • Move the relevant tests to clang/test/CodeGen/AArch64/neon/
    • Preserve the original CHECK patterns whenever possible (you may need to update the FilleCheck prefix: CHECK -> LLVM). If changes are required, please document them in the PR summary.
  2. Avoid checking attributes. Do not check attributes such as noundef. Instead, match them with {{.*}} to avoid brittle tests.

Final note

Please do not hesitate to ask for help. The purpose of marking this as a "good first issue" is to help new contributors get involved with the project.

banach-space avatar Mar 09 '26 09:03 banach-space

Hey, I would like to work on set vector lane. Thanks!

abhijeetsharma200 avatar Mar 09 '26 11:03 abhijeetsharma200

assign me Extract one element from vector

Ayush3941 avatar Mar 09 '26 13:03 Ayush3941

@banach-space I'd like to work on the Vector rounding shift right group. Thanks!

ArfiH avatar Mar 09 '26 14:03 ArfiH

I can take up vector shift left

albertbolt1 avatar Mar 09 '26 17:03 albertbolt1

Hi, I would like to work on Split vectors @banach-space

fileho avatar Mar 09 '26 19:03 fileho

@banach-space Hi, I'd like to take Vector shift right

alowqie avatar Mar 09 '26 19:03 alowqie

@banach-space I'd like to work on Set all lanes to the same value (FP16) :)

neonetizen avatar Mar 09 '26 23:03 neonetizen

@banach-space I would like to work on Vector rounding shift right and accumulate

varunkumare99 avatar Mar 10 '26 05:03 varunkumare99

Please pull LLVM at the earliest opportunity - I have landed a couple of related changes (nothing major though).

banach-space avatar Mar 10 '26 14:03 banach-space

Hi, I'd like to work on Set all lanes to the same value (BFloat16) @banach-space

E00N777 avatar Mar 11 '26 02:03 E00N777

@banach-space can you assign me Vector shift left? thanks :)

Ko496-glitch avatar Mar 11 '26 04:03 Ko496-glitch

@banach-space can you assign me Vector shift left? thanks :) Hi it’s taken can you pick up something else

albertbolt1 avatar Mar 11 '26 04:03 albertbolt1

@albertbolt1 Scalar & vector intrinsics ?

Ko496-glitch avatar Mar 11 '26 04:03 Ko496-glitch

@albertbolt1 Scalar & vector intrinsics ?

https://arm-software.github.io/acle/neon_intrinsics/advsimd.html#vector-shift-left I am planning to work on upstreaming the ones mentioned in here. I see that none of them are worked on in the clang cir

albertbolt1 avatar Mar 11 '26 04:03 albertbolt1

@albertbolt1 Vector saturating shift left ? you can assign any on the free ones

Ko496-glitch avatar Mar 11 '26 04:03 Ko496-glitch

@albertbolt1 Vector saturating shift left ? you can assign any on the free ones

Yes you can take that up, it is not taken yet

albertbolt1 avatar Mar 11 '26 04:03 albertbolt1

great

Ko496-glitch avatar Mar 11 '26 04:03 Ko496-glitch