[CIR] Upstream handling of AArch64 (Arm) Neon builtins
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
-
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.
-
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.
-
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.
-
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).
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:
- 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.
- 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.
- Fix the issue locally.
- Run the test suite locally. Remember that the subdirectories under
test/create fine-grained testing targets, so you can e.g. usemake check-clang-astto only run Clang's AST tests. - Create a Git commit.
- Run
git clang-format HEAD~1to format your changes. - 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.
@llvm/issue-subscribers-good-first-issue
Author: Andrzej Warzyński (banach-space)
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
-
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.
-
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.
-
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/
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:
- Someone has already implemented this feature.
- You are using the wrong repository (e.g. you built the ClangIR incubator repository).
- 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:
- Conform to LLVM + MLIR coding standards (see also CIR Style).
- Align the implementation with the current Clang lowering logic used for direct LLVM IR generation.
You will also need to create or migrate the appropriate LIT tests to exercise the new functionality.
4. How to write tests?
-
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.
-
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 (...)
}
-
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.
-
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.
Hey, I would like to work on set vector lane. Thanks!
assign me Extract one element from vector
@banach-space I'd like to work on the Vector rounding shift right group. Thanks!
I can take up vector shift left
Hi, I would like to work on Split vectors @banach-space
@banach-space Hi, I'd like to take Vector shift right
@banach-space I'd like to work on Set all lanes to the same value (FP16) :)
@banach-space I would like to work on Vector rounding shift right and accumulate
Please pull LLVM at the earliest opportunity - I have landed a couple of related changes (nothing major though).
Hi, I'd like to work on Set all lanes to the same value (BFloat16) @banach-space
@banach-space can you assign me Vector shift left? thanks :)
@banach-space can you assign me Vector shift left? thanks :) Hi it’s taken can you pick up something else
@albertbolt1 Scalar & vector intrinsics ?
@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 Vector saturating shift left ? you can assign any on the free ones
@albertbolt1 Vector saturating shift left ? you can assign any on the free ones
Yes you can take that up, it is not taken yet
great