23 Commits

Author SHA1 Message Date
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
David Olsen
c695a32576
[CIR] Call code gen; create empty cir.func op (#113483)
Finish hooking up ClangIR code gen into the Clang control flow,
initializing enough that basic code gen is possible.

Add an almost empty `cir.func` op to the ClangIR dialect. Currently the
only property of the function is its name. Add the code necessary to
code gen a cir.func op.

Create essentially empty files
clang/lib/CIR/Dialect/IR/{CIRAttrs.cpp,CIRTypes.cpp}. These will be
filled in later as attributes and types are defined in the ClangIR
dialect.

(Part of upstreaming the ClangIR incubator project into LLVM.)
2024-11-05 11:16:30 -08:00
Nathan Lanza
1bb52e9462
[CIR] Build out AST consumer patterns to reach the entry point into CIRGen
Build out the necessary infrastructure for the main entry point into
ClangIR generation -- CIRGenModule. A set of boilerplate classes exist
to facilitate this -- CIRGenerator, CIRGenAction, EmitCIRAction and
CIRGenConsumer. These all mirror the corresponding types from LLVM
generation by Clang's CodeGen.

The main entry point to CIR generation is
`CIRGenModule::buildTopLevelDecl`. It is currently just an empty
function. We've added a test to ensure that the pipeline reaches this
point and doesn't fail, but does nothing else. This will be removed in
one of the subsequent patches that'll add basic `cir.func` emission.

This patch also re-adds `-emit-cir` to the driver. lib/Driver/Driver.cpp
requires that a driver flag exists to facilirate the selection of the
right actions for the driver to create. Without a driver flag you get
the standard behaviors of `-S`, `-c`, etc. If we want to emit CIR IR
and, eventually, bytecode we'll need a driver flag to force this. This
is why `-emit-llvm` is a driver flag. Notably, `-emit-llvm-bc` as a cc1
flag doesn't ever do the right thing. Without a driver flag it is
incorrectly ignored and an executable is emitted. With `-S` a file named
`something.s` is emitted which actually contains bitcode.

Reviewers: AaronBallman, MaskRay, bcardosolopes

Reviewed By: bcardosolopes, AaronBallman

Pull Request: https://github.com/llvm/llvm-project/pull/91007
2024-10-09 14:20:50 -04:00
Nathan Lanza
dd0fe4fb74
[CIR] Add .clang-tidy files for ClangIR specific coding style rules
https://llvm.github.io/clangir/GettingStarted/coding-guideline.html

Reviewers: dkolsen-pgi, bcardosolopes, erichkeane

Reviewed By: erichkeane, dkolsen-pgi

Pull Request: https://github.com/llvm/llvm-project/pull/111417
2024-10-07 15:55:03 -04:00
Nathan Lanza
10661ba240
[CIR][NFC] Add scaffolding for the CIR dialect and CIROps.td
This adds no real content, it just incrementally adds some scaffolding
necessary to enable a future patch to just add our first few ops.

Test Plan:
```
$ cmake -Sllvm -Bbuild -DCLANG_ENABLE_CIR=1 \
  -DLLVM_ENABLE_PROJECTS='clang;mlir' \
  -DCMAKE_BUILD_TYPE=Release -GNinja
$ ninja -C build check-clang
$ ninja -C build MLIRCIROpsIncGen
$ ninja -C build MLIRCIR
```

Reviewers: AaronBallman, erichkeane, bcardosolopes

Reviewed By: erichkeane, AaronBallman, bcardosolopes

Pull Request: https://github.com/llvm/llvm-project/pull/86080
2024-04-24 22:26:40 -04:00
Nathan Lanza
44de2bb694
[CIR][cmake] Add support for cmake variable CLANG_ENABLE_CIR
Introduce a cmake variable that guards the inclusion of ClangIR into the
build of clang. Guard that we aren't trying to build without MLIR. Add
two subdirectories that, as of now, don't do anything.

Reviewers: bcardosolopes, erichkeane, petrhosek, Ericson2314

Reviewed By: bcardosolopes

Pull Request: https://github.com/llvm/llvm-project/pull/86078
2024-04-11 16:56:31 -04:00