tensorflow/compiler/mlir/tensorflow/transforms/tpu_sharding_identification_pass.cc
// Returns XLA sharding from XlaSharding op connected to a result value.
// XlaSharding op may be directly connected to output but it may also be
// followed by Identity or simple arithmetic ops. In case where bfloat16 type is
// used, we might see a Cast op.
//
// TODO(hongjunchoi): Add logic to parse XlaSharding op inside control flow (If,
// Case, While) ops and Caller argument values.
// TODO(hongjunchoi): Consider explicitly checking op patterns to detect sharded
// inputs.
Returns XLA sharding from XlaSharding op connected to a result value.
XlaSharding op may be directly connected to output but it may also be
followed by Identity or simple arithmetic ops. In case where bfloat16 type is
used, we might see a Cast op.

Add logic to parse XlaSharding op inside control flow (If,
Case, While) ops and Caller argument values.
TODO(hongjunchoi): Consider explicitly checking op patterns to detect sharded
inputs.
Returns XLA sharding from XlaSharding op connected to a result value . XlaSharding op may be directly connected to output but it may also be followed by Identity or simple arithmetic ops . In case where bfloat16 type is used we might see a Cast op . Add logic to parse XlaSharding op inside control flow ( If Case While ) ops and Caller argument values . TODO ( hongjunchoi ) : Consider explicitly checking op patterns to detect sharded inputs .
llvm::Optional<StringRef> GetXlaShardingFromRetval(Value value) {
  llvm::SmallPtrSet<Value, 4> visited_values;
  llvm::SmallVector<Value, 4> values_to_visit;
  values_to_visit.push_back(value);
  while (!values_to_visit.empty()) {
    Value value_to_visit = values_to_visit.pop_back_val();
    if (!visited_values.insert(value_to_visit).second) {
      continue;
    }
    Operation* def = value_to_visit.getDefiningOp();
    if (!def) {
      continue;
    }
    if (auto sharding = llvm::dyn_cast_or_null<TF::XlaShardingOp>(def))
      return sharding._XlaSharding();
    if (  
        def->hasTrait<mlir::OpTrait::SameOperandsAndResultShape>() ||
        def->hasTrait<mlir::OpTrait::SameOperandsAndResultType>() ||
        def->hasTrait<mlir::OpTrait::TF::OperandsSameAsResultsTypeOrRef>() ||
        (def->hasTrait<
             mlir::OpTrait::TF::SameOperandsAndResultElementTypeResolveRef>() &&
         def->hasTrait<mlir::OpTrait::TF::CwiseBinary>())) {
      for (auto operand : def->getOperands()) {
        values_to_visit.push_back(operand);
      }
      continue;
    }
    if (auto call_op = llvm::dyn_cast_or_null<CallOpInterface>(def)) {
      FuncOp func = llvm::dyn_cast<FuncOp>(call_op.resolveCallable());
      if (!func) continue;
      value_to_visit = func.front().getTerminator()->getOperand(
          value_to_visit.cast<OpResult>().getResultNumber());
      values_to_visit.push_back(value_to_visit);
      continue;
    }
  }
  return llvm::None;
}
llvm :: Optional < StringRef > GetXlaShardingFromRetval ( Value value ) { llvm :: SmallPtrSet < Value , 4 > visited_values ; llvm :: SmallVector < Value , 4 > values_to_visit ; values_to_visit . push_back ( value ) ; while ( ! values_to_visit . empty ( ) ) { Value value_to_visit = values_to_visit . pop_back_val ( ) ; if ( ! visited_values . insert ( value_to_visit ) . second ) { continue ; } Operation * def = value_to_visit . getDefiningOp ( ) ; if ( ! def ) { continue ; } if ( auto sharding = llvm :: dyn_cast_or_null < TF :: XlaShardingOp > ( def ) ) return sharding . _XlaSharding ( ) ; if ( def -> hasTrait < mlir :: OpTrait :: SameOperandsAndResultShape > ( ) || def -> hasTrait < mlir :: OpTrait :: SameOperandsAndResultType > ( ) || def -> hasTrait < mlir :: OpTrait :: TF :: OperandsSameAsResultsTypeOrRef > ( ) || ( def -> hasTrait < mlir :: OpTrait :: TF :: SameOperandsAndResultElementTypeResolveRef > ( ) && def -> hasTrait < mlir :: OpTrait :: TF :: CwiseBinary > ( ) ) ) { for ( auto operand : def -> getOperands ( ) ) { values_to_visit . push_back ( operand ) ; } continue ; } if ( auto call_op = llvm :: dyn_cast_or_null < CallOpInterface > ( def ) ) { FuncOp func = llvm :: dyn_cast < FuncOp > ( call_op . resolveCallable ( ) ) ; if ( ! func ) continue ; value_to_visit = func . front ( ) . getTerminator ( ) -> getOperand ( value_to_visit . cast < OpResult > ( ) . getResultNumber ( ) ) ; values_to_visit . push_back ( value_to_visit ) ; continue ; } } return llvm :: None ; }
Returns XLA sharding from XlaSharding op connected to a result value.
Returns XLA sharding from XlaSharding op connected to a result value .
// Cast, real/imag, etc. // Exp, ceil, etc. // Identity // AddV2, Sub, etc.