Skip to content

Commit d6be064

Browse files
committed
xe: jit: add comments to ir codegen
1 parent a4ad80c commit d6be064

File tree

3 files changed

+79
-22
lines changed

3 files changed

+79
-22
lines changed

src/gpu/intel/jit/codegen/codegen.cpp

Lines changed: 23 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -98,25 +98,28 @@ class ir_to_ngen_t : public ir_visitor_t {
9898
}
9999
expr_binding_.bind(obj.buf, rbd);
100100
}
101-
gpu_trace() << "codegen:bind " << obj.buf << " -> "
102-
<< expr_binding_.get(obj.buf);
101+
host_->comment(
102+
obj.line_str() + " -> " + expr_binding_.get(obj.buf).str());
103103
visit(obj.body);
104104
if (do_alloc) expr_binding_.unbind(obj.buf);
105105
if (use_bc_alloc) release_bank_conflict_allocation(obj);
106106
}
107107

108108
void _visit(const for_t &obj) override {
109+
host_->comment(obj.line_str());
109110
auto scope = register_scope();
110111
auto var_op = scope.alloc_reg_data(obj.var.type());
111112
bool dynamic_loop = !is_const(obj.init) || !is_const(obj.bound);
112113
auto init_op = eval(obj.init, scope);
113114
auto bound_op = eval(obj.bound, scope);
114115
auto step_op = eval(obj.step, scope);
115116

116-
host_->emov(1, var_op, init_op);
117117
expr_binding_.bind(obj.var, var_op);
118-
gpu_trace() << "codegen:bind " << obj.var << " -> "
119-
<< expr_binding_.get(obj.var);
118+
host_->comment(
119+
obj.var.str() + " -> " + expr_binding_.get(obj.var).str());
120+
121+
host_->emov(1, var_op, init_op);
122+
120123
// For dynamic loops use standard format otherwise
121124
// use do-while format.
122125
if (dynamic_loop) {
@@ -141,9 +144,11 @@ class ir_to_ngen_t : public ir_visitor_t {
141144
}
142145

143146
expr_binding_.unbind(obj.var);
147+
host_->comment("end " + obj.line_str());
144148
}
145149

146150
void _visit(const func_call_t &obj) override {
151+
host_->comment(obj.line_str());
147152
auto scope = register_scope();
148153

149154
auto &func = obj.func;
@@ -202,6 +207,7 @@ class ir_to_ngen_t : public ir_visitor_t {
202207

203208
void _visit(const if_t &obj) override {
204209
gpu_assert(obj.cond.type().elems() == simd_size_);
210+
host_->comment(obj.line_str());
205211

206212
bool has_else = !obj.else_body.is_empty();
207213
auto scope = register_scope();
@@ -213,18 +219,20 @@ class ir_to_ngen_t : public ir_visitor_t {
213219
has_else ? l_else : l_endif, l_endif);
214220
visit(obj.body);
215221
if (has_else) {
222+
host_->comment("else // " + obj.line_str());
216223
host_->else_(simd_size_, l_endif, l_endif);
217224
host_->mark(l_else);
218225
visit(obj.else_body);
219226
}
220227
host_->mark(l_endif);
221228
host_->endif(simd_size_);
229+
host_->comment("end " + obj.line_str());
222230
}
223231

224232
void _visit(const let_t &obj) override {
225233
if (obj.value.is_empty()) {
226234
auto var_op = expr_binding_.get(obj.var);
227-
gpu_trace() << "codegen:bind " << obj.var << " -> " << var_op;
235+
host_->comment(obj.line_str() + " -> " + var_op.str());
228236
// External variable, must be already bound.
229237
gpu_assert(expr_binding_.is_bound(obj.var))
230238
<< "Variable is not defined: " << obj.var;
@@ -233,6 +241,7 @@ class ir_to_ngen_t : public ir_visitor_t {
233241
}
234242

235243
auto scope = register_scope();
244+
host_->comment(obj.line_str());
236245
if (is_const(obj.value) || is_shuffle_const(obj.value)
237246
|| obj.var.type() != obj.value.type()) {
238247
auto &var_type = obj.var.type();
@@ -247,7 +256,7 @@ class ir_to_ngen_t : public ir_visitor_t {
247256
}
248257

249258
auto var_op = expr_binding_.get(obj.var);
250-
gpu_trace() << "codegen:bind " << obj.var << " -> " << var_op;
259+
host_->comment(obj.var.str() + " -> " + var_op.str());
251260

252261
// At this point the scope contains allocations for temporary
253262
// expressions. We need to 1) query and later re-claim the allocation
@@ -279,6 +288,7 @@ class ir_to_ngen_t : public ir_visitor_t {
279288
}
280289

281290
void _visit(const store_t &obj) override {
291+
host_->comment(obj.line_str());
282292
auto scope = register_scope();
283293
auto buf_op = eval(obj.buf, scope);
284294
auto off = to_cpp<int>(obj.off);
@@ -303,6 +313,7 @@ class ir_to_ngen_t : public ir_visitor_t {
303313
}
304314

305315
void _visit(const while_t &obj) override {
316+
host_->comment(obj.line_str());
306317
auto scope = register_scope();
307318

308319
ngen::Label loop_end_label;
@@ -314,6 +325,7 @@ class ir_to_ngen_t : public ir_visitor_t {
314325
visit(obj.body);
315326
host_->jmpi(1, loop_begin_label);
316327
host_->mark(loop_end_label);
328+
host_->comment("end " + obj.line_str());
317329
}
318330

319331
private:
@@ -1567,15 +1579,19 @@ template <typename ngen_generator_t>
15671579
void convert_ir_to_ngen_impl(const stmt_t &body, ngen_generator_t *host,
15681580
const walk_order_t *kernel_grid_walk_order) {
15691581
expr_binding_t expr_binding(host->getHardware());
1582+
host->comment("Prologue");
15701583
host->generate_prologue();
1584+
15711585
host->bind_external_vars(body, expr_binding);
15721586
if (kernel_grid_walk_order)
15731587
host->bind_kernel_grid_walk_order(
15741588
*kernel_grid_walk_order, expr_binding);
15751589

1590+
host->comment("IR");
15761591
ir_to_ngen_t<ngen_generator_t> visitor(host, expr_binding);
15771592
visitor.visit(body);
15781593

1594+
host->comment("Epilogue");
15791595
host->generate_epilogue();
15801596
}
15811597

src/gpu/intel/jit/ir/core.hpp

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2075,6 +2075,12 @@ class alloc_t : public stmt_impl_t {
20752075
: 0;
20762076
}
20772077

2078+
std::string line_str() const {
2079+
std::ostringstream out;
2080+
out << "alloc " << buf.as<var_t>().name << "[" << size << "]";
2081+
return out.str();
2082+
}
2083+
20782084
IR_DECLARE_TRAVERSERS()
20792085

20802086
expr_t buf;
@@ -2148,6 +2154,17 @@ class store_t : public stmt_impl_t {
21482154

21492155
bool has_default_stride() const { return stride == default_stride; }
21502156

2157+
std::string line_str() const {
2158+
std::ostringstream out;
2159+
out << load_t::make(value.type(), buf, off, stride);
2160+
out << " = " << value;
2161+
if (!mask.is_empty()) {
2162+
out << ", mask = " << mask.str();
2163+
if (fill_mask0) out << " [FILL]";
2164+
}
2165+
return out.str();
2166+
}
2167+
21512168
IR_DECLARE_TRAVERSERS()
21522169

21532170
static const int default_stride = -1;
@@ -2207,6 +2224,14 @@ class for_t : public stmt_impl_t {
22072224
return ir_utils::get_hash(var, init, bound, body, step, unroll);
22082225
}
22092226

2227+
std::string line_str() const {
2228+
std::ostringstream out;
2229+
out << "for (" << var << " = " << init << "; " << var << " < " << bound
2230+
<< "; " << var << " += " << step << ") ";
2231+
if (unroll != 1) out << "[unroll: " << unroll << "] ";
2232+
return out.str();
2233+
}
2234+
22102235
IR_DECLARE_TRAVERSERS()
22112236

22122237
expr_t var;
@@ -2256,6 +2281,12 @@ class if_t : public stmt_impl_t {
22562281
return ir_utils::get_hash(cond, body, else_body);
22572282
}
22582283

2284+
std::string line_str() const {
2285+
std::ostringstream oss;
2286+
oss << "if (" << cond << ")";
2287+
return oss.str();
2288+
}
2289+
22592290
IR_DECLARE_TRAVERSERS()
22602291

22612292
expr_t cond;
@@ -2304,6 +2335,12 @@ class let_t : public stmt_impl_t {
23042335
return utils::rnd_up(var.type().size(), reg_allocator_t::granularity);
23052336
};
23062337

2338+
std::string line_str() const {
2339+
std::ostringstream out;
2340+
out << var << "." << var.type() << " = " << value;
2341+
return out.str();
2342+
}
2343+
23072344
IR_DECLARE_TRAVERSERS()
23082345

23092346
expr_t var;
@@ -2498,6 +2535,12 @@ class while_t : public stmt_impl_t {
24982535

24992536
size_t get_hash() const override { return ir_utils::get_hash(cond, body); }
25002537

2538+
std::string line_str() const {
2539+
std::ostringstream out;
2540+
out << "while (" << cond << ")";
2541+
return out.str();
2542+
}
2543+
25012544
IR_DECLARE_TRAVERSERS()
25022545

25032546
expr_t cond;
@@ -2660,6 +2703,13 @@ class func_call_t : public stmt_impl_t {
26602703

26612704
size_t get_hash() const override { return ir_utils::get_hash(args, attr); }
26622705

2706+
std::string line_str() const {
2707+
std::ostringstream out;
2708+
out << func << "(" << ir_utils::make_seq_print_helper(args) << ")";
2709+
if (!attr.is_empty()) out << " " << attr;
2710+
return out.str();
2711+
}
2712+
26632713
IR_DECLARE_TRAVERSERS()
26642714

26652715
func_t func;

src/gpu/intel/jit/ir/ir.cpp

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,7 @@ class ir_printer_t : public ir_visitor_t {
4343
auto grf_size = 1; // Assume all objects are grf aligned
4444
auto guard = mem_usage_guard(obj.register_alloc_size(grf_size));
4545
print_indent();
46-
out_ << "alloc " << obj.buf.as<var_t>().name << "[" << obj.size
47-
<< "] (mem_usage: " << mem_usage_bytes_ << ")\n";
46+
out_ << obj.line_str() << "(mem_usage: " << mem_usage_bytes_ << ")\n";
4847
visit(obj.body);
4948
}
5049

@@ -98,16 +97,14 @@ class ir_printer_t : public ir_visitor_t {
9897

9998
void _visit(const func_call_t &obj) override {
10099
print_indent();
101-
out_ << obj.func << "(" << make_seq_print_helper(obj.args) << ")";
102-
if (!obj.attr.is_empty()) out_ << " " << obj.attr;
103-
out_ << "\n";
100+
out_ << obj.line_str() << "\n";
104101
}
105102

106103
void _visit(const func_impl_t &obj) override { out_ << obj.str(); }
107104

108105
void _visit(const if_t &obj) override {
109106
print_indent();
110-
out_ << "if (" << strip_parens(obj.cond.str()) << ") {\n";
107+
out_ << obj.line_str() << " {\n";
111108
add_indent();
112109
visit(obj.body);
113110
remove_indent();
@@ -137,7 +134,7 @@ class ir_printer_t : public ir_visitor_t {
137134
int size = obj.register_alloc_size();
138135
auto guard = mem_usage_guard(size);
139136
print_indent();
140-
out_ << obj.var << "." << obj.var.type() << " = " << obj.value << "\n";
137+
out_ << obj.line_str() << "\n";
141138
visit(obj.body);
142139
}
143140

@@ -216,13 +213,7 @@ class ir_printer_t : public ir_visitor_t {
216213

217214
void _visit(const store_t &obj) override {
218215
print_indent();
219-
out_ << load_t::make(obj.value.type(), obj.buf, obj.off, obj.stride);
220-
out_ << " = " << obj.value;
221-
if (!obj.mask.is_empty()) {
222-
out_ << ", mask = " << obj.mask.str();
223-
if (obj.fill_mask0) out_ << " [FILL]";
224-
}
225-
out_ << "\n";
216+
out_ << obj.line_str() << "\n";
226217
}
227218

228219
void _visit(const ternary_op_t &obj) override {
@@ -239,7 +230,7 @@ class ir_printer_t : public ir_visitor_t {
239230

240231
void _visit(const while_t &obj) override {
241232
print_indent();
242-
out_ << "while (" << obj.cond << ") {\n";
233+
out_ << obj.line_str() << " {\n";
243234
add_indent();
244235
visit(obj.body);
245236
remove_indent();

0 commit comments

Comments
 (0)