Skip to content

[SPIR-V] Add legalize-addrspace-cast pass #137598

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

Closed
wants to merge 1 commit into from
Closed

Conversation

Keenuts
Copy link
Contributor

@Keenuts Keenuts commented Apr 28, 2025

This commit adds a new pass in the backend which propagates the
addrspace of the pointers down to the last use, making sure the
addrspace remains consistent, and thus stripping any addrspacecast.
This is required to lower LLVM-IR to logical SPIR-V, which does not
support generic pointers.

This is now required as HLSL emits several address spaces, and thus
addrspacecasts in some cases:

Example 1: resource access

%handle = tail call target("spirv.VulkanBuffer", ...)
%rptr = @llvm.spv.resource.getpointer(%handle, ...);
%cptr = addrspacecast ptr addrspace(11) %rptr to ptr
%fptr = load i32, ptr %cptr

Example 2: object methods

define void @objectmethod(ptr %this) {
}

define void @foo(ptr addrspace(11) %object) {
  call void @objectmethod(ptr addrspacecast(addrspace(11) %object to ptr));
}

This is waiting on #135794

@Keenuts Keenuts requested a review from s-perron April 28, 2025 09:01
This commit adds a new pass in the backend which propagates the
addrspace of the pointers down to the last use, making sure the
addrspace remains consistent, and thus stripping any addrspacecast.
This is required to lower LLVM-IR to logical SPIR-V, which does not
support generic pointers.

This is now required as HLSL emits several address spaces, and thus
addrspacecasts in some cases:

Example 1: resource access

```llvm
%handle = tail call target("spirv.VulkanBuffer", ...)
%rptr = @llvm.spv.resource.getpointer(%handle, ...);
%cptr = addrspacecast ptr addrspace(11) %rptr to ptr
%fptr = load i32, ptr %cptr
```

Example 2: object methods

```llvm
define void @objectmethod(ptr %this) {
}

define void @foo(ptr addrspace(11) %object) {
  call void @objectmethod(ptr addrspacecast(addrspace(11) %object to ptr));
}
```
@Naghasan
Copy link
Contributor

Why not using the existing address space inference pass ? It looks redundant

@Keenuts
Copy link
Contributor Author

Keenuts commented Apr 28, 2025

Why not using the existing address space inference pass ? It looks redundant

That's a good question, and reason was I didn't knew this existed. I just tried, the llvm/test/CodeGen/SPIRV/pointers/pointer-addrspacecast.ll test pass but llvm/test/CodeGen/SPIRV/pointers/resource-addrspacecast.ll don't.

I need to look into why the passes fails to infer the resource address spaces.

@Keenuts
Copy link
Contributor Author

Keenuts commented Apr 29, 2025

Thanks a lot @Naghasan , looks like this is what we needed in the end. Opened #137766 and closing this one.

@Keenuts Keenuts closed this Apr 29, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants