Skip to content

Commit

Permalink
Add reflection for binary expressions, and first working autodiff exa…
Browse files Browse the repository at this point in the history
…mple

The test @autodiff function can now handle the "r = a + b;" case.

This code:

   test: @autodiff @print type = {

      // A simple first function would be:
      // func: (a: double, b: double) -> double = { return a + b; }
      func: (a: double, b: double) -> (r: double) = { r = a + b; }

      // The generated autodiff forward transformation would be:
      // func_diff: (a: double, a_d: double, b: double, b_d: double) -> (r: double, r_d: double) = {
      //     r_d = a * b_d + b * a_d;
      //     r = a * b;
      // }

   }

Generates the following, as reported by @print in the cppfront compile output:

   test:/* @autodiff @print */ type =
   {
      func:(
         in a: double,
         in b: double,
      ) -> (out r: double, ) =
      {
         r = a + b;
         return;
      }

      func_diff:(
         in a: double,
         in a_d: double,
         in b: double,
         in b_d: double,
      ) -> (
               out r: double = 1,
               out r_d: double = 1,
         ) =
      {
         r_d = a * b_d + b * a_d;
         r = a * b;
         return;
      }
   }

And a bug fix: When a file that contains Cpp2 code begins with Cpp1 #includes, preserve their position at the top of the generated file too. This allows a Cpp2 type's template parameter list to use types a from Cpp1 header file.
  • Loading branch information
hsutter committed Feb 6, 2025
1 parent 8a99d2e commit 61dc376
Show file tree
Hide file tree
Showing 34 changed files with 1,570 additions and 677 deletions.
4 changes: 2 additions & 2 deletions build_h2.bat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
@echo off
cd source
cppfront reflect.h2 -verb
cppfront reflect.h2 -verb %1
cd ..\include
cppfront cpp2regex.h2 -verb
cppfront cpp2regex.h2 -verb %1
cd..
4 changes: 2 additions & 2 deletions regression-tests/test-results/mixed-bounds-check.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#line 2 "mixed-bounds-check.cpp2"
#include <vector>


//=== Cpp2 type declarations ====================================================
Expand All @@ -12,8 +14,6 @@

#line 1 "mixed-bounds-check.cpp2"

#include <vector>

#line 4 "mixed-bounds-check.cpp2"
[[nodiscard]] auto main() -> int;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <chrono>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,7 +12,8 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-bugfix-for-literal-as-nttp.cpp2"
#include <chrono>

#line 2 "mixed-bugfix-for-literal-as-nttp.cpp2"
auto main() -> int;

//=== Cpp2 function definitions =================================================
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#line 2 "mixed-captures-in-expressions-and-postconditions.cpp2"
#include <algorithm>
#include <vector>


//=== Cpp2 type declarations ====================================================
Expand All @@ -12,9 +15,6 @@

#line 1 "mixed-captures-in-expressions-and-postconditions.cpp2"

#include <algorithm>
#include <vector>

#line 5 "mixed-captures-in-expressions-and-postconditions.cpp2"
[[nodiscard]] auto main() -> int;

Expand Down
7 changes: 4 additions & 3 deletions regression-tests/test-results/mixed-fixed-type-aliases.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <filesystem>
#include <iostream>
#include <typeinfo>


//=== Cpp2 type declarations ====================================================
Expand All @@ -14,9 +17,7 @@ template<typename T> class mytype;
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-fixed-type-aliases.cpp2"
#include <filesystem>
#include <iostream>
#include <typeinfo>
#line 4 "mixed-fixed-type-aliases.cpp2"

namespace my {
using u16 = float;
Expand Down
5 changes: 3 additions & 2 deletions regression-tests/test-results/mixed-forwarding.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
#include <iostream>
#include <utility>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,8 +13,7 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-forwarding.cpp2"
#include <iostream>
#include <utility>
#line 3 "mixed-forwarding.cpp2"

struct X {
int i;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
#include <vector>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,11 +16,6 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-function-expression-and-std-for-each.cpp2"
#include <vector>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>

#line 7 "mixed-function-expression-and-std-for-each.cpp2"
[[nodiscard]] auto main() -> int;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#include <vector>
#include <ranges>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,12 +17,6 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-function-expression-and-std-ranges-for-each-with-capture.cpp2"
#include <vector>
#include <ranges>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>

#line 8 "mixed-function-expression-and-std-ranges-for-each-with-capture.cpp2"
[[nodiscard]] auto main() -> int;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#include <vector>
#include <ranges>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,12 +17,6 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-function-expression-and-std-ranges-for-each.cpp2"
#include <vector>
#include <ranges>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>

#line 8 "mixed-function-expression-and-std-ranges-for-each.cpp2"
[[nodiscard]] auto main() -> int;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#include <vector>
#include <ranges>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,12 +17,6 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-function-expression-with-pointer-capture.cpp2"
#include <vector>
#include <ranges>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>

#line 8 "mixed-function-expression-with-pointer-capture.cpp2"
[[nodiscard]] auto main() -> int;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#include <vector>
#include <ranges>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,12 +17,6 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-function-expression-with-repeated-capture.cpp2"
#include <vector>
#include <ranges>
#include <string>
#include <span>
#include <algorithm>
#include <iostream>

#line 8 "mixed-function-expression-with-repeated-capture.cpp2"
[[nodiscard]] auto main() -> int;
Expand Down
6 changes: 3 additions & 3 deletions regression-tests/test-results/mixed-hello.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#line 2 "mixed-hello.cpp2"
#include <iostream>
#include <string>


//=== Cpp2 type declarations ====================================================
Expand All @@ -12,9 +15,6 @@

#line 1 "mixed-hello.cpp2"

#include <iostream>
#include <string>

#line 5 "mixed-hello.cpp2"
[[nodiscard]] auto name() -> std::string;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <random>
#include <string>
#include <vector>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,9 +14,6 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-initialization-safety-3-contract-violation.cpp2"
#include <random>
#include <string>
#include <vector>

#line 5 "mixed-initialization-safety-3-contract-violation.cpp2"
[[nodiscard]] auto main() -> int;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <random>
#include <string>
#include <vector>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,9 +14,6 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-initialization-safety-3.cpp2"
#include <random>
#include <string>
#include <vector>

#line 5 "mixed-initialization-safety-3.cpp2"
[[nodiscard]] auto main() -> int;
Expand Down
7 changes: 4 additions & 3 deletions regression-tests/test-results/mixed-inspect-templates.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#include <vector>
#include <array>
#include <string>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,9 +14,7 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-inspect-templates.cpp2"
#include <vector>
#include <array>
#include <string>
#line 4 "mixed-inspect-templates.cpp2"

template <typename A, typename B>
struct my_type {};
Expand Down
6 changes: 4 additions & 2 deletions regression-tests/test-results/mixed-inspect-values-2.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#line 2 "mixed-inspect-values-2.cpp2"
#include <iostream>
#include <vector>


//=== Cpp2 type declarations ====================================================
Expand All @@ -12,8 +15,7 @@

#line 1 "mixed-inspect-values-2.cpp2"

#include <iostream>
#include <vector>
#line 4 "mixed-inspect-values-2.cpp2"

constexpr auto less_than = [](int value) {
return [=](auto x) { return x < value;};
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#line 2 "mixed-lifetime-safety-and-null-contracts.cpp2"
#include <iostream>
#include <vector>
#include <string>


//=== Cpp2 type declarations ====================================================
Expand All @@ -12,10 +16,6 @@

#line 1 "mixed-lifetime-safety-and-null-contracts.cpp2"

#include <iostream>
#include <vector>
#include <string>

#line 6 "mixed-lifetime-safety-and-null-contracts.cpp2"
[[nodiscard]] auto main() -> int;
#line 11 "mixed-lifetime-safety-and-null-contracts.cpp2"
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#line 2 "mixed-lifetime-safety-pointer-init-4.cpp2"
#include <iostream>
#include <random>


//=== Cpp2 type declarations ====================================================
Expand All @@ -12,9 +15,6 @@

#line 1 "mixed-lifetime-safety-pointer-init-4.cpp2"

#include <iostream>
#include <random>

#line 5 "mixed-lifetime-safety-pointer-init-4.cpp2"
[[nodiscard]] auto main() -> int;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#line 2 "mixed-multiple-return-values.cpp2"
#include <iostream>
#include <random>
#include <string>


//=== Cpp2 type declarations ====================================================
Expand All @@ -12,9 +16,6 @@

#line 1 "mixed-multiple-return-values.cpp2"

#include <iostream>
#include <random>
#include <string>

struct f_ret { int i; std::string s; };

Expand Down
3 changes: 2 additions & 1 deletion regression-tests/test-results/mixed-out-destruction.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iostream>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,7 +12,7 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-out-destruction.cpp2"
#include <iostream>
#line 2 "mixed-out-destruction.cpp2"

struct X {
inline static int Xnum = 0;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <iostream>


//=== Cpp2 type declarations ====================================================
Expand All @@ -11,7 +12,7 @@
//=== Cpp2 type definitions and function declarations ===========================

#line 1 "mixed-parameter-passing-generic-out.cpp2"
#include <iostream>
#line 2 "mixed-parameter-passing-generic-out.cpp2"

struct X {
X(int) { }
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
#line 2 "mixed-parameter-passing-with-forward.cpp2"
#include <string>
#include <cstdlib>
#include <ctime>


//=== Cpp2 type declarations ====================================================
Expand All @@ -12,10 +16,6 @@

#line 1 "mixed-parameter-passing-with-forward.cpp2"

#include <string>
#include <cstdlib>
#include <ctime>

#line 6 "mixed-parameter-passing-with-forward.cpp2"
auto copy_from([[maybe_unused]] auto unnamed_param_1) -> void;

Expand Down
Loading

3 comments on commit 61dc376

@hackjackhack
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi @hsutter, I'm just a little confused by the formula used to handle binary add a * b_d + b * a_d. Isn't that for multiplication?

@MaxSagebaum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I fixed that in my branch #1366

@MaxSagebaum
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is now merged.

Please sign in to comment.