Skip to content

Implement HLSL Diagnostics for LinAlg operations #7430

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 44 commits into
base: main
Choose a base branch
from

Conversation

anupamachandra
Copy link
Collaborator

Implements #7336

@anupamachandra
Copy link
Collaborator Author

Couple of items left to do here:

  1. Update the DXIL tests with IR, since the HLSL diagnostics prevent the invalid IR generation we were relying on.
  2. Packed Format input vector validations. Will discuss this offline about the integer to float cast issue I am running into.

@anupamachandra anupamachandra requested review from tex3d and damyanp May 5, 2025 16:38
@tex3d tex3d self-assigned this May 5, 2025
@tex3d tex3d moved this to Active in HLSL Support May 5, 2025
@damyanp
Copy link
Member

damyanp commented May 5, 2025

@anupamachandra - given that there's build errors and you have some more work planned, is it worth reviewing at this point?

@anupamachandra
Copy link
Collaborator Author

@anupamachandra - given that there's build errors and you have some more work planned, is it worth reviewing at this point?

Yes please. I'm working on the build errors and missed item is marked with a //FIXME. It think it'll be good to get some eyes on it to check if the PR meets expectations in terms of scope and if any big issues/gaps stand out.

@damyanp damyanp assigned bob80905 and unassigned tex3d May 12, 2025
Copy link
Collaborator

@bob80905 bob80905 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A few nits / questions

def err_hlsl_linalg_isunsigned_incorrect_for_given_type : Error<
"%0 must be %select{false|true}1 for a %2 vector type">;
def err_hlsl_linalg_interpretation_value_incorrect : Error<
"%0 is an invalid %select{Memory|Register}1 Interpretation value">;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"%0 is an invalid %select{Memory|Register}1 Interpretation value">;
"%0 is an invalid %select{memory|register}1 interpretation value">;

def err_hlsl_linalg_optimal_matrix_layout_matrix_stride_must_be_zero : Error<
"for optimal matrix layout, matrix stride must be zero">;
def err_hlsl_linalg_matrix_dim_must_not_be_zero: Error<
"matrix dimension must not be zero">;
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
"matrix dimension must not be zero">;
"matrix dimension must be greater than 0">;

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah, much better.

"matrix dimension must not be zero">;
def err_hlsl_linalg_matrix_layout_invalid : Error<
"matrix layout %0 is not valid, must be in the range %1 - %2">;
def err_hlsl_linalg_function_requires_shader_model_6_9_or_above : Error<
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should use warn_hlsl_intrinsic_in_wrong_shader_model (which is a default-error warning)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

warnings can be overriden?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, and for all the availability warnings we allow them to be overridden because they might be valid if the compiler fully optimizes them away.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I remembered as I was modifying this per your feedback that I had originally tried to use warn_hlsl_intrinsic_in_wrong_shader_model but that error prints out the entry function which is not available here. In my earlier PR I had added check to the DiagnoseReachableHLSLCall in SemaHLSL.cpp, but that gets called after these intrinsics are processed. Is there a better place to move this diagnostic?

Copy link
Contributor

@tex3d tex3d left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A couple comments on linalg.h API changes.

template <> bool IsUnsigned<uint16_t>() { return true; }
#endif
template <typename T> struct IsUnsigned {
static const bool Value = false;
Copy link
Contributor

@tex3d tex3d May 14, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I feel like the default template shouldn't have any definition for Value, if possible. That way, unsupported types trigger an error here instead of defaulting to signed. That would require specializations for float types and all supported signed int types, of course, but it would also prevent use of unexpected types.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still want this, given the builtins are now updated to use only the allowed types?

Comment on lines 55 to 62
template <> struct IsUnsigned<uint64_t> {
static const bool value = true;
};

#ifdef __HLSL_ENABLE_16_BIT
template <> struct IsUnsigned<uint16_t> {
static const bool value = true;
};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These specializations use a different casing for Value.

Suggested change
template <> struct IsUnsigned<uint64_t> {
static const bool value = true;
};
#ifdef __HLSL_ENABLE_16_BIT
template <> struct IsUnsigned<uint16_t> {
static const bool value = true;
};
template <> struct IsUnsigned<uint64_t> {
static const bool Value = true;
};
#ifdef __HLSL_ENABLE_16_BIT
template <> struct IsUnsigned<uint16_t> {
static const bool Value = true;
};

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch - but this makes me concerned we have a test gap?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, this is the issue Joshua mentioned in his review that I mentioned at today's morning meeting was a bug. I am adding high-level API tests that would have caught this.

Copy link
Contributor

github-actions bot commented May 15, 2025

✅ With the latest revision this PR passed the Python code formatter.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: New
Development

Successfully merging this pull request may close these issues.

6 participants