Skip to content

Commit e624648

Browse files
Simon Camphausenmarbre
Simon Camphausen
andauthored
[mlir][EmitC] Add verbatim op (#79584)
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 <[email protected]>
1 parent 78e0cca commit e624648

File tree

4 files changed

+84
-6
lines changed

4 files changed

+84
-6
lines changed

mlir/include/mlir/Dialect/EmitC/IR/EmitC.td

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -544,6 +544,43 @@ def EmitC_VariableOp : EmitC_Op<"variable", []> {
544544
let hasVerifier = 1;
545545
}
546546

547+
def EmitC_VerbatimOp : EmitC_Op<"verbatim"> {
548+
let summary = "Verbatim operation";
549+
let description = [{
550+
The `verbatim` operation produces no results and the value is emitted as is
551+
followed by a line break ('\n' character) during translation.
552+
553+
Note: Use with caution. This operation can have arbitrary effects on the
554+
semantics of the emitted code. Use semantically more meaningful operations
555+
whenever possible. Additionally this op is *NOT* intended to be used to
556+
inject large snippets of code.
557+
558+
This operation can be used in situations where a more suitable operation is
559+
not yet implemented in the dialect or where preprocessor directives
560+
interfere with the structure of the code. One example of this is to declare
561+
the linkage of external symbols to make the generated code usable in both C
562+
and C++ contexts:
563+
564+
```c++
565+
#ifdef __cplusplus
566+
extern "C" {
567+
#endif
568+
569+
...
570+
571+
#ifdef __cplusplus
572+
}
573+
#endif
574+
```
575+
}];
576+
577+
let arguments = (ins
578+
StrAttr:$value,
579+
UnitAttr:$trailing_semicolon
580+
);
581+
let assemblyFormat = "$value attr-dict";
582+
}
583+
547584
def EmitC_AssignOp : EmitC_Op<"assign", []> {
548585
let summary = "Assign operation";
549586
let description = [{

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -429,6 +429,15 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::CmpOp cmpOp) {
429429
return printBinaryOperation(emitter, operation, binaryOperator);
430430
}
431431

432+
static LogicalResult printOperation(CppEmitter &emitter,
433+
emitc::VerbatimOp verbatimOp) {
434+
raw_ostream &os = emitter.ostream();
435+
436+
os << verbatimOp.getValue();
437+
438+
return success();
439+
}
440+
432441
static LogicalResult printOperation(CppEmitter &emitter,
433442
cf::BranchOp branchOp) {
434443
raw_ostream &os = emitter.ostream();
@@ -814,11 +823,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
814823
for (Operation &op : block.getOperations()) {
815824
// When generating code for an emitc.if or cf.cond_br op no semicolon
816825
// needs to be printed after the closing brace.
817-
// When generating code for an emitc.for op, printing a trailing semicolon
818-
// is handled within the printOperation function.
819-
bool trailingSemicolon =
820-
!isa<cf::CondBranchOp, emitc::ForOp, emitc::IfOp, emitc::LiteralOp>(
821-
op);
826+
// When generating code for an emitc.for and emitc.verbatim op, printing a
827+
// trailing semicolon is handled within the printOperation function.
828+
bool trailingSemicolon = !isa<cf::CondBranchOp, emitc::ForOp, emitc::IfOp,
829+
emitc::LiteralOp, emitc::VerbatimOp>(op);
822830

823831
if (failed(emitter.emitOperation(
824832
op, /*trailingSemicolon=*/trailingSemicolon)))
@@ -1144,7 +1152,8 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
11441152
emitc::CallOpaqueOp, emitc::CastOp, emitc::CmpOp,
11451153
emitc::ConstantOp, emitc::DivOp, emitc::ExpressionOp,
11461154
emitc::ForOp, emitc::IfOp, emitc::IncludeOp, emitc::MulOp,
1147-
emitc::RemOp, emitc::SubOp, emitc::VariableOp>(
1155+
emitc::RemOp, emitc::SubOp, emitc::VariableOp,
1156+
emitc::VerbatimOp>(
11481157
[&](auto op) { return printOperation(*this, op); })
11491158
// Func ops.
11501159
.Case<func::CallOp, func::ConstantOp, func::FuncOp, func::ReturnOp>(

mlir/test/Dialect/EmitC/ops.mlir

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,3 +166,14 @@ func.func @test_for_not_index_induction(%arg0 : i16, %arg1 : i16, %arg2 : i16) {
166166
}
167167
return
168168
}
169+
170+
emitc.verbatim "#ifdef __cplusplus"
171+
emitc.verbatim "extern \"C\" {"
172+
emitc.verbatim "#endif // __cplusplus"
173+
174+
emitc.verbatim "#ifdef __cplusplus"
175+
emitc.verbatim "} // extern \"C\""
176+
emitc.verbatim "#endif // __cplusplus"
177+
178+
emitc.verbatim "typedef int32_t i32;"
179+
emitc.verbatim "typedef float f32;"

mlir/test/Target/Cpp/verbatim.mlir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s
2+
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s
3+
4+
5+
emitc.verbatim "#ifdef __cplusplus"
6+
// CHECK: #ifdef __cplusplus
7+
emitc.verbatim "extern \"C\" {"
8+
// CHECK-NEXT: extern "C" {
9+
emitc.verbatim "#endif // __cplusplus"
10+
// CHECK-NEXT: #endif // __cplusplus
11+
emitc.verbatim "#ifdef __cplusplus"
12+
// CHECK-NEXT: #ifdef __cplusplus
13+
emitc.verbatim "} // extern \"C\""
14+
// CHECK-NEXT: } // extern "C"
15+
emitc.verbatim "#endif // __cplusplus"
16+
// CHECK-NEXT: #endif // __cplusplus
17+
18+
emitc.verbatim "typedef int32_t i32;"
19+
// CHECK-NEXT: typedef int32_t i32;
20+
emitc.verbatim "typedef float f32;"
21+
// CHECK-NEXT: typedef float f32;

0 commit comments

Comments
 (0)