27 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
Akimasa Watanuki
e927f4bbf4
[CIR] Handle empty unions in record lowering and LLVM conversion (#172666)
Handle empty unions in CIR record lowering and LLVM conversion by
emitting padding when needed, guarding `getLargestMember` for
empty/padded unions, and lowering to empty or padded LLVM structs based
on language rules.

Added regression tests for C and C++ empty union lowering in
`clang/test/CIR/CodeGen/empty-union.c` and `empty-union.cpp`.
2026-01-09 09:52:30 -08:00
Andy Kaylor
407ecfe6a3
[CIR][NFC] Cleanup MissingFeature asserts in RecordLayoutBuilder (#161605)
This change cleans up some cir::MissingFeature asserts in
CIRGenRecordLayoutBuilder.cpp. In a couple of cases the asserts were
stale markers that we failed to remove when the correspond support was
implemented.

In one case, a cir::MissingFeature::bitfields() assert was ambiguous in
meaning and has been replaced by a comment and something more specific.
The missing feature in this case is just a bit of debug code to verify
certain bitfield-related conditions. This check should be added, but it
is not functionally required.
2025-10-02 09:16:48 -07:00
Andy Kaylor
71365c0b6b
[CIR][NFC] Fix CIR build after CharUnits change (#161580)
My previous attempt to fix this missed one case.
2025-10-01 13:34:27 -07:00
Andy Kaylor
9a30ada53d
[CIR][NFC] Fix CIR build (#161577)
This fixes the CIR build after recent changes to CharUnits.
2025-10-01 13:20:59 -07:00
Morris Hafner
1dbe65a008
[CIR] Add constant record ILE support (#155663)
This patch adds basic support for constant record initializer list
expressions. There's a couple of limitations:

* No zero initialized padding bytes in C mode
* No bitfields
* No designated initializer lists
* Record alignments are not calculated, yet
* ILEs of derived records don't work, yet
* The constant attribute is not propagated to the backend, resulting in
non-constants being emitted in the LLVM IR
2025-09-04 23:10:47 +02:00
Andy Kaylor
9647a8646e
[CIR] Finish record layout for classes with virtual bases (#156770)
There was a small piece left unimplemented for classes with a primary
virtual base. This adds that implementation.
2025-09-04 12:41:53 -07:00
Andres-Salamanca
71b64bc5c7
[CIR] Add support for discrete bit-field (#156085)
This PR adds support for the discrete bit-field layout.
It is the same as this PR: https://github.com/llvm/clangir/pull/1860
2025-09-03 21:59:36 -05:00
Andy Kaylor
e7c9f2db41
[CIR] Add initial support for virtual base classes (#155534)
This adds support for declaring a class with a virtual base class and
initializing the vptr in the constructor. This does not yet handle
constructors that require a virtual table table (VTT) implicit argument.
2025-08-27 15:40:40 -07: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
Andres-Salamanca
7e9927127f
[CIR] Fix outdated bitfield iteration logic in accumulateFields (#151741)
This PR fixes the outdated logic for accumulating bitfields in
`accumulateFields`. The old approach remained after the algorithm was
updated. A non-bitfield member would act as a barrier, causing
`accumulateBitFields` to receive an incomplete range of fields. As a
result, it failed to accumulate them properly when clipping was
necessary.

For reference, in ClangIR we already handle this correctly:

[b647f4b97b/clang/lib/CIR/CodeGen/CIRRecordLayoutBuilder.cpp (L711-L714))
2025-08-02 09:39:51 -05:00
Andy Kaylor
c188e1db6a
[CIR] Add vptr type and generate vptr field when needed (#151377)
This adds a new CIR type, cir.vptr, and generates a field of that type
when a record is declared that requires a vptr member.
2025-08-01 12:10:04 -07:00
Andres-Salamanca
4005edd5c4
[CIR] Add ComputeVolatileBitfields Implementation (#151252)
This PR adds the implementation of the `ComputeVolatileBitfields`
function for the AAPCS ABI, following the rules described in [AAPCS64
§8.1.8.5 Volatile
Bit-fields](f52e1ad3f8/aapcs64/aapcs64.rst (8185volatile-bit-fields----preserving-number-and-width-of-container-accesses)).
When accessing a volatile bit-field either reading or writing the
compiler must perform a load or store using the access size that matches
the width of the declared type (i.e., the type of the container), rather
than the packed bit-field size.
For example, if a field is declared as `int`, it must read or write 32
bits, even if the bit-field is only 3 bits wide.
The `ComputeVolatileBitfields` function calculates the correct values
and offsets necessary for proper lowering of volatile bitfields.
Support for emitting calls to `get_bitfield` and `set_bitfield` with the
correct access size for volatile bitfields will be implemented in a
future PR.
2025-07-31 10:12:56 -05:00
Andres-Salamanca
a5db2c2844
[CIR] Add support for -ffine-grained-bitfield-accesses (#150687)
This PR adds support for `-ffine-grained-bitfield-accesses`. I reused
the tests from classic CodeGen, available here:

[c2c881fcc8/clang/test/CodeGenCXX/finegrain-bitfield-access.cpp)

We produce almost exactly the same codegen, except when returning a
variable: we emit an extra variable to hold the return value, whereas
classic CodeGen does not. Also, the GEP instructions use slightly
different syntax compared to classic CodeGen.
2025-07-29 10:31:40 -05:00
Andy Kaylor
79da5fecdb
[CIR][NFC] Remove duplicate code (#145790)
This change removes a bit of code that was left as an artifact of a
previous "not yet implemented" state. The implementation is in place,
but the code to report an NYI diagnostic was left behind.
2025-06-25 14:44:33 -07:00
Andres-Salamanca
280f60ed6d
[CIR] Fix NYI AAPCS bit-fields by skipping unsupported case (#145560)
This PR addresses the error mentioned in
https://github.com/llvm/llvm-project/pull/145067#issuecomment-3001104015,
which occurs on ARM when handling an unsupported bit-field case. The
patch removes the error and replaces it with an assert, marking the
missing feature.
2025-06-24 12:03:17 -07:00
Andres-Salamanca
8763d2968d
[CIR] Add support for bitfields in unions (#145096)
This PR introduces support for bitfields inside unions in CIR.
2025-06-24 10:17:58 -07:00
Andres-Salamanca
2767ff4995
[CIR][NFC] Upstream computeVolatileBitfields (#145414)
This PR upstreams functionality for computing volatile bitfields when
the target follows the AAPCS ABI. The implementation matches the one in
the incubator, so no tests are included as the feature is not yet fully
implemented (NYI).
2025-06-24 09:38:02 -05:00
Andres-Salamanca
66214410c4
[CIR] Add support for DumpRecordLayouts (#145058)
This PR adds support for the `-fdump-record-layouts` flag.
2025-06-23 14:58:28 -05:00
Andres-Salamanca
f4d31cdee3
[CIR] Add bitfield offset calculation for big-endian targets (#145067)
This PR updates the bitfield offset calculation to correctly handle
big-endian architectures.
2025-06-23 14:20:26 -05:00
Andres-Salamanca
bd36f7331a
[CIR] Add initial support for bitfields in structs (#142041)
This change adds support for bitfields CIR records can now contain bit
fields.

I’ve updated the `CIRGenBitFieldInfo` comment, which originally came
from the incubator and was identical to the one in OGCodeGen, to better
reflect the current implementation.

Support for bitfields in unions big-endian architectures and `get` and
`set` operations remains unimplemented and will be addressed in a future
patch.
2025-06-20 09:03:02 -05:00
Andy Kaylor
599b2a3475
[CIR] Add support for derived class declarations (#142823)
This adds the minimal support for declaring a pointer to a derived
class. This includes only the changes necessary to compute the record
layout for the derived class and declare a variable that points to it.

Support for accessing members of either the derived or base class is
deferred until a later change, as is support for declaring a variable
that is an instance of the derived class.
2025-06-05 10:10:39 -07:00
Andy Kaylor
8d9f5160b4
[CIR] Unblock simple C++ structure support (#138368)
This change adds additional checks to a few places where a simple struct
in C++ code was triggering `errorNYI` in places where no additional
handling was needed, and adds a very small amount of trivial
initialization. The code now checks for the conditions that do require
extra handling before issuing the diagnostic.

New tests are added for declaring and using a simple struct in C++ code.
2025-05-07 09:21:01 -07:00
Iris Shi
708053cd7e
[CIR] Upstream initial support for union type (#137501)
Closes #136059
2025-05-01 07:56:38 +08:00
Henrich Lauko
9b6fbc06ad
[CIR] Infer MLIR context in type builders when possible (#136362)
This mirrors incubator changes from
https://github.com/llvm/clangir/pull/1570
2025-04-19 08:40:11 +02:00
Andy Kaylor
e298f16e89
[CIR] Upstream support for record packing and padding (#136036)
This change adds support for packing and padding record types in ClangIR
and introduces some infrastructure needed for this computation.

Although union support has not been upstreamed yet, there is no good way
to report unions as NYI in the layout computation, so the code added
here includes layout computation for unions. Unions will be added soon.
2025-04-18 10:30:36 -07:00
Andy Kaylor
80c19b3b1d
[CIR] Upstream initial support for complete record types (#135844)
This adds basic support for populating record types. In order to keep
the change small, everything non-essential was deferred to a later
change set. Only non-recursive structures are handled. Structures
padding is not yet implemented. Bitfields are not supported. No attempt
is made to handle ABI requirements for passing structure arguments.
2025-04-16 10:33:09 -07:00