137 Commits

Author SHA1 Message Date
Henrich Lauko
dec90ffbc9
[CIR] Fix record layout for [[no_unique_address]] fields (#186701)
Fix two bugs in CIR's handling of `[[no_unique_address]]` fields:

- Record layout: Use the base subobject type (without tail padding)
instead of the complete object type for [[no_unique_address]] fields,
allowing subsequent fields to overlap with tail padding.
- Field access: Insert bitcasts from the base subobject pointer to the
complete object pointer after cir.get_member for potentially-overlapping
fields, so downstream code sees the expected type.
- Zero-sized fields: Handle truly empty [[no_unique_address]] fields by
computing their address via byte offsets rather than cir.get_member,
since they have no entry in the record layout.

A known gap (CIR copies 8 bytes where OG copies 5 via
`ConstructorMemcpyizer`) is noted for follow-up.
2026-04-03 19:07:25 +02:00
Andy Kaylor
5b56352757
[CIR] Implement cleanups for temporaries with automatic duration (#189754)
This implements handling for cleanup of temporary variables with
automatic storage duration. This is a simplified implementation that
doesn't yet handle the possibility of exceptions being thrown within
this cleanup scope or the cleanup scope being inside a conditional
operation. Support for those cases will be added later.
2026-04-03 08:38:06 -07:00
Andy Kaylor
49dbeca38d
[CIR] Fix missing terminators in regions with cleanup (#187604)
When a cleanup scope was created within an if operation's then or else
region and the source scope ended with a return, we would fail to
terminate the region following the cleanup scope, which surrounded the
return operation, resulting in an MLIR verification error. This change
forces the addition of terminators in the if's then and else regions.
2026-03-31 10:54:56 -07:00
Henrich Lauko
2d01df187f
[CIR] Fix reference alignment to use pointee type (#186667)
getNaturalTypeAlignment on a reference type returned pointer alignment
instead of pointee alignment. Pass the pointee type with
forPointeeType=true to match traditional codegen's
getNaturalPointeeTypeAlignment behavior. Fix applies to both argument
and return type attribute construction paths.
2026-03-21 11:14:53 +00:00
David Rivera
0ec6e1d21e
[CIR] Address Space support for GlobalOps (#179082)
Related: https://github.com/llvm/llvm-project/issues/179278,
https://github.com/llvm/llvm-project/issues/160386

Extends cir.global to accept address space attributes. Globals can now
specify either `target_address_space(N)` or
`lang_address_space(offload_*)`. Address spaces are also preserved
throughout get_global ops.
2026-03-20 14:01:20 -04: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
Erich Keane
8a25f9534a
[CIR] Implement non-odr use of reference type lowering (#185720)
This is used somewhat rarely, but is a pretty simple emission of
pointers, and ends up using infrastructure we already have.
Additionally, this is the first use of `getNaturalTypeAlignment` that
uses the `pointee` argument, so this adds the implementation there,
which includes some alignment work for CXXRecordDecls, so this
implements that as well.
2026-03-11 06:26:13 -07:00
Erich Keane
1ea11e4262
[CIR] Implement 'builtin-addressof' for 'getPointerWithAlignment' (#185684)
The 'getPointerWithAlignment' is really only called when evaluating
arguments for builtins, so the test is a touch weird as it test through
bcopy. However, this shows up in some headers, so it is important that
we support this.

This patch just adds the implementation, which mirrors classic-codegen,
except that we don't generate TBAA.
2026-03-11 06:26:02 -07:00
Erich Keane
c186db874b
[CIR] Implement 'typeid' operator lowering (#184449)
This patch adds typeid lowering, which uses a lot of the infrastructure
from dynamic_cast. However, this adds a `get_type_info` operation that
gets the type info out of a vtable pointer as well, which lets the
offset be handled by the ABI specific lowering code.
2026-03-05 06:07:13 -08:00
Akimasa Watanuki
03e2af7a65
[CIR] Fix bitfield store locations for assignment codegen (#184005)
Update bitfield-assignment codegen to emit stores at
assignment-expression source locations.
Keep `cir.set_bitfield` aligned with other store-like operations.
Prevent regressions that reattach bitfield stores to declaration-site
locations.

Add a CIR test on `clang/test/CIR/CodeGen/bitfield-assignment-loc.c`.

Fix https://github.com/llvm/llvm-project/issues/183759
2026-03-03 08:12:04 +09: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
Andy Kaylor
c78f37fdeb
[CIR] Fix dominance problems with values defined in cleanup scopes (#183810)
We currently encounter dominance verification errors when a value is
defined inside a cleanup scope but used outside the scope. This occurs
when forceCleanup() is used to exit a cleanup scope while a variable is
holding a value that was created in the scope body. Classic codegen
solved this problem by passing a list of values to spill and reload to
forceCleanup(). This change implements that same solution for CIR.

I have also aligned the ScalarExprEmitter::VisitExprWithCleanups
implementation with that of classic codegen, eliminating an extra
lexical scope. This causes temporary allocas to be created at the next
higher existing lexical scope, but I think that's OK since they would be
hoisted there anyway by a later pass.
2026-02-27 15:31:07 -08:00
Erich Keane
62d473bd01
[CIR] Implement 1 more global-ref variable fixup (#183403)
This is related to #182608, and I discovered this doing something else.
This patch adds 1 missing call/removes 1 NYI to get references to a
global reference that weren't caught elsewhere to correctly codegen.
2026-02-26 07:01:40 -08:00
David Rivera
29f32e674a
[CIR][CUDA][HIP] Emit host-side kernel calls (#179809)
Related: https://github.com/llvm/llvm-project/issues/179278,
https://github.com/llvm/llvm-project/issues/175871
2026-02-25 01:51:26 -05: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
Erich Keane
06c673ed49
[CIR] Add support for globals reference variables (#182608)
These are fairly simple, particularly if they don't need special
cleanups (which is left unimplemented), but this provides init for a
global reference variable.
2026-02-24 06:23:44 -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
Jameson Nash
d040788af6
[clang] remove unused SrcAddr parameter from performAddrSpaceCast (#179330)
The conversion code always ended up just getting the type of Src from
the Src argument itself, with no virtual users of this, so there is no
point in also providing this API hook. Fix the documentation as well,
since it seems DestAddr must have been similarly removed at some point
in the past from the API but was still documented.

Also fixes CIR to actually return the casted value!
2026-02-05 14:03:19 -05:00
Andy Kaylor
b79f5b56f0
[CIR] Add handling for non-ODR use DeclRefLValues (#178323)
This upstreams the code to handle decl ref l-values for non-odr uses.
2026-01-28 16:27:53 -08:00
Andy Kaylor
f53c2e69d9
[CIR] Upstream support for calling through method pointers (#176063)
This adds support to CIR for calling functions through pointer to method
pointers with the Itanium ABI for x86_64 targets. The ARM-specific
handling of method pointers is not-yet implemented.
2026-01-16 16:38:49 -08: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
Amr Hesham
c01c226e4a
[CIR] SubscriptExpr for VariableLengthArray (#175370)
Support the SubscriptExpr where the expr type is VariableLengthArray
2026-01-14 19:30:11 +01:00
Amr Hesham
4bd84a9f33
[CIR] Implement AtomicExpr for Aggregate expr (#173775)
Implement support for AtomicExpr for Aggregate expr
2026-01-08 18:55:31 +01:00
Andy Kaylor
4c21e460ed
[CIR] Add support for runtime data member pointer access (#171510)
This adds support for a CIR operation to represent runtime data member
access.
2025-12-11 10:19:00 -08:00
adams381
0a2e56df64
[CIR] Add support for thread-local storage (TLS) (#168662)
This commit adds full support for thread-local storage variables in
ClangIR, including code generation, lowering to LLVM IR, and
comprehensive testing.

Changes include:
- Added CIR_TLSModel enum with 4 TLS models (GeneralDynamic,
LocalDynamic, InitialExec, LocalExec) to CIROps.td
- Extended GlobalOp with optional tls_model attribute
- Extended GetGlobalOp with thread_local unit attribute
- Added verification to ensure thread_local GetGlobalOp references
globals with tls_model set
- Implemented GetDefaultCIRTLSModel() and setTLSMode() in CIRGenModule
- Updated getAddrOfGlobalVar() to handle TLS access
- Removed MissingFeatures assertions for TLS operations
- Added lowering of GetGlobalOp with TLS to llvm.threadlocal.address
intrinsic
- Added lowering of GlobalOp with tls_model to LLVM thread_local globals
- Added comprehensive test with CIR, LLVM, and OGCG checks

Known limitations (matching incubator):
- Static local TLS variables not yet implemented
- TLS_Dynamic with wrapper functions not yet implemented

Fixes #153270
2025-12-09 15:11:01 -08:00
Farzon Lotfi
dd1b4abfb7
[HLSL][Matrix] Add support for Matrix element and trunc Casts (#168915)
fixes #168737
fixes #168755

This change fixes adds support for Matrix truncations via the
ICK_HLSL_Matrix_Truncation enum. That ends up being most of the files
changed.

It also allows Matrix as an HLSL Elementwise cast as long as the cast
does not perform a shape transformation ie 3x2 to 2x3.

Tests for the new elementwise and truncation behavior were added. As
well as sema tests to make sure we error n the shape transformation
cast.

I am punting right now on the ConstExpr Matrix support. That will need
to be addressed later. Will file a seperate issue for that if reviewers
agree it can wait.
2025-12-02 19:02:25 -05:00
Henrich Lauko
04dd71cb0b
[CIR] Align inline-kind FuncOp attribute with incubator (#170050)
Switches to more efficient explicit enum property instead of a wrapped
storage, simplifying the string representation. The attribute is now
placed before the symbol name for consistency with other FuncOp
attributes. FileCheck patterns are also simplified to match only the
attributes under test.
2025-12-02 09:53:50 +01:00
Amr Hesham
a8e0afe988
[CIR] ArraySubscriptExpr on ExtVectorElementExpr (#169158)
Implement ArraySubscriptExpr support for ExtVectorElementExpr
2025-11-25 19:44:13 +01:00
Amr Hesham
5b8656ccb1
[CIR] ExtVectorElementExpr with rvalue base (#168260)
Upstream ExtVectorElementExpr with rvalue base
2025-11-20 17:13:49 +01:00
Andy Kaylor
fb2b1387fb
[CIR] Handle __asm labels on function declarations (#168149)
This updates the CIR direct callee builder code to handle the case of
calls to functions that were declared with an assembly label using
`__asm`. The implementation doesn't actually have any explicit handling
of the AsmLabelAttr. It is handled by the name mangler.

See https://reviews.llvm.org/D137073 and
https://reviews.llvm.org/D134362 for details on how this was implemented
in classic codegen. The test here is copied from
https://reviews.llvm.org/D134362 because the test in
https://reviews.llvm.org/D134362 requires a target that isn't yet
supported in CIR.
2025-11-17 09:14:19 -08:00
Andy Kaylor
0c8464330a
[CIR] Upstream handling for BaseToDerived casts (#167769)
Upstream handling for BaseToDerived casts, adding the
cir.base_class_addr operation and lowering to LLVM IR.
2025-11-17 09:09:46 -08:00
Amr Hesham
a1e523bece
[CIR] ExtVectorElementExpr with pointer to a vector (#168203)
Upstream ExtVectorElementExpr with a pointer to a vector
2025-11-16 09:13:34 +01:00
Amr Hesham
22f550bec2
[CIR] ExtVectorElementExpr with result Vector type (#167925)
Upstream ExtVectorElementExpr with result Vector type
2025-11-15 09:25:27 +01:00
Amr Hesham
9216e17fd2
[CIR] Upstream basic support for ExtVector element expr (#167570)
Upstream the basic support for the ExtVectorType element expr
2025-11-13 18:23:11 +01: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
Andy Kaylor
a22834a4d2
[CIR] Handle null base class initialization (#167023)
This adds handling for null base class initialization, but only for the
trivial case where the class is empty. This also moves
emitCXXConstructExpr to CIRGenExprCXX.cpp for consistency with classic
codegen and the incubator repo.
2025-11-12 10:13:36 -08:00
Sirui Mu
c1dc064ba0
[CIR] Add support for storing into _Atomic variables (#165872) 2025-11-05 19:32:34 +08:00
Morris Hafner
952d4b4c0b
[CIR] Fix assignment ignore in ScalarExprEmitter (#166118)
We are missing a couple of cases were we are not supposed to ignore
assignment results but did so, which results in compiler crashes. Fix
that.

Also start ignoring IgnoredExprs unless there's side effects (assignments) inside.
2025-11-05 03:35:11 +00:00
Andres-Salamanca
217f0e54c9
[CIR][NFC] Update TypeCache file to use MLIR-style camel case (#165060)
This PR updates the file `CIRGenTypeCache` to use MLIR-style camel case
naming.The change was inspired by the discussion here:
https://github.com/llvm/llvm-project/pull/164180#discussion_r2461444730
2025-10-28 19:26:33 -05:00
Amr Hesham
71b21b5391
[CIR] Move alloca from cir.try to the surrounding cir.scope (#164488)
Upstream moving the allocas from cir.try block to the surrounding
cir.scope

Issue https://github.com/llvm/llvm-project/issues/154992
2025-10-23 18:20:23 +02:00
Andy Kaylor
91d666b542
[CIR] Upstream support for calling functions via member expressions (#164518)
This adds support for calling functions via class member access
expressions.
2025-10-22 16:31:07 -07:00
Andy Kaylor
d46ef863d9
[CIR][NFC] Migrate more calls to free create functions (#164719)
This migrates some code that was calling the now-deprecated form of the
MLIR builder create function.
2025-10-22 15:43:05 -07:00
Jakub Kuderski
9a2e825c13
[clang][CIR][mlir] Migrate to free create functions. NFC. (#164656)
See
https://discourse.llvm.org/t/psa-opty-create-now-with-100-more-tab-complete/87339.

I plan to make these deprecated in
https://github.com/llvm/llvm-project/pull/164649.
2025-10-22 14:30:19 -04:00
Morris Hafner
3155b05044
[CIR] Implement inline builtin functions (#163911)
This patch implements the handling of inline builtin functions in CIR.
There is a known limitation in CIR where direct calls to shadowed inline
builtin functions are generated instead of the intrinsic. This is
expected to be fixed by the introduction of the nobuiltin attribute in a
future patch.
2025-10-22 00:17:52 +08:00
Andy Kaylor
437cad9121
[CIR] Upstream aggregate binary assign handling (#163877)
This upstreams the implementation for handling binary assignment
involving aggregate types.
2025-10-20 15:05:10 -07:00
Morris Hafner
babecd41a8
[CIR] Add support for ternary operator as lvalue (#163580)
Added support for ConditionalOperator, BinaryConditionalOperator and
OpaqueValueExpr as lvalue.

Implemented support for ternary operators with one branch being a throw
expression. This required weakening the requirement that the true and
false regions of the ternary operator must terminate with a `YieldOp`.
Instead the true and false regions are now allowed to terminate with an
`UnreachableOp` and no `YieldOp` gets emitted when the block throws.
2025-10-21 00:18:43 +07:00
Andy Kaylor
85265a93cc
[CIR] Upstream support for variable length arrays (#163297)
This adds the code needed to emit alloca operations for variable length
array local variables and the necessary calls to stacksave and
stackrestore to adjust the local stack as the array variables go in an
out of scope.
2025-10-15 13:55:18 -07:00
Andy Kaylor
4e5306745a
[CIR] Add initial support for dynamic cast (#162337)
This adds support for dynamic cast handling and generating
`cir.dyn_cast` operations and `cir.dyn_cast_info` attributes.

This does not include support for lowering the dynamic cast to LLVM IR,
which will require changes to the LoweringPrepare pass that will be made
in a future change.

This also does not yet handle dynamic cast to void or exact dynamic
casts.
2025-10-09 10:24:09 -07:00
David Rivera
3896212cea
[CIR] Implement Target-specific address space handling support for PointerType (#161028)
This PR adds support for address spaces in CIR pointer types by:

1. Introducing a `TargetAddressSpaceAttr` to represent target-specific
numeric address spaces (A Lang-specific attribute is to be implemented
in a different PR)
2. Extending the `PointerType` to include an optional address space
parameter
3. Adding helper methods in `CIRBaseBuilder` to create pointers with
address spaces
4. Implementing custom parsers and printers for address space attributes
5. Updating the LLVM lowering to properly handle address spaces when
converting CIR to LLVM IR

The implementation allows for creating pointers with specific address
spaces, which is necessary for supporting language features like Clang's
`__attribute__((address_space(N)))`. Address spaces are preserved
through the CIR representation and correctly lowered to LLVM IR.
2025-10-03 21:40:54 -04:00
Andy Kaylor
d6449b55cd
[CIR] Add support for emitting predefined expressions (#161757)
This adds support for emitting pseudo-macro expressions that represent
some form of the name of a function (such as `__func__` or
`__PRETTY_FUNCTION__`) as l-values.
2025-10-03 10:37:06 -07:00