82 Commits

Author SHA1 Message Date
Andy Kaylor
fa70ee45be
[CIR] Implement __builtin_flt_rounds and __builtin_set_flt_rounds (#190706)
This adds CIR handling for the __builtin_flt_rounds and
__builtin_set_flt_rounds builtin functions. Because the LLVM dialect
does not have dedicated operations for these, I have chosen not to
implement them as operations in CIR either. Instead, we just call the
LLVM intrinsic.
2026-04-06 17:11:49 -07:00
Yeongu Choe
228b6ae560
[CIR][CodeGen] Implement __builtin_signbit (#188433)
__builtin_signbit function checks if the sign bit of a floating-point
number is set to 0 or 1.
2026-04-06 16:11:13 -07:00
Yeongu Choe
df461c164c
[CIR][CodeGen] Implement __builtin_fpclassify (#187977)
I implemented CIR version of __builtin_fpclassify function.
2026-04-06 14:41:55 -07:00
Rahul Joshi
89d5d502e8
[NFC][LLVM] Rename IITDescriptor fields to confirm to LLVM CS (#189448)
Rename fields of `IITDescriptor` to conform to LLVM coding standards
naming conventions.
2026-03-30 16:11:56 -07:00
Jianjian Guan
b549b3378f
[CIR][RISCV][NFC] Add CIRGenBuiltinRISCV file to support RISCV builtins codegen (#186050)
This PR adds CIRGenBuiltinRISCV.cpp file for RISCV specific builtins
codegen support.
List all builtins except vector builtins which need tablegen, and mark
them as "NYI".
2026-03-25 17:27:51 +08:00
Henrich Lauko
74a5efa331
[CIR] Split BinOpOverflowOp into separate overflow-checked ops (#186653)
Replace the monolithic cir.binop.overflow operation and its
BinOpOverflowKind enum with three individual operations:
cir.add.overflow, cir.sub.overflow, and cir.mul.overflow.

This follows the same pattern used when BinOp and UnaryOp were
previously split into per-operation ops (cir.add, cir.sub, etc.),
eliminating enum dispatch and enabling per-op traits like Commutative.
2026-03-17 18:57:23 +01:00
Henrich Lauko
3bc216c29c
[CIR] Split CIR_UnaryOp into individual operations (#185280)
Split the monolithic cir.unary operation (which dispatched on a
UnaryOpKind enum) into four separate operations: cir.inc, cir.dec,
cir.minus, and cir.not.

Changes:
- Add CIR_UnaryOpInterface with getInput()/getResult() methods
- Add CIR_UnaryOp and CIR_UnaryOpWithOverflowFlag base classes
- Define IncOp, DecOp, MinusOp, NotOp with per-op folds
- Add Involution trait to NotOp for not(not(x)) -> x folding
- Replace createUnaryOp() with createInc/Dec/Minus/Not builders
- Split LLVM lowering into four separate patterns
- Split LoweringPrepare complex-type handling per unary op
- Update CIRCanonicalize and CIRSimplify for new op types
- Update all codegen files to use bool params instead of UnaryOpKind
- Remove CIR_UnaryOpKind enum and old CIR_UnaryOp definition

Assembly format change:
  cir.unary(inc, %x) nsw : !s32i, !s32i  ->  cir.inc nsw %x : !s32i
  cir.unary(not, %x) : !u32i, !u32i      ->  cir.not %x : !u32i
2026-03-14 23:50:43 +01:00
Ayokunle Amodu
48f4d31f6a
[CIR][CIRGen] Add support for __sync_* binary atomic builtins (#186026)
Adds CIRGen support for a subset of the __sync_fetch_and_* builtins,
including arithmetic (add, sub) and bitwise (and, or, xor, nand)
variants.
2026-03-12 20:12:41 -06:00
Ayokunle Amodu
4095ac92b4
[CIR][CIRGen] Upstream support for __builtin_bcopy (#185038)
This adds CIR support for the bcopy builtin.
2026-03-09 17:59:00 -06:00
Erich Keane
af0f3379ae
[CIR] Implement 'bzero' builtin lowering in terms of cir.libc.memcpy (#184835)
This showed up on a benchmark, so getting it out of the way. bzero just
does a memset-0, so this lowers to the cir.libc.memset intrinsic. Tests
added were from the classic codegen with improved check lines.
2026-03-06 06:07:28 -08:00
Andy Kaylor
bb78a0a470
[CIR] Fix __builtin_va_start handling (#184654)
We were attempting to capture a `count` argument for __builtin_va_start
but the builtin doesn't actually have that as a defined argument. This
change removes the errant handling.
2026-03-04 12:07:26 -08:00
Ayokunle Amodu
1953b87a31
[CIR][CodeGen] Upstream support for __builtin_isinf_sign (#183977)
This adds CIR codegen and lowering support for `__builtin_isinf_sign`.
2026-03-03 14:01:49 -08:00
Henrich Lauko
89a4bcf023
[CIR] Split cir.binop into separate per-operation binary ops (#184227)
Replace the single `cir.binop` operation (dispatched via a `BinOpKind`
enum) with nine distinct ops — `cir.add`, `cir.sub`, `cir.mul`,
`cir.div`, `cir.rem`, `cir.and`, `cir.or`, `cir.xor`, and `cir.max` —
each with precise type constraints and only the attributes it needs
(nsw/nuw/sat on add/sub via `BinaryOverflowOp`).

A new `BinaryOpInterface` provides uniform `getLhs`/`getRhs`/`getResult`
access for passes and analyses.

The monolithic switch-based CIRToLLVMBinOpLowering is replaced by per-op
patterns generated through the existing CIRLowering.inc TableGen
infrastructure, with shared dispatch factored into two helpers:
`lowerSaturatableArithOp` for add/sub and `lowerIntFPBinaryOp` for
div/rem.
2026-03-03 22:34:18 +01:00
David Rivera
0b88ee12dd
[CIR] Infrastructure and MemorySpaceAttrInterface for Address Spaces (#179073)
Related: https://github.com/llvm/llvm-project/issues/175871,
https://github.com/issues/assigned?issue=llvm%7Cllvm-project%7C179278,
https://github.com/issues/assigned?issue=llvm%7Cllvm-project%7C160386

- Introducing the LangAddressSpace enum with offload address space kinds
(offload_private, offload_local, offload_global, offload_constant,
offload_generic) and the LangAddressSpaceAttr attribute.


- Generalizes CIR AS attributes as MemorySpaceAttrInterface and Attaches
it to `PointerType`. Includes test coverage for valid IR roundtrips and
invalid address space parsing.

This starts a series of patches with the purpose of bringing complete
address spaces support features for CIR. Most of the test coverage is
provided in subsequent patches further down the stack. note that most of
these patches are based on: https://github.com/llvm/clangir/pull/1986
2026-02-27 23:08:20 -05:00
Benjamin Maxwell
ce952a224c
[Clang] Add __builtin_reduce_[in_order|assoc]_fadd for floating-point reductions (#176160)
This adds `__builtin_reduce_[in_order|assoc]_fadd` to expose the
`llvm.vector.reduce.fadd.*` intrinsic directly in Clang, for the full
range of supported FP types.

Given a floating-point vector `vec` and a scalar floating-point value
`acc`:

- `__builtin_reduce_assoc_fadd(vec)` corresponds to an fast/associative
  reduction
  * i.e, the fadds can occur in any order
- `__builtin_reduce_in_order_fadd(vec, acc)` corresponds to an ordered
  redunction
  * i.e, the result is as-if an accumulator was initialized with `acc` 
    and each lane was added to it in-order, starting from lane 0
2026-02-25 10:10:59 +00:00
Ayokunle Amodu
a637cde428
[CIR][NFC] Update the constructor sites of CIRGenFPOptionsRAII (#182187)
As support for RAII FP options has been upstreamed (#179121), this patch
removes `CIRGenFPOptionsRAII` from the `MissingFeatures` list and
updates its expected constructor sites.
2026-02-24 09:49:13 -08:00
Priyanshu Kumar
fab8cb48dc
[CIR]Upstream support for IITDescriptor::Pointer and Vector types (#182112) 2026-02-19 09:43:22 -08:00
adams381
34c28ba3d0
[CIR] Add non-floating-point builtin intrinsics (#178093)
## Summary

This PR adds support for non-floating-point builtin intrinsics as a
follow-up to #175233:

- Integer `abs`/`labs`/`llabs` with `cir.abs` operation
- `__builtin_unpredictable` handling  
- Integer elementwise abs support
- Tests: `builtin-rotate.c`, `pred-info-builtins.c`, updates to `libc.c`
and `builtins-elementwise.c`

## Dependency

**This PR depends on #175233 (FP math builtins) and should be merged
after it.**

The non-FP builtins were split from #175233 per reviewer feedback to
reduce PR size.

## Test plan

- [x] All CIR codegen tests pass
- [x] All CIR tests pass
2026-02-18 13:21:30 -08:00
Ayokunle Amodu
7edf569ce6
[CIR] Upstream support for setjmp & longjmp builtins (#178989)
This adds support in CIR for the setjmp & longjmp builtins.
2026-02-17 10:50:40 -08:00
Andy Kaylor
60e50a4729
[CIR] Fix handling of boolean builtin expressions (#181444)
Previously we were generating a signed 1-bit integer constant for
builtin expressions that returned a boolean value. This caused a
verification error of mismatched types when we tried to store this
constant result to a pointer-to-bool location. This change adds a check
for boolean types.
2026-02-17 09:22:11 -08:00
Sirui Mu
ee9263bc7f
[CIR] Add sync scope to atomic fetch-and-update operation (#180770)
This patch adds synchronization scope support to the `cir.atomic.fetch`
operation.

Most of the new test code in `atomic-scoped.c` is generated by an AI
agent. The generated tests are manually reviewed and verified.

Assisted-by: Copilot with GPT-5.2-Codex
2026-02-12 23:27:41 +08:00
Priyanshu Kumar
716bd041fb
[CIR]Upstream generic intrinsic emission path (#179098)
This PR upstreams the generic intrinsic emission path and tests it for
the rdpmc builtin. The incubator has llvm_unreachable("NYI") when the
intrinsic return type doesn't match. This PR adds the type coercion to
handle that case.
2026-02-11 15:05:54 -08:00
Priyanshu Kumar
0f6ee50e4e
[CIR][X86] Add support for vpshl/vpshr builtins (#179538)
This patch also adds support for fshl/fshr operations so that
vpshl/vpshr intrinsics can lower to them
Part of: #167765
2026-02-10 16:29:23 -08:00
adams381
550e0d1b0d
[CIR] Add math and builtin intrinsics support (#175233)
This PR adds support for various math and builtin intrinsics to CIR:

## Changes

1. **Floating-point math intrinsics** - sqrt, cos, exp, exp2, floor,
fabs, sin, log, log2, log10, ceil, nearbyint, rint, round, trunc,
copysign, fma, fmax, fmin, pow
2. **Inverse trig, atan2, and roundeven** - acos, asin, atan, atan2,
roundeven
3. **Elementwise builtins** - add_sat, sub_sat, abs, max, min,
bitreverse, popcount, canonicalize
4. **Integer abs family** - abs, labs, llabs and their __builtin_
variants
5. **Prediction builtins** - __builtin_unpredictable
6. **Tests for rotate builtins** - Added OGCG checks for
__builtin_rotateleft/right

All changes include CIR, LLVM lowering, and OGCG test checks to verify
correctness.
2026-02-10 13:14:21 -08:00
Timm Baeder
4344268ad6
[clang] Return std::optional from all Expr::tryEvaluate* API (#179230)
tryEvaluateString was returning an std::optional, but the other try* API
was not. Update tryEvaluateObjectSize and tryEvaluateStrLen to return an
std::optional<uint64_t>.
2026-02-09 13:37:14 +01:00
Chaitanya
1c428074ef
[CIR][AMDGPU][NFC] Add CIRGenBuiltinAMDGPU file to support AMDGPU builtins codegen (#179237)
Upstreaming ClangIR PR : https://github.com/llvm/clangir/pull/2030

This PR adds CIRGenBuiltinAMDGPU.cpp file for AMDGPU specific builtin
codegen support.
Lists out all the builtins that are currently supported for codegen in
`clang/lib/CodeGen/TargetBuiltins/AMDGPU.cpp`.
All builtins codegen are currently "NYI".
2026-02-05 10:11:43 +05:30
Chibuoyim (Wilson) Ogbonna
dc949e3966
[CIR] Upstream ClearCacheOp support for __builtin___clear_cache (#178260)
Adds CIR `ClearCacheOp` and lowers `__builtin___clear_cache` through CIR
to the LLVM `llvm.clear_cache` intrinsic. Includes codegen + lowering
support and a test.
2026-01-29 14:37:40 -08:00
adams381
501b7e9cfb
[CIR] Add MemChrOp for __builtin_char_memchr and __builtin_memchr (#175234)
This PR adds support for the memchr builtin functions:

## Changes

- Define `CIR_MemChrOp` (`cir.libc.memchr`) operation in CIROps.td
- Add builtin handling for `__builtin_char_memchr` and
`__builtin_memchr` in CIRGenBuiltin.cpp
- Add LLVM lowering to call the `memchr` library function
- Add CodeGen and IR tests with CIR, LLVM, and OGCG checks

The operation searches for a pattern byte in a memory region and returns
a pointer to the first occurrence or null.
2026-01-29 14:03:37 -08:00
Amr Hesham
4dd7535ca8
[CIR] Upstream addressof builtin (#177860)
Upstream the addressof builtin
2026-01-26 17:47:43 +01:00
Hendrik Hübner
e515529712
[CIR] Add __sync_<OP>_and_fetch builtins (#168347)
Adds support for several `__sync_<OP>_and_fetch` builtins, and several helper methods for emitting atomic fetch + arithmetic operations.

---------

Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
2026-01-16 12:23:38 +01:00
Haocong Lu
7c9c6bec15
[CIR] Add codegen for atomic fence builtin with non-const memory order (#172455)
- Support CIR codegen for follow atomic fence builtin when the memory
order is non constant: `__atomic_thread_fence` `__atomic_signal_fence`
`__c11_atomic_thread_fence` `__c11_atomic_signal_fence`
- Refactor current implementation when the memory order is constant, the
argument expression at AST is evaluated as a constant directly.
- Merge both static memory order implementation and dynamic's into one
interface `emitAtomicExprWithMemOrder`
- Add test cases that cover all kinds of memory order.
2025-12-22 14:11:07 +08:00
Hendrik Hübner
da98be133e
[CIR] Only emit FP math intrinsics when precision/errno settings allow it (#169424)
Depending on the compiler CLI options, attributes near the call site and
pragmas we might not be allowed to emit a call to an intrinsic (e.g. if
it does not set errno and we expect it to be set). This is checked by
`shouldGenerateFPMathIntrinsic` (shared with classing codegen).

This commit adds this check and additionally adds remaining cases in the
switch statement for math builtins.
2025-12-21 01:57:18 +01:00
Haocong Lu
6d6c0cc3bb
[CIR][X86] Implement lowering for _AddressOfReturnAddress builtin (#171974)
- Add new `CIR_AddrOfReturnAddrOp` and support lowering it to LLVMIR
- Add CIR CodeGen for `_AddressOfReturnAddress` X86 builtin
- Fix error return type of `FrameAddrOp`, and add missing test for
`_ReturnAddress`

Part of https://github.com/llvm/llvm-project/issues/167765
2025-12-18 14:48:19 -08:00
Haocong Lu
1e57119cbb
[CIR][X86] Implement lowering for readwritebarrier builtins (#171094)
- Support CIR codegen for follow X86 builtin: `_ReadWriteBarrier`
`_ReadBarrier` `_WriteBarrier` `__faststorefence`
- CIR dialect operations may have no results, no values will be returned
after `emitTargetBuiltinExpr` even if it executes successfully. Using
`std::optional<mlir::Value>` as return type to reslove this problem.
- Part of [#167765](https://github.com/llvm/llvm-project/issues/167765)
2025-12-11 09:01:17 -08:00
Haocong Lu
6cacbdbc38
[CIR][NFC] Rename AtomicFence to AtomicFenceOp (#171248)
This fixes missed suffix `Op` of `CIR_AtomicFence` defination and also
improves API `makeAtomicFenceValue`.
2025-12-10 09:30:55 -08:00
Hendrik Hübner
0cb237d69e
[CIR] Add AtomicFenceOp and signal/thread fence builtins and required helpers (#168346)
This PR adds the AtomicFenceOp and signal/thread fence builtins.
2025-12-05 23:12:21 +01:00
Andy Kaylor
817ab49ece
[CIR][NFC] Add infrastructure for AArch64 builtins (#170386)
This change adds the basic code structure for handling AArch64 builtins.
The structure of this code is brought over from classic codegen to make
implementing missing builtins easier. In some cases, the handling
involved too much logic for a simple NFC change, so those parts were
replaced with a MissingFeature assert.

The actual handling for all builtins is left for later changes.
2025-12-03 09:30:16 -08:00
Andy Kaylor
12ae72744c
[CIR] Upstream support for builtin_constant_p (#170354)
This upstreams the handler for the BI__builtin_constant_p function.
2025-12-02 21:15:17 +00:00
Hendrik Hübner
7e29448b4e
[CIR] Upstream var arg copy builtin (#169415)
This PR upstreams `__builtin_va_copy`, and extends the existing tests.
2025-12-02 15:28:12 +01:00
Letu Ren
91e8780424
[CIR] Upstream Builtin FloorOp (#169954) 2025-12-01 16:51:08 -08:00
Letu Ren
411a53e16f
[CIR] Upstream Builtin Exp2Op (#169152)
Add the cir::exp2 operation and handling for the related builtins.
2025-11-26 09:49:59 -08:00
Andres-Salamanca
97023fba55
[CIR] Emit ready and suspend branches for cir.await (#168814)
This PR adds codegen for `cir.await` ready and suspend. One notable
difference from the classic codegen is that, in the suspend branch, it
emits an `AwaitSuspendWrapper`(`.__await_suspend_wrapper__init`)
function that is always inlined. This function wraps the suspend logic
inside an internal wrapper that gets inlined. Example here:
https://godbolt.org/z/rWYGcaaG4
2025-11-25 17:16:21 -05:00
Hendrik Hübner
e6f60a61cd
[CIR] Add builtin operator new/delete (#168578)
This PR adds `__builtin_operator_new` and `__builtin_operator_delete`.

The implementation is taken from clang code gen.
2025-11-23 18:44:42 +01:00
adams381
5bf7e8a59a
[CIR] Upstream overflow builtins (#166643)
This implements the builtins that handle overflow.

This fixes issue https://github.com/llvm/llvm-project/issues/163888
2025-11-21 16:17:01 -08:00
Hendrik Hübner
dbac91743a
[CIR] Add NYI cases to builtin switch statement and move existing cases into functions (#168699)
This PR adds a number of cases to the switch statement in
`CIRGenBUiltin.cpp`. Some existing cases were relocated, so the order
matches the order from the switch statement in clangs codegen.
Additionally, some exisiting cases were moved to functions, to keep the
code a little cleaner. In the future, it will be easier to keep track of
which builtins have not been implemented, since there would always be a
NYI case for unimplemented builtins.
2025-11-21 09:58:07 -08:00
Jasmine Tang
1278d47e9f
[CIR] Upstream isfpclass op (#166037)
Ref commit in incubator: ee17ff67f3e567585db991cdad1159520c516bb4
 
There is a minor change in the assumption for emitting a direct callee.
In incubator, `bool hasAttributeNoBuiltin = false`
(`llvm-project/clang/lib/CIR/CodeGen/CIRGenExpr.cpp:1671`), while in
upstream, it's true, therefore, the call to finite(...) is not converted
to a builtin anymore.

Fixes #163892
2025-11-19 15:52:51 -08:00
Thibault Monnier
e02fdf0fce
[CIR] Upstream CIR codegen for vec_ext x86 builtins (#167942)
This PR upstreams the codegen for the x86 vec_ext builtins from the
incubator. It is part of #167752.
2025-11-14 15:35:30 -08:00
David Rivera
8f9071651d
[CIR] Upstream AddressSpace conversions support (#161212)
related: #160386 
Add support for address space conversions in CIR.

- Added `createAddrSpaceCast` methods to `CIRBaseBuilderTy` to handle
address space conversions
- Implemented address space conversion handling in `emitCastLValue` and
`VisitCastExpr`
- Added `performAddrSpaceCast` method to `TargetCIRGenInfo` for
target-specific address space casting
- Added `getLangTempAllocaAddressSpace` to `CIRGenModule` to get the
language-specific address space for temporary allocations
- Added a test file `address-space-conversion.cpp` to verify address
space conversion functionality
2025-11-12 19:35:45 -05:00
Morris Hafner
e974c65774
[CIR] Implement __builtin_object_size and __builtin_dynamic_object_size (#166191)
* Add cir.objsize operation to CIR dialect
* Add lowering for cir.objsize operation to LLVM dialect
* Add codegen for __builtin_object_size and
__builtin_dynamic_object_size

Note that this does not support the pass_object_size attribute yet.

---------

Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
2025-11-06 14:09:56 +01:00
Amr Hesham
8ee1803538
[CIR] Upstream Builtin ExpOp (#166061)
Upstream the Builtin ExpOp
2025-11-04 18:12:41 +00:00