MLIR  20.0.0git
Macros | Functions
TranslateToCpp.cpp File Reference
#include "mlir/Dialect/ControlFlow/IR/ControlFlowOps.h"
#include "mlir/Dialect/EmitC/IR/EmitC.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/BuiltinTypes.h"
#include "mlir/IR/Dialect.h"
#include "mlir/IR/Operation.h"
#include "mlir/IR/SymbolTable.h"
#include "mlir/Support/IndentedOstream.h"
#include "mlir/Support/LLVM.h"
#include "mlir/Target/Cpp/CppEmitter.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/ScopedHashTable.h"
#include "llvm/ADT/StringExtras.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/Support/Debug.h"
#include "llvm/Support/FormatVariadic.h"
#include <stack>
#include <utility>

Go to the source code of this file.

Macros

#define DEBUG_TYPE   "translate-to-cpp"
 

Functions

template<typename ForwardIterator , typename UnaryFunctor , typename NullaryFunctor >
LogicalResult interleaveWithError (ForwardIterator begin, ForwardIterator end, UnaryFunctor eachFn, NullaryFunctor betweenFn)
 Convenience functions to produce interleaved output with functions returning a LogicalResult. More...
 
template<typename Container , typename UnaryFunctor , typename NullaryFunctor >
LogicalResult interleaveWithError (const Container &c, UnaryFunctor eachFn, NullaryFunctor betweenFn)
 
template<typename Container , typename UnaryFunctor >
LogicalResult interleaveCommaWithError (const Container &c, raw_ostream &os, UnaryFunctor eachFn)
 
static FailureOr< int > getOperatorPrecedence (Operation *operation)
 Return the precedence of a operator as an integer, higher values imply higher precedence. More...
 
static bool hasDeferredEmission (Operation *op)
 Determine whether expression op should be emitted in a deferred way. More...
 
static bool shouldBeInlined (ExpressionOp expressionOp)
 Determine whether expression expressionOp should be emitted inline, i.e. More...
 
static LogicalResult printConstantOp (CppEmitter &emitter, Operation *operation, Attribute value)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::ConstantOp constantOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::VariableOp variableOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::GlobalOp globalOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::AssignOp assignOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::LoadOp loadOp)
 
static LogicalResult printBinaryOperation (CppEmitter &emitter, Operation *operation, StringRef binaryOperator)
 
static LogicalResult printUnaryOperation (CppEmitter &emitter, Operation *operation, StringRef unaryOperator)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::AddOp addOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::DivOp divOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::MulOp mulOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::RemOp remOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::SubOp subOp)
 
static LogicalResult emitSwitchCase (CppEmitter &emitter, raw_indented_ostream &os, Region &region)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::SwitchOp switchOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::CmpOp cmpOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::ConditionalOp conditionalOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::VerbatimOp verbatimOp)
 
static LogicalResult printOperation (CppEmitter &emitter, cf::BranchOp branchOp)
 
static LogicalResult printOperation (CppEmitter &emitter, cf::CondBranchOp condBranchOp)
 
static LogicalResult printCallOperation (CppEmitter &emitter, Operation *callOp, StringRef callee)
 
static LogicalResult printOperation (CppEmitter &emitter, func::CallOp callOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::CallOp callOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::CallOpaqueOp callOpaqueOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::ApplyOp applyOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::BitwiseAndOp bitwiseAndOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::BitwiseLeftShiftOp bitwiseLeftShiftOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::BitwiseNotOp bitwiseNotOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::BitwiseOrOp bitwiseOrOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::BitwiseRightShiftOp bitwiseRightShiftOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::BitwiseXorOp bitwiseXorOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::UnaryPlusOp unaryPlusOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::UnaryMinusOp unaryMinusOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::CastOp castOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::ExpressionOp expressionOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::IncludeOp includeOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::LogicalAndOp logicalAndOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::LogicalNotOp logicalNotOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::LogicalOrOp logicalOrOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::ForOp forOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::IfOp ifOp)
 
static LogicalResult printOperation (CppEmitter &emitter, func::ReturnOp returnOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::ReturnOp returnOp)
 
static LogicalResult printOperation (CppEmitter &emitter, ModuleOp moduleOp)
 
static LogicalResult printFunctionArgs (CppEmitter &emitter, Operation *functionOp, ArrayRef< Type > arguments)
 
static LogicalResult printFunctionArgs (CppEmitter &emitter, Operation *functionOp, Region::BlockArgListType arguments)
 
static LogicalResult printFunctionBody (CppEmitter &emitter, Operation *functionOp, Region::BlockListType &blocks)
 
static LogicalResult printOperation (CppEmitter &emitter, func::FuncOp functionOp)
 
static LogicalResult printOperation (CppEmitter &emitter, emitc::FuncOp functionOp)
 
static LogicalResult printOperation (CppEmitter &emitter, DeclareFuncOp declareFuncOp)
 

Macro Definition Documentation

◆ DEBUG_TYPE

#define DEBUG_TYPE   "translate-to-cpp"

Definition at line 30 of file TranslateToCpp.cpp.

Function Documentation

◆ emitSwitchCase()

static LogicalResult emitSwitchCase ( CppEmitter &  emitter,
raw_indented_ostream os,
Region region 
)
static

Definition at line 459 of file TranslateToCpp.cpp.

References mlir::Region::op_begin(), and mlir::Region::op_end().

Referenced by printOperation().

◆ getOperatorPrecedence()

static FailureOr<int> getOperatorPrecedence ( Operation operation)
static

Return the precedence of a operator as an integer, higher values imply higher precedence.

Definition at line 73 of file TranslateToCpp.cpp.

◆ hasDeferredEmission()

static bool hasDeferredEmission ( Operation op)
static

Determine whether expression op should be emitted in a deferred way.

Definition at line 286 of file TranslateToCpp.cpp.

Referenced by shouldBeInlined().

◆ interleaveCommaWithError()

template<typename Container , typename UnaryFunctor >
LogicalResult interleaveCommaWithError ( const Container &  c,
raw_ostream &  os,
UnaryFunctor  eachFn 
)
inline

Definition at line 65 of file TranslateToCpp.cpp.

References interleaveWithError().

Referenced by printFunctionArgs(), and printOperation().

◆ interleaveWithError() [1/2]

template<typename Container , typename UnaryFunctor , typename NullaryFunctor >
LogicalResult interleaveWithError ( const Container &  c,
UnaryFunctor  eachFn,
NullaryFunctor  betweenFn 
)
inline

Definition at line 58 of file TranslateToCpp.cpp.

References interleaveWithError().

◆ interleaveWithError() [2/2]

template<typename ForwardIterator , typename UnaryFunctor , typename NullaryFunctor >
LogicalResult interleaveWithError ( ForwardIterator  begin,
ForwardIterator  end,
UnaryFunctor  eachFn,
NullaryFunctor  betweenFn 
)
inline

Convenience functions to produce interleaved output with functions returning a LogicalResult.

This is different than those in STLExtras as functions used on each element doesn't return a string.

Definition at line 42 of file TranslateToCpp.cpp.

Referenced by interleaveCommaWithError(), and interleaveWithError().

◆ printBinaryOperation()

static LogicalResult printBinaryOperation ( CppEmitter &  emitter,
Operation operation,
StringRef  binaryOperator 
)
static

Definition at line 394 of file TranslateToCpp.cpp.

References mlir::Operation::getOperand().

Referenced by printOperation().

◆ printCallOperation()

static LogicalResult printCallOperation ( CppEmitter &  emitter,
Operation callOp,
StringRef  callee 
)
static

Definition at line 626 of file TranslateToCpp.cpp.

Referenced by printOperation().

◆ printConstantOp()

static LogicalResult printConstantOp ( CppEmitter &  emitter,
Operation operation,
Attribute  value 
)
static

Definition at line 323 of file TranslateToCpp.cpp.

References mlir::Operation::getLoc(), and mlir::Operation::getResult().

Referenced by printOperation().

◆ printFunctionArgs() [1/2]

static LogicalResult printFunctionArgs ( CppEmitter &  emitter,
Operation functionOp,
ArrayRef< Type arguments 
)
static

Definition at line 959 of file TranslateToCpp.cpp.

References mlir::Operation::getLoc(), and interleaveCommaWithError().

Referenced by printOperation().

◆ printFunctionArgs() [2/2]

static LogicalResult printFunctionArgs ( CppEmitter &  emitter,
Operation functionOp,
Region::BlockArgListType  arguments 
)
static

◆ printFunctionBody()

static LogicalResult printFunctionBody ( CppEmitter &  emitter,
Operation functionOp,
Region::BlockListType blocks 
)
static

◆ printOperation() [1/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
cf::BranchOp  branchOp 
)
static

Definition at line 560 of file TranslateToCpp.cpp.

References mlir::Block::getArguments(), and mlir::Block::getSuccessor().

◆ printOperation() [2/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
cf::CondBranchOp  condBranchOp 
)
static

◆ printOperation() [3/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
DeclareFuncOp  declareFuncOp 
)
static

Definition at line 1136 of file TranslateToCpp.cpp.

References printFunctionArgs().

◆ printOperation() [4/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::AddOp  addOp 
)
static

Definition at line 429 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [5/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::ApplyOp  applyOp 
)
static

Definition at line 701 of file TranslateToCpp.cpp.

◆ printOperation() [6/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::AssignOp  assignOp 
)
static

◆ printOperation() [7/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::BitwiseAndOp  bitwiseAndOp 
)
static

Definition at line 714 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [8/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::BitwiseLeftShiftOp  bitwiseLeftShiftOp 
)
static

Definition at line 721 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [9/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::BitwiseNotOp  bitwiseNotOp 
)
static

Definition at line 727 of file TranslateToCpp.cpp.

References printUnaryOperation().

◆ printOperation() [10/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::BitwiseOrOp  bitwiseOrOp 
)
static

Definition at line 733 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [11/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::BitwiseRightShiftOp  bitwiseRightShiftOp 
)
static

Definition at line 740 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [12/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::BitwiseXorOp  bitwiseXorOp 
)
static

Definition at line 746 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [13/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::CallOp  callOp 
)
static

Definition at line 646 of file TranslateToCpp.cpp.

References printCallOperation().

◆ printOperation() [14/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::CallOpaqueOp  callOpaqueOp 
)
static

◆ printOperation() [15/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::CastOp  castOp 
)
static

◆ printOperation() [16/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::CmpOp  cmpOp 
)
static

Definition at line 496 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [17/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::ConditionalOp  conditionalOp 
)
static

Definition at line 528 of file TranslateToCpp.cpp.

◆ printOperation() [18/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::ConstantOp  constantOp 
)
static

Definition at line 355 of file TranslateToCpp.cpp.

References printConstantOp().

◆ printOperation() [19/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::DivOp  divOp 
)
static

Definition at line 435 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [20/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::ExpressionOp  expressionOp 
)
static

Definition at line 777 of file TranslateToCpp.cpp.

References shouldBeInlined().

◆ printOperation() [21/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::ForOp  forOp 
)
static

◆ printOperation() [22/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::FuncOp  functionOp 
)
static

Definition at line 1095 of file TranslateToCpp.cpp.

References printFunctionArgs(), and printFunctionBody().

◆ printOperation() [23/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::GlobalOp  globalOp 
)
static

Definition at line 371 of file TranslateToCpp.cpp.

◆ printOperation() [24/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::IfOp  ifOp 
)
static

◆ printOperation() [25/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::IncludeOp  includeOp 
)
static

Definition at line 790 of file TranslateToCpp.cpp.

◆ printOperation() [26/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::LoadOp  loadOp 
)
static

Definition at line 387 of file TranslateToCpp.cpp.

◆ printOperation() [27/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::LogicalAndOp  logicalAndOp 
)
static

Definition at line 803 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [28/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::LogicalNotOp  logicalNotOp 
)
static

Definition at line 809 of file TranslateToCpp.cpp.

References printUnaryOperation().

◆ printOperation() [29/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::LogicalOrOp  logicalOrOp 
)
static

Definition at line 815 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [30/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::MulOp  mulOp 
)
static

Definition at line 441 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [31/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::RemOp  remOp 
)
static

Definition at line 447 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [32/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::ReturnOp  returnOp 
)
static

Definition at line 936 of file TranslateToCpp.cpp.

◆ printOperation() [33/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::SubOp  subOp 
)
static

Definition at line 453 of file TranslateToCpp.cpp.

References printBinaryOperation().

◆ printOperation() [34/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::SwitchOp  switchOp 
)
static

◆ printOperation() [35/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::UnaryMinusOp  unaryMinusOp 
)
static

Definition at line 758 of file TranslateToCpp.cpp.

References printUnaryOperation().

◆ printOperation() [36/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::UnaryPlusOp  unaryPlusOp 
)
static

Definition at line 752 of file TranslateToCpp.cpp.

References printUnaryOperation().

◆ printOperation() [37/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::VariableOp  variableOp 
)
static

Definition at line 363 of file TranslateToCpp.cpp.

References printConstantOp().

◆ printOperation() [38/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
emitc::VerbatimOp  verbatimOp 
)
static

Definition at line 551 of file TranslateToCpp.cpp.

◆ printOperation() [39/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
func::CallOp  callOp 
)
static

Definition at line 639 of file TranslateToCpp.cpp.

References printCallOperation().

◆ printOperation() [40/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
func::FuncOp  functionOp 
)
static

Definition at line 1058 of file TranslateToCpp.cpp.

References printFunctionArgs(), and printFunctionBody().

◆ printOperation() [41/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
func::ReturnOp  returnOp 
)
static

Definition at line 915 of file TranslateToCpp.cpp.

◆ printOperation() [42/42]

static LogicalResult printOperation ( CppEmitter &  emitter,
ModuleOp  moduleOp 
)
static

Definition at line 949 of file TranslateToCpp.cpp.

◆ printUnaryOperation()

static LogicalResult printUnaryOperation ( CppEmitter &  emitter,
Operation operation,
StringRef  unaryOperator 
)
static

Definition at line 413 of file TranslateToCpp.cpp.

References mlir::Operation::getOperand().

Referenced by printOperation().

◆ shouldBeInlined()

static bool shouldBeInlined ( ExpressionOp  expressionOp)
static

Determine whether expression expressionOp should be emitted inline, i.e.

as part of its user. This function recommends inlining of any expressions that can be inlined unless it is used by another expression, under the assumption that any expression fusion/re-materialization was taken care of by transformations run by the backend.

Definition at line 296 of file TranslateToCpp.cpp.

References mlir::Value::getUsers(), hasDeferredEmission(), mlir::Value::hasOneUse(), and mlir::Operation::hasTrait().

Referenced by printFunctionBody(), and printOperation().