155 Commits

Author SHA1 Message Date
Erich Keane
297a70c9b5
[CIR] Implement global decomposition declarations (#190364)
No real challenge to these, it is effectively a copy/paste of the
classic codegen as it just requires we properly emit the holding
variable. The rest falls out of the rest of our handling of variables.
2026-04-06 07:38:21 -07:00
Henrich Lauko
942e1082ac
[CIR] Convert global_visibility from attribute to property (#190488)
Replace CIR_VisibilityAttr with
DefaultValuedProp<EnumProp<CIR_VisibilityKind>>
for global_visibility on GlobalOp and FuncOp. This removes the need for
custom
parse/print functions and simplifies callers to use direct enum values
instead
of wrapping/unwrapping VisibilityAttr.
2026-04-05 07:35:53 +02:00
Andy Kaylor
68b6a27771
[CIR] Use destination type when emitting constant function ptrs (#189741)
This updates the CIR constant emitter to use the correct destination
type when emitting a constant initializer for a structure that might be
initialized with non-prototyped function pointers. We were previously
using the type from whatever function declaration we had, but this may
not be the correct type.

This change also updates the `replaceUsesOfNonProtoTypeWithRealFunction`
to ignore global initializer uses, which do not need to be updated after
this change.
2026-04-03 10:58:46 -07:00
Erich Keane
2c734b3951
[CIR] Implement top level 'ExportDecl' emission (#190286)
This is a pretty simple one, its just a type of decl-context. The actual
exporty-ness is handled on a per-declaration basis.

This patch just makes sure we emit them, as I suspect this will reveal
quite a bit more issues in module code I suspect.
2026-04-03 05:59:25 -07:00
David Rivera
e3cbd9984a
[CIR][AMDGPU] Lower Language specific address spaces and implement AMDGPU target (#179084) 2026-04-02 03:00:14 -04:00
Chaitanya
dbc206f35d
[CIR][CIRGen] Support for section atttribute on cir.global (#188200)
Upstreaming clangIR PR: https://github.com/llvm/clangir/pull/422

This PR implement support for `__attribute__((section("name")))` on
global variables in ClangIR, matching OGCG behavior.
2026-04-02 09:58:17 +05:30
Jan Leyonberg
e04f8bb754
[CIR] Add OMP module attributes to MLIR module (#189394)
This patch adds OpenMP attributes to the MLIR module op used to
communicate various settings e.g. thread options and offloading
information.

Co-authored-by: Claude Opus 4.6 <noreply@anthropic.com>
2026-03-31 12:52:41 -04:00
Andy Kaylor
a0ffdf2850
[CIR] Allow replacement of a structor declaration with an alias (#188320)
We had an errorNYI diagnostic to trigger when we generated an alias for
a ctor or dtor that had an existing declaration. Because functions are
used via flat symbol references, all that is needed is to erase the old
declaration. This change does that.
2026-03-30 13:51:29 -07:00
Michael Liao
3d6c0a683c [CIR] Match codegen change on global temporary
- Match codgen chagne from cbe9891b44d3d1c15bd8f632d6d84e486751e530
2026-03-27 02:26:25 -04:00
Chaitanya
83451d8d4d
[CIR][AMDGPU] Add AMDGPU-specific function attributes for HIP kernels (#188007)
Upstreaming clangIR PR: https://github.com/llvm/clangir/pull/2091

This patch adds support for AMDGPU-specific function attributes for HIP
kernels

Added setTargetAttributes for AMDGPUTargetCIRGenInfo to set kernel
attributes
Added generic string attribute handler in amendFunction to translate
string-values with "cir." prefix function attributes to LLVM function
attributes
Follows OGCG AMDGPU implementation from
"clang/lib/CodeGen/Targets/AMDGPU.cpp".
2026-03-27 11:54:29 +05:30
Perdixky
b6151c4f32
[CIR] Implement pointer type RTTI (#184011)
#163601 
#163850

---------

Signed-off-by: Nikita B <n2h9z4@gmail.com>
Co-authored-by: Nikita B <n2h9z4@gmail.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
2026-03-24 11:30:44 -07:00
Henrich Lauko
2d01df187f
[CIR] Fix reference alignment to use pointee type (#186667)
getNaturalTypeAlignment on a reference type returned pointer alignment
instead of pointee alignment. Pass the pointee type with
forPointeeType=true to match traditional codegen's
getNaturalPointeeTypeAlignment behavior. Fix applies to both argument
and return type attribute construction paths.
2026-03-21 11:14:53 +00:00
David Rivera
0ec6e1d21e
[CIR] Address Space support for GlobalOps (#179082)
Related: https://github.com/llvm/llvm-project/issues/179278,
https://github.com/llvm/llvm-project/issues/160386

Extends cir.global to accept address space attributes. Globals can now
specify either `target_address_space(N)` or
`lang_address_space(offload_*)`. Address spaces are also preserved
throughout get_global ops.
2026-03-20 14:01:20 -04:00
Chaitanya
a4f97f0d90
[CIR][AMDGPU] Add module flags for AMDGPU target using amendOperation of CIRDialectLLVMIRTranslationInterface (#186073)
Add the amendOperation override to handle CIR dialect attributes during
MLIR-to-LLVM IR translation. This dispatches to  amendModule for ModuleOp,
enabling module metadata.

This PR also adds support to emit AMDGPU-specific module flags
amdhsa_code_object_version and amdgpu_printf_kind to match OGCG
behavior.

In CIRGenModule, the flags are stored as CIR module attributes:

cir.amdhsa_code_object_version (integer)
cir.amdgpu_printf_kind (string: "hostcall" or "buffered")
During lowering to LLVM IR (in LowerToLLVMIR.cpp), these attributes are
converted to LLVM module flags.

Upstreaming basic changes from clangIR PRs: 

61e9ebd9f8
https://github.com/llvm/clangir/pull/768
https://github.com/llvm/clangir/pull/773
https://github.com/llvm/clangir/pull/2100
2026-03-20 12:20:42 +05:30
Andy Kaylor
9eb852c062
[CIR] Implement global variable replacement in global view (#186168)
This change upstreams the CIR implementation of global variable
replacement handling for cases where the global was used in a
cir.global_view operation, either as an initializer for another global
or as a constant ptr.
2026-03-19 15:44:16 -07:00
Erich Keane
1b904e948a
[CIR][NFC] Unify the 'null data member attr' getters (#186876)
In preperation of actually lowering data members as fields to a record
type, this patch does a minor refactor to make their single current use
have a slightly simpler interface. This will prevent us from having to
copy/paste this later.

Also, this patch removes a pair of now-orphaned builders, instead
preferring to use the ones that come from the parent builder type.
2026-03-18 06:28:43 -07:00
Andy Kaylor
f6f57f0da6
[CIR] Add handling for nounwind attributes (#187096)
This adds support for setting the `nounwind` attribute on declarations
and call sites in CIR.

Currently, we have both `nothrow` and `nounwind` in CIR. I've chosen to
use `nothrow` in this PR because it was the most common. I plan to
combine them, using `nounwind` everywhere since that's the LLVM IR
spelling, but that's a more invasive so I'd like to defer it to a future
change.
2026-03-17 17:15:39 -07:00
Chaitanya
65d0ef49e1
[CIR][AMDGPU] Add AMDGPU target support to CIR CodeGen (#185819)
This PR adds AMDGPUTargetCIRGenInfo and AMDGPUABIInfo to handle the
amdgcn triple in CIR code generation, along with a
basic HIP codegen test.
2026-03-12 10:20:02 +05:30
Zaky Hermawan
e86d4fb04b
[CIR][CUDA] Handle local, __device__, __shared__, and __constant__ variables (#184248)
Support local, `__device__`, `__shared__`, and `__constant__` variables.
Mark device variables as `externally_initialized`.

References: https://github.com/llvm/llvm-project/issues/175871,
https://github.com/llvm/llvm-project/issues/179278,
https://github.com/llvm/clangir/pull/1368,
https://github.com/llvm/clangir/pull/1394

---------

Signed-off-by: ZakyHermawan <zaky.hermawan9615@gmail.com>
2026-03-11 11:29:34 -07:00
Andy Kaylor
a63249cb98
[CIR] Set builtin attribute for new and delete calls (#184920)
This adds code to set the `builtin` attribute when needed on operator
new and delete calls. This also required setting `nobuiltin` for
replaceable global allocation functions and threading the `builtin`
attribute through the LLVM dialect.
2026-03-11 09:31:47 -07:00
Erich Keane
8a25f9534a
[CIR] Implement non-odr use of reference type lowering (#185720)
This is used somewhat rarely, but is a pretty simple emission of
pointers, and ends up using infrastructure we already have.
Additionally, this is the first use of `getNaturalTypeAlignment` that
uses the `pointee` argument, so this adds the implementation there,
which includes some alignment work for CXXRecordDecls, so this
implements that as well.
2026-03-11 06:26:13 -07:00
Erich Keane
6bc0faf67d
[CIR] Implement deferred V-Table emission (#185655)
We are currently only emitting Vtables that have an 'immediate' need to
emit. There rest, we are supposed to add to a list and emit at the end
of the translation unit if necessary. This patch implements that
infrastructure.

The test added is from classic-codegen and came in at the same time as
the deferred vtable emission over there, and only works with deferred
vtable emission, and while it does test the deferred emission, tests
quite a bit more than that. AND since it came in with the same
functionality in classic codegen, seemed to make sense to come in here
too.
2026-03-11 06:25:51 -07:00
Erich Keane
7484eb7488
[CIR] Ensure strings are null-terminated, better deal with trailing null (#185513)
Our current implementation of string lowering did some work to remove
extra trailing zeros, plus do a 'zero' constant. That is unchanged by
this patch. However, this patch ALSO ensures that we do the 'remove
extra trailing zeros' to remove ALL trailing zeros, which likely has
canonicalization benefits later on.

However, the real benefit of this patch is to make string emission by
default emit a null-terminator, which fixes the virtual table 'name'
field get lowered correctly. We do this by making the builder::getString
function take an argument (true by default) that will ensure we add a
null terminator if necessary.

This reflects the llvm::ConstantDataArray::getString function, which has
the same functionality. However, doing this during lowering seems
incorrect, since the FE is the one that knows whether these null
terminators are necessary. There is not currently an 'opt out' use of
the behavior, but the functionality is left in place to better reflect
the ConstantDataArray::getString function interface.

Note with the tests that this fixes some inconsistencies between LLVM
and OGCG lowering.
2026-03-10 07:02:36 -07:00
Erich Keane
c186db874b
[CIR] Implement 'typeid' operator lowering (#184449)
This patch adds typeid lowering, which uses a lot of the infrastructure
from dynamic_cast. However, this adds a `get_type_info` operation that
gets the type info out of a vtable pointer as well, which lets the
offset be handled by the ABI specific lowering code.
2026-03-05 06:07:13 -08:00
Andy Kaylor
954e5e7aa2
[CIR] Upstream global variable replacement (#184686)
This change upstreams the CIR implementation of global variable
replacement. When we get a call to get or create a global variable using
a type that does not match the previous type for a variable of the same
name, we need to replace the old definition with the new one. In classic
codegen that was as simple as replaceAllUses+eraseFromParent, but in CIR
because we have typed pointers, we need to visit the uses and update
them with bitcasts to reflect the new type.
2026-03-04 13:50:54 -08:00
sweiglbosker
a368bd4049
[CIR][CUDA]: Handle duplicate mangled names (#183976)
Replace the NYI for duplicate function defs with the proper diagnostic
logic from OG codegen.

Related: #175871, #179278
2026-03-03 07:04:49 -05:00
Andy Kaylor
a14d8b2e36
[CIR] Upstream vtable thunk handling (#183629)
This implements vtable thunk handling in CIR based on the incubator
code, but also compared against the latest Clang LLVM IR codegen.

Eventually, we'll want to create CIR abstractions for all of this and
move the CXXABI-specific details into the CXXABI lowering pass. For now,
we just implement it directly in codegen.
2026-03-02 14:15:52 -08:00
David Rivera
29f32e674a
[CIR][CUDA][HIP] Emit host-side kernel calls (#179809)
Related: https://github.com/llvm/llvm-project/issues/179278,
https://github.com/llvm/llvm-project/issues/175871
2026-02-25 01:51:26 -05:00
Erich Keane
06c673ed49
[CIR] Add support for globals reference variables (#182608)
These are fairly simple, particularly if they don't need special
cleanups (which is left unimplemented), but this provides init for a
global reference variable.
2026-02-24 06:23:44 -08:00
Erich Keane
94d4cb5f7b
[CIR] Implement basic attributes for this/arguments (#182910)
Similar to what I did for return types in #181052, this patch adds
support for 4 of the function attributes on arguments (noundef, nonnull,
    dereferenceable, align).  The logic for these is all pretty similar
(though SLIGHTLY different enough from eachother unfortunately), so they
are being submitted together. This handles 'this' and normal arguments.
2026-02-24 06:23:14 -08:00
Gauravsingh Sisodia
14b213f4b1
[CIR][CodeGen] Use MapVector instead of StringMap for replacements (#181969)
When using llvm::StringMap transitive replacements may be out of order, so use llvm::MapVector which is also used in Clang's LLVM IR CodeGen.
2026-02-18 14:41:03 -08:00
Erich Keane
b16e7de0f2
[CIR] Implement func/call return-attributes (#181052)
This patch implements the infrastructure for return attributes on
function/call operations, a little of the common infrastructure for arg
attributes on the same, and 4 return attributes lowering: noundef
nonnull
dereferenceable
align

These 4 common attributes are all pretty reasonable/common, so these
will change a lot of tests.

This patch chooses to just use the LLVM-IR-Dialect variant of these
attributes (as a NamedAttr), which means no changes to the dialect or
lowering are necessary.
2026-02-13 06:20:27 -08:00
Bruno Cardoso Lopes
da9c513c87
[CIR][LoweringPrepare] Emit guard variables for static local initialization (#179828)
This implements the lowering of static local variables with the Itanium C++ ABI
guard variable pattern in LoweringPrepare. This is initial support, errorNYI covering all that hasn't been added just yet.

When a GlobalOp has the static_local attribute and a ctor region, this pass:
1. Creates a guard variable global (mangled name from AST)
2. Inserts the guard check pattern at each GetGlobalOp use site:
   - Load guard byte with acquire ordering
   - If zero, call __cxa_guard_acquire
   - If acquire returns non-zero, inline the ctor region code
   - Call __cxa_guard_release
3. Clears the static_local attribute and ctor region from the GlobalOp

Once the new design doc lands I'll add more information over there.
2026-02-13 06:22:44 +01:00
Bruno Cardoso Lopes
b5c41b4bde
[CIR] Add CIRGen support for static local variables with non-constant initializers (#179827)
This adds CIRGen infrastructure for C++ function-local static variables
that require guarded initialization (Itanium C++ ABI).

Changes:
- Add ASTVarDeclAttr to carry VarDecl AST through the pipeline
- Add emitGuardedInit() to CIRGenCXXABI for guarded initialization
- Add emitCXXGuardedInit() to CIRGenFunction
- Replace NYI in addInitializerToStaticVarDecl() with ctor region emission
- Set static_local attribute on GlobalOp and GetGlobalOp

This doesn't unwrap the high level constructs just yet - The global's ctor region contains the initialization code, which will be lowered by LoweringPrepare to emit the actual guard variable pattern with __cxa_guard_acquire/__cxa_guard_release calls.
2026-02-13 04:45:51 +01:00
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