Skip to content

Commit 6311180

Browse files
committed
nfc: factor out validations in translate metadata
1 parent a638740 commit 6311180

File tree

4 files changed

+41
-41
lines changed

4 files changed

+41
-41
lines changed

llvm/lib/Target/DirectX/DXILTranslateMetadata.cpp

Lines changed: 3 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -36,26 +36,6 @@ using namespace llvm;
3636
using namespace llvm::dxil;
3737

3838
namespace {
39-
/// A simple Wrapper DiagnosticInfo that generates Module-level diagnostic
40-
/// for TranslateMetadata pass
41-
class DiagnosticInfoTranslateMD : public DiagnosticInfo {
42-
private:
43-
const Twine &Msg;
44-
const Module &Mod;
45-
46-
public:
47-
/// \p M is the module for which the diagnostic is being emitted. \p Msg is
48-
/// the message to show. Note that this class does not copy this message, so
49-
/// this reference must be valid for the whole life time of the diagnostic.
50-
DiagnosticInfoTranslateMD(const Module &M,
51-
const Twine &Msg LLVM_LIFETIME_BOUND,
52-
DiagnosticSeverity Severity = DS_Error)
53-
: DiagnosticInfo(DK_Unsupported, Severity), Msg(Msg), Mod(M) {}
54-
55-
void print(DiagnosticPrinter &DP) const override {
56-
DP << Mod.getName() << ": " << Msg << '\n';
57-
}
58-
};
5939

6040
enum class EntryPropsTag {
6141
ShaderFlags = 0,
@@ -391,9 +371,6 @@ static void translateGlobalMetadata(Module &M, DXILResourceMap &DRM,
391371
uint64_t CombinedMask = ShaderFlags.getCombinedFlags();
392372
EntryFnMDNodes.emplace_back(
393373
emitTopLevelLibraryNode(M, ResourceMD, CombinedMask));
394-
} else if (MMDI.EntryPropertyVec.size() > 1) {
395-
M.getContext().diagnose(DiagnosticInfoTranslateMD(
396-
M, "Non-library shader: One and only one entry expected"));
397374
}
398375

399376
for (const EntryProperties &EntryProp : MMDI.EntryPropertyVec) {
@@ -402,20 +379,9 @@ static void translateGlobalMetadata(Module &M, DXILResourceMap &DRM,
402379

403380
// If ShaderProfile is Library, mask is already consolidated in the
404381
// top-level library node. Hence it is not emitted.
405-
uint64_t EntryShaderFlags = 0;
406-
if (MMDI.ShaderProfile != Triple::EnvironmentType::Library) {
407-
EntryShaderFlags = EntrySFMask;
408-
if (EntryProp.ShaderStage != MMDI.ShaderProfile) {
409-
M.getContext().diagnose(DiagnosticInfoTranslateMD(
410-
M,
411-
"Shader stage '" +
412-
Twine(getShortShaderStage(EntryProp.ShaderStage) +
413-
"' for entry '" + Twine(EntryProp.Entry->getName()) +
414-
"' different from specified target profile '" +
415-
Twine(Triple::getEnvironmentTypeName(MMDI.ShaderProfile) +
416-
"'"))));
417-
}
418-
}
382+
uint64_t EntryShaderFlags =
383+
MMDI.ShaderProfile == Triple::EnvironmentType::Library ? 0
384+
: EntrySFMask;
419385
EntryFnMDNodes.emplace_back(emitEntryMD(EntryProp, Signatures, ResourceMD,
420386
EntryShaderFlags,
421387
MMDI.ShaderProfile));

llvm/lib/Target/DirectX/DXILValidateMetadata.cpp

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "DXILTranslateMetadata.h"
1111
#include "DirectX.h"
1212
#include "llvm/ADT/Twine.h"
13+
#include "llvm/Analysis/DXILMetadataAnalysis.h"
1314
#include "llvm/IR/BasicBlock.h"
1415
#include "llvm/IR/DiagnosticInfo.h"
1516
#include "llvm/IR/DiagnosticPrinter.h"
@@ -43,14 +44,43 @@ class DiagnosticInfoValidateMD : public DiagnosticInfo {
4344
}
4445
};
4546

47+
static bool reportError(Module &M, Twine Message,
48+
DiagnosticSeverity Severity = DS_Error) {
49+
M.getContext().diagnose(DiagnosticInfoValidateMD(M, Message, Severity));
50+
return true;
51+
}
52+
4653
} // namespace
4754

4855
static void validateInstructionMetadata(Module &M) {
4956
llvm::errs() << "hello from new pass!\n";
5057
}
5158

59+
static void validateGlobalMetadata(Module &M,
60+
const dxil::ModuleMetadataInfo &MMDI) {
61+
if (MMDI.ShaderProfile != Triple::EnvironmentType::Library) {
62+
if (1 < MMDI.EntryPropertyVec.size())
63+
reportError(M, "Non-library shader: One and only one entry expected");
64+
65+
for (const dxil::EntryProperties &EntryProp : MMDI.EntryPropertyVec)
66+
if (EntryProp.ShaderStage != MMDI.ShaderProfile)
67+
reportError(
68+
M,
69+
"Shader stage '" +
70+
Twine(Twine(Triple::getEnvironmentTypeName(
71+
EntryProp.ShaderStage)) +
72+
"' for entry '" + Twine(EntryProp.Entry->getName()) +
73+
"' different from specified target profile '" +
74+
Twine(Triple::getEnvironmentTypeName(MMDI.ShaderProfile) +
75+
"'")));
76+
}
77+
}
78+
5279
PreservedAnalyses DXILValidateMetadata::run(Module &M,
5380
ModuleAnalysisManager &MAM) {
81+
82+
const dxil::ModuleMetadataInfo MMDI = MAM.getResult<DXILMetadataAnalysis>(M);
83+
validateGlobalMetadata(M, MMDI);
5484
validateInstructionMetadata(M);
5585

5686
return PreservedAnalyses::all();
@@ -65,12 +95,15 @@ class DXILValidateMetadataLegacy : public ModulePass {
6595
StringRef getPassName() const override { return "DXIL Validate Metadata"; }
6696

6797
void getAnalysisUsage(AnalysisUsage &AU) const override {
98+
AU.addRequired<DXILMetadataAnalysisWrapperPass>();
6899
AU.addRequired<DXILTranslateMetadataLegacy>();
69100
AU.setPreservesAll();
70101
}
71102

72103
bool runOnModule(Module &M) override {
73-
validateInstructionMetadata();
104+
dxil::ModuleMetadataInfo MMDI =
105+
getAnalysis<DXILMetadataAnalysisWrapperPass>().getModuleMetadata();
106+
validateGlobalMetadata(M, MMDI);
74107
return true;
75108
}
76109
};
@@ -85,6 +118,7 @@ ModulePass *llvm::createDXILValidateMetadataLegacyPass() {
85118

86119
INITIALIZE_PASS_BEGIN(DXILValidateMetadataLegacy, "dxil-validate-metadata",
87120
"DXIL Validate Metadata", false, false)
121+
INITIALIZE_PASS_DEPENDENCY(DXILMetadataAnalysisWrapperPass)
88122
INITIALIZE_PASS_DEPENDENCY(DXILTranslateMetadataLegacy)
89123
INITIALIZE_PASS_END(DXILValidateMetadataLegacy, "dxil-validate-metadata",
90124
"DXIL validate Metadata", false, false)

llvm/test/CodeGen/DirectX/Metadata/multiple-entries-cs-error.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
; RUN: not opt -S -S -dxil-translate-metadata %s 2>&1 | FileCheck %s
1+
; RUN: not opt -S -dxil-validate-metadata %s 2>&1 | FileCheck %s
22
target triple = "dxil-pc-shadermodel6.8-compute"
33

44
; CHECK: Non-library shader: One and only one entry expected

llvm/test/CodeGen/DirectX/Metadata/target-profile-error.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
; RUN: not opt -S -dxil-translate-metadata %s 2>&1 | FileCheck %s
1+
; RUN: not opt -S -dxil-validate-metadata %s 2>&1 | FileCheck %s
22

33
target triple = "dxil-pc-shadermodel6.6-pixel"
44

5-
; CHECK: Shader stage 'cs' for entry 'entry' different from specified target profile 'pixel'
5+
; CHECK: Shader stage 'compute' for entry 'entry' different from specified target profile 'pixel'
66

77
define void @entry() #0 {
88
entry:

0 commit comments

Comments
 (0)