Skip to content

Commit e1ab97b

Browse files
committed
Stub handling of value declarations.
1 parent dad71ef commit e1ab97b

File tree

6 files changed

+579
-484
lines changed

6 files changed

+579
-484
lines changed

regression-tests/pure2-autodiff.cpp2

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,13 @@ ad_test: @autodiff @print type = {
8181
direct_return: (x: double, y: double) -> double = {
8282
return x + y;
8383
}
84+
85+
intermediate_var: (x: double, y: double) -> (r: double) = {
86+
t: double = (); // TODO: change to x initializer when we have access to the initializer expression.
87+
t = x + y;
88+
89+
r = t;
90+
}
8491
}
8592

8693
write_output: (func: std::string, x: double, x_d: double, y: double, y_d: double, ret) = {
@@ -111,4 +118,5 @@ main: () = {
111118
write_output("if branch", x, x_d, y, y_d, ad_test::if_branch_diff(x, x_d, y, y_d));
112119
write_output("if else branch", x, x_d, y, y_d, ad_test::if_else_branch_diff(x, x_d, y, y_d));
113120
write_output("direct return", x, x_d, y, y_d, ad_test::direct_return_diff(x, x_d, y, y_d));
121+
write_output("intermediate var", x, x_d, y, y_d, ad_test::intermediate_var_diff(x, x_d, y, y_d));
114122
}

regression-tests/test-results/gcc-13-c++2b/pure2-autodiff.cpp.execution

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,4 @@ diff(sin(x + y)) at (x = 2.000000, x_d = 1.000000, y = 3.000000, y_d = 2.000000)
1515
diff(if branch) at (x = 2.000000, x_d = 1.000000, y = 3.000000, y_d = 2.000000) = (r = 2.000000, r_d = 1.000000)
1616
diff(if else branch) at (x = 2.000000, x_d = 1.000000, y = 3.000000, y_d = 2.000000) = (r = 2.000000, r_d = 1.000000)
1717
diff(direct return) at (x = 2.000000, x_d = 1.000000, y = 3.000000, y_d = 2.000000) = (r = 5.000000, r_d = 3.000000)
18+
diff(intermediate var) at (x = 2.000000, x_d = 1.000000, y = 3.000000, y_d = 2.000000) = (r = 5.000000, r_d = 3.000000)

regression-tests/test-results/pure2-autodiff.cpp

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,11 @@ using if_else_branch_ret = double;
106106

107107
#line 81 "pure2-autodiff.cpp2"
108108
public: [[nodiscard]] static auto direct_return(cpp2::impl::in<double> x, cpp2::impl::in<double> y) -> double;
109+
using intermediate_var_ret = double;
110+
111+
112+
#line 85 "pure2-autodiff.cpp2"
113+
public: [[nodiscard]] static auto intermediate_var(cpp2::impl::in<double> x, cpp2::impl::in<double> y) -> intermediate_var_ret;
109114
struct add_1_diff_ret { double r; double r_d; };
110115

111116

@@ -179,17 +184,21 @@ struct direct_return_diff_ret { double r; double r_d; };
179184

180185
public: [[nodiscard]] static auto direct_return_diff(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> direct_return_diff_ret;
181186

187+
struct intermediate_var_diff_ret { double r; double r_d; };
188+
189+
public: [[nodiscard]] static auto intermediate_var_diff(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> intermediate_var_diff_ret;
190+
182191
public: ad_test() = default;
183192
public: ad_test(ad_test const&) = delete; /* No 'that' constructor, suppress copy */
184193
public: auto operator=(ad_test const&) -> void = delete;
185194

186195

187-
#line 84 "pure2-autodiff.cpp2"
196+
#line 91 "pure2-autodiff.cpp2"
188197
};
189198

190199
auto write_output(cpp2::impl::in<std::string> func, cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d, auto const& ret) -> void;
191200

192-
#line 90 "pure2-autodiff.cpp2"
201+
#line 97 "pure2-autodiff.cpp2"
193202
auto main() -> int;
194203

195204
//=== Cpp2 function definitions =================================================
@@ -329,6 +338,16 @@ auto main() -> int;
329338
return x + y;
330339
}
331340

341+
#line 85 "pure2-autodiff.cpp2"
342+
[[nodiscard]] auto ad_test::intermediate_var(cpp2::impl::in<double> x, cpp2::impl::in<double> y) -> intermediate_var_ret{
343+
cpp2::impl::deferred_init<double> r;
344+
#line 86 "pure2-autodiff.cpp2"
345+
double t {}; // TODO: change to x initializer when we have access to the initializer expression.
346+
t = x + y;
347+
348+
r.construct(cpp2::move(t));
349+
return std::move(r.value()); }
350+
332351
[[nodiscard]] auto ad_test::add_1_diff(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> add_1_diff_ret{
333352
double r {0.0};
334353
double r_d {0.0};r_d = x_d + y_d;r = x + y;return { std::move(r), std::move(r_d) };
@@ -447,13 +466,21 @@ auto temp_1 {x - y}; r_d = cos(temp_1) * cpp2::move(temp_1_d);
447466
double r_d {};r_d = x_d + y_d;r = x + y;
448467
return { std::move(r), std::move(r_d) };
449468
}
469+
[[nodiscard]] auto ad_test::intermediate_var_diff(cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d) -> intermediate_var_diff_ret{
470+
double r {0.0};
471+
double r_d {0.0};
472+
double t_d {};
450473

451-
#line 86 "pure2-autodiff.cpp2"
474+
double t {};
475+
t_d = x_d + y_d;t = x + y;r_d = cpp2::move(t_d);r = cpp2::move(t);return { std::move(r), std::move(r_d) };
476+
}
477+
478+
#line 93 "pure2-autodiff.cpp2"
452479
auto write_output(cpp2::impl::in<std::string> func, cpp2::impl::in<double> x, cpp2::impl::in<double> x_d, cpp2::impl::in<double> y, cpp2::impl::in<double> y_d, auto const& ret) -> void{
453480
std::cout << "diff(" + cpp2::to_string(func) + ") at (x = " + cpp2::to_string(x) + ", x_d = " + cpp2::to_string(x_d) + ", y = " + cpp2::to_string(y) + ", y_d = " + cpp2::to_string(y_d) + ") = (r = " + cpp2::to_string(ret.r) + ", r_d = " + cpp2::to_string(ret.r_d) + ")" << std::endl;
454481
}
455482

456-
#line 90 "pure2-autodiff.cpp2"
483+
#line 97 "pure2-autodiff.cpp2"
457484
auto main() -> int{
458485

459486
double x {2.0};
@@ -477,6 +504,7 @@ auto main() -> int{
477504
write_output("sin(x + y)", x, x_d, y, y_d, ad_test::sin_call_diff(x, x_d, y, y_d));
478505
write_output("if branch", x, x_d, y, y_d, ad_test::if_branch_diff(x, x_d, y, y_d));
479506
write_output("if else branch", x, x_d, y, y_d, ad_test::if_else_branch_diff(x, x_d, y, y_d));
480-
write_output("direct return", x, x_d, y, y_d, ad_test::direct_return_diff(cpp2::move(x), cpp2::move(x_d), cpp2::move(y), cpp2::move(y_d)));
507+
write_output("direct return", x, x_d, y, y_d, ad_test::direct_return_diff(x, x_d, y, y_d));
508+
write_output("intermediate var", x, x_d, y, y_d, ad_test::intermediate_var_diff(cpp2::move(x), cpp2::move(x_d), cpp2::move(y), cpp2::move(y_d)));
481509
}
482510

regression-tests/test-results/pure2-autodiff.cpp2.output

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,17 @@ ad_test:/* @autodiff @print */ type =
174174
return x + y;
175175
}
176176

177+
intermediate_var:(
178+
in x: double,
179+
in y: double,
180+
) -> (out r: double, ) =
181+
{
182+
t: double = ();
183+
t = x + y;
184+
r = t;
185+
return;
186+
}
187+
177188
add_1_diff:(
178189
in x: double,
179190
in x_d: double,
@@ -474,6 +485,25 @@ ad_test:/* @autodiff @print */ type =
474485
r = x + y;
475486
return;
476487
}
488+
489+
intermediate_var_diff:(
490+
in x: double,
491+
in x_d: double,
492+
in y: double,
493+
in y_d: double,
494+
) -> (
495+
out r: double = 0.0,
496+
out r_d: double = 0.0,
497+
) =
498+
{
499+
t_d: double = ();
500+
t: double = ();
501+
t_d = x_d + y_d;
502+
t = x + y;
503+
r_d = t_d;
504+
r = t;
505+
return;
506+
}
477507
}
478508
ok (all Cpp2, passes safety checks)
479509

0 commit comments

Comments
 (0)