121 Commits

Author SHA1 Message Date
adams381
550e0d1b0d
[CIR] Add math and builtin intrinsics support (#175233)
This PR adds support for various math and builtin intrinsics to CIR:

## Changes

1. **Floating-point math intrinsics** - sqrt, cos, exp, exp2, floor,
fabs, sin, log, log2, log10, ceil, nearbyint, rint, round, trunc,
copysign, fma, fmax, fmin, pow
2. **Inverse trig, atan2, and roundeven** - acos, asin, atan, atan2,
roundeven
3. **Elementwise builtins** - add_sat, sub_sat, abs, max, min,
bitreverse, popcount, canonicalize
4. **Integer abs family** - abs, labs, llabs and their __builtin_
variants
5. **Prediction builtins** - __builtin_unpredictable
6. **Tests for rotate builtins** - Added OGCG checks for
__builtin_rotateleft/right

All changes include CIR, LLVM lowering, and OGCG test checks to verify
correctness.
2026-02-10 13:14:21 -08:00
David Rivera
a35b5940b4
[CIR][CUDA][HIP] Add NVPTX target info and CUDA/HIP global emission filtering (#177827)
related: #175871 

This patch adds foundational infra for device-side CUDA/HIP compilation
by introducing NVPTX target info and implementing the global emission
filtering logic.


  NVPTX Target Info to allows us to compile against that triple:
  - Add NVPTXABIInfo and NVPTXTargetCIRGenInfo classes
  - Wire up nvptx and nvptx64 triples in getTargetCIRGenInfo()
  - Add createNVPTXTargetCIRGenInfo() factory function

CUDA/HIP Global Emission Filtering (most of this is boilerplate from the
AST) This basically narrows down to:
- Skip host-only functions (no `__device__` attribute) when
`-fcuda-is-device`
   - Skip device-only functions (device without  host) on host side
- Always emit ` __global__` kernels and `__host__` `__device__`
functions on both sides
- Add `shouldEmitCUDAGlobalVar()` to handle variable emission
(device/constant/shared variables)
- Handle special cases: implicit host/device templates, lambda call
operators
2026-02-03 06:18:39 -05:00
David Rivera
2d12279528
[CIR][CUDA] Upstream device stub body emission and name mangling (#177790)
Part of #175871 

This patch adds the initial implementation of the CUDA/NV Runtimes
generating code for the device stub body. tested on CUDA. HIP coverage
to be added in a later PR.
2026-02-03 05:20:43 -05:00
Erich Keane
8f64d44ab4
[CIR] Apply 'side-effect' attribute logic to the function as well. (#177242)
Previous efforts in both the incubator and upstreamed added the 'pure'
and 'const' attribute lowering to calls. This patch completes that
effort by making it also appear on functions with this attribute.
2026-01-26 06:08:42 -08:00
Andy Kaylor
d29aa30fc3
[CIR] Upstream support for null and virtual method pointers (#176522)
This upstreams support for pointer-to-member value representations of
null and virtual member pointers and the lowering of virtual member
pointers.
2026-01-23 14:07:53 -08:00
Erich Keane
3b2ddab324
[CIR][NFCI] Add some infrastructure for function attributes (#176433)
While looking into the work required to add attributes to
functions/arguments/etc, I discovered a lot of overlap with calling
conventions, which I know others are working on. Rather than risk a
later conflict, this patch adds some basic infrastructure that will
hopefully limit the fallout. This patch is NFC as it doesn't modify the
IR at all, just adds a few calls to the 'right place' and adds some
arguments.

Additionally, it adds the calling convention type to the IR, but leaves
it empty, as it is useful to be able to pass around but not particularly
possible to define/make do anything yet.
2026-01-20 05:59:25 -08:00
Erich Keane
41dd36b8f8
[CIR] Implement codegen for inline assembly with output operands (#176006)
Part of: #153267

This is a continuation of: #154014

This patch handles output operands for inline assembly, taking the
original patch, but adding some additional tests, plus responding to all
of the original comments, plus doing some of my personal cleanup
(including extracting addVariableConstraints in a separate patch).

---------

Co-authored-by: Iris Shi <0.0@owo.li>
2026-01-15 11:28:23 -08:00
Andy Kaylor
7584c28063
[CIR] Upstream CIR method attribute handling (#174640)
This adds code for generating cir.method attributes and lowering them to
LLVM IR to implement support the C++ method pointer variables.
2026-01-13 16:35:04 -08:00
adams381
d524ecbf0f
[CIR] Add emitDeclInvariant for global with constant storage (#171915)
Implement emitDeclInvariant to emit llvm.invariant.start intrinsic for
global variables with constant storage. This enables optimizations by
marking when a global becomes read-only after initialization.

## Changes
- Add emitDeclInvariant and emitInvariantStart functions in
CIRGenCXX.cpp
- Add emitInvariantStart declaration in CIRGenFunction.h
- Update emitCXXGlobalVarDeclInit to call emitDeclInvariant for constant
storage globals after initialization
- Update getOrCreateCIRGlobal to set constant flag on globals with
constant storage
- Add comprehensive test covering positive and negative cases

## Implementation Details
The implementation handles address spaces correctly, dynamically
constructing the intrinsic name (e.g., invariant.start.p0,
invariant.start.p10) based on the pointer's address space. The intrinsic
is only emitted when optimizations are enabled, matching classic codegen
behavior.

## Testing
All tests pass (411/412, 1 unsupported). The test file includes CIR,
LLVM, and OGCG checks for both optimized and non-optimized builds.
2025-12-18 14:40:41 -08:00
Erich Keane
2e2e48f171
[OpenMP][CIR] Add basic infrastructure for CIR lowering (#171902)
This patch adds the basic infrastructure for lowering an OpenMP
directive, which should enable someone to take over the OpenMP lowering
in the future. It adds the lowering entry points to CIR in the same way
as OpenACC.

Note that this does nothing with any of the directives, which will
happen in a followup patch. No infrastructure for clauses is added
either, but that will come in a followup patch as well.
2025-12-15 06:07:37 -08:00
adams381
f195d5278f
[CIR] Support wide string literals in CIR codegen (#171541)
This PR migrates support for wide string literals from the incubator to
upstream.

## Changes

- Implement wide string literal support in
`getConstantArrayFromStringLiteral`
- Handle wchar_t, char16_t, and char32_t string literals
- Collect code units and create constant arrays with IntAttr elements
- Use ZeroAttr for null-filled strings

## Testing

- Copied `wide-string.cpp` test file from incubator
- Expanded test to include wchar_t test cases (incubator only had
char16_t and char32_t)
- All tests pass

---------

Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
2025-12-12 20:30:00 +00:00
Erich Keane
1dbff71312
[OpenACC][CIR] 'bind' lowering with identifier (#171749)
The bind clause specifies the name of the function to call on the
device, and takes either a string or identifier(per the standard):

"If the name is specified as an identifier, it is callled as if the name
were specified in the language being compiled. If the name is specified
as a string, the string is used for the procedure name unmodified".

The latter (as a string) is already implemented, this patch implements
the former. Unfortunately, no existing implementation of this in C++
seems to exist. Other languages, the 'name' of a function is sufficient
to identify it (in this case 'bind' can refer to undeclared functions),
so it is possible to figure out what the name should be. In C++ with
overloading (without a discriminator, ala-fortran), a name only names an
infinite overload set.

SO, in order to implement this, I've decided that the 'called as'
(bound) function must have the same signature as the one marked by the
'routine'. This is trivially sensible in non-member functions, however
requires a bit more thought for member(and thus lambda-call-operators)
functions. In this case, we 'promote' the type of the function to a
'free' function by turning the implicit 'this' to an explicit 'this'.

I believe this is the most sensible and reasonable way to implement
this, and really the only way to make something usable.
2025-12-12 06:04:47 -08:00
Henrich Lauko
b2f36149d8
[CIR] Implement function personality attribute and its lowering (#171001) 2025-12-12 01:55:47 +01:00
Amr Hesham
2acefcd9d5
[CIR] Add support for SourceLocExpr (#171492)
Add support for the SourceLocExpr
2025-12-10 15:50:44 +01:00
adams381
0a2e56df64
[CIR] Add support for thread-local storage (TLS) (#168662)
This commit adds full support for thread-local storage variables in
ClangIR, including code generation, lowering to LLVM IR, and
comprehensive testing.

Changes include:
- Added CIR_TLSModel enum with 4 TLS models (GeneralDynamic,
LocalDynamic, InitialExec, LocalExec) to CIROps.td
- Extended GlobalOp with optional tls_model attribute
- Extended GetGlobalOp with thread_local unit attribute
- Added verification to ensure thread_local GetGlobalOp references
globals with tls_model set
- Implemented GetDefaultCIRTLSModel() and setTLSMode() in CIRGenModule
- Updated getAddrOfGlobalVar() to handle TLS access
- Removed MissingFeatures assertions for TLS operations
- Added lowering of GetGlobalOp with TLS to llvm.threadlocal.address
intrinsic
- Added lowering of GlobalOp with tls_model to LLVM thread_local globals
- Added comprehensive test with CIR, LLVM, and OGCG checks

Known limitations (matching incubator):
- Static local TLS variables not yet implemented
- TLS_Dynamic with wrapper functions not yet implemented

Fixes #153270
2025-12-09 15:11:01 -08:00
Andy Kaylor
87bf5ee238
[CIR] Add basic support for data member pointers (#170939)
This adds the minimum support for C++ data member pointer variables.
2025-12-09 12:55:47 -08:00
Andres-Salamanca
762a171b3d
[CIR] Upstream support for cir.indirectbr (#169967)
This PR upstreams support for the `cir.indirectBr` operation, which is
used to implement GCC’s labels-as-values `indirect goto`.
To ensure correct lowering, we introduce precise bookkeeping to
associate each `block_address` operation with its corresponding `label`
op. This is required because a `block_address` may be emitted before the
`label` it refers to. In such cases, the reference is deferred and later
resolved by `resolveBlockAddresses`, which guarantees that all
`indirectBr` successors are emitted in the correct and fully resolved
order.
2025-12-05 16:12:37 -08:00
Erich Keane
6dd639ec9e
[CIR][OpenACC] Implement 'routine' lowering + seq clause (#170207)
The 'routine' construct just adds a acc.routine element to the global
module, which contains all of the information about the directive. it
contains a reference to the function, which also contains a reference to
the acc.routine, which this generates.

This handles both the implicit-func version (where the routine is
    spelled without parens, and just applies to the next function) and
the explicit-func version (where the routine is spelled with the func
    name in parens).

The AST stores the directive in an OpenACCRoutineDeclAttr in the
implicit case, so we can emit that when we hit the function declaration.
The explicit case is held in an OpenACCRoutineAnnotAttr on the function,
however, when we emit the function we haven't necessarily seen the
construct yet, so we can't depend on that attribute. Instead, we save up
the list in Sema so that we can emit them all at the end.

This results in the tests getting really hard to read (because ordering
is a little awkward based on spelling, with no way to fix it), so we
instead split the tests up based on topic.

One last thing: Flang spends some time determining if the clause lists
of two routines on the same function are identical, and omits the
duplicates. However, it seems to do a poor job on this when the ordering
isn't the same, or references are slightly different. This patch doesn't
bother trying that, and instead emits all, trusting the ACC dialect to
remove duplicates/handle duplicates gracefully.

Note; This doesn't cause emission of functions that would otherwise not
be emitted, but DOES emit routine references based on which function
they are attached to.
2025-12-02 11:55:14 -08:00
Henrich Lauko
04dd71cb0b
[CIR] Align inline-kind FuncOp attribute with incubator (#170050)
Switches to more efficient explicit enum property instead of a wrapped
storage, simplifying the string representation. The attribute is now
placed before the symbol name for consistency with other FuncOp
attributes. FileCheck patterns are also simplified to match only the
attributes under test.
2025-12-02 09:53:50 +01:00
Erich Keane
4e9b76e23b
[OpenACC][CIR] 'declare' lowering for globals/ns/struct-scopes (+create) (#169409)
This patch does the lowering for a 'declare' construct that is not a
function-local-scope. It also does the lowering for 'create', which has
an entry-op of create and exit-op of delete.

Global/NS/Struct scope 'declare's emit a single 'acc_ctor' and
'acc_dtor' (except in the case of 'link') per variable referenced. The
ctor is the entry op followed by a declare_enter. The dtor is a
get_device_ptr, followed by a declare_exit, followed by a delete(exit
op). This DOES include any necessary bounds.

This patch implements all of the above. We use a separate 'visitor' for
the clauses here since it is particularly different from the other uses,
AND there are only 4 valid clauses. Additionally, we had to split the
modifier conversion into its own 'helpers' file, which will hopefully
get some additional use in the future.
2025-11-25 05:56:34 -08:00
Andy Kaylor
ad1be4a589
[CIR] Add handling for static data members (#169134)
This adds some trivial handling to force emitting of child decls inside
C++ records.
2025-11-24 09:27:38 -08:00
Hendrik Hübner
03f4d4d492
[CIR] Add CxxCTorAttr, CxxDTorAttr, CxxAssignAttr, CxxSpecialMemberAttr to cir::FuncOp (#167975)
This PR adds a special member attribute to `cir::FuncOp`. This attribute
is also present in the incubator repo. Additionally, I added a
"is_trivial" flag, to mark trivial members. I think that might be useful
when trying to replace calls to the copy constructor with memcpy for
example, but please let me know your thoughts on this. [Here in the
incubator
repo](823e943d1b/clang/lib/CIR/Dialect/Transforms/LoweringPrepare.cpp (L1537-L1550))
this function is called `LowerTrivialConstructorCall`, but I don't see a
check that ensures the constructor is actually trivial.
2025-11-19 15:57:34 -08:00
Andy Kaylor
3e499e9427
[CIR] Add support for common linkage (#168613)
Add support for marking global variables with common linkage.
2025-11-18 14:51:23 -08:00
Andy Kaylor
56b1d42a65
[CIR] Mark globals as constants (#168463)
We previously added support for marking GlobalOp operations as constant,
but the handling to actually do so was left mostly unimplemented. This
fills in the missing pieces.
2025-11-18 12:14:09 -08:00
David Rivera
8f9071651d
[CIR] Upstream AddressSpace conversions support (#161212)
related: #160386 
Add support for address space conversions in CIR.

- Added `createAddrSpaceCast` methods to `CIRBaseBuilderTy` to handle
address space conversions
- Implemented address space conversion handling in `emitCastLValue` and
`VisitCastExpr`
- Added `performAddrSpaceCast` method to `TargetCIRGenInfo` for
target-specific address space casting
- Added `getLangTempAllocaAddressSpace` to `CIRGenModule` to get the
language-specific address space for temporary allocations
- Added a test file `address-space-conversion.cpp` to verify address
space conversion functionality
2025-11-12 19:35:45 -05:00
Andres-Salamanca
217f0e54c9
[CIR][NFC] Update TypeCache file to use MLIR-style camel case (#165060)
This PR updates the file `CIRGenTypeCache` to use MLIR-style camel case
naming.The change was inspired by the discussion here:
https://github.com/llvm/llvm-project/pull/164180#discussion_r2461444730
2025-10-28 19:26:33 -05:00
Jakub Kuderski
9a2e825c13
[clang][CIR][mlir] Migrate to free create functions. NFC. (#164656)
See
https://discourse.llvm.org/t/psa-opty-create-now-with-100-more-tab-complete/87339.

I plan to make these deprecated in
https://github.com/llvm/llvm-project/pull/164649.
2025-10-22 14:30:19 -04:00
jiang1997
116b94ce79
[CIR] Add array new cookie support (#163649)
This patch adds the minimal support for array cookies needed to enable
ClangIR generation for an array new expression that requires cookies but
does not require an explicit initializer.

This only provides the cookie support for the base Itanium CXXABI.
Different cookie calculations are required for AppleARM64, which will be
added in a subsequent patch.

Ported from ClangIR incubator PR
https://github.com/llvm/clangir/pull/1297.
This is the second PR in a series intended to address
https://github.com/llvm/llvm-project/issues/160383.

---------

Co-authored-by: Andy Kaylor <akaylor@nvidia.com>
2025-10-21 13:27:54 -07:00
Morris Hafner
3155b05044
[CIR] Implement inline builtin functions (#163911)
This patch implements the handling of inline builtin functions in CIR.
There is a known limitation in CIR where direct calls to shadowed inline
builtin functions are generated instead of the intrinsic. This is
expected to be fixed by the introduction of the nobuiltin attribute in a
future patch.
2025-10-22 00:17:52 +08:00
Morris Hafner
4aba5edd49
[CIR] Add inline function attributes (#162866)
Unlike the incubator, this adds the inline attribute directly to FuncOp
instead of adding the ExtraFnAttr dict.

This adds three new optional keywords to CIR: inline_always,
inline_never and inline_hint. Just like in OGCG -O0 implies inline_never
on functions withoutt the C++ `inline` keyword and no other
inlining-related attribute.

This patch also adapts all tests that use functions so they account for
LLVM attributes being attached now.
2025-10-17 12:02:09 +07:00
Andy Kaylor
85265a93cc
[CIR] Upstream support for variable length arrays (#163297)
This adds the code needed to emit alloca operations for variable length
array local variables and the necessary calls to stacksave and
stackrestore to adjust the local stack as the array variables go in an
out of scope.
2025-10-15 13:55:18 -07:00
Andy Kaylor
645745f9de
[CIR] Add support for global ctor/dtor attributes (#163247)
This adds support for adding the `global_ctor` or `global_dtor`
attribute to the CIR representation of functions defined with
`__attribute__((constructor))` or `__attribute__((destructor))` and
adding them to the `@llvm.global_ctors` or `@llvm.global_dtors` list
during lowering to LLVM IR.
2025-10-13 17:07:05 -07:00
Andy Kaylor
4e5306745a
[CIR] Add initial support for dynamic cast (#162337)
This adds support for dynamic cast handling and generating
`cir.dyn_cast` operations and `cir.dyn_cast_info` attributes.

This does not include support for lowering the dynamic cast to LLVM IR,
which will require changes to the LoweringPrepare pass that will be made
in a future change.

This also does not yet handle dynamic cast to void or exact dynamic
casts.
2025-10-09 10:24:09 -07:00
David Rivera
3896212cea
[CIR] Implement Target-specific address space handling support for PointerType (#161028)
This PR adds support for address spaces in CIR pointer types by:

1. Introducing a `TargetAddressSpaceAttr` to represent target-specific
numeric address spaces (A Lang-specific attribute is to be implemented
in a different PR)
2. Extending the `PointerType` to include an optional address space
parameter
3. Adding helper methods in `CIRBaseBuilder` to create pointers with
address spaces
4. Implementing custom parsers and printers for address space attributes
5. Updating the LLVM lowering to properly handle address spaces when
converting CIR to LLVM IR

The implementation allows for creating pointers with specific address
spaces, which is necessary for supporting language features like Clang's
`__attribute__((address_space(N)))`. Address spaces are preserved
through the CIR representation and correctly lowered to LLVM IR.
2025-10-03 21:40:54 -04:00
Andres-Salamanca
16f5a85fb6
[CIR] Initial support for emitting coroutine body (#161616)
This PR adds new `FuncOp` attributes (`coroutine` and `builtin`) and
begins the implementation of the `emitCoroutineBody` function. Feature
markers were also added for guidance in future PRs.
2025-10-03 13:56:53 -05:00
Andy Kaylor
d6449b55cd
[CIR] Add support for emitting predefined expressions (#161757)
This adds support for emitting pseudo-macro expressions that represent
some form of the name of a function (such as `__func__` or
`__PRETTY_FUNCTION__`) as l-values.
2025-10-03 10:37:06 -07:00
Andy Kaylor
ca84f2aa3b
[CIR] Upstream support for generating global ctor regions (#161298)
This adds support for handling global variables with non-trivial
constructors. The constructor call is emitted in CIR as a 'ctor' region
associated with the global definition. This form of global definition
cannot be lowered to LLVM IR yet.

A later change will add support in LoweringPrepare to move the ctor code
into a __cxx_global_var_init() function and add that function to the
list of global global ctors, but for now we must stop at the initial CIR
generation.
2025-09-30 14:20:13 -07:00
Amr Hesham
6caa0d05c2
[CIR] Upstream RTTI Builder & RTTI for VTable Definitions (#160002)
Upstream the RTTI builder with helpers and used them in the VTable
Definitions

Issue https://github.com/llvm/llvm-project/issues/154992
2025-09-30 20:16:04 +02:00
Andy Kaylor
782ab835dc
[CIR] Set the module name to the input filename (#160934)
This sets the MLIR module name to the main filename (according to the
SourceManager), if one is available. The module name gets used when
creating global init functions, so we will need it to be set.
2025-09-29 10:27:34 -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
88c3825889
[CIR] Add support for constructors with VTT parameters (#156521)
This adds the support for implicit VTT arguments in constructors.
2025-09-03 12:06:56 -07:00
Matheus Izvekov
2ec71d93ad
[clang] NFC: introduce Type::getAsEnumDecl, and cast variants for all TagDecls (#155463)
And make use of those.

These changes are split from prior PR #155028, in order to decrease the
size of that PR and facilitate review.
2025-08-26 16:05:59 -03:00
Matheus Izvekov
dc8596d548
[clang] NFC: change more places to use Type::getAsTagDecl and friends (#155313)
This changes a bunch of places which use getAs<TagType>, including
derived types, just to obtain the tag definition.

This is preparation for #155028, offloading all the changes that PR used
to introduce which don't depend on any new helpers.
2025-08-25 20:18:56 -03:00
Andy Kaylor
480d528053
[CIR][NFC] Fix build warning in getCIRSourceLanguage (#155029)
The getCIRSourceLanguage wasn't returning a value if the source language
was anything other than C or C++. This change updates that function to
return a std::optional value and only adds the source language attribute
if one was returned.
2025-08-22 14:57:31 -07:00
Andy Kaylor
ffe3768dfb
[CIR] Add support for emitting vtables (#154808)
This adds a simplified version of the code to emit vtables. It does not
yet handle RTTI or cases that require multiple vtables.
2025-08-22 13:26:17 -07:00
7mile
761125f267
[CIR][Dialect] Add SourceLangAttr (#152511)
This patch upstreams `SourceLangAttr` and its CodeGen logic in the CGM,
which encodes the source language in CIR.
2025-08-21 09:51:37 -07: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
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
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