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
13#include "llvm/Frontend/HLSL/HLSLResource.h"
14#include "llvm/IR/IRBuilder.h"
15#include "llvm/IR/Metadata.h"
16#include "llvm/IR/Module.h"
17
18using namespace llvm;
19using namespace llvm::hlsl;
20
21GlobalVariable *FrontendResource::getGlobalVariable() {
22 return cast<GlobalVariable>(
23 Val: cast<ConstantAsMetadata>(Val: Entry->getOperand(I: 0))->getValue());
24}
25
26ResourceKind FrontendResource::getResourceKind() {
27 return static_cast<ResourceKind>(
28 cast<ConstantInt>(
29 Val: cast<ConstantAsMetadata>(Val: Entry->getOperand(I: 1))->getValue())
30 ->getLimitedValue());
31}
32ElementType FrontendResource::getElementType() {
33 return static_cast<ElementType>(
34 cast<ConstantInt>(
35 Val: cast<ConstantAsMetadata>(Val: Entry->getOperand(I: 2))->getValue())
36 ->getLimitedValue());
37}
38bool FrontendResource::getIsROV() {
39 return cast<ConstantInt>(
40 Val: cast<ConstantAsMetadata>(Val: Entry->getOperand(I: 3))->getValue())
41 ->getLimitedValue();
42}
43uint32_t FrontendResource::getResourceIndex() {
44 return cast<ConstantInt>(
45 Val: cast<ConstantAsMetadata>(Val: Entry->getOperand(I: 4))->getValue())
46 ->getLimitedValue();
47}
48uint32_t FrontendResource::getSpace() {
49 return cast<ConstantInt>(
50 Val: cast<ConstantAsMetadata>(Val: Entry->getOperand(I: 5))->getValue())
51 ->getLimitedValue();
52}
53
54FrontendResource::FrontendResource(MDNode *E) : Entry(E) {
55 assert(Entry->getNumOperands() == 6 && "Unexpected metadata shape");
56}
57
58FrontendResource::FrontendResource(GlobalVariable *GV, ResourceKind RK,
59 ElementType ElTy, bool IsROV,
60 uint32_t ResIndex, uint32_t Space) {
61 auto &Ctx = GV->getContext();
62 IRBuilder<> B(Ctx);
63 Entry = MDNode::get(
64 Context&: Ctx, MDs: {ValueAsMetadata::get(V: GV),
65 ConstantAsMetadata::get(C: B.getInt32(C: static_cast<int>(RK))),
66 ConstantAsMetadata::get(C: B.getInt32(C: static_cast<int>(ElTy))),
67 ConstantAsMetadata::get(C: B.getInt1(V: IsROV)),
68 ConstantAsMetadata::get(C: B.getInt32(C: ResIndex)),
69 ConstantAsMetadata::get(C: B.getInt32(C: Space))});
70}
71