Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
CodeBuilder.h
Go to the documentation of this file.
1#ifndef PROTEUS_FRONTEND_CODE_BUILDER_H
2#define PROTEUS_FRONTEND_CODE_BUILDER_H
3
5#include "proteus/Error.h"
10
11#include <cstdint>
12#include <functional>
13#include <optional>
14#include <string>
15#include <vector>
16
17namespace proteus {
18
21struct LoopHints {
22 bool Unroll = false;
23 std::optional<int> UnrollCount;
24};
25
39
40enum class ScopeKind { FUNCTION, IF, FOR, WHILE };
41
43enum class ArithOp { Add, Sub, Mul, Div, Rem };
44
46enum class CmpOp { EQ, NE, LT, LE, GT, GE };
47
48inline std::string toString(ScopeKind Kind) {
49 switch (Kind) {
51 return "FUNCTION";
52 case ScopeKind::IF:
53 return "IF";
54 case ScopeKind::FOR:
55 return "FOR";
57 return "WHILE";
58 default:
59 reportFatalError("Unsupported Kind " +
60 std::to_string(static_cast<int>(Kind)));
61 }
62}
63
66enum class CodeBuilderKind { LLVM, MLIR };
67
71public:
72 virtual ~CodeBuilder() = default;
73
74 virtual TargetModelType getTargetModel() const = 0;
75 virtual CodeBuilderKind getBackendKind() const = 0;
76
77 // -----------------------------------------------------------------------
78 // Function management.
79 // -----------------------------------------------------------------------
80
84 virtual IRFunction *addFunction(const std::string &Name, IRType RetTy,
85 const std::vector<IRType> &ArgTys,
86 bool IsKernel = false) = 0;
87
89 virtual void setFunctionName(IRFunction *F, const std::string &Name) = 0;
90
92 virtual IRValue *getArg(IRFunction *F, size_t Idx) = 0;
93
95 virtual void beginFunction(IRFunction *F, const char *File, int Line) = 0;
96 virtual void endFunction() = 0;
97
98 // -----------------------------------------------------------------------
99 // Insertion point management.
100 // -----------------------------------------------------------------------
101 virtual void setInsertPointAtEntry() = 0;
102 virtual void clearInsertPoint() = 0;
103
104 // -----------------------------------------------------------------------
105 // Control flow.
106 // -----------------------------------------------------------------------
107 virtual void beginIf(IRValue *Cond, const char *File, int Line) = 0;
108 virtual void endIf() = 0;
109
116 virtual void beginFor(IRValue *IterSlot, IRType IterTy, IRValue *InitVal,
117 IRValue *UpperBoundVal, IRValue *IncVal, bool IsSigned,
118 const char *File, int Line, LoopHints Hints = {}) = 0;
119 virtual void endFor() = 0;
120
123 virtual void beginWhile(std::function<IRValue *()> CondFn, const char *File,
124 int Line) = 0;
125 virtual void endWhile() = 0;
126
127 virtual void createRetVoid() = 0;
128 virtual void createRet(IRValue *V) = 0;
129
130 // -----------------------------------------------------------------------
131 // Arithmetic.
132 // -----------------------------------------------------------------------
133 virtual IRValue *createArith(ArithOp Op, IRValue *LHS, IRValue *RHS,
134 IRType Ty) = 0;
135
136 // -----------------------------------------------------------------------
137 // Atomics.
138 // -----------------------------------------------------------------------
139 virtual IRValue *createAtomicAdd(IRValue *Addr, IRValue *Val) = 0;
140 virtual IRValue *createAtomicSub(IRValue *Addr, IRValue *Val) = 0;
141 virtual IRValue *createAtomicMax(IRValue *Addr, IRValue *Val) = 0;
142 virtual IRValue *createAtomicMin(IRValue *Addr, IRValue *Val) = 0;
143
144 // -----------------------------------------------------------------------
145 // Comparisons.
146 // -----------------------------------------------------------------------
147 virtual IRValue *createCmp(CmpOp Op, IRValue *LHS, IRValue *RHS,
148 IRType Ty) = 0;
149
150 // -----------------------------------------------------------------------
151 // Logical.
152 // -----------------------------------------------------------------------
153 virtual IRValue *createAnd(IRValue *LHS, IRValue *RHS) = 0;
154 virtual IRValue *createOr(IRValue *LHS, IRValue *RHS) = 0;
155 virtual IRValue *createXor(IRValue *LHS, IRValue *RHS) = 0;
156 virtual IRValue *createNot(IRValue *Val) = 0;
157
158 // -----------------------------------------------------------------------
159 // Load/Store.
160 // -----------------------------------------------------------------------
161 virtual IRValue *createLoad(IRType Ty, IRValue *Ptr,
162 const std::string &Name = "") = 0;
163 virtual void createStore(IRValue *Val, IRValue *Ptr) = 0;
164
165 // -----------------------------------------------------------------------
166 // Casts.
167 // -----------------------------------------------------------------------
168 virtual IRValue *createCast(IRValue *V, IRType FromTy, IRType ToTy) = 0;
169 virtual IRValue *createBitCast(IRValue *V, IRType DestTy) = 0;
170 virtual IRValue *createZExt(IRValue *V, IRType DestTy) = 0;
171
172 // -----------------------------------------------------------------------
173 // Constants.
174 // -----------------------------------------------------------------------
175 virtual IRValue *getConstantInt(IRType Ty, uint64_t Val) = 0;
176 virtual IRValue *getConstantFP(IRType Ty, double Val) = 0;
177
178 // -----------------------------------------------------------------------
179 // GEP — semantic element-pointer operations.
180 // -----------------------------------------------------------------------
181 virtual VarAlloc getElementPtr(IRValue *Base, IRType BaseTy, IRValue *Index,
182 IRType ElemTy) = 0;
183 virtual VarAlloc getElementPtr(IRValue *Base, IRType BaseTy, size_t Index,
184 IRType ElemTy) = 0;
185
186 // -----------------------------------------------------------------------
187 // Calls.
188 // -----------------------------------------------------------------------
189 virtual IRValue *createCall(const std::string &FName, IRType RetTy,
190 const std::vector<IRType> &ArgTys,
191 const std::vector<IRValue *> &Args) = 0;
192 virtual IRValue *createCall(const std::string &FName, IRType RetTy) = 0;
193
194 // -----------------------------------------------------------------------
195 // Math intrinsics and GPU builtins.
196 // -----------------------------------------------------------------------
197
199 virtual IRValue *emitIntrinsic(const std::string &Name, IRType RetTy,
200 const std::vector<IRValue *> &Args) = 0;
201
204 virtual IRValue *emitBuiltin(const std::string &Name, IRType RetTy,
205 const std::vector<IRValue *> &Args) = 0;
206
207 // -----------------------------------------------------------------------
208 // Storage-aware load/store.
209 // -----------------------------------------------------------------------
210
212 virtual IRValue *loadScalar(IRValue *Slot, IRType ValueTy) = 0;
214 virtual void storeScalar(IRValue *Slot, IRValue *Val) = 0;
216 virtual IRValue *loadAddress(IRValue *Slot, IRType AllocTy) = 0;
218 virtual void storeAddress(IRValue *Slot, IRValue *Addr) = 0;
220 virtual IRValue *loadFromPointee(IRValue *Slot, IRType AllocTy,
221 IRType ValueTy) = 0;
223 virtual void storeToPointee(IRValue *Slot, IRType AllocTy, IRValue *Val) = 0;
224
225 // -----------------------------------------------------------------------
226 // Alloc factories — return a VarAlloc aggregate.
227 // -----------------------------------------------------------------------
228 virtual VarAlloc allocScalar(const std::string &Name, IRType ValueTy) = 0;
229 virtual VarAlloc allocPointer(const std::string &Name, IRType ElemTy,
230 unsigned AddrSpace = 0) = 0;
231 virtual VarAlloc allocArray(const std::string &Name, AddressSpace AS,
232 IRType ElemTy, size_t NElem) = 0;
233
234 // -----------------------------------------------------------------------
235 // GPU kernel support (CUDA / HIP only).
236 // -----------------------------------------------------------------------
237#if defined(PROTEUS_ENABLE_CUDA) || defined(PROTEUS_ENABLE_HIP)
238 virtual void setLaunchBoundsForKernel(IRFunction *F, int MaxThreadsPerBlock,
239 int MinBlocksPerSM) = 0;
240#endif
241};
242
243} // namespace proteus
244
245#endif // PROTEUS_FRONTEND_CODE_BUILDER_H
char int void ** Args
Definition CompilerInterfaceHost.cpp:23
Definition CodeBuilder.h:70
virtual IRValue * createOr(IRValue *LHS, IRValue *RHS)=0
virtual VarAlloc allocPointer(const std::string &Name, IRType ElemTy, unsigned AddrSpace=0)=0
virtual IRValue * loadFromPointee(IRValue *Slot, IRType AllocTy, IRType ValueTy)=0
Dereference the pointer stored in Slot, then load the pointee.
virtual void endWhile()=0
virtual TargetModelType getTargetModel() const =0
virtual IRValue * createArith(ArithOp Op, IRValue *LHS, IRValue *RHS, IRType Ty)=0
virtual IRValue * emitBuiltin(const std::string &Name, IRType RetTy, const std::vector< IRValue * > &Args)=0
virtual IRValue * createZExt(IRValue *V, IRType DestTy)=0
virtual void createStore(IRValue *Val, IRValue *Ptr)=0
virtual void storeAddress(IRValue *Slot, IRValue *Addr)=0
Store Addr into Slot (pointer alloca).
virtual void setFunctionName(IRFunction *F, const std::string &Name)=0
Rename the function identified by F.
virtual void clearInsertPoint()=0
virtual IRValue * createCall(const std::string &FName, IRType RetTy, const std::vector< IRType > &ArgTys, const std::vector< IRValue * > &Args)=0
virtual IRValue * getConstantFP(IRType Ty, double Val)=0
virtual void setInsertPointAtEntry()=0
virtual IRValue * createAtomicAdd(IRValue *Addr, IRValue *Val)=0
virtual void beginFor(IRValue *IterSlot, IRType IterTy, IRValue *InitVal, IRValue *UpperBoundVal, IRValue *IncVal, bool IsSigned, const char *File, int Line, LoopHints Hints={})=0
virtual CodeBuilderKind getBackendKind() const =0
virtual VarAlloc allocScalar(const std::string &Name, IRType ValueTy)=0
virtual IRValue * createLoad(IRType Ty, IRValue *Ptr, const std::string &Name="")=0
virtual void beginIf(IRValue *Cond, const char *File, int Line)=0
virtual IRValue * createCall(const std::string &FName, IRType RetTy)=0
virtual IRValue * emitIntrinsic(const std::string &Name, IRType RetTy, const std::vector< IRValue * > &Args)=0
Lower a frontend intrinsic name to backend IR.
virtual VarAlloc getElementPtr(IRValue *Base, IRType BaseTy, IRValue *Index, IRType ElemTy)=0
virtual void storeScalar(IRValue *Slot, IRValue *Val)=0
Store Val directly into Slot (scalar alloca).
virtual ~CodeBuilder()=default
virtual VarAlloc getElementPtr(IRValue *Base, IRType BaseTy, size_t Index, IRType ElemTy)=0
virtual IRValue * createCmp(CmpOp Op, IRValue *LHS, IRValue *RHS, IRType Ty)=0
virtual IRFunction * addFunction(const std::string &Name, IRType RetTy, const std::vector< IRType > &ArgTys, bool IsKernel=false)=0
virtual IRValue * createAtomicMax(IRValue *Addr, IRValue *Val)=0
virtual void endFor()=0
virtual IRValue * getArg(IRFunction *F, size_t Idx)=0
Return the Nth argument of F as an IRValue.
virtual IRValue * loadAddress(IRValue *Slot, IRType AllocTy)=0
Load the pointer stored in Slot (pointer alloca).
virtual void beginWhile(std::function< IRValue *()> CondFn, const char *File, int Line)=0
virtual IRValue * loadScalar(IRValue *Slot, IRType ValueTy)=0
Load the value stored directly in Slot (scalar alloca).
virtual IRValue * createAtomicSub(IRValue *Addr, IRValue *Val)=0
virtual IRValue * createCast(IRValue *V, IRType FromTy, IRType ToTy)=0
virtual void endFunction()=0
virtual void beginFunction(IRFunction *F, const char *File, int Line)=0
Set F as the active function and begin IR emission.
virtual IRValue * createNot(IRValue *Val)=0
virtual IRValue * createAtomicMin(IRValue *Addr, IRValue *Val)=0
virtual VarAlloc allocArray(const std::string &Name, AddressSpace AS, IRType ElemTy, size_t NElem)=0
virtual IRValue * createBitCast(IRValue *V, IRType DestTy)=0
virtual void createRetVoid()=0
virtual IRValue * createXor(IRValue *LHS, IRValue *RHS)=0
virtual IRValue * createAnd(IRValue *LHS, IRValue *RHS)=0
virtual void storeToPointee(IRValue *Slot, IRType AllocTy, IRValue *Val)=0
Dereference the pointer stored in Slot, then store Val to it.
virtual void createRet(IRValue *V)=0
virtual IRValue * getConstantInt(IRType Ty, uint64_t Val)=0
virtual void endIf()=0
Definition IRFunction.h:9
Definition IRValue.h:15
Definition MemoryCache.h:27
AddressSpace
Definition AddressSpace.h:6
TargetModelType
Definition TargetModel.h:8
CodeBuilderKind
Definition CodeBuilder.h:66
ArithOp
Semantic arithmetic operation selector.
Definition CodeBuilder.h:43
CmpOp
Semantic comparison operation selector.
Definition CodeBuilder.h:46
void setLaunchBoundsForKernel(Function &F, int MaxThreadsPerSM, int MinBlocksPerSM=0)
Definition CoreLLVMCUDA.h:87
void reportFatalError(const llvm::Twine &Reason, const char *FILE, unsigned Line)
Definition Error.cpp:14
std::string toString(CodegenOption Option)
Definition Config.h:28
ScopeKind
Definition CodeBuilder.h:40
Definition IRType.h:34
Definition CodeBuilder.h:21
bool Unroll
Definition CodeBuilder.h:22
std::optional< int > UnrollCount
Definition CodeBuilder.h:23
Definition CodeBuilder.h:29
unsigned AddrSpace
Address space (relevant for pointers/arrays).
Definition CodeBuilder.h:37
IRType ValueTy
Logical element type (pointee/element for pointer/array).
Definition CodeBuilder.h:33
IRValue * Slot
The alloca (or global base pointer for arrays).
Definition CodeBuilder.h:31
IRType AllocTy
Type of the alloca itself.
Definition CodeBuilder.h:35