https://github.com/Qi-Ye-079 updated https://github.com/llvm/llvm-project/pull/244409
>From 67d1c84eaa714804a3ec2ec3f1a7607848e3f2de Mon Sep 17 00:00:00 2001 From: "Ye, Qi" <[email protected]> Date: Thu, 19 Nov 2026 13:32:27 -0800 Subject: [PATCH] [CIR] Enable CallConvLowering for SPIRV triples --- clang/docs/CIR/ABILowering.md | 5 +- clang/lib/CIR/Lowering/CIRPasses.cpp | 17 ++- .../CodeGen/call-conv-lowering-spirv-target.c | 22 ++++ ...rargs-with-nonzero-default-address-space.c | 7 +- .../CodeGenHLSL/call-conv-lowering-spirv.hlsl | 8 ++ .../CodeGenSYCL/kernel-caller-entry-point.cpp | 4 +- clang/test/CIR/CodeGenSYCL/sycl-external.cpp | 8 +- .../CodeGenCUDASPIRV/copy-aggregate-byval.cu | 3 + .../test/CodeGenCUDASPIRV/kernel-args-abi.cu | 43 +++++++ .../test/CodeGenCUDASPIRV/kernel-argument.cu | 3 + .../CodeGenSPIRV/abi-classify-arg-types.c | 114 ++++++++++++++++++ .../CodeGenSPIRV/abi-classify-cxx-records.cpp | 81 +++++++++++++ .../test/CodeGenSPIRV/abi-classify-intel.cpp | 52 ++++++++ .../CodeGenSPIRV/abi-classify-return-types.c | 83 +++++++++++++ clang/test/CodeGenSPIRV/kernel-ptr-arg.c | 2 + .../CodeGenSYCL/spirv-kernel-args-abi.cpp | 79 ++++++++++++ 16 files changed, 515 insertions(+), 16 deletions(-) create mode 100644 clang/test/CIR/CodeGen/call-conv-lowering-spirv-target.c create mode 100644 clang/test/CIR/CodeGenHLSL/call-conv-lowering-spirv.hlsl create mode 100644 clang/test/CodeGenCUDASPIRV/kernel-args-abi.cu create mode 100644 clang/test/CodeGenSPIRV/abi-classify-arg-types.c create mode 100644 clang/test/CodeGenSPIRV/abi-classify-cxx-records.cpp create mode 100644 clang/test/CodeGenSPIRV/abi-classify-intel.cpp create mode 100644 clang/test/CodeGenSPIRV/abi-classify-return-types.c create mode 100644 clang/test/CodeGenSYCL/spirv-kernel-args-abi.cpp diff --git a/clang/docs/CIR/ABILowering.md b/clang/docs/CIR/ABILowering.md index 8d9a525e98e2e2..662caa83030e96 100644 --- a/clang/docs/CIR/ABILowering.md +++ b/clang/docs/CIR/ABILowering.md @@ -543,9 +543,8 @@ indirect call, and each `cir.va_arg`. ### Enabling the Pass `cir-call-conv-lowering` runs by default whenever ClangIR code generation is -enabled on an x86_64 target. On every other triple the pipeline omits the -pass, because x86_64 System V is the only classifier implemented so far, and -signatures reach the LLVM dialect in their high-level form. +enabled on a target that has a classifier. On other triples the pipeline omits +the pass, and signatures reach the LLVM dialect in their high-level form. `-fno-clangir-call-conv-lowering` turns the pass off and `-fclangir-call-conv-lowering` turns it back on. The last one on the diff --git a/clang/lib/CIR/Lowering/CIRPasses.cpp b/clang/lib/CIR/Lowering/CIRPasses.cpp index 02d6a144d6a96f..54b80b9dcb03e0 100644 --- a/clang/lib/CIR/Lowering/CIRPasses.cpp +++ b/clang/lib/CIR/Lowering/CIRPasses.cpp @@ -23,7 +23,9 @@ namespace cir { /// Map a target triple to the ABI target that drives CallConvLowering. /// Returns None for targets whose calling convention is not yet implemented. -static CallConvTarget getCallConvTarget(const llvm::Triple &triple) { +static CallConvTarget +getCallConvTarget(const llvm::Triple &triple, + cir::LoweringOptionsAttr loweringLangOpts) { // Windows is not supported. UEFI shares its convention. if (triple.getArch() == llvm::Triple::x86_64 && !triple.isOSWindowsOrUEFI()) return CallConvTarget::X86_64; @@ -32,6 +34,11 @@ static CallConvTarget getCallConvTarget(const llvm::Triple &triple) { if (triple.isAArch64() && !triple.isOSWindows() && triple.getArch() != llvm::Triple::aarch64_32) return CallConvTarget::AArch64; + bool isHLSL = loweringLangOpts && loweringLangOpts.getHlsl(); + // AMDGCN-flavored SPIR-V follows the AMDGPU ABI instead. HLSL's integer + // promotion rules and resource types are not modeled yet. + if (triple.isSPIRV() && triple.getVendor() != llvm::Triple::AMD && !isHLSL) + return CallConvTarget::SPIRV; return CallConvTarget::None; } @@ -136,7 +143,9 @@ runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirContext, // CallConvLowering rewrites signatures and call sites using the classifier, // so it must run after CXXABILowering has lowered C++ ABI types to plain // records the classifier can handle. - CallConvTarget target = getCallConvTarget(*triple); + auto loweringLangOpts = theModule->getAttrOfType<cir::LoweringOptionsAttr>( + cir::CIRDialect::getLoweringOptionsAttrName()); + CallConvTarget target = getCallConvTarget(*triple, loweringLangOpts); if (target != CallConvTarget::None) { // Source the ABI-compatibility version from the module's serialized // #cir.lowering_options so a reloaded .cir classifies the same way @@ -144,9 +153,7 @@ runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirContext, // attribute at module construction; if it is absent fall back to the // LangOptions default, matching how LowerModule reads the same attribute. auto compat = clang::LangOptions::ClangABI::Latest; - if (auto loweringLangOpts = - theModule->getAttrOfType<cir::LoweringOptionsAttr>( - cir::CIRDialect::getLoweringOptionsAttrName())) + if (loweringLangOpts) compat = static_cast<clang::LangOptions::ClangABI>( loweringLangOpts.getClangAbiCompat()); pm.addPass(mlir::createCallConvLoweringPass( diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-spirv-target.c b/clang/test/CIR/CodeGen/call-conv-lowering-spirv-target.c new file mode 100644 index 00000000000000..bf263f01570283 --- /dev/null +++ b/clang/test/CIR/CodeGen/call-conv-lowering-spirv-target.c @@ -0,0 +1,22 @@ +// Checks which SPIR-V triples run CallConvLowering in the Clang pipeline. The +// SPIR-V ABI extends a char argument with signext, so it is there only when the +// pass runs. + +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -fclangir -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix=ON +// RUN: %clang_cc1 -triple spirv32-unknown-unknown -fclangir -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix=ON +// RUN: %clang_cc1 -triple spirv64-intel -fclangir -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix=ON +// RUN: %clang_cc1 -triple spirv-unknown-vulkan1.3 -fclangir -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix=ON +// RUN: %clang_cc1 -triple spirv64-amd-amdhsa -fclangir -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix=OFF +// RUN: %clang_cc1 -triple spir64-unknown-unknown -fclangir -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix=OFF +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -emit-llvm %s -o - \ +// RUN: | FileCheck %s --check-prefix=ON + +// ON: define {{.*}}void @take_char(i8 noundef signext %{{[^,)]+}}) +// OFF: define {{.*}}void @take_char(i8 noundef %{{[^,)]+}}) +void take_char(char c) {} diff --git a/clang/test/CIR/CodeGen/varargs-with-nonzero-default-address-space.c b/clang/test/CIR/CodeGen/varargs-with-nonzero-default-address-space.c index 3247361347188f..61b524673f88e1 100644 --- a/clang/test/CIR/CodeGen/varargs-with-nonzero-default-address-space.c +++ b/clang/test/CIR/CodeGen/varargs-with-nonzero-default-address-space.c @@ -1,10 +1,13 @@ -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -fcuda-is-device -fclangir -emit-cir %s -o %t.cir +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -fcuda-is-device -fclangir -fno-clangir-call-conv-lowering -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR -// RUN: %clang_cc1 -triple spirv64-unknown-unknown -fcuda-is-device -fclangir -emit-llvm %s -o %t-cir.ll +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -fcuda-is-device -fclangir -fno-clangir-call-conv-lowering -emit-llvm %s -o %t-cir.ll // RUN: FileCheck --input-file=%t-cir.ll %s -check-prefixes=LLVM,LLVMCIR // RUN: %clang_cc1 -triple spirv64-unknown-unknown -fcuda-is-device -emit-llvm %s -o %t.ll // RUN: FileCheck --input-file=%t.ll %s -check-prefix=LLVM +// RUN: not %clang_cc1 -triple spirv64-unknown-unknown -fcuda-is-device -fclangir -emit-llvm %s -o /dev/null 2>&1 | FileCheck %s -check-prefix=NYI +// NYI: error: 'cir.va_arg' op not yet implemented in CallConvLowering for SPIR-V +// // TODO(cir): Add aggregate va_arg once va_arg ABI lowering supports SPIR-V. int testva(int n, ...) { __builtin_va_list ap; diff --git a/clang/test/CIR/CodeGenHLSL/call-conv-lowering-spirv.hlsl b/clang/test/CIR/CodeGenHLSL/call-conv-lowering-spirv.hlsl new file mode 100644 index 00000000000000..a9c08e608c80a3 --- /dev/null +++ b/clang/test/CIR/CodeGenHLSL/call-conv-lowering-spirv.hlsl @@ -0,0 +1,8 @@ +// The Clang pipeline doesn't run CallConvLowering for HLSL on SPIR-V, so a bool +// argument isn't extended, matching classic codegen. + +// RUN: %clang_cc1 -x hlsl -triple spirv-unknown-vulkan-library -finclude-default-header -emit-llvm -disable-llvm-passes %s -o - | FileCheck %s +// RUN: %clang_cc1 -x hlsl -triple spirv-unknown-vulkan-library -finclude-default-header -fclangir -emit-llvm %s -o - | FileCheck %s + +// CHECK: define {{.*}}void @_Z9take_boolb(i1 noundef %{{[^,)]+}}) +export void take_bool(bool b) {} diff --git a/clang/test/CIR/CodeGenSYCL/kernel-caller-entry-point.cpp b/clang/test/CIR/CodeGenSYCL/kernel-caller-entry-point.cpp index cf9a923d466144..0218a91b5ca52b 100644 --- a/clang/test/CIR/CodeGenSYCL/kernel-caller-entry-point.cpp +++ b/clang/test/CIR/CodeGenSYCL/kernel-caller-entry-point.cpp @@ -87,11 +87,13 @@ void test() { // CIR: cir.call @_ZNK1KclEv // CIR: cir.return -// LLVM-OGCG-LABEL: define {{.*}}spir_kernel void @_ZTS2KN +// The kernel object is copied byval. +// LLVM-OGCG-LABEL: define {{.*}}spir_kernel void @_ZTS2KN(ptr noundef byval(%struct.K) align 1 %{{[^,)]+}}) // LLVM: call {{.*}}void @_ZNK1KclEv // OGCG: call {{.*}}spir_func void @_ZNK1KclEv // LLVM-OGCG: ret void // LLVM-OGCG-NOT: define {{.*}}@_Z18kernel_single_task +// LLVM-OGCG-LABEL: define {{.*}}spir_kernel void @_ZTS8MemberKN(ptr noundef byval(%struct.K) align 1 %{{[^,)]+}}) // On ELF, the kernel caller entry point definition is dso_local in CIR, // CIR-lowered LLVM IR, and classic CodeGen alike. diff --git a/clang/test/CIR/CodeGenSYCL/sycl-external.cpp b/clang/test/CIR/CodeGenSYCL/sycl-external.cpp index ff0ca3af7d21be..3655f9b80fc4cd 100644 --- a/clang/test/CIR/CodeGenSYCL/sycl-external.cpp +++ b/clang/test/CIR/CodeGenSYCL/sycl-external.cpp @@ -1,9 +1,9 @@ // RUN: %clang_cc1 -fsycl-is-device -triple spirv64-unknown-unknown -fclangir -emit-cir %s -o %t.cir // RUN: FileCheck --input-file=%t.cir %s -check-prefix=CIR // RUN: %clang_cc1 -fsycl-is-device -triple spirv64-unknown-unknown -fclangir -emit-llvm %s -o %t-cir.ll -// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefixes=LLVM,LLVM-CIR +// RUN: FileCheck --input-file=%t-cir.ll %s --check-prefix=LLVM // RUN: %clang_cc1 -fsycl-is-device -triple spirv64-unknown-unknown -emit-llvm %s -o %t.ll -// RUN: FileCheck --input-file=%t.ll %s --check-prefixes=LLVM,OGCG +// RUN: FileCheck --input-file=%t.ll %s --check-prefix=LLVM // Verify that sycl_external functions are emitted in device code, matching // classic CodeGen, and that their definitions carry the "sycl-module-id" @@ -62,9 +62,7 @@ int squareNoAttr(int x) { return x * x; } // LLVM: define spir_func noundef i32 @_Z8withAttrv() #[[EXT]] // LLVM: define spir_func noundef i32 @_Z4ret1v() #[[NOEXT:[0-9]+]] // LLVM: define spir_func void @_Z5tFuncIiEvT_(i32 noundef %{{.*}}) #[[EXT]] -// ClangIR does not yet pass kernel arguments byval. -// LLVM-CIR: define spir_kernel void @_ZTS2KN({{.*}}) #[[EXT]] -// OGCG: define spir_kernel void @_ZTS2KN(ptr noundef byval({{.*}}) #[[EXT]] +// LLVM: define spir_kernel void @_ZTS2KN(ptr noundef byval({{.*}}) #[[EXT]] // LLVM-NOT: @_Z8declOnlyv // LLVM-NOT: @_Z12squareNoAttri // LLVM-DAG: attributes #[[EXT]] = { {{.*}}"sycl-module-id"="{{.*}}sycl-external.cpp" } diff --git a/clang/test/CodeGenCUDASPIRV/copy-aggregate-byval.cu b/clang/test/CodeGenCUDASPIRV/copy-aggregate-byval.cu index a011298c1a1705..cf1361afad14a7 100644 --- a/clang/test/CodeGenCUDASPIRV/copy-aggregate-byval.cu +++ b/clang/test/CodeGenCUDASPIRV/copy-aggregate-byval.cu @@ -16,6 +16,9 @@ // RUN: llvm-dis %t.bc -o %t.ll // RUN: FileCheck %s --input-file=%t.ll +// RUN: %if cir-enabled %{ %clang -emit-llvm -S --cuda-device-only --offload=spirv32 -fclangir -nocudalib -nocudainc %s -o - | FileCheck %s %} +// RUN: %if cir-enabled %{ %clang -emit-llvm -S --cuda-device-only --offload=spirv64 -fclangir -nocudalib -nocudainc %s -o - | FileCheck %s %} + class GpuData { public: __attribute__((host)) __attribute__((device)) GpuData(int* src) {} diff --git a/clang/test/CodeGenCUDASPIRV/kernel-args-abi.cu b/clang/test/CodeGenCUDASPIRV/kernel-args-abi.cu new file mode 100644 index 00000000000000..847848bc5db87b --- /dev/null +++ b/clang/test/CodeGenCUDASPIRV/kernel-args-abi.cu @@ -0,0 +1,43 @@ +// Checks how CUDA kernels on SPIR-V pass their arguments. The default address +// space is Generic. + +// RUN: %clang_cc1 -fcuda-is-device -triple spirv64 -x cuda -emit-llvm %s -o - | FileCheck %s +// RUN: %if cir-enabled %{ %clang_cc1 -fcuda-is-device -triple spirv64 -x cuda -fclangir -emit-llvm %s -o - | FileCheck %s %} + +#define __device__ __attribute__((device)) +#define __global__ __attribute__((global)) + +struct Small { int a, b; }; +struct NonCopyable { + int i; + __device__ NonCopyable(const NonCopyable &); +}; + +// Device function pointers keep the Generic address space. +// CHECK: define spir_func void @_Z7dev_ptrPi(ptr addrspace(4) noundef %{{[^,)]+}}) +__device__ void dev_ptr(int *p) {} + +// Generic kernel pointers are passed as CrossWorkgroup ones; the body still +// sees a Generic pointer. +// CHECK-LABEL: define spir_kernel void @_Z5k_ptrPi(ptr addrspace(1) noundef %{{[^,)]+}}) +// CHECK: call spir_func void @_Z7dev_ptrPi(ptr addrspace(4) noundef %{{[^,)]+}}) +__global__ void k_ptr(int *p) { dev_ptr(p); } + +// Pointers in any other address space are left alone. +// CHECK: define spir_kernel void @_Z12k_global_ptrPU3AS1i(ptr addrspace(1) noundef %{{[^,)]+}}) +__global__ void k_global_ptr(__attribute__((address_space(1))) int *p) {} + +// CHECK: define spir_kernel void @_Z13k_private_ptrPU3AS0i(ptr noundef %{{[^,)]+}}) +__global__ void k_private_ptr(__attribute__((address_space(0))) int *p) {} + +// Device compilations copy kernel records byval, even one that cannot be +// copied bitwise. +// CHECK: define spir_kernel void @_Z7k_small5Small(ptr noundef byval(%struct.Small) align 4 %{{[^,)]+}}) +__global__ void k_small(Small s) {} + +// CHECK: define spir_kernel void @_Z14k_non_copyable11NonCopyable(ptr noundef byval(%struct.NonCopyable) align 4 %{{[^,)]+}}) +__global__ void k_non_copyable(NonCopyable n) {} + +// Sub-word kernel arguments are extended. +// CHECK: define spir_kernel void @_Z5k_extcb(i8 noundef signext %{{[^,)]+}}, i1 noundef zeroext %{{[^,)]+}}) +__global__ void k_ext(char c, bool b) {} diff --git a/clang/test/CodeGenCUDASPIRV/kernel-argument.cu b/clang/test/CodeGenCUDASPIRV/kernel-argument.cu index 37ee76ad1b1456..49d30a0b975327 100644 --- a/clang/test/CodeGenCUDASPIRV/kernel-argument.cu +++ b/clang/test/CodeGenCUDASPIRV/kernel-argument.cu @@ -16,6 +16,9 @@ // RUN: llvm-dis %t.bc -o %t.ll // RUN: FileCheck %s --input-file=%t.ll +// RUN: %if cir-enabled %{ %clang -emit-llvm -S --cuda-device-only --offload=spirv32 -fclangir -nocudalib -nocudainc %s -o - | FileCheck %s %} +// RUN: %if cir-enabled %{ %clang -emit-llvm -S --cuda-device-only --offload=spirv64 -fclangir -nocudalib -nocudainc %s -o - | FileCheck %s %} + // CHECK: define // CHECK-SAME: spir_kernel void @_Z6kernelPi(ptr addrspace(1) noundef diff --git a/clang/test/CodeGenSPIRV/abi-classify-arg-types.c b/clang/test/CodeGenSPIRV/abi-classify-arg-types.c new file mode 100644 index 00000000000000..c27257ad3fb9f0 --- /dev/null +++ b/clang/test/CodeGenSPIRV/abi-classify-arg-types.c @@ -0,0 +1,114 @@ +// Checks how non-kernel SPIR-V functions pass arguments. + +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -emit-llvm %s -o - | FileCheck %s +// RUN: %if cir-enabled %{ %clang_cc1 -triple spirv64-unknown-unknown -fclangir -emit-llvm %s -o - | FileCheck %s %} + +typedef struct {} empty_t; +typedef struct { int i; } i32_wrapper_t; +typedef struct { char a, b, c; } small_t; +typedef struct { int data[17]; } big_t; +typedef union { int i; float f; } int_float_t; +typedef union { + int *ip; + float *fp; +} __attribute__((transparent_union)) transparent_t; +typedef short short4 __attribute__((ext_vector_type(4))); +typedef float float3 __attribute__((ext_vector_type(3))); + +// Sub-word integers and bool carry signext or zeroext per their signedness. +// CHECK: define spir_func void @arg_bool(i1 noundef zeroext %{{[^,)]+}}) +void arg_bool(_Bool b) {} + +// CHECK: define spir_func void @arg_char(i8 noundef signext %{{[^,)]+}}) +void arg_char(char c) {} + +// CHECK: define spir_func void @arg_uchar(i8 noundef zeroext %{{[^,)]+}}) +void arg_uchar(unsigned char c) {} + +// CHECK: define spir_func void @arg_short(i16 noundef signext %{{[^,)]+}}) +void arg_short(short s) {} + +// CHECK: define spir_func void @arg_ushort(i16 noundef zeroext %{{[^,)]+}}) +void arg_ushort(unsigned short s) {} + +// The call site carries the same extension as the callee. +// CHECK-LABEL: define spir_func void @call_char(i8 noundef signext %{{[^,)]+}}) +// CHECK: call spir_func void @arg_char(i8 noundef signext %{{[^,)]+}}) +void call_char(char c) { arg_char(c); } + +// Word-sized and wider scalars, pointers and vectors pass through unchanged. +// CHECK: define spir_func void @arg_int(i32 noundef %{{[^,)]+}}) +void arg_int(int i) {} + +// CHECK: define spir_func void @arg_long(i64 noundef %{{[^,)]+}}) +void arg_long(long l) {} + +// CHECK: define spir_func void @arg_half(half noundef %{{[^,)]+}}) +void arg_half(_Float16 h) {} + +// CHECK: define spir_func void @arg_float(float noundef %{{[^,)]+}}) +void arg_float(float f) {} + +// CHECK: define spir_func void @arg_double(double noundef %{{[^,)]+}}) +void arg_double(double d) {} + +// Pointers keep their address space outside kernels. +// CHECK: define spir_func void @arg_ptr(ptr noundef %{{[^,)]+}}) +void arg_ptr(int *p) {} + +// CHECK: define spir_func void @arg_short4(<4 x i16> noundef %{{[^,)]+}}) +void arg_short4(short4 v) {} + +// CHECK: define spir_func void @arg_float3(<3 x float> noundef %{{[^,)]+}}) +void arg_float3(float3 v) {} + +// SPIR-V has no 128-bit integer, so a _BitInt wider than 64 bits is passed +// indirectly. +// CHECK: define spir_func void @arg_bitint64(i64 noundef %{{[^,)]+}}) +void arg_bitint64(_BitInt(64) i) {} + +// CHECK: define spir_func void @arg_bitint65(ptr noundef byval(i128) align 8 %{{[^,)]+}}) +void arg_bitint65(_BitInt(65) i) {} + +// Every aggregate is copied byval, including an empty one. +// CHECK: define spir_func void @arg_empty(ptr noundef byval(%struct.empty_t) align 1 %{{[^,)]+}}, i32 noundef %{{[^,)]+}}) +void arg_empty(empty_t e, int i) {} + +// CHECK: define spir_func void @arg_i32_wrapper(ptr noundef byval(%struct.i32_wrapper_t) align 4 %{{[^,)]+}}) +void arg_i32_wrapper(i32_wrapper_t w) {} + +// CHECK: define spir_func void @arg_small(ptr noundef byval(%struct.small_t) align 1 %{{[^,)]+}}) +void arg_small(small_t s) {} + +// CHECK: define spir_func void @arg_big(ptr noundef byval(%struct.big_t) align 4 %{{[^,)]+}}) +void arg_big(big_t b) {} + +// CHECK: define spir_func void @arg_union(ptr noundef byval(%union.int_float_t) align 4 %{{[^,)]+}}) +void arg_union(int_float_t u) {} + +// CHECK: define spir_func void @arg_cfloat(ptr noundef byval({ float, float }) align 4 %{{[^,)]+}}) +void arg_cfloat(_Complex float c) {} + +// CHECK: define spir_func void @arg_cdouble(ptr noundef byval({ double, double }) align 8 %{{[^,)]+}}) +void arg_cdouble(_Complex double c) {} + +// A transparent union is passed as its first member. +// CHECK: define spir_func void @arg_transparent(ptr %{{[^,)]+}}) +void arg_transparent(transparent_t u) {} + +// Call sites, direct and indirect, pass the same byval pointer. +// CHECK-LABEL: define spir_func void @call_small(ptr noundef byval(%struct.small_t) align 1 %{{[^,)]+}}) +// CHECK: call spir_func void @arg_small(ptr noundef byval(%struct.small_t) align 1 %{{[^,)]+}}) +void call_small(small_t s) { arg_small(s); } + +// CHECK-LABEL: define spir_func void @call_fnptr(ptr noundef %{{[^,)]+}}, ptr noundef byval(%struct.small_t) align 1 %{{[^,)]+}}) +// CHECK: call spir_func void %{{.*}}(ptr noundef byval(%struct.small_t) align 1 %{{[^,)]+}}) +void call_fnptr(void (*fp)(small_t), small_t s) { fp(s); } + +void variadic(int n, ...); + +// Variadic arguments are classified like fixed ones, after default argument +// promotion. +// CHECK-LABEL: define spir_func void @call_variadic(ptr noundef byval(%struct.small_t) align 1 %{{[^,)]+}}, i8 noundef signext %{{[^,)]+}}) +// CHECK: call spir_func void (i32, ...) @variadic(i32 noundef 2, ptr noundef byval(%struct.small_t) align 1 %{{[^,)]+}}, i32 noundef %{{[^,)]+}}) +void call_variadic(small_t s, char c) { variadic(2, s, c); } diff --git a/clang/test/CodeGenSPIRV/abi-classify-cxx-records.cpp b/clang/test/CodeGenSPIRV/abi-classify-cxx-records.cpp new file mode 100644 index 00000000000000..d291680a963b4f --- /dev/null +++ b/clang/test/CodeGenSPIRV/abi-classify-cxx-records.cpp @@ -0,0 +1,81 @@ +// Checks how SPIR-V functions pass and return C++ records, where triviality +// changes the result. Without a device language the default address space is +// the private one and kernels don't copy non-trivial records byval. + +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -emit-llvm %s -o - | FileCheck %s +// RUN: %if cir-enabled %{ %clang_cc1 -triple spirv64-unknown-unknown -fclangir -emit-llvm %s -o - | FileCheck %s %} + +struct Empty {}; +struct Small { int a, b; }; +struct NonTrivialDtor { int i; ~NonTrivialDtor(); }; +struct NonTrivialCopy { int i; NonTrivialCopy(const NonTrivialCopy &); }; +struct TrivialBase { int i; }; +struct Derived : TrivialBase { int j; }; +typedef int TrivialBase::*mdp_t; +typedef void (TrivialBase::*mfp_t)(); + +// A record that cannot be passed in registers is built directly in the sret. +// CHECK-LABEL: define spir_func void @_Z20ret_non_trivial_dtorv( +// CHECK-SAME: ptr dead_on_unwind noalias writable sret(%struct.NonTrivialDtor) align 4 %[[RET:[^,)]+]]) +// CHECK: %[[I:[^,)]+]] = getelementptr {{.*}}%struct.NonTrivialDtor, ptr %[[RET]], i32 0, i32 0 +// CHECK: store i32 1, ptr %[[I]], align 4 +NonTrivialDtor ret_non_trivial_dtor() { return {1}; } + +// CHECK-LABEL: define spir_func void @_Z20use_non_trivial_dtorv() +// CHECK: call spir_func void @_Z20ret_non_trivial_dtorv(ptr dead_on_unwind writable sret(%struct.NonTrivialDtor) align 4 %{{[^,)]+}}) +void use_non_trivial_dtor() { NonTrivialDtor x = ret_non_trivial_dtor(); } + +// A record that cannot be copied bitwise is passed as the address of the +// caller's copy, not byval. +// CHECK: define spir_func noundef i32 @_Z20arg_non_trivial_copy14NonTrivialCopy(ptr nofreeobj noundef align 4 dead_on_return dereferenceable(4) %{{[^,)]+}}) +int arg_non_trivial_copy(NonTrivialCopy n) { return n.i; } + +// CHECK-LABEL: define spir_func noundef i32 @_Z21call_non_trivial_copyR14NonTrivialCopy( +// CHECK: call spir_func void @_ZN14NonTrivialCopyC1ERKS_(ptr noundef nonnull align 4 dereferenceable(4) %[[TMP:[^,)]+]], ptr +// CHECK: call spir_func noundef i32 @_Z20arg_non_trivial_copy14NonTrivialCopy(ptr nofreeobj noundef align 4 dead_on_return dereferenceable(4) %[[TMP]]) +int call_non_trivial_copy(NonTrivialCopy &n) { return arg_non_trivial_copy(n); } + +// Trivially copyable records, with a base or empty, are copied byval and +// returned through sret. +// CHECK: define spir_func void @_Z11arg_derived7Derived(ptr noundef byval(%struct.Derived) align 4 %{{[^,)]+}}) +void arg_derived(Derived d) {} + +// CHECK: define spir_func void @_Z11ret_derivedv(ptr dead_on_unwind noalias writable sret(%struct.Derived) align 4 %{{[^,)]+}}) +Derived ret_derived() { return {}; } + +// CHECK: define spir_func void @_Z9arg_empty5Emptyi(ptr noundef byval(%struct.Empty) align 1 %{{[^,)]+}}, i32 noundef %{{[^,)]+}}) +void arg_empty(Empty e, int i) {} + +// CHECK: define spir_func void @_Z9ret_emptyv(ptr dead_on_unwind noalias writable sret(%struct.Empty) align 1 %{{[^,)]+}}) +Empty ret_empty() { return {}; } + +// A data member pointer is an offset, passed and returned as an integer. +// CHECK: define spir_func void @_Z7arg_mdpM11TrivialBasei(i64 %{{[^,)]+}}) +void arg_mdp(mdp_t p) {} + +// CHECK: define spir_func i64 @_Z7ret_mdpM11TrivialBasei(i64 %{{[^,)]+}}) +mdp_t ret_mdp(mdp_t p) { return p; } + +// A member function pointer is a pair, so it is copied byval and returned +// through sret. +// CHECK: define spir_func void @_Z7arg_mfpM11TrivialBaseFvvE(ptr noundef byval({ i64, i64 }) align 8 %{{[^,)]+}}) +void arg_mfp(mfp_t p) {} + +// CHECK: define spir_func void @_Z7ret_mfpM11TrivialBaseFvvE(ptr dead_on_unwind noalias writable sret({ i64, i64 }) align 8 %{{[^,)]+}}, ptr noundef byval({ i64, i64 }) align 8 %{{[^,)]+}}) +mfp_t ret_mfp(mfp_t p) { return p; } + +// Kernels copy trivially copyable records byval, but pass a non-trivial one +// like other functions: device languages are what make kernels copy it. +// CHECK: define spir_kernel void @_Z7k_small5Small(ptr noundef byval(%struct.Small) align 4 %{{[^,)]+}}) +__attribute__((device_kernel)) void k_small(Small s) {} + +// CHECK: define spir_kernel void @_Z18k_non_trivial_copy14NonTrivialCopy(ptr nofreeobj noundef align 4 dead_on_return dereferenceable(4) %{{[^,)]+}}) +__attribute__((device_kernel)) void k_non_trivial_copy(NonTrivialCopy n) {} + +// Kernel pointers and references in the default (private) address space are +// passed as CrossWorkgroup ones. std::nullptr_t is such a pointer too. +// CHECK: define spir_kernel void @_Z5k_refRi(ptr addrspace(1) noundef nonnull align 4 dereferenceable(4) %{{[^,)]+}}) +__attribute__((device_kernel)) void k_ref(int &r) {} + +// CHECK: define spir_kernel void @_Z9k_nullptrDn(ptr addrspace(1) %{{[^,)]+}}) +__attribute__((device_kernel)) void k_nullptr(decltype(nullptr) n) {} diff --git a/clang/test/CodeGenSPIRV/abi-classify-intel.cpp b/clang/test/CodeGenSPIRV/abi-classify-intel.cpp new file mode 100644 index 00000000000000..f0e4f81f8dfdd8 --- /dev/null +++ b/clang/test/CodeGenSPIRV/abi-classify-intel.cpp @@ -0,0 +1,52 @@ +// Checks how SPIR-V functions on the Intel vendor triple pass and return +// values. The vendor makes the Generic address space the default without a +// device language, so kernels coerce Generic pointers but don't copy +// non-trivial records byval. + +// RUN: %clang_cc1 -triple spirv64-intel -emit-llvm %s -o - | FileCheck %s +// RUN: %if cir-enabled %{ %clang_cc1 -triple spirv64-intel -fclangir -emit-llvm %s -o - | FileCheck %s %} + +struct Small { int a, b; }; +struct NonTrivialCopy { int i; NonTrivialCopy(const NonTrivialCopy &); }; +struct NoCopy { + NoCopy(); + NoCopy(const NoCopy &); + ~NoCopy(); + int a; +}; + +// Pointers keep their address space outside kernels. +// CHECK: define spir_func void @_Z7dev_ptrPi(ptr addrspace(4) noundef %{{[^,)]+}}) +void dev_ptr(int *p) {} + +// A record that cannot be copied is built in place through a Generic sret. +// CHECK-LABEL: define spir_func void @_Z11ret_no_copyv( +// CHECK-SAME: ptr addrspace(4) dead_on_unwind noalias writable sret(%struct.NoCopy) align 4 %[[RET:[^,)]+]]) +// CHECK: call spir_func addrspace(9) void @_ZN6NoCopyC1Ev(ptr addrspace(4) noundef align 4 dereferenceable_or_null(4) %[[RET]]) +NoCopy ret_no_copy() { return NoCopy(); } + +// CHECK-LABEL: define spir_func void @_Z11use_no_copyv() +// CHECK: call spir_func addrspace(9) void @_Z11ret_no_copyv(ptr addrspace(4) dead_on_unwind writable sret(%struct.NoCopy) align 4 %{{[^,)]+}}) +void use_no_copy() { NoCopy x = ret_no_copy(); } + +// Generic kernel pointers are passed as CrossWorkgroup ones; the body still +// sees a Generic pointer. +// CHECK-LABEL: define spir_kernel void @_Z5k_ptrPi(ptr addrspace(1) noundef %{{[^,)]+}}) +// CHECK: call spir_func addrspace(9) void @_Z7dev_ptrPi(ptr addrspace(4) noundef %{{[^,)]+}}) +__attribute__((device_kernel)) void k_ptr(int *p) { dev_ptr(p); } + +// Pointers in any other address space are left alone, including +// std::nullptr_t, which is a private pointer. +// CHECK: define spir_kernel void @_Z13k_private_ptrPU3AS0i(ptr noundef %{{[^,)]+}}) +__attribute__((device_kernel)) void k_private_ptr(__attribute__((address_space(0))) int *p) {} + +// CHECK: define spir_kernel void @_Z9k_nullptrDn(ptr %{{[^,)]+}}) +__attribute__((device_kernel)) void k_nullptr(decltype(nullptr) n) {} + +// Kernels copy trivially copyable records byval, but pass a non-trivial one +// as the address of the caller's copy. +// CHECK: define spir_kernel void @_Z7k_small5Small(ptr noundef byval(%struct.Small) align 4 %{{[^,)]+}}) +__attribute__((device_kernel)) void k_small(Small s) {} + +// CHECK: define spir_kernel void @_Z18k_non_trivial_copy14NonTrivialCopy(ptr nofreeobj noundef align 4 dead_on_return dereferenceable(4) %{{[^,)]+}}) +__attribute__((device_kernel)) void k_non_trivial_copy(NonTrivialCopy n) {} diff --git a/clang/test/CodeGenSPIRV/abi-classify-return-types.c b/clang/test/CodeGenSPIRV/abi-classify-return-types.c new file mode 100644 index 00000000000000..c9852275b1012d --- /dev/null +++ b/clang/test/CodeGenSPIRV/abi-classify-return-types.c @@ -0,0 +1,83 @@ +// Checks how non-kernel SPIR-V functions return values. + +// RUN: %clang_cc1 -triple spirv64-unknown-unknown -emit-llvm %s -o - | FileCheck %s +// RUN: %if cir-enabled %{ %clang_cc1 -triple spirv64-unknown-unknown -fclangir -emit-llvm %s -o - | FileCheck %s %} + +typedef struct {} empty_t; +typedef struct { int i; } i32_wrapper_t; +typedef struct { int data[17]; } big_t; +typedef union { int i; float f; } int_float_t; +typedef short short4 __attribute__((ext_vector_type(4))); + +// Sub-word integers and bool carry signext or zeroext per their signedness. +// CHECK: define spir_func zeroext i1 @ret_bool() +_Bool ret_bool(void) { return 1; } + +// CHECK: define spir_func signext i8 @ret_char() +char ret_char(void) { return 1; } + +// CHECK: define spir_func zeroext i8 @ret_uchar() +unsigned char ret_uchar(void) { return 1; } + +// CHECK: define spir_func signext i16 @ret_short() +short ret_short(void) { return 1; } + +// CHECK: define spir_func zeroext i16 @ret_ushort() +unsigned short ret_ushort(void) { return 1; } + +// The call site carries the same extension as the callee. +// CHECK-LABEL: define spir_func signext i8 @call_ret_char() +// CHECK: call spir_func signext i8 @ret_char() +char call_ret_char(void) { return ret_char(); } + +// Word-sized and wider scalars, pointers and vectors are returned directly. +// CHECK: define spir_func i32 @ret_int() +int ret_int(void) { return 1; } + +// CHECK: define spir_func i64 @ret_long() +long ret_long(void) { return 1; } + +// CHECK: define spir_func float @ret_float() +float ret_float(void) { return 1; } + +// CHECK: define spir_func double @ret_double() +double ret_double(void) { return 1; } + +// CHECK: define spir_func ptr @ret_ptr(ptr noundef %{{[^,)]+}}) +int *ret_ptr(int *p) { return p; } + +// CHECK: define spir_func <4 x i16> @ret_short4(<4 x i16> noundef %{{[^,)]+}}) +short4 ret_short4(short4 v) { return v; } + +// SPIR-V has no 128-bit integer, so a _BitInt wider than 64 bits is returned +// through sret. +// CHECK: define spir_func i64 @ret_bitint64() +_BitInt(64) ret_bitint64(void) { return 1; } + +// CHECK: define spir_func void @ret_bitint65(ptr dead_on_unwind noalias writable sret(i128) align 8 %{{[^,)]+}}) +_BitInt(65) ret_bitint65(void) { return 1; } + +// CHECK-LABEL: define spir_func i32 @call_ret_bitint65() +// CHECK: call spir_func void @ret_bitint65(ptr dead_on_unwind writable sret(i128) align 8 %{{[^,)]+}}) +int call_ret_bitint65(void) { return (int)ret_bitint65(); } + +// Every aggregate is returned through sret in the private address space, +// including an empty one. +// CHECK: define spir_func void @ret_empty(ptr dead_on_unwind noalias writable sret(%struct.empty_t) align 1 %{{[^,)]+}}) +empty_t ret_empty(void) { empty_t e; return e; } + +// CHECK: define spir_func void @ret_i32_wrapper(ptr dead_on_unwind noalias writable sret(%struct.i32_wrapper_t) align 4 %{{[^,)]+}}) +i32_wrapper_t ret_i32_wrapper(void) { i32_wrapper_t w = {1}; return w; } + +// CHECK: define spir_func void @ret_big(ptr dead_on_unwind noalias writable sret(%struct.big_t) align 4 %{{[^,)]+}}) +big_t ret_big(void) { big_t b = {}; return b; } + +// CHECK: define spir_func void @ret_union(ptr dead_on_unwind noalias writable sret(%union.int_float_t) align 4 %{{[^,)]+}}) +int_float_t ret_union(void) { int_float_t u = {1}; return u; } + +// CHECK: define spir_func void @ret_cfloat(ptr dead_on_unwind noalias writable sret({ float, float }) align 4 %{{[^,)]+}}) +_Complex float ret_cfloat(void) { return 1; } + +// CHECK-LABEL: define spir_func i32 @call_ret_i32_wrapper() +// CHECK: call spir_func void @ret_i32_wrapper(ptr dead_on_unwind writable sret(%struct.i32_wrapper_t) align 4 %{{[^,)]+}}) +int call_ret_i32_wrapper(void) { return ret_i32_wrapper().i; } diff --git a/clang/test/CodeGenSPIRV/kernel-ptr-arg.c b/clang/test/CodeGenSPIRV/kernel-ptr-arg.c index eb274f766e6695..46db58cc9a8c28 100644 --- a/clang/test/CodeGenSPIRV/kernel-ptr-arg.c +++ b/clang/test/CodeGenSPIRV/kernel-ptr-arg.c @@ -2,6 +2,8 @@ // RUN: %clang_cc1 -triple spirv32 %s -emit-llvm -o - | FileCheck %s // RUN: %clang_cc1 -triple spirv64 -fexperimental-abi-lowering %s -emit-llvm -o - | FileCheck %s // RUN: %clang_cc1 -triple spirv32 -fexperimental-abi-lowering %s -emit-llvm -o - | FileCheck %s +// RUN: %if cir-enabled %{ %clang_cc1 -triple spirv64 -fclangir %s -emit-llvm -o - | FileCheck %s %} +// RUN: %if cir-enabled %{ %clang_cc1 -triple spirv32 -fclangir %s -emit-llvm -o - | FileCheck %s %} // CHECK: define spir_func void @func(ptr noundef %{{.*}}) void func(int* arg) { diff --git a/clang/test/CodeGenSYCL/spirv-kernel-args-abi.cpp b/clang/test/CodeGenSYCL/spirv-kernel-args-abi.cpp new file mode 100644 index 00000000000000..6d209bfc592703 --- /dev/null +++ b/clang/test/CodeGenSYCL/spirv-kernel-args-abi.cpp @@ -0,0 +1,79 @@ +// Checks how SYCL device code on SPIR-V passes kernel objects and the +// arguments and results of device functions. The default address space is +// Generic. + +// RUN: %clang_cc1 -std=c++20 -fsycl-is-device -triple spirv64-unknown-unknown -emit-llvm %s -o - | FileCheck %s +// RUN: %if cir-enabled %{ %clang_cc1 -std=c++20 -fsycl-is-device -triple spirv64-unknown-unknown -fclangir -emit-llvm %s -o - | FileCheck %s %} + +template <typename KernelName, typename... Ts> +void sycl_kernel_launch(const char *, Ts...) {} + +template <typename KernelName, typename KernelType> +[[clang::sycl_kernel_entry_point(KernelName)]] +void kernel_single_task(KernelType kf) { kf(); } + +struct Small { int a, b; }; +struct NonCopyable { + int i; + NonCopyable(); + NonCopyable(const NonCopyable &); + ~NonCopyable(); +}; + +// Pointers and references keep their address space outside kernels. +// CHECK: define spir_func void @_Z7dev_ptrPiRi(ptr addrspace(4) noundef %{{[^,)]+}}, ptr addrspace(4) noundef align 4 dereferenceable(4) %{{[^,)]+}}) +[[clang::sycl_external]] void dev_ptr(int *p, int &r) {} + +// CHECK: define spir_func void @_Z14dev_global_ptrPU3AS1i(ptr addrspace(1) noundef %{{[^,)]+}}) +[[clang::sycl_external]] void dev_global_ptr(__attribute__((opencl_global)) int *p) {} + +// Sub-word integers and bool are extended. +// CHECK: define spir_func noundef signext i8 @_Z7dev_extabt(i8 noundef signext %{{[^,)]+}}, i1 noundef zeroext %{{[^,)]+}}, i16 noundef zeroext %{{[^,)]+}}) +[[clang::sycl_external]] signed char dev_ext(signed char c, bool b, unsigned short s) { return c; } + +// Trivially copyable records are copied byval and returned through a private +// sret. +// CHECK: define spir_func void @_Z9dev_small5Small(ptr noundef byval(%struct.Small) align 4 %{{[^,)]+}}) +[[clang::sycl_external]] void dev_small(Small s) {} + +// CHECK: define spir_func void @_Z13dev_ret_smallv(ptr dead_on_unwind noalias writable sret(%struct.Small) align 4 %{{[^,)]+}}) +[[clang::sycl_external]] Small dev_ret_small() { return {}; } + +// A record that cannot be copied is built in place through a Generic sret. +// CHECK-LABEL: define spir_func void @_Z8dev_makev( +// CHECK-SAME: ptr addrspace(4) dead_on_unwind noalias writable sret(%struct.NonCopyable) align 4 %[[RET:[^,)]+]]) +// CHECK: call spir_func void @_ZN11NonCopyableC1Ev(ptr addrspace(4) noundef align 4 dereferenceable_or_null(4) %[[RET]]) +[[clang::sycl_external]] NonCopyable dev_make() { return NonCopyable(); } + +// It is passed as the private address of the caller's copy, which the callee +// uses through a Generic pointer. +// CHECK-LABEL: define spir_func noundef i32 @_Z8dev_take11NonCopyable( +// CHECK-SAME: ptr nofreeobj noundef align 4 dereferenceable(4) %[[N:[^,)]+]]) +// CHECK: addrspacecast ptr %[[N]] to ptr addrspace(4) +[[clang::sycl_external]] int dev_take(NonCopyable n) { return n.i; } + +// CHECK-LABEL: define spir_func noundef i32 @_Z7dev_usev() +// CHECK: call spir_func void @_Z8dev_makev(ptr addrspace(4) dead_on_unwind writable sret(%struct.NonCopyable) align 4 %{{[^,)]+}}) +// CHECK: call spir_func noundef i32 @_Z8dev_take11NonCopyable(ptr nofreeobj noundef align 4 dereferenceable(4) %{{[^,)]+}}) +[[clang::sycl_external]] int dev_use() { NonCopyable n = dev_make(); return dev_take(n); } + +struct Functor { + int *p; + Small s; + void operator()() const { *p = s.a; } +}; + +struct KFunctor; +struct KLambda; +struct KEmpty; + +// Kernel objects are copied byval: a functor, a lambda with captures, and an +// empty lambda. +// CHECK: define spir_kernel void @_ZTS8KFunctor(ptr noundef byval(%struct.Functor) align 8 %{{[^,)]+}}) +// CHECK: define spir_kernel void @_ZTS7KLambda(ptr noundef byval(%class.anon{{[.0-9]*}}) align 8 %{{[^,)]+}}) +// CHECK: define spir_kernel void @_ZTS6KEmpty(ptr noundef byval(%class.anon{{[.0-9]*}}) align 1 %{{[^,)]+}}) +void test(int *p, Small s) { + kernel_single_task<KFunctor>(Functor{p, s}); + kernel_single_task<KLambda>([=] { *p = s.a; }); + kernel_single_task<KEmpty>([] {}); +} _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
