Skip to content

Commit 5aac761

Browse files
author
joaosaffran
committed
improve error handling
1 parent 93f7c4c commit 5aac761

File tree

9 files changed

+61
-62
lines changed

9 files changed

+61
-62
lines changed

llvm/include/llvm/BinaryFormat/DXContainer.h

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -550,16 +550,12 @@ static_assert(sizeof(ProgramSignatureElement) == 32,
550550

551551
struct RootSignatureValidations {
552552

553-
static Expected<uint32_t> validateRootFlag(uint32_t Flags) {
554-
if ((Flags & ~0x80000fff) != 0)
555-
return llvm::make_error<BinaryStreamError>("Invalid flag");
556-
return Flags;
553+
static bool validateRootFlag(uint32_t Flags) {
554+
return (Flags & ~0x80000fff) != 0;
557555
}
558556

559-
static Expected<uint32_t> validateVersion(uint32_t Version) {
560-
if (Version < 1 || Version > 2)
561-
return llvm::make_error<BinaryStreamError>("Invalid Version");
562-
return Version;
557+
static bool validateVersion(uint32_t Version) {
558+
return (Version < 1 || Version > 2);
563559
}
564560
};
565561

llvm/lib/Object/DXContainer.cpp

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include "llvm/Object/Error.h"
1212
#include "llvm/Support/Alignment.h"
1313
#include "llvm/Support/Endian.h"
14+
#include "llvm/Support/Error.h"
1415
#include "llvm/Support/FormatVariadic.h"
1516

1617
using namespace llvm;
@@ -254,11 +255,9 @@ Error DirectX::RootSignature::parse(StringRef Data) {
254255
support::endian::read<uint32_t, llvm::endianness::little>(Current);
255256
Current += sizeof(uint32_t);
256257

257-
Expected<uint32_t> MaybeVersion =
258-
dxbc::RootSignatureValidations::validateVersion(VValue);
259-
if (Error E = MaybeVersion.takeError())
260-
return E;
261-
Version = MaybeVersion.get();
258+
if (dxbc::RootSignatureValidations::validateVersion(VValue))
259+
return make_error<GenericBinaryError>("Invalid Version");
260+
Version = VValue;
262261

263262
NumParameters =
264263
support::endian::read<uint32_t, llvm::endianness::little>(Current);
@@ -280,11 +279,9 @@ Error DirectX::RootSignature::parse(StringRef Data) {
280279
support::endian::read<uint32_t, llvm::endianness::little>(Current);
281280
Current += sizeof(uint32_t);
282281

283-
Expected<uint32_t> MaybeFlag =
284-
dxbc::RootSignatureValidations::validateRootFlag(FValue);
285-
if (Error E = MaybeFlag.takeError())
286-
return E;
287-
Flags = MaybeFlag.get();
282+
if (dxbc::RootSignatureValidations::validateRootFlag(FValue))
283+
return make_error<GenericBinaryError>("Invalid flag");
284+
Flags = FValue;
288285

289286
return Error::success();
290287
}

llvm/lib/Target/DirectX/DXContainerGlobals.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -152,16 +152,18 @@ void DXContainerGlobals::addSignature(Module &M,
152152
void DXContainerGlobals::addRootSignature(Module &M,
153153
SmallVector<GlobalValue *> &Globals) {
154154

155-
std::optional<ModuleRootSignature> MRS =
156-
getAnalysis<RootSignatureAnalysisWrapper>().getRootSignature();
157-
if (!MRS.has_value())
155+
auto &RSA = getAnalysis<RootSignatureAnalysisWrapper>();
156+
157+
if (!RSA.hasRootSignature())
158158
return;
159159

160+
ModuleRootSignature MRS = RSA.getRootSignature();
161+
160162
SmallString<256> Data;
161163
raw_svector_ostream OS(Data);
162164

163165
RootSignatureHeader RSH;
164-
RSH.Flags = MRS->Flags;
166+
RSH.Flags = MRS.Flags;
165167

166168
RSH.write(OS);
167169

llvm/lib/Target/DirectX/DXILRootSignature.cpp

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,23 @@
1515
#include "llvm/ADT/StringSwitch.h"
1616
#include "llvm/ADT/Twine.h"
1717
#include "llvm/Analysis/DXILMetadataAnalysis.h"
18+
#include "llvm/BinaryFormat/DXContainer.h"
1819
#include "llvm/IR/Constants.h"
20+
#include "llvm/IR/DiagnosticInfo.h"
1921
#include "llvm/IR/Function.h"
22+
#include "llvm/IR/LLVMContext.h"
2023
#include "llvm/IR/Module.h"
2124
#include "llvm/InitializePasses.h"
2225
#include "llvm/Pass.h"
26+
#include "llvm/Support/Error.h"
27+
#include <optional>
2328

2429
using namespace llvm;
2530
using namespace llvm::dxil;
2631

27-
static bool reportError(Twine Message) {
28-
report_fatal_error(Message, false);
32+
bool ModuleRootSignature::reportError(Twine Message,
33+
DiagnosticSeverity Severity) {
34+
Ctx->diagnose(DiagnosticInfoGeneric(Message, Severity));
2935
return true;
3036
}
3137

@@ -130,43 +136,33 @@ bool ModuleRootSignature::parse(NamedMDNode *Root, const Function *EF) {
130136
return HasError;
131137
}
132138

133-
bool ModuleRootSignature::validateRootFlag() {
134-
// Root Element validation, as specified:
135-
// https://github.com/llvm/wg-hlsl/blob/main/proposals/0002-root-signature-in-clang.md#validations-during-dxil-generation
136-
if ((Flags & ~0x80000fff) != 0)
137-
return reportError("Invalid flag value for RootFlag");
138-
139-
return false;
140-
}
141-
142139
bool ModuleRootSignature::validate() {
143-
if (validateRootFlag())
140+
if (dxbc::RootSignatureValidations::validateRootFlag(Flags)) {
144141
return reportError("Invalid flag value for RootFlag");
145-
142+
}
146143
return false;
147144
}
148145

149-
ModuleRootSignature ModuleRootSignature::analyzeModule(Module &M,
150-
const Function *F) {
151-
ModuleRootSignature MRS;
146+
OptionalRootSignature ModuleRootSignature::analyzeModule(Module &M,
147+
const Function *F) {
148+
ModuleRootSignature MRS(&M.getContext());
152149

153150
NamedMDNode *RootSignatureNode = M.getNamedMetadata("dx.rootsignatures");
154-
if (RootSignatureNode) {
155-
if (MRS.parse(RootSignatureNode, F) || MRS.validate())
156-
llvm_unreachable("Invalid Root Signature Metadata.");
157-
}
151+
if (RootSignatureNode == nullptr || MRS.parse(RootSignatureNode, F) ||
152+
MRS.validate())
153+
return std::nullopt;
158154

159155
return MRS;
160156
}
161157

162158
AnalysisKey RootSignatureAnalysis::Key;
163159

164-
ModuleRootSignature RootSignatureAnalysis::run(Module &M,
165-
ModuleAnalysisManager &AM) {
160+
OptionalRootSignature RootSignatureAnalysis::run(Module &M,
161+
ModuleAnalysisManager &AM) {
166162
auto MMI = AM.getResult<DXILMetadataAnalysis>(M);
167163

168164
if (MMI.ShaderProfile == Triple::Library)
169-
return ModuleRootSignature();
165+
return std::nullopt;
170166

171167
assert(MMI.EntryPropertyVec.size() == 1);
172168

@@ -186,7 +182,6 @@ bool RootSignatureAnalysisWrapper::runOnModule(Module &M) {
186182

187183
const Function *EntryFunction = MMI.EntryPropertyVec[0].Entry;
188184
MRS = ModuleRootSignature::analyzeModule(M, EntryFunction);
189-
190185
return false;
191186
}
192187

llvm/lib/Target/DirectX/DXILRootSignature.h

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,13 @@
1212
///
1313
//===----------------------------------------------------------------------===//
1414

15+
#include "llvm/IR/DiagnosticInfo.h"
1516
#include "llvm/IR/Metadata.h"
17+
#include "llvm/IR/Module.h"
1618
#include "llvm/IR/PassManager.h"
1719
#include "llvm/Pass.h"
1820
#include <optional>
21+
#include <utility>
1922

2023
namespace llvm {
2124
namespace dxil {
@@ -31,44 +34,52 @@ enum class RootSignatureElementKind {
3134

3235
struct ModuleRootSignature {
3336
uint32_t Flags = 0;
34-
35-
ModuleRootSignature() = default;
36-
static ModuleRootSignature analyzeModule(Module &M, const Function *F);
37+
ModuleRootSignature() { Ctx = nullptr; };
38+
ModuleRootSignature(LLVMContext *Ctx) : Ctx(Ctx) {}
39+
static std::optional<ModuleRootSignature> analyzeModule(Module &M,
40+
const Function *F);
3741

3842
private:
43+
LLVMContext *Ctx;
44+
3945
bool parse(NamedMDNode *Root, const Function *F);
4046
bool parseRootSignatureElement(MDNode *Element);
4147
bool parseRootFlags(MDNode *RootFlagNode);
4248

4349
bool validate();
44-
bool validateRootFlag();
50+
51+
bool reportError(Twine Message, DiagnosticSeverity Severity = DS_Error);
4552
};
4653

54+
using OptionalRootSignature = std::optional<ModuleRootSignature>;
55+
4756
class RootSignatureAnalysis : public AnalysisInfoMixin<RootSignatureAnalysis> {
4857
friend AnalysisInfoMixin<RootSignatureAnalysis>;
4958
static AnalysisKey Key;
5059

5160
public:
5261
RootSignatureAnalysis() = default;
5362

54-
using Result = ModuleRootSignature;
63+
using Result = OptionalRootSignature;
5564

56-
ModuleRootSignature run(Module &M, ModuleAnalysisManager &AM);
65+
OptionalRootSignature run(Module &M, ModuleAnalysisManager &AM);
5766
};
5867

5968
/// Wrapper pass for the legacy pass manager.
6069
///
6170
/// This is required because the passes that will depend on this are codegen
6271
/// passes which run through the legacy pass manager.
6372
class RootSignatureAnalysisWrapper : public ModulePass {
64-
std::optional<ModuleRootSignature> MRS;
73+
private:
74+
OptionalRootSignature MRS;
6575

6676
public:
6777
static char ID;
6878

6979
RootSignatureAnalysisWrapper() : ModulePass(ID) {}
7080

71-
const std::optional<ModuleRootSignature> &getRootSignature() { return MRS; }
81+
const ModuleRootSignature &getRootSignature() { return MRS.value(); }
82+
bool hasRootSignature() { return MRS.has_value(); }
7283

7384
bool runOnModule(Module &M) override;
7485

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Error.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
target triple = "dxil-unknown-shadermodel6.0-compute"
44

5-
; CHECK: LLVM ERROR: Invalid format for Root Signature Definition. Pairs of function, root signature expected.
5+
; CHECK: error: Invalid format for Root Signature Definition. Pairs of function, root signature expected.
66

77

88
define void @main() #0 {

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags-Error.ll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
; RUN: not llc %s --filetype=obj -o - 2>&1 | FileCheck %s
1+
; RUN: not llc %s --filetype=obj -o -
22

33
target triple = "dxil-unknown-shadermodel6.0-compute"
44

5-
; CHECK: LLVM ERROR: Invalid Root Element: NOTRootFlags
5+
; expected-error@-1: Invalid Root Element: NOTRootFlags
66

77

88
define void @main() #0 {

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-Flags-Validation-Error.ll

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
; RUN: not llc %s --filetype=obj -o - 2>&1 | FileCheck %s
2-
1+
; RUN: not llc %s --filetype=obj -o -
32
target triple = "dxil-unknown-shadermodel6.0-compute"
4-
5-
; CHECK: LLVM ERROR: Invalid flag value for RootFlag
3+
; expected-error@-1: Invalid flag value for RootFlag
64

75

86
define void @main() #0 {

llvm/test/CodeGen/DirectX/ContainerData/RootSignature-RootElement-Error.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
target triple = "dxil-unknown-shadermodel6.0-compute"
44

5-
; CHECK: LLVM ERROR: Missing Root Element Metadata Node.
5+
; CHECK: error: Missing Root Element Metadata Node.
66

77

88
define void @main() #0 {

0 commit comments

Comments
 (0)