78 Commits

Author SHA1 Message Date
Amr Hesham
78921cd884
[CIR] Upstream ArraySubscriptExpr for fixed size array (#134536)
This change adds ArraySubscriptExpr for fixed size ArrayType

Issue #130197
2025-04-11 00:05:22 +02: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
Erich Keane
74c2b41feb
[OpenACC][CIR] Implement 'device_type' clause lowering for 'init'/'sh… (#135102)
…utdown'

This patch emits the lowering for 'device_type' on an 'init' or
'shutdown'. This one is fairly unique, as these directives have it as an
attribute, rather than as a component of the individual operands, like
the rest of the constructs.

So this patch implements the lowering as an attribute.

In order to do tis, a few refactorings had to happen: First, the
'emitOpenACCOp' functions needed to pick up th edirective kind/location
so that the NYI diagnostic could be reasonable.

Second, and most impactful, the `applyAttributes` function ends up
needing to encode some of the appertainment rules, thanks to the way the
OpenACC-MLIR operands get their attributes attached. Since they each use
a special function (rather than something that can be legalized at
runtime), the forms of 'setDefaultAttr' is only valid for some ops. SO
this patch uses some `if constexpr` and a small type-trait to help
legalize these.
2025-04-10 12:38:35 -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
f75dce43ea
[CIR][NFC] Fix an unused variable warning (#135046)
This fixes a warning where a variable assigned in a 'if' statement
wasn't referenced again.
2025-04-09 14:27:09 -07:00
erichkeane
505a7e7d1c [OpenACC][CIR] Support for init and shutdown lowering
These two are very simple.  they don't require any clauses and don't
have an associated statement, so they have very simple output.  This
patch implements them, but none of the associated clauses.
2025-04-09 11:22:35 -07:00
Erich Keane
fa273e1158
[OpenACC][CIR] Implement 'data' construct lowering (#135038)
This patch does the lowering of the OpenACC 'data' construct, which
requires getting the `default` clause (as `data` requires at least 1 of
a list of clauses, and this is the easiest one). The lowering of the
clauses appears to happen in 1 of 2 ways: a- as an operand. or b- as an
attribute.

This patch adds infrastructure to lower as an attribute, as that is how
'data' works.

In addition to that, it changes the OpenACCClauseVisitor a bit, which
previously just required that each of the derived classes have all of
the clauses covered. This patch modifies it so that the visitor directly
calls the derived class from its visitor function, which leaves the
base-class ones the ability to defer to a generic function. This was
previously like this because I had some use cases that I didn't end up
using, and the 'generic' function here seems much more useful.
2025-04-09 10:45:17 -07:00
Erich Keane
6e7c40b83d
[OpenACC][CIR] Initial patch to do OpenACC->IR lowering (#134936)
This patch adds some lowering code for Compute Constructs, plus the
infrastructure to someday do clauses. Doing this requires adding the
dialect to the CIRGenerator.

This patch does not however implement/correctly initialize lowering from
OpenACC-Dialect to anything lower however.
2025-04-09 06:05:31 -07:00
Andres-Salamanca
ccf22848da
[CIR] Add if statement support (#134333)
This patch adds support for if statements in the CIR dialect
Additionally, multiple RUN lines were introduced to improve codegen test
coverage
2025-04-08 17:50:53 -07:00
Andy Kaylor
1a1698b5dd
[CIR] Handle NullStmt (#134889)
The handling for NullStmt was going to an error saying the statement
handling wasn't implemented. It doesn't need any implementation. It is
sufficient for emitSimpleStmt to just return success for that statement
class. This change does that.
2025-04-08 13:55:12 -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
Morris Hafner
441f87968d
[CIR] Upstream CmpOp (#133159)
This patch adds support for comparison operators with ClangIR, both
integral and floating point.

---------

Co-authored-by: Morris Hafner <mhafner@nvidia.com>
Co-authored-by: Henrich Lauko <xlauko@mail.muni.cz>
Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
2025-04-08 09:53:54 -07:00
Andy Kaylor
78905ce6cb
[CIR] Upstream support for logical not operations (#133966)
When unary operation support was initially upstreamed, the cir.cast
operation hadn't been upstreamed yet, so logical not wasn't included.
Since casts have now been added, this change adds support for logical
not.
2025-04-04 17:36:14 -07:00
Andy Kaylor
b0d0636026
[CIR] Upstream support for break and continue statements (#134181)
This adds ClangIR support for break and continue statements in loops.

Because only loops are currently implemented upstream in CIR, only
breaks in loops are supported here, but this same code will work (with
minor changes to the verification and cfg flattening) when switch
statements are upstreamed.
2025-04-04 15:53:11 -07:00
Andy Kaylor
a06ae976dc
[CIR] Upstream support for promoted types with unary plus/minus (#133829)
The initial upstreaming of unary operations left promoted types
unhandled for the unary plus and minus operators. This change implements
support for promoted types and performs a bit of related code cleanup.
2025-04-03 14:04:32 -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
Andy Kaylor
c57b9c233a
[CIR] Generate the nsw flag correctly for unary ops (#133815)
A previous checkin used a workaround to generate the nsw flag where
needed for unary ops. This change upstreams a subsequent change that was
made in the incubator to generate the flag correctly.
2025-04-02 15:48:55 -07:00
Michael Liao
a2ca2f3f10 [CIR] Fix cir-canonicalize pass upstreaming issues. NFC
- Fix typos in 'RemoveEmptyScope' pattern rewriting and combine 'match'
  and 'rewrite' into 'matchAndRewrite' as they are deprecated.
2025-04-02 02:14:17 -04:00
Andy Kaylor
9f3d8e8fb8
[CIR] Upstream support for while and do..while loops (#133157)
This adds basic support for while and do..while loops. Support for break
and continue are left for a subsequent patch.
2025-04-01 13:03:24 -07: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
Andy Kaylor
514f984a8d
[CIR][NFC] Fix warnings in ClangIR code (#133134)
This fixes unused variable warnings that have crept into the ClangIR
code. In some cases the variable will be needed later, but all unused
variables are being removed here. They can be reintroduced when they are
needed.
2025-03-31 09:55:07 -07:00
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