aboutsummaryrefslogtreecommitdiff
path: root/mlir/lib/Target/Cpp/TranslateToCpp.cpp
AgeCommit message (Collapse)AuthorFilesLines
6 hours[mlir][EmitC]Allow Fields to have initial values (#151437)Jaden Angella1-26/+22
This will ensure that: - The `field` of a class can have an initial value - The `field` op is emitted correctly - The `getfield` op is emitted correctly
8 days[mlir][NFC] Use `getDefiningOp<OpTy>()` instead of ↵Longsheng Mou1-3/+2
`dyn_cast<OpTy>(getDefiningOp())` (#150428) This PR uses `val.getDefiningOp<OpTy>()` to replace `dyn_cast<OpTy>(val.getDefiningOp())` , `dyn_cast_or_null<OpTy>(val.getDefiningOp())` and `dyn_cast_if_present<OpTy>(val.getDefiningOp())`.
8 days[mlir] Remove unused includes (NFC) (#150476)Kazu Hirata1-3/+0
These are identified by misc-include-cleaner. I've filtered out those that break builds. Also, I'm staying away from llvm-config.h, config.h, and Compiler.h, which likely cause platform- or compiler-specific build failures.
2025-07-10[MLIR][Target/Cpp] Fix variable naming conflict for function declarations ↵Niklas Degener1-0/+1
(#147927) This is a fix for https://github.com/llvm/llvm-project/pull/136102. It missed scoping for `DeclareFuncOps`. In scenarios with multiple function declarations, the `valueMapper` wasn't updated and later uses of values in other functions still used the assigned names in prior functions. This is visible in the reproducer here https://github.com/iree-org/iree/issues/21303: Although the counter for variable enumeration was reset, as it is visible for the local vars, the function arguments were mapped to old names. Due to this mapping, the counter was never increased, and the local variables conflicted with the arguments. This fix adds proper scoping for declarations and a test-case to cover the scenario with multiple `DeclareFuncOps`.
2025-07-08[MLIR][Target/Cpp] Natural induction variable naming. (#136102)Niklas Degener1-27/+84
Changed naming of loop induction variables to follow natural naming (i, j, k, ...). This helps readability and locating positions referred to. Created new scopes to represent different behavior at function and loop level, to still enable re-using value names between different functions (as before). Removed unused scoping at other levels.
2025-06-29Add`final` specifier to the classop (#145977)Jaden Angella1-2/+4
In some use cases of the `ClassOp`, eg MLGO, we would like to be able to declare the class as final. This specifier allows for that.
2025-06-26[mlir][emitC] Add support to emitter for `classop`, `fieldop` and ↵Jaden Angella1-8/+51
`getfieldop` (#145605) Add support to the emitter for `ClassOp`, `FieldOp` and `GetFieldOp`. These ops were introduced in #141158
2025-06-18[mlir][emitc] Make CExpression trait into interface (#142771)Kirill Chibisov1-3/+3
By defining `CExpressionInterface`, we move the side effect detection logic from `emitc.expression` into the individual operations implementing the interface allowing operations to gradually tune the side effect. It also allows checking for side effects each operation individually.
2025-05-27Fix handling of integer template argument in emitc.call_opaque (#141451)Vimal1-8/+16
Integer attributes supplied to `emitc.call_opaque` as arguments were treated as index into the operands list. This should be the case only for the normal arguments but not for the template arguments which can't refer to SSA values. This commit updates the handling of template arguments in mlir-to-cpp by removing special handling of integer attributes.
2025-04-22[mlir][emitc] mark `emitc.load` with `CExpression` (#130802)Kirill Chibisov1-0/+1
Follow the `call` and `call_opaque` operations, as well as `apply`, which already are marked as `CExpression` even though they have side effects. Even though `emitc.load` can be included inside the `emitc.expression`, the inlining and `--form-expression` pass won't actually inline them inside other expression due to it having a side effect, thus unless the user manually writes the `emitc.load` inside the `emitc.expression` it won't appear there. -- It was brought https://github.com/llvm/llvm-project/pull/91475#issuecomment-2302529428 and while there was some opposition due to `load` having a side effect, `emitc` already allows all the rest operations that have it, so for consistency reasons, enabling it doesn't really hurt from my point of view. Especially given that `--form-expression` doesn't allow it to really inline inside other expressions, which makes sense, since if the users want such behavior, they should explicitly opt-in.
2025-03-19[mlir][emitc][NFC] Eliminate the extra newline printout during emitc.switch ↵Andrey Timonin1-1/+1
emission (#129257) Before the output was: ```c++ void emitc_switch_ptrdiff_t() { ptrdiff_t v1 = 1; switch (v1) { ... } return; } ``` After: ```c++ void emitc_switch_ptrdiff_t() { ptrdiff_t v1 = 1; switch (v1) { ... } return; } ```
2025-02-28[MLIR][EmitC][cf] Bugfix: correctly inline emitc.expression op in the ↵gdehame1-2/+4
emitted if condition of a cf.cond_br (#128958) emitc.expression ops are expected to be inlined in the if condition in the lowering of cf.cond_br if this is their only use but they weren't inlined. Instead, a use of the variable corresponding to the expression result was generated but with no declaration/definition.
2025-02-18[MLIR] emitc: Add fmtArgs to verbatim (#123294)Matthias Gehre1-1/+15
Allows to print code snippets that refer to arguments or local variables. E.g. `emitc.verbatim "#pragma abc var={}" args %arg0 : !emitc.ptr<i32>` is printed as `#pragma abc var=v1` when the translator had decided to print `%arg0` as `v1`. As a follow-up PR, we will use the same infra to extend opaque type, which provides a way to generate template types depending on the spelling of other types.
2025-02-18[MLIR] emitc: Add emitc.file op (#123298)Matthias Gehre1-13/+40
A `emitc.file` represents a file that can be emitted into a single C++ file. This allows to manage multiple source files within the same MLIR module, but emit them into separate files. This feature is opt-in. By default, `mlir-translate` emits all ops outside of `emitc.file` and ignores all `emitc.file` ops and their bodies. When specifying the `-file-id=id` flag, `mlir-translate` emits all ops outside of `emitc.file` and the ops within the `emitc.file` with matching `id`. Example: ```mlir emitc.file "main" { func @func_one() { return } } emitc.file "test" { func @func_two() { return } } ``` `mlir-translate -file-id=main` will emit `func_one` and `mlir-translate -file-id=test` will emit `func_two`.
2025-02-05[emitc] Fix precedence when emit emit.expression (#124087)Jianjian Guan1-4/+2
Fixes https://github.com/llvm/llvm-project/issues/124086.
2025-01-23[emitc] Fix the translation switchop with argument of expressionop (#123701)Jianjian Guan1-1/+4
Now a `emitc.switch` with argument of `emitc.expression` wouldn't emit its argument to cpp. This patch fix it.
2025-01-14[mlir][emitc] Don't emit extra semicolon after bracket (#122464)Kirill Chibisov1-10/+11
Extra semicolons were emitted for operations that should never have them, since not every place was checking whether semicolon would be actually needed. Thus change the emitOperation to ignore trailingSemicolon field for such operations.
2024-10-10[mlir][emitc] Fix the error with closing bracket in CppEmitter in switchOp ↵Andrey Timonin1-1/+1
(#110269) While working with `emitc::SwitchOp`, it was identified that `mlir-translate` emits **invalid C code** for switch. This commit fixes the issue with the closing bracket in `CppEmitter` within `printOperation` for `emitc::SwitchOp`.
2024-08-27[mlir] Support emit fp16 and bf16 type to cpp (#105803)Jianjian Guan1-6/+22
2024-08-20[mlir][EmitC] Model lvalues as a type in EmitC (#91475)Simon Camphausen1-3/+17
This adds an `emitc.lvalue` type which models assignable lvlaues in the type system. Operations modifying memory are restricted to this type accordingly. See also the discussion on [discourse](https://discourse.llvm.org/t/rfc-separate-variables-from-ssa-values-in-emitc/75224/9). The most notable changes are as follows. - `emitc.variable` and `emitc.global` ops are restricted to return `emitc.array` or `emitc.lvalue` types - Taking the address of a value is restricted to operands with lvalue type - Conversion from lvalues into SSA values is done with the new `emitc.load` op - The var operand of the `emitc.assign` op is restricted to lvalue type - The result of the `emitc.subscript` and `emitc.get_global` ops is a lvalue type - The operands and results of the `emitc.member` and `emitc.member_of_ptr` ops are restricted to lvalue types --------- Co-authored-by: Matthias Gehre <matthias.gehre@amd.com>
2024-08-16[mlir][emitc] Add 'emitc.switch' op to the dialect (#102331)Andrey Timonin1-3/+40
This PR is continuation of the [previous one](https://github.com/llvm/llvm-project/pull/101478). As a result, the `emitc::SwitchOp` op was developed inspired by `scf::IndexSwitchOp`. Main points of PR: - Added the `emitc::SwitchOp` op to the EmitC dialect + CppEmitter - Corresponding tests were added - Conversion from the SCF dialect to the EmitC dialect for the op
2024-07-13[mlir][EmitC] Add member access ops (#98460)Marius Brehler1-2/+32
This adds an `emitc.member` and `emitc.member_of_ptr` operation for the corresponding member access operators. Furthermore, `emitc.assign` is adjusted to be used with the member access operators.
2024-07-10[mlir][EmitC] Unify handling of operations which are emitted in a deferred ↵Simon Camphausen1-46/+43
way (#97804) Several operations from the EmitC dialect don't produce output directly during emission, but rather when being used as an operand. These changes unify the handling of such operations and fix a bug in the emission of global ops. Co-authored-by: Marius Brehler <marius.brehler@iml.fraunhofer.de>
2024-06-17[mlir][emitc] Add EmitC index types (#93155)Corentin Ferry1-0/+6
This commit adds `emitc.size_t`, `emitc.ssize_t` and `emitc.ptrdiff_t` types to the EmitC dialect. These are used to map `index` types to C/C++ types with an explicit signedness, and are emitted in C/C++ as `size_t`, `ssize_t` and `ptrdiff_t`.
2024-06-04[mlir][EmitC] Do not inline expressions used by ops with the CExpression ↵Simon Camphausen1-3/+3
trait (#93691) Currently an expression is inlined without emitting enclosing parentheses regardless of the context of the user. This could led to wrong evaluation order depending on the precedence of both expressions. If the inlining is intended, the user operation should be merged into the expression op. Fixes #93470.
2024-05-29[mlir][EmitC] Fix evaluation order of expressions (#93549)Simon Camphausen1-1/+5
Expressions with the same precedence were not parenthesized and therefore were possibly evaluated in the wrong order depending on the shape of the expression tree. --------- Co-authored-by: Matthias Gehre <matthias.gehre@amd.com> Co-authored-by: Corentin Ferry <corentin.ferry@amd.com>
2024-05-06[MLIR][EmitC] Don't translate expressions inline if user is ↵Chris1-1/+8
`emitc.subscript` (#91087) This change updates the logic that determines whether an `emitc.expression` result is translated into a dedicated variable assignment. Due to how the translation of `emitc.subscript` currently works, a previously inline-able `emitc.expression` would produce incorrect C++ if its single user was a `emitc.subscript` operation.
2024-04-23EmitC: Add emitc.global and emitc.get_global (#145) (#88701)Matthias Gehre1-6/+49
This adds - `emitc.global` and `emitc.get_global` ops to model global variables similar to how `memref.global` and `memref.get_global` work. - translation of those ops to C++ - lowering of `memref.global` and `memref.get_global` into those ops --------- Co-authored-by: Simon Camphausen <simon.camphausen@iml.fraunhofer.de>
2024-04-03[mlir][EmitC] Add support for pointer and opaque types to subscript op (#86266)Simon Camphausen1-1/+1
For pointer types the indices are restricted to one integer-like operand. For opaque types no further restrictions are made.
2024-04-01[mlir][NFC] Simplify type checks with isa predicates (#87183)Jakub Kuderski1-3/+2
For more context on isa predicates, see: https://github.com/llvm/llvm-project/pull/83753.
2024-03-18TranslateToCpp: Emit floating point literals with suffix (#85392)Matthias Gehre1-4/+11
Emits `2.0e+00f` instead of `(float)2.0e+00`. This helps consumers of the emitted code, especially when there are large numbers of floating point literals, to have a simple AST.
2024-03-15[MLIR] EmitC: Add subscript operator (#84783)Matthias Gehre1-6/+33
Introduces a SubscriptOp that allows to write IR like ``` func.func @load_store(%arg0: !emitc.array<4x8xf32>, %arg1: !emitc.array<3x5xf32>, %arg2: index, %arg3: index) { %0 = emitc.subscript %arg0[%arg2, %arg3] : <4x8xf32>, index, index %1 = emitc.subscript %arg1[%arg2, %arg3] : <3x5xf32>, index, index emitc.assign %0 : f32 to %1 : f32 return } ``` which gets translated into the C++ code ``` v1[v2][v3] = v0[v1][v2]; ``` To make this happen, this - adds the SubscriptOp - allows the subscript op as rhs of emitc.assign - updates the emitter to print SubscriptOps The emitter prints emitc.subscript in a delayed fashing to allow it being used as lvalue. I.e. while processing ``` %0 = emitc.subscript %arg0[%arg2, %arg3] : <4x8xf32>, index, index ``` it will not emit any text, but record in the `valueMapper` that the name for `%0` is `v0[v1][v2]`, see `CppEmitter::getSubscriptName`. Only when that result is then used (here in `emitc.assign`), that name is inserted into the text.
2024-03-14[mlir][Target][Cpp] Cleanup includes (#85105)Marius Brehler1-0/+2
2024-03-12[mlir][EmitC] Add an `emitc.conditional` operator (#84883)Marius Brehler1-6/+31
This adds an `emitc.conditional` operation for the ternary conditional operator. Furthermore, this adds a converion from `arith.select` to the new op.
2024-03-11[mlir][emitc] Add ArrayType (#83386)Matthias Gehre1-6/+48
This models a one or multi-dimensional C/C++ array. The type implements the `ShapedTypeInterface` and prints similar to memref/tensor: ``` %arg0: !emitc.array<1xf32>, %arg1: !emitc.array<10x20x30xi32>, %arg2: !emitc.array<30x!emitc.ptr<i32>>, %arg3: !emitc.array<30x!emitc.opaque<"int">> ``` It can be translated to a C array type when used as function parameter or as `emitc.variable` type.
2024-03-08[mlir][emitc] Arith to EmitC conversion: constants (#83798)Tina Jung1-12/+0
* Add a conversion from `arith.constant` to `emitc.constant`. * Drop the translation for `arith.constant`s.
2024-03-08[mlir][EmitC] Add `unary_{minus,plus}` operators (#84329)Marius Brehler1-1/+16
This adds operations for the unary minus and the unary plus operator.
2024-03-07[mlir][EmitC] Allow further ops within expressions (#84284)Marius Brehler1-8/+18
This adds the `CExpression` trait to additional ops to allow to use these ops within the expression operation. Furthermore, the operator precedence is defined for those ops.
2024-03-01[mlir][EmitC] Add bitwise operators (#83387)Marius Brehler1-12/+60
This adds operations for bitwise operators. Furthermore, an UnaryOp class and a helper to print unary operations are introduced.
2024-02-28[mlir][EmitC] Add logical operators (#83123)Marius Brehler1-1/+29
This adds operations for the logical operators AND, NOT and OR.
2024-02-20[mlir][EmitC] Remove `func.constant` from emitter (#82342)Marius Brehler1-9/+1
As part of the renaming the Standard dialect to Func dialect, *support* for the `func.constant` operation was added to the emitter. However, the emitter cannot emit function types. Hence the emission for a snippet like ``` %0 = func.constant @myfn : (f32) -> f32 func.func private @myfn(%arg0: f32) -> f32 { return %arg0 : f32 } ``` failes with `func.mlir:1:6: error: cannot emit type '(f32) -> f32'`. This removes `func.constant` from the emitter.
2024-02-06[mlir][EmitC] Remove unreachable code and fix Windows build warning (#80677)Simon Camphausen1-9/+15
2024-02-06[mlir][emitc] Add a `declare_func` operation (#80297)Marius Brehler1-6/+39
This adds the `emitc.declare_func` operation that allows to emit the declaration of an `emitc.func` at a specific location.
2024-02-05[mlir][EmitC] Add support for external functions (#80547)Marius Brehler1-10/+25
This adds a conversion from an externaly defined `func.func`, a `func.func` without function body, to an `emitc.func` with an `extern` specifier.
2024-02-01 [mlir][EmitC] Add func, call and return operations and conversions (#79612)Marius Brehler1-31/+117
This adds a `func`, `call` and `return` operation to the EmitC dialect, closely related to the corresponding operations of the Func dialect. In contrast to the operations of the Func dialect, the EmitC operations do not support multiple results. The `emitc.func` op features a `specifiers` argument that for example allows, with corresponding support in the emitter, to emit `inline static` functions. Furthermore, this adds patterns and a pass to convert the Func dialect to EmitC. A `func.func` op that is `private` is converted to `emitc.func` with a `"static"` specifier.
2024-01-31[mlir][EmitC] Add `verbatim` op (#79584)Simon Camphausen1-6/+15
The `verbatim` operation produces no results and the value is emitted as is followed by a line break ('\n' character) during translation. Note: Use with caution. This operation can have arbitrary effects on the semantics of the emitted code. Use semantically more meaningful operations whenever possible. Additionally this op is *NOT* intended to be used to inject large snippets of code. This operation can be used in situations where a more suitable operation is not yet implemented in the dialect or where preprocessor directives interfere with the structure of the code. Co-authored-by: Marius Brehler <marius.brehler@iml.fraunhofer.de>
2023-12-20[mlir][emitc] Add op modelling C expressions (#71631)Gil Rapaport1-25/+221
Add an emitc.expression operation that models C expressions, and provide transforms to form and fold expressions. The translator emits the body of emitc.expression ops as a single C expression. This expression is emitted by default as the RHS of an EmitC SSA value, but if possible, expressions with a single use that is not another expression are instead inlined. Specific expression's inlining can be fine tuned by lowering passes and transforms.
2023-11-17[mlir][emitc] Rename `call` op to `call_opaque` (#72494)Marius Brehler1-12/+14
This renames the `emitc.call` op to `emitc.call_opaque` as the existing call op does not refer to the callee by symbol. The rename allows to introduce a new call op alongside with a future `emitc.func` op to model and facilitate functions and function calls.
2023-11-06[mlir][emitc] Fix corner case in translation of literal ops (#71375)Simon Camphausen1-4/+5
Fix a corner case missed in #71296 when operands generated by literals are mixed with the args attribute of a call op. Additionally remove a range check that is already handled by the CallOp verifier.
2023-11-05[mlir][emitc] Fix literal translation (#71296)Gil Rapaport1-1/+4
- Do not emit variables-at-top for literals - Do not emit an error for a missing name for literals used as call operands.