-
Notifications
You must be signed in to change notification settings - Fork 125
/
ClangPlugin.cpp
414 lines (359 loc) · 15.8 KB
/
ClangPlugin.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
//--------------------------------------------------------------------*- C++ -*-
// clad - the C++ Clang-based Automatic Differentiator
// version: $Id$
// author: Vassil Vassilev <vvasilev-at-cern.ch>
//------------------------------------------------------------------------------
#include "ClangPlugin.h"
#include "clad/Differentiator/DerivativeBuilder.h"
#include "clad/Differentiator/EstimationModel.h"
#include "clad/Differentiator/Version.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/Attr.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/Version.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Frontend/FrontendPluginRegistry.h"
#include "clang/Frontend/MultiplexConsumer.h"
#include "clang/Lex/LexDiagnostic.h"
#include "clang/Sema/Sema.h"
#include "clang/Sema/Lookup.h"
#include "llvm/Support/Timer.h"
#include "llvm/Support/raw_ostream.h"
#include "clad/Differentiator/Compatibility.h"
#include <algorithm>
using namespace clang;
namespace clad {
namespace plugin {
/// Keeps track if we encountered #pragma clad on/off.
// FIXME: Figure out how to make it a member of CladPlugin.
std::vector<clang::SourceRange> CladEnabledRange;
// Define a pragma handler for #pragma clad
class CladPragmaHandler : public PragmaHandler {
public:
CladPragmaHandler() : PragmaHandler("clad") {}
void HandlePragma(Preprocessor& PP, PragmaIntroducer Introducer,
Token& PragmaTok) override {
// Handle #pragma clad ON/OFF/DEFAULT
if (PragmaTok.isNot(tok::identifier)) {
PP.Diag(PragmaTok, diag::warn_pragma_diagnostic_invalid);
return;
}
#ifndef NDEBUG
IdentifierInfo* II = PragmaTok.getIdentifierInfo();
assert(II->isStr("clad"));
#endif
tok::OnOffSwitch OOS;
if (PP.LexOnOffSwitch(OOS))
return; // failure
SourceLocation TokLoc = PragmaTok.getLocation();
if (OOS == tok::OOS_ON) {
SourceRange R(TokLoc, /*end*/ SourceLocation());
// If a second ON is seen, ignore it if the interval is open.
if (CladEnabledRange.empty() ||
CladEnabledRange.back().getEnd().isValid())
CladEnabledRange.push_back(R);
} else if (!CladEnabledRange.empty()) { // OOS_OFF or OOS_DEFAULT
assert(CladEnabledRange.back().getEnd().isInvalid());
CladEnabledRange.back().setEnd(TokLoc);
}
}
};
CladPlugin::CladPlugin(CompilerInstance& CI, DifferentiationOptions& DO)
: m_CI(CI), m_DO(DO), m_HasRuntime(false) {
#if CLANG_VERSION_MAJOR > 8
FrontendOptions& Opts = CI.getFrontendOpts();
// Find the path to clad.
llvm::StringRef CladSoPath;
for (llvm::StringRef P : Opts.Plugins)
if (llvm::sys::path::stem(P).endswith("clad")) {
CladSoPath = P;
break;
}
// Register clad as a backend pass.
CodeGenOptions& CGOpts = CI.getCodeGenOpts();
CGOpts.PassPlugins.push_back(CladSoPath.str());
#endif // CLANG_VERSION_MAJOR > 8
}
CladPlugin::~CladPlugin() {}
// We cannot use HandleTranslationUnit because codegen already emits code on
// HandleTopLevelDecl calls and makes updateCall with no effect.
bool CladPlugin::HandleTopLevelDecl(DeclGroupRef DGR) {
if (!CheckBuiltins())
return true;
Sema& S = m_CI.getSema();
if (!m_DerivativeBuilder)
m_DerivativeBuilder.reset(new DerivativeBuilder(m_CI.getSema(), *this));
// if HandleTopLevelDecl was called through clad we don't need to process
// it for diff requests
if (m_HandleTopLevelDeclInternal)
return true;
DiffSchedule requests{};
DiffCollector collector(DGR, CladEnabledRange, requests, m_CI.getSema());
if (requests.empty())
return true;
// FIXME: flags have to be set manually since DiffCollector's constructor
// does not have access to m_DO.
if (m_DO.EnableTBRAnalysis)
for (DiffRequest& request : requests)
request.EnableTBRAnalysis = true;
// FIXME: Remove the PerformPendingInstantiations altogether. We should
// somehow make the relevant functions referenced.
// Instantiate all pending for instantiations templates, because we will
// need the full bodies to produce derivatives.
// FIXME: Confirm if we really need `m_PendingInstantiationsInFlight`?
if (!m_PendingInstantiationsInFlight) {
m_PendingInstantiationsInFlight = true;
S.PerformPendingInstantiations();
m_PendingInstantiationsInFlight = false;
}
for (DiffRequest& request : requests)
ProcessDiffRequest(request);
return true; // Happiness
}
void CladPlugin::ProcessTopLevelDecl(Decl* D) {
m_HandleTopLevelDeclInternal = true;
m_CI.getASTConsumer().HandleTopLevelDecl(DeclGroupRef(D));
m_HandleTopLevelDeclInternal = false;
}
FunctionDecl* CladPlugin::ProcessDiffRequest(DiffRequest& request) {
Sema& S = m_CI.getSema();
// Required due to custom derivatives function templates that might be
// used in the function that we need to derive.
S.PerformPendingInstantiations();
if (request.Function->getDefinition())
request.Function = request.Function->getDefinition();
request.UpdateDiffParamsInfo(m_CI.getSema());
const FunctionDecl* FD = request.Function;
// set up printing policy
clang::LangOptions LangOpts;
LangOpts.CPlusPlus = true;
clang::PrintingPolicy Policy(LangOpts);
Policy.Bool = true;
// if enabled, print source code of the original functions
if (m_DO.DumpSourceFn) {
FD->print(llvm::outs(), Policy);
}
// if enabled, print ASTs of the original functions
if (m_DO.DumpSourceFnAST) {
FD->dumpColor();
}
// if enabled, load the dynamic library input from user to use
// as a custom estimation model.
if (m_DO.CustomEstimationModel) {
std::string Err;
if (llvm::sys::DynamicLibrary::
LoadLibraryPermanently(m_DO.CustomModelName.c_str(), &Err)) {
auto& SemaInst = m_CI.getSema();
unsigned diagID = SemaInst.Diags.getCustomDiagID(
DiagnosticsEngine::Error, "Failed to load '%0', %1. Aborting.");
clang::Sema::SemaDiagnosticBuilder stream = SemaInst.Diag(noLoc,
diagID);
stream << m_DO.CustomModelName << Err;
return nullptr;
}
for (auto it = ErrorEstimationModelRegistry::begin(),
ie = ErrorEstimationModelRegistry::end();
it != ie; ++it) {
auto estimationPlugin = it->instantiate();
m_DerivativeBuilder->AddErrorEstimationModel(
estimationPlugin->InstantiateCustomModel(*m_DerivativeBuilder));
}
}
// If enabled, set the proper fields in derivative builder.
if (m_DO.PrintNumDiffErrorInfo) {
m_DerivativeBuilder->setNumDiffErrDiag(true);
}
FunctionDecl* DerivativeDecl = nullptr;
bool alreadyDerived = false;
FunctionDecl* OverloadedDerivativeDecl = nullptr;
{
// FIXME: Move the timing inside the DerivativeBuilder. This would
// require to pass in the DifferentiationOptions in the DiffPlan.
// derive the collected functions
#if CLANG_VERSION_MAJOR > 11
bool WantTiming =
getenv("LIBCLAD_TIMING") || m_CI.getCodeGenOpts().TimePasses;
#else
bool WantTiming =
getenv("LIBCLAD_TIMING") || m_CI.getFrontendOpts().ShowTimers;
#endif
auto DFI = m_DFC.Find(request);
if (DFI.IsValid()) {
DerivativeDecl = DFI.DerivedFn();
OverloadedDerivativeDecl = DFI.OverloadedDerivedFn();
alreadyDerived = true;
} else {
// Only time the function when it is first encountered
if (WantTiming)
m_CTG.StartNewTimer("Timer for clad func",
request.BaseFunctionName);
auto deriveResult = m_DerivativeBuilder->Derive(request);
DerivativeDecl = deriveResult.derivative;
OverloadedDerivativeDecl = deriveResult.overload;
if (WantTiming)
m_CTG.StopTimer();
}
}
if (DerivativeDecl) {
if (!alreadyDerived) {
m_DFC.Add(
DerivedFnInfo(request, DerivativeDecl, OverloadedDerivativeDecl));
// if enabled, print source code of the derived functions
if (m_DO.DumpDerivedFn) {
DerivativeDecl->print(llvm::outs(), Policy);
}
// if enabled, print ASTs of the derived functions
if (m_DO.DumpDerivedAST) {
DerivativeDecl->dumpColor();
}
// if enabled, print the derivatives in a file.
if (m_DO.GenerateSourceFile) {
std::error_code err;
llvm::raw_fd_ostream f("Derivatives.cpp", err,
CLAD_COMPAT_llvm_sys_fs_Append);
DerivativeDecl->print(f, Policy);
f.flush();
}
S.MarkFunctionReferenced(SourceLocation(), DerivativeDecl);
if (OverloadedDerivativeDecl)
S.MarkFunctionReferenced(SourceLocation(),
OverloadedDerivativeDecl);
// We ideally should not call `HandleTopLevelDecl` for declarations
// inside a namespace. After parsing a namespace that is defined
// directly in translation unit context , clang calls
// `BackendConsumer::HandleTopLevelDecl`.
// `BackendConsumer::HandleTopLevelDecl` emits LLVM IR of each
// declaration inside the namespace using CodeGen. We need to manually
// call `HandleTopLevelDecl` for each new declaration added to a
// namespace because `HandleTopLevelDecl` has already been called for
// a namespace by Clang when the namespace is parsed.
// Call CodeGen only if the produced Decl is a top-most
// decl or is contained in a namespace decl.
DeclContext* derivativeDC = DerivativeDecl->getDeclContext();
bool isTUorND =
derivativeDC->isTranslationUnit() || derivativeDC->isNamespace();
if (isTUorND) {
ProcessTopLevelDecl(DerivativeDecl);
if (OverloadedDerivativeDecl)
ProcessTopLevelDecl(OverloadedDerivativeDecl);
}
}
bool lastDerivativeOrder = (request.CurrentDerivativeOrder ==
request.RequestedDerivativeOrder);
// If this is the last required derivative order, replace the function
// inside a call to clad::differentiate/gradient with its derivative.
if (request.CallUpdateRequired && lastDerivativeOrder)
request.updateCall(DerivativeDecl, OverloadedDerivativeDecl,
m_CI.getSema());
// Last requested order was computed, return the result.
if (lastDerivativeOrder)
return DerivativeDecl;
// If higher order derivatives are required, proceed to compute them
// recursively.
request.Function = DerivativeDecl;
request.CurrentDerivativeOrder += 1;
return ProcessDiffRequest(request);
}
return nullptr;
}
bool CladPlugin::CheckBuiltins() {
// If we have included "clad/Differentiator/Differentiator.h" return.
if (m_HasRuntime)
return true;
ASTContext& C = m_CI.getASTContext();
// The plugin has a lot of different ways to be compiled: in-tree,
// out-of-tree and hybrid. When we pick up the wrong header files we
// usually see a problem with C.Idents not being properly initialized.
// This assert tries to catch such situations heuristically.
assert(&C.Idents == &m_CI.getPreprocessor().getIdentifierTable()
&& "Miscompiled?");
// FIXME: Use `utils::LookupNSD` instead.
DeclarationName Name = &C.Idents.get("clad");
Sema &SemaR = m_CI.getSema();
LookupResult R(SemaR, Name, SourceLocation(), Sema::LookupNamespaceName,
Sema::ForVisibleRedeclaration);
SemaR.LookupQualifiedName(R, C.getTranslationUnitDecl(),
/*allowBuiltinCreation*/ false);
m_HasRuntime = !R.empty();
return m_HasRuntime;
}
} // end namespace plugin
clad::CladTimerGroup::CladTimerGroup()
: m_Tg("Timers for Clad Funcs", "Timers for Clad Funcs") {}
void clad::CladTimerGroup::StartNewTimer(llvm::StringRef TimerName,
llvm::StringRef TimerDesc) {
std::unique_ptr<llvm::Timer> tm(
new llvm::Timer(TimerName, TimerDesc, m_Tg));
m_Timers.push_back(std::move(tm));
m_Timers.back()->startTimer();
}
void clad::CladTimerGroup::StopTimer() {
m_Timers.back()->stopTimer();
if (m_Timers.size() != 1)
m_Timers.pop_back();
}
// Routine to check clang version at runtime against the clang version for
// which clad was built.
bool checkClangVersion() {
std::string runtimeVersion = clang::getClangFullCPPVersion();
std::string builtVersion = CLANG_MAJOR_VERSION;
if (runtimeVersion.find(builtVersion) == std::string::npos)
return false;
else
return true;
}
void DerivedFnCollector::Add(const DerivedFnInfo& DFI) {
assert(!AlreadyExists(DFI) &&
"We are generating same derivative more than once, or calling "
"`DerivedFnCollector::Add` more than once for the same derivative "
". Ideally, we shouldn't do either.");
m_DerivedFnInfoCollection[DFI.OriginalFn()].push_back(DFI);
}
bool DerivedFnCollector::AlreadyExists(const DerivedFnInfo& DFI) const {
auto subCollectionIt = m_DerivedFnInfoCollection.find(DFI.OriginalFn());
if (subCollectionIt == m_DerivedFnInfoCollection.end())
return false;
auto& subCollection = subCollectionIt->second;
auto it = std::find_if(subCollection.begin(), subCollection.end(),
[&DFI](const DerivedFnInfo& info) {
return DerivedFnInfo::
RepresentsSameDerivative(DFI, info);
});
return it != subCollection.end();
}
DerivedFnInfo DerivedFnCollector::Find(const DiffRequest& request) const {
auto subCollectionIt = m_DerivedFnInfoCollection.find(request.Function);
if (subCollectionIt == m_DerivedFnInfoCollection.end())
return DerivedFnInfo();
auto& subCollection = subCollectionIt->second;
auto it = std::find_if(subCollection.begin(), subCollection.end(),
[&request](DerivedFnInfo DFI) {
return DFI.SatisfiesRequest(request);
});
if (it == subCollection.end())
return DerivedFnInfo();
return *it;
}
} // end namespace clad
// Attach the frontend plugin.
using namespace clad::plugin;
// register the PluginASTAction in the registry.
static clang::FrontendPluginRegistry::Add<Action<CladPlugin> >
X("clad", "Produces derivatives or arbitrary functions");
static PragmaHandlerRegistry::Add<CladPragmaHandler>
Y("clad", "Clad pragma directives handler.");
#include "clang/Basic/Version.h" // for CLANG_VERSION_MAJOR
#if CLANG_VERSION_MAJOR > 8
// Attach the backend plugin.
#include "ClangBackendPlugin.h"
#define BACKEND_PLUGIN_NAME "CladBackendPlugin"
// FIXME: Add a proper versioning that's based on CLANG_VERSION_STRING and
// a similar approach for clad (see Version.cpp and VERSION).
#define BACKEND_PLUGIN_VERSION "FIXME"
extern "C" ::llvm::PassPluginLibraryInfo LLVM_ATTRIBUTE_WEAK
llvmGetPassPluginInfo() {
return {LLVM_PLUGIN_API_VERSION, BACKEND_PLUGIN_NAME, BACKEND_PLUGIN_VERSION,
clad::ClangBackendPluginPass::registerCallbacks};
}
#endif // CLANG_VERSION_MAJOR