From c6cb005465f1dfc9b023e5d1184f7ea7e1601289 Mon Sep 17 00:00:00 2001 From: TennyZhuang Date: Tue, 19 Oct 2021 03:24:27 +0800 Subject: [PATCH] use ostream --- taichi/ir/expr.cpp | 2 +- taichi/ir/expr.h | 2 +- taichi/ir/expression.cpp | 2 +- taichi/ir/expression.h | 4 +-- taichi/ir/frontend_ir.cpp | 12 ++++----- taichi/ir/frontend_ir.h | 44 ++++++++++++++++----------------- taichi/util/action_recorder.cpp | 8 +++--- taichi/util/action_recorder.h | 2 +- 8 files changed, 38 insertions(+), 38 deletions(-) diff --git a/taichi/ir/expr.cpp b/taichi/ir/expr.cpp index fbf32c111221e..8fd0ea3dc4a7f 100644 --- a/taichi/ir/expr.cpp +++ b/taichi/ir/expr.cpp @@ -6,7 +6,7 @@ TLANG_NAMESPACE_BEGIN -void Expr::serialize(std::stringstream &ss) const { +void Expr::serialize(std::ostream &ss) const { TI_ASSERT(expr); expr->serialize(ss); } diff --git a/taichi/ir/expr.h b/taichi/ir/expr.h index 6f8169100ce23..e9c2182d0799d 100644 --- a/taichi/ir/expr.h +++ b/taichi/ir/expr.h @@ -78,7 +78,7 @@ class Expr { Expr operator[](const ExprGroup &indices) const; std::string serialize() const; - void serialize(std::stringstream &ss) const; + void serialize(std::ostream &ss) const; void operator+=(const Expr &o); void operator-=(const Expr &o); diff --git a/taichi/ir/expression.cpp b/taichi/ir/expression.cpp index 63be887cdc19e..7b8c7b4015ef9 100644 --- a/taichi/ir/expression.cpp +++ b/taichi/ir/expression.cpp @@ -18,7 +18,7 @@ ExprGroup ExprGroup::loaded() const { return indices_loaded; } -void ExprGroup::serialize(std::stringstream &ss) const { +void ExprGroup::serialize(std::ostream &ss) const { for (int i = 0; i < (int)exprs.size(); i++) { exprs[i].serialize(ss); if (i + 1 < (int)exprs.size()) { diff --git a/taichi/ir/expression.h b/taichi/ir/expression.h index b585f322112c4..9696754025a88 100644 --- a/taichi/ir/expression.h +++ b/taichi/ir/expression.h @@ -37,7 +37,7 @@ class Expression { stmt = nullptr; } - virtual void serialize(std::stringstream &ss) = 0; + virtual void serialize(std::ostream &ss) = 0; virtual void flatten(FlattenContext *ctx) { TI_NOT_IMPLEMENTED; @@ -99,7 +99,7 @@ class ExprGroup { return exprs[i]; } - void serialize(std::stringstream &ss) const; + void serialize(std::ostream &ss) const; std::string serialize() const; diff --git a/taichi/ir/frontend_ir.cpp b/taichi/ir/frontend_ir.cpp index 43cdded644318..20247f04d9d23 100644 --- a/taichi/ir/frontend_ir.cpp +++ b/taichi/ir/frontend_ir.cpp @@ -100,7 +100,7 @@ void RandExpression::flatten(FlattenContext *ctx) { stmt = ctx->back_stmt(); } -void UnaryOpExpression::serialize(std::stringstream &ss) { +void UnaryOpExpression::serialize(std::ostream &ss) { ss << '('; if (is_cast()) { ss << (type == UnaryOpType::cast_value ? "" : "reinterpret_"); @@ -187,7 +187,7 @@ void GlobalVariableExpression::flatten(FlattenContext *ctx) { ctx->push_back(std::move(ptr)); } -void GlobalPtrExpression::serialize(std::stringstream &ss) { +void GlobalPtrExpression::serialize(std::ostream &ss) { if (snode) { ss << snode->get_node_type_name_hinted(); } else { @@ -304,7 +304,7 @@ void RangeAssumptionExpression::flatten(FlattenContext *ctx) { stmt = ctx->back_stmt(); } -void LoopUniqueExpression::serialize(std::stringstream &ss) { +void LoopUniqueExpression::serialize(std::ostream &ss) { ss << "loop_unique("; input.serialize(ss); for (int i = 0; i < covers.size(); i++) { @@ -344,7 +344,7 @@ void IdExpression::flatten(FlattenContext *ctx) { } } -void AtomicOpExpression::serialize(std::stringstream &ss) { +void AtomicOpExpression::serialize(std::ostream &ss) { if (op_type == AtomicOpType::add) { ss << "atomic_add("; } else if (op_type == AtomicOpType::sub) { @@ -395,7 +395,7 @@ void AtomicOpExpression::flatten(FlattenContext *ctx) { stmt = ctx->back_stmt(); } -void SNodeOpExpression::serialize(std::stringstream &ss) { +void SNodeOpExpression::serialize(std::ostream &ss) { ss << snode_op_type_name(op_type); ss << '('; ss << snode->get_node_type_name_hinted() << ", ["; @@ -477,7 +477,7 @@ void FuncCallExpression::flatten(FlattenContext *ctx) { stmt = ctx->back_stmt(); } -void FuncCallExpression::serialize(std::stringstream &ss) { +void FuncCallExpression::serialize(std::ostream &ss) { ss << "func_call(\"" << func->func_key.get_full_name() << "\", "; args.serialize(ss); ss << ')'; diff --git a/taichi/ir/frontend_ir.h b/taichi/ir/frontend_ir.h index 835c096a00894..b8adc2bc9ccc1 100644 --- a/taichi/ir/frontend_ir.h +++ b/taichi/ir/frontend_ir.h @@ -242,7 +242,7 @@ class ArgLoadExpression : public Expression { ArgLoadExpression(int arg_id, DataType dt) : arg_id(arg_id), dt(dt) { } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << fmt::format("arg[{}] (dt={})", arg_id, data_type_name(dt)); } @@ -256,7 +256,7 @@ class RandExpression : public Expression { RandExpression(DataType dt) : dt(dt) { } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << fmt::format("rand<{}>()", data_type_name(dt)); } @@ -276,7 +276,7 @@ class UnaryOpExpression : public Expression { bool is_cast() const; - void serialize(std::stringstream &ss) override; + void serialize(std::ostream &ss) override; void flatten(FlattenContext *ctx) override; }; @@ -292,7 +292,7 @@ class BinaryOpExpression : public Expression { this->rhs.set(smart_load(rhs)); } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << '('; lhs->serialize(ss); ss << ' '; @@ -320,7 +320,7 @@ class TernaryOpExpression : public Expression { this->op3.set(load_if_ptr(op3)); } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << ternary_type_name(type) << '('; op1->serialize(ss); ss << ' '; @@ -346,7 +346,7 @@ class InternalFuncCallExpression : public Expression { } } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << "internal call " << func_name << '('; std::string args_str; for (int i = 0; i < args.size(); i++) { @@ -375,7 +375,7 @@ class ExternalFuncCallExpression : public Expression { : func(func), source(source), args(args), outputs(outputs) { } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { if (func) { ss << fmt::format("call {:x} (", (uint64)func); } else { @@ -416,7 +416,7 @@ class ExternalTensorExpression : public Expression { set_attribute("dim", std::to_string(dim)); } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << fmt::format("{}d_ext_arr", dim); } @@ -452,7 +452,7 @@ class GlobalVariableExpression : public Expression { set_attribute("dim", std::to_string(snode->num_active_indices)); } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << "#" << ident.name(); } @@ -473,7 +473,7 @@ class GlobalPtrExpression : public Expression { : snode(snode), indices(indices) { } - void serialize(std::stringstream &ss) override; + void serialize(std::ostream &ss) override; void flatten(FlattenContext *ctx) override; @@ -500,7 +500,7 @@ class TensorElementExpression : public Expression { bool is_global_tensor() const; - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { var.serialize(ss); ss << '['; for (int i = 0; i < (int)indices.size(); i++) { @@ -533,7 +533,7 @@ class EvalExpression : public Expression { // cache stmt->id since it may be released later } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << '%' << stmt_id; } @@ -554,7 +554,7 @@ class RangeAssumptionExpression : public Expression { : input(input), base(base), low(low), high(high) { } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << "assume_in_range({"; base.serialize(ss); ss << fmt::format("{:+d}", low); @@ -577,7 +577,7 @@ class LoopUniqueExpression : public Expression { : input(input), covers(covers) { } - void serialize(std::stringstream &ss) override; + void serialize(std::ostream &ss) override; void flatten(FlattenContext *ctx) override; }; @@ -590,7 +590,7 @@ class IdExpression : public Expression { IdExpression(const Identifier &id) : id(id) { } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << id.name(); } @@ -615,7 +615,7 @@ class AtomicOpExpression : public Expression { : op_type(op_type), dest(dest), val(val) { } - void serialize(std::stringstream &ss) override; + void serialize(std::ostream &ss) override; void flatten(FlattenContext *ctx) override; }; @@ -638,7 +638,7 @@ class SNodeOpExpression : public Expression { : snode(snode), op_type(op_type), indices(indices), value(value) { } - void serialize(std::stringstream &ss) override; + void serialize(std::ostream &ss) override; void flatten(FlattenContext *ctx) override; }; @@ -649,7 +649,7 @@ class LocalLoadExpression : public Expression { LocalLoadExpression(const Expr &ptr) : ptr(ptr) { } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << "lcl load "; ptr.serialize(ss); } @@ -663,7 +663,7 @@ class GlobalLoadExpression : public Expression { GlobalLoadExpression(const Expr &ptr) : ptr(ptr) { } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << "gbl load "; ptr.serialize(ss); } @@ -679,7 +679,7 @@ class ConstExpression : public Expression { ConstExpression(const T &x) : val(x) { } - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << val.stringify(); } @@ -691,7 +691,7 @@ class ExternalTensorShapeAlongAxisExpression : public Expression { Expr ptr; int axis; - void serialize(std::stringstream &ss) override { + void serialize(std::ostream &ss) override { ss << "external_tensor_shape_along_axis("; ptr->serialize(ss); ss << ", " << axis << ')'; @@ -709,7 +709,7 @@ class FuncCallExpression : public Expression { Function *func; ExprGroup args; - void serialize(std::stringstream &ss) override; + void serialize(std::ostream &ss) override; FuncCallExpression(Function *func, const ExprGroup &args) : func(func), args(args) { diff --git a/taichi/util/action_recorder.cpp b/taichi/util/action_recorder.cpp index a14da84f8c5f8..bf74ad0acee45 100644 --- a/taichi/util/action_recorder.cpp +++ b/taichi/util/action_recorder.cpp @@ -3,7 +3,7 @@ TI_NAMESPACE_BEGIN -void ActionArg::serialize(std::stringstream &ss) const { +void ActionArg::serialize(std::ostream &ss) const { ss << key << ": "; if (type == argument_type::str) { ss << lang::c_quoted(val_str); @@ -46,9 +46,9 @@ void ActionRecorder::record(const std::string &content, return; ofs << "- action: \"" << content << "\"" << std::endl; for (auto &arg : arguments) { - std::stringstream ss; - arg.serialize(ss); - ofs << " " << ss.str() << std::endl; + ofs << " "; + arg.serialize(ofs); + ofs << std::endl; } ofs.flush(); } diff --git a/taichi/util/action_recorder.h b/taichi/util/action_recorder.h index be925a3ec7fa9..9a2c1ce0a6fa2 100644 --- a/taichi/util/action_recorder.h +++ b/taichi/util/action_recorder.h @@ -27,7 +27,7 @@ struct ActionArg { : key(key), val_float64(val), type(argument_type::float64) { } - void serialize(std::stringstream &ss) const; + void serialize(std::ostream &ss) const; std::string key;