1 //===- llvm/MC/DXContainerPSVInfo.h - DXContainer PSVInfo -*- C++ -------*-===//
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 #ifndef LLVM_MC_DXCONTAINERPSVINFO_H
10 #define LLVM_MC_DXCONTAINERPSVINFO_H
11 
12 #include "llvm/ADT/SmallVector.h"
13 #include "llvm/ADT/StringRef.h"
14 #include "llvm/BinaryFormat/DXContainer.h"
15 #include "llvm/TargetParser/Triple.h"
16 
17 #include <array>
18 #include <numeric>
19 #include <stdint.h>
20 
21 namespace llvm {
22 
23 class raw_ostream;
24 
25 namespace mcdxbc {
26 
27 struct PSVSignatureElement {
28   StringRef Name;
29   SmallVector<uint32_t> Indices;
30   uint8_t StartRow;
31   uint8_t Cols;
32   uint8_t StartCol;
33   bool Allocated;
34   dxbc::PSV::SemanticKind Kind;
35   dxbc::PSV::ComponentType Type;
36   dxbc::PSV::InterpolationMode Mode;
37   uint8_t DynamicMask;
38   uint8_t Stream;
39 };
40 
41 // This data structure is a helper for reading and writing PSV RuntimeInfo data.
42 // It is implemented in the BinaryFormat library so that it can be used by both
43 // the MC layer and Object tools.
44 // This structure is used to represent the extracted data in an inspectable and
45 // modifiable format, and can be used to serialize the data back into valid PSV
46 // RuntimeInfo.
47 struct PSVRuntimeInfo {
48   bool IsFinalized = false;
49   dxbc::PSV::v2::RuntimeInfo BaseData;
50   SmallVector<dxbc::PSV::v2::ResourceBindInfo> Resources;
51   SmallVector<PSVSignatureElement> InputElements;
52   SmallVector<PSVSignatureElement> OutputElements;
53   SmallVector<PSVSignatureElement> PatchOrPrimElements;
54 
55   // TODO: Make this interface user-friendly.
56   // The interface here is bad, and we'll want to change this in the future. We
57   // probably will want to build out these mask vectors as vectors of bools and
58   // have this utility object convert them to the bit masks. I don't want to
59   // over-engineer this API now since we don't know what the data coming in to
60   // feed it will look like, so I kept it extremely simple for the immediate use
61   // case.
62   std::array<SmallVector<uint32_t>, 4> OutputVectorMasks;
63   SmallVector<uint32_t> PatchOrPrimMasks;
64   std::array<SmallVector<uint32_t>, 4> InputOutputMap;
65   SmallVector<uint32_t> InputPatchMap;
66   SmallVector<uint32_t> PatchOutputMap;
67 
68   // Serialize PSVInfo into the provided raw_ostream. The version field
69   // specifies the data version to encode, the default value specifies encoding
70   // the highest supported version.
71   void write(raw_ostream &OS,
72              uint32_t Version = std::numeric_limits<uint32_t>::max()) const;
73 
finalizePSVRuntimeInfo74   void finalize(Triple::EnvironmentType Stage) {
75     IsFinalized = true;
76     BaseData.SigInputElements = static_cast<uint32_t>(InputElements.size());
77     BaseData.SigOutputElements = static_cast<uint32_t>(OutputElements.size());
78     BaseData.SigPatchOrPrimElements =
79         static_cast<uint32_t>(PatchOrPrimElements.size());
80     if (!sys::IsBigEndianHost)
81       return;
82     BaseData.swapBytes();
83     BaseData.swapBytes(Stage);
84     for (auto &Res : Resources)
85       Res.swapBytes();
86   }
87 };
88 
89 class Signature {
90   struct Parameter {
91     uint32_t Stream;
92     StringRef Name;
93     uint32_t Index;
94     dxbc::D3DSystemValue SystemValue;
95     dxbc::SigComponentType CompType;
96     uint32_t Register;
97     uint8_t Mask;
98     uint8_t ExclusiveMask;
99     dxbc::SigMinPrecision MinPrecision;
100   };
101 
102   SmallVector<Parameter> Params;
103 
104 public:
addParam(uint32_t Stream,StringRef Name,uint32_t Index,dxbc::D3DSystemValue SystemValue,dxbc::SigComponentType CompType,uint32_t Register,uint8_t Mask,uint8_t ExclusiveMask,dxbc::SigMinPrecision MinPrecision)105   void addParam(uint32_t Stream, StringRef Name, uint32_t Index,
106                 dxbc::D3DSystemValue SystemValue,
107                 dxbc::SigComponentType CompType, uint32_t Register,
108                 uint8_t Mask, uint8_t ExclusiveMask,
109                 dxbc::SigMinPrecision MinPrecision) {
110     Params.push_back(Parameter{Stream, Name, Index, SystemValue, CompType,
111                                Register, Mask, ExclusiveMask, MinPrecision});
112   }
113 
114   void write(raw_ostream &OS);
115 };
116 
117 } // namespace mcdxbc
118 } // namespace llvm
119 
120 #endif // LLVM_MC_DXCONTAINERPSVINFO_H
121