58 "llvm.OpenCL.sampler.get.resource.id";
65 "kernel_arg_addr_space",
66 "kernel_arg_access_qual",
68 "kernel_arg_base_type",
69 "kernel_arg_type_qual"};
83 return TypeString ==
"image2d_t" || TypeString ==
"image3d_t";
88 return TypeString ==
"sampler_t";
97 if (NumOps != NumKernelArgMDNodes + 1)
100 auto F = mdconst::dyn_extract<Function>(Node->
getOperand(0));
105 size_t ExpectNumArgNodeOps =
F->arg_size() + 1;
116 if (!StringNode || StringNode->
getString() != KernelArgMDNodeNames[i])
126 return cast<MDString>(ArgAQNode->
getOperand(ArgIdx + 1))->getString();
132 return cast<MDString>(ArgTypeNode->
getOperand(ArgIdx + 1))->getString();
149 MD.ArgVector[i].push_back(V[i]);
155 class R600OpenCLImageTypeLoweringPass :
public ModulePass {
161 Type *ImageFormatType;
169 for (
auto &
Use : ImageArg.
uses()) {
179 Value *Replacement =
nullptr;
181 if (Name.
startswith(GetImageResourceIDFunc)) {
183 }
else if (Name.
startswith(GetImageSizeFunc)) {
184 Replacement = &ImageSizeArg;
185 }
else if (Name.
startswith(GetImageFormatFunc)) {
186 Replacement = &ImageFormatArg;
202 for (
const auto &
Use : SamplerArg.
uses()) {
212 Value *Replacement =
nullptr;
214 if (Name == GetSamplerResourceIDFunc) {
220 Inst->replaceAllUsesWith(Replacement);
234 InstsToErase.
clear();
243 if (AccessQual ==
"read_only") {
244 ResourceID = NumReadOnlyImageArgs++;
245 }
else if (AccessQual ==
"write_only") {
246 ResourceID = NumWriteOnlyImageArgs++;
253 Modified |= replaceImageUses(Arg, ResourceID, SizeArg, FormatArg);
257 uint32_t ResourceID = NumSamplerArgs++;
258 Modified |= replaceSamplerUses(Arg, ResourceID);
261 for (
unsigned i = 0; i < InstsToErase.
size(); ++i) {
262 InstsToErase[i]->eraseFromParent();
268 std::tuple<Function *, MDNode *>
276 KernelArgMD NewArgMDs;
282 MDVector ArgMD =
GetArgMD(KernelMDNode, i + 1);
290 ArgMD[2] = ArgMD[3] =
MDString::get(*Context, ImageSizeArgMDType);
295 ArgMD[2] = ArgMD[3] =
MDString::get(*Context, ImageFormatArgMDType);
301 return std::make_tuple(
nullptr,
nullptr);
308 auto NewFArgIt = NewF->arg_begin();
310 auto ArgName =
Arg.getName();
311 NewFArgIt->setName(ArgName);
312 VMap[&
Arg] = &(*NewFArgIt++);
314 (NewFArgIt++)->setName(
Twine(
"__size_") + ArgName);
315 (NewFArgIt++)->setName(
Twine(
"__format_") + ArgName);
328 return std::make_tuple(NewF, NewMDNode);
331 bool transformKernels(
Module &M) {
345 std::tie(NewF, NewMDNode) = addImplicitArgs(F, KernelMDNode);
355 KernelMDNode = NewMDNode;
359 Modified |= replaceImageAndSamplerUses(F, KernelMDNode);
366 R600OpenCLImageTypeLoweringPass() :
ModulePass(ID) {}
368 bool runOnModule(
Module &M)
override {
374 return transformKernels(M);
378 return "R600 OpenCL Image Type Pass";
387 return new R600OpenCLImageTypeLoweringPass();
iterator_range< use_iterator > uses()
This class represents an incoming formal argument to a Function.
MDNode * getOperand(unsigned i) const
static StringRef AccessQualFromMD(MDNode *KernelMDNode, unsigned ArgIdx)
This class represents lattice values for constants.
Type * getParamType(unsigned i) const
Parameter type accessors.
static StringRef GetImageFormatFunc
A Module instance is used to store all the information related to an LLVM module. ...
ModulePass * createR600OpenCLImageTypeLoweringPass()
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
static MDString * get(LLVMContext &Context, StringRef Str)
LLVM_NODISCARD bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
void push_back(const T &Elt)
This class represents a function call, abstracting a target machine's calling convention.
const MDOperand & getOperand(unsigned I) const
This defines the Use class.
static StringRef ImageFormatArgMDType
void setOperand(unsigned I, MDNode *New)
static StringRef KernelsMDNodeName
static StringRef GetSamplerResourceIDFunc
void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, bool ModuleLevelChanges, SmallVectorImpl< ReturnInst *> &Returns, const char *NameSuffix="", ClonedCodeInfo *CodeInfo=nullptr, ValueMapTypeRemapper *TypeMapper=nullptr, ValueMaterializer *Materializer=nullptr)
Clone OldFunc into NewFunc, transforming the old arguments into references to VMap values...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVMContext & getContext() const
Get the global data context.
A Use represents the edge between a Value definition and its users.
static bool IsImageType(StringRef TypeString)
unsigned getNumOperands() const
User * getUser() const LLVM_READONLY
Returns the User that contains this Use.
Class to represent function types.
NamedMDNode * getNamedMetadata(const Twine &Name) const
Return the first NamedMDNode in the module with the specified name.
static StringRef ImageSizeArgMDType
AttributeList getAttributes() const
Return the attribute list for this Function.
LinkageTypes getLinkage() const
void replaceAllUsesWith(Value *V)
Change all uses of this to point to a new Value.
StringRef getString() const
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata *> MDs)
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
const FunctionListType & getFunctionList() const
Get the Module's list of functions (constant).
The instances of the Type class are immutable: once they are created, they are never changed...
This is an important class for using LLVM in a threaded context.
static MDVector GetArgMD(MDNode *KernelMDNode, unsigned OpIdx)
This file contains the declarations for the subclasses of Constant, which represent the different fla...
unsigned getNumParams() const
Return the number of fixed parameters this function type requires.
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static FunctionType * get(Type *Result, ArrayRef< Type *> Params, bool isVarArg)
This static method is the primary way of constructing a FunctionType.
static void PushArgMD(KernelArgMD &MD, const MDVector &V)
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small...
Module.h This file contains the declarations for the Module class.
Type * getReturnType() const
static Constant * get(Type *Ty, uint64_t V, bool isSigned=false)
If Ty is a vector type, return a Constant with a splat of the given value.
FunctionCallee getOrInsertFunction(StringRef Name, FunctionType *T, AttributeList AttributeList)
Look up the specified function in the module symbol table.
static StringRef KernelArgMDNodeNames[]
FunctionType * getFunctionType() const
Returns the FunctionType for me.
void push_back(pointer val)
static const unsigned NumKernelArgMDNodes
static StringRef GetImageResourceIDFunc
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
static IntegerType * getInt32Ty(LLVMContext &C)
StringRef getName() const
Return a constant reference to the value's name.
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
static ArrayType * get(Type *ElementType, uint64_t NumElements)
This static method is the primary way to construct an ArrayType.
LLVM_NODISCARD std::enable_if<!is_simple_type< Y >::value, typename cast_retty< X, const Y >::ret_type >::type dyn_cast(const Y &Val)
static Function * GetFunctionFromMDNode(MDNode *Node)
void eraseFromParent()
eraseFromParent - This method unlinks 'this' from the containing module and deletes it...
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
LLVM Value Representation.
StringRef - Represent a constant reference to a string, i.e.
static bool IsSamplerType(StringRef TypeString)
static StringRef GetImageSizeFunc
unsigned getNumOperands() const
Return number of MDNode operands.
static StringRef ArgTypeFromMD(MDNode *KernelMDNode, unsigned ArgIdx)
iterator_range< arg_iterator > args()