[clang] [HLSL][RootSignature] Add basic parameter validations of Root Elements (PR #145795)
Aaron Ballman via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 15 10:52:30 PDT 2025
================
@@ -1083,6 +1085,92 @@ void SemaHLSL::ActOnFinishRootSignatureDecl(
bool SemaHLSL::handleRootSignatureElements(
ArrayRef<hlsl::RootSignatureElement> Elements) {
+ // Define some common error handling functions
+ bool HadError = false;
+ auto ReportError = [this, &HadError](SourceLocation Loc, uint32_t LowerBound,
+ uint32_t UpperBound) {
+ HadError = true;
+ this->Diag(Loc, diag::err_hlsl_invalid_rootsig_value)
+ << LowerBound << UpperBound;
+ };
+
+ auto ReportFloatError = [this, &HadError](SourceLocation Loc,
+ float LowerBound,
+ float UpperBound) {
+ HadError = true;
+ this->Diag(Loc, diag::err_hlsl_invalid_rootsig_value)
+ << llvm::formatv("{0:f}", LowerBound).sstr<6>()
+ << llvm::formatv("{0:f}", UpperBound).sstr<6>();
+ };
+
+ auto VerifyRegister = [ReportError](SourceLocation Loc, uint32_t Register) {
+ if (!llvm::hlsl::rootsig::verifyRegisterValue(Register))
+ ReportError(Loc, 0, 0xfffffffe);
+ };
+
+ auto VerifySpace = [ReportError](SourceLocation Loc, uint32_t Space) {
+ if (!llvm::hlsl::rootsig::verifyRegisterSpace(Space))
+ ReportError(Loc, 0, 0xffffffef);
+ };
+
+ const uint32_t Version =
+ llvm::to_underlying(SemaRef.getLangOpts().HLSLRootSigVer);
+ const uint32_t VersionEnum = Version - 1;
+ auto ReportFlagError = [this, &HadError, VersionEnum](SourceLocation Loc) {
+ HadError = true;
+ this->Diag(Loc, diag::err_hlsl_invalid_rootsig_flag)
+ << /*version minor*/ VersionEnum;
+ };
+
+ // Iterate through the elements and do basic validations
+ for (const hlsl::RootSignatureElement &RootSigElem : Elements) {
+ SourceLocation Loc = RootSigElem.getLocation();
+ const llvm::hlsl::rootsig::RootElement &Elem = RootSigElem.getElement();
+ if (const auto *Descriptor =
+ std::get_if<llvm::hlsl::rootsig::RootDescriptor>(&Elem)) {
+ VerifyRegister(Loc, Descriptor->Reg.Number);
+ VerifySpace(Loc, Descriptor->Space);
+
+ if (!llvm::hlsl::rootsig::verifyRootDescriptorFlag(
+ Version, llvm::to_underlying(Descriptor->Flags)))
+ ReportFlagError(Loc);
+ } else if (const auto *Constants =
+ std::get_if<llvm::hlsl::rootsig::RootConstants>(&Elem)) {
+ VerifyRegister(Loc, Constants->Reg.Number);
+ VerifySpace(Loc, Constants->Space);
+ } else if (const auto *Sampler =
+ std::get_if<llvm::hlsl::rootsig::StaticSampler>(&Elem)) {
+ VerifyRegister(Loc, Sampler->Reg.Number);
+ VerifySpace(Loc, Sampler->Space);
+
+ assert(!std::isnan(Sampler->MaxLOD) && !std::isnan(Sampler->MinLOD) &&
+ "By construction, parseFloatParam can't produce a NaN from a "
+ "float_literal token");
+
+ if (!llvm::hlsl::rootsig::verifyMaxAnisotropy(Sampler->MaxAnisotropy))
+ ReportError(Loc, 0, 16);
+ if (!llvm::hlsl::rootsig::verifyMipLODBias(Sampler->MipLODBias))
+ ReportFloatError(Loc, -16.f, 15.99);
----------------
AaronBallman wrote:
Thank you! And no worries on timing, I'm not aware of any bots that are down because of this.
https://github.com/llvm/llvm-project/pull/145795
More information about the cfe-commits
mailing list