Enzyme main
Loading...
Searching...
No Matches
InstructionBatcher.h
Go to the documentation of this file.
1//===- InstructionBatcher.h
2//--------------------------------------------------===//
3//
4// Enzyme Project
5//
6// Part of the Enzyme Project, under the Apache License v2.0 with LLVM
7// Exceptions. See https://llvm.org/LICENSE.txt for license information.
8// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
9//
10// If using this code in an academic setting, please cite the following:
11// @incollection{enzymeNeurips,
12// title = {Instead of Rewriting Foreign Code for Machine Learning,
13// Automatically Synthesize Fast Gradients},
14// author = {Moses, William S. and Churavy, Valentin},
15// booktitle = {Advances in Neural Information Processing Systems 33},
16// year = {2020},
17// note = {To appear in},
18// }
19//
20//===----------------------------------------------------------------------===//
21//
22// This file contains an instruction visitor InstructionBatcher that generates
23// the batches all LLVM instructions.
24//
25//===----------------------------------------------------------------------===//
26
27#ifndef INSTRUCTION_BATCHER_H_
28#define INSTRUCTION_BATCHER_H_
29
30#include <llvm/Config/llvm-config.h>
31
32#if LLVM_VERSION_MAJOR >= 16
33#define private public
34#include "llvm/Analysis/ScalarEvolution.h"
35#include "llvm/Transforms/Utils/ScalarEvolutionExpander.h"
36#undef private
37#else
38#include "SCEV/ScalarEvolution.h"
39#include "SCEV/ScalarEvolutionExpander.h"
40#endif
41
42#include "llvm/IR/InstVisitor.h"
43#include "llvm/IR/Instruction.h"
44#include "llvm/IR/Value.h"
45
46#include "llvm/ADT/SmallPtrSet.h"
47
48#include "EnzymeLogic.h"
49
50class InstructionBatcher final : public llvm::InstVisitor<InstructionBatcher> {
51public:
54 llvm::Function *oldFunc, llvm::Function *newFunc, unsigned width,
55 llvm::ValueMap<const llvm::Value *, std::vector<llvm::Value *>>
56 &vectorizedValues,
57 llvm::ValueMap<const llvm::Value *, llvm::WeakTrackingVH>
58 &originalToNewFn,
59 llvm::SmallPtrSetImpl<llvm::Value *> &toVectorize, EnzymeLogic &Logic);
60
61private:
62 llvm::ValueMap<const llvm::Value *, std::vector<llvm::Value *>>
63 &vectorizedValues;
64 llvm::ValueMap<const llvm::Value *, llvm::WeakTrackingVH> &originalToNewFn;
65 llvm::SmallPtrSetImpl<llvm::Value *> &toVectorize;
66 unsigned width;
67 EnzymeLogic &Logic;
68
69private:
70 llvm::Value *getNewOperand(unsigned int i, llvm::Value *op);
71
72public:
73 void visitInstruction(llvm::Instruction &inst);
74
75 void visitPHINode(llvm::PHINode &phi);
76
77 void visitSwitchInst(llvm::SwitchInst &inst);
78
79 void visitBranchInst(llvm::BranchInst &branch);
80
81 void visitReturnInst(llvm::ReturnInst &ret);
82
83 void visitCallInst(llvm::CallInst &call);
84};
85
86#endif
void visitReturnInst(llvm::ReturnInst &ret)
void visitSwitchInst(llvm::SwitchInst &inst)
void visitBranchInst(llvm::BranchInst &branch)
void visitInstruction(llvm::Instruction &inst)
void visitPHINode(llvm::PHINode &phi)
InstructionBatcher(llvm::Function *oldFunc, llvm::Function *newFunc, unsigned width, llvm::ValueMap< const llvm::Value *, std::vector< llvm::Value * > > &vectorizedValues, llvm::ValueMap< const llvm::Value *, llvm::WeakTrackingVH > &originalToNewFn, llvm::SmallPtrSetImpl< llvm::Value * > &toVectorize, EnzymeLogic &Logic)
void visitCallInst(llvm::CallInst &call)