summaryrefslogtreecommitdiffstats
path: root/src/gallium/drivers/radeon/AMDILKernelManager.h
blob: d5eb296cbf250b4e0b4004f06fb6eb8e7db2c40d (plain)
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
//===-- AMDILKernelManager.h - TODO: Add brief description -------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//==-----------------------------------------------------------------------===//
// 
// Class that handles the metadata/abi management for the
// ASM printer. Handles the parsing and generation of the metadata
// for each kernel and keeps track of its arguments.
//
//==-----------------------------------------------------------------------===//
#ifndef _AMDILKERNELMANAGER_H_
#define _AMDILKERNELMANAGER_H_
#include "AMDIL.h"
#include "AMDILDevice.h"
#include "llvm/ADT/DenseMap.h"
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/StringMap.h"
#include "llvm/ADT/ValueMap.h"
#include "llvm/CodeGen/MachineBasicBlock.h"
#include "llvm/Function.h"

#include <map>
#include <set>
#include <string>

#define IMAGETYPE_2D 0
#define IMAGETYPE_3D 1
#define RESERVED_LIT_COUNT 6

namespace llvm {
class AMDILGlobalManager;
class AMDILSubtarget;
class AMDILMachineFunctionInfo;
class AMDILTargetMachine;
class AMDILAsmPrinter;
class StructType;
class Value;
class TypeSymbolTable;
class MachineFunction;
class MachineInstr;
class ConstantFP;
class PrintfInfo;


class AMDILKernelManager {
public:
  typedef enum {
    RELEASE_ONLY,
    DEBUG_ONLY,
    ALWAYS
  } ErrorMsgEnum;
  AMDILKernelManager(AMDILTargetMachine *TM, AMDILGlobalManager *GM);
  virtual ~AMDILKernelManager();
  
  /// Clear the state of the KernelManager putting it in its most initial state.
  void clear();
  void setMF(MachineFunction *MF);

  /// Process the specific kernel parsing out the parameter information for the
  /// kernel.
  void processArgMetadata(llvm::raw_ostream &O,
                          uint32_t buf, bool kernel);


  /// Prints the header for the kernel which includes the groupsize declaration
  /// and calculation of the local/group/global id's.
  void printHeader(AMDILAsmPrinter *AsmPrinter, llvm::raw_ostream &O,
                   const std::string &name);

  virtual void printDecls(AMDILAsmPrinter *AsmPrinter, llvm::raw_ostream &O);
  virtual void printGroupSize(llvm::raw_ostream &O);

  /// Copies the data from the runtime setup constant buffers into registers so
  /// that the program can correctly access memory or data that was set by the
  /// host program.
  void printArgCopies(llvm::raw_ostream &O, AMDILAsmPrinter* RegNames);

  /// Prints out the end of the function.
  void printFooter(llvm::raw_ostream &O);
  
  /// Prints out the metadata for the specific function depending if it is a
  /// kernel or not.
  void printMetaData(llvm::raw_ostream &O, uint32_t id, bool isKernel = false);
  
  /// Set bool value on whether to consider the function a kernel or a normal
  /// function.
  void setKernel(bool kernel);

  /// Set the unique ID of the kernel/function.
  void setID(uint32_t id);

  /// Set the name of the kernel/function.
  void setName(const std::string &name);

  /// Flag to specify whether the function is a kernel or not.
  bool isKernel();

  /// Flag that specifies whether this function has a kernel wrapper.
  bool wasKernel();

  void getIntrinsicSetup(AMDILAsmPrinter *AsmPrinter, llvm::raw_ostream &O); 

  // Returns whether a compiler needs to insert a write to memory or not.
  bool useCompilerWrite(const MachineInstr *MI);

  // Set the flag that there exists an image write.
  void setImageWrite();
  void setOutputInst();

  const char *getTypeName(const Type *name, const char * symTab);

  void emitLiterals(llvm::raw_ostream &O);

  // Set the uav id for the specific pointer value.  If value is NULL, then the
  // ID sets the default ID.
  void setUAVID(const Value *value, uint32_t ID);

  // Get the UAV id for the specific pointer value.
  uint32_t getUAVID(const Value *value);

private:

  /// Helper function that prints the actual metadata and should only be called
  /// by printMetaData.
  void printKernelArgs(llvm::raw_ostream &O);
  void printCopyStructPrivate(const StructType *ST,
                              llvm::raw_ostream &O,
                              size_t stackSize,
                              uint32_t Buffer,
                              uint32_t mLitIdx,
                              uint32_t &counter);
  virtual void
  printConstantToRegMapping(AMDILAsmPrinter *RegNames,
                            uint32_t &LII,
                            llvm::raw_ostream &O,
                            uint32_t &counter,
                            uint32_t Buffer,
                            uint32_t n,
                            const char *lit = NULL,
                            uint32_t fcall = 0,
                            bool isImage = false,
                            bool isHWCB = false);
  void updatePtrArg(llvm::Function::const_arg_iterator Ip,
                    int numWriteImages,
                    int raw_uav_buffer,
                    int counter,
                    bool isKernel,
                    const Function *F);
  /// Name of the current kernel.
  std::string mName;
  uint32_t mUniqueID;
  bool mIsKernel;
  bool mWasKernel;
  bool mCompilerWrite;
  /// Flag to specify if an image write has occured or not in order to not add a
  /// compiler specific write if no other writes to memory occured.
  bool mHasImageWrite;
  bool mHasOutputInst;
  
  /// Map from const Value * to UAV ID.
  std::map<const Value *, uint32_t> mValueIDMap;

  AMDILTargetMachine * mTM;
  const AMDILSubtarget * mSTM;
  AMDILGlobalManager * mGM;
  /// This is the global offset of the printf string id's.
  MachineFunction *mMF;
  AMDILMachineFunctionInfo *mMFI;
}; // class AMDILKernelManager

} // llvm namespace
#endif // _AMDILKERNELMANAGER_H_