Skip to content

Commit 7f8d5d1

Browse files
authored
generate ss instead of ss_ptr in static cpp (#12)
* generate `ss` instead of `ss_ptr` in static cpp * update file hash in test data
1 parent 23f510e commit 7f8d5d1

11 files changed

Lines changed: 89 additions & 89 deletions

File tree

src/code-generator-static-impl.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,9 +69,9 @@ export class StaticCodeGenerator implements CodeGenerator, SourceBuilder {
6969
case "raw":
7070
return segment.content;
7171
case "code":
72-
return `(*ss_ptr) << ${this.#renderString(segment.content)};\n`;
72+
return `ss << ${this.#renderString(segment.content)};\n`;
7373
case "expression":
74-
return `(*ss_ptr) << ${segment.content};\n`;
74+
return `ss << ${segment.content};\n`;
7575
}
7676
})
7777
.join("");
@@ -205,10 +205,10 @@ export class StaticCodeGenerator implements CodeGenerator, SourceBuilder {
205205
206206
#pragma push_macro("MainFunctionStart")
207207
#undef MainFunctionStart
208-
#define MainFunctionStart ss_ptr = &shader_helper.MainFunctionBody
208+
#define MainFunctionStart() { [[maybe_unused]] auto& ss = shader_helper.MainFunctionBody();
209209
#pragma push_macro("MainFunctionEnd")
210210
#undef MainFunctionEnd
211-
#define MainFunctionEnd ss_ptr = &shader_helper.AdditionalImplementation
211+
#define MainFunctionEnd() }
212212
213213
// Helper templates
214214
@@ -271,7 +271,7 @@ std::string pass_as_string(T&& v) {
271271
filePath
272272
)}>::type ${paramsIsNotUsed ? "" : "params"}) {`
273273
);
274-
implContent.push(" OStringStream* ss_ptr = &shader_helper.AdditionalImplementation();");
274+
implContent.push(" [[maybe_unused]] auto& ss = shader_helper.AdditionalImplementation();");
275275
implContent.push("");
276276

277277
// Add parameter assignments for easier access

test/testcases/build-basic/expected/static-cpp/generated/shader/triangle.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,9 @@
77

88
template <>
99
Status ApplyTemplate<"shader/triangle.wgsl.template">(ShaderHelper& shader_helper, TemplateParameter<"shader/triangle.wgsl.template">::type ) {
10-
OStringStream* ss_ptr = &shader_helper.AdditionalImplementation();
10+
[[maybe_unused]] auto& ss = shader_helper.AdditionalImplementation();
1111

12-
(*ss_ptr) << __str_0;
12+
ss << __str_0;
1313

1414

1515
return Status::OK();

test/testcases/build-basic/expected/static-cpp/index_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
#pragma push_macro("MainFunctionStart")
1111
#undef MainFunctionStart
12-
#define MainFunctionStart ss_ptr = &shader_helper.MainFunctionBody
12+
#define MainFunctionStart() { [[maybe_unused]] auto& ss = shader_helper.MainFunctionBody();
1313
#pragma push_macro("MainFunctionEnd")
1414
#undef MainFunctionEnd
15-
#define MainFunctionEnd ss_ptr = &shader_helper.AdditionalImplementation
15+
#define MainFunctionEnd() }
1616

1717
// Helper templates
1818

@@ -35,7 +35,7 @@ std::string pass_as_string(T&& v) {
3535

3636
// Include template implementations
3737

38-
#include "generated/shader/triangle.h" // f5cec46558b917d8a4ec739f0ab02e71a31046d9c0d14028daa4aa0557a72da6
38+
#include "generated/shader/triangle.h" // d61822d677fc73d87adbe0b7b8e81d556ca515fef270bbe01dcdcb10596c350b
3939

4040
#pragma pop_macro("MainFunctionStart")
4141
#pragma pop_macro("MainFunctionEnd")

test/testcases/build-example-pad/expected/static-cpp-literal/generated/tensor/pad.h

Lines changed: 32 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
template <>
99
Status ApplyTemplate<"tensor/pad.wgsl.template">(ShaderHelper& shader_helper, TemplateParameter<"tensor/pad.wgsl.template">::type params) {
10-
OStringStream* ss_ptr = &shader_helper.AdditionalImplementation();
10+
[[maybe_unused]] auto& ss = shader_helper.AdditionalImplementation();
1111

1212
// Extract parameters
1313
auto& __param_dim_value_zero = params.param_dim_value_zero;
@@ -18,51 +18,51 @@ Status ApplyTemplate<"tensor/pad.wgsl.template">(ShaderHelper& shader_helper, Te
1818
auto& __var_output = *params.var_output;
1919

2020
MainFunctionStart();
21-
(*ss_ptr) << "\n ";
22-
(*ss_ptr) << shader_helper.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size");
23-
(*ss_ptr) << ";\n\n let constant_value =\n";
21+
ss << "\n ";
22+
ss << shader_helper.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size");
23+
ss << ";\n\n let constant_value =\n";
2424
if (__param_is_float16) {
25-
(*ss_ptr) << " bitcast<vec2<f16>>(uniforms.constant_value)[0];\n";
25+
ss << " bitcast<vec2<f16>>(uniforms.constant_value)[0];\n";
2626
} else {
27-
(*ss_ptr) << " bitcast<output_value_t>(uniforms.constant_value);\n";
27+
ss << " bitcast<output_value_t>(uniforms.constant_value);\n";
2828
}
2929
if (__param_dim_value_zero) {
30-
(*ss_ptr) << " output[global_idx] = constant_value;\n";
30+
ss << " output[global_idx] = constant_value;\n";
3131
} else {
32-
(*ss_ptr) << " let output_indices = ";
33-
(*ss_ptr) << __var_output.OffsetToIndices("global_idx");
34-
(*ss_ptr) << ";\n var input_index = u32(0);\n var use_pad_value = false;\n var in_coord = i32(0);\n\n for (var dim = 0; dim < ";
35-
(*ss_ptr) << __var_output.Rank();
36-
(*ss_ptr) << " && !use_pad_value; dim++) {\n let output_index = i32(";
37-
(*ss_ptr) << GetElementAt("output_indices", "dim", __var_output.Rank());
38-
(*ss_ptr) << ");\n let lower_pads = ";
39-
(*ss_ptr) << GetElementAt("uniforms.lower_pads", "dim", __var_output.Rank());
40-
(*ss_ptr) << ";\n let data_shape = i32(";
41-
(*ss_ptr) << GetElementAt("uniforms.data_shape", "dim", __var_output.Rank());
42-
(*ss_ptr) << ");\n";
32+
ss << " let output_indices = ";
33+
ss << __var_output.OffsetToIndices("global_idx");
34+
ss << ";\n var input_index = u32(0);\n var use_pad_value = false;\n var in_coord = i32(0);\n\n for (var dim = 0; dim < ";
35+
ss << __var_output.Rank();
36+
ss << " && !use_pad_value; dim++) {\n let output_index = i32(";
37+
ss << GetElementAt("output_indices", "dim", __var_output.Rank());
38+
ss << ");\n let lower_pads = ";
39+
ss << GetElementAt("uniforms.lower_pads", "dim", __var_output.Rank());
40+
ss << ";\n let data_shape = i32(";
41+
ss << GetElementAt("uniforms.data_shape", "dim", __var_output.Rank());
42+
ss << ");\n";
4343
if (__param_pad_mode == 0) {
44-
(*ss_ptr) << " if (output_index < lower_pads || output_index >= data_shape + lower_pads) {\n use_pad_value = true;\n";
44+
ss << " if (output_index < lower_pads || output_index >= data_shape + lower_pads) {\n use_pad_value = true;\n";
4545
} else if (__param_pad_mode == 2) {
46-
(*ss_ptr) << " if (output_index < lower_pads) {\n in_coord = 0;\n } else if (output_index >= data_shape + lower_pads) {\n in_coord = data_shape - 1;\n";
46+
ss << " if (output_index < lower_pads) {\n in_coord = 0;\n } else if (output_index >= data_shape + lower_pads) {\n in_coord = data_shape - 1;\n";
4747
} else if (__param_pad_mode == 1) {
48-
(*ss_ptr) << " if (output_index < lower_pads || output_index >= data_shape + lower_pads) {\n in_coord = output_index - lower_pads;\n if (in_coord < 0) {\n in_coord = -in_coord;\n }\n let _2n_1 = 2 * (data_shape - 1);\n in_coord = in_coord % _2n_1;\n if (in_coord >= data_shape) {\n in_coord = _2n_1 - in_coord;\n }\n";
48+
ss << " if (output_index < lower_pads || output_index >= data_shape + lower_pads) {\n in_coord = output_index - lower_pads;\n if (in_coord < 0) {\n in_coord = -in_coord;\n }\n let _2n_1 = 2 * (data_shape - 1);\n in_coord = in_coord % _2n_1;\n if (in_coord >= data_shape) {\n in_coord = _2n_1 - in_coord;\n }\n";
4949
} else {
50-
(*ss_ptr) << " if (output_index < lower_pads) {\n in_coord = data_shape + output_index - lower_pads;\n } else if (output_index >= data_shape + lower_pads) {\n in_coord = output_index - data_shape - lower_pads;\n";
50+
ss << " if (output_index < lower_pads) {\n in_coord = data_shape + output_index - lower_pads;\n } else if (output_index >= data_shape + lower_pads) {\n in_coord = output_index - data_shape - lower_pads;\n";
5151
}
52-
(*ss_ptr) << " } else {\n in_coord = output_index - lower_pads;\n }\n\n input_index += select(u32(in_coord)\n";
52+
ss << " } else {\n in_coord = output_index - lower_pads;\n }\n\n input_index += select(u32(in_coord)\n";
5353
if (__var_output.Rank() > 1) {
54-
(*ss_ptr) << " * ";
55-
(*ss_ptr) << GetElementAt("uniforms.data_stride", "dim", __var_output.Rank() - 1);
56-
(*ss_ptr) << "\n";
54+
ss << " * ";
55+
ss << GetElementAt("uniforms.data_stride", "dim", __var_output.Rank() - 1);
56+
ss << "\n";
5757
}
58-
(*ss_ptr) << " , u32(in_coord), dim == ";
59-
(*ss_ptr) << __var_output.Rank();
60-
(*ss_ptr) << " - 1);\n }\n\n ";
61-
(*ss_ptr) << __var_output.SetByOffset("global_idx", "select(data[input_index], constant_value, use_pad_value)");
62-
(*ss_ptr) << ";\n";
58+
ss << " , u32(in_coord), dim == ";
59+
ss << __var_output.Rank();
60+
ss << " - 1);\n }\n\n ";
61+
ss << __var_output.SetByOffset("global_idx", "select(data[input_index], constant_value, use_pad_value)");
62+
ss << ";\n";
6363
}
6464
MainFunctionEnd();
65-
(*ss_ptr) << "\n";
65+
ss << "\n";
6666

6767

6868
return Status::OK();

test/testcases/build-example-pad/expected/static-cpp-literal/index_impl.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@
99

1010
#pragma push_macro("MainFunctionStart")
1111
#undef MainFunctionStart
12-
#define MainFunctionStart ss_ptr = &shader_helper.MainFunctionBody
12+
#define MainFunctionStart() { [[maybe_unused]] auto& ss = shader_helper.MainFunctionBody();
1313
#pragma push_macro("MainFunctionEnd")
1414
#undef MainFunctionEnd
15-
#define MainFunctionEnd ss_ptr = &shader_helper.AdditionalImplementation
15+
#define MainFunctionEnd() }
1616

1717
// Helper templates
1818

@@ -34,7 +34,7 @@ std::string pass_as_string(T&& v) {
3434

3535
// Include template implementations
3636

37-
#include "generated/tensor/pad.h" // 0a0ab18c4abbbd85c08852d67af2741d77f44c28c604fe599c54a0d050ea354f
37+
#include "generated/tensor/pad.h" // 6903e8c7560b2507fffd2da3b327d9c6a354da219640c3131c09a92781356c9f
3838

3939
#pragma pop_macro("MainFunctionStart")
4040
#pragma pop_macro("MainFunctionEnd")
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(*ss_ptr) << "@compute @workgroup_size(128)\nfn main(@builtin(global_invocation_id) global_id: vec3<u32>) {\n let buffer_count = 4;\n let index = global_id.x;\n}\n";
1+
ss << "@compute @workgroup_size(128)\nfn main(@builtin(global_invocation_id) global_id: vec3<u32>) {\n let buffer_count = 4;\n let index = global_id.x;\n}\n";
Lines changed: 31 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
11
MainFunctionStart();
2-
(*ss_ptr) << "\n ";
3-
(*ss_ptr) << shader_helper.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size");
4-
(*ss_ptr) << ";\n\n let constant_value =\n";
2+
ss << "\n ";
3+
ss << shader_helper.GuardAgainstOutOfBoundsWorkgroupSizes("uniforms.output_size");
4+
ss << ";\n\n let constant_value =\n";
55
if (__param_is_float16) {
6-
(*ss_ptr) << " bitcast<vec2<f16>>(uniforms.constant_value)[0];\n";
6+
ss << " bitcast<vec2<f16>>(uniforms.constant_value)[0];\n";
77
} else {
8-
(*ss_ptr) << " bitcast<output_value_t>(uniforms.constant_value);\n";
8+
ss << " bitcast<output_value_t>(uniforms.constant_value);\n";
99
}
1010
if (__param_dim_value_zero) {
11-
(*ss_ptr) << " output[global_idx] = constant_value;\n";
11+
ss << " output[global_idx] = constant_value;\n";
1212
} else {
13-
(*ss_ptr) << " let output_indices = ";
14-
(*ss_ptr) << __var_output.OffsetToIndices("global_idx");
15-
(*ss_ptr) << ";\n var input_index = u32(0);\n var use_pad_value = false;\n var in_coord = i32(0);\n\n for (var dim = 0; dim < ";
16-
(*ss_ptr) << __var_output.Rank();
17-
(*ss_ptr) << " && !use_pad_value; dim++) {\n let output_index = i32(";
18-
(*ss_ptr) << GetElementAt("output_indices", "dim", __var_output.Rank());
19-
(*ss_ptr) << ");\n let lower_pads = ";
20-
(*ss_ptr) << GetElementAt("uniforms.lower_pads", "dim", __var_output.Rank());
21-
(*ss_ptr) << ";\n let data_shape = i32(";
22-
(*ss_ptr) << GetElementAt("uniforms.data_shape", "dim", __var_output.Rank());
23-
(*ss_ptr) << ");\n";
13+
ss << " let output_indices = ";
14+
ss << __var_output.OffsetToIndices("global_idx");
15+
ss << ";\n var input_index = u32(0);\n var use_pad_value = false;\n var in_coord = i32(0);\n\n for (var dim = 0; dim < ";
16+
ss << __var_output.Rank();
17+
ss << " && !use_pad_value; dim++) {\n let output_index = i32(";
18+
ss << GetElementAt("output_indices", "dim", __var_output.Rank());
19+
ss << ");\n let lower_pads = ";
20+
ss << GetElementAt("uniforms.lower_pads", "dim", __var_output.Rank());
21+
ss << ";\n let data_shape = i32(";
22+
ss << GetElementAt("uniforms.data_shape", "dim", __var_output.Rank());
23+
ss << ");\n";
2424
if (__param_pad_mode == 0) {
25-
(*ss_ptr) << " if (output_index < lower_pads || output_index >= data_shape + lower_pads) {\n use_pad_value = true;\n";
25+
ss << " if (output_index < lower_pads || output_index >= data_shape + lower_pads) {\n use_pad_value = true;\n";
2626
} else if (__param_pad_mode == 1) {
27-
(*ss_ptr) << " if (output_index < lower_pads) {\n in_coord = 0;\n } else if (output_index >= data_shape + lower_pads) {\n in_coord = data_shape - 1;\n";
27+
ss << " if (output_index < lower_pads) {\n in_coord = 0;\n } else if (output_index >= data_shape + lower_pads) {\n in_coord = data_shape - 1;\n";
2828
} else if (__param_pad_mode == 2) {
29-
(*ss_ptr) << " if (output_index < lower_pads || output_index >= data_shape + lower_pads) {\n in_coord = output_index - lower_pads;\n if (in_coord < 0) {\n in_coord = -in_coord;\n }\n let _2n_1 = 2 * (data_shape - 1);\n in_coord = in_coord % _2n_1;\n if (in_coord >= data_shape) {\n in_coord = _2n_1 - in_coord;\n }\n";
29+
ss << " if (output_index < lower_pads || output_index >= data_shape + lower_pads) {\n in_coord = output_index - lower_pads;\n if (in_coord < 0) {\n in_coord = -in_coord;\n }\n let _2n_1 = 2 * (data_shape - 1);\n in_coord = in_coord % _2n_1;\n if (in_coord >= data_shape) {\n in_coord = _2n_1 - in_coord;\n }\n";
3030
} else {
31-
(*ss_ptr) << " if (output_index < lower_pads) {\n in_coord = data_shape + output_index - lower_pads;\n } else if (output_index >= data_shape + lower_pads) {\n in_coord = output_index - data_shape - lower_pads;\n";
31+
ss << " if (output_index < lower_pads) {\n in_coord = data_shape + output_index - lower_pads;\n } else if (output_index >= data_shape + lower_pads) {\n in_coord = output_index - data_shape - lower_pads;\n";
3232
}
33-
(*ss_ptr) << " } else {\n in_coord = output_index - lower_pads;\n }\n\n input_index += select(u32(in_coord)\n";
33+
ss << " } else {\n in_coord = output_index - lower_pads;\n }\n\n input_index += select(u32(in_coord)\n";
3434
if (__var_output.Rank() > 1) {
35-
(*ss_ptr) << " * ";
36-
(*ss_ptr) << GetElementAt("uniforms.data_stride", "dim", __var_output.Rank() - 1);
37-
(*ss_ptr) << "\n";
35+
ss << " * ";
36+
ss << GetElementAt("uniforms.data_stride", "dim", __var_output.Rank() - 1);
37+
ss << "\n";
3838
}
39-
(*ss_ptr) << " , u32(in_coord), dim == ";
40-
(*ss_ptr) << __var_output.Rank();
41-
(*ss_ptr) << " - 1);\n }\n\n ";
42-
(*ss_ptr) << __var_output.SetByOffset("global_idx", "select(data[input_index], constant_value, use_pad_value)");
43-
(*ss_ptr) << ";\n";
39+
ss << " , u32(in_coord), dim == ";
40+
ss << __var_output.Rank();
41+
ss << " - 1);\n }\n\n ";
42+
ss << __var_output.SetByOffset("global_idx", "select(data[input_index], constant_value, use_pad_value)");
43+
ss << ";\n";
4444
}
4545
MainFunctionEnd();
46-
(*ss_ptr) << "\n";
46+
ss << "\n";
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
MainFunctionStart();
2-
(*ss_ptr) << "\n let index = global_id.x;\n\n output[index] = input[index] * 2.0;\n";
2+
ss << "\n let index = global_id.x;\n\n output[index] = input[index] * 2.0;\n";
33
MainFunctionEnd();
4-
(*ss_ptr) << "\n";
4+
ss << "\n";
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
(*ss_ptr) << "let size = BUFFER_SIZE;\n";
1+
ss << "let size = BUFFER_SIZE;\n";
Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
(*ss_ptr) << "let size = ";
2-
(*ss_ptr) << __param_BUFFER_SIZE;
3-
(*ss_ptr) << ";\n";
1+
ss << "let size = ";
2+
ss << __param_BUFFER_SIZE;
3+
ss << ";\n";

0 commit comments

Comments
 (0)