-
Notifications
You must be signed in to change notification settings - Fork 14.4k
[SPIRV] Add logic for OpGenericCastToPtrExplicit rewriting #146596
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
jzc
wants to merge
2
commits into
llvm:main
Choose a base branch
from
jzc:generic-cast
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+150
−6
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
//===- SPIRVTargetTransformInfo.cpp - SPIR-V specific TTI -------*- C++ -*-===// | ||
// | ||
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | ||
// See https://llvm.org/LICENSE.txt for license information. | ||
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | ||
// | ||
//===----------------------------------------------------------------------===// | ||
|
||
#include "SPIRVTargetTransformInfo.h" | ||
#include "llvm/IR/IntrinsicsSPIRV.h" | ||
|
||
using namespace llvm; | ||
|
||
bool llvm::SPIRVTTIImpl::collectFlatAddressOperands( | ||
SmallVectorImpl<int> &OpIndexes, Intrinsic::ID IID) const { | ||
switch (IID) { | ||
case Intrinsic::spv_generic_cast_to_ptr_explicit: | ||
OpIndexes.push_back(0); | ||
return true; | ||
default: | ||
return false; | ||
} | ||
} | ||
|
||
Value *llvm::SPIRVTTIImpl::rewriteIntrinsicWithAddressSpace(IntrinsicInst *II, | ||
Value *OldV, | ||
Value *NewV) const { | ||
auto IntrID = II->getIntrinsicID(); | ||
switch (IntrID) { | ||
case Intrinsic::spv_generic_cast_to_ptr_explicit: { | ||
unsigned NewAS = NewV->getType()->getPointerAddressSpace(); | ||
unsigned DstAS = II->getType()->getPointerAddressSpace(); | ||
return NewAS == DstAS ? NewV | ||
: ConstantPointerNull::get( | ||
PointerType::get(NewV->getContext(), DstAS)); | ||
} | ||
default: | ||
return nullptr; | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
102 changes: 102 additions & 0 deletions
102
llvm/test/Transforms/InferAddressSpaces/SPIRV/generic-cast-explicit.ll
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,102 @@ | ||
; This test checks that the address space casts for SPIR-V generic pointer casts | ||
; are lowered correctly by the infer-address-spaces pass. | ||
; RUN: opt < %s -passes=infer-address-spaces -S --mtriple=spirv64-unknown-unknown | FileCheck %s | ||
|
||
; Casting a global pointer to a global pointer. | ||
; The uses of c2 will be replaced with %global. | ||
; CHECK: @kernel1(ptr addrspace(1) %global) | ||
define i1 @kernel1(ptr addrspace(1) %global) { | ||
%c1 = addrspacecast ptr addrspace(1) %global to ptr addrspace(4) | ||
%c2 = call ptr addrspace(1) @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr addrspace(1) %global, null | ||
%b1 = icmp eq ptr addrspace(1) %c2, null | ||
ret i1 %b1 | ||
} | ||
|
||
; Casting a global pointer to a local pointer. | ||
; The uses of c2 will be replaced with null. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this kind of comment is quite helpful, can we add something similar for the test cases below? |
||
; CHECK: @kernel2(ptr addrspace(1) %global) | ||
define i1 @kernel2(ptr addrspace(1) %global) { | ||
%c1 = addrspacecast ptr addrspace(1) %global to ptr addrspace(4) | ||
%c2 = call ptr addrspace(3) @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr addrspace(3) null, null | ||
%b1 = icmp eq ptr addrspace(3) %c2, null | ||
ret i1 %b1 | ||
} | ||
|
||
; Casting a global pointer to a private pointer. | ||
; The uses of c2 will be replaced with null. | ||
; CHECK: @kernel3(ptr addrspace(1) %global) | ||
define i1 @kernel3(ptr addrspace(1) %global) { | ||
%c1 = addrspacecast ptr addrspace(1) %global to ptr addrspace(4) | ||
%c2 = call ptr @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr null, null | ||
%b1 = icmp eq ptr %c2, null | ||
ret i1 %b1 | ||
} | ||
|
||
; Casting a local pointer to a local pointer. | ||
; The uses of c2 will be replaced with %local. | ||
; CHECK: @kernel4(ptr addrspace(3) %local) | ||
define i1 @kernel4(ptr addrspace(3) %local) { | ||
%c1 = addrspacecast ptr addrspace(3) %local to ptr addrspace(4) | ||
%c2 = call ptr addrspace(3) @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr addrspace(3) %local, null | ||
%b1 = icmp eq ptr addrspace(3) %c2, null | ||
ret i1 %b1 | ||
} | ||
|
||
; Casting a local pointer to a global pointer. | ||
; The uses of c2 will be replaced with null. | ||
; CHECK: @kernel5(ptr addrspace(3) %local) | ||
define i1 @kernel5(ptr addrspace(3) %local) { | ||
%c1 = addrspacecast ptr addrspace(3) %local to ptr addrspace(4) | ||
%c2 = call ptr addrspace(1) @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr addrspace(1) null, null | ||
%b1 = icmp eq ptr addrspace(1) %c2, null | ||
ret i1 %b1 | ||
} | ||
|
||
; Casting a local pointer to a private pointer. | ||
; The uses of c2 will be replaced with null. | ||
; CHECK: @kernel6(ptr addrspace(3) %local) | ||
define i1 @kernel6(ptr addrspace(3) %local) { | ||
%c1 = addrspacecast ptr addrspace(3) %local to ptr addrspace(4) | ||
%c2 = call ptr @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr null, null | ||
%b1 = icmp eq ptr %c2, null | ||
ret i1 %b1 | ||
} | ||
|
||
; Casting a private pointer to a private pointer. | ||
; The uses of c2 will be replaced with %private. | ||
; CHECK: @kernel7(ptr %private) | ||
define i1 @kernel7(ptr %private) { | ||
%c1 = addrspacecast ptr %private to ptr addrspace(4) | ||
%c2 = call ptr @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr %private, null | ||
%b1 = icmp eq ptr %c2, null | ||
ret i1 %b1 | ||
} | ||
|
||
; Casting a private pointer to a global pointer. | ||
; The uses of c2 will be replaced with null. | ||
; CHECK: @kernel8(ptr %private) | ||
define i1 @kernel8(ptr %private) { | ||
%c1 = addrspacecast ptr %private to ptr addrspace(4) | ||
%c2 = call ptr addrspace(1) @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr addrspace(1) null, null | ||
%b1 = icmp eq ptr addrspace(1) %c2, null | ||
ret i1 %b1 | ||
} | ||
|
||
; Casting a private pointer to a local pointer. | ||
; The uses of c2 will be replaced with null. | ||
; CHECK: @kernel9(ptr %private) | ||
define i1 @kernel9(ptr %private) { | ||
%c1 = addrspacecast ptr %private to ptr addrspace(4) | ||
%c2 = call ptr addrspace(3) @llvm.spv.generic.cast.to.ptr.explicit(ptr addrspace(4) %c1) | ||
; CHECK: %b1 = icmp eq ptr addrspace(3) null, null | ||
%b1 = icmp eq ptr addrspace(3) %c2, null | ||
ret i1 %b1 | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does the compiler not complain about this being unused?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's a virtual function, so I don't think so