Context.cpp 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  1. /*
  2. * Copyright 2017, 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 "Context.h"
  17. #include "llvm/ADT/Triple.h"
  18. #include "llvm/IR/Module.h"
  19. #include "llvm/Support/Debug.h"
  20. #include "llvm/Support/raw_ostream.h"
  21. #include <limits>
  22. #define DEBUG_TYPE "rs2spirv-context"
  23. namespace rs2spirv {
  24. Context &Context::getInstance() {
  25. static Context c;
  26. return c;
  27. }
  28. Context::Context() : mInitialized(false) {}
  29. bool Context::Initialize(std::unique_ptr<bcinfo::MetadataExtractor> ME) {
  30. if (mInitialized) {
  31. return true;
  32. }
  33. mMetadata = std::move(ME);
  34. if (!mMetadata->extract()) {
  35. llvm::errs() << "cannot extract metadata\n";
  36. return false;
  37. }
  38. const char **varNames = mMetadata->getExportVarNameList();
  39. size_t varCount = mMetadata->getExportVarCount();
  40. mExportVarIndices.resize(varCount);
  41. // Builds the lookup table from a variable name to its slot number
  42. for (size_t slot = 0; slot < varCount; slot++) {
  43. std::string varName(varNames[slot]);
  44. mVarNameToSlot.insert(std::make_pair(varName, (uint32_t)slot));
  45. }
  46. const size_t kernelCount = mMetadata->getExportForEachSignatureCount();
  47. const char **kernelNames = mMetadata->getExportForEachNameList();
  48. for (size_t slot = 0; slot < kernelCount; slot++) {
  49. mForEachNameToSlot.insert(std::make_pair(kernelNames[slot], slot));
  50. }
  51. mInitialized = true;
  52. return true;
  53. }
  54. void Context::addExportVarIndex(const char *varName, uint32_t index) {
  55. DEBUG(llvm::dbgs() << varName << " index=" << index << '\n');
  56. const uint32_t slot = getSlotForExportVar(varName);
  57. if (slot == std::numeric_limits<uint32_t>::max()) {
  58. return;
  59. }
  60. addExportVarIndex(slot, index);
  61. }
  62. } // namespace rs2spirv