60 Commits

Author SHA1 Message Date
Morris Hafner
f78d6caadc
[CIR] Add Minimal Destructor Definition Support (#144719)
This patch upstreams support for writing inline and out of line C++
destructor definitions. Calling a destructor implcitly or explicitly is
left for a future patch.

Because of that restriction complete destructors (D2 in Itanium
mangling) do not call into the base (D1) destructors yet but simply
behave like a base destructor. Deleting (D0) destructor support is not
part of this patch.

Destructor aliases aren't supported, either. Because of this compilation
with -mno-constructor-aliases may be required to avoid running into NYI
errors.
2025-07-14 19:02:00 +02:00
Andy Kaylor
b415db02e7
[CIR] Add handlers for 'using enum' and namespace alias (#148011)
These decl types don't require any code generation, though when debug
info is implemented, we will need to add handling for that. Until then,
we just need to have a handler so they don't generate an NYI error.
2025-07-10 16:16:37 -07:00
Sirui Mu
1841b021c6
[CIR] Add OptInfo attribute (#146261)
This patch adds the `#cir.opt_info` attribute which holds module-level
optimization information.
2025-07-02 23:29:43 +08:00
Andy Kaylor
32180cf9f9
[CIR] Upstream support for operator assign (#145979)
This adds support for assignment operators, including implicit operator
definitions.
2025-06-27 16:56:26 -07:00
Andy Kaylor
74cabdb806
[CIR] Add basic support for operator new (#145802)
This adds the code to handle operator new expressions in ClangIR.
2025-06-27 09:43:26 -07:00
Andy Kaylor
3e9fd4966d
[CIR] Add support for constructor aliases (#145792)
This change adds support for handling the -mconstructor-aliases option
in CIR. Aliases are not yet correctly lowered to LLVM IR. That will be
implemented in a future change.
2025-06-27 09:43:05 -07:00
Amr Hesham
720d7e09b3
[CIR][NFC] Fix an unused variable warning (#145922)
This fixes a warning where a variable assigned in 'if' statement wasn't
referenced again.
2025-06-26 18:32:38 +02:00
Andy Kaylor
1e45ea12db
[CIR] Add support for function linkage and visibility (#145600)
This change adds support for function linkage and visibility and related
attributes. Most of the test changes are generalizations to allow
'dso_local' to be accepted where we aren't specifically testing for it.
Some tests based on CIR inputs have been updated to add 'private' to
function declarations where required by newly supported interfaces.

The dso-local.c test has been updated to add specific tests for
dso_local being set correctly, and a new test, func-linkage.cpp tests
other linkage settings.

This change sets `comdat` correctly in CIR, but it is not yet applied to
functions when lowering to LLVM IR. That will be handled in a later
change.
2025-06-25 10:59:30 -07:00
Andy Kaylor
418b409df8
[CIR] Add support for member initialization from constructors (#144583)
Upstream the code to handle member variable initialization in a
constructor. At this point only simple scalar values (including members
of anonymous unions) are handled.
2025-06-24 10:05:48 -07:00
Andy Kaylor
77834a40cf
[CIR] Upstream support for emitting constructors (#143639)
This change upstreams the code to emit simple constructor defintions.
2025-06-12 09:24:26 -07:00
Andy Kaylor
b9329fe88e
[CIR] Upstream support for calling constructors (#143579)
This change adds support for calling C++ constructors. The support for
actually defining a constructor is still missing and will be added in a
later change.
2025-06-10 16:50:29 -07:00
Andy Kaylor
6559831025
[CIR] Add support for accessing members of base classes (#143195)
This change adds the support for accessing a member of a base class from
a derived class object.
2025-06-09 13:11:12 -07:00
Andy Kaylor
16dda4d3f4
[CIR] Add support for completing forward-declared types (#143176)
This adds the needed handling for completing record types which were
previously declared leading us to create an incomplete record type.
2025-06-06 12:54:43 -07:00
Andy Kaylor
6c1ca07586
[CIR] Add decl case for template specialization (#143029)
This change adds the switch case to allow template specialization to
pass through emitTopLevelDecl without issuing an error.
2025-06-05 16:50:36 -07:00
Andy Kaylor
e2ae39d0df
[CIR] Add empty handlers for Using and UsingShadow decls (#143032)
This adds emitTopLevelDecl "handlers" for Using and UsingShadow. These
don't actually need any handling, but they need to be present in the
switch to avoid hitting the default handler, which issues a diagnostic
about the decl kind not being implemented.

There are several other decl kinds that don't need any handling. Those
will be added when I have test cases for them.
2025-06-05 16:05:01 -07:00
Andy Kaylor
af54790ca0
[CIR] Defer emitting function definitions (#142862)
This change implements deferring function definition emission until
first use.
2025-06-05 13:08:29 -07:00
Andy Kaylor
f327d6d4c3
[CIR] Defer definitions of global variables until they are used. (#142496)
This change adds support for deferring global variable definitions until
first use whenever it is possible to do so. Although deferring function
definitions uses much of the same implementation, function deferral will
be added in a follow-up change.
2025-06-04 14:34:54 -07:00
Andy Kaylor
19dcec979c
[CIR] Support inline C++ member function definitions (#142484)
This change upstreams the code to support emitting inline C++ function
definitions, including the ASTConsumer handler for inline definitions
and the code to load the 'this' pointer.

This necessitates introducing the Itanium CXXABI class. No other CXXABI
subclasses are supported at this time. The Itanium CXXABI is used for
AppleARM64, which will require its own handler for a few special cases
(such as array cookies) later.
2025-06-02 17:13:19 -07:00
Andy Kaylor
08190e55ab
[CIR] Fix dso_local and comdat handling for global vars (#142214)
This change adds extra processing of global variable definitions to
correctly set the dso_local and comdat attributes.
2025-06-02 13:46:17 -07:00
Andy Kaylor
0e90a846d6
[CIR] Add support for global linkage and visibility (#141973)
This change adds support for the CIRGlobalValueInterface and attributes
for visibility and comdat to GlobalOp.

The comdat attribute isn't correctly calculated yet, but it was required
for the CIRGlobalValueInterface interface. There are also some cases
where dso_local isn't set correctly, but it is better than it was before
this change. Those issues will be addressed in a future patch.
2025-05-30 13:36:48 -07:00
Andy Kaylor
b574c811e8
[CIR] Defer declarations and tentative definitions (#141700)
This change adds code to defer emitting declarations and tentative
definitions until they are referenced or trigger by a call to
CompleteTentativeDefinition. This is needed to avoid premature handling
of declarations and definitions that might not be referenced in the
current translation unit. It also avoids incorrectly adding an
initializer to external declarations.

This change also updates the way the insertion location for globals is
chosen so that all globals will be emitted together at the top of the
module. This makes no functional difference, but it is very useful for
writing sensible tests.

Some tests are modified in this change to reorder global variables so
that they can be checked in the order in which they will be emitted.
2025-05-28 13:53:30 -07:00
Andy Kaylor
ea3c225786
[CIR] Add alignment support for global, store, and load ops (#141163)
This adds alignment support for GlobalOp, LoadOp, and StoreOp.

Tests which failed because cir.store/cir.load now print alignment were
updated with wildcard matches, except where the alignment was relevant
to the test. Tests which check for cir.store/cir.load in cases that
don't have explicit alignment were not updated.

New tests for alignment are alignment.c, align-load.c, and
align-store.c.
2025-05-23 15:34:59 -07:00
Andy Kaylor
3ce74c3b40
[CIR] Upstream support for string literals (#140796)
This adds the minimal support needed to handle string literals.
2025-05-21 11:32:50 -07:00
Andy Kaylor
a0c515a9ef
[CIR] Upstream support for C++ member function calls (#140290)
This change adds the support needed to handle a C++ member function
call, including arranging the function type with an argument added for
the 'this' parameter. It was necessary to introduce the class to handle
the CXXABI, but at this time no target-specific subclasses have been
added.
2025-05-19 14:42:50 -07:00
Andy Kaylor
fa2263b309
[CIR][NFC] Use arrangeFunctionDeclaration to build function types (#139787)
This change replaces the simplified call that we were previously using
to convert the function type provided by a global declaration to the CIR
function type. We now go through 'arrangeGlobalDeclaration' which builds
the function type in a more complicated manner. This change has no
observable differences for the currently upstreamed CIR support, but it
is necessary to prepare for C++ member function calls, which require the
extra handling.
2025-05-16 09:08:16 -07:00
Iris Shi
3f6ef4ecfb
[CIR] Cleanup support for C functions (#136854)
This adds basic handling for non-prototype functions in C.

Closes #130200.
2025-05-13 09:47:21 -07:00
Ankur Ahir
3e393d9aac
[CIR] Upstream enum support (#136807) 2025-05-11 17:52:57 +08:00
Andy Kaylor
e882590083
[CIR] Refactor global variable emission and initialization (#138222)
When global variable support was initially upstreamed, we took some
shortcuts and only implemented the minimum support for simple variables
and constant initializers.

This change refactors the code that creates global variables to
introduce more of the complexities that are present in the incubator and
the classic codegen. I can't really say this is NFC, because the code
executed is very different and it will report different NYI diagnostics,
but for the currently implemented cases, it results in the same output.
2025-05-02 16:54:29 -07:00
Erich Keane
0e07478a5e
[CIR] Implement lowering for LinkageSpecDecl (#137634)
Like the NamespaceDecl, this is just emitted as a decl-context, where
all its internal declarations get emitted for it. The incubator doesn't
seem to have any good tests for this, so I wrote what I could think of
as a half-decent test for this one, though the lowering doesn't manage
whether these should be mangled, so the test is mostly just for
spot-checking purposes.

I'm implementing this as it will make writing further tests for future
features a little easier.
2025-04-28 15:23:10 -07:00
Amr Hesham
e44f7760fa
[CIR][NFC] Fix an unused variable warning (#137466)
This fixes a warning where a variable assigned in 'if' statement wasn't referenced again.
2025-04-28 19:06:20 +02:00
Andy Kaylor
8b98612910
[CIR] Fix calling defined functions (#137271)
Until now our function symbol lookup has been assuming that the function
did not exist and creating a definition for it. This caused us to create
a duplicate definition if we ever tried to call a function that was
already defined.

This change fixes that by adding handling for trying to look up existing
global definitions before creating a new one.
2025-04-25 12:08:52 -07:00
Andy Kaylor
b6746b0657
[CIR] Upstream namepsace handling (#137253)
This adds the handlers for Decl::Namespace and Decl::UsingDirective
(which is needed for anonymous namespaces).
2025-04-24 16:42:36 -07:00
Andy Kaylor
7d71164855
[CIR] Upstream support for name mangling (#137094)
We have been using the default names for global symbols to this point.
This change introduces proper name mangling for functions.

This requires introducing a CXXABI class in the CIRGenModule. Because
only target independent name mangling is handled in this patch, the
CXXABI class does not require a target-specific implementation. The
general mechanism for selecting an implementation is introduced here,
but the actual target-specific subclasses are deferred until needed.
2025-04-24 14:24:19 -07:00
Andy Kaylor
daf3c985f7
[CIR] Upstream support for typedef and type aliases (#136335)
Nothing is actually needed in ClangIR to support typedef and type
aliases, but the Decl kinds need to be explicitly ignored in the
emitDecl handlers to avoid hitting the default NYI errors. This change
does that and adds tests.
2025-04-18 12:52:51 -07:00
Sirui Mu
78857e7263
[CIR] cir.call with scalar return type (#135552)
This PR introduces support for calling functions with a scalar return
type to the upstream. This PR also includes an initial version of
`CIRGenTargetInfo` and related definitions which are essential for the
CIRGen of call ops.

Related to #132487 .
2025-04-17 22:38:37 +08:00
Andy Kaylor
cd7d2c3bf8
[CIR] Upstream minimal support for structure types (#135105)
This change adds minimal support for structure types. To keep the
initial change small, only incomplete declarations are being supported
in this patch. More complete support will follow.
2025-04-14 14:40:19 -07:00
Andy Kaylor
abe3b90b3f
[CIR] Fix warnings, again! (#135284)
The calleeDecl var will be used in the near future, so I left it. At
least for clang, the [[maybe_unused]] attribute takes care of the
warnings related to that variable. The other warning was a simple lack
of return after errorNYI.
2025-04-11 10:57:08 -07:00
Andy Kaylor
db22909089
[CIR] Upstream support for cir.get_global (#135095)
This adds basic support for referencing global variables from within
functions via the cir.get_global operation.
2025-04-10 14:15:10 -07:00
Sirui Mu
85614e160b
[CIR] Upstream initial function call support (#134673)
This patch upstreams initial support for making function calls in CIR.
Function arguments and return values are not included to keep the patch
small for review.

Related to #132487
2025-04-10 22:41:00 +08:00
Andy Kaylor
58b91d10a4
[CIR][NFC] Upstream LValueBaseInfo handling (#134928)
Previous implementations that used the cir::LValue class omitted hanling
of the LValueBaseInfo class, which tracks information about the basis
for the LValue's alignment. As more code was upstreamed from the
incubator, we were accumulating technical debt by adding more places
where this wasn't handled correctly. This change puts the interfaces in
place to track this information.

The information being tracked isn't used yet, so no functional change is
intended. The tracking is being added now because it will become more
difficult to add it as more features are implemented.
2025-04-09 15:27:50 -07:00
Andy Kaylor
4928093a21
[CIR] Upstream support for address of and dereference (#134317)
This adds support for handling the address of and dereference unary
operations in ClangIR code generation. This also adds handling for
nullptr and proper initialization via the NullToPointer cast.
2025-04-08 10:32:03 -07:00
Erich Keane
231aa3070d
[OpenACC][CIR] Basic infrastructure for OpenACC lowering (#134717)
This is the first of a few patches that will do infrastructure work to
enable the OpenACC lowering via the OpenACC dialect.

At the moment this just gets the various function calls that will end up
generating OpenACC, plus some tests to validate that we're doing the
diagnostics in OpenACC specific locations.

Additionally, this adds Stmt and Decl files for CIRGen.
2025-04-08 10:06:28 -07:00
Amr Hesham
262b9b5153
[CIR][Upstream] Local initialization for ArrayType (#132974)
This change adds local initialization for ArrayType

Issue #130197
2025-04-03 19:25:25 +02:00
Amr Hesham
143c37123b
[CIR] Upstream zero init for global variables (#133100)
This change adds zero initialization for global variables
2025-03-31 19:41:29 +02:00
Amr Hesham
6aeae62aef
[CIR] Upstream global initialization for ArrayType (#131657)
This change adds global initialization for ArrayType

Issue #130197
2025-03-19 21:29:37 +01:00
Andy Kaylor
39ce99589b
[CIR] Upstream cir-canonicalize pass (#131891)
This change introduces the cir-canonicalize pass. This is a simple
cir-to-cir transformation that eliminates empty scopes and redundant
branches. It will be expanded in future changes to simplify other
redundant instruction sequences.

MLIR verification and mlir-specific command-line option handling is also
introduced here.
2025-03-19 09:42:03 -07:00
Morris Hafner
710de09f17
[CIR] Upstream global variable linkage types (#129072)
This change implements variable linkage types in ClangIR except for
common linkage which requires Comdat support.

---------

Co-authored-by: Morris Hafner <mhafner@nvidia.com>
Co-authored-by: Henrich Lauko <xlauko@mail.muni.cz>
2025-03-06 10:17:58 -08:00
David Olsen
eacbcbe477
[CIR] Upstream type bool (#128601)
Support the type `bool` and the literals `true` and `false`. Add the
type `cir::BoolType` and the attribute `cir::BoolAttr` to ClangIR. Add
code in all the necessary places in ClangIR CodeGen to handle and to
recognize the type and the attribute.

Add test cases to existing tests func-simple.cpp and
global-var-simple.cpp.
2025-02-25 11:40:15 -08:00
David Olsen
f8bdbed5b3
[CIR] Upstream simple function bodies (#127674)
Enable ClangIR generation for very simple functions. The functions have
to return `void` or an integral type, contain only compound statements
or `return` statements, and `return` statement expressions can only be
integral literals of the correct type. The functions can have
parameters, but those are currently ignored because there is no way to
access them.

This change intentionally focuses on breadth (introducing scopes,
statements, and expressions) rather than depth, because it enables
people to work on upstreaming in parallel without interference.

The new ClangIR ops in this change are `ReturnOp`, `YieldOp`, `ScopeOp`,
and `TrapOp`. These operations are complete (except for the
`ParentOneOf` property) and shouldn't require further upstreaming
changes. Significant additions were made to `FuncOp`, adding a type and
a region, but that operation is still far from complete.

The classes `ScalarExprEmitter` and `CIRGenFunction`, along with the
`emit*` functions in `CIRGenFunction` that generate ClangIR for
statements, are new in this change. All of these are very incomplete and
will be filled out in later upstreaming patches.

Existing test `hello.c` is removed and replaced by the new test
`func-simple.cpp`. This tests all forms of functions that are currently
supported.
2025-02-19 19:58:12 -08:00
Andy Kaylor
75ea7aed93
[CIR] Add additional frontend actions (#127249)
Add frontend actions to support emitting assembly, bitcode, and object
files when compiling with ClangIR. This change also correctly sets and
propagates the target triple in the MLIR and LLVM modules, which was a
necessary prerequisite for emitting assembly and object files.
2025-02-19 09:08:37 -08:00