44 "llvm.OpenCL.sampler.get.resource.id";
51 "kernel_arg_addr_space",
52 "kernel_arg_access_qual",
54 "kernel_arg_base_type",
55 "kernel_arg_type_qual"};
69 return TypeString ==
"image2d_t" || TypeString ==
"image3d_t";
74 return TypeString ==
"sampler_t";
91 size_t ExpectNumArgNodeOps =
F->arg_size() + 1;
135 MD.ArgVector[i].push_back(V[i]);
141class R600OpenCLImageTypeLoweringPass :
public ModulePass {
144 LLVMContext *Context;
147 Type *ImageFormatType;
148 SmallVector<Instruction *, 4> InstsToErase;
150 bool replaceImageUses(Argument &ImageArg, uint32_t ResourceID,
151 Argument &ImageSizeArg,
152 Argument &ImageFormatArg) {
155 for (
auto &Use : ImageArg.
uses()) {
165 Value *Replacement =
nullptr;
166 StringRef
Name =
F->getName();
168 Replacement = ConstantInt::get(Int32Type, ResourceID);
170 Replacement = &ImageSizeArg;
172 Replacement = &ImageFormatArg;
177 Inst->replaceAllUsesWith(Replacement);
178 InstsToErase.push_back(Inst);
185 bool replaceSamplerUses(Argument &SamplerArg, uint32_t ResourceID) {
188 for (
const auto &Use : SamplerArg.
uses()) {
198 Value *Replacement =
nullptr;
199 StringRef
Name =
F->getName();
201 Replacement = ConstantInt::get(Int32Type, ResourceID);
206 Inst->replaceAllUsesWith(Replacement);
207 InstsToErase.push_back(Inst);
214 bool replaceImageAndSamplerUses(Function *
F, MDNode *KernelMDNode) {
215 uint32_t NumReadOnlyImageArgs = 0;
216 uint32_t NumWriteOnlyImageArgs = 0;
217 uint32_t NumSamplerArgs = 0;
220 InstsToErase.clear();
221 for (
auto *ArgI =
F->arg_begin(); ArgI !=
F->arg_end(); ++ArgI) {
229 if (AccessQual ==
"read_only") {
230 ResourceID = NumReadOnlyImageArgs++;
231 }
else if (AccessQual ==
"write_only") {
232 ResourceID = NumWriteOnlyImageArgs++;
239 Modified |= replaceImageUses(Arg, ResourceID, SizeArg, FormatArg);
243 uint32_t ResourceID = NumSamplerArgs++;
244 Modified |= replaceSamplerUses(Arg, ResourceID);
247 for (
auto *Inst : InstsToErase)
248 Inst->eraseFromParent();
253 std::tuple<Function *, MDNode *>
254 addImplicitArgs(Function *
F, MDNode *KernelMDNode) {
257 FunctionType *FT =
F->getFunctionType();
261 KernelArgMD NewArgMDs;
265 for (
unsigned i = 0; i < FT->getNumParams(); ++i) {
267 MDVector ArgMD =
GetArgMD(KernelMDNode, i + 1);
286 return std::tuple(
nullptr,
nullptr);
290 auto *NewFT = FunctionType::get(FT->getReturnType(), ArgTypes,
false);
293 auto *NewFArgIt = NewF->arg_begin();
294 for (
auto &Arg:
F->args()) {
296 NewFArgIt->setName(ArgName);
297 VMap[&Arg] = &(*NewFArgIt++);
299 (NewFArgIt++)->setName(Twine(
"__size_") + ArgName);
300 (NewFArgIt++)->setName(Twine(
"__format_") + ArgName);
310 for (
const MDVector &MDV : NewArgMDs.ArgVector)
312 MDNode *NewMDNode =
MDNode::get(*Context, KernelMDArgs);
314 return std::tuple(NewF, NewMDNode);
317 bool transformKernels(
Module &M) {
324 MDNode *KernelMDNode = KernelsMDNode->
getOperand(i);
331 std::tie(NewF, NewMDNode) = addImplicitArgs(
F, KernelMDNode);
334 F->eraseFromParent();
335 M.getFunctionList().push_back(NewF);
341 KernelMDNode = NewMDNode;
345 Modified |= replaceImageAndSamplerUses(
F, KernelMDNode);
352 R600OpenCLImageTypeLoweringPass() : ModulePass(ID) {}
354 bool runOnModule(
Module &M)
override {
355 Context = &
M.getContext();
356 Int32Type = Type::getInt32Ty(
M.getContext());
357 ImageSizeType = ArrayType::get(Int32Type, 3);
358 ImageFormatType = ArrayType::get(Int32Type, 2);
360 return transformKernels(M);
363 StringRef getPassName()
const override {
364 return "R600 OpenCL Image Type Pass";
370char R600OpenCLImageTypeLoweringPass::ID = 0;
373 return new R600OpenCLImageTypeLoweringPass();
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
This file contains the declarations for the subclasses of Constant, which represent the different fla...
Module.h This file contains the declarations for the Module class.
const size_t AbstractManglingParser< Derived, Alloc >::NumOps
Machine Check Debug Module
MachineInstr unsigned OpIdx
static bool IsImageType(StringRef TypeString)
static StringRef KernelsMDNodeName
static StringRef ArgTypeFromMD(MDNode *KernelMDNode, unsigned ArgIdx)
static StringRef GetImageSizeFunc
static StringRef GetSamplerResourceIDFunc
static const unsigned NumKernelArgMDNodes
static Function * GetFunctionFromMDNode(MDNode *Node)
static bool IsSamplerType(StringRef TypeString)
static void PushArgMD(KernelArgMD &MD, const MDVector &V)
static StringRef AccessQualFromMD(MDNode *KernelMDNode, unsigned ArgIdx)
static StringRef GetImageFormatFunc
static StringRef KernelArgMDNodeNames[]
static StringRef ImageSizeArgMDType
static MDVector GetArgMD(MDNode *KernelMDNode, unsigned OpIdx)
static StringRef GetImageResourceIDFunc
static StringRef ImageFormatArgMDType
This file defines the SmallVector class.
unsigned getArgNo() const
Return the index of this formal argument in its containing function.
static Function * Create(FunctionType *Ty, LinkageTypes Linkage, unsigned AddrSpace, const Twine &N="", Module *M=nullptr)
FunctionType * getFunctionType() const
Returns the FunctionType for me.
AttributeList getAttributes() const
Return the attribute list for this Function.
const MDOperand & getOperand(unsigned I) const
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
unsigned getNumOperands() const
Return number of MDNode operands.
LLVM_ABI StringRef getString() const
static LLVM_ABI MDString * get(LLVMContext &Context, StringRef Str)
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
LLVM_ABI void setOperand(unsigned I, MDNode *New)
LLVM_ABI MDNode * getOperand(unsigned i) const
LLVM_ABI unsigned getNumOperands() const
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
StringRef - Represent a constant reference to a string, i.e.
iterator_range< use_iterator > uses()
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
std::enable_if_t< detail::IsValidPointer< X, Y >::value, X * > dyn_extract(Y &&MD)
Extract a Value from Metadata, if any.
NodeAddr< UseNode * > Use
This is an optimization pass for GlobalISel generic memory operations.
FunctionAddr VTableAddr Value
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
auto dyn_cast_or_null(const Y &Val)
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
ModulePass * createR600OpenCLImageTypeLoweringPass()
ValueMap< const Value *, WeakTrackingVH > ValueToValueMapTy
LLVM_ABI void CloneFunctionInto(Function *NewFunc, const Function *OldFunc, ValueToValueMapTy &VMap, CloneFunctionChangeType Changes, 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.
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.