Skip to content

Commit

Permalink
Fix translation of OpGenericCastToPtrExplicit and add tests
Browse files Browse the repository at this point in the history
OpGenericCastToPtrExplicit should be translated to __to_global(),
__to_local() or __to_private() as appropriate, without name mangling,
as this is what Clang generates.

Fix this to remove name mangling for these builtins, and add transcoding
tests for both OpGenericCastToPtrExplicit and OpGenericCastToPtr.
  • Loading branch information
StuartDBrady authored and svenvh committed Sep 16, 2019
1 parent cf70d1f commit a073937
Show file tree
Hide file tree
Showing 4 changed files with 85 additions and 11 deletions.
6 changes: 4 additions & 2 deletions lib/SPIRV/OCLUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -777,7 +777,7 @@ bool isSamplerTy(Type *Ty) {
return STy && STy->hasName() && STy->getName() == kSPR2TypeName::Sampler;
}

bool isPipeBI(const StringRef MangledName) {
bool isPipeOrAddressSpaceCastBI(const StringRef MangledName) {
return MangledName == "write_pipe_2" || MangledName == "read_pipe_2" ||
MangledName == "write_pipe_2_bl" || MangledName == "read_pipe_2_bl" ||
MangledName == "write_pipe_4" || MangledName == "read_pipe_4" ||
Expand All @@ -796,7 +796,9 @@ bool isPipeBI(const StringRef MangledName) {
MangledName == "sub_group_reserve_write_pipe" ||
MangledName == "sub_group_reserve_read_pipe" ||
MangledName == "sub_group_commit_write_pipe" ||
MangledName == "sub_group_commit_read_pipe";
MangledName == "sub_group_commit_read_pipe" ||
MangledName == "to_global" || MangledName == "to_local" ||
MangledName == "to_private";
}

bool isEnqueueKernelBI(const StringRef MangledName) {
Expand Down
2 changes: 1 addition & 1 deletion lib/SPIRV/OCLUtil.h
Original file line number Diff line number Diff line change
Expand Up @@ -416,7 +416,7 @@ bool isPipeStorageInitializer(Instruction *Inst);
/// Check (isSamplerInitializer || isPipeStorageInitializer)
bool isSpecialTypeInitializer(Instruction *Inst);

bool isPipeBI(const StringRef MangledName);
bool isPipeOrAddressSpaceCastBI(const StringRef MangledName);
bool isEnqueueKernelBI(const StringRef MangledName);
bool isKernelQueryBI(const StringRef MangledName);

Expand Down
19 changes: 11 additions & 8 deletions lib/SPIRV/SPIRVUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -461,13 +461,14 @@ bool getSPIRVBuiltin(const std::string &OrigName, spv::BuiltIn &B) {
return getByName(R.str(), B);
}

// Enqueue kernel, kernel query and pipe built-ins are not mangled
// Enqueue kernel, kernel query, pipe and address space cast built-ins
// are not mangled.
bool isNonMangledOCLBuiltin(const StringRef &Name) {
if (!Name.startswith("__"))
return false;

return isEnqueueKernelBI(Name) || isKernelQueryBI(Name) ||
isPipeBI(Name.drop_front(2));
isPipeOrAddressSpaceCastBI(Name.drop_front(2));
}

bool oclIsBuiltin(const StringRef &Name, std::string *DemangledName,
Expand Down Expand Up @@ -1391,10 +1392,12 @@ bool eraseUselessFunctions(Module *M) {
}

// The mangling algorithm follows OpenCL pipe built-ins clang 3.8 CodeGen rules.
static SPIR::MangleError manglePipeBuiltin(const SPIR::FunctionDescriptor &Fd,
std::string &MangledName) {
assert(OCLUtil::isPipeBI(Fd.Name) &&
"Method is expected to be called only for pipe builtins!");
static SPIR::MangleError
manglePipeOrAddressSpaceCastBuiltin(const SPIR::FunctionDescriptor &Fd,
std::string &MangledName) {
assert(OCLUtil::isPipeOrAddressSpaceCastBI(Fd.Name) &&
"Method is expected to be called only for pipe and address space cast "
"builtins!");
if (Fd.isNull()) {
MangledName.assign(SPIR::FunctionDescriptor::nullString());
return SPIR::MANGLE_NULL_FUNC_DESCRIPTOR;
Expand Down Expand Up @@ -1443,8 +1446,8 @@ std::string mangleBuiltin(const std::string &UniqName,
SPIR::NameMangler Mangler(SPIR::SPIR20);
Mangler.mangle(FD, MangledName);
#else
if (OCLUtil::isPipeBI(BtnInfo->getUnmangledName())) {
manglePipeBuiltin(FD, MangledName);
if (OCLUtil::isPipeOrAddressSpaceCastBI(BtnInfo->getUnmangledName())) {
manglePipeOrAddressSpaceCastBuiltin(FD, MangledName);
} else {
SPIR::NameMangler Mangler(SPIR::SPIR20);
Mangler.mangle(FD, MangledName);
Expand Down
69 changes: 69 additions & 0 deletions test/transcoding/GenericCastToPtr.cl
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// RUN: %clang_cc1 -triple spir-unknown-unknown -O1 -cl-std=CL2.0 -emit-llvm-bc -finclude-default-header %s -o %t.bc
// RUN: llvm-spirv %t.bc -spirv-text -o - | FileCheck %s --check-prefix=CHECK-SPIRV
// RUN: llvm-spirv %t.bc -o %t.spv
// RUN: spirv-val %t.spv
// RUN: llvm-spirv -r %t.spv -o %t.rev.bc
// RUN: llvm-dis < %t.rev.bc | FileCheck %s --check-prefix=CHECK-LLVM

// CHECK-SPIRV: 4 GenericCastToPtr

// CHECK-LLVM-LABEL: @testGenericCastToPtrGlobal
// CHECK-LLVM: %0 = addrspacecast <2 x i16> addrspace(4)* %a to <2 x i16> addrspace(1)*

global short2 *testGenericCastToPtrGlobal(generic short2 *a) {
return (global short2 *)a;
}

// CHECK-SPIRV: 4 GenericCastToPtr

// CHECK-LLVM-LABEL: @testGenericCastToPtrLocal
// CHECK-LLVM: %0 = addrspacecast <2 x i16> addrspace(4)* %a to <2 x i16> addrspace(3)*

local short2 *testGenericCastToPtrLocal(generic short2 *a) {
return (local short2 *)a;
}

// CHECK-SPIRV: 4 GenericCastToPtr

// CHECK-LLVM-LABEL: @testGenericCastToPtrPrivate
// CHECK-LLVM: %0 = addrspacecast <2 x i16> addrspace(4)* %a to <2 x i16>*

private short2 *testGenericCastToPtrPrivate(generic short2 *a) {
return (private short2 *)a;
}

// CHECK-SPIRV: 5 GenericCastToPtrExplicit

// CHECK-LLVM-LABEL: @testGenericCastToPtrExplicitGlobal
// CHECK-LLVM: %[[VoidPtrCast:[0-9]+]] = bitcast <2 x i16> addrspace(4)* %a to i8 addrspace(4)*
// CHECK-LLVM-NEXT: %[[AddrSpaceCast:[0-9]+]] = bitcast i8 addrspace(4)* %[[VoidPtrCast]] to i8 addrspace(4)*
// CHECK-LLVM-NEXT: %.tmp = call spir_func i8 addrspace(1)* @__to_global(i8 addrspace(4)* %[[AddrSpaceCast]])
// CHECK-LLVM: bitcast i8 addrspace(1)* %{{[0-9]+}} to <2 x i16> addrspace(1)*

global short2 *testGenericCastToPtrExplicitGlobal(generic short2 *a) {
return to_global(a);
}

// CHECK-SPIRV: 5 GenericCastToPtrExplicit

// CHECK-LLVM-LABEL: @testGenericCastToPtrExplicitLocal
// CHECK-LLVM: %[[VoidPtrCast:[0-9]+]] = bitcast <2 x i16> addrspace(4)* %a to i8 addrspace(4)*
// CHECK-LLVM-NEXT: %[[AddrSpaceCast:[0-9]+]] = bitcast i8 addrspace(4)* %[[VoidPtrCast]] to i8 addrspace(4)*
// CHECK-LLVM-NEXT: %.tmp = call spir_func i8 addrspace(3)* @__to_local(i8 addrspace(4)* %[[AddrSpaceCast]])
// CHECK-LLVM: bitcast i8 addrspace(3)* %{{[0-9]+}} to <2 x i16> addrspace(3)*

local short2 *testGenericCastToPtrExplicitLocal(generic short2 *a) {
return to_local(a);
}

// CHECK-SPIRV: 5 GenericCastToPtrExplicit

// CHECK-LLVM-LABEL: @testGenericCastToPtrExplicitPrivate
// CHECK-LLVM: %[[VoidPtrCast:[0-9]+]] = bitcast <2 x i16> addrspace(4)* %a to i8 addrspace(4)*
// CHECK-LLVM-NEXT: %[[AddrSpaceCast:[0-9]+]] = bitcast i8 addrspace(4)* %[[VoidPtrCast]] to i8 addrspace(4)*
// CHECK-LLVM-NEXT: %.tmp = call spir_func i8* @__to_private(i8 addrspace(4)* %[[AddrSpaceCast]])
// CHECK-LLVM: bitcast i8* %{{[0-9]+}} to <2 x i16>*

private short2 *testGenericCastToPtrExplicitPrivate(generic short2 *a) {
return to_private(a);
}

0 comments on commit a073937

Please sign in to comment.