tensorflow/compiler/mlir/quantization/tensorflow/utils/lift_as_function_call_utils.cc
// Creates a function to wrap the section between arguments and results.
Creates a function to wrap the section between arguments and results.
Creates a function to wrap the section between arguments and results .
llvm::SmallVector<Value, 4> LiftAsFunctionCall(
    OpBuilder builder, Location location, StringRef func_name,
    const llvm::SmallVector<Value> &arguments,
    const llvm::SmallVector<Value> &results,
    const llvm::SmallVector<Attribute> &attributes) {
  MLIRContext *context = builder.getContext();
  if (results.empty()) {
    mlir::emitError(UnknownLoc::get(context), "No result values specified");
    return {};
  }
  Operation *result_op = results[0].getDefiningOp();
  auto module = result_op->getParentOfType<ModuleOp>();
  auto current_func = result_op->getParentOfType<FuncOp>();
  auto guard = OpBuilder::InsertionGuard(builder);
  builder.setInsertionPointAfter(current_func);
  TypeRange arg_types(
      llvm::ArrayRef<Value>(arguments.begin(), arguments.end()));
  TypeRange result_types(llvm::ArrayRef<Value>(results.begin(), results.end()));
  auto func_type = FunctionType::get(context, arg_types, result_types);
  llvm::SmallVector<Location> arg_locs;
  for (const auto &arg : arguments) {
    arg_locs.push_back(arg.getLoc());
  }
  auto wrap_func = builder.create<FuncOp>(location, func_name, func_type);
  wrap_func.setVisibility(SymbolTable::Visibility::Private);
  wrap_func->setAttr(kFusedFunctionAttr, builder.getUnitAttr());
  builder.createBlock(&wrap_func.getBody(), wrap_func.begin(), arg_types,
                      arg_locs);
  BlockAndValueMapping mapping;
  for (int32_t i : llvm::seq<int32_t>(0, arguments.size())) {
    mapping.map(arguments[i], wrap_func.getArgument(i));
  }
  auto cloning_ops = FindOpsFromArgumentsToResults(arguments, results);
  if (failed(SetAttributeMap(context, attributes, cloning_ops))) {
    current_func.emitError() << "Some attributes couldn't be found.";
  }
  for (Operation *op : cloning_ops) {
    builder.clone(*op, mapping);
  }
  llvm::SmallVector<Value> return_values;
  for (Value result : results) {
    return_values.push_back(mapping.lookupOrNull(result));
  }
  builder.create<mlir::func::ReturnOp>(location, return_values);
  StringAttr new_func_name =
      InsertToSymbolTable(module, wrap_func, func_name.str());
  builder.setInsertionPointAfter(result_op);
  ValueRange new_results = createFusedFnCall(
      builder, location, new_func_name.getValue(), result_types, arguments);
  return llvm::SmallVector<Value, 4>(new_results.begin(), new_results.end());
}
llvm :: SmallVector < Value , 4 > LiftAsFunctionCall ( OpBuilder builder , Location location , StringRef func_name , const llvm :: SmallVector < Value > & arguments , const llvm :: SmallVector < Value > & results , const llvm :: SmallVector < Attribute > & attributes ) { MLIRContext * context = builder . getContext ( ) ; if ( results . empty ( ) ) { mlir :: emitError ( UnknownLoc :: get ( context ) , " " ) ; return { } ; } Operation * result_op = results [ 0 ] . getDefiningOp ( ) ; auto module = result_op -> getParentOfType < ModuleOp > ( ) ; auto current_func = result_op -> getParentOfType < FuncOp > ( ) ; auto guard = OpBuilder :: InsertionGuard ( builder ) ; builder . setInsertionPointAfter ( current_func ) ; TypeRange arg_types ( llvm :: ArrayRef < Value > ( arguments . begin ( ) , arguments . end ( ) ) ) ; TypeRange result_types ( llvm :: ArrayRef < Value > ( results . begin ( ) , results . end ( ) ) ) ; auto func_type = FunctionType :: get ( context , arg_types , result_types ) ; llvm :: SmallVector < Location > arg_locs ; for ( const auto & arg : arguments ) { arg_locs . push_back ( arg . getLoc ( ) ) ; } auto wrap_func = builder . create < FuncOp > ( location , func_name , func_type ) ; wrap_func . setVisibility ( SymbolTable :: Visibility :: Private ) ; wrap_func -> setAttr ( kFusedFunctionAttr , builder . getUnitAttr ( ) ) ; builder . createBlock ( & wrap_func . getBody ( ) , wrap_func . begin ( ) , arg_types , arg_locs ) ; BlockAndValueMapping mapping ; for ( int32_t i : llvm :: seq < int32_t > ( 0 , arguments . size ( ) ) ) { mapping . map ( arguments [ i ] , wrap_func . getArgument ( i ) ) ; } auto cloning_ops = FindOpsFromArgumentsToResults ( arguments , results ) ; if ( failed ( SetAttributeMap ( context , attributes , cloning_ops ) ) ) { current_func . emitError ( ) << " " ; } for ( Operation * op : cloning_ops ) { builder . clone ( * op , mapping ) ; } llvm :: SmallVector < Value > return_values ; for ( Value result : results ) { return_values . push_back ( mapping . lookupOrNull ( result ) ) ; } builder . create < mlir :: func :: ReturnOp > ( location , return_values ) ; StringAttr new_func_name = InsertToSymbolTable ( module , wrap_func , func_name . str ( ) ) ; builder . setInsertionPointAfter ( result_op ) ; ValueRange new_results = createFusedFnCall ( builder , location , new_func_name . getValue ( ) , result_types , arguments ) ; return llvm :: SmallVector < Value , 4 > ( new_results . begin ( ) , new_results . end ( ) ) ; }
Creates a function to wrap the section between arguments and results.
Creates a function to wrap the section between arguments and results .
// Create a private function and copy all ops between arguments and results. // Create a function call to the newly created function.