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 uint32_t FrontendResource::FrontendResource::getResourceKind() {
31   return cast<ConstantInt>(
32              cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue())
33       ->getLimitedValue();
34 }
35 uint32_t FrontendResource::getResourceIndex() {
36   return cast<ConstantInt>(
37              cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
38       ->getLimitedValue();
39 }
40 uint32_t FrontendResource::getSpace() {
41   return cast<ConstantInt>(
42              cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
43       ->getLimitedValue();
44 }
45 
46 FrontendResource::FrontendResource(GlobalVariable *GV, StringRef TypeStr,
47                                    ResourceKind RK, uint32_t ResIndex,
48                                    uint32_t Space) {
49   auto &Ctx = GV->getContext();
50   IRBuilder<> B(Ctx);
51   Entry = MDNode::get(
52       Ctx, {ValueAsMetadata::get(GV), MDString::get(Ctx, TypeStr),
53             ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))),
54             ConstantAsMetadata::get(B.getInt32(ResIndex)),
55             ConstantAsMetadata::get(B.getInt32(Space))});
56 }
57