Skip to content

Commit 192078c

Browse files
committed
[mlir][EmitC] Refactor MLIR Translate To Cpp
This refactors the MLIR TranslateToCpp part by introducing CppTranslationInterface that breaks down the overall translation into different dialects. This majorly helps the readability of the code and extensibility for future c++ translation addition.
1 parent 2a30684 commit 192078c

23 files changed

+1679
-1056
lines changed

mlir/include/mlir/Target/Cpp/CppEmitter.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,21 +14,15 @@
1414
#define MLIR_TARGET_CPP_CPPEMITTER_H
1515

1616
#include "mlir/IR/BuiltinTypes.h"
17-
#include "mlir/IR/Value.h"
18-
#include "llvm/ADT/ScopedHashTable.h"
19-
#include "llvm/Support/raw_ostream.h"
20-
#include <stack>
2117

2218
namespace mlir {
23-
namespace emitc {
2419

2520
/// Translates the given operation to C++ code. The operation or operations in
2621
/// the region of 'op' need almost all be in EmitC dialect. The parameter
2722
/// 'declareVariablesAtTop' enforces that all variables for op results and block
2823
/// arguments are declared at the beginning of the function.
2924
LogicalResult translateToCpp(Operation *op, raw_ostream &os,
3025
bool declareVariablesAtTop = false);
31-
} // namespace emitc
3226
} // namespace mlir
3327

3428
#endif // MLIR_TARGET_CPP_CPPEMITTER_H
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
//===--- CppTranslationInterface.h - Translation to Cpp iface ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This header file defines dialect interfaces for translation to Cpp.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_CPP_CPPTRANSLATIONINTERFACE_H
14+
#define MLIR_TARGET_CPP_CPPTRANSLATIONINTERFACE_H
15+
16+
#include "mlir/IR/DialectInterface.h"
17+
#include "mlir/Support/LogicalResult.h"
18+
19+
namespace mlir {
20+
struct CppEmitter;
21+
22+
/// Base class for dialect interfaces providing translation to Cpp. Dialects
23+
/// should implement this interface with supported operation translations to
24+
/// be registered and used with translate-to-cpp.
25+
class CppTranslationDialectInterface
26+
: public DialectInterface::Base<CppTranslationDialectInterface> {
27+
public:
28+
CppTranslationDialectInterface(Dialect *dialect) : Base(dialect) {}
29+
30+
/// Hook for derived dialect interface to provide op translation to Cpp.
31+
virtual LogicalResult emitOperation(Operation *op, CppEmitter &cppEmitter,
32+
bool trailingSemicolon) const {
33+
return failure();
34+
}
35+
};
36+
37+
/// Interface collection for translation to Cpp, dispatches to a concrete
38+
/// interface implementation based on the dialect to which the given op belongs.
39+
class CppTranslationInterface
40+
: public DialectInterfaceCollection<CppTranslationDialectInterface> {
41+
public:
42+
using Base::Base;
43+
44+
/// Translates the given operation to Cpp using the derived dialect interface.
45+
virtual LogicalResult emitOperation(Operation *op, CppEmitter &cppEmitter,
46+
bool trailingSemicolon) const {
47+
if (const CppTranslationDialectInterface *iface = getInterfaceFor(op))
48+
return iface->emitOperation(op, cppEmitter, trailingSemicolon);
49+
return failure();
50+
}
51+
};
52+
53+
} // namespace mlir
54+
55+
#endif // MLIR_TARGET_CPP_CPPTRANSLATIONINTERFACE_H
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
//===- CppTranslationUtils.h - Helpers to used in C++ emitter ---*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines common helper functions used across different dialects
10+
// during the Cpp translation.
11+
//
12+
//===----------------------------------------------------------------------===//
13+
14+
#ifndef MLIR_TARGET_CPP_CPPTRANSLATIONUTILS_H
15+
#define MLIR_TARGET_CPP_CPPTRANSLATIONUTILS_H
16+
17+
#include "mlir/Dialect/EmitC/IR/EmitC.h"
18+
#include "mlir/Target/Cpp/TranslateToCpp.h"
19+
20+
using namespace mlir;
21+
22+
/// Convenience functions to produce interleaved output with functions returning
23+
/// a LogicalResult. This is different than those in STLExtras as functions used
24+
/// on each element doesn't return a string.
25+
template <typename ForwardIterator, typename UnaryFunctor,
26+
typename NullaryFunctor>
27+
inline LogicalResult
28+
interleaveWithError(ForwardIterator begin, ForwardIterator end,
29+
UnaryFunctor eachFn, NullaryFunctor betweenFn) {
30+
if (begin == end)
31+
return success();
32+
if (failed(eachFn(*begin)))
33+
return failure();
34+
++begin;
35+
for (; begin != end; ++begin) {
36+
betweenFn();
37+
if (failed(eachFn(*begin)))
38+
return failure();
39+
}
40+
return success();
41+
}
42+
43+
template <typename Container, typename UnaryFunctor, typename NullaryFunctor>
44+
inline LogicalResult interleaveWithError(const Container &c,
45+
UnaryFunctor eachFn,
46+
NullaryFunctor betweenFn) {
47+
return interleaveWithError(c.begin(), c.end(), eachFn, betweenFn);
48+
}
49+
50+
template <typename Container, typename UnaryFunctor>
51+
inline LogicalResult interleaveCommaWithError(const Container &c,
52+
raw_ostream &os,
53+
UnaryFunctor eachFn) {
54+
return interleaveWithError(c.begin(), c.end(), eachFn, [&]() { os << ", "; });
55+
}
56+
57+
58+
/// Determine whether expression \p expressionOp should be emitted inline, i.e.
59+
/// as part of its user. This function recommends inlining of any expressions
60+
/// that can be inlined unless it is used by another expression, under the
61+
/// assumption that any expression fusion/re-materialization was taken care of
62+
/// by transformations run by the backend.
63+
bool shouldBeInlined(emitc::ExpressionOp expressionOp);
64+
65+
LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,
66+
Attribute value);
67+
68+
LogicalResult printBinaryOperation(CppEmitter &emitter, Operation *operation,
69+
StringRef binaryOperator);
70+
71+
LogicalResult printUnaryOperation(CppEmitter &emitter, Operation *operation,
72+
StringRef unaryOperator);
73+
74+
LogicalResult printCallOperation(CppEmitter &emitter, Operation *callOp,
75+
StringRef callee);
76+
77+
LogicalResult printFunctionArgs(CppEmitter &emitter, Operation *functionOp,
78+
ArrayRef<Type> arguments);
79+
80+
LogicalResult printFunctionArgs(CppEmitter &emitter, Operation *functionOp,
81+
Region::BlockArgListType arguments);
82+
83+
LogicalResult printFunctionBody(CppEmitter &emitter, Operation *functionOp,
84+
Region::BlockListType &blocks);
85+
86+
#endif // MLIR_TARGET_CPP_CPPTRANSLATIONUTILS_H
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
//===----- All.h - MLIR To Cpp Translation Registration ---------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This file defines a helper to register the all dialect translations to Cpp.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_CPP_DIALECT_ALL_H
14+
#define MLIR_TARGET_CPP_DIALECT_ALL_H
15+
16+
#include "mlir/Target/Cpp/Dialect/Builtin/BuiltinToCppTranslation.h"
17+
#include "mlir/Target/Cpp/Dialect/ControlFlow/ControlFlowToCppTranslation.h"
18+
#include "mlir/Target/Cpp/Dialect/EmitC/EmitCToCppTranslation.h"
19+
#include "mlir/Target/Cpp/Dialect/Func/FuncToCppTranslation.h"
20+
21+
namespace mlir {
22+
class DialectRegistry;
23+
24+
/// Registers all dialects that can be translated to Cpp and the
25+
/// corresponding translation interfaces.
26+
static inline void registerAllToCppTranslations(DialectRegistry &registry) {
27+
registerBuiltinDialectCppTranslation(registry);
28+
registerControlFlowDialectCppTranslation(registry);
29+
registerEmitCDialectCppTranslation(registry);
30+
registerFuncDialectCppTranslation(registry);
31+
}
32+
33+
} // namespace mlir
34+
35+
#endif // MLIR_TARGET_CPP_DIALECT_ALL_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===---- BuiltinToCppTranslation.h - Builtin Dialect to Cpp ----*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This provides registration calls for Builtin dialect to Cpp translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_CPP_DIALECT_BUILTIN_BUILTINTOCPPTRANSLATION_H
14+
#define MLIR_TARGET_CPP_DIALECT_BUILTIN_BUILTINTOCPPTRANSLATION_H
15+
16+
namespace mlir {
17+
18+
class DialectRegistry;
19+
class MLIRContext;
20+
21+
/// Register the Builtin dialect and the translation from it to the Cpp in the
22+
/// given registry;
23+
void registerBuiltinDialectCppTranslation(DialectRegistry &registry);
24+
25+
/// Register the Builtin dialect and the translation from it in the registry
26+
/// associated with the given context.
27+
void registerBuiltinDialectCppTranslation(MLIRContext &context);
28+
29+
} // namespace mlir
30+
31+
#endif // MLIR_TARGET_CPP_DIALECT_BUILTIN_BUILTINTOCPPTRANSLATION_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===- ControlFlowToCppTranslation.h - ControlFlow Dialect to Cpp -*-C++-*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This provides registration calls for ControlFlow dialect to Cpp translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_CPP_DIALECT_CONTROLFLOW_CONTROLFLOWTOCPPTRANSLATION_H
14+
#define MLIR_TARGET_CPP_DIALECT_CONTROLFLOW_CONTROLFLOWTOCPPTRANSLATION_H
15+
16+
namespace mlir {
17+
18+
class DialectRegistry;
19+
class MLIRContext;
20+
21+
/// Register the ControlFlow dialect and the translation from it to the Cpp in
22+
/// the given registry;
23+
void registerControlFlowDialectCppTranslation(DialectRegistry &registry);
24+
25+
/// Register the ControlFlow dialect and the translation from it in the registry
26+
/// associated with the given context.
27+
void registerControlFlowDialectCppTranslation(MLIRContext &context);
28+
29+
} // namespace mlir
30+
31+
#endif // MLIR_TARGET_CPP_DIALECT_CONTROLFLOW_CONTROLFLOWTOCPPTRANSLATION_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===------ EmitCToCppTranslation.h - EmitC Dialect to Cpp-------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This provides registration calls for EmitC dialect to Cpp translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_CPP_DIALECT_EMITC_EMITCTOCPPTRANSLATION_H
14+
#define MLIR_TARGET_CPP_DIALECT_EMITC_EMITCTOCPPTRANSLATION_H
15+
16+
namespace mlir {
17+
18+
class DialectRegistry;
19+
class MLIRContext;
20+
21+
/// Register the EmitC dialect and the translation from it to the Cpp in the
22+
/// given registry;
23+
void registerEmitCDialectCppTranslation(DialectRegistry &registry);
24+
25+
/// Register the EmitC dialect and the translation from it in the registry
26+
/// associated with the given context.
27+
void registerEmitCDialectCppTranslation(MLIRContext &context);
28+
29+
} // namespace mlir
30+
31+
#endif // MLIR_TARGET_CPP_DIALECT_EMITC_EMITCTOCPPTRANSLATION_H
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//===------- FuncToCppTranslation.h - Func Dialect to Cpp -------*- C++ -*-===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
//
9+
// This provides registration calls for Func dialect to Cpp translation.
10+
//
11+
//===----------------------------------------------------------------------===//
12+
13+
#ifndef MLIR_TARGET_CPP_DIALECT_FUNC_FUNCTOCPPTRANSLATION_H
14+
#define MLIR_TARGET_CPP_DIALECT_FUNC_FUNCTOCPPTRANSLATION_H
15+
16+
namespace mlir {
17+
18+
class DialectRegistry;
19+
class MLIRContext;
20+
21+
/// Register the Func dialect and the translation from it to the Cpp in the
22+
/// given registry;
23+
void registerFuncDialectCppTranslation(DialectRegistry &registry);
24+
25+
/// Register the Func dialect and the translation from it in the registry
26+
/// associated with the given context.
27+
void registerFuncDialectCppTranslation(MLIRContext &context);
28+
29+
} // namespace mlir
30+
31+
#endif // MLIR_TARGET_CPP_DIALECT_FUNC_FUNCTOCPPTRANSLATION_H

0 commit comments

Comments
 (0)