|
12 | 12 |
|
13 | 13 | #include "llvm/Frontend/HLSL/HLSLRootSignature.h" |
14 | 14 | #include "llvm/ADT/bit.h" |
| 15 | +#include "llvm/IR/IRBuilder.h" |
| 16 | +#include "llvm/IR/Metadata.h" |
| 17 | +#include "llvm/IR/Module.h" |
15 | 18 |
|
16 | 19 | namespace llvm { |
17 | 20 | namespace hlsl { |
@@ -160,6 +163,65 @@ void dumpRootElements(raw_ostream &OS, ArrayRef<RootElement> Elements) { |
160 | 163 | OS << "}"; |
161 | 164 | } |
162 | 165 |
|
| 166 | +MDNode *MetadataBuilder::BuildRootSignature() { |
| 167 | + for (const RootElement &Element : Elements) { |
| 168 | + MDNode *ElementMD = nullptr; |
| 169 | + if (const auto &Clause = std::get_if<DescriptorTableClause>(&Element)) |
| 170 | + ElementMD = BuildDescriptorTableClause(*Clause); |
| 171 | + if (const auto &Table = std::get_if<DescriptorTable>(&Element)) |
| 172 | + ElementMD = BuildDescriptorTable(*Table); |
| 173 | + |
| 174 | + // FIXME(#126586): remove once all RootElemnt variants are handled in a |
| 175 | + // visit or otherwise |
| 176 | + assert(ElementMD != nullptr && |
| 177 | + "Constructed an unhandled root element type."); |
| 178 | + |
| 179 | + GeneratedMetadata.push_back(ElementMD); |
| 180 | + } |
| 181 | + |
| 182 | + return MDNode::get(Ctx, GeneratedMetadata); |
| 183 | +} |
| 184 | + |
| 185 | +MDNode *MetadataBuilder::BuildDescriptorTable(const DescriptorTable &Table) { |
| 186 | + IRBuilder<> Builder(Ctx); |
| 187 | + SmallVector<Metadata *> TableOperands; |
| 188 | + // Set the mandatory arguments |
| 189 | + TableOperands.push_back(MDString::get(Ctx, "DescriptorTable")); |
| 190 | + TableOperands.push_back(ConstantAsMetadata::get( |
| 191 | + Builder.getInt32(llvm::to_underlying(Table.Visibility)))); |
| 192 | + |
| 193 | + // Remaining operands are references to the table's clauses. The in-memory |
| 194 | + // representation of the Root Elements created from parsing will ensure that |
| 195 | + // the previous N elements are the clauses for this table. |
| 196 | + assert(Table.NumClauses <= GeneratedMetadata.size() && |
| 197 | + "Table expected all owned clauses to be generated already"); |
| 198 | + // So, add a refence to each clause to our operands |
| 199 | + TableOperands.append(GeneratedMetadata.end() - Table.NumClauses, |
| 200 | + GeneratedMetadata.end()); |
| 201 | + // Then, remove those clauses from the general list of Root Elements |
| 202 | + GeneratedMetadata.pop_back_n(Table.NumClauses); |
| 203 | + |
| 204 | + return MDNode::get(Ctx, TableOperands); |
| 205 | +} |
| 206 | + |
| 207 | +MDNode *MetadataBuilder::BuildDescriptorTableClause( |
| 208 | + const DescriptorTableClause &Clause) { |
| 209 | + IRBuilder<> Builder(Ctx); |
| 210 | + std::string Name; |
| 211 | + llvm::raw_string_ostream OS(Name); |
| 212 | + OS << Clause.Type; |
| 213 | + return MDNode::get( |
| 214 | + Ctx, { |
| 215 | + MDString::get(Ctx, OS.str()), |
| 216 | + ConstantAsMetadata::get(Builder.getInt32(Clause.NumDescriptors)), |
| 217 | + ConstantAsMetadata::get(Builder.getInt32(Clause.Reg.Number)), |
| 218 | + ConstantAsMetadata::get(Builder.getInt32(Clause.Space)), |
| 219 | + ConstantAsMetadata::get(Builder.getInt32(Clause.Offset)), |
| 220 | + ConstantAsMetadata::get( |
| 221 | + Builder.getInt32(llvm::to_underlying(Clause.Flags))), |
| 222 | + }); |
| 223 | +} |
| 224 | + |
163 | 225 | } // namespace rootsig |
164 | 226 | } // namespace hlsl |
165 | 227 | } // namespace llvm |
0 commit comments