Skip to content

[SPIRV] Add intrinsic for OpGenericCastToPtrExplicit #137626

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
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions llvm/include/llvm/IR/IntrinsicsSPIRV.td
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
//
//===----------------------------------------------------------------------===//

def generic_ptr_ty : LLVMQualPointerType<4>;

let TargetPrefix = "spv" in {
def int_spv_assign_type : Intrinsic<[], [llvm_any_ty, llvm_metadata_ty]>;
def int_spv_assign_ptr_type : Intrinsic<[], [llvm_any_ty, llvm_metadata_ty, llvm_i32_ty], [ImmArg<ArgIndex<2>>]>;
Expand Down Expand Up @@ -146,4 +148,9 @@ let TargetPrefix = "spv" in {

// FPMaxErrorDecorationINTEL
def int_spv_assign_fpmaxerror_decoration: Intrinsic<[], [llvm_any_ty, llvm_metadata_ty]>;

// Convert between the generic storage class and a concrete one.
def int_spv_generic_cast_to_ptr_explicit
: DefaultAttrsIntrinsic<[llvm_anyptr_ty], [generic_ptr_ty],
[IntrNoMem, NoUndef<RetIndex>]>;
}
4 changes: 4 additions & 0 deletions llvm/lib/Target/SPIRV/SPIRVEmitIntrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,10 @@ Type *SPIRVEmitIntrinsics::deduceElementTypeHelper(
} else {
llvm_unreachable("Unknown handle type for spv_resource_getpointer.");
}
} else if (II && II->getIntrinsicID() ==
Intrinsic::spv_generic_cast_to_ptr_explicit) {
Ty = deduceElementTypeHelper(CI->getArgOperand(0), Visited,
UnknownElemTypeI8);
} else if (Function *CalledF = CI->getCalledFunction()) {
std::string DemangledName =
getOclOrSpirvBuiltinDemangledName(CalledF->getName());
Expand Down
15 changes: 15 additions & 0 deletions llvm/lib/Target/SPIRV/SPIRVInstructionSelector.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3120,6 +3120,21 @@ bool SPIRVInstructionSelector::selectIntrinsic(Register ResVReg,
.addUse(MemSemReg)
.constrainAllUses(TII, TRI, RBI);
}
case Intrinsic::spv_generic_cast_to_ptr_explicit: {
Register PtrReg = I.getOperand(I.getNumExplicitDefs() + 1).getReg();
SPIRV::StorageClass::StorageClass ResSC =
GR.getPointerStorageClass(ResType);
if (!isGenericCastablePtr(ResSC))
report_fatal_error("The target storage class is not castable from the "
"Generic storage class");
return BuildMI(BB, I, I.getDebugLoc(),
TII.get(SPIRV::OpGenericCastToPtrExplicit))
.addDef(ResVReg)
.addUse(GR.getSPIRVTypeID(ResType))
.addUse(PtrReg)
.addImm(ResSC)
.constrainAllUses(TII, TRI, RBI);
}
case Intrinsic::spv_lifetime_start:
case Intrinsic::spv_lifetime_end: {
unsigned Op = IID == Intrinsic::spv_lifetime_start ? SPIRV::OpLifetimeStart
Expand Down
44 changes: 44 additions & 0 deletions llvm/test/CodeGen/SPIRV/pointers/generic_cast_to_ptr_explicit.ll
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
; RUN: llc -O0 -verify-machineinstrs -mtriple=spirv64-unknown-unknown %s -o - | FileCheck %s
; RUN: %if spirv-tools %{ llc -O0 -mtriple=spirv64-unknown-unknown %s -o - -filetype=obj | spirv-val %}

; Make sure SPIRV operation function calls for generic_cast_to_ptr_explicit are lowered correctly.

; CHECK: %[[#Char:]] = OpTypeInt 8 0
; CHECK: %[[#GenericPtr:]] = OpTypePointer Generic %[[#Char]]
; CHECK: %[[#GlobalPtr:]] = OpTypePointer CrossWorkgroup %[[#Char]]
; CHECK: %[[#LocalPtr:]] = OpTypePointer Workgroup %[[#Char]]
; CHECK: %[[#PrivatePtr:]] = OpTypePointer Function %[[#Char]]

; CHECK: OpFunction %[[#GlobalPtr]]
; CHECK-NEXT: %[[#Arg:]] = OpFunctionParameter %[[#GenericPtr]]
; CHECK-NEXT: OpLabel
; CHECK-NEXT: OpGenericCastToPtrExplicit %[[#GlobalPtr]] %[[#Arg]] CrossWorkgroup
define ptr addrspace(1) @test_to_global(ptr addrspace(4) noundef %ptr) {
entry:
%cast = call spir_func noundef ptr addrspace(1) @llvm.spv.generic.cast.to.ptr.explicit.p1(ptr addrspace(4) noundef %ptr)
ret ptr addrspace(1) %cast
}

; CHECK: OpFunction %[[#LocalPtr]]
; CHECK-NEXT: %[[#Arg:]] = OpFunctionParameter %[[#GenericPtr]]
; CHECK-NEXT: OpLabel
; CHECK-NEXT: OpGenericCastToPtrExplicit %[[#LocalPtr]] %[[#Arg]] Workgroup
define ptr addrspace(3) @test_to_local(ptr addrspace(4) noundef %ptr) {
entry:
%cast = call spir_func noundef ptr addrspace(3) @llvm.spv.generic.cast.to.ptr.explicit.p3(ptr addrspace(4) noundef %ptr)
ret ptr addrspace(3) %cast
}

; CHECK: OpFunction %[[#PrivatePtr]]
; CHECK-NEXT: %[[#Arg:]] = OpFunctionParameter %[[#GenericPtr]]
; CHECK-NEXT: OpLabel
; CHECK-NEXT: OpGenericCastToPtrExplicit %[[#PrivatePtr]] %[[#Arg]] Function
define ptr @test_to_private(ptr addrspace(4) noundef %ptr) {
entry:
%cast = call spir_func noundef ptr @llvm.spv.generic.cast.to.ptr.explicit.p0(ptr addrspace(4) noundef %ptr)
ret ptr %cast
}

declare noundef ptr @llvm.spv.generic.cast.to.ptr.explicit.p0(ptr addrspace(4))
declare noundef ptr addrspace(1) @llvm.spv.generic.cast.to.ptr.explicit.p1(ptr addrspace(4))
declare noundef ptr addrspace(3) @llvm.spv.generic.cast.to.ptr.explicit.p3(ptr addrspace(4))
Loading