RSAllocationUtils.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229
  1. /*
  2. * Copyright 2016, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "RSAllocationUtils.h"
  17. #include "llvm/ADT/StringRef.h"
  18. #include "llvm/IR/Constants.h"
  19. #include "llvm/IR/GlobalVariable.h"
  20. #include "llvm/IR/Instructions.h"
  21. #include "llvm/IR/Module.h"
  22. #include "llvm/Support/Debug.h"
  23. #include "llvm/Support/raw_ostream.h"
  24. #include "cxxabi.h"
  25. #include <sstream>
  26. #include <unordered_map>
  27. #define DEBUG_TYPE "rs2spirv-rs-allocation-utils"
  28. using namespace llvm;
  29. namespace rs2spirv {
  30. bool isRSAllocation(const GlobalVariable &GV) {
  31. auto *PT = cast<PointerType>(GV.getType());
  32. DEBUG(PT->dump());
  33. auto *VT = PT->getElementType();
  34. DEBUG(VT->dump());
  35. std::string TypeName;
  36. raw_string_ostream RSO(TypeName);
  37. VT->print(RSO);
  38. RSO.str(); // Force flush.
  39. DEBUG(dbgs() << "TypeName: " << TypeName << '\n');
  40. return TypeName.find("struct.rs_allocation") != std::string::npos;
  41. }
  42. bool getRSAllocationInfo(Module &M, SmallVectorImpl<RSAllocationInfo> &Allocs) {
  43. DEBUG(dbgs() << "getRSAllocationInfo\n");
  44. for (auto &GV : M.globals()) {
  45. if (GV.isDeclaration() || !isRSAllocation(GV))
  46. continue;
  47. Allocs.push_back({'%' + GV.getName().str(), None, &GV, -1});
  48. }
  49. return true;
  50. }
  51. // Collect Allocation access calls into the Calls
  52. // Also update Allocs with assigned ID.
  53. // After calling this function, Allocs would contain the mapping from
  54. // GV name to the corresponding ID.
  55. bool getRSAllocAccesses(SmallVectorImpl<RSAllocationInfo> &Allocs,
  56. SmallVectorImpl<RSAllocationCallInfo> &Calls) {
  57. DEBUG(dbgs() << "getRSGEATCalls\n");
  58. DEBUG(dbgs() << "\n\n~~~~~~~~~~~~~~~~~~~~~\n\n");
  59. std::unordered_map<const Value *, const GlobalVariable *> Mapping;
  60. int id_assigned = 0;
  61. for (auto &A : Allocs) {
  62. auto *GV = A.GlobalVar;
  63. std::vector<User *> WorkList(GV->user_begin(), GV->user_end());
  64. size_t Idx = 0;
  65. while (Idx < WorkList.size()) {
  66. auto *U = WorkList[Idx];
  67. DEBUG(dbgs() << "Visiting ");
  68. DEBUG(U->dump());
  69. ++Idx;
  70. auto It = Mapping.find(U);
  71. if (It != Mapping.end()) {
  72. if (It->second == GV) {
  73. continue;
  74. } else {
  75. errs() << "Duplicate global mapping discovered!\n";
  76. errs() << "\nGlobal: ";
  77. GV->print(errs());
  78. errs() << "\nExisting mapping: ";
  79. It->second->print(errs());
  80. errs() << "\nUser: ";
  81. U->print(errs());
  82. errs() << '\n';
  83. return false;
  84. }
  85. }
  86. Mapping[U] = GV;
  87. DEBUG(dbgs() << "New mapping: ");
  88. DEBUG(U->print(dbgs()));
  89. DEBUG(dbgs() << " -> " << GV->getName() << '\n');
  90. if (auto *FCall = dyn_cast<CallInst>(U)) {
  91. if (auto *F = FCall->getCalledFunction()) {
  92. const auto FName = F->getName();
  93. DEBUG(dbgs() << "Discovered function call to : " << FName << '\n');
  94. // Treat memcpy as moves for the purpose of this analysis
  95. if (FName.startswith("llvm.memcpy")) {
  96. assert(FCall->getNumArgOperands() > 0);
  97. Value *CopyDest = FCall->getArgOperand(0);
  98. // We are interested in the users of the dest operand of
  99. // memcpy here
  100. Value *LocalCopy = CopyDest->stripPointerCasts();
  101. User *NewU = dyn_cast<User>(LocalCopy);
  102. assert(NewU);
  103. WorkList.push_back(NewU);
  104. continue;
  105. }
  106. char *demangled = __cxxabiv1::__cxa_demangle(
  107. FName.str().c_str(), nullptr, nullptr, nullptr);
  108. if (!demangled)
  109. continue;
  110. const StringRef DemangledNameRef(demangled);
  111. DEBUG(dbgs() << "Demangled name: " << DemangledNameRef << '\n');
  112. const StringRef GEAPrefix = "rsGetElementAt_";
  113. const StringRef SEAPrefix = "rsSetElementAt_";
  114. const StringRef DIMXPrefix = "rsAllocationGetDimX";
  115. assert(GEAPrefix.size() == SEAPrefix.size());
  116. const bool IsGEA = DemangledNameRef.startswith(GEAPrefix);
  117. const bool IsSEA = DemangledNameRef.startswith(SEAPrefix);
  118. const bool IsDIMX = DemangledNameRef.startswith(DIMXPrefix);
  119. assert(IsGEA || IsSEA || IsDIMX);
  120. if (!A.hasID()) {
  121. A.assignID(id_assigned++);
  122. }
  123. if (IsGEA || IsSEA) {
  124. DEBUG(dbgs() << "Found rsAlloc function!\n");
  125. const auto Kind =
  126. IsGEA ? RSAllocAccessKind::GEA : RSAllocAccessKind::SEA;
  127. const auto RSElementTy =
  128. DemangledNameRef.drop_front(GEAPrefix.size());
  129. Calls.push_back({A, FCall, Kind, RSElementTy.str()});
  130. continue;
  131. } else if (DemangledNameRef.startswith(GEAPrefix.drop_back()) ||
  132. DemangledNameRef.startswith(SEAPrefix.drop_back())) {
  133. errs() << "Untyped accesses to global rs_allocations are not "
  134. "supported.\n";
  135. return false;
  136. } else if (IsDIMX) {
  137. DEBUG(dbgs() << "Found rsAllocationGetDimX function!\n");
  138. const auto Kind = RSAllocAccessKind::DIMX;
  139. Calls.push_back({A, FCall, Kind, ""});
  140. }
  141. }
  142. }
  143. // TODO: Consider using set-like container to reduce computational
  144. // complexity.
  145. for (auto *NewU : U->users())
  146. if (std::find(WorkList.begin(), WorkList.end(), NewU) == WorkList.end())
  147. WorkList.push_back(NewU);
  148. }
  149. }
  150. std::unordered_map<const GlobalVariable *, std::string> GVAccessTypes;
  151. for (auto &Access : Calls) {
  152. auto AccessElemTyIt = GVAccessTypes.find(Access.RSAlloc.GlobalVar);
  153. if (AccessElemTyIt != GVAccessTypes.end() &&
  154. AccessElemTyIt->second != Access.RSElementTy) {
  155. errs() << "Could not infere element type for: ";
  156. Access.RSAlloc.GlobalVar->print(errs());
  157. errs() << '\n';
  158. return false;
  159. } else if (AccessElemTyIt == GVAccessTypes.end()) {
  160. GVAccessTypes.emplace(Access.RSAlloc.GlobalVar, Access.RSElementTy);
  161. Access.RSAlloc.RSElementType = Access.RSElementTy;
  162. }
  163. }
  164. DEBUG(dbgs() << "\n\n~~~~~~~~~~~~~~~~~~~~~\n\n");
  165. return true;
  166. }
  167. bool solidifyRSAllocAccess(Module &M, RSAllocationCallInfo CallInfo) {
  168. DEBUG(dbgs() << "solidifyRSAllocAccess " << CallInfo.RSAlloc.VarName << '\n');
  169. auto *FCall = CallInfo.FCall;
  170. auto *Fun = FCall->getCalledFunction();
  171. assert(Fun);
  172. StringRef FName;
  173. if (CallInfo.Kind == RSAllocAccessKind::DIMX)
  174. FName = "rsAllocationGetDimX";
  175. else
  176. FName = Fun->getName();
  177. std::ostringstream OSS;
  178. OSS << "__rsov_" << FName.str();
  179. // Make up uint32_t F(uint32_t)
  180. Type *UInt32Ty = IntegerType::get(M.getContext(), 32);
  181. auto *NewFT = FunctionType::get(UInt32Ty, ArrayRef<Type *>(UInt32Ty), false);
  182. auto *NewF = Function::Create(NewFT, // Fun->getFunctionType(),
  183. Function::ExternalLinkage, OSS.str(), &M);
  184. FCall->setCalledFunction(NewF);
  185. FCall->setArgOperand(0, ConstantInt::get(UInt32Ty, 0, false));
  186. NewF->setAttributes(Fun->getAttributes());
  187. DEBUG(M.dump());
  188. return true;
  189. }
  190. } // namespace rs2spirv