Skip to content

Add subscript operator support in cuda #6830

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

Merged
merged 6 commits into from
Apr 30, 2025

Conversation

mkeshavaNV
Copy link
Contributor

@mkeshavaNV mkeshavaNV commented Apr 16, 2025

This CL adds support for the subscript operator for Read Only
textures in cuda. Also adds a test for this.

Now that we support .Load for read only textures in cuda, an existing test also needs to be modified.

@mkeshavaNV mkeshavaNV changed the title Bugfix/gh 6781 WiP: Add Load support for ReadOnly textures in CUDA Apr 16, 2025
@mkeshavaNV mkeshavaNV force-pushed the bugfix/gh-6781 branch 2 times, most recently from 795637a to 1fbd035 Compare April 24, 2025 12:16
@mkeshavaNV mkeshavaNV changed the title WiP: Add Load support for ReadOnly textures in CUDA Add Load support for ReadOnly textures in CUDA Apr 24, 2025
@mkeshavaNV mkeshavaNV added the pr: non-breaking PRs without breaking changes label Apr 24, 2025
@mkeshavaNV
Copy link
Contributor Author

/format

@slangbot
Copy link
Contributor

🌈 Formatted, please merge the changes from this PR

mkeshavaNV added a commit to mkeshavaNV/slang that referenced this pull request Apr 25, 2025
@mkeshavaNV mkeshavaNV requested a review from expipiplus1 April 25, 2025 11:11
@mkeshavaNV mkeshavaNV marked this pull request as ready for review April 25, 2025 11:11
@mkeshavaNV mkeshavaNV requested a review from a team as a code owner April 25, 2025 11:11
@mkeshavaNV mkeshavaNV requested a review from Copilot April 25, 2025 11:39
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR adds support for the subscript operator for read-only textures in CUDA by introducing inline PTX intrinsic functions to perform texture fetch/load operations across 1D, 2D, 3D, and array textures. Key changes include:

  • Implementation of tex1Dfetch_int, tex2Dfetch_int, tex3Dfetch_int functions for various dimensional textures.
  • Addition of tex1DArrayfetch_int and tex2DArrayfetch_int to support array textures.
  • Updates to the test suite to validate the new functionality.
Files not reviewed (4)
  • source/slang/hlsl.meta.slang: Language not supported
  • tests/compute/texture-subscript-cuda.slang: Language not supported
  • tests/compute/texture-subscript-cuda.slang.expected.txt: Language not supported
  • tests/language-feature/capability/capability-invalid-fragment-in-compute.slang: Language not supported

@mkeshavaNV mkeshavaNV changed the title Add Load support for ReadOnly textures in CUDA Add subscript operator support in cuda Apr 25, 2025
switch(Shape.flavor)
{
case $(SLANG_TEXTURE_1D):
__intrinsic_asm "<PTX does not support 1D texture read>";
Copy link
Collaborator

Choose a reason for hiding this comment

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

isn't this just going to lead to an error in the downstream compiler (nvrtc)?
should that be a static_assert instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

makes sense. updated.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

hm, seems like the bootstrap has some issues with the assert. I will try to fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ok seems like that was because of missing default. I've added that back, and the static_assert. Please check.

__intrinsic_asm "tex2Dfetch_int<$T0>($0, ($1).x, ($1).y)";
case $(SLANG_TEXTURE_3D):
__intrinsic_asm "tex3Dfetch_int<$T0>($0, ($1).x, ($1).y, ($1).z)";
case $(SLANG_TEXTURE_CUBE):
Copy link
Collaborator

Choose a reason for hiding this comment

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

are there no PTX intrinsics for cube map reads?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

nope, cubemap requires float coordinates. Confirmed with the ptx team as well. I even saw error:
line 84; error : Floating-point coordinates required

mkeshavaNV added a commit to mkeshavaNV/slang that referenced this pull request Apr 25, 2025
mkeshavaNV and others added 2 commits April 25, 2025 18:05
This CL adds support for the subscript operator for Read Only
textures in cuda. Also adds a test for this.

Fixes shader-slang#6781
asm("tex.3d.v4.f32.s32 {%0, %1, %2, %3}, [%4, {%5, %6, %7, %8}];"
: "=f"(result), "=f"(dummy), "=f"(dummy), "=f"(dummy)
: "l"(texObj), "r"(x), "r"(y), "r"(z), "r"(z));
// Note: The repeated z is a WAR for a bug in PTX. That is a dummy parameter.
Copy link
Contributor

Choose a reason for hiding this comment

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

can you add more details. Does PTX only support vec4 fetch?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we write a test which will start failing when this bug is fixed downstream?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

oh that is not a bug. Fixed the note after I confirmed with the ptx team. It is how the 3D textures are addressed.

From the docs:
https://docs.nvidia.com/cuda/parallel-thread-execution/index.html#texture-instructions-tex

Operand c is a scalar or singleton tuple for 1d textures; is a two-element vector for 2d textures; 
and is a four-element vector for 3d textures, 

asm("tex.3d.v4.f32.s32 {%0, %1, %2, %3}, [%4, {%5, %6, %7, %8}];"
: "=f"(result), "=f"(dummy), "=f"(dummy), "=f"(dummy)
: "l"(texObj), "r"(x), "r"(y), "r"(z), "r"(z));
// Note: The repeated z is a WAR for a bug in PTX. That is a dummy parameter.
Copy link
Collaborator

Choose a reason for hiding this comment

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

Can we write a test which will start failing when this bug is fixed downstream?


switch(Shape.flavor)
{
case $(SLANG_TEXTURE_2D):
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to handle 3d here as well, inline with the static assert (I think that an if/else here would be clearer)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think we support 3D array textures in slang? I could not find Texture3DArray. I have updated the static_assert to be clearer, and also made this an if. Please let me know if this is OK.

@mkeshavaNV
Copy link
Contributor Author

/format

@slangbot
Copy link
Contributor

🌈 Formatted, please merge the changes from this PR

@mkeshavaNV mkeshavaNV enabled auto-merge (squash) April 30, 2025 09:29
@mkeshavaNV mkeshavaNV merged commit b0e1505 into shader-slang:master Apr 30, 2025
16 checks passed
szihs pushed a commit to szihs/slang that referenced this pull request May 7, 2025
* cuda: Add support for subscript operator

This CL adds support for the subscript operator for Read Only
textures in cuda. Also adds a test for this.

Fixes shader-slang#6781

* format code

* fix review comments

* format code

---------

Co-authored-by: slangbot <186143334+slangbot@users.noreply.github.com>
Co-authored-by: Ellie Hermaszewska <ellieh@nvidia.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr: non-breaking PRs without breaking changes
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants