LLVM  15.0.0git
DXILTranslateMetadata.cpp
Go to the documentation of this file.
1 //===- DXILTranslateMetadata.cpp - Pass to emit DXIL metadata ---*- 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 //===----------------------------------------------------------------------===//
10 
11 #include "DirectX.h"
12 #include "llvm/ADT/StringSet.h"
13 #include "llvm/ADT/Triple.h"
14 #include "llvm/IR/Constants.h"
15 #include "llvm/IR/Metadata.h"
16 #include "llvm/IR/Module.h"
17 #include "llvm/Pass.h"
18 
19 using namespace llvm;
20 
21 static uint32_t ConstMDToUint32(const MDOperand &MDO) {
22  ConstantInt *pConst = mdconst::extract<ConstantInt>(MDO);
23  return (uint32_t)pConst->getZExtValue();
24 }
25 
26 static ConstantAsMetadata *Uint32ToConstMD(unsigned v, LLVMContext &Ctx) {
29 }
30 
31 constexpr StringLiteral ValVerKey = "dx.valver";
32 constexpr unsigned DXILVersionNumFields = 2;
33 
34 static void emitDXILValidatorVersion(Module &M, VersionTuple &ValidatorVer) {
35  NamedMDNode *DXILValidatorVersionMD = M.getNamedMetadata(ValVerKey);
36 
37  // Allow re-writing the validator version, since this can be changed at
38  // later points.
39  if (DXILValidatorVersionMD)
40  M.eraseNamedMetadata(DXILValidatorVersionMD);
41 
42  DXILValidatorVersionMD = M.getOrInsertNamedMetadata(ValVerKey);
43 
44  auto &Ctx = M.getContext();
46  MDVals[0] = Uint32ToConstMD(ValidatorVer.getMajor(), Ctx);
47  MDVals[1] = Uint32ToConstMD(ValidatorVer.getMinor().value_or(0), Ctx);
48 
49  DXILValidatorVersionMD->addOperand(MDNode::get(Ctx, MDVals));
50 }
51 
53  if (ValVerMD->getNumOperands() != DXILVersionNumFields)
54  return VersionTuple();
55 
56  unsigned Major = ConstMDToUint32(ValVerMD->getOperand(0));
57  unsigned Minor = ConstMDToUint32(ValVerMD->getOperand(1));
58  return VersionTuple(Major, Minor);
59 }
60 
61 static void cleanModuleFlags(Module &M) {
62  constexpr StringLiteral DeadKeys[] = {ValVerKey};
63  // Collect DeadKeys in ModuleFlags.
64  StringSet<> DeadKeySet;
65  for (auto &Key : DeadKeys) {
66  if (M.getModuleFlag(Key))
67  DeadKeySet.insert(Key);
68  }
69  if (DeadKeySet.empty())
70  return;
71 
73  M.getModuleFlagsMetadata(ModuleFlags);
74  NamedMDNode *MDFlags = M.getModuleFlagsMetadata();
75  MDFlags->eraseFromParent();
76  // Add ModuleFlag which not dead.
77  for (auto &Flag : ModuleFlags) {
78  StringRef Key = Flag.Key->getString();
79  if (DeadKeySet.contains(Key))
80  continue;
81  M.addModuleFlag(Flag.Behavior, Key, Flag.Val);
82  }
83 }
84 
85 static void cleanModule(Module &M) { cleanModuleFlags(M); }
86 
87 namespace {
88 class DXILTranslateMetadata : public ModulePass {
89 public:
90  static char ID; // Pass identification, replacement for typeid
91  explicit DXILTranslateMetadata() : ModulePass(ID), ValidatorVer(1, 0) {}
92 
93  StringRef getPassName() const override { return "DXIL Metadata Emit"; }
94 
95  bool runOnModule(Module &M) override;
96 
97 private:
98  VersionTuple ValidatorVer;
99 };
100 
101 } // namespace
102 
103 bool DXILTranslateMetadata::runOnModule(Module &M) {
104  if (MDNode *ValVerMD = cast_or_null<MDNode>(M.getModuleFlag(ValVerKey))) {
105  auto ValVer = loadDXILValidatorVersion(ValVerMD);
106  if (!ValVer.empty())
107  ValidatorVer = ValVer;
108  }
109  emitDXILValidatorVersion(M, ValidatorVer);
110  cleanModule(M);
111  return false;
112 }
113 
115 
117  return new DXILTranslateMetadata();
118 }
119 
120 INITIALIZE_PASS(DXILTranslateMetadata, "dxil-metadata-emit",
121  "DXIL Metadata Emit", false, false)
llvm
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:17
M
We currently emits eax Perhaps this is what we really should generate is Is imull three or four cycles eax eax The current instruction priority is based on pattern complexity The former is more complex because it folds a load so the latter will not be emitted Perhaps we should use AddedComplexity to give LEA32r a higher priority We should always try to match LEA first since the LEA matching code does some estimate to determine whether the match is profitable if we care more about code then imull is better It s two bytes shorter than movl leal On a Pentium M
Definition: README.txt:252
llvm::NamedMDNode
A tuple of MDNodes.
Definition: Metadata.h:1488
Metadata.h
llvm::ModulePass
ModulePass class - This class is used to implement unstructured interprocedural optimizations and ana...
Definition: Pass.h:248
Pass.h
llvm::SmallVector
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1185
Module.h
llvm::ConstantAsMetadata::get
static ConstantAsMetadata * get(Constant *C)
Definition: Metadata.h:420
llvm::StringSet::insert
std::pair< typename Base::iterator, bool > insert(StringRef key)
Definition: StringSet.h:34
ConstMDToUint32
static uint32_t ConstMDToUint32(const MDOperand &MDO)
Definition: DXILTranslateMetadata.cpp:21
llvm::MDNode::get
static MDTuple * get(LLVMContext &Context, ArrayRef< Metadata * > MDs)
Definition: Metadata.h:1300
llvm::VersionTuple::getMajor
unsigned getMajor() const
Retrieve the major version number.
Definition: VersionTuple.h:72
llvm::MDNode::getNumOperands
unsigned getNumOperands() const
Return number of MDNode operands.
Definition: Metadata.h:1210
llvm::NamedMDNode::addOperand
void addOperand(MDNode *M)
Definition: Metadata.cpp:1160
llvm::ConstantInt
This is the shared class of boolean and integer constants.
Definition: Constants.h:79
llvm::StringLiteral
A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...
Definition: StringRef.h:914
DirectX.h
llvm::createDXILTranslateMetadataPass
ModulePass * createDXILTranslateMetadataPass()
Pass to emit metadata for DXIL.
Definition: DXILTranslateMetadata.cpp:116
Constants.h
INITIALIZE_PASS
#define INITIALIZE_PASS(passName, arg, name, cfg, analysis)
Definition: PassSupport.h:37
cleanModuleFlags
static void cleanModuleFlags(Module &M)
Definition: DXILTranslateMetadata.cpp:61
llvm::AMDGPU::PALMD::Key
Key
PAL metadata keys.
Definition: AMDGPUMetadata.h:486
llvm::StringSet::contains
bool contains(StringRef key) const
Check if the set contains the given key.
Definition: StringSet.h:51
llvm::MCID::Flag
Flag
These should be considered private to the implementation of the MCInstrDesc class.
Definition: MCInstrDesc.h:147
llvm::NamedMDNode::eraseFromParent
void eraseFromParent()
Drop all references and remove the node from parent module.
Definition: Metadata.cpp:1167
llvm::VersionTuple
Represents a version number in the form major[.minor[.subminor[.build]]].
Definition: VersionTuple.h:31
loadDXILValidatorVersion
static VersionTuple loadDXILValidatorVersion(MDNode *ValVerMD)
Definition: DXILTranslateMetadata.cpp:52
llvm::Metadata
Root of the metadata hierarchy.
Definition: Metadata.h:62
emitDXILValidatorVersion
static void emitDXILValidatorVersion(Module &M, VersionTuple &ValidatorVer)
Definition: DXILTranslateMetadata.cpp:34
llvm::CallingConv::ID
unsigned ID
LLVM IR allows to use arbitrary numbers as calling convention identifiers.
Definition: CallingConv.h:24
cleanModule
static void cleanModule(Module &M)
Definition: DXILTranslateMetadata.cpp:85
llvm::MDNode::getOperand
const MDOperand & getOperand(unsigned I) const
Definition: Metadata.h:1204
llvm::LLVMContext
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:68
llvm::VersionTuple::getMinor
Optional< unsigned > getMinor() const
Retrieve the minor version number, if provided.
Definition: VersionTuple.h:75
llvm::Module
A Module instance is used to store all the information related to an LLVM module.
Definition: Module.h:65
llvm::MDNode
Metadata node.
Definition: Metadata.h:937
llvm::APInt
Class for arbitrary precision integers.
Definition: APInt.h:75
Triple.h
llvm::StringSet
StringSet - A wrapper for StringMap that provides set-like functionality.
Definition: StringSet.h:23
llvm::StringRef
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:58
uint32_t
llvm::ConstantInt::getZExtValue
uint64_t getZExtValue() const
Return the constant as a 64-bit unsigned integer value after it has been zero extended as appropriate...
Definition: Constants.h:142
StringSet.h
llvm::ConstantAsMetadata
Definition: Metadata.h:413
DXILVersionNumFields
constexpr unsigned DXILVersionNumFields
Definition: DXILTranslateMetadata.cpp:32
llvm::Constant::getIntegerValue
static Constant * getIntegerValue(Type *Ty, const APInt &V)
Return the value for an integer or pointer constant, or a vector thereof, with the given scalar value...
Definition: Constants.cpp:378
llvm::StringMapImpl::empty
bool empty() const
Definition: StringMap.h:94
llvm::IntegerType::get
static IntegerType * get(LLVMContext &C, unsigned NumBits)
This static method is the primary way of constructing an IntegerType.
Definition: Type.cpp:311
ValVerKey
constexpr StringLiteral ValVerKey
Definition: DXILTranslateMetadata.cpp:31
llvm::Optional::value_or
constexpr T value_or(U &&alt) const &
Definition: Optional.h:318
Uint32ToConstMD
static ConstantAsMetadata * Uint32ToConstMD(unsigned v, LLVMContext &Ctx)
Definition: DXILTranslateMetadata.cpp:26
llvm::MDOperand
Tracking metadata reference owned by Metadata.
Definition: Metadata.h:773
llvm::Intrinsic::ID
unsigned ID
Definition: TargetTransformInfo.h:38