xref: /freebsd/contrib/llvm-project/llvm/lib/Frontend/HLSL/HLSLResource.cpp (revision 0fca6ea1d4eea4c934cfff25ac9ee8ad6fe95583)
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 
getGlobalVariable()21bdd1243dSDimitry Andric GlobalVariable *FrontendResource::getGlobalVariable() {
22bdd1243dSDimitry Andric   return cast<GlobalVariable>(
23bdd1243dSDimitry Andric       cast<ConstantAsMetadata>(Entry->getOperand(0))->getValue());
24bdd1243dSDimitry Andric }
25bdd1243dSDimitry Andric 
getResourceKind()265f757f3fSDimitry Andric ResourceKind FrontendResource::getResourceKind() {
275f757f3fSDimitry Andric   return static_cast<ResourceKind>(
285f757f3fSDimitry Andric       cast<ConstantInt>(
29cb14a3feSDimitry Andric           cast<ConstantAsMetadata>(Entry->getOperand(1))->getValue())
30cb14a3feSDimitry Andric           ->getLimitedValue());
31cb14a3feSDimitry Andric }
getElementType()32cb14a3feSDimitry Andric ElementType FrontendResource::getElementType() {
33cb14a3feSDimitry Andric   return static_cast<ElementType>(
34cb14a3feSDimitry Andric       cast<ConstantInt>(
35bdd1243dSDimitry Andric           cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue())
365f757f3fSDimitry Andric           ->getLimitedValue());
37bdd1243dSDimitry Andric }
getIsROV()385f757f3fSDimitry Andric bool FrontendResource::getIsROV() {
39bdd1243dSDimitry Andric   return cast<ConstantInt>(
40bdd1243dSDimitry Andric              cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
41bdd1243dSDimitry Andric       ->getLimitedValue();
42bdd1243dSDimitry Andric }
getResourceIndex()435f757f3fSDimitry Andric uint32_t FrontendResource::getResourceIndex() {
44bdd1243dSDimitry Andric   return cast<ConstantInt>(
45bdd1243dSDimitry Andric              cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
46bdd1243dSDimitry Andric       ->getLimitedValue();
47bdd1243dSDimitry Andric }
getSpace()485f757f3fSDimitry Andric uint32_t FrontendResource::getSpace() {
495f757f3fSDimitry Andric   return cast<ConstantInt>(
505f757f3fSDimitry Andric              cast<ConstantAsMetadata>(Entry->getOperand(5))->getValue())
515f757f3fSDimitry Andric       ->getLimitedValue();
525f757f3fSDimitry Andric }
53bdd1243dSDimitry Andric 
FrontendResource(MDNode * E)54*0fca6ea1SDimitry Andric FrontendResource::FrontendResource(MDNode *E) : Entry(E) {
55*0fca6ea1SDimitry Andric   assert(Entry->getNumOperands() == 6 && "Unexpected metadata shape");
56*0fca6ea1SDimitry Andric }
57*0fca6ea1SDimitry Andric 
FrontendResource(GlobalVariable * GV,ResourceKind RK,ElementType ElTy,bool IsROV,uint32_t ResIndex,uint32_t Space)58cb14a3feSDimitry Andric FrontendResource::FrontendResource(GlobalVariable *GV, ResourceKind RK,
59cb14a3feSDimitry Andric                                    ElementType ElTy, bool IsROV,
605f757f3fSDimitry Andric                                    uint32_t ResIndex, uint32_t Space) {
61bdd1243dSDimitry Andric   auto &Ctx = GV->getContext();
62bdd1243dSDimitry Andric   IRBuilder<> B(Ctx);
63bdd1243dSDimitry Andric   Entry = MDNode::get(
64cb14a3feSDimitry Andric       Ctx, {ValueAsMetadata::get(GV),
65bdd1243dSDimitry Andric             ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))),
66cb14a3feSDimitry Andric             ConstantAsMetadata::get(B.getInt32(static_cast<int>(ElTy))),
675f757f3fSDimitry Andric             ConstantAsMetadata::get(B.getInt1(IsROV)),
68bdd1243dSDimitry Andric             ConstantAsMetadata::get(B.getInt32(ResIndex)),
69bdd1243dSDimitry Andric             ConstantAsMetadata::get(B.getInt32(Space))});
70bdd1243dSDimitry Andric }
71