diff --git a/clang/test/SemaHLSL/RootSignature-Validation-Textures.hlsl b/clang/test/SemaHLSL/RootSignature-Validation-Textures.hlsl new file mode 100644 index 0000000000000..9daf38e30dd67 --- /dev/null +++ b/clang/test/SemaHLSL/RootSignature-Validation-Textures.hlsl @@ -0,0 +1,13 @@ +// RUN: not %clang_dxc -T cs_6_6 -E CSMain %s 2>&1 | FileCheck %s + +// CHECK: error: register srv (space=0, register=0) is bound to a texture or typed buffer. + +RWStructuredBuffer Out : register(u0); +Buffer B : register(t0); +// Compute Shader for UAV testing +[numthreads(8, 8, 1)] +[RootSignature("SRV(t0), UAV(u0)")] +void CSMain(uint id : SV_GroupID) +{ + Out[0] = B[0]; +} diff --git a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp index a52a04323514c..c34f31fbe35e5 100644 --- a/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp +++ b/llvm/lib/Target/DirectX/DXILPostOptimizationValidation.cpp @@ -85,6 +85,17 @@ static void reportOverlappingBinding(Module &M, DXILResourceMap &DRM) { } } +static void +reportInvalidHandleTyBoundInRs(Module &M, Twine Type, + ResourceInfo::ResourceBinding Binding) { + SmallString<128> Message; + raw_svector_ostream OS(Message); + OS << "register " << Type << " (space=" << Binding.Space + << ", register=" << Binding.LowerBound << ")" + << " is bound to a texture or typed buffer."; + M.getContext().diagnose(DiagnosticInfoGeneric(Message)); +} + static void reportRegNotBound(Module &M, Twine Type, ResourceInfo::ResourceBinding Binding) { SmallString<128> Message; @@ -163,12 +174,26 @@ static void reportErrors(Module &M, DXILResourceMap &DRM, ResourceInfo::ResourceBinding Binding = SRV.getBinding(); if (!Validation.checkTRegBinding(Binding)) reportRegNotBound(M, "srv", Binding); + else { + const auto *Handle = + dyn_cast_or_null(SRV.getHandleTy()); + + if (!Handle) + reportInvalidHandleTyBoundInRs(M, "srv", Binding); + } } for (const ResourceInfo &UAV : DRM.uavs()) { ResourceInfo::ResourceBinding Binding = UAV.getBinding(); if (!Validation.checkURegBinding(Binding)) reportRegNotBound(M, "uav", Binding); + else { + const auto *Handle = + dyn_cast_or_null(UAV.getHandleTy()); + + if (!Handle) + reportInvalidHandleTyBoundInRs(M, "srv", Binding); + } } for (const ResourceInfo &Sampler : DRM.samplers()) { diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.cpp b/llvm/lib/Target/DirectX/DXILRootSignature.cpp index 5a53ea8a3631b..3848ad63dc468 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.cpp +++ b/llvm/lib/Target/DirectX/DXILRootSignature.cpp @@ -556,10 +556,7 @@ AnalysisKey RootSignatureAnalysis::Key; RootSignatureAnalysis::Result RootSignatureAnalysis::run(Module &M, ModuleAnalysisManager &AM) { - if (!AnalysisResult) - AnalysisResult = std::make_unique( - RootSignatureBindingInfo(analyzeModule(M))); - return *AnalysisResult; + return RootSignatureBindingInfo(analyzeModule(M)); } //===----------------------------------------------------------------------===// @@ -638,15 +635,14 @@ PreservedAnalyses RootSignatureAnalysisPrinter::run(Module &M, //===----------------------------------------------------------------------===// bool RootSignatureAnalysisWrapper::runOnModule(Module &M) { - if (!FuncToRsMap) - FuncToRsMap = std::make_unique( - RootSignatureBindingInfo(analyzeModule(M))); + FuncToRsMap = std::make_unique( + RootSignatureBindingInfo(analyzeModule(M))); return false; } void RootSignatureAnalysisWrapper::getAnalysisUsage(AnalysisUsage &AU) const { AU.setPreservesAll(); - AU.addRequired(); + AU.addPreserved(); } char RootSignatureAnalysisWrapper::ID = 0; diff --git a/llvm/lib/Target/DirectX/DXILRootSignature.h b/llvm/lib/Target/DirectX/DXILRootSignature.h index e975ceb22ea54..07dbd51bed391 100644 --- a/llvm/lib/Target/DirectX/DXILRootSignature.h +++ b/llvm/lib/Target/DirectX/DXILRootSignature.h @@ -47,7 +47,7 @@ class RootSignatureBindingInfo { RootSignatureBindingInfo() = default; RootSignatureBindingInfo( SmallDenseMap Map) - : FuncToRsMap(Map){}; + : FuncToRsMap(Map) {}; iterator find(const Function *F) { return FuncToRsMap.find(F); } @@ -73,9 +73,6 @@ class RootSignatureAnalysis : public AnalysisInfoMixin { using Result = RootSignatureBindingInfo; Result run(Module &M, ModuleAnalysisManager &AM); - -private: - std::unique_ptr AnalysisResult; }; /// Wrapper pass for the legacy pass manager. diff --git a/llvm/test/CodeGen/DirectX/rootsignature-validation-textures.ll b/llvm/test/CodeGen/DirectX/rootsignature-validation-textures.ll new file mode 100644 index 0000000000000..8c407fdd60205 --- /dev/null +++ b/llvm/test/CodeGen/DirectX/rootsignature-validation-textures.ll @@ -0,0 +1,87 @@ +; RUN: not opt -S -passes='dxil-post-optimization-validation' -mtriple=dxil-pc-shadermodel6.6-compute %s 2>&1 +; CHECK: error: register srv (space=0, register=0) is bound to a texture or typed buffer. + +; +; Resource Bindings: +; +; Name Type Format Dim ID HLSL Bind Count +; ------------------------------ ---------- ------- ----------- ------- -------------- --------- +; B texture f32 buf T0 t0 1 +; Out UAV struct r/w U0 u0 1 +; +; ModuleID = '../clang/test/SemaHLSL/RootSignature-Validation-Textures.hlsl' +target datalayout = "e-m:e-p:32:32-i1:32-i8:8-i16:16-i32:32-i64:64-f16:16-f32:32-f64:64-n8:16:32:64" +target triple = "dxilv1.5-unknown-shadermodel6.5-compute" + +%"Buffer" = type { float } +%"RWStructuredBuffer" = type { i32 } + +@.str = private unnamed_addr constant [4 x i8] c"Out\00", align 1 +@.str.2 = private unnamed_addr constant [2 x i8] c"B\00", align 1 +@B = external constant %"Buffer" +@Out = external constant %"RWStructuredBuffer" + +; Function Attrs: mustprogress nofree noinline norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) +define void @CSMain() local_unnamed_addr #0 { +entry: + %0 = tail call target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32 0, i32 0, i32 1, i32 0, i1 false, ptr nonnull @.str) + %1 = tail call target("dx.TypedBuffer", float, 0, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_0_0_0t(i32 0, i32 0, i32 1, i32 0, i1 false, ptr nonnull @.str.2) + %2 = call { float, i1 } @llvm.dx.resource.load.typedbuffer.f32.tdx.TypedBuffer_f32_0_0_0t(target("dx.TypedBuffer", float, 0, 0, 0) %1, i32 0) + %3 = extractvalue { float, i1 } %2, 0 + %conv.i = fptosi float %3 to i32 + call void @llvm.dx.resource.store.rawbuffer.tdx.RawBuffer_i32_1_0t.i32(target("dx.RawBuffer", i32, 1, 0) %0, i32 0, i32 0, i32 %conv.i) + ret void +} + +; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(none) +declare target("dx.RawBuffer", i32, 1, 0) @llvm.dx.resource.handlefrombinding.tdx.RawBuffer_i32_1_0t(i32, i32, i32, i32, i1, ptr) #1 + +; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(none) +declare target("dx.TypedBuffer", float, 0, 0, 0) @llvm.dx.resource.handlefrombinding.tdx.TypedBuffer_f32_0_0_0t(i32, i32, i32, i32, i1, ptr) #1 + +; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(none) +declare ptr @llvm.dx.resource.getpointer.p0.tdx.RawBuffer_i32_1_0t(target("dx.RawBuffer", i32, 1, 0), i32) #1 + +; Function Attrs: mustprogress nocallback nofree nosync nounwind willreturn memory(none) +declare ptr @llvm.dx.resource.getpointer.p0.tdx.TypedBuffer_f32_0_0_0t(target("dx.TypedBuffer", float, 0, 0, 0), i32) #1 + +; Function Attrs: nocallback nofree nosync nounwind willreturn memory(read) +declare { float, i1 } @llvm.dx.resource.load.typedbuffer.f32.tdx.TypedBuffer_f32_0_0_0t(target("dx.TypedBuffer", float, 0, 0, 0), i32) #2 + +; Function Attrs: nocallback nofree nosync nounwind willreturn memory(write) +declare void @llvm.dx.resource.store.rawbuffer.tdx.RawBuffer_i32_1_0t.i32(target("dx.RawBuffer", i32, 1, 0), i32, i32, i32) #3 + +attributes #0 = { mustprogress nofree noinline norecurse nosync nounwind willreturn memory(readwrite, inaccessiblemem: none) "approx-func-fp-math"="true" "frame-pointer"="all" "hlsl.numthreads"="8,8,1" "hlsl.shader"="compute" "no-infs-fp-math"="true" "no-nans-fp-math"="true" "no-signed-zeros-fp-math"="true" "no-trapping-math"="true" "stack-protector-buffer-size"="8" } +attributes #1 = { mustprogress nocallback nofree nosync nounwind willreturn memory(none) } +attributes #2 = { nocallback nofree nosync nounwind willreturn memory(read) } +attributes #3 = { nocallback nofree nosync nounwind willreturn memory(write) } + +!dx.rootsignatures = !{!0} +!llvm.module.flags = !{!4, !5} +!dx.valver = !{!6} +!llvm.ident = !{!7} +!dx.shaderModel = !{!8} +!dx.version = !{!9} +!dx.resources = !{!10} +!dx.entryPoints = !{!17} + +!0 = !{ptr @CSMain, !1, i32 2} +!1 = !{!2, !3} +!2 = !{!"RootSRV", i32 0, i32 0, i32 0, i32 4} +!3 = !{!"RootUAV", i32 0, i32 0, i32 0, i32 2} +!4 = !{i32 1, !"wchar_size", i32 4} +!5 = !{i32 7, !"frame-pointer", i32 2} +!6 = !{i32 1, i32 8} +!7 = !{!"clang version 21.0.0git (https://github.com/joaosaffran/llvm-project.git c16f15b4cd469a3f6efc2e4b0e098190d7fd0787)"} +!8 = !{!"cs", i32 6, i32 5} +!9 = !{i32 1, i32 5} +!10 = !{!11, !14, null, null} +!11 = !{!12} +!12 = !{i32 0, ptr @B, !"B", i32 0, i32 0, i32 1, i32 10, i32 0, !13} +!13 = !{i32 0, i32 9} +!14 = !{!15} +!15 = !{i32 0, ptr @Out, !"Out", i32 0, i32 0, i32 1, i32 12, i1 false, i1 false, i1 false, !16} +!16 = !{i32 1, i32 4} +!17 = !{ptr @CSMain, !"CSMain", null, !10, !18} +!18 = !{i32 0, i64 16, i32 4, !19} +!19 = !{i32 8, i32 8, i32 1}