xref: /llvm-project/llvm/lib/Frontend/HLSL/HLSLResource.cpp (revision 18f0da26b292341f84f91d4ce731b046315cd0a1)
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 
18 using namespace llvm;
19 using namespace llvm::hlsl;
20 
21 GlobalVariable *FrontendResource::getGlobalVariable() {
22   return cast<GlobalVariable>(
23       cast<ConstantAsMetadata>(Entry->getOperand(0))->getValue());
24 }
25 
26 StringRef FrontendResource::getSourceType() {
27   return cast<MDString>(Entry->getOperand(1))->getString();
28 }
29 
30 ResourceKind FrontendResource::getResourceKind() {
31   return static_cast<ResourceKind>(
32       cast<ConstantInt>(
33           cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue())
34           ->getLimitedValue());
35 }
36 uint32_t FrontendResource::getResourceIndex() {
37   return cast<ConstantInt>(
38              cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
39       ->getLimitedValue();
40 }
41 uint32_t FrontendResource::getSpace() {
42   return cast<ConstantInt>(
43              cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
44       ->getLimitedValue();
45 }
46 
47 FrontendResource::FrontendResource(GlobalVariable *GV, StringRef TypeStr,
48                                    ResourceKind RK, uint32_t ResIndex,
49                                    uint32_t Space) {
50   auto &Ctx = GV->getContext();
51   IRBuilder<> B(Ctx);
52   Entry = MDNode::get(
53       Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, TypeStr),
54             ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))),
55             ConstantAsMetadata::get(B.getInt32(ResIndex)),
56             ConstantAsMetadata::get(B.getInt32(Space))});
57 }
58