-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Delay the differentiation process until the end of TU.
Before this patch clad attaches itself as a first consumer and applies AD before code generation. However, that is limited since clang sends every top-level declaration to codegen which limits the amount of flexibility clad has. For example, we have to instantiate all pending templates at every HandleTopLevelDecl calls; we cannot really differentiate virtual functions whose classes have sent their key function to CodeGen; and in general we perform actions which are semantically useful for the end of the translation unit. This patch makes clad a single consumer of clang which dispatches to the others. That's done by delaying all calls to the consumers until the end of the TU where clad can replay the exact sequence of calls to the other consumers as if they were directly connected to the frontend. Fixes #248
- Loading branch information
1 parent
e930a4a
commit a0376e7
Showing
6 changed files
with
419 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
#ifndef CLAD_DIFFERENTIATOR_SINS_H | ||
#define CLAD_DIFFERENTIATOR_SINS_H | ||
|
||
#include <type_traits> | ||
|
||
/// Standard-protected facility allowing access into private members in C++. | ||
/// Use with caution! | ||
// NOLINTBEGIN(cppcoreguidelines-macro-usage) | ||
#define CONCATE_(X, Y) X##Y | ||
#define CONCATE(X, Y) CONCATE_(X, Y) | ||
#define ALLOW_ACCESS(CLASS, MEMBER, ...) \ | ||
template <typename Only, __VA_ARGS__ CLASS::*Member> \ | ||
struct CONCATE(MEMBER, __LINE__) { \ | ||
friend __VA_ARGS__ CLASS::*Access(Only*) { return Member; } \ | ||
}; \ | ||
template <typename> struct Only_##MEMBER; \ | ||
template <> struct Only_##MEMBER<CLASS> { \ | ||
friend __VA_ARGS__ CLASS::*Access(Only_##MEMBER<CLASS>*); \ | ||
}; \ | ||
template struct CONCATE(MEMBER, \ | ||
__LINE__)<Only_##MEMBER<CLASS>, &CLASS::MEMBER> | ||
|
||
#define ACCESS(OBJECT, MEMBER) \ | ||
(OBJECT).*Access((Only_##MEMBER< \ | ||
std::remove_reference<decltype(OBJECT)>::type>*)nullptr) | ||
|
||
// NOLINTEND(cppcoreguidelines-macro-usage) | ||
|
||
#endif // CLAD_DIFFERENTIATOR_SINS_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
// RUN: %cladclang %s -I%S/../../include -oClangConsumers.out \ | ||
// RUN: -fms-compatibility -std=c++14 -fmodules -Xclang \ | ||
// RUN: -print-stats 2>&1 | FileCheck %s | ||
// CHECK-NOT: {{.*error|warning|note:.*}} | ||
|
||
|
||
|
||
#pragma clang module build N | ||
module N {} | ||
#pragma clang module contents | ||
#pragma clang module begin N | ||
struct f { void operator()() const {} }; | ||
template <typename T> auto vtemplate = f{}; | ||
#pragma clang module end | ||
#pragma clang module endbuild | ||
|
||
#pragma clang module import N | ||
|
||
class __single_inheritance IncSingle; | ||
// CHECK: HandleImplicitImportDecl | ||
// CHECK: AssignInheritanceModel | ||
// CHECK: HandleTopLevelDecl | ||
// CHECK: HandleInterestingDecl | ||
// CHECK: HandleCXXStaticMemberVarInstantiation | ||
|
||
int main() { | ||
vtemplate<int>(); | ||
} |
Oops, something went wrong.