[Mlir-commits] [mlir] e624648 - [mlir][EmitC] Add `verbatim` op (#79584)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Jan 31 02:56:20 PST 2024
Author: Simon Camphausen
Date: 2024-01-31T11:56:16+01:00
New Revision: e624648bd903208a92d8edbecbfa92085d1c34dc
URL: https://github.com/llvm/llvm-project/commit/e624648bd903208a92d8edbecbfa92085d1c34dc
DIFF: https://github.com/llvm/llvm-project/commit/e624648bd903208a92d8edbecbfa92085d1c34dc.diff
LOG: [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 <marius.brehler at iml.fraunhofer.de>
Added:
mlir/test/Target/Cpp/verbatim.mlir
Modified:
mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
mlir/lib/Target/Cpp/TranslateToCpp.cpp
mlir/test/Dialect/EmitC/ops.mlir
Removed:
################################################################################
diff --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index b8f8f1e2d818d..ca040f2555e13 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -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 = [{
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index c32cb03caf9db..72b382709925e 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -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();
@@ -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)))
@@ -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>(
diff --git a/mlir/test/Dialect/EmitC/ops.mlir b/mlir/test/Dialect/EmitC/ops.mlir
index 45ce2bcb99092..7ad3787558b7f 100644
--- a/mlir/test/Dialect/EmitC/ops.mlir
+++ b/mlir/test/Dialect/EmitC/ops.mlir
@@ -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;"
diff --git a/mlir/test/Target/Cpp/verbatim.mlir b/mlir/test/Target/Cpp/verbatim.mlir
new file mode 100644
index 0000000000000..10465dd781a81
--- /dev/null
+++ b/mlir/test/Target/Cpp/verbatim.mlir
@@ -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;
More information about the Mlir-commits
mailing list