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