Skip to content

Commit 34619da

Browse files
committed
moving offset logic into shared validations
1 parent bf5714d commit 34619da

File tree

3 files changed

+49
-38
lines changed

3 files changed

+49
-38
lines changed

llvm/include/llvm/Frontend/HLSL/RootSignatureValidations.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ LLVM_ABI bool verifyMaxAnisotropy(uint32_t MaxAnisotropy);
4040
LLVM_ABI bool verifyComparisonFunc(uint32_t ComparisonFunc);
4141
LLVM_ABI bool verifyBorderColor(uint32_t BorderColor);
4242
LLVM_ABI bool verifyLOD(float LOD);
43+
LLVM_ABI bool verifyOffsetOverflowing(
44+
uint64_t &AppendingRegister, uint32_t OffsetInDescriptorsFromTableStart,
45+
uint32_t BaseRegister, uint32_t Space, uint32_t NumDescriptors);
4346

4447
} // namespace rootsig
4548
} // namespace hlsl

llvm/lib/Frontend/HLSL/RootSignatureMetadata.cpp

Lines changed: 4 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -539,53 +539,19 @@ Error validateDescriptorTableSamplerMixin(mcdxbc::DescriptorTable Table,
539539
return Error::success();
540540
}
541541

542-
/** This validation logic was extracted from the DXC codebase
543-
* https://github.com/microsoft/DirectXShaderCompiler/blob/7a1b1df9b50a8350a63756720e85196e0285e664/lib/DxilRootSignature/DxilRootSignatureValidator.cpp#L205
544-
*
545-
* It checks if the registers in a descriptor table are overflowing, meaning,
546-
* they are trying to bind a register larger than MAX_UINT.
547-
* This will usually happen when the descriptor table appends a resource
548-
* after an unbounded range.
549-
**/
550542
Error validateDescriptorTableRegisterOverflow(mcdxbc::DescriptorTable Table,
551543
uint32_t Location) {
552544
uint64_t AppendingRegister = 0;
553545

554546
for (const dxbc::RTS0::v2::DescriptorRange &Range : Table.Ranges) {
555-
556547
dxbc::DescriptorRangeType RangeType =
557548
static_cast<dxbc::DescriptorRangeType>(Range.RangeType);
558-
559-
uint64_t Register = AppendingRegister;
560-
561-
// Checks if the current register should be appended to the previous range.
562-
if (Range.OffsetInDescriptorsFromTableStart != ~0U)
563-
Register = Range.OffsetInDescriptorsFromTableStart;
564-
565-
// Check for overflow in the register value.
566-
if (Register > ~0U)
549+
if (verifyOffsetOverflowing(AppendingRegister,
550+
Range.OffsetInDescriptorsFromTableStart,
551+
Range.BaseShaderRegister, Range.RegisterSpace,
552+
Range.NumDescriptors))
567553
return make_error<TableRegisterOverflowError>(
568554
RangeType, Range.BaseShaderRegister, Range.RegisterSpace);
569-
// Is the current range unbounded?
570-
if (Range.NumDescriptors == ~0U) {
571-
// No ranges should be appended to an unbounded range.
572-
AppendingRegister = (uint64_t)~0U + (uint64_t)1ULL;
573-
} else {
574-
// Is the defined range, overflowing?
575-
uint64_t UpperBound = (uint64_t)Range.BaseShaderRegister +
576-
(uint64_t)Range.NumDescriptors - (uint64_t)1U;
577-
if (UpperBound > ~0U)
578-
return make_error<TableRegisterOverflowError>(
579-
RangeType, Range.BaseShaderRegister, Range.RegisterSpace);
580-
581-
// If we append this range, will it overflow?
582-
uint64_t AppendingUpperBound =
583-
(uint64_t)Register + (uint64_t)Range.NumDescriptors - (uint64_t)1U;
584-
if (AppendingUpperBound > ~0U)
585-
return make_error<TableRegisterOverflowError>(
586-
RangeType, Range.BaseShaderRegister, Range.RegisterSpace);
587-
AppendingRegister = Register + Range.NumDescriptors;
588-
}
589555
}
590556

591557
return Error::success();

llvm/lib/Frontend/HLSL/RootSignatureValidations.cpp

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,48 @@ bool verifyBorderColor(uint32_t BorderColor) {
180180

181181
bool verifyLOD(float LOD) { return !std::isnan(LOD); }
182182

183+
/** This validation logic was extracted from the DXC codebase
184+
* https://github.com/microsoft/DirectXShaderCompiler/blob/7a1b1df9b50a8350a63756720e85196e0285e664/lib/DxilRootSignature/DxilRootSignatureValidator.cpp#L205
185+
*
186+
* It checks if the registers in a descriptor table are overflowing, meaning,
187+
* they are trying to bind a register larger than MAX_UINT.
188+
* This will usually happen when the descriptor table appends a resource
189+
* after an unbounded range.
190+
**/
191+
bool verifyOffsetOverflowing(uint64_t &AppendingRegister,
192+
uint32_t OffsetInDescriptorsFromTableStart,
193+
uint32_t BaseRegister, uint32_t Space,
194+
uint32_t NumDescriptors) {
195+
uint64_t Register = AppendingRegister;
196+
197+
// Checks if the current register should be appended to the previous range.
198+
if (OffsetInDescriptorsFromTableStart != ~0U)
199+
Register = OffsetInDescriptorsFromTableStart;
200+
201+
// Check for overflow in the register value.
202+
if (Register > ~0U)
203+
return true;
204+
// Is the current range unbounded?
205+
if (NumDescriptors == ~0U) {
206+
// No ranges should be appended to an unbounded range.
207+
AppendingRegister = (uint64_t)~0U + (uint64_t)1ULL;
208+
} else {
209+
// Is the defined range, overflowing?
210+
uint64_t UpperBound =
211+
(uint64_t)BaseRegister + (uint64_t)NumDescriptors - (uint64_t)1U;
212+
if (UpperBound > ~0U)
213+
return true;
214+
215+
// If we append this range, will it overflow?
216+
uint64_t AppendingUpperBound =
217+
(uint64_t)Register + (uint64_t)NumDescriptors - (uint64_t)1U;
218+
if (AppendingUpperBound > ~0U)
219+
return true;
220+
AppendingRegister = Register + NumDescriptors;
221+
}
222+
return false;
223+
}
224+
183225
} // namespace rootsig
184226
} // namespace hlsl
185227
} // namespace llvm

0 commit comments

Comments
 (0)