File: Image3dToImage2darray.cpp

package info (click to toggle)
intel-graphics-compiler2 2.16.0-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 106,644 kB
  • sloc: cpp: 805,640; lisp: 287,672; ansic: 16,414; python: 3,952; yacc: 2,588; lex: 1,666; pascal: 313; sh: 186; makefile: 35
file content (155 lines) | stat: -rw-r--r-- 5,336 bytes parent folder | download
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
/*========================== begin_copyright_notice ============================

Copyright (C) 2017-2023 Intel Corporation

SPDX-License-Identifier: MIT

============================= end_copyright_notice ===========================*/

#include "Compiler/Optimizer/OpenCLPasses/Image3dToImage2darray/Image3dToImage2darray.hpp"
#include "Compiler/IGCPassSupport.h"
#include "GenISAIntrinsics/GenIntrinsicInst.h"
#include "Compiler/MetaDataUtilsWrapper.h"
#include "Compiler/Optimizer/OCLBIUtils.h"

#include "common/LLVMWarningsPush.hpp"
#include <llvm/Pass.h>
#include "common/LLVMWarningsPop.hpp"

using namespace llvm;
using namespace IGC;
using namespace IGC::IGCMD;

#define PASS_FLAG "igc-3d-to-2darray"
#define PASS_DESCRIPTION "Converts 3d images access to 2d array image accesses where possible"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(Image3dToImage2darray, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(Image3dToImage2darray, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)

char Image3dToImage2darray::ID = 0;

Image3dToImage2darray::Image3dToImage2darray()
    : FunctionPass(ID), m_Changed(false), m_MetadataUtils(nullptr), m_modMD(nullptr) {
  initializeImage3dToImage2darrayPass(*PassRegistry::getPassRegistry());
}

bool Image3dToImage2darray::createImageAnnotations(GenIntrinsicInst *pCall, unsigned imageIdx,
                                                   const MetaDataUtils *pMdUtils, const ModuleMetaData *modMD,
                                                   const Value *pCoord) {
  uint argNum = 0;
  auto *pFunc = pCall->getParent()->getParent();
  FunctionInfoMetaDataHandle funcInfoMD = pMdUtils->getFunctionsInfoItem(pFunc);
  auto *pImgOp = pCall->getArgOperand(imageIdx);

  // Find the arg number of the image so we can look up its ArgInfoList metadata.
  if (pImgOp->getType()->isPointerTy()) {
    auto *pImg = ValueTracker::track(pCall, imageIdx, pMdUtils, modMD);
    if (pImg == nullptr)
      return false;

    auto *pImgArg = cast<Argument>(pImg);

    argNum = pImgArg->getArgNo();
  } else if (auto *Idx = dyn_cast<ConstantInt>(pImgOp)) {
    uint64_t IdxVal = Idx->getZExtValue();
    auto *arg = CImagesBI::CImagesUtils::findImageFromBufferPtr(*pMdUtils, pFunc, RESOURCE, IdxVal, modMD);

    if (!arg)
      return false;

    argNum = arg->getArgNo();
  } else {
    return false;
  }

  ArgInfoMetaDataHandle argHandle;
  bool found = false;
  if (funcInfoMD->isArgInfoListHasValue()) {
    for (auto AI = funcInfoMD->begin_ArgInfoList(), AE = funcInfoMD->end_ArgInfoList(); AI != AE; ++AI) {
      ArgInfoMetaDataHandle arg = *AI;
      if (arg->getExplicitArgNum() == argNum) {
        argHandle = arg;
        found = true;
        break;
      }
    }
  }

  if (!found) {
    argHandle = ArgInfoMetaDataHandle(new ArgInfoMetaData());
    argHandle->setExplicitArgNum(argNum);
    funcInfoMD->addArgInfoListItem(argHandle);
  }

  if (!argHandle->isImgAccessFloatCoordsHasValue())
    argHandle->setImgAccessFloatCoords(false);

  if (!argHandle->isImgAccessIntCoordsHasValue())
    argHandle->setImgAccessIntCoords(false);

  // We check here to see if the third coord (or for 2darray, the image layer)
  // has an integer origin.  For samplers of:
  // UNNORMALIZED COORDS, NEAREST FILTERTING, CLAMP_TO_EDGE ADDRESSING
  // In OCL:
  // 3D Images 3rd component: k = clamp((int)floor(w), 0, size-1)
  // 2D Image arrays: layer = clamp((int)rint(w), 0, size-1)
  // The two will match when floor(w) == rint(w)
  bool intCoords = CImagesBI::derivedFromInt(pCoord);

  if (intCoords)
    argHandle->setImgAccessIntCoords(true);
  else
    argHandle->setImgAccessFloatCoords(true);

  return true;
}

void Image3dToImage2darray::visitCallInst(CallInst &CI) {
  GenIntrinsicInst *pCall = nullptr;
  if ((pCall = dyn_cast<GenIntrinsicInst>(&CI)) == nullptr)
    return;

  switch (pCall->getIntrinsicID()) {
  case GenISAIntrinsic::GenISA_ldptr: {
    auto *pLoad = cast<SamplerLoadIntrinsic>(pCall);
    auto *pCoord = pCall->getArgOperand(3);
    m_Changed |= createImageAnnotations(pCall, pLoad->getTextureIndex(), m_MetadataUtils, m_modMD, pCoord);
    break;
  }
  case GenISAIntrinsic::GenISA_sampleLptr: {
    auto *pSample = cast<SampleIntrinsic>(pCall);
    auto *pCoord = pCall->getArgOperand(3);
    m_Changed |= createImageAnnotations(pCall, pSample->getTextureIndex(), m_MetadataUtils, m_modMD, pCoord);
    break;
  }
  default:
    return;
  }
}

bool Image3dToImage2darray::runOnFunction(Function &F) {
  m_MetadataUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
  m_modMD = getAnalysis<MetaDataUtilsWrapper>().getModuleMetaData();

  // This pass is not compatible with the SPV_INTEL_bindless_images extension.
  // The incompatibility arises because this pass requires images to be trackable
  // at compile time, a condition that bindless images from the SPV_INTEL_bindless_images
  // extension do not satisfy.
  if (m_modMD->extensions.spvINTELBindlessImages) {
    return false;
  }

  if (m_MetadataUtils->findFunctionsInfoItem(&F) == m_MetadataUtils->end_FunctionsInfo()) {
    return false;
  }

  visit(F);

  if (m_Changed) {
    m_MetadataUtils->save(F.getContext());
  }

  return m_Changed;
}