[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-07-11 Thread Deric C. via llvm-branch-commits

https://github.com/Icohedron dismissed 
https://github.com/llvm/llvm-project/pull/144577
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-07-11 Thread Chris B via llvm-branch-commits


@@ -48,6 +48,71 @@ static bool reportValueError(LLVMContext *Ctx, Twine 
ParamName,
   return true;
 }
 
+// Template function to get formatted type string based on C++ type
+template  std::string getTypeFormatted() {
+  if constexpr (std::is_same_v) {
+return "string";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "metadata";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "constant";
+  } else if constexpr (std::is_same_v) {
+return "constant";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "constant int";
+  } else if constexpr (std::is_same_v) {
+return "constant int";
+  }
+  return "unknown";
+}
+
+// Helper function to get the actual type of a metadata operand
+std::string getActualMDType(const MDNode *Node, unsigned Index) {
+  if (!Node || Index >= Node->getNumOperands())
+return "null";
+
+  Metadata *Op = Node->getOperand(Index);
+  if (!Op)
+return "null";
+
+  if (isa(Op))
+return getTypeFormatted();
+
+  if (isa(Op)) {
+if (auto *CAM = dyn_cast(Op)) {
+  Type *T = CAM->getValue()->getType();
+  if (T->isIntegerTy())
+return (Twine("i") + Twine(T->getIntegerBitWidth())).str();
+  if (T->isFloatingPointTy())
+return T->isFloatTy()? getTypeFormatted()
+   : T->isDoubleTy() ? getTypeFormatted()
+ : "fp";
+
+  return getTypeFormatted();
+}
+  }
+  if (isa(Op))
+return getTypeFormatted();
+
+  return "unknown";
+}
+
+// Helper function to simplify error reporting for invalid metadata values
+template 
+auto reportInvalidTypeError(LLVMContext *Ctx, Twine ParamName,

llvm-beanz wrote:

`auto` as a return type rarely meets LLVM's coding standards:
see: 
https://llvm.org/docs/CodingStandards.html#use-auto-type-deduction-to-make-code-more-readable

https://github.com/llvm/llvm-project/pull/144577
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-07-11 Thread Chris B via llvm-branch-commits


@@ -48,6 +48,71 @@ static bool reportValueError(LLVMContext *Ctx, Twine 
ParamName,
   return true;
 }
 
+// Template function to get formatted type string based on C++ type
+template  std::string getTypeFormatted() {
+  if constexpr (std::is_same_v) {
+return "string";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "metadata";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "constant";
+  } else if constexpr (std::is_same_v) {
+return "constant";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "constant int";
+  } else if constexpr (std::is_same_v) {
+return "constant int";
+  }
+  return "unknown";
+}
+
+// Helper function to get the actual type of a metadata operand
+std::string getActualMDType(const MDNode *Node, unsigned Index) {

llvm-beanz wrote:

```suggestion
StringRef getActualMDType(const MDNode *Node, unsigned Index) {
```

https://github.com/llvm/llvm-project/pull/144577
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-07-11 Thread Chris B via llvm-branch-commits


@@ -48,6 +48,71 @@ static bool reportValueError(LLVMContext *Ctx, Twine 
ParamName,
   return true;
 }
 
+// Template function to get formatted type string based on C++ type
+template  std::string getTypeFormatted() {

llvm-beanz wrote:

Looks like this could be a StringRef.
```suggestion
template  StringRef getTypeFormatted() {
```

https://github.com/llvm/llvm-project/pull/144577
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-07-04 Thread Deric C. via llvm-branch-commits

https://github.com/Icohedron approved this pull request.


https://github.com/llvm/llvm-project/pull/144577
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-06-19 Thread via llvm-branch-commits

https://github.com/joaosaffran edited 
https://github.com/llvm/llvm-project/pull/144577
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-06-17 Thread via llvm-branch-commits

llvmbot wrote:




@llvm/pr-subscribers-backend-directx

Author: None (joaosaffran)


Changes

This patch enhances error handling and validation in the DirectX backend's root 
signature parsing. The changes include:

1. **Improved Error Reporting**:
   - Introduced `reportInvalidTypeError` utility to provide detailed error 
messages for type mismatches.
   - Enhanced diagnostic messages for invalid metadata nodes and values.

2. **Validation Updates**:
   - Added stricter validation for descriptor tables and static samplers.
   - Improved handling of invalid values for filter modes, address modes, and 
LOD parameters.

Example changes:
```cpp
if (Element == nullptr)
  return reportInvalidTypeError(Ctx, "DescriptorTableNode", 
DescriptorTableNode, I);

if (std::optional Val = extractMdIntValue(StaticSamplerNode, 1))
  Sampler.Filter = *Val;
else
  return reportInvalidTypeError(Ctx, "StaticSamplerNode", 
StaticSamplerNode, 1);
  ```
Testing:
- Validation of invalid metadata nodes and values.
- Proper diagnostic messages for type mismatches.
- All existing DirectX backend tests continue to pass.


---
Full diff: https://github.com/llvm/llvm-project/pull/144577.diff


4 Files Affected:

- (modified) llvm/lib/Target/DirectX/DXILRootSignature.cpp (+125-31) 
- (modified) 
llvm/test/CodeGen/DirectX/ContainerData/RootSignature-RootConstants-Invalid-Num32BitValues.ll
 (+1-1) 
- (modified) 
llvm/test/CodeGen/DirectX/ContainerData/RootSignature-RootConstants-Invalid-RegisterSpace.ll
 (+1-1) 
- (modified) 
llvm/test/CodeGen/DirectX/ContainerData/RootSignature-RootConstants-Invalid-ShaderRegister.ll
 (+1-1) 


``diff
diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp 
b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 3aef7d3eb1e69..57d5ee8ac467c 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -48,6 +48,71 @@ static bool reportValueError(LLVMContext *Ctx, Twine 
ParamName,
   return true;
 }
 
+// Template function to get formatted type string based on C++ type
+template  std::string getTypeFormatted() {
+  if constexpr (std::is_same_v) {
+return "string";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "metadata";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "constant";
+  } else if constexpr (std::is_same_v) {
+return "constant";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "constant int";
+  } else if constexpr (std::is_same_v) {
+return "constant int";
+  }
+  return "unknown";
+}
+
+// Helper function to get the actual type of a metadata operand
+std::string getActualMDType(const MDNode *Node, unsigned Index) {
+  if (!Node || Index >= Node->getNumOperands())
+return "null";
+
+  Metadata *Op = Node->getOperand(Index);
+  if (!Op)
+return "null";
+
+  if (isa(Op))
+return getTypeFormatted();
+
+  if (isa(Op)) {
+if (auto *CAM = dyn_cast(Op)) {
+  Type *T = CAM->getValue()->getType();
+  if (T->isIntegerTy())
+return (Twine("i") + Twine(T->getIntegerBitWidth())).str();
+  if (T->isFloatingPointTy())
+return T->isFloatTy()? getTypeFormatted()
+   : T->isDoubleTy() ? getTypeFormatted()
+ : "fp";
+
+  return getTypeFormatted();
+}
+  }
+  if (isa(Op))
+return getTypeFormatted();
+
+  return "unknown";
+}
+
+// Helper function to simplify error reporting for invalid metadata values
+template 
+auto reportInvalidTypeError(LLVMContext *Ctx, Twine ParamName,
+const MDNode *Node, unsigned Index) {
+  std::string ExpectedType = getTypeFormatted();
+  std::string ActualType = getActualMDType(Node, Index);
+
+  return reportError(Ctx, "Root Signature Node: " + ParamName +
+  " expected metadata node of type " +
+  ExpectedType + " at index " + Twine(Index) +
+  " but got " + ActualType);
+}
+
 static std::optional extractMdIntValue(MDNode *Node,
  unsigned int OpId) {
   if (auto *CI =
@@ -80,7 +145,8 @@ static bool parseRootFlags(LLVMContext *Ctx, 
mcdxbc::RootSignatureDesc &RSD,
   if (std::optional Val = extractMdIntValue(RootFlagNode, 1))
 RSD.Flags = *Val;
   else
-return reportError(Ctx, "Invalid value for RootFlag");
+return reportInvalidTypeError(Ctx, "RootFlagNode",
+   RootFlagNode, 1);
 
   return false;
 }
@@ -100,23 +166,27 @@ static bool parseRootConstants(LLVMContext *Ctx, 
mcdxbc::RootSignatureDesc &RSD,
   if (std::optional Val = extractMdIntValue(RootConstantNode, 1))
 Header.ShaderVisibility = *Val;
   else
-return reportError(Ctx, "Invalid value for ShaderVisibility");
+return reportInvalidTypeError(Ctx, "Ro

[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-06-17 Thread via llvm-branch-commits

https://github.com/joaosaffran ready_for_review 
https://github.com/llvm/llvm-project/pull/144577
___
llvm-branch-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-branch-commits


[llvm-branch-commits] [llvm] [DirectX] Improve error handling and validation in root signature parsing (PR #144577)

2025-06-17 Thread via llvm-branch-commits

https://github.com/joaosaffran created 
https://github.com/llvm/llvm-project/pull/144577

This patch enhances error handling and validation in the DirectX backend's root 
signature parsing. The changes include:

1. **Improved Error Reporting**:
   - Introduced `reportInvalidTypeError` utility to provide detailed error 
messages for type mismatches.
   - Enhanced diagnostic messages for invalid metadata nodes and values.

2. **Validation Updates**:
   - Added stricter validation for descriptor tables and static samplers.
   - Improved handling of invalid values for filter modes, address modes, and 
LOD parameters.

Example changes:
```cpp
if (Element == nullptr)
  return reportInvalidTypeError(Ctx, "DescriptorTableNode", 
DescriptorTableNode, I);

if (std::optional Val = extractMdIntValue(StaticSamplerNode, 1))
  Sampler.Filter = *Val;
else
  return reportInvalidTypeError(Ctx, "StaticSamplerNode", 
StaticSamplerNode, 1);
  ```
Testing:
- Validation of invalid metadata nodes and values.
- Proper diagnostic messages for type mismatches.
- All existing DirectX backend tests continue to pass.


>From 02f1f21b8ecc608341440c573483e69c161a06d4 Mon Sep 17 00:00:00 2001
From: joaosaffran 
Date: Fri, 6 Jun 2025 20:04:00 +
Subject: [PATCH 1/2] changing error message

---
 llvm/lib/Target/DirectX/DXILRootSignature.cpp | 119 +++---
 ...re-RootConstants-Invalid-Num32BitValues.ll |   2 +-
 ...ure-RootConstants-Invalid-RegisterSpace.ll |   2 +-
 ...re-RootConstants-Invalid-ShaderRegister.ll |   2 +-
 4 files changed, 104 insertions(+), 21 deletions(-)

diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp 
b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
index 3aef7d3eb1e69..3a27afc6c660f 100644
--- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp
+++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp
@@ -12,6 +12,7 @@
 
//===--===//
 #include "DXILRootSignature.h"
 #include "DirectX.h"
+#include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/StringSwitch.h"
 #include "llvm/ADT/Twine.h"
 #include "llvm/Analysis/DXILMetadataAnalysis.h"
@@ -30,6 +31,7 @@
 #include 
 #include 
 #include 
+#include 
 #include 
 
 using namespace llvm;
@@ -48,6 +50,71 @@ static bool reportValueError(LLVMContext *Ctx, Twine 
ParamName,
   return true;
 }
 
+// Template function to get formatted type string based on C++ type
+template  std::string getTypeFormatted() {
+  if constexpr (std::is_same_v) {
+return "string";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "metadata";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "constant";
+  } else if constexpr (std::is_same_v) {
+return "constant";
+  } else if constexpr (std::is_same_v ||
+   std::is_same_v) {
+return "constant int";
+  } else if constexpr (std::is_same_v) {
+return "constant int";
+  }
+  return "unknown";
+}
+
+// Helper function to get the actual type of a metadata operand
+std::string getActualMDType(const MDNode *Node, unsigned Index) {
+  if (!Node || Index >= Node->getNumOperands())
+return "null";
+
+  Metadata *Op = Node->getOperand(Index);
+  if (!Op)
+return "null";
+
+  if (isa(Op))
+return getTypeFormatted();
+
+  if (isa(Op)) {
+if (auto *CAM = dyn_cast(Op)) {
+  Type *T = CAM->getValue()->getType();
+  if (T->isIntegerTy())
+return (Twine("i") + Twine(T->getIntegerBitWidth())).str();
+  if (T->isFloatingPointTy())
+return T->isFloatTy()? getTypeFormatted()
+   : T->isDoubleTy() ? getTypeFormatted()
+ : "fp";
+
+  return getTypeFormatted();
+}
+  }
+  if (isa(Op))
+return getTypeFormatted();
+
+  return "unknown";
+}
+
+// Helper function to simplify error reporting for invalid metadata values
+template 
+auto reportInvalidTypeError(LLVMContext *Ctx, Twine ParamName,
+const MDNode *Node, unsigned Index) {
+  std::string ExpectedType = getTypeFormatted();
+  std::string ActualType = getActualMDType(Node, Index);
+
+  return reportError(Ctx, "Root Signature Node: " + ParamName +
+  " expected metadata node of type " +
+  ExpectedType + " at index " + Twine(Index) +
+  " but got " + ActualType);
+}
+
 static std::optional extractMdIntValue(MDNode *Node,
  unsigned int OpId) {
   if (auto *CI =
@@ -80,7 +147,8 @@ static bool parseRootFlags(LLVMContext *Ctx, 
mcdxbc::RootSignatureDesc &RSD,
   if (std::optional Val = extractMdIntValue(RootFlagNode, 1))
 RSD.Flags = *Val;
   else
-return reportError(Ctx, "Invalid value for RootFlag");
+return reportInvalidTypeError(Ctx, "RootFlagNode",
+   RootFlagNode, 1);
 
   return false;
 }
@@ -100,23 +168,27