Skip to content

[mlir][EmitC] Add verbatim op #79584

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
Original file line number Diff line number Diff line change
Expand Up @@ -544,6 +544,43 @@ def EmitC_VariableOp : EmitC_Op<"variable", []> {
let hasVerifier = 1;
}

def EmitC_VerbatimOp : EmitC_Op<"verbatim"> {
let summary = "Verbatim operation";
let description = [{
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. One example of this is to declare
the linkage of external symbols to make the generated code usable in both C
and C++ contexts:

```c++
#ifdef __cplusplus
extern "C" {
#endif

...

#ifdef __cplusplus
}
#endif
```
}];

let arguments = (ins
StrAttr:$value,
UnitAttr:$trailing_semicolon
);
let assemblyFormat = "$value attr-dict";
}

def EmitC_AssignOp : EmitC_Op<"assign", []> {
let summary = "Assign operation";
let description = [{
Expand Down
21 changes: 15 additions & 6 deletions mlir/lib/Target/Cpp/TranslateToCpp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -429,6 +429,15 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::CmpOp cmpOp) {
return printBinaryOperation(emitter, operation, binaryOperator);
}

static LogicalResult printOperation(CppEmitter &emitter,
emitc::VerbatimOp verbatimOp) {
raw_ostream &os = emitter.ostream();

os << verbatimOp.getValue();

return success();
}

static LogicalResult printOperation(CppEmitter &emitter,
cf::BranchOp branchOp) {
raw_ostream &os = emitter.ostream();
Expand Down Expand Up @@ -814,11 +823,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
for (Operation &op : block.getOperations()) {
// When generating code for an emitc.if or cf.cond_br op no semicolon
// needs to be printed after the closing brace.
// When generating code for an emitc.for op, printing a trailing semicolon
// is handled within the printOperation function.
bool trailingSemicolon =
!isa<cf::CondBranchOp, emitc::ForOp, emitc::IfOp, emitc::LiteralOp>(
op);
// When generating code for an emitc.for and emitc.verbatim op, printing a
// trailing semicolon is handled within the printOperation function.
bool trailingSemicolon = !isa<cf::CondBranchOp, emitc::ForOp, emitc::IfOp,
emitc::LiteralOp, emitc::VerbatimOp>(op);

if (failed(emitter.emitOperation(
op, /*trailingSemicolon=*/trailingSemicolon)))
Expand Down Expand Up @@ -1144,7 +1152,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
emitc::ConstantOp, emitc::DivOp, emitc::ExpressionOp,
emitc::ForOp, emitc::IfOp, emitc::IncludeOp, emitc::MulOp,
emitc::RemOp, emitc::SubOp, emitc::VariableOp>(
emitc::RemOp, emitc::SubOp, emitc::VariableOp,
emitc::VerbatimOp>(
[&](auto op) { return printOperation(*this, op); })
// Func ops.
.Case<func::CallOp, func::ConstantOp, func::FuncOp, func::ReturnOp>(
Expand Down
11 changes: 11 additions & 0 deletions mlir/test/Dialect/EmitC/ops.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -166,3 +166,14 @@ func.func @test_for_not_index_induction(%arg0 : i16, %arg1 : i16, %arg2 : i16) {
}
return
}

emitc.verbatim "#ifdef __cplusplus"
emitc.verbatim "extern \"C\" {"
emitc.verbatim "#endif // __cplusplus"

emitc.verbatim "#ifdef __cplusplus"
emitc.verbatim "} // extern \"C\""
emitc.verbatim "#endif // __cplusplus"

emitc.verbatim "typedef int32_t i32;"
emitc.verbatim "typedef float f32;"
21 changes: 21 additions & 0 deletions mlir/test/Target/Cpp/verbatim.mlir
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s


emitc.verbatim "#ifdef __cplusplus"
// CHECK: #ifdef __cplusplus
emitc.verbatim "extern \"C\" {"
// CHECK-NEXT: extern "C" {
emitc.verbatim "#endif // __cplusplus"
// CHECK-NEXT: #endif // __cplusplus
emitc.verbatim "#ifdef __cplusplus"
// CHECK-NEXT: #ifdef __cplusplus
emitc.verbatim "} // extern \"C\""
// CHECK-NEXT: } // extern "C"
emitc.verbatim "#endif // __cplusplus"
// CHECK-NEXT: #endif // __cplusplus

emitc.verbatim "typedef int32_t i32;"
// CHECK-NEXT: typedef int32_t i32;
emitc.verbatim "typedef float f32;"
// CHECK-NEXT: typedef float f32;