437 Commits

Author SHA1 Message Date
Sirui Mu
91569fa030
[CIR][NFC] Use Op::create to create CIR operations in CIRGenBuilder (#154540) 2025-08-21 09:46:45 +08:00
Andy Kaylor
59b33242af
[CIR][NFC] Fix warning in MemOrder lowering (#154609)
This fixes a warning about having a default case in a fully covered enum
switch statement.
2025-08-20 14:30:22 -07:00
Sirui Mu
318b0dda7c
[CIR] Add atomic load and store operations (#153814)
This patch adds support for atomic loads and stores. Specifically, it
adds support for the following intrinsic calls:

- `__atomic_load` and `__atomic_store`;
- `__c11_atomic_load` and `__c11_atomic_store`.
2025-08-20 21:49:04 +08:00
Morris Hafner
b01f05977c
[CIR] Add support for string literal lvalues in ConstantLValueEmitter (#154514) 2025-08-20 13:30:21 +02:00
Morris Hafner
0989ff5de8
[CIR] Add constant attribute to GlobalOp (#154359)
This patch adds the constant attribute to cir.global, the appropriate
lowering to LLVM constant and updates the tests.

---------

Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
2025-08-20 12:53:00 +02:00
Morris Hafner
3b9664840b
[CIR] Implement__builtin_va_arg (#153834)
Part of https://github.com/llvm/llvm-project/issues/153286.
Depends on https://github.com/llvm/llvm-project/pull/153819.

This patch adds support for __builtin_va_arg by adding the cir.va.arg
operator. Unlike the incubator it doesn't depend on any target specific
lowering (yet) but maps to llvm.va_arg.
2025-08-20 12:52:11 +02:00
Morris Hafner
088555cf6b
[CIR] Add support for base classes in type conversion safety check (#154385)
This patch enables the record layout computation of types that are
derived more than once.
2025-08-20 12:44:26 +02:00
Amr Hesham
018c5ba161
[CIR] Implement MemberExpr with VarDecl for ComplexType (#154307)
This change adds support for MemberExpr with VarDecl ComplexType

Issue: https://github.com/llvm/llvm-project/issues/141365
2025-08-20 10:33:08 +02:00
Andres-Salamanca
577ee97d4e
[CIR] Fix access to bitfields inside a union (#154398)
This PR fixes the access to bitfields inside a union.
Previously, we were using a `getMemberOp` to access the field, but
because it is a union, `getMemberOp` would always use index `0`.

For example, given:
```c
typedef union {
  int x;
  int y : 4;
  int z : 8;
} demo;
```
```mlir
!rec_demo = !cir.record<union "demo" {!s32i, !u8i, !u8i}>
```

In the case of:

```c
d.y = 2;
```

It would generate:

```mlir
cir.get_member %0[0] {name = "y"} : !cir.ptr<!rec_demo> -> !cir.ptr<!s32i>
```

with a return type of `!s32i`, when it should be `!u8i`.
the get_member verifier would detect that the return type does not match
the `y` member.
To fix this, we now use `bitcast` to get the start of the union.
2025-08-19 17:43:13 -05:00
Andy Kaylor
6747139bc2
[CIR] Use zero-initializer for partial array fills (#154161)
If an array initializer list leaves eight or more elements that require
zero fill, we had been generating an individual zero element for every
one of them. This change instead follows the behavior of classic
codegen, which creates a constant structure with the specified elements
followed by a zero-initializer for the trailing zeros.
2025-08-19 12:14:05 -07:00
Sirui Mu
10d193bf5e
[CIR][NFC] Fix regression by #153819 (#154346)
This patch fixes a regression introduced by #153819. The evaluation
order of the arguments to `emitVAStart` is unspecified, but the test
requires the arguments to be evaluated in left-to-right order.

It's a bit strange that the pre-merge checks did not catch this. The
tests failed on my local machine, which runs Fedora 42 with gcc 15.2.1 .
2025-08-19 23:32:02 +08:00
Erich Keane
dab8c88f15
[OpenACC] Implement 'firstprivate' clause copy lowering (#154150)
This patch is the last of the 'firstprivate' clause lowering patches. It
takes the already generated 'copy' init from Sema and uses it to
generate the IR for the copy section of the recipe.

However, one thing that this patch had to do, was come up with a way to
hijack the decl registration in CIRGenFunction. Because these decls are
being created in a 'different' place, we need to remove the things we've
added. We could alternatively generate these 'differently', but it seems
worth a little extra effort here to avoid having to re-implement
variable initialization.
2025-08-19 06:02:10 -07:00
Amr Hesham
5581e34bd9
[CIR] Implement MemberExpr support for ComplexType (#154027)
This change adds support for the MemberExpr ComplexType

Issue: https://github.com/llvm/llvm-project/issues/141365
2025-08-19 10:32:22 +02:00
Morris Hafner
b5e5794534
[CIR] Implement Statement Expressions (#153677)
Depends on #153625

This patch adds support for statement expressions. It also changes
emitCompoundStmt and emitCompoundStmtWithoutScope to accept an Address
that the optional result is written to. This allows the creation of the
alloca ahead of the creation of the scope which saves us from hoisting
the alloca to its parent scope.
2025-08-19 10:11:15 +02:00
Morris Hafner
b44e47a68f
[CIR] Upstream __builtin_va_start and __builtin_va_end (#153819)
Part of #153286
2025-08-19 09:16:11 +02:00
Iris Shi
cc68e45343
[CIR] Implement codegen for inline assembly without input and output operands (#153546)
- Part of #153267


https://github.com/llvm/clangir/blob/main/clang/lib/CIR/CodeGen/CIRAsm.cpp
2025-08-18 18:54:24 -07:00
Andy Kaylor
7ac4d9bd53
[CIR] Add support for calling virtual functions (#153893)
This change adds support for calling virtual functions. This includes
adding the cir.vtable.get_virtual_fn_addr operation to lookup the
address of the function being called from an object's vtable.
2025-08-18 15:56:33 -07:00
Andres-Salamanca
916218ccbd
[CIR] Upstream GotoOp (#153701)
This PR upstreams `GotoOp`. It moves some tests from the `goto` test
file to the `label` test file, and adds verify logic to `FuncOp`. The
gotosSolver, required for lowering, will be implemented in a future PR.
2025-08-18 10:25:40 -05:00
Erich Keane
340fa3e1bb
[OpenACC] Implement firstprivate lowering except init. (#153847)
This patch implements the basic lowering infrastructure, but does not
quite implement the copy initialization, which requires #153622.

It does however pass verification for the 'copy' section, which just
contains a yield.
2025-08-18 06:33:40 -07:00
Morris Hafner
df0e9f3a2d
[CIR] Implement __builtin_return_address and __builtin_frame_address (#153698)
This adds ReturnAddrOp and FrameAddrOp that represent
__builtin_return_address and __builtin_frame_address and the respective
lowering to LLVM parts.

---------

Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
2025-08-16 00:47:04 +02:00
Andy Kaylor
0cd35e7afd
[CIR] Add cir.vtable.get_vptr operation (#153630)
This adds support for the cir.vtable.get_vptr operation and uses it to
initialize the vptr member during constructors of dynamic classes.
2025-08-15 15:14:51 -07:00
Erich Keane
15d7a95ea9
[CIR] Refactor recipe init generation, cleanup after init (#153610)
In preperation of the firstprivate implementation, this separates out
some functions to make it easier to read.

Additionally, it cleans up the VarDecl->alloca relationship, which will
prevent issues if we have to re-use the same vardecl for a future
generated recipe (and causes concerns in firstprivate later).
2025-08-15 06:41:42 -07:00
Andy Kaylor
a1529cd85a
[CIR] Add index support for global_view (#153254)
The #cir.global_view attribute was initially added without support for
the optional index list. This change adds index list support. This is
used when the address of an array or structure member is used as an
initializer.

This patch does not include support for taking the address of a
structure or class member. That will be added later.
2025-08-14 15:14:12 -07:00
Morris Hafner
e56ae9651b
[CIR][NFC] Add Symbol Table to CIRGenFunction (#153625)
This patchs adds a symbol table to CIRGenFunction plus scopes and
insertions to the table where we were missing them previously.
2025-08-14 22:53:09 +02:00
Iris Shi
dc0becc4d0
[CIR] Add InlineAsmOp lowering to LLVM (#153387)
- Part of #153267

Added support for lowering `InlineAsmOp` directly to LLVM IR

---------
Co-authored-by: Morris Hafner <mhafner@nvidia.com>
2025-08-14 17:48:14 +00:00
Iris Shi
9a28783f5d
[CIR] Add InlineAsmOp (#153362)
- Part of #153267

---------

Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
Co-authored-by: Morris Hafner <mmha@users.noreply.github.com>
2025-08-14 17:34:38 +00:00
Andy Kaylor
45066c2937
[CIR] Add lowering for the cir.vtable.address_point operation (#153243)
This adds support for lowering the cir.vtable.address_point operation to
the LLVM dialect, as well as type converter support for the cir.vptr
type.
2025-08-14 10:07:54 -07:00
Andy Kaylor
f2d76b58f8
[CIR] Use a loop for array initialization (#153499)
This updates the array initialization loop to use a do..while loop
rather than a fully serialized initialization. It also allows the
initialization of destructed objects when exception handling is not
enabled.

Array initialization when exception handling is enabled remains
unimplemented, but more precise messages are now emitted.
2025-08-13 16:14:32 -07:00
Morris Hafner
0045bfca9c
[CIR} Add support for static member variable instantiation (#153200)
This patch handles both implicit and explicit template instantiations of
template class static member variables.
2025-08-13 18:03:05 +02:00
Amr Hesham
dc84f3aea8
[CIR] Upstream builtin FAbs op (#151750)
Upstreaming FAbsOp as a prerequisite for upstreaming ComplexDivOp
2025-08-13 17:57:06 +02:00
Andres-Salamanca
4b112d23a5
[CIR] Upstream LabelOp (#152802)
This PR introduces the `LabelOp`, which is required for implementing
`GotoOp` lowering in the future.
Lowering to LLVM IR is **not** included in this patch, since it depends
on the upcoming `GotoSolver`.

The `GotoSolver` traverses the function body, and if it finds a
`LabelOp` without a matching `GotoOp`, it erases the label.
This means our implementation differs from the classic codegen approach,
where labels may be retained even if unused.

Example:
https://godbolt.org/z/37Mvr4MMr
2025-08-13 09:52:00 -05:00
Sirui Mu
331a5db9de
[CIR] Add initial support for atomic types (#152923) 2025-08-13 09:22:48 +08:00
Sirui Mu
7b8189aab8
[CIR] Add CIRGen for pseudo destructor calls (#153014) 2025-08-13 09:21:40 +08:00
Michael Liao
c1f797ea6d [CIR] Fix build due to the improvement after 91cdd35008e9 2025-08-12 13:42:12 -04:00
Andy Kaylor
54f53c988d
[CIR] Introduce the CIR global_view attribute (#153044)
This change introduces the #cir.global_view attribute and adds support
for using that attribute to handle initializing a global variable with
the address of another global variable.

This does not yet include support for the optional list of indices to
get an offset from the base address. Those will be added in a follow-up
patch.
2025-08-12 10:02:00 -07:00
Andy Kaylor
7f195b36ee
[CIR] Initialize vptr in dynamic classes (#152574)
This adds support for initializing the vptr member of a dynamic class in
the constructor of that class.

This does not include support for lowering the
`cir.vtable.address_point` operation to the LLVM dialect. That handling
will be added in a follow-up patch.
2025-08-12 10:00:38 -07:00
Andy Kaylor
7f22f5bac1
[CIR] Introduce more cleanup infrastructure (#152589)
Support for normal cleanups was introduced with a simplified
implementation compared to what's in the incubator (which corresponds
closely to the classic codegen implementation).

This change introduces more of the infrastructure that will later be
needed to handle non-trivial cleanup cases, including exception
handling.
2025-08-12 10:00:13 -07:00
Amr Hesham
475aa1b1a1
[CIR] CompoundAssignment from ComplexType to ScalarType (#152915)
This change adds support for the CompoundAssignment for ComplexType and
updates our approach for emitting bin op between Complex & Scalar

https://github.com/llvm/llvm-project/issues/141365
2025-08-12 18:01:31 +02:00
Amr Hesham
dde474cfc4
[CIR] Make ClangIR compatible with latest nested name specifier AST representation (#152846)
After AST representation, new modifications landed in
(https://github.com/llvm/llvm-project/pull/147835). ClangIR requires
some changes in how we use Clang AST to be compatible with the new
changes
2025-08-11 20:08:37 +02:00
Sirui Mu
b2cdd80411
[CIR] Add support for __builtin_assume_aligned (#152152)
This patch upstreams CIRGen and LLVM lowering support for the
`__builtin_assume_aligned` builtin function.
2025-08-10 12:25:45 +08:00
Michael Liao
c9b6242771 [CIR] Fix build after the improved nested name specifier AST repr (91cdd35008e9) 2025-08-09 16:38:49 -04:00
Andy Kaylor
ca52d9b8be
[CIR] Upstream EHScopeStack memory allocator (#152215)
When the cleanup handling code was initially upstreamed, a SmallVector
was used to simplify the handling of the stack of cleanup objects.
However, that mechanism won't scale well enough for the rate at which
cleanup handlers are going to be pushed and popped while compiling a
large program. This change introduces the custom memory allocator which
is used in classic codegen and the CIR incubator.

Thiis does not otherwise change the cleanup handling implementation and
many parts of the infrastructure are still missing.

This is not intended to have any observable effect on the generated CIR,
but it does change the internal implementation significantly, so it's
not exactly an NFC change. The functionality is covered by existing
tests.
2025-08-07 12:42:51 -07:00
Andy Kaylor
1e2e903684
[CIR] Add VTableAddrPointOp (#148730)
This change adds the definition of VTableAddrPointOp and the related
AddressPointAttr to the CIR dialect, along with tests for the parsing
and verification of these elements.

Code to generate this operation will be added in a later change.
2025-08-07 11:25:40 -07:00
Amr Hesham
093395ca6b
[CIR] Mul CompoundAssignment support for ComplexType (#152354)
This change adds support for Mul CompoundAssignment for ComplexType

https://github.com/llvm/llvm-project/issues/141365
2025-08-07 19:42:15 +02:00
gitoleg
d97f0e9364
[CIR] add support for file scope assembly (#152093)
This PR adds a support for file scope assembly in CIR.
2025-08-07 10:12:58 -07:00
Amr Hesham
ed9a552563
[CIR][NFC] Fix typo in ComplexRangeKind comment (#152535)
Fix typo in ComplexRangeKind comment

Catched in https://github.com/llvm/clangir/pull/1779
2025-08-07 18:55:59 +02:00
Amr Hesham
4c9bb65639
[CIR] Plus & Minus CompoundAssignment support for ComplexType (#150759)
This change adds support for Plus & Minus CompoundAssignment for
ComplexType

https://github.com/llvm/llvm-project/issues/141365
2025-08-06 18:13:48 +02:00
Morris Hafner
4f166513df
[CIR] Upstream support for variable template specializations (#151069) 2025-08-06 16:43:42 +02:00
Sirui Mu
13600c72ce
[CIR] Add CIRGen for cir.unreachable and cir.trap (#151363) 2025-08-05 18:52:02 +08:00
Andres-Salamanca
9f7f3d6548
[CIR] Implemented get/set for volatile bitfields (#151875)
This PR adds support for loading and storing volatile bit-field members
according to the AAPCS specification.

> A volatile bit-field must always be accessed using an access width
appropriate to the type of its container, except when any of the
following are true:
>
> * The bit-field container overlaps with a zero-length bit-field.
> * The bit-field container overlaps with a non-bit-field member.

For example, if a bit-field is declared as `int`, the load/store must
use a 32-bit access, even if the field itself is only 3 bits wide.
2025-08-04 17:29:25 -05:00