163 Commits

Author SHA1 Message Date
Andy Kaylor
4a5da64759
[CIR][NFC] Minor cleanups to missing feature markers (#187754)
This fixes a few places where MissingFeatures asserts were incorrect,
extends the text of two errorNYI diagnostics to disambiguate them, and
fixes a typo in an adjacent comment.
2026-03-20 18:00:21 +00:00
Amr Hesham
c630b09af7
[CIR][NFC] Remove NYI checks in ternary with cleanup (#186870)
We added those checks when CleanupScopeOp is used to emit an error
message in this edge case until we fix it. Now it's already fixed, and
we don't need to keep the NYI
2026-03-18 20:35:08 +01:00
Erich Keane
1b904e948a
[CIR][NFC] Unify the 'null data member attr' getters (#186876)
In preperation of actually lowering data members as fields to a record
type, this patch does a minor refactor to make their single current use
have a slightly simpler interface. This will prevent us from having to
copy/paste this later.

Also, this patch removes a pair of now-orphaned builders, instead
preferring to use the ones that come from the parent builder type.
2026-03-18 06:28:43 -07: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
Henrich Lauko
e3baec4bfb
[CIR] Remove cir.unary(plus, ...) and emit nothing for unary plus (#185278)
Traditional codegen never emits any operation for unary plus — it just
visits the subexpression as a pure identity at the codegen level. Align
CIRGen with this behavior by removing Plus from UnaryOpKind entirely
and having VisitUnaryPlus directly visit the subexpression with the
appropriate promotion/demotion handling.
2026-03-14 20:54:44 +01:00
Amr Hesham
c6128e2fb3
[CIR] Fix Codegen for Comparison of Complex bin ops (#185316)
Fix Codegen for Comparison between two Complex bin ops
2026-03-10 17:11:23 +01:00
Henrich Lauko
71f5e43bc6
[CIR] Fix operator-precedence bugs in assert conditions (#185119)
Due to && binding tighter than ||, asserts of the form
assert(A || B && "msg") always pass when A is true. Add
parentheses so the string message is properly attached:
assert((A || B) && "msg").
2026-03-07 08:07:21 +01:00
Amr Hesham
ce47e259bc
[CIR] Implement OpenMPRequiredSimdAlign UnaryExpr (#184214)
Implement the OpenMPRequiredSimdAlign kind of UnaryExprOrTypeTraitExpr
2026-03-05 19:12:24 +01:00
Amr Hesham
f986523c59
[CIR] Fix Codegen for Complex & Scalar comparisons (#184006)
Fix Codegen for emitting comparisons between Complex & Scalar
2026-03-04 21:14:58 +01:00
Amr Hesham
56ed7485b7
[CIR][NFC] Remove unnecessary call to clangCmpToCIRCmp (#184217)
Remove unnecessary call to clangCmpToCIRCmp and use the result from the
previous call L1165
2026-03-04 20:51:34 +01: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
Erich Keane
b5baf5e062
[CIR] Implement func-ptr/void-ptr addition/subtraction/inc/dec. (#184254)
As a GNU extension, clang supports math on void* and function pointers
in C mode only. From a CIR perspective, it makes sense to leave these
types in the IR, since it might be useful to do analysis.

During lowering, we already properly lower these to a size-1 element, so
there is no changes that need to happen besides letting this get through
CIR generation. This patch does that, plus adds some tests.
2026-03-03 11:29:27 -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
Gauravsingh Sisodia
08b0f39dcf
[CIR][CodeGen] Implement VisitImplicitValueInitExpr in CIRGenExprScalar.cpp (#182256)
MRE:
```cpp
#include <vector>

std::vector<int> V;
```
```shell
$ ./bin/clang++ -fclangir -c test.cpp
error: ClangIR code gen Not Yet Implemented: ScalarExprEmitter: implicit value init
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script.
Stack dump:
....
```
This PR fixes the crash by upstreaming:

c1e076e803/clang/lib/CIR/CodeGen/CIRGenExprScalar.cpp (L397-L399)
2026-02-24 11:11:29 -08:00
Amr Hesham
ab14dab5ec
[CIR] Fix Codegen for Vector logical op with FP (#182761)
Previously, we perform nq comparison between LHS, RHS, and zero attr
with the type of the logical Op, but in the case of a vector with FP
element type, the verifier will fail because we perform a comparison of
a vector of FP and a vector of int (the result type of the logical op in
this case is vector of int)
2026-02-24 19:50:35 +01: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
74f2ab4942
[CIR] Handle sizeof-pack lowering: (#182973)
This is just a compile time constant, so we just have to make sure we
get the value and lower it in the correct type, otherwise this is a
fairly trivial task.
2026-02-24 06:24:10 -08:00
Amr Hesham
990996358d
[CIR] Represent Cleanups with Cleanup scopes (#180276)
Represent the Cleanups stack explicitly with Cleanup scopes
2026-02-23 18:03:37 +01:00
Amr Hesham
0d0012ec63
[CIR] Emit cir.zero directly in Vector logical ops (#182703)
Emit `cir.zero` directly instead of `vec.create<n, 0>` that will be
folded to `cir.const_vector<n, 0>` later
2026-02-22 17:03:54 +01:00
Andy Kaylor
0e31e6aff8
[CIR] Fix type handling with TypeTraitExpr (#181944)
There were some cases when compiling a C program where
VisitTypeTraitExpr would return a cir.bool value for a TypeTraitExpr
that required an integer. This was happening because we were checking
`e->isStoredAsBoolean` which returns a result that is inconsistent with
the expression type for C compilation units.

This change fixes the handling and also adds support for other
non-boolean type trait expressions.
2026-02-18 09:36:09 -08:00
Farzon Lotfi
5f4543fbaf
[HLSL][Matrix] introduce MatrixElementExpr as accessor and swizzle operator (#171225)
fixes #159438

This patch adds `MatrixElementExpr`, a new AST node for HLSL matrix
element and swizzle access (e.g. M._m00, M._11_22_33).

It introduces a shared `ElementAccessExprBase` used by both matrix and
vector swizzle expressions, updates Sema to parse and validate
zero-based and one-based accessors, detects duplicates for l-value
checks, and emits improved diagnostics. CodeGen is updated to lower
scalar and multi-element accesses consistently, and full AST
serialization, dumping, and tooling support is included. This
implementation reflects the updated
[RFC](https://github.com/llvm/wg-hlsl/pull/357/files) for HLSL matrix
accessor semantics.
2026-02-06 13:16:49 -05: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
1f8a3f2fdb
[CIR] Fix a problem with createSub default overflow behavior (#179731)
When the createSub function was added in CIRBaseBuilderTy, the default
for the overflow behavior parameter was set to
OverflowBehavior::Saturated. This led to incorrect behavior for
subtraction when the -fwrapv option was used, but we didn't have a test
for that so it wasn't noticed.

This change adds a test and corrects the behavior.
2026-02-04 12:36:59 -08:00
Andy Kaylor
542e9b94ab
[CIR] Upstream support for pointer-to-method casts (#178004)
This adds support in CIR for handling based-to-derived and
derived-to-base casts of pointer-to-member values that point to member
functions.

Co-authored-by: Sirui Mu <msrlancern@gmail.com>
2026-01-26 17:27:35 -08:00
Andy Kaylor
d29aa30fc3
[CIR] Upstream support for null and virtual method pointers (#176522)
This upstreams support for pointer-to-member value representations of
null and virtual member pointers and the lowering of virtual member
pointers.
2026-01-23 14:07:53 -08:00
Andrzej Warzyński
aca0df927f
[Clang][CIR] Implement CIRGen logic for __builtin_bit_cast (#176782)
NOTE: This PR upstreams code from
  * https://github.com/llvm/clangir.

This logic was originally implemented by Sirui Mu in
https://github.com/llvm/clangir/pull/762. Further
modification were made by other ClangIR contributors.

Co-authored-by: Sirui Mu <msrlancern@gmail.com>
2026-01-22 18:24:58 +00:00
Andy Kaylor
7bee6c16e6
[CIR] Upstream handling for member pointer bool and reinterpret casts (#175996)
This change upstreams the code to generate CIR for bool casts and
reinterpret casts involving member pointer types and the code to lower
these casts for the Itanium C++ ABI.
2026-01-20 18:59:16 +00:00
Andres-Salamanca
ba2eaa9f61
[CIR] Upstream support for coroutine co_yield expression (#173162)
This PR upstreams support for the co_yield expression by emitting a
cir.await op with the yield kind.
2026-01-14 22:04:07 -05:00
Andy Kaylor
02984cf61b
[CIR] Attempt to fold casts and unary ops during codegen (#175675)
This change introduces basic folding of casts and unary ops as they are
created this is needed in order to allow later codegen pieces, such as
builtin handlers, to more easily identify and examine constant operands.
For example, many X86 builtin functions use a default mask operand of
-1, which was previously generated as a constant 1 and a unary minus.

In some cases, the folding process leaves behind unused constant
operations, so I am also added a simple change to the canonicalize pass
to remove unused constants. We had other places where unused constants
were being generated already, and this change cleans those up too.
2026-01-13 15:31:30 -08:00
Andy Kaylor
e21fcc2e18
[CIR] Upstream handling for data member pointer casts (#171950)
This adds the CIR basic handling for casts of data member pointers. Cast
to bool and null, as well as member function pointer casts will be
handled in followup PRs.
2026-01-09 23:35:15 +00:00
Amr Hesham
aa5bcffa06
[CIR] Implement part of ConstantExpr support for ScalarExpr (#173009)
Implement part of the ConstantExpr support for the ScalarExpr
2026-01-06 19:18:23 +01:00
Amr Hesham
46902a71f3
[CIR] Add sizeof for fixed vector type (#172861)
Add sizeof for fixed vector type and Mark sizeof & alignof as NYI for
scalable Vector as it depends on using llvm.vscale not just the vector
size
2025-12-20 19:30:27 +01:00
Andy Kaylor
bae033b906
[CIR] Add support for null data member pointers (#171945)
This adds the CIR support for handling null data member pointer values.
2025-12-18 16:24:04 -08:00
Amr Hesham
0009536bac
[CIR] Add support for EmbedExpr for ScalarExpr (#172088)
Add support for the EmbedExpr for ScalarExpr
2025-12-15 20:48:51 +01:00
Amr Hesham
c3a084933f
[CIR] Add support for the ArrayTypeTraitExpr (#171710)
Add support for the ArrayTypeTraitExpr
2025-12-13 12:44:55 +00:00
Amr Hesham
95e4dc62b1
[CIR] Add support for the RequiresExpr (#171818)
Add support for the RequiresExpr
2025-12-13 13:01:19 +01:00
Amr Hesham
366f3ac144
[CIR] Add support for the ConceptSpecializationExpr (#171824)
Add support for the ConceptSpecializationExpr
2025-12-12 18:52:36 +01:00
Amr Hesham
44aec0e768
[CIR] Add support for TypeTraitExpr with bool result (#171687)
Add support for the TypeTraitExpr with a boolean result
2025-12-12 18:19:42 +01:00
Amr Hesham
dbd0122cf5
[CIR] Add support the ChooseExpr for scalar (#171882)
Add support the ChooseExpr for scalar expr
2025-12-12 17:42:30 +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
Amr Hesham
ec4aba3821
[CIR] Add support for ExpressionTraitExpr (#171634)
Add support for the ExpressionTraitExpr
2025-12-10 20:37:54 +01:00
Amr Hesham
2acefcd9d5
[CIR] Add support for SourceLocExpr (#171492)
Add support for the SourceLocExpr
2025-12-10 15:50:44 +01:00
Amr Hesham
3ba0ff6139
[CIR][NFC] Remove duplicate VisitCXXNoexceptExpr (#171624)
Remove duplicate VisitCXXNoexceptExpr to fix building ClangIR
2025-12-10 15:27:18 +01:00
Amr Hesham
a7aa852ee5
[CIR] Upstream CXXNoexceptExpr (#171462)
Upstream the support for CXXNoexceptExpr
2025-12-10 15:06:25 +01:00
Andy Kaylor
87bf5ee238
[CIR] Add basic support for data member pointers (#170939)
This adds the minimum support for C++ data member pointer variables.
2025-12-09 12:55:47 -08:00
Andy Kaylor
e6145e8709
[CIR][NFC] Add stubs for missing visitors in ScalarExprEmitter (#171222)
This adds stubs that issue NYI errors for any visitor that is present in
the ClangIR incubator but missing in the upstream implementation. This
will make it easier to find to correct locations to implement missing
functionality.
2025-12-09 09:01:16 -08:00
Andres-Salamanca
762a171b3d
[CIR] Upstream support for cir.indirectbr (#169967)
This PR upstreams support for the `cir.indirectBr` operation, which is
used to implement GCC’s labels-as-values `indirect goto`.
To ensure correct lowering, we introduce precise bookkeeping to
associate each `block_address` operation with its corresponding `label`
op. This is required because a `block_address` may be emitted before the
`label` it refers to. In such cases, the reference is deferred and later
resolved by `resolveBlockAddresses`, which guarantees that all
`indirectBr` successors are emitted in the correct and fully resolved
order.
2025-12-05 16:12:37 -08:00
Amr Hesham
9f9e15f715
[CIR] Upstream SizeOf for VariableArrayType (#169993)
Upstream SizeOf support for VariableArrayType
2025-12-03 17:17:56 +01:00