Skip to content

Commit

Permalink
[spirv] [refactor] Rename debug_ segment to names_ (#6094)
Browse files Browse the repository at this point in the history
Issue: #6075 (followup)

### Brief Summary
Rename debug_ segment to a more proper name.
  • Loading branch information
ailzhang authored Sep 19, 2022
1 parent a8b5fc2 commit 18c946e
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 34 deletions.
12 changes: 6 additions & 6 deletions taichi/codegen/spirv/spirv_codegen.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class TaskCodegen : public IRVisitor {
Result run() {
ir_->init_header();
kernel_function_ = ir_->new_function(); // void main();
ir_->debug(spv::OpName, kernel_function_, "main");
ir_->debug_name(spv::OpName, kernel_function_, "main");

compile_args_struct();
compile_ret_struct();
Expand Down Expand Up @@ -1670,15 +1670,15 @@ class TaskCodegen : public IRVisitor {
task_attribs_.advisory_total_num_threads = kMaxNumThreadsGridStrideLoop;
}
task_attribs_.advisory_num_threads_per_group = stmt->block_dim;
ir_->debug(spv::OpName, begin_expr_value, "begin_expr_value");
ir_->debug(spv::OpName, total_elems, total_elems_name);
ir_->debug_name(spv::OpName, begin_expr_value, "begin_expr_value");
ir_->debug_name(spv::OpName, total_elems, total_elems_name);

spirv::Value begin_ =
ir_->add(ir_->cast(ir_->i32_type(), ir_->get_global_invocation_id(0)),
begin_expr_value);
ir_->debug(spv::OpName, begin_, "begin_");
ir_->debug_name(spv::OpName, begin_, "begin_");
spirv::Value end_ = ir_->add(total_elems, begin_expr_value);
ir_->debug(spv::OpName, end_, "end_");
ir_->debug_name(spv::OpName, end_, "end_");
const std::string total_invocs_name = "total_invocs";
// For now, |total_invocs_name| is equal to |total_elems|. Once we support
// dynamic range, they will be different.
Expand All @@ -1700,7 +1700,7 @@ class TaskCodegen : public IRVisitor {
false);
*/

ir_->debug(spv::OpName, total_invocs, total_invocs_name);
ir_->debug_name(spv::OpName, total_invocs, total_invocs_name);

// Must get init label after making value(to make sure they are correct)
spirv::Label init_label = ir_->current_label();
Expand Down
40 changes: 20 additions & 20 deletions taichi/codegen/spirv/spirv_ir_builder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,7 @@ std::vector<uint32_t> IRBuilder::finalize() {
data.insert(data.end(), entry_.begin(), entry_.end());
data.insert(data.end(), exec_mode_.begin(), exec_mode_.end());
data.insert(data.end(), strings_.begin(), strings_.end());
data.insert(data.end(), debug_.begin(), debug_.end());
data.insert(data.end(), names_.begin(), names_.end());
data.insert(data.end(), decorate_.begin(), decorate_.end());
data.insert(data.end(), global_.begin(), global_.end());
data.insert(data.end(), func_header_.begin(), func_header_.end());
Expand Down Expand Up @@ -218,7 +218,7 @@ void IRBuilder::init_pre_defs() {
const_i32_one_ = int_immediate_number(t_int32_, 1);
}

Value IRBuilder::make_string(std::string s) {
Value IRBuilder::debug_string(std::string s) {
Value val = new_value(SType(), ValueKind::kNormal);
ib_.begin(spv::OpString).add_seq(val, s).commit(&strings_);
return val;
Expand Down Expand Up @@ -592,7 +592,7 @@ SType IRBuilder::create_struct_type(
for (auto &[type, name, offset] : components) {
this->decorate(spv::OpMemberDecorate, struct_type, i, spv::DecorationOffset,
offset);
this->debug(spv::OpMemberName, struct_type, i, name);
this->debug_name(spv::OpMemberName, struct_type, i, name);
i++;
}

Expand All @@ -612,7 +612,7 @@ Value IRBuilder::buffer_struct_argument(const SType &struct_type,
storage_class = spv::StorageClassStorageBuffer;
}

this->debug(spv::OpName, struct_type, name + "_t");
this->debug_name(spv::OpName, struct_type, name + "_t");

if (device_->get_cap(cap::spirv_version) < 0x10300) {
// NOTE: BufferBlock was deprecated in SPIRV 1.3
Expand All @@ -625,14 +625,14 @@ Value IRBuilder::buffer_struct_argument(const SType &struct_type,

SType ptr_type = get_pointer_type(struct_type, storage_class);

this->debug(spv::OpName, ptr_type, name + "_ptr");
this->debug_name(spv::OpName, ptr_type, name + "_ptr");

Value val = new_value(ptr_type, ValueKind::kStructArrayPtr);
ib_.begin(spv::OpVariable)
.add_seq(ptr_type, val, storage_class)
.commit(&global_);

this->debug(spv::OpName, val, name);
this->debug_name(spv::OpName, val, name);

this->decorate(spv::OpDecorate, val, spv::DecorationDescriptorSet,
descriptor_set);
Expand All @@ -648,20 +648,20 @@ Value IRBuilder::uniform_struct_argument(const SType &struct_type,
// use StorageClassStorageBuffer instead.
spv::StorageClass storage_class = spv::StorageClassUniform;

this->debug(spv::OpName, struct_type, name + "_t");
this->debug_name(spv::OpName, struct_type, name + "_t");

this->decorate(spv::OpDecorate, struct_type, spv::DecorationBlock);

SType ptr_type = get_pointer_type(struct_type, storage_class);

this->debug(spv::OpName, ptr_type, name + "_ptr");
this->debug_name(spv::OpName, ptr_type, name + "_ptr");

Value val = new_value(ptr_type, ValueKind::kStructArrayPtr);
ib_.begin(spv::OpVariable)
.add_seq(ptr_type, val, storage_class)
.commit(&global_);

this->debug(spv::OpName, val, name);
this->debug_name(spv::OpName, val, name);

this->decorate(spv::OpDecorate, val, spv::DecorationDescriptorSet,
descriptor_set);
Expand All @@ -686,18 +686,18 @@ Value IRBuilder::buffer_argument(const SType &value_type,

auto typed_name = name + "_" + value_type.dt.to_string();

this->debug(spv::OpName, sarr_type, typed_name + "_struct_array");
this->debug_name(spv::OpName, sarr_type, typed_name + "_struct_array");

SType ptr_type = get_pointer_type(sarr_type, storage_class);

this->debug(spv::OpName, sarr_type, typed_name + "_ptr");
this->debug_name(spv::OpName, sarr_type, typed_name + "_ptr");

Value val = new_value(ptr_type, ValueKind::kStructArrayPtr);
ib_.begin(spv::OpVariable)
.add_seq(ptr_type, val, storage_class)
.commit(&global_);

this->debug(spv::OpName, val, typed_name);
this->debug_name(spv::OpName, val, typed_name);

this->decorate(spv::OpDecorate, val, spv::DecorationDescriptorSet,
descriptor_set);
Expand Down Expand Up @@ -744,7 +744,7 @@ Value IRBuilder::texture_argument(int num_channels,
descriptor_set);
this->decorate(spv::OpDecorate, val, spv::DecorationBinding, binding);

this->debug(spv::OpName, val, "tex");
this->debug_name(spv::OpName, val, "tex");

this->global_values.push_back(val);

Expand All @@ -769,7 +769,7 @@ Value IRBuilder::storage_image_argument(int num_channels,
descriptor_set);
this->decorate(spv::OpDecorate, val, spv::DecorationBinding, binding);

this->debug(spv::OpName, val, "tex");
this->debug_name(spv::OpName, val, "tex");

this->global_values.push_back(val);

Expand Down Expand Up @@ -1201,7 +1201,7 @@ void IRBuilder::register_value(std::string name, Value value) {
if (it != value_name_tbl_.end() && it->second.flag != ValueKind::kConstant) {
TI_ERROR("{} already exists.", name);
}
this->debug(
this->debug_name(
spv::OpName, value,
fmt::format("{}_{}", name, value.stype.dt.to_string())); // Debug info
value_name_tbl_[name] = value;
Expand Down Expand Up @@ -1439,13 +1439,13 @@ void IRBuilder::init_random_function(Value global_tmp_) {
ib_.begin(spv::OpVariable)
.add_seq(local_type, rand_w_, spv::StorageClassPrivate)
.commit(&global_);
debug(spv::OpName, rand_x_, "_rand_x");
debug(spv::OpName, rand_y_, "_rand_y");
debug(spv::OpName, rand_z_, "_rand_z");
debug(spv::OpName, rand_w_, "_rand_w");
debug_name(spv::OpName, rand_x_, "_rand_x");
debug_name(spv::OpName, rand_y_, "_rand_y");
debug_name(spv::OpName, rand_z_, "_rand_z");
debug_name(spv::OpName, rand_w_, "_rand_w");
SType gtmp_type = get_pointer_type(t_uint32_, spv::StorageClassStorageBuffer);
Value rand_gtmp_ = new_value(gtmp_type, ValueKind::kVariablePtr);
debug(spv::OpName, rand_gtmp_, "rand_gtmp");
debug_name(spv::OpName, rand_gtmp_, "rand_gtmp");

auto load_var = [&](Value pointer, const SType &res_type) {
TI_ASSERT(pointer.flag == ValueKind::kVariablePtr ||
Expand Down
23 changes: 15 additions & 8 deletions taichi/codegen/spirv/spirv_ir_builder.h
Original file line number Diff line number Diff line change
Expand Up @@ -209,10 +209,12 @@ class IRBuilder {
}

template <typename... Args>
void debug(spv::Op op, Args &&...args) {
ib_.begin(op).add_seq(std::forward<Args>(args)...).commit(&debug_);
void debug_name(spv::Op op, Args &&...args) {
ib_.begin(op).add_seq(std::forward<Args>(args)...).commit(&names_);
}

Value debug_string(std::string str);

template <typename... Args>
void execution_mode(Value func, Args &&...args) {
ib_.begin(spv::OpExecutionMode)
Expand Down Expand Up @@ -294,8 +296,6 @@ class IRBuilder {
double value,
bool cache = true);

Value make_string(std::string str);

// Match zero type
Value get_zero(const SType &stype) {
TI_ASSERT(stype.flag == TypeKind::kPrimitive);
Expand Down Expand Up @@ -464,7 +464,7 @@ class IRBuilder {

// Create a debugPrintf call
void call_debugprintf(std::string formats, const std::vector<Value> &args) {
Value format_str = make_string(formats);
Value format_str = debug_string(formats);
Value val = new_value(t_void_, ValueKind::kNormal);
ib_.begin(spv::OpExtInst)
.add_seq(t_void_, val, debug_printf_, 1, format_str);
Expand Down Expand Up @@ -619,11 +619,18 @@ class IRBuilder {
std::vector<uint32_t> entry_;
// Header segment
std::vector<uint32_t> exec_mode_;
// Debug segment
// According to SPIR-V spec, the following debug instructions must be
// grouped in the order:
// - All OpString, OpSourceExtension, OpSource, and OpSourceContinued,
// without forward references.
// - All OpName and all OpMemberName.
// - All OpModuleProcessed instructions.

// OpString segment
std::vector<uint32_t> strings_;
// TODO: Rename this to names_ in a followup PR
// Debug segment
std::vector<uint32_t> debug_;
// OpName segment
std::vector<uint32_t> names_;
// Annotation segment
std::vector<uint32_t> decorate_;
// Global segment: types, variables, types
Expand Down

0 comments on commit 18c946e

Please sign in to comment.