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

[CIR] Upstream handling of X86 builtins

Open andykaylor opened this issue 1 year ago • 28 comments

Overview

This is an umbrella task for all X86 builtin upstreaming. There are enough X86-specific builtins that creating separate issues for each logical group is not practical. Many people can work on this in parallel, if we can successfully coordinate the work to avoid overlap as much as possible. To that end, I am adding a partial list here of the builtins that need to be upstreamed. If you would like to contribute to this task, please comment saying which builtin(s) you are working on.

I will remove builtins from this list as they are upstreamed.

Please check the table below before beginning work to make sure no one else is working on the builtins you are targeting. If you have any questions, please comment here tagging @andykaylor.

Needed builtins

Builtin group Assignee
rdtsc, rdtscp @adit4443ya
lzcnt, tzcnt @Luhaocong
undef @Thibault-Monnier
vec_ext @Thibault-Monnier
vec_set @jacquesguan
xsave, xrstor @medhatiwari
masked store @woruyu
masked load @woruyu
convert to mask @MarwanTarik
compress store @AnkitDubeycs25
gather @badumbatish
scatter @badumbatish
vinsert, shuffle, blend , perm @Thibault-Monnier

Many more builtins are needed but not yet implemented in the incubator. I will create a separate issue for that. work.

Existing incubator tests

clang/test/CIR/CodeGen/X86/*

andykaylor avatar Nov 12 '25 20:11 andykaylor

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 Nov 12 '25 20:11 llvmbot

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

Author: Andy Kaylor (andykaylor)

### Overview This is an umbrella task for all X86 builtin upstreaming. There are enough X86-specific builtins that creating separate issues for each logical group is not practical. Many people can work on this in parallel, if we can successfully coordinate the work to avoid overlap as much as possible. To that end, I am adding a partial list here of the builtins that need to be upstreamed. If you would like to contribute to this task, please comment saying which builtin(s) you are working on.

I will remove builtins from this list as they are upstreamed.

Please check the table below before beginning work to make sure no one else is working on the builtins you are targeting. If you have any questions, please comment here tagging @andykaylor.

Needed builtins

Builtin group Assignee
rdtsc, rdtscp
lzcnt, tzcnt
undef
vec_ext
vec_set
xsave, xrstor
masked store
masked load
convert to mask
compress store
gather
scatter
vinsert, shuffle, blend , perm

Many more builtins are needed but not yet implemented in the incubator. I will create a separate issue for that. work.

Existing incubator tests

clang/test/CIR/CodeGen/X86/*

llvmbot avatar Nov 12 '25 20:11 llvmbot

I have marked this as a "good first issue" but interested contributors should be aware of some special handling needed for working on ClangIR issues.

First, in order to even reproduce the problem you will need to build clang with ClangIR enabled. By default, clang does not build ClangIR support. You can find instructions for building with ClangIR enabled here.

Second, after building but before starting any other work, you should verify that you see an error when compiling the suggested minimal test case with ClangIR enabled. You should see an error (in this case, ``) when you compile the test case using the following options: -fclangir -emit-cir. The error may be followed by other errors or a stack dump. If you do not see an error, either someone else has already implemented this feature, or you are using the wrong repository (perhaps you built the ClangIR incubator repository?).

Third, the code to implement this feature is already present in the ClangIR incubator repository. The task here is to find the correct code, migrate it to the main LLVM repository, and creating a pull request to merge it there. As part of the migration process, you may need to update the code to conform to the coding standards used in the LLVM project. You will also need to create or migrate a LIT test that exercises the new code.

Don't hesitate to ask for help if you need it. The entire point of "good first issue" is to help new people get involved with our project.

andykaylor avatar Nov 12 '25 20:11 andykaylor

@Thibault-Monnier @woruyu @HendrikHuebner @siddu0660 FYI

andykaylor avatar Nov 12 '25 20:11 andykaylor

Currently open PRs:

  • https://github.com/llvm/llvm-project/pull/167125
  • https://github.com/llvm/llvm-project/pull/167945
  • https://github.com/llvm/llvm-project/pull/167948

Thibault-Monnier avatar Nov 12 '25 21:11 Thibault-Monnier

I'll also add the prefetch builtins.

HendrikHuebner avatar Nov 12 '25 21:11 HendrikHuebner

@Thibault-Monnier Can you comment here on the groups in my table that you have implemented in the PR that I asked you to split up for review?

andykaylor avatar Nov 12 '25 23:11 andykaylor

@andykaylor I am working on:

  • undef
  • vec_ext
  • vinsert, shuffle, blend , perm

Thibault-Monnier avatar Nov 13 '25 17:11 Thibault-Monnier

Hey @andykaylor, where do the _mm_getcsr and _mm_setcsr builtins fall into in your list?

Thibault-Monnier avatar Nov 13 '25 19:11 Thibault-Monnier

Hey @andykaylor, where do the _mm_getcsr and _mm_setcsr builtins fall into in your list?

@Thibault-Monnier I left those out of the table because they were already in your PR. I was probably a bit inconsistent in how I handled that, and you added more builtins to the PR while I was creating this issue, I think. Basically, I would like to avoid anyone working on the pieces that you've already prepared.

andykaylor avatar Nov 13 '25 20:11 andykaylor

Hi @andykaylor, I am interested in this work, could I try working on lzcnt, tzcnt ?

Luhaocong avatar Nov 17 '25 04:11 Luhaocong

hi, @andykaylor, can i work with rdtsc, rdtscp?

ghost avatar Nov 17 '25 21:11 ghost

hi, @andykaylor, I'm interested in CIR, can I work on vec_set?

jacquesguan avatar Nov 18 '25 07:11 jacquesguan

hello, can i take gather and scatter ?

badumbatish avatar Nov 18 '25 08:11 badumbatish

hello, @andykaylor , I would like to take masked load/store!

woruyu avatar Nov 18 '25 13:11 woruyu

i'm failing an unrelated undef builtin just by including #include <immintrin.h> and having this as the clang command

error:

# | /Users/jjasmine/Developer/igalia/llvm-project/build/lib/clang/22/include/avxintrin.h:3620:18: error: ClangIR code gen Not Yet Implemented: unimplemented X86 builtin call: __builtin_ia32_undef256
# |  3620 |   return (__m256)__builtin_ia32_undef256();

test case:

// RUN: %clang_cc1 -flax-vector-conversions=none -ffreestanding %s -triple=x86_64-unknown-linux -target-feature +avx512f -target-feature +avx512vl -fclangir -emit-cir -o %t.cir -Wall -Werror -Wsign-conversion

#include <immintrin.h>

__m256 test_mm512_i64gather_ps(__m512i __index, void const *__addr) {
  // CHECK-LABEL: test_mm512_i64gather_ps
  // CHECK: @llvm.x86.avx512.mask.gather.qps.512
  return _mm512_i64gather_ps(__index, __addr, 2); 
}

should i start by implementing undef first?

badumbatish avatar Nov 19 '25 06:11 badumbatish

Hey @badumbatish, I am already working on implementing the undef builtins in #167945. It should be ready and merged soon enough.

It's a fairly simple change, so you may just copy the changes of that PR to the CIRGenBuiltinX86.cpp file locally if you wish. That should be enough.

Thibault-Monnier avatar Nov 19 '25 09:11 Thibault-Monnier

Hi @andykaylor! I'd like to work on the xsave, xrstor builtins.

medhatiwari avatar Dec 04 '25 10:12 medhatiwari

Hi, @andykaylor, I will work on convert to mask and compress store

MarwanTarik avatar Dec 06 '25 22:12 MarwanTarik

Hi Andy, sorry for the delay! Are there any other open tests

sonar3d avatar Dec 08 '25 03:12 sonar3d