Skip to content

Commit 8dca6b9

Browse files
MrSidimsvmaksimo
authored andcommitted
Replace illegal address space casts asserts with errors
Purpose of this patch is to make the translator a little bit more user-friendly. For example following SYCL code: SYCL_EXTERNAL sycl::global_ptr<int> kernel_fun(int *ptr) { return sycl::global_ptr<int>((int*)&ptr); } is UB since it has an implicit cast from private address space to global. It's hard to detect such UB in FE, hence this diagnostic is moved to the translator (which already had it, but in form of an assert). Signed-off-by: Dmitry Sidorov <dmitry.sidorov@intel.com>
1 parent b6cf290 commit 8dca6b9

File tree

4 files changed

+115
-13
lines changed

4 files changed

+115
-13
lines changed

llvm-spirv/lib/SPIRV/SPIRVWriter.cpp

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -888,18 +888,22 @@ SPIRV::SPIRVInstruction *LLVMToSPIRV::transUnaryInst(UnaryInstruction *U,
888888
const auto SrcAddrSpace = Cast->getSrcTy()->getPointerAddressSpace();
889889
const auto DestAddrSpace = Cast->getDestTy()->getPointerAddressSpace();
890890
if (DestAddrSpace == SPIRAS_Generic) {
891-
assert(SrcAddrSpace != SPIRAS_Constant &&
892-
"Casts from constant address space to generic are illegal");
891+
getErrorLog().checkError(
892+
SrcAddrSpace != SPIRAS_Constant, SPIRVEC_InvalidModule,
893+
"Casts from constant address space to generic are illegal\n" +
894+
toString(U));
893895
BOC = OpPtrCastToGeneric;
894896
// In SPIR-V only casts to/from generic are allowed. But with
895897
// SPV_INTEL_usm_storage_classes we can also have casts from global_device
896898
// and global_host to global addr space and vice versa.
897899
} else if (SrcAddrSpace == SPIRAS_GlobalDevice ||
898900
SrcAddrSpace == SPIRAS_GlobalHost) {
899-
assert(
900-
(DestAddrSpace == SPIRAS_Global || DestAddrSpace == SPIRAS_Generic) &&
901-
"Casts from global_device/global_host only allowed to \
902-
global/generic");
901+
getErrorLog().checkError(DestAddrSpace == SPIRAS_Global ||
902+
DestAddrSpace == SPIRAS_Generic,
903+
SPIRVEC_InvalidModule,
904+
"Casts from global_device/global_host only "
905+
"allowed to global/generic\n" +
906+
toString(U));
903907
if (!BM->isAllowedToUseExtension(
904908
ExtensionID::SPV_INTEL_usm_storage_classes)) {
905909
if (DestAddrSpace == SPIRAS_Global)
@@ -910,10 +914,12 @@ SPIRV::SPIRVInstruction *LLVMToSPIRV::transUnaryInst(UnaryInstruction *U,
910914
}
911915
} else if (DestAddrSpace == SPIRAS_GlobalDevice ||
912916
DestAddrSpace == SPIRAS_GlobalHost) {
913-
assert(
914-
(SrcAddrSpace == SPIRAS_Global || SrcAddrSpace == SPIRAS_Generic) &&
915-
"Casts to global_device/global_host only allowed from \
916-
global/generic");
917+
getErrorLog().checkError(SrcAddrSpace == SPIRAS_Global ||
918+
SrcAddrSpace == SPIRAS_Generic,
919+
SPIRVEC_InvalidModule,
920+
"Casts to global_device/global_host only "
921+
"allowed from global/generic\n" +
922+
toString(U));
917923
if (!BM->isAllowedToUseExtension(
918924
ExtensionID::SPV_INTEL_usm_storage_classes)) {
919925
if (SrcAddrSpace == SPIRAS_Global)
@@ -923,9 +929,15 @@ SPIRV::SPIRVInstruction *LLVMToSPIRV::transUnaryInst(UnaryInstruction *U,
923929
BOC = OpCrossWorkgroupCastToPtrINTEL;
924930
}
925931
} else {
926-
assert(DestAddrSpace != SPIRAS_Constant &&
927-
"Casts from generic address space to constant are illegal");
928-
assert(SrcAddrSpace == SPIRAS_Generic);
932+
getErrorLog().checkError(
933+
SrcAddrSpace == SPIRAS_Generic, SPIRVEC_InvalidModule,
934+
"Casts from private/local/global address space are allowed only to "
935+
"generic\n" +
936+
toString(U));
937+
getErrorLog().checkError(
938+
DestAddrSpace != SPIRAS_Constant, SPIRVEC_InvalidModule,
939+
"Casts from generic address space to constant are illegal\n" +
940+
toString(U));
929941
BOC = OpGenericCastToPtr;
930942
}
931943
} else {
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: not llvm-spirv %t.bc -o %t.spv 2>&1 | FileCheck %s
3+
4+
; CHECK: InvalidModule: Invalid SPIR-V module: Casts from generic address space to constant are illegal
5+
6+
source_filename = "sample.cpp"
7+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
8+
target triple = "spir64-unknown-unknown-sycldevice"
9+
10+
; Function Attrs: nofree norecurse nounwind writeonly
11+
define dso_local spir_func void @_Z10kernel_funPi(i32 addrspace(4)* %ptr) {
12+
entry:
13+
%0 = addrspacecast i32 addrspace(4)* %ptr to i32 addrspace(2)*
14+
ret void
15+
}
16+
17+
!llvm.module.flags = !{!0}
18+
!opencl.spir.version = !{!1}
19+
!spirv.Source = !{!2}
20+
!llvm.ident = !{!3}
21+
22+
!0 = !{i32 1, !"wchar_size", i32 4}
23+
!1 = !{i32 1, i32 2}
24+
!2 = !{i32 4, i32 100000}
25+
!3 = !{!"clang version 12.0.0"}
26+
!4 = !{!5, !6, i64 0}
27+
!5 = !{!"_ZTSN2cl4sycl9multi_ptrIiLNS0_6access13address_spaceE1EEE", !6, i64 0}
28+
!6 = !{!"any pointer", !7, i64 0}
29+
!7 = !{!"omnipotent char", !8, i64 0}
30+
!8 = !{!"Simple C++ TBAA"}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: not llvm-spirv %t.bc -o %t.spv 2>&1 | FileCheck %s
3+
4+
; CHECK: InvalidModule: Invalid SPIR-V module: Casts from global_device/global_host only allowed to global/generic
5+
6+
source_filename = "sample.cpp"
7+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
8+
target triple = "spir64-unknown-unknown-sycldevice"
9+
10+
; Function Attrs: nofree norecurse nounwind writeonly
11+
define dso_local spir_func void @_Z10kernel_funPi(i32 addrspace(5)* %ptr) {
12+
entry:
13+
%0 = addrspacecast i32 addrspace(5)* %ptr to i32 addrspace(3)*
14+
ret void
15+
}
16+
17+
!llvm.module.flags = !{!0}
18+
!opencl.spir.version = !{!1}
19+
!spirv.Source = !{!2}
20+
!llvm.ident = !{!3}
21+
22+
!0 = !{i32 1, !"wchar_size", i32 4}
23+
!1 = !{i32 1, i32 2}
24+
!2 = !{i32 4, i32 100000}
25+
!3 = !{!"clang version 12.0.0"}
26+
!4 = !{!5, !6, i64 0}
27+
!5 = !{!"_ZTSN2cl4sycl9multi_ptrIiLNS0_6access13address_spaceE1EEE", !6, i64 0}
28+
!6 = !{!"any pointer", !7, i64 0}
29+
!7 = !{!"omnipotent char", !8, i64 0}
30+
!8 = !{!"Simple C++ TBAA"}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
; RUN: llvm-as %s -o %t.bc
2+
; RUN: not llvm-spirv %t.bc -o %t.spv 2>&1 | FileCheck %s
3+
4+
; CHECK: InvalidModule: Invalid SPIR-V module: Casts from private/local/global address space are allowed only to generic
5+
6+
source_filename = "sample.cpp"
7+
target datalayout = "e-i64:64-v16:16-v24:32-v32:32-v48:64-v96:128-v192:256-v256:256-v512:512-v1024:1024-n8:16:32:64"
8+
target triple = "spir64-unknown-unknown-sycldevice"
9+
10+
; Function Attrs: nofree norecurse nounwind writeonly
11+
define dso_local spir_func void @_Z10kernel_funPi(i32 addrspace(1)* %ptr) {
12+
entry:
13+
%0 = addrspacecast i32 addrspace(1)* %ptr to i32*
14+
ret void
15+
}
16+
17+
!llvm.module.flags = !{!0}
18+
!opencl.spir.version = !{!1}
19+
!spirv.Source = !{!2}
20+
!llvm.ident = !{!3}
21+
22+
!0 = !{i32 1, !"wchar_size", i32 4}
23+
!1 = !{i32 1, i32 2}
24+
!2 = !{i32 4, i32 100000}
25+
!3 = !{!"clang version 12.0.0"}
26+
!4 = !{!5, !6, i64 0}
27+
!5 = !{!"_ZTSN2cl4sycl9multi_ptrIiLNS0_6access13address_spaceE1EEE", !6, i64 0}
28+
!6 = !{!"any pointer", !7, i64 0}
29+
!7 = !{!"omnipotent char", !8, i64 0}
30+
!8 = !{!"Simple C++ TBAA"}

0 commit comments

Comments
 (0)