55 Commits

Author SHA1 Message Date
Andy Kaylor
6d1184d05f
[CIR][NFC] Organize emit functions in CIRGenFunction.h (#133017)
To make rebasing the incubator project easier, we've been trying to
place upstreamed code in the same order in which it appears in the
incubator project. However, while the upstream implementation is still
relatively sparse, it is often difficult to find points of reference for
placement of new declarations. To help with that, I refactored
CIRGenFunction.h in the incubator to put all the emit* functions in one
place and sort them alphabetically.

This change reorganizes the upstream CIRGenFunction.h to match the new
incubator ordering.
2025-03-26 11:37:47 -07:00
Andy Kaylor
7563e31127
[CIR] Implement lowering of int-to-bool casts (#132996)
Lowering of int-to-bool casts had been left as NYI because the incubator
implemented it by lowering to cir.cmp, which hasn't been upstreamed yet,
but there is no reason this cast can't be lowered directly to LLVM's
compare operation when we're lowering directly to the LLVM dialect.

This change lowers the cast directly to an LLVM compare to zero.
2025-03-26 09:44:05 -07:00
Andy Kaylor
bff94d774c
[CIR] Emit allocas into the proper lexical scope (#132468)
Alloca operations were being emitted into the entry block of the current
function unconditionally, even if the variable they represented was
declared in a different scope. This change upstreams the code for
handling
insertion of the alloca into the proper lexcial scope. It also adds a
CIR-to-CIR transformation to hoist allocas to the function entry block,
which is necessary to produce the expected LLVM IR during lowering.
2025-03-25 16:13:57 -07:00
Morris Hafner
2f3c93743f
[CIR] Add binary operators (#132420)
This patch adds upstreams support for BinOp including lvalue
assignments. Note that this does not include ternary ops,
BinOpOverflowOp, pointer arithmetic, ShiftOp and SelectOp which are
required for logical binary operators.

---------

Co-authored-by: Morris Hafner <mhafner@nvidia.com>
Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
2025-03-25 14:12:27 -07:00
Andy Kaylor
f51e5f3f65
[CIR] Upstream initial for-loop support (#132266)
This change adds support for empty for-loops. This will be the
infrastructre needed for complete for loops, but that depends on compare
operations, which have not been upstreamed yet.
2025-03-21 09:13:13 -07:00
Andy Kaylor
f25185b579
[CIR] Fix unary op folding (#132269)
Unary ops had previously been omitted from the list of ops handled in
the CIRCanonicalizePass. Although the incubator code doesn't use them
directly, the mlir folding code does.

This change enables folding of unary ops by adding them to the list.
2025-03-20 16:54:09 -07:00
Amr Hesham
7c64790213
[CIR] Follow up for addressing style comment (#132093)
Follow up PR to address style comment
(https://github.com/llvm/llvm-project/pull/131657#discussion_r2004197917)
2025-03-20 08:07:28 +01:00
Andy Kaylor
93afd8f9ac
[CIR] Upstream support for type aliases (#131912)
CIR uses aliases for standard integer types and void. This change adds
upstream support for those aliases and updates existing tests.
2025-03-19 14:13:22 -07: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
David Olsen
4e3440d9dd
[CIR] Upstream a basic version of class LexicalScope (#131945)
Upstream the parts of class `CIRGenFunction::LexicalScope` that
implement function return values. There is a bit of other functionality
in here, such as the implicit `cir.yield` at the end of a non-function
scope, but this is mostly about function returns.

The parts of `LexicalScope` that handle calling destructors, switch
statements, ternary expressions, and exception handling still need to be
upstreamed.

There is a change in the generated ClangIR (which is why many of the
tests needed updating). Return values are stored in the
compiler-generated variable `__retval` rather than being passed to the
`cir.return` op directly. But there should be no change in the behavior
of the generated code.
2025-03-19 12:56:41 -07: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
27d8bd3dca
[CIR] Upstream CastOp and scalar conversions (#130690)
This patch upstreams ClangIR's CastOp with the following exceptions:
- No Fixed/FP conversions
- No casts between value categories
- No complex casts
- No array_to_ptrdecay
- No address_space
- No casts involving record types (member pointers, base/derived casts)
- No casts specific to ObjC or OpenCL

---------

Co-authored-by: Morris Hafner <mhafner@nvidia.com>
Co-authored-by: Erich Keane <ekeane@nvidia.com>
2025-03-18 09:36:43 -07:00
Andy Kaylor
5f866666a6
[CIR] Upstream initial support for unary op (#131369)
This adds support for the cir.unary plus, minus, inc, dec, and not operations for integer, floating point, and boolean types.
2025-03-17 15:27:37 -07:00
darkbuck
f6a7306bea
[clang][CIR] Add missing dependency on MLIR headers (#131057) 2025-03-17 11:17:10 -07:00
Amr Hesham
e9fc7683a5
[CIR] Upstream basic support for sizeof and alignof (#130847)
This change adds the essential support for sizeof and alignof operators

- Support for VariableArrayType can be added after closing #130197
2025-03-14 19:12:27 +01:00
Morris Hafner
0f98d1b9fa
[CIR] Don't generate ClangIR after an unrecoverable error occured (#130971)
Co-authored-by: Morris Hafner <mhafner@nvidia.com>
2025-03-14 07:20:42 -07:00
Amr Hesham
bd0d28ac25
[CIR] Upstream basic support for ArrayType (#130502)
This change adds the basic support for ArrayType

Issue #130197
2025-03-13 18:46:31 +01:00
Andy Kaylor
64b94105d5
[CIR] Upstream support for emitting ignored statements (#130869)
This adds support for emitting ClangIR for statements whose value is
ignored. The test case being added (CIR/CodeGen/basic.c) tests a few
more things. The "f1" test case is the only part that's immediately
relevant to this change, but the other cases were part of the same test
in the incubator and they are supported so I brought in the entire test.
2025-03-12 14:38:19 -07:00
Andy Kaylor
701148f05a
[CIR] Upstream initial support for CIR flattening (#130648)
The ClangIR CFG has to be flat before it can be lowered to LLVM IR. That
is, there can be no nested regions and all blocks in a region must
belong to the parent region. Currently only cir.scope operations violate
these rules, so the initial implementation of the cir-flatten-cfg pass
only has to transform scope operations.
2025-03-11 15:47:02 -07:00
Andy Kaylor
e1bd39c173
[CIR] Disable gcc partially overloaded virtual warning (#130322)
GCC, unlike clang, issues a warning when one virtual function is
overridden in a derived class but one or more other virtual functions
with the same name and different signature from a base class are not
overridden. This leads to many warnings in the MLIR and ClangIR code
when using the OpenConversionPattern<>::matchAndRewrite() function in
the ordinary way. The "hiding" behavior is what we want, so we're just
disabling the warning here.
2025-03-10 10:21:29 -07:00
Andy Kaylor
8eb9b947af
[CIR] Emit init of local variables (#130164)
Local variable initialization was previously being ignored. This change
adds support for initialization of scalar variables with constant values
and introduces the constant emitter framework.
2025-03-07 10:23:06 -08: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
Andy Kaylor
fa072bd29a
[CIR] Add lowering for Func, Return, Alloca, Load, and Store (#129571)
Add support for lowering recently upstreamed CIR ops to LLVM IR.
2025-03-04 14:50:34 -08:00
Andy Kaylor
6f256145c0
[CIR] Clean up warnings (#129604)
Previous CIR commits have introduced a few warnings. This change fixes
those.

There are still warnings present when building with GCC because GCC
warns about virtual functions being hidden in the mlir::OpConversion
classes. A separate discussion will be required to decide what should be
done about those.
2025-03-04 10:50:06 -08:00
Amr Hesham
ab30df470a
[CIR] Upstream floating point literal expressions (#129304)
This change adds support for floating point literal expressions
2025-03-03 19:22:44 +01:00
Andy Kaylor
a19e685ea2
[CIR] Realign CIR-to-LLVM IR lowering code with incubator (#129293)
The previously upstreamed lowering from ClangIR to LLVM IR diverged from
the incubator implementation, but when the incubator was updated to
incorporate these changes some issues arose which require the upstream
implementation to be modified to re-align with the incubator.

First, in the earlier upstream implementation a CIRAttrVisitor class was
introduced with the intention that an mlir-tblgen based extension would
be created to automatically add all CIR attributes to the visitor. When
I proposed this in mlir-tblgen a reviewer suggested that what I wanted
could be better accomplished with TypeSwitch.

See https://github.com/llvm/llvm-project/pull/126332

This was done in the incubator, and here I am bringing that
implementation upstream.

The other issue was that the global op initialization in the incubator
had more cases than I had accounted for in my previous upstream
refactoring. I did still refactor the incubator code, but not in quite
the same way as the upstream code. This change re-aligns the two.
2025-02-28 14:43:39 -08:00
Andy Kaylor
56cc9299b7
[CIR] Upstream func args alloca handling (#129167)
This change adds support for collecting function arguments and storing
them in alloca memory slots.
2025-02-28 11:43:53 -08:00
Andy Kaylor
3989b78fa9
[CIR] Upstream basic alloca and load support (#128792)
This change implements basic support in ClangIR for local variables
using the cir.alloca and cir.load operations.
2025-02-27 14:22:26 -08:00
David Olsen
556eb82442
[CIR] Function type return type improvements (#128787)
When a C or C++ function has a return type of `void`, the function type
is now represented in MLIR as having no return type rather than having a
return type of `!cir.void`. This avoids breaking MLIR invariants that
require the number of return types and the number of return values to
match.

Change the assembly format for `cir::FuncType` from having a leading
return type to having a trailing return type. In other words, change
```
!cir.func<!returnType (!argTypes)>
```
to
```
!cir.func<(!argTypes) -> !returnType)>
```
Unless the function returns `void`, in which case change
```
!cir.func<!cir.void (!argTypes)>
```
to
```
!cir.func<(!argTypes)>
```
2025-02-26 20:58:49 -08:00
David Olsen
ad94af973a
[CIR] React to breaking change to DataLayoutTypeInterface (#128772)
In #128754, `DataLayoutTypeInterface` was changed to give
`getPreferredAlignment` a default implemention. As a result, table-gen
no longer declared `getPreferredAlignment` when defining a class that
contained `[DeclareTypeInterfaceMethods<DataLayoutTypeInterface>]` in
the table-gen definition. That means all of the definitions in
`CIRTypes.cpp`, such as `PointerType::getPreferredAligment`, were
compilation errors.

Delete all the definitions of `getPreferredAlignment`. I verified that
the default implementation does the exact same thing as the explicit
overrides that are being deleted.
2025-02-25 13:44:24 -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
Andy Kaylor
607a1f2ace
[CIR] Add cir-opt tool to exercise CIR dialect parsing (#128254)
We need to be able to read in and parse files using the ClangIR dialect
in order to test this part of the functionality.

This change adds the minimum cir-opt tool needed to read and parse cir
files and write them back to text. This tool will later be extended to
add features for lowering from CIR to other MLIR dialects and to run CIR
passes as they are upstreamed.
2025-02-24 11:22:58 -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
Henrich Lauko
d9b55b7210
[CIR] Fix extra ; warning, and replace new with emplaceBlock (NFC) (#127207) 2025-02-15 00:29:18 +01:00
Andy Kaylor
2b5cc89b3f
[CIR] Lowering to LLVM for global pointers (#125619)
Add support for lowering global variables of any pointer type to LLVM
IR.
2025-02-05 15:44:29 -08:00
Andy Kaylor
622ee03e26
[CIR] Initial implementation of CIR-to-LLVM IR lowering pass (#125260)
This change introduces lowering from CIR to LLVM IR of global integer
and floating-point variables, using defaults for attributes that aren't yet
implemented.
2025-02-03 13:35:43 -08:00
Michael Liao
eb0af4e48d [CIR] Fix shared build. NFC 2025-01-31 13:19:43 -05:00
Andy Kaylor
fbf544c422
[CIR] Fix some clang-tidy problems in CIR (#125128)
This adds a .clang-tidy file to the clang/lib/CIR/FrontendAction
directory, moves and updates the incorrectly located
include/clang/CIR/FrontendAction .clang-tidy file, and updates two files
from a recent commit to bring them into conformance with previously
agreed upon rules for where to use LLVM naming conventions and where to
use MLIR naming conventions.
2025-01-31 08:39:33 -08:00
Andy Kaylor
38ddcb7e36
[CIR] Add framework for CIR to LLVM IR lowering (#124650)
Create the skeleton framework for lowering from ClangIR to LLVM IR. This
initial implementation just creates an empty LLVM IR module. Actual
lowering of even minimal ClangIR is deferred to a later patch.
2025-01-30 13:11:25 -08:00
David Olsen
8e32959331
[CIR] Upstream initial attribute support (#121069)
Upstream several ClangIR-specific MLIR attributes, in particular
attributes for integer, floating-point, and null pointer constants.
These are the first ClangIR attributes to be upstreamed, so
infrastructure changes are included, such as the table-gen file
`CIRAttrs.td`.

Attributes can be used as the initial values for global variables. The
existing automated test global-var-simple.cpp includes initial values
for some of the global variables in the test.
2024-12-28 14:02:15 -08:00
David Olsen
8ae8a90585
[CIR] floating-point, pointer, and function types (#120484)
Upstream ClangIR support for `void` type, floating-point types, pointer
types, and function types.

Floating-point support is missing the IBM double-double format, because
that hasn't been implemented in the incubator project yet.

Pointer types do not yet support address spaces.

Function type support includes only the return type and the parameter
types. The many other properties and attributes of function types will
be upstreamed later.
2024-12-20 12:22:25 -08:00
David Olsen
7eb73b95cb
[CIR] Cleanup: mlirContext and astContext (#119450)
ClangIR CodeGen code uses both `mlir::MLIRContext` and
`clang::ASTContext` objects extensively. Refering to either of those as
just "context" can be confusing.

Change the names of all variables, parameters, and fields in
`clang/lib/CIR/CodeGen` that refer to `MLIRContext` or an `ASTContext`
to be either `mlirContext` or `astContext`.

This change is only the renaming of variables/parameters/fields. There
are no behavior changes. So there are no new tests or changes to
existing tests. This change mimics a recent change in the ClangIR
incubator repository.
2024-12-10 13:46:07 -08:00
David Olsen
ffb19f4018
[CIR] Infrastructure: class CIRGenBuilderTy; cache CIR types (#119037)
Small infrastructure and background changes to ClangIR.

Create class `CIRGenBuilderTy` and its base class `CIRBaseBuilderTy`.
These are mostly empty for now, except for what is inherited from
`mlir::OpBuilder`. But they will fill up quickly as more ClangIR code
gen is upstreamed. `CIRGenModule` and `CIRGenTypes` are changed to use
`CIRGenBuilderTy`.

Add cached types to struct `CIRGenTypeCache` for the integral types that
are currently supported. Initialize those cached types in the
`CIRGenModule` constructor. The first uses of those types (well, one of
them) is in `CIRGenTypes::convertType`.

Have `CIRGenTypes::convertType` cache its results in a map from
`clang::Type` to `mlir::Type`, saving it from having to convert the same
type again and again.

There are no new tests or changed tests in this commit. There are no
changes to behavior, just improvements to how the existing behavior is
implemented.
2024-12-10 11:29:48 -08:00
David Olsen
a43b2e13f9
[CIR] Integral types; simple global variables (#118743)
Add integral types to ClangIR. These are the first ClangIR types, so the
change includes some infrastructure for managing ClangIR types.

So that the integral types can be used somewhere, generate ClangIR for
global variables using the new `cir.global` op. As with the current
support for functions, global variables are just a stub at the moment.
The only properties that global variables have are a name and a type.

Add a new ClangIR code gen test global-var-simple.cpp, which defines
global variables with most of the integral types.

(Part of upstreaming the ClangIR incubator project into LLVM.)
2024-12-06 07:01:09 -08:00
David Olsen
eaa4eb281d
[CIR] Fix warning in CIRGenAction.cpp (#118389)
Fix a compiler warning in `CIRGenConsumer::HandleTranslationUnit` in
`clang/lib/CIR/FrontendAction/CIRGenAction.cpp`. The warning was about a
`default:` section in a switch statement that already covered all the
values of an enum. Delete the `default:` section.
2024-12-02 13:19:33 -08:00
Luohao Wang
56e56c9e66
[clang][CIR] Fix missing dependency of MLIRCIR (#116221)
Building `MLIRCIR` will report an error `CIROpsDialect.h.inc` not found.
This is because `MLIRCIR` hasn't declared its dependence on the tablegen
target `MLIRCIROpsIncGen`. This patch fixes the issue.
2024-11-14 22:21:07 -08:00
Shoaib Meenai
1791b25f43
[clang][CIR] Change buildX functions to emitX (#115568)
The buildX naming convention originated when the CIRGen implementation
was planned to be substantially different from original CodeGen. CIRGen
is now a much closer adaption of CodeGen, and the emitX to buildX
renaming just makes things more confusing, since CodeGen also has some
helper functions whose names start with build or Build, so it's not
immediately clear which CodeGen function corresponds to a CIRGen buildX
function. Rename the buildX functions back to emitX to fix this.
2024-11-12 09:56:25 -08:00
Shoaib Meenai
c72389d4fe
[clang][CIR] Merge the mlir::cir namespace into cir (#115386)
https://github.com/llvm/clangir/issues/1025 discusses the motivation.
The mechanical parts of this change were done via:

find clang \( -name '*.h' -o -name '*.cpp' -o -name '*.td' \) -print0 |
xargs -0 perl -pi -e 's/mlir::cir/cir/g'
find clang \( -name '*.h' -o -name '*.cpp' \) -print0 | xargs -0 perl
-pi -e 's/::cir/cir/g'

There were some manual fixups and a clang-format run afterwards.
2024-11-08 10:43:28 -08:00
Shoaib Meenai
40e545098e
[clang][CIR] Move CIRGen types into clang::CIRGen (#115385)
https://github.com/llvm/clangir/issues/1025 explains why we want to move
the CIR dialect from the `mlir::cir` to the `cir` namespace. To avoid
overloading the `cir` namespace too much afterwards, move all symbols
whose equivalents live inside the `clang::CodeGen` namespace to a new
`clang::CIRGen` namespace, so that we match the original CodeGen's
structure more closely.
2024-11-08 10:41:39 -08:00