This is an archive of the discontinued LLVM Phabricator instance.

[Driver][HLSL] Improve diagnostics for invalid shader model and stage
ClosedPublic

Authored by bogner on Aug 29 2023, 8:11 AM.

Details

Summary

This adds more validation that a dxil triple is actually useable when
compiling HLSL.

The OS field of the triple needs to be a versioned shader model.
Later, we should set a default if this is empty and check that the
version is a shader model we can actually handle.

The Environment field of the triple needs to be specified and be a
valid shader stage. I'd like to allow this to be empty and treat it
like library, but allowing that currently crashes in DXIL metadata
handling.

Diff Detail

Event Timeline

bogner created this revision.Aug 29 2023, 8:11 AM
Herald added a project: Restricted Project. · View Herald TranscriptAug 29 2023, 8:11 AM
bogner requested review of this revision.Aug 29 2023, 8:11 AM
Herald added projects: Restricted Project, Restricted Project. · View Herald TranscriptAug 29 2023, 8:11 AM
aaron.ballman added inline comments.Aug 29 2023, 10:31 AM
clang/include/clang/Basic/DiagnosticDriverKinds.td
710–716

Hmmm in theory:

def err_drv_dxil_bad_shader_required_in_target : Error<
  "shader %select{model|stage}0 is required in target '%1' for DXIL generation">;

def err_drv_dxil_bad_shader_unsupported : Error<
  "shader %select{model|stage}0 '%1' in target '%2' is invalid for DXIL generation">;
clang/lib/Frontend/CompilerInvocation.cpp
4148

Idle question: can you pass T directly here? (I thought we had diagnostic streaming support for Triple but I'm not spotting it with a quick look through the source).

bogner added inline comments.Aug 29 2023, 4:17 PM
clang/include/clang/Basic/DiagnosticDriverKinds.td
710–716

To make that readable you kind of have to do something like this:

if (T.isDXIL()) {
  enum { ShaderModel, ShaderStage };
  if (T.getOSName().empty()) {
    Diags.Report(diag::err_drv_dxil_bad_shader_required_in_target)
        << ShaderModel << T.str();
  } else if (!T.isShaderModelOS() || T.getOSVersion() == VersionTuple(0)) {
    Diags.Report(diag::err_drv_dxil_bad_shader_invalid)
        ShaderModel << T.getOSName() << T.str();
  } else if (T.getEnvironmentName().empty()) {
    Diags.Report(diag::err_drv_dxil_bad_shader_required_in_target)
        << ShaderStage << T.str();
  } else if (!T.isShaderStageEnvironment()) {
    Diags.Report(diag::err_drv_dxil_bad_shader_invalid)
        << ShaderStage << T.getEnvironmentName() << T.str();
  }
}

It's not really clear to me which way is better. WDYT?

clang/lib/Frontend/CompilerInvocation.cpp
4148

Tried it, and no. Looks like we don't have that overload.

bogner updated this revision to Diff 555451.Sep 1 2023, 11:59 AM
  • Consolidate error messages
  • Error messages should start with a lower case letter
bogner added a comment.Sep 7 2023, 9:50 AM

Ping. Please take another look when you get a chance, thanks!

bogner updated this revision to Diff 556512.Sep 11 2023, 6:24 PM

Rebased to fix conflicts

bogner added a subscriber: Keenuts.Sep 11 2023, 6:25 PM
bogner added inline comments.
clang/lib/Frontend/CompilerInvocation.cpp
4146

@Keenuts Does this look right, or do we want something like T.isSPIRVLogical here?

Keenuts added inline comments.Sep 12 2023, 4:50 AM
clang/lib/Frontend/CompilerInvocation.cpp
4146

Thanks for asking!
I think isSPIRVLogical() would be better here, as other SPIR-V flavors are not compatible with graphics (including compute shaders).
Ideally you could also add a TODO: revisit this once we figured out how to handle PhysicalStorageBuffer64 memory model.

bogner updated this revision to Diff 556589.Sep 12 2023, 10:12 AM
  • Check isSPIRVLogical rather than isSPIRV
  • Improve diagnostics given that HLSL can generate both DXIL and SPIR-V
  • Add tests for SPIR-V triples
  • Fix missing REQUIRES: in dxil triple tests
Keenuts accepted this revision.Sep 13 2023, 2:57 AM

LGTM from the SPIR-V side. Thanks!

This revision is now accepted and ready to land.Sep 13 2023, 2:57 AM
This revision was landed with ongoing or failed builds.Sep 13 2023, 11:14 AM
This revision was automatically updated to reflect the committed changes.