Enzyme main
Loading...
Searching...
No Matches
PreserveNVVM.h
Go to the documentation of this file.
1//===- PreserveNVVM.h - Mark NVVM attributes for preservation. -------===//
2//
3// Enzyme Project
4//
5// Part of the Enzyme Project, under the Apache License v2.0 with LLVM
6// Exceptions. See https://llvm.org/LICENSE.txt for license information.
7// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
8//
9// If using this code in an academic setting, please cite the following:
10// @incollection{enzymeNeurips,
11// title = {Instead of Rewriting Foreign Code for Machine Learning,
12// Automatically Synthesize Fast Gradients},
13// author = {Moses, William S. and Churavy, Valentin},
14// booktitle = {Advances in Neural Information Processing Systems 33},
15// year = {2020},
16// note = {To appear in},
17// }
18//
19//===----------------------------------------------------------------------===//
20//
21// This file contains createPreserveNVVM, a transformation pass that marks
22// calls to __nv_* functions, marking them as noinline as implementing the llvm
23// intrinsic.
24//
25//===----------------------------------------------------------------------===//
26
27#ifndef ENZYME_PRESERVE_NVVM_H
28#define ENZYME_PRESERVE_NVVM_H
29
30#include "PassUtils.h"
31#include "llvm/IR/PassManager.h"
32
33namespace llvm {
34class ModulePass;
35class FunctionPass;
36} // namespace llvm
37
38llvm::ModulePass *createPreserveNVVMPass(bool Begin);
39llvm::FunctionPass *createPreserveNVVMFnPass(bool Begin);
40
41class PreserveNVVMNewPM final : public PassParent<PreserveNVVMNewPM> {
43
44private:
45 bool Begin;
46 // Whether a function named as one half of a custom derivative rule is kept
47 // external and uninlined so the rule can still be resolved later. A pipeline
48 // that does not implement custom rules -- the MLIR one -- has nothing to
49 // resolve them with, and holding the rule's functions in place only keeps
50 // dead code, and the kernels it launches, alive. The globals are consumed
51 // either way.
52 bool PreserveCustomRuleLinkage;
53 // Only meaningful with !Begin: the end of an LTO pre-link pipeline that
54 // deferred differentiation to the post-link run. The functions Begin made
55 // external get their original linkage back, so the bitcode does not export a
56 // strong copy of every static or inline function a rule names, and are kept
57 // alive through llvm.compiler.used instead. Their inlining attributes stay as
58 // Begin left them; the post-link end pass restores those and drops the
59 // llvm.compiler.used entries.
60 bool LTOPreLink;
61 static llvm::AnalysisKey Key;
62
63public:
64 using Result = llvm::PreservedAnalyses;
65 PreserveNVVMNewPM(bool Begin, bool PreserveCustomRuleLinkage = true,
66 bool LTOPreLink = false)
67 : Begin(Begin), PreserveCustomRuleLinkage(PreserveCustomRuleLinkage),
68 LTOPreLink(LTOPreLink) {}
69
70 Result run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM);
71
72 static bool isRequired() { return true; }
73};
74
75#endif // ENZYME_PRESERVE_NVVM_H
llvm::ModulePass * createPreserveNVVMPass(bool Begin)
llvm::FunctionPass * createPreserveNVVMFnPass(bool Begin)
llvm::PreservedAnalyses Result
PreserveNVVMNewPM(bool Begin, bool PreserveCustomRuleLinkage=true, bool LTOPreLink=false)
Result run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM)
static bool isRequired()