Skip to content

Commit

Permalink
Enable differentiation when functions are defined in transparent cont…
Browse files Browse the repository at this point in the history
…exts.

This supports differentiating functions defined places such as in extern C
contexts.
  • Loading branch information
vgvassilev committed Dec 19, 2024
1 parent 1a1b2ce commit 3e50707
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
3 changes: 3 additions & 0 deletions lib/Differentiator/DerivativeBuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,9 @@ static void registerDerivative(FunctionDecl* derivedFD, Sema& semaRef) {
// Consider out-of-line virtual functions.
{
DeclContext* LookupCtx = derivedFD->getDeclContext();
// Find the first non-transparent context to perform the lookup in.
while (LookupCtx->isTransparentContext())
LookupCtx = LookupCtx->getParent();
auto R = LookupCtx->noload_lookup(derivedFD->getDeclName());

for (NamedDecl* I : R) {
Expand Down
9 changes: 8 additions & 1 deletion test/FirstDerivative/BuiltinDerivatives.C
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,10 @@ double f17(double x) {
return 2*y;
}

extern "C" {
double f18(double x) { return x * x; }
}

int main () { //expected-no-diagnostics
float f_result[2];
double d_result[2];
Expand Down Expand Up @@ -417,6 +421,9 @@ int main () { //expected-no-diagnostics
INIT_GRADIENT(f17);

TEST_GRADIENT(f17, /*numOfDerivativeArgs=*/1, -3, &d_result[0]); // CHECK-EXEC: {-2.00}


auto f18_darg0 = clad::differentiate(f18, 0);
printf("Result is = %f\n", f18_darg0.execute(1)); // CHECK-EXEC: Result is = 2

return 0;
}

0 comments on commit 3e50707

Please sign in to comment.