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