LLVM: lib/Target/DirectX/DXILTranslateMetadata.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

33#include

34

35using namespace llvm;

37

38namespace {

39

40

41class DiagnosticInfoTranslateMD : public DiagnosticInfo {

42private:

45

46public:

47

48

49

50 DiagnosticInfoTranslateMD(const Module &M, const Twine &Msg,

53

55 DP << Mod.getName() << ": " << Msg << '\n';

56 }

57};

58

59enum class EntryPropsTag {

60 ShaderFlags = 0,

61 GSState,

62 DSState,

63 HSState,

64 NumThreads,

65 AutoBindingSpace,

66 RayPayloadSize,

67 RayAttribSize,

68 ShaderKind,

69 MSState,

70 ASStateTag,

71 WaveSize,

72 EntryRootSig,

73};

74

75}

76

81

83 if (!RI.hasSymbol())

84 RI.createSymbol(M, DRTM[RI.getHandleTy()].createElementStruct());

85

88 SRVs.push_back(RI.getAsMetadata(M, DRTM[RI.getHandleTy()]));

90 UAVs.push_back(RI.getAsMetadata(M, DRTM[RI.getHandleTy()]));

92 CBufs.push_back(RI.getAsMetadata(M, DRTM[RI.getHandleTy()]));

94 Smps.push_back(RI.getAsMetadata(M, DRTM[RI.getHandleTy()]));

95

100 bool HasResources = !DBM.empty();

101

102 if (MDResources.hasUAVs()) {

103 assert(!UAVMD && "Old and new UAV representations can't coexist");

104 UAVMD = MDResources.writeUAVs(M);

105 HasResources = true;

106 }

107

109 assert(!CBufMD && "Old and new cbuffer representations can't coexist");

111 HasResources = true;

112 }

113

114 if (!HasResources)

115 return nullptr;

116

117 NamedMDNode *ResourceMD = M.getOrInsertNamedMetadata("dx.resources");

119 MDNode::get(M.getContext(), {SRVMD, UAVMD, CBufMD, SmpMD}));

120

121 return ResourceMD;

122}

123

125 switch (Env) {

127 return "ps";

129 return "vs";

131 return "gs";

133 return "hs";

135 return "ds";

137 return "cs";

139 return "lib";

141 return "ms";

143 return "as";

144 default:

145 break;

146 }

147 llvm_unreachable("Unsupported environment for DXIL generation.");

148}

149

152}

153

158 ConstantInt::get(Type::getInt32Ty(Ctx), static_cast<int>(Tag))));

159 switch (Tag) {

160 case EntryPropsTag::ShaderFlags:

163 break;

164 case EntryPropsTag::ShaderKind:

167 break;

168 case EntryPropsTag::GSState:

169 case EntryPropsTag::DSState:

170 case EntryPropsTag::HSState:

171 case EntryPropsTag::NumThreads:

172 case EntryPropsTag::AutoBindingSpace:

173 case EntryPropsTag::RayPayloadSize:

174 case EntryPropsTag::RayAttribSize:

175 case EntryPropsTag::MSState:

176 case EntryPropsTag::ASStateTag:

177 case EntryPropsTag::WaveSize:

178 case EntryPropsTag::EntryRootSig:

180 }

181 return MDVals;

182}

183

189 if (EntryShaderFlags != 0)

191 EntryShaderFlags, Ctx));

192

193 if (EP.Entry != nullptr) {

194

195

196

197 if (ShaderProfile == Triple::EnvironmentType::Library &&

198 EP.ShaderStage != Triple::EnvironmentType::Library)

201

202 if (EP.ShaderStage == Triple::EnvironmentType::Compute) {

204 Type::getInt32Ty(Ctx), static_cast<int>(EntryPropsTag::NumThreads))));

212 }

213 }

214 if (MDVals.empty())

215 return nullptr;

217}

218

222

223

224

225

226

227

229 MDVals[0] =

234 MDVals[4] = Properties;

236}

237

240 const uint64_t EntryShaderFlags,

246}

247

250 return;

251

255 MDVals[0] =

259 NamedMDNode *ValVerNode = M.getOrInsertNamedMetadata("dx.valver");

260

263}

264

274 NamedMDNode *SMMDNode = M.getOrInsertNamedMetadata("dx.shaderModel");

276}

277

284 DXILVals[1] =

286 NamedMDNode *DXILVerMDNode = M.getOrInsertNamedMetadata("dx.version");

288}

289

293 MDTuple *Properties = nullptr;

294 if (ShaderFlags != 0) {

299 }

300

301

303}

304

305

306

310 Instruction *BBTerminatorInst = BB.getTerminator();

311

312 MDNode *HlslControlFlowMD =

313 BBTerminatorInst->getMetadata("hlsl.controlflow.hint");

314

315 if (!HlslControlFlowMD)

316 continue;

317

319 "invalid operands for hlsl.controlflow.hint");

320

321 MDBuilder MDHelper(M.getContext());

323 mdconst::extract(HlslControlFlowMD->getOperand(1));

324

328

330

332 BBTerminatorInst->setMetadata("hlsl.controlflow.hint", nullptr);

333 }

334 }

335}

336

345

351 auto *ResourceMD =

352 (NamedResourceMD != nullptr) ? NamedResourceMD->getOperand(0) : nullptr;

353

354

356

357 if (MMDI.ShaderProfile == Triple::EnvironmentType::Library) {

358

359

360

361 uint64_t CombinedMask = ShaderFlags.getCombinedFlags();

365 M.getContext().diagnose(DiagnosticInfoTranslateMD(

366 M, "Non-library shader: One and only one entry expected"));

367 }

368

371 ShaderFlags.getFunctionFlags(EntryProp.Entry);

372

373

374

375 uint64_t EntryShaderFlags = 0;

376 if (MMDI.ShaderProfile != Triple::EnvironmentType::Library) {

377 EntryShaderFlags = EntrySFMask;

379 M.getContext().diagnose(DiagnosticInfoTranslateMD(

380 M,

381 "Shader stage '" +

384 "' different from specified target profile '" +

386 "'"))));

387 }

388 }

390 EntryShaderFlags,

392 }

393

395 M.getOrInsertNamedMetadata("dx.entryPoints");

396 for (auto *Entry : EntryFnMDNodes)

397 EntryPointsNamedMD->addOperand(Entry);

398}

399

407

410

412}

413

414namespace {

415class DXILTranslateMetadataLegacy : public ModulePass {

416public:

417 static char ID;

418 explicit DXILTranslateMetadataLegacy() : ModulePass(ID) {}

419

420 StringRef getPassName() const override { return "DXIL Translate Metadata"; }

421

422 void getAnalysisUsage(AnalysisUsage &AU) const override {

432 }

433

434 bool runOnModule(Module &M) override {

436 getAnalysis().getBindingMap();

438 getAnalysis().getResourceTypeMap();

440 getAnalysis().getDXILResource();

442 getAnalysis().getShaderFlags();

444 getAnalysis().getModuleMetadata();

445

448 return true;

449 }

450};

451

452}

453

454char DXILTranslateMetadataLegacy::ID = 0;

455

457 return new DXILTranslateMetadataLegacy();

458}

459

461 "DXIL Translate Metadata", false, false)

Add AMDGPU uniform metadata

This file contains the declarations for the subclasses of Constant, which represent the different fla...

Module.h This file contains the declarations for the Module class.

if(auto Err=PB.parsePassPipeline(MPM, Passes)) return wrap(std MPM run * Mod

ModuleAnalysisManager MAM

#define INITIALIZE_PASS_DEPENDENCY(depName)

#define INITIALIZE_PASS_END(passName, arg, name, cfg, analysis)

#define INITIALIZE_PASS_BEGIN(passName, arg, name, cfg, analysis)

assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())

This file defines the SmallVector class.

static const FuncProtoTy Signatures[]

Defines the llvm::VersionTuple class, which represents a version in the form major[....

A container for analyses that lazily runs them and caches their results.

PassT::Result & getResult(IRUnitT &IR, ExtraArgTs... ExtraArgs)

Get the result of an analysis pass for a given IR unit.

Represent the analysis usage information of a pass.

AnalysisUsage & addRequired()

AnalysisUsage & addPreserved()

Add the specified Pass class to the set of analyses preserved by this pass.

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

LLVM Basic Block Representation.

This is the shared class of boolean and integer constants.

iterator_range< iterator > cbuffers()

iterator_range< iterator > srvs()

iterator_range< iterator > samplers()

iterator_range< iterator > uavs()

Analysis pass that exposes the DXILResource for a module.

The legacy pass manager's analysis pass to compute DXIL resource information.

This is the base abstract class for diagnostic reporting in the backend.

virtual void print(DiagnosticPrinter &DP) const =0

Print using the given DP a user-friendly message.

Interface for custom diagnostic printing.

LLVMContext & getContext() const

getContext - Return a reference to the LLVMContext associated with this function.

ConstantInt * getInt32(uint32_t C)

Get a constant 32-bit value.

This provides a uniform API for creating instructions and inserting them into a basic block: either a...

MDNode * getMetadata(unsigned KindID) const

Get the metadata of given kind attached to this Instruction.

void setMetadata(unsigned KindID, MDNode *Node)

Set the metadata of the specified kind to the specified node.

This is an important class for using LLVM in a threaded context.

ConstantAsMetadata * createConstant(Constant *C)

Return the given constant as metadata.

MDString * createString(StringRef Str)

Return the given string as metadata.

const MDOperand & getOperand(unsigned I) const

static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)

unsigned getNumOperands() const

Return number of MDNode operands.

static MDString * get(LLVMContext &Context, StringRef Str)

ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...

A Module instance is used to store all the information related to an LLVM module.

MDNode * getOperand(unsigned i) const

void clearOperands()

Drop all references to this node's operands.

void addOperand(MDNode *M)

A set of analyses that are preserved following a run of a transformation pass.

static PreservedAnalyses all()

Construct a special preserved set that preserves all passes.

reference emplace_back(ArgTypes &&... Args)

void append(ItTy in_start, ItTy in_end)

Add the specified range to the end of the SmallVector.

void push_back(const T &Elt)

This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.

StringRef - Represent a constant reference to a string, i.e.

static StringRef getEnvironmentTypeName(EnvironmentType Kind)

Get the canonical name for the Kind environment.

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

static IntegerType * getInt32Ty(LLVMContext &C)

static IntegerType * getInt64Ty(LLVMContext &C)

LLVM Value Representation.

StringRef getName() const

Return a constant reference to the value's name.

Represents a version number in the form major[.minor[.subminor[.build]]].

unsigned getMajor() const

Retrieve the major version number.

bool empty() const

Determine whether this version information is empty (e.g., all version components are zero).

std::optional< unsigned > getMinor() const

Retrieve the minor version number, if provided.

Metadata * writeUAVs(Module &M) const

Metadata * writeCBuffers(Module &M) const

Wrapper pass for the legacy pass manager.

#define llvm_unreachable(msg)

Marks that the current location is not supposed to be reachable.

unsigned ID

LLVM IR allows to use arbitrary numbers as calling convention identifiers.

This is an optimization pass for GlobalISel generic memory operations.

ModulePass * createDXILTranslateMetadataLegacyPass()

Pass to emit metadata for DXIL.

@ Mod

The access may modify the value stored in memory.

DiagnosticSeverity

Defines the different supported severity of a diagnostic.

Triple::EnvironmentType ShaderStage