xref: /llvm-project/llvm/lib/Frontend/HLSL/HLSLResource.cpp (revision 36ada1b9b26eeff34a9427214672cf1540c01667)
1911d2dc2SChris Bieneman //===- HLSLResource.cpp - HLSL Resource helper objects --------------------===//
2911d2dc2SChris Bieneman //
3911d2dc2SChris Bieneman // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4911d2dc2SChris Bieneman // See https://llvm.org/LICENSE.txt for license information.
5911d2dc2SChris Bieneman // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6911d2dc2SChris Bieneman //
7911d2dc2SChris Bieneman //===----------------------------------------------------------------------===//
8911d2dc2SChris Bieneman ///
9911d2dc2SChris Bieneman /// \file This file contains helper objects for working with HLSL Resources.
10911d2dc2SChris Bieneman ///
11911d2dc2SChris Bieneman //===----------------------------------------------------------------------===//
12911d2dc2SChris Bieneman 
13911d2dc2SChris Bieneman #include "llvm/Frontend/HLSL/HLSLResource.h"
14911d2dc2SChris Bieneman #include "llvm/IR/IRBuilder.h"
15911d2dc2SChris Bieneman #include "llvm/IR/Metadata.h"
16911d2dc2SChris Bieneman 
17911d2dc2SChris Bieneman using namespace llvm;
18911d2dc2SChris Bieneman using namespace llvm::hlsl;
19911d2dc2SChris Bieneman 
20911d2dc2SChris Bieneman GlobalVariable *FrontendResource::getGlobalVariable() {
21911d2dc2SChris Bieneman   return cast<GlobalVariable>(
22911d2dc2SChris Bieneman       cast<ConstantAsMetadata>(Entry->getOperand(0))->getValue());
23911d2dc2SChris Bieneman }
24911d2dc2SChris Bieneman 
2518f0da26SJustin Bogner ResourceKind FrontendResource::getResourceKind() {
2618f0da26SJustin Bogner   return static_cast<ResourceKind>(
2718f0da26SJustin Bogner       cast<ConstantInt>(
284f54d715SJustin Bogner           cast<ConstantAsMetadata>(Entry->getOperand(1))->getValue())
294f54d715SJustin Bogner           ->getLimitedValue());
304f54d715SJustin Bogner }
314f54d715SJustin Bogner ElementType FrontendResource::getElementType() {
324f54d715SJustin Bogner   return static_cast<ElementType>(
334f54d715SJustin Bogner       cast<ConstantInt>(
344c7218e7SChris Bieneman           cast<ConstantAsMetadata>(Entry->getOperand(2))->getValue())
3518f0da26SJustin Bogner           ->getLimitedValue());
3613163dd8SXiang Li }
377a13e410SJustin Bogner bool FrontendResource::getIsROV() {
3813163dd8SXiang Li   return cast<ConstantInt>(
394c7218e7SChris Bieneman              cast<ConstantAsMetadata>(Entry->getOperand(3))->getValue())
4013163dd8SXiang Li       ->getLimitedValue();
4113163dd8SXiang Li }
427a13e410SJustin Bogner uint32_t FrontendResource::getResourceIndex() {
4313163dd8SXiang Li   return cast<ConstantInt>(
444c7218e7SChris Bieneman              cast<ConstantAsMetadata>(Entry->getOperand(4))->getValue())
4513163dd8SXiang Li       ->getLimitedValue();
4613163dd8SXiang Li }
477a13e410SJustin Bogner uint32_t FrontendResource::getSpace() {
487a13e410SJustin Bogner   return cast<ConstantInt>(
497a13e410SJustin Bogner              cast<ConstantAsMetadata>(Entry->getOperand(5))->getValue())
507a13e410SJustin Bogner       ->getLimitedValue();
517a13e410SJustin Bogner }
5213163dd8SXiang Li 
53*f12655acSNikita Popov FrontendResource::FrontendResource(MDNode *E) : Entry(E) {
54*f12655acSNikita Popov   assert(Entry->getNumOperands() == 6 && "Unexpected metadata shape");
55*f12655acSNikita Popov }
56*f12655acSNikita Popov 
574f54d715SJustin Bogner FrontendResource::FrontendResource(GlobalVariable *GV, ResourceKind RK,
584f54d715SJustin Bogner                                    ElementType ElTy, bool IsROV,
597a13e410SJustin Bogner                                    uint32_t ResIndex, uint32_t Space) {
60911d2dc2SChris Bieneman   auto &Ctx = GV->getContext();
61911d2dc2SChris Bieneman   IRBuilder<> B(Ctx);
6213163dd8SXiang Li   Entry = MDNode::get(
634f54d715SJustin Bogner       Ctx, {ValueAsMetadata::get(GV),
6413163dd8SXiang Li             ConstantAsMetadata::get(B.getInt32(static_cast<int>(RK))),
654f54d715SJustin Bogner             ConstantAsMetadata::get(B.getInt32(static_cast<int>(ElTy))),
667a13e410SJustin Bogner             ConstantAsMetadata::get(B.getInt1(IsROV)),
6713163dd8SXiang Li             ConstantAsMetadata::get(B.getInt32(ResIndex)),
6813163dd8SXiang Li             ConstantAsMetadata::get(B.getInt32(Space))});
69911d2dc2SChris Bieneman }
70