HLSLResource.cpp 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. //===- HLSLResource.cpp - HLSL Resource helper objects --------------------===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. ///
  9. /// \file This file contains helper objects for working with HLSL Resources.
  10. ///
  11. //===----------------------------------------------------------------------===//
  12. #include "llvm/Frontend/HLSL/HLSLResource.h"
  13. #include "llvm/IR/IRBuilder.h"
  14. #include "llvm/IR/Metadata.h"
  15. #include "llvm/IR/Module.h"
  16. using namespace llvm;
  17. using namespace llvm::hlsl;
  18. GlobalVariable *FrontendResource::getGlobalVariable() {
  19. return cast<GlobalVariable>(
  20. cast<ConstantAsMetadata>(Entry->getOperand(0))->getValue());
  21. }
  22. StringRef FrontendResource::getSourceType() {
  23. return cast<MDString>(Entry->getOperand(1))->getString();
  24. }
  25. uint32_t FrontendResource::FrontendResource::getResourceKind() {
  26. return cast<ConstantInt>(
  27. cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue())
  28. ->getLimitedValue();
  29. }
  30. uint32_t FrontendResource::getResourceIndex() {
  31. return cast<ConstantInt>(
  32. cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
  33. ->getLimitedValue();
  34. }
  35. uint32_t FrontendResource::getSpace() {
  36. return cast<ConstantInt>(
  37. cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
  38. ->getLimitedValue();
  39. }
  40. FrontendResource::FrontendResource(GlobalVariable *GV, StringRef TypeStr,
  41. ResourceKind RK, uint32_t ResIndex,
  42. uint32_t Space) {
  43. auto &Ctx = GV->getContext();
  44. IRBuilder<> B(Ctx);
  45. Entry = MDNode::get(
  46. Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, TypeStr),
  47. ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))),
  48. ConstantAsMetadata::get(B.getInt32(ResIndex)),
  49. ConstantAsMetadata::get(B.getInt32(Space))});
  50. }