Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[lang] Fix scalarization for PrintStmt #6945

Merged
merged 5 commits into from
Dec 23, 2022
Merged
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
46 changes: 43 additions & 3 deletions taichi/transforms/scalarize.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -281,15 +281,55 @@ class Scalarize : public BasicStmtVisitor {
Stmt *print_stmt = std::get<Stmt *>(content);
if (print_stmt->is<MatrixInitStmt>()) {
auto matrix_init_stmt = print_stmt->cast<MatrixInitStmt>();
for (size_t j = 0; j < matrix_init_stmt->values.size(); j++) {
new_contents.push_back(matrix_init_stmt->values[j]);
auto tensor_shape =
print_stmt->ret_type->as<TensorType>()->get_shape();

bool is_matrix = tensor_shape.size() == 2;
int m = tensor_shape[0];
int n = is_matrix ? tensor_shape[1] : 1;
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved

new_contents.push_back("[");
if (is_matrix) {
for (size_t i = 0; i < m; i++) {
new_contents.push_back("[");
for (size_t j = 0; j < n; j++) {
size_t index = i * n + j;
new_contents.push_back(matrix_init_stmt->values[index]);
new_contents.push_back(", ");
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved
}
new_contents.push_back("], ");
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
for (size_t i = 0; i < m; i++) {
new_contents.push_back(matrix_init_stmt->values[i]);
new_contents.push_back(", ");
jim19930609 marked this conversation as resolved.
Show resolved Hide resolved
}
}
new_contents.push_back("]");
} else {
new_contents.push_back(print_stmt);
}
}
}
modifier_.insert_before(stmt, Stmt::make<PrintStmt>(new_contents));

// Merge string contents
std::vector<std::variant<Stmt *, std::string>> merged_contents;
std::string merged_string = "";
for (const auto &content : new_contents) {
if (auto string_content = std::get_if<std::string>(&content)) {
merged_string += *string_content;
} else {
if (!merged_string.empty()) {
merged_contents.push_back(merged_string);
merged_string = "";
}
merged_contents.push_back(content);
}
}
if (!merged_string.empty())
merged_contents.push_back(merged_string);

modifier_.insert_before(stmt, Stmt::make<PrintStmt>(merged_contents));
modifier_.erase(stmt);
}

Expand Down