Browse Source

Disallow CreateHandle in SM66+ and library targets (#3629)

(cherry picked from commit 634cd49ba5175023992ab5fa53c1cbf822a04112)
Tex Riddell 4 years ago
parent
commit
3409368ad8

+ 11 - 0
lib/HLSL/DxilValidation.cpp

@@ -2567,6 +2567,17 @@ static void ValidateDxilOperationCallInProfile(CallInst *CI,
       ValCtx.EmitInstrFormatError(CI, ValidationRule::SmOpcodeInInvalidFunction,
                                   {"64-bit atomic operations", "Shader Model 6.6+"});
   } break;
+  case DXIL::OpCode::CreateHandle:
+    if (ValCtx.isLibProfile) {
+      ValCtx.EmitInstrFormatError(CI, ValidationRule::SmOpcodeInInvalidFunction,
+                                  {"CreateHandle", "non-library targets"});
+    }
+    // CreateHandle should not be used in SM 6.6 and above:
+    if (DXIL::CompareVersions(ValCtx.m_DxilMajor, ValCtx.m_DxilMinor, 1, 5) > 0) {
+      ValCtx.EmitInstrFormatError(CI, ValidationRule::SmOpcodeInInvalidFunction,
+                                  {"CreateHandle", "Shader model 6.5 and below"});
+    }
+    break;
   default:
     // TODO: make sure every opcode is checked.
     // Skip opcodes don't need special check.

+ 4 - 1
tools/clang/test/CodeGenHLSL/basic.hlsl

@@ -1,5 +1,8 @@
 // RUN: %dxc -E main -T ps_6_0 %s
 
+Buffer<float4> g_buf;
+
+[shader("pixel")]
 float4 main() : SV_Target {
-  return float4(1, 0, 0, 1);
+  return g_buf.Load(0);
 }

+ 16 - 0
tools/clang/unittests/HLSL/ValidationTest.cpp

@@ -298,6 +298,7 @@ public:
   TEST_METHOD(ValidatePrintfNotAllowed)
 
   TEST_METHOD(ValidateVersionNotAllowed)
+  TEST_METHOD(CreateHandleNotAllowedSM66)
 
   dxc::DxcDllSupport m_dllSupport;
   VersionSupportInfo m_ver;
@@ -3858,3 +3859,18 @@ TEST_F(ValidationTest, ValidateVersionNotAllowed) {
     ("= !{i32 1, i32 " + higherDxilMinor + "}").c_str(),
     ("error: Dxil version in metadata (1." + higherDxilMinor + ") is not supported; maximum: (1." + maxDxilMinor + ")").c_str());
 }
+
+TEST_F(ValidationTest, CreateHandleNotAllowedSM66) {
+  if (m_ver.SkipDxilVersion(1, 6)) return;
+  RewriteAssemblyCheckMsg(L"..\\CodeGenHLSL\\basic.hlsl", "ps_6_5",
+    {"= !{i32 1, i32 5}", "= !{!\"ps\", i32 6, i32 5}"},
+    {"= !{i32 1, i32 6}", "= !{!\"ps\", i32 6, i32 6}"},
+    "opcode 'CreateHandle' should only be used in 'Shader model 6.5 and below'");
+  RewriteAssemblyCheckMsg(L"..\\CodeGenHLSL\\basic.hlsl", "lib_6_5",
+    {"call %dx.types.Handle @\"dx.op.createHandleForLib.class.Buffer<vector<float, 4> >\"\\(i32 160, %\"class.Buffer<vector<float, 4> >\" %[0-9]+\\)",
+     "declare %dx.types.Handle @\"dx.op.createHandleForLib.class.Buffer<vector<float, 4> >\"\\(i32, %\"class.Buffer<vector<float, 4> >\"\\) #1"},
+    {"call %dx.types.Handle @dx.op.createHandle(i32 57, i8 0, i32 0, i32 0, i1 false)",
+     "declare %dx.types.Handle @dx.op.createHandle(i32, i8, i32, i32, i1) #1"},
+    "opcode 'CreateHandle' should only be used in 'non-library targets'",
+    true);
+}