SPIRVContext.cpp 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //===--- SPIRVContext.cpp - SPIR-V SPIRVContext implementation-------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #include <tuple>
  10. #include "clang/SPIRV/SPIRVContext.h"
  11. #include "llvm/llvm_assert/assert.h"
  12. namespace clang {
  13. namespace spirv {
  14. uint32_t SPIRVContext::getResultIdForType(const Type *t, bool *isRegistered) {
  15. assert(t != nullptr);
  16. uint32_t result_id = 0;
  17. auto iter = typeResultIdMap.find(t);
  18. if (iter == typeResultIdMap.end()) {
  19. // The Type has not been defined yet. Reserve an ID for it.
  20. result_id = takeNextId();
  21. typeResultIdMap[t] = result_id;
  22. if (isRegistered)
  23. *isRegistered = false;
  24. } else {
  25. result_id = iter->second;
  26. if (isRegistered)
  27. *isRegistered = true;
  28. }
  29. assert(result_id != 0);
  30. return result_id;
  31. }
  32. uint32_t SPIRVContext::getResultIdForConstant(const Constant *c) {
  33. assert(c != nullptr);
  34. uint32_t result_id = 0;
  35. auto iter = constantResultIdMap.find(c);
  36. if (iter == constantResultIdMap.end()) {
  37. // The constant has not been defined yet. Reserve an ID for it.
  38. result_id = takeNextId();
  39. constantResultIdMap[c] = result_id;
  40. } else {
  41. result_id = iter->second;
  42. }
  43. assert(result_id != 0);
  44. return result_id;
  45. }
  46. const Type *SPIRVContext::registerType(const Type &t) {
  47. // Insert function will only insert if it doesn't already exist in the set.
  48. TypeSet::iterator it;
  49. std::tie(it, std::ignore) = existingTypes.insert(t);
  50. return &*it;
  51. }
  52. const Constant *SPIRVContext::registerConstant(const Constant &c) {
  53. // Insert function will only insert if it doesn't already exist in the set.
  54. ConstantSet::iterator it;
  55. std::tie(it, std::ignore) = existingConstants.insert(c);
  56. return &*it;
  57. }
  58. const Decoration *SPIRVContext::registerDecoration(const Decoration &d) {
  59. // Insert function will only insert if it doesn't already exist in the set.
  60. DecorationSet::iterator it;
  61. std::tie(it, std::ignore) = existingDecorations.insert(d);
  62. return &*it;
  63. }
  64. } // end namespace spirv
  65. } // end namespace clang