Proteus
Programmable JIT compilation and optimization for C/C++ using LLVM
Loading...
Searching...
No Matches
MLIRCodeBuilder.h
Go to the documentation of this file.
1#ifndef PROTEUS_FRONTEND_MLIR_CODE_BUILDER_H
2#define PROTEUS_FRONTEND_MLIR_CODE_BUILDER_H
3
5
6#include <memory>
7#include <string>
8#include <vector>
9
10namespace llvm {
11class LLVMContext;
12class Module;
13class raw_ostream;
14} // namespace llvm
15
16namespace proteus {
17
22public:
24 ~MLIRCodeBuilder() override;
25
28
29 TargetModelType getTargetModel() const override { return TargetModel; }
32 }
33
35 void print();
36
39 void printLLVMIR(llvm::raw_ostream &OS);
40
45 std::unique_ptr<llvm::LLVMContext> takeContext();
46 std::unique_ptr<llvm::Module> takeModule();
47
51 void setDeviceArch(const std::string &Arch);
52
53 IRFunction *addFunction(const std::string &Name, IRType RetTy,
54 const std::vector<IRType> &ArgTys,
55 bool IsKernel = false) override;
56 void setFunctionName(IRFunction *F, const std::string &Name) override;
57 IRValue *getArg(IRFunction *F, size_t Idx) override;
58 void beginFunction(IRFunction *F, const char *File, int Line) override;
59 void endFunction() override;
60 void setInsertPointAtEntry() override;
61 void clearInsertPoint() override;
62 void createRetVoid() override;
63 void createRet(IRValue *V) override;
65 IRType Ty) override;
66 IRValue *createCast(IRValue *V, IRType FromTy, IRType ToTy) override;
67 IRValue *getConstantInt(IRType Ty, uint64_t Val) override;
68 IRValue *getConstantFP(IRType Ty, double Val) override;
69 IRValue *loadScalar(IRValue *Slot, IRType ValueTy) override;
70 void storeScalar(IRValue *Slot, IRValue *Val) override;
71 VarAlloc allocScalar(const std::string &Name, IRType ValueTy) override;
72 void beginIf(IRValue *Cond, const char *File, int Line) override;
73 void endIf() override;
74 void beginFor(IRValue *IterSlot, IRType IterTy, IRValue *InitVal,
75 IRValue *UpperBoundVal, IRValue *IncVal, bool IsSigned,
76 const char *File, int Line, LoopHints Hints = {}) override;
77 void endFor() override;
78 void beginWhile(std::function<IRValue *()> CondFn, const char *File,
79 int Line) override;
80 void endWhile() override;
81 IRValue *createAtomicAdd(IRValue *Addr, IRValue *Val) override;
82 IRValue *createAtomicSub(IRValue *Addr, IRValue *Val) override;
83 IRValue *createAtomicMax(IRValue *Addr, IRValue *Val) override;
84 IRValue *createAtomicMin(IRValue *Addr, IRValue *Val) override;
85 IRValue *createCmp(CmpOp Op, IRValue *LHS, IRValue *RHS, IRType Ty) override;
86 IRValue *createAnd(IRValue *LHS, IRValue *RHS) override;
87 IRValue *createOr(IRValue *LHS, IRValue *RHS) override;
88 IRValue *createXor(IRValue *LHS, IRValue *RHS) override;
89 IRValue *createNot(IRValue *Val) override;
90 IRValue *createLoad(IRType Ty, IRValue *Ptr,
91 const std::string &Name = "") override;
92 void createStore(IRValue *Val, IRValue *Ptr) override;
93 IRValue *createBitCast(IRValue *V, IRType DestTy) override;
94 IRValue *createZExt(IRValue *V, IRType DestTy) override;
95 VarAlloc getElementPtr(IRValue *Base, IRType BaseTy, IRValue *Index,
96 IRType ElemTy) override;
97 VarAlloc getElementPtr(IRValue *Base, IRType BaseTy, size_t Index,
98 IRType ElemTy) override;
99 IRValue *createCall(const std::string &FName, IRType RetTy,
100 const std::vector<IRType> &ArgTys,
101 const std::vector<IRValue *> &Args) override;
102 IRValue *createCall(const std::string &FName, IRType RetTy) override;
103 IRValue *emitIntrinsic(const std::string &Name, IRType RetTy,
104 const std::vector<IRValue *> &Args) override;
105 IRValue *emitBuiltin(const std::string &Name, IRType RetTy,
106 const std::vector<IRValue *> &Args) override;
107 IRValue *loadAddress(IRValue *Slot, IRType AllocTy) override;
108 void storeAddress(IRValue *Slot, IRValue *Addr) override;
109 IRValue *loadFromPointee(IRValue *Slot, IRType AllocTy,
110 IRType ValueTy) override;
111 void storeToPointee(IRValue *Slot, IRType AllocTy, IRValue *Val) override;
112 VarAlloc allocPointer(const std::string &Name, IRType ElemTy,
113 unsigned AddrSpace = 0) override;
114 VarAlloc allocArray(const std::string &Name, AddressSpace AS, IRType ElemTy,
115 size_t NElem) override;
116
117#if defined(PROTEUS_ENABLE_CUDA) || defined(PROTEUS_ENABLE_HIP)
118 void setLaunchBoundsForKernel(IRFunction *F, int MaxThreadsPerBlock,
119 int MinBlocksPerSM) override;
120#endif
121
122private:
123 void ensureLoweredToLLVM(int OptLevel = 3,
124 const std::string &TargetTriple = "",
125 const std::string &Features = "");
126
127 struct Impl;
128 std::unique_ptr<Impl> PImpl;
129 TargetModelType TargetModel;
130};
131
132} // namespace proteus
133
134#endif // PROTEUS_FRONTEND_MLIR_CODE_BUILDER_H
char int void ** Args
Definition CompilerInterfaceHost.cpp:23
Definition CodeBuilder.h:70
Definition IRFunction.h:9
Definition IRValue.h:15
Definition MLIRCodeBuilder.h:21
void setInsertPointAtEntry() override
Definition MLIRCodeBuilder.cpp:526
IRValue * createLoad(IRType Ty, IRValue *Ptr, const std::string &Name="") override
Definition MLIRCodeBuilder.cpp:961
std::unique_ptr< llvm::Module > takeModule()
Definition MLIRCodeBuilder.cpp:329
void printLLVMIR(llvm::raw_ostream &OS)
Definition MLIRCodeBuilder.cpp:299
void createRet(IRValue *V) override
Definition MLIRCodeBuilder.cpp:552
void storeScalar(IRValue *Slot, IRValue *Val) override
Store Val directly into Slot (scalar alloca).
Definition MLIRCodeBuilder.cpp:713
IRValue * createCall(const std::string &FName, IRType RetTy, const std::vector< IRType > &ArgTys, const std::vector< IRValue * > &Args) override
Definition MLIRCodeBuilder.cpp:1135
std::unique_ptr< llvm::LLVMContext > takeContext()
Definition MLIRCodeBuilder.cpp:324
void beginIf(IRValue *Cond, const char *File, int Line) override
Definition MLIRCodeBuilder.cpp:747
void beginFor(IRValue *IterSlot, IRType IterTy, IRValue *InitVal, IRValue *UpperBoundVal, IRValue *IncVal, bool IsSigned, const char *File, int Line, LoopHints Hints={}) override
Definition MLIRCodeBuilder.cpp:776
void storeToPointee(IRValue *Slot, IRType AllocTy, IRValue *Val) override
Dereference the pointer stored in Slot, then store Val to it.
Definition MLIRCodeBuilder.cpp:1676
IRValue * createAnd(IRValue *LHS, IRValue *RHS) override
Definition MLIRCodeBuilder.cpp:928
IRValue * getConstantInt(IRType Ty, uint64_t Val) override
Definition MLIRCodeBuilder.cpp:679
IRValue * getConstantFP(IRType Ty, double Val) override
Definition MLIRCodeBuilder.cpp:687
IRValue * createXor(IRValue *LHS, IRValue *RHS) override
Definition MLIRCodeBuilder.cpp:942
IRValue * createAtomicMax(IRValue *Addr, IRValue *Val) override
Definition MLIRCodeBuilder.cpp:1576
void setDeviceArch(const std::string &Arch)
Definition MLIRCodeBuilder.cpp:334
void endWhile() override
Definition MLIRCodeBuilder.cpp:857
IRValue * createArith(ArithOp Op, IRValue *LHS, IRValue *RHS, IRType Ty) override
Definition MLIRCodeBuilder.cpp:567
IRValue * emitIntrinsic(const std::string &Name, IRType RetTy, const std::vector< IRValue * > &Args) override
Lower a frontend intrinsic name to backend IR.
Definition MLIRCodeBuilder.cpp:1202
VarAlloc allocScalar(const std::string &Name, IRType ValueTy) override
Definition MLIRCodeBuilder.cpp:721
IRValue * createAtomicMin(IRValue *Addr, IRValue *Val) override
Definition MLIRCodeBuilder.cpp:1614
IRValue * loadScalar(IRValue *Slot, IRType ValueTy) override
Load the value stored directly in Slot (scalar alloca).
Definition MLIRCodeBuilder.cpp:704
IRValue * createOr(IRValue *LHS, IRValue *RHS) override
Definition MLIRCodeBuilder.cpp:935
IRValue * createAtomicAdd(IRValue *Addr, IRValue *Val) override
Definition MLIRCodeBuilder.cpp:1506
void clearInsertPoint() override
Definition MLIRCodeBuilder.cpp:535
void print()
Dump the MLIR module to llvm::outs().
Definition MLIRCodeBuilder.cpp:297
MLIRCodeBuilder(const MLIRCodeBuilder &)=delete
void setFunctionName(IRFunction *F, const std::string &Name) override
Rename the function identified by F.
Definition MLIRCodeBuilder.cpp:450
void endIf() override
Definition MLIRCodeBuilder.cpp:765
CodeBuilderKind getBackendKind() const override
Definition MLIRCodeBuilder.h:30
void beginFunction(IRFunction *F, const char *File, int Line) override
Set F as the active function and begin IR emission.
Definition MLIRCodeBuilder.cpp:491
IRValue * createZExt(IRValue *V, IRType DestTy) override
Definition MLIRCodeBuilder.cpp:1064
VarAlloc getElementPtr(IRValue *Base, IRType BaseTy, IRValue *Index, IRType ElemTy) override
Definition MLIRCodeBuilder.cpp:1102
IRValue * createCast(IRValue *V, IRType FromTy, IRType ToTy) override
Definition MLIRCodeBuilder.cpp:627
IRValue * createBitCast(IRValue *V, IRType DestTy) override
Definition MLIRCodeBuilder.cpp:1042
IRFunction * addFunction(const std::string &Name, IRType RetTy, const std::vector< IRType > &ArgTys, bool IsKernel=false) override
Definition MLIRCodeBuilder.cpp:356
VarAlloc allocPointer(const std::string &Name, IRType ElemTy, unsigned AddrSpace=0) override
Definition MLIRCodeBuilder.cpp:1694
IRValue * emitBuiltin(const std::string &Name, IRType RetTy, const std::vector< IRValue * > &Args) override
Definition MLIRCodeBuilder.cpp:1284
void endFunction() override
Definition MLIRCodeBuilder.cpp:506
void createRetVoid() override
Definition MLIRCodeBuilder.cpp:544
IRValue * getArg(IRFunction *F, size_t Idx) override
Return the Nth argument of F as an IRValue.
Definition MLIRCodeBuilder.cpp:469
void createStore(IRValue *Val, IRValue *Ptr) override
Definition MLIRCodeBuilder.cpp:1004
VarAlloc allocArray(const std::string &Name, AddressSpace AS, IRType ElemTy, size_t NElem) override
Definition MLIRCodeBuilder.cpp:1710
void endFor() override
Definition MLIRCodeBuilder.cpp:816
IRValue * createAtomicSub(IRValue *Addr, IRValue *Val) override
Definition MLIRCodeBuilder.cpp:1541
IRValue * createCmp(CmpOp Op, IRValue *LHS, IRValue *RHS, IRType Ty) override
Definition MLIRCodeBuilder.cpp:867
MLIRCodeBuilder & operator=(const MLIRCodeBuilder &)=delete
IRValue * loadAddress(IRValue *Slot, IRType AllocTy) override
Load the pointer stored in Slot (pointer alloca).
Definition MLIRCodeBuilder.cpp:1444
void storeAddress(IRValue *Slot, IRValue *Addr) override
Store Addr into Slot (pointer alloca).
Definition MLIRCodeBuilder.cpp:1458
void beginWhile(std::function< IRValue *()> CondFn, const char *File, int Line) override
Definition MLIRCodeBuilder.cpp:826
IRValue * loadFromPointee(IRValue *Slot, IRType AllocTy, IRType ValueTy) override
Dereference the pointer stored in Slot, then load the pointee.
Definition MLIRCodeBuilder.cpp:1652
TargetModelType getTargetModel() const override
Definition MLIRCodeBuilder.h:29
IRValue * createNot(IRValue *Val) override
Definition MLIRCodeBuilder.cpp:949
Definition CompiledLibrary.h:7
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
Definition IRType.h:34
Definition CodeBuilder.h:21
Definition CodeBuilder.h:29