https://github.com/skc7 updated https://github.com/llvm/llvm-project/pull/233822
>From adb730fc9d3627d8ef4a89b978a00688872cc14b Mon Sep 17 00:00:00 2001 From: skc7 <[email protected]> Date: Tue, 1 Sep 2026 14:31:15 +0530 Subject: [PATCH 1/3] [CIR] Enable CallConvLowering for AMDGPU triples --- clang/lib/CIR/Lowering/CIRPasses.cpp | 6 +- .../call-conv-lowering-amdgpu-arg-types.c | 165 ++++++++++++++++++ .../call-conv-lowering-amdgpu-cxx-records.cpp | 74 ++++++++ .../call-conv-lowering-amdgpu-return-types.c | 158 +++++++++++++++++ .../CIR/CodeGenCUDA/vtable-host-device.cu | 4 +- .../call-conv-lowering-amdgpu-kernel-args.hip | 137 +++++++++++++++ .../CodeGenHIP/cleanup-alloca-addrspace.hip | 8 +- 7 files changed, 544 insertions(+), 8 deletions(-) create mode 100644 clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-arg-types.c create mode 100644 clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-cxx-records.cpp create mode 100644 clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-return-types.c create mode 100644 clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip diff --git a/clang/lib/CIR/Lowering/CIRPasses.cpp b/clang/lib/CIR/Lowering/CIRPasses.cpp index af35409dd647fc..3594300f27daad 100644 --- a/clang/lib/CIR/Lowering/CIRPasses.cpp +++ b/clang/lib/CIR/Lowering/CIRPasses.cpp @@ -26,6 +26,8 @@ static CallConvTarget getCallConvTarget(const llvm::Triple &triple) { // Windows is not supported. UEFI shares its convention. if (triple.getArch() == llvm::Triple::x86_64 && !triple.isOSWindowsOrUEFI()) return CallConvTarget::X86_64; + if (triple.isAMDGPU()) + return CallConvTarget::AMDGPU; return CallConvTarget::None; } @@ -124,8 +126,8 @@ runCIRToCIRPasses(mlir::ModuleOp theModule, mlir::MLIRContext &mlirContext, if (enableCallConvLowering) { // 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. Only the x86_64 System V classifier - // is implemented; other targets are left unchanged. + // records the classifier can handle. Only the x86_64 System V and AMDGPU + // classifiers are implemented. Other targets are left unchanged. CallConvTarget target = getCallConvTarget(*triple); if (target != CallConvTarget::None) { // Source the ABI-compatibility version from the module's serialized diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-arg-types.c b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-arg-types.c new file mode 100644 index 00000000000000..ef65e90a528166 --- /dev/null +++ b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-arg-types.c @@ -0,0 +1,165 @@ +// REQUIRES: amdgpu-registered-target +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fclangir -fclangir-call-conv-lowering -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fclangir -fclangir-call-conv-lowering -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +// Checks that CallConvLowering classifies arguments of ordinary AMDGPU +// functions, which use the default convention rather than the kernel one. + +// TODO(cir): Add the argument cases still NYI in CallConvLowering: +// - Aggregates of 33 to 64 bits, which classic coerces to [2 x i32]. +// abiTypeToCIR has no array case yet. +// - Aggregates over 64 bits that fit the 16-register budget. The classifier +// returns Direct with no coerce type, which the bridge rejects. +// - Aggregates past the register budget, passed byref in addrspace(5). +// - Structs with a flexible array member and transparent unions. The bridge +// does not set those record flags yet. +// - Arguments passed through an ellipsis. + +typedef struct {} empty_t; +typedef struct { char c; } i8_wrapper_t; +typedef struct { int i; } i32_wrapper_t; +typedef struct { float f; } f32_wrapper_t; +typedef struct { int *p; } ptr_wrapper_t; +typedef struct { char a, b; } char2_t; +typedef struct { char a, b, c; } small_t; +typedef union { int i; float f; } int_float_t; +typedef short short4 __attribute__((ext_vector_type(4))); +typedef float float3 __attribute__((ext_vector_type(3))); + +void arg_void(void) {} + +// CIR: cir.func {{.*}}@arg_void() +// LLVM: define {{.*}}void @arg_void() + +// Sub-word integers and bool carry signext or zeroext per their signedness. +void arg_bool(_Bool b) {} + +// CIR: cir.func {{.*}}@arg_bool(%arg0: !cir.bool {{.*}}llvm.zeroext +// LLVM: define {{.*}}void @arg_bool(i1 noundef zeroext %{{.*}}) + +void arg_char(char c) {} + +// CIR: cir.func {{.*}}@arg_char(%arg0: !s8i {{.*}}llvm.signext +// LLVM: define {{.*}}void @arg_char(i8 noundef signext %{{.*}}) + +void arg_uchar(unsigned char c) {} + +// CIR: cir.func {{.*}}@arg_uchar(%arg0: !u8i {{.*}}llvm.zeroext +// LLVM: define {{.*}}void @arg_uchar(i8 noundef zeroext %{{.*}}) + +void arg_short(short s) {} + +// CIR: cir.func {{.*}}@arg_short(%arg0: !s16i {{.*}}llvm.signext +// LLVM: define {{.*}}void @arg_short(i16 noundef signext %{{.*}}) + +// Word-sized and wider scalars pass through unchanged. +void arg_int(int i) {} + +// CIR: cir.func {{.*}}@arg_int(%arg0: !s32i {{.*}}) +// LLVM: define {{.*}}void @arg_int(i32 noundef %{{.*}}) + +void arg_long(long l) {} + +// CIR: cir.func {{.*}}@arg_long(%arg0: !s64i {{.*}}) +// LLVM: define {{.*}}void @arg_long(i64 noundef %{{.*}}) + +void arg_float(float f) {} + +// CIR: cir.func {{.*}}@arg_float(%arg0: !cir.float {{.*}}) +// LLVM: define {{.*}}void @arg_float(float noundef %{{.*}}) + +void arg_double(double d) {} + +// CIR: cir.func {{.*}}@arg_double(%arg0: !cir.double {{.*}}) +// LLVM: define {{.*}}void @arg_double(double noundef %{{.*}}) + +void arg_half(_Float16 h) {} + +// CIR: cir.func {{.*}}@arg_half(%arg0: !cir.f16 {{.*}}) +// LLVM: define {{.*}}void @arg_half(half noundef %{{.*}}) + +// Outside a HIP kernel a generic pointer stays generic. +void arg_ptr(int *p) {} + +// CIR: cir.func {{.*}}@arg_ptr(%arg0: !cir.ptr<!s32i> {{.*}}) +// LLVM: define {{.*}}void @arg_ptr(ptr noundef %{{.*}}) + +// A _BitInt wider than 64 bits and vectors, including 16-bit and 3-element +// ones, pass whole. +void arg_bitint65(_BitInt(65) i) {} + +// CIR: cir.func {{.*}}@arg_bitint65(%arg0: !cir.int<s, 65, bitint> {{.*}}) +// LLVM: define {{.*}}void @arg_bitint65(i65 noundef %{{.*}}) + +void arg_short4(short4 v) {} + +// CIR: cir.func {{.*}}@arg_short4(%arg0: !cir.vector<4 x !s16i> {{.*}}) +// LLVM: define {{.*}}void @arg_short4(<4 x i16> noundef %{{.*}}) + +void arg_float3(float3 v) {} + +// CIR: cir.func {{.*}}@arg_float3(%arg0: !cir.vector<3 x !cir.float> {{.*}}) +// LLVM: define {{.*}}void @arg_float3(<3 x float> noundef %{{.*}}) + +// An empty struct is dropped from the signature. +void arg_empty(empty_t e) {} + +// CIR: cir.func {{.*}}@arg_empty() +// LLVM: define {{.*}}void @arg_empty() + +// Single-element structs pass as their element. The callee rebuilds the +// struct in a private (addrspace 5) slot. +void arg_i8_wrapper(i8_wrapper_t w) {} + +// CIR: cir.func {{.*}}@arg_i8_wrapper(%arg0: !s8i{{.*}}) +// CIR: cir.alloca "coerce" {{.*}} : !cir.ptr<{{.*}}, target_address_space(5)> +// LLVM: define {{.*}}void @arg_i8_wrapper(i8 %{{.*}}) + +void arg_i32_wrapper(i32_wrapper_t w) {} + +// CIR: cir.func {{.*}}@arg_i32_wrapper(%arg0: !s32i{{.*}}) +// LLVM: define {{.*}}void @arg_i32_wrapper(i32 %{{.*}}) + +void arg_f32_wrapper(f32_wrapper_t w) {} + +// CIR: cir.func {{.*}}@arg_f32_wrapper(%arg0: !cir.float{{.*}}) +// LLVM: define {{.*}}void @arg_f32_wrapper(float %{{.*}}) + +void arg_ptr_wrapper(ptr_wrapper_t w) {} + +// CIR: cir.func {{.*}}@arg_ptr_wrapper(%arg0: !cir.ptr<{{[^,]*}}>{{.*}}) +// LLVM: define {{.*}}void @arg_ptr_wrapper(ptr %{{.*}}) + +// Aggregates up to 32 bits are packed into one integer register. +void arg_char2(char2_t s) {} + +// CIR: cir.func {{.*}}@arg_char2(%arg0: !u16i{{.*}}) +// LLVM: define {{.*}}void @arg_char2(i16 %{{.*}}) + +void arg_small(small_t s) {} + +// CIR: cir.func {{.*}}@arg_small(%arg0: !u32i{{.*}}) +// LLVM: define {{.*}}void @arg_small(i32 %{{.*}}) + +void arg_union(int_float_t u) {} + +// CIR: cir.func {{.*}}@arg_union(%arg0: !u32i{{.*}}) +// LLVM: define {{.*}}void @arg_union(i32 %{{.*}}) + +// The call site is coerced the same way as the callee. +void call_small(small_t s) { arg_small(s); } + +// CIR: cir.func {{.*}}@call_small(%arg0: !u32i{{.*}}) +// CIR: cir.call @arg_small(%{{.*}}){{.*}}: (!u32i{{.*}}) -> () +// LLVM: define {{.*}}void @call_small(i32 %{{.*}}) +// LLVM: call void @arg_small(i32 %{{.*}}) + +// The call site carries the same extension as the callee. +void call_char(char c) { arg_char(c); } + +// LLVM: define {{.*}}void @call_char(i8 noundef signext %{{.*}}) +// LLVM: call void @arg_char(i8 noundef signext %{{.*}}) diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-cxx-records.cpp b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-cxx-records.cpp new file mode 100644 index 00000000000000..e434f9d37bcfc1 --- /dev/null +++ b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-cxx-records.cpp @@ -0,0 +1,74 @@ +// REQUIRES: amdgpu-registered-target +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fclangir -fclangir-call-conv-lowering -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fclangir -fclangir-call-conv-lowering -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +// Checks that CallConvLowering classifies C++ records on ordinary AMDGPU +// functions, where C++ rules on triviality and layout change the result. + +// TODO(cir): Add the C++ record cases still NYI in CallConvLowering: +// - Non-trivial records as arguments. Classic passes them indirectly as +// ptr addrspace(5), which the bridge cannot express yet. +// - Member function pointers, lowered to a 128-bit pair that the bridge +// cannot coerce yet. +// - Records of 33 to 64 bits, such as a base plus a field, which classic +// coerces to [2 x i32]. + +struct Empty {}; + +struct NonTrivialDtor { + int i; + ~NonTrivialDtor(); +}; + +struct NonCopyable { + int i; + NonCopyable(); + NonCopyable(const NonCopyable &) = delete; +}; + +struct TrivialBase { int i; }; +struct DerivedSingle : TrivialBase {}; +struct MemberPtrHolder { int TrivialBase::*p; }; + +// A record that cannot be passed in registers returns through a generic sret. +NonTrivialDtor ret_non_trivial_dtor() { return {1}; } + +// CIR: cir.func {{.*}}@_Z20ret_non_trivial_dtorv(%arg0: !cir.ptr<!rec_NonTrivialDtor> {{.*}}llvm.sret = !rec_NonTrivialDtor +// LLVM: define {{.*}}void @_Z20ret_non_trivial_dtorv(ptr dead_on_unwind noalias writable sret(%struct.NonTrivialDtor) align 4 %{{.*}}) + +NonCopyable ret_non_copyable() { return NonCopyable(); } + +// CIR: cir.func {{.*}}@_Z16ret_non_copyablev(%arg0: !cir.ptr<!rec_NonCopyable> {{.*}}llvm.sret = !rec_NonCopyable +// LLVM: define {{.*}}void @_Z16ret_non_copyablev(ptr dead_on_unwind noalias writable sret(%struct.NonCopyable) align 4 %{{.*}}) + +// A base subobject holding the only field still counts as a single element. +DerivedSingle ret_derived_single() { return {}; } + +// CIR: cir.func {{.*}}@_Z18ret_derived_singlev() -> !s32i +// LLVM: define {{.*}}i32 @_Z18ret_derived_singlev() + +void arg_derived_single(DerivedSingle d) {} + +// CIR: cir.func {{.*}}@_Z18arg_derived_single13DerivedSingle(%arg0: !s32i{{.*}}) +// LLVM: define {{.*}}void @_Z18arg_derived_single13DerivedSingle(i32 %{{.*}}) + +// A data member pointer is lowered to an i64 offset before classification. +MemberPtrHolder ret_member_ptr() { return {nullptr}; } + +// CIR: cir.func {{.*}}@_Z14ret_member_ptrv() -> !s64i +// LLVM: define {{.*}}i64 @_Z14ret_member_ptrv() + +void arg_member_ptr(MemberPtrHolder h) {} + +// CIR: cir.func {{.*}}@_Z14arg_member_ptr15MemberPtrHolder(%arg0: !s64i{{.*}}) +// LLVM: define {{.*}}void @_Z14arg_member_ptr15MemberPtrHolder(i64 %{{.*}}) + +// An empty C++ record is one byte in size but is still dropped. +void arg_empty(Empty e) {} + +// CIR: cir.func {{.*}}@_Z9arg_empty5Empty() +// LLVM: define {{.*}}void @_Z9arg_empty5Empty() diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-return-types.c b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-return-types.c new file mode 100644 index 00000000000000..e62674bd5a1702 --- /dev/null +++ b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-return-types.c @@ -0,0 +1,158 @@ +// REQUIRES: amdgpu-registered-target +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fclangir -fclangir-call-conv-lowering -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -fclangir -fclangir-call-conv-lowering -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +// Checks that CallConvLowering classifies return values of ordinary AMDGPU +// functions, which use the default convention rather than the kernel one. + +// TODO(cir): Add the return cases still NYI in CallConvLowering: +// - Aggregates of 33 to 64 bits, which classic coerces to [2 x i32]. +// abiTypeToCIR has no array case yet. +// - Aggregates over 64 bits that fit the 16-register budget. The classifier +// returns Direct with no coerce type, which the bridge rejects. +// - Larger aggregates, returned through sret in addrspace(5). The bridge +// cannot express an indirect address space yet. +// - Structs with a flexible array member. The bridge does not set that +// record flag yet. +// - 3-element vector returns, blocked on CIRGen's vec3 load and store NYI. + +typedef struct {} empty_t; +typedef struct { char c; } i8_wrapper_t; +typedef struct { int i; } i32_wrapper_t; +typedef struct { float f; } f32_wrapper_t; +typedef struct { int *p; } ptr_wrapper_t; +typedef struct { char a, b; } char2_t; +typedef struct { char a, b, c; } small_t; +typedef union { int i; float f; } int_float_t; + +void ret_void(void) {} + +// CIR: cir.func {{.*}}@ret_void() +// LLVM: define {{.*}}void @ret_void() + +// Sub-word integers and bool carry signext or zeroext per their signedness. +_Bool ret_bool(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_bool() -> (!cir.bool {{.*}}llvm.zeroext +// LLVM: define {{.*}}zeroext i1 @ret_bool() + +char ret_char(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_char() -> (!s8i {{.*}}llvm.signext +// LLVM: define {{.*}}signext i8 @ret_char() + +unsigned char ret_uchar(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_uchar() -> (!u8i {{.*}}llvm.zeroext +// LLVM: define {{.*}}zeroext i8 @ret_uchar() + +short ret_short(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_short() -> (!s16i {{.*}}llvm.signext +// LLVM: define {{.*}}signext i16 @ret_short() + +// Word-sized and wider scalars and pointers return unchanged. +int ret_int(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_int() -> {{\(?}}!s32i +// LLVM: define {{.*}}i32 @ret_int() + +long ret_long(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_long() -> {{\(?}}!s64i +// LLVM: define {{.*}}i64 @ret_long() + +float ret_float(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_float() -> {{\(?}}!cir.float +// LLVM: define {{.*}}float @ret_float() + +double ret_double(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_double() -> {{\(?}}!cir.double +// LLVM: define {{.*}}double @ret_double() + +_Float16 ret_half(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_half() -> {{\(?}}!cir.f16 +// LLVM: define {{.*}}half @ret_half() + +int *ret_ptr(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_ptr() -> {{\(?}}!cir.ptr<!s32i> +// LLVM: define {{.*}}ptr @ret_ptr() +_BitInt(65) ret_bitint65(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_bitint65() -> {{\(?}}!cir.int<s, 65, bitint> +// LLVM: define {{.*}}i65 @ret_bitint65() + +// An empty struct return becomes void. +empty_t ret_empty(void) { + empty_t e; + return e; +} + +// CIR: cir.func {{.*}}@ret_empty() +// LLVM: define {{.*}}void @ret_empty() + +// Single-element structs return as their element. +i8_wrapper_t ret_i8_wrapper(void) { + i8_wrapper_t w = {1}; + return w; +} + +// CIR: cir.func {{.*}}@ret_i8_wrapper() -> !s8i +// LLVM: define {{.*}}i8 @ret_i8_wrapper() + +i32_wrapper_t ret_i32_wrapper(void) { + i32_wrapper_t w = {1}; + return w; +} + +// CIR: cir.func {{.*}}@ret_i32_wrapper() -> !s32i +// LLVM: define {{.*}}i32 @ret_i32_wrapper() + +f32_wrapper_t ret_f32_wrapper(void) { + f32_wrapper_t w = {1.0f}; + return w; +} + +// CIR: cir.func {{.*}}@ret_f32_wrapper() -> !cir.float +// LLVM: define {{.*}}float @ret_f32_wrapper() + +ptr_wrapper_t ret_ptr_wrapper(void) { + ptr_wrapper_t w = {0}; + return w; +} + +// CIR: cir.func {{.*}}@ret_ptr_wrapper() -> !cir.ptr<{{[^>]*}}> +// LLVM: define {{.*}}ptr @ret_ptr_wrapper() + +// Aggregates up to 32 bits are packed into one integer register. +char2_t ret_char2(void) { + char2_t s = {1, 2}; + return s; +} + +// CIR: cir.func {{.*}}@ret_char2() -> !u16i +// LLVM: define {{.*}}i16 @ret_char2() + +small_t ret_small(void) { + small_t s = {1, 2, 3}; + return s; +} + +// CIR: cir.func {{.*}}@ret_small() -> !u32i +// LLVM: define {{.*}}i32 @ret_small() + +int_float_t ret_union(void) { + int_float_t u = {1}; + return u; +} + +// CIR: cir.func {{.*}}@ret_union() -> !u32i +// LLVM: define {{.*}}i32 @ret_union() diff --git a/clang/test/CIR/CodeGenCUDA/vtable-host-device.cu b/clang/test/CIR/CodeGenCUDA/vtable-host-device.cu index e7cb421ecfbbf6..0fb2e072291f32 100644 --- a/clang/test/CIR/CodeGenCUDA/vtable-host-device.cu +++ b/clang/test/CIR/CodeGenCUDA/vtable-host-device.cu @@ -53,10 +53,10 @@ const char *host_name() { return Square(3.0f).name(); } // HOST-DAG: declare {{.*}}void @__cxa_pure_virtual() // HOST-DAG: declare {{.*}}void @__cxa_deleted_virtual() -// CIR-DEV: cir.func weak {{.*}}@__cxa_deleted_virtual() requires_call_conv_lowering attributes {runtime_func} { +// CIR-DEV: cir.func weak {{.*}}@__cxa_deleted_virtual() attributes {runtime_func} { // CIR-DEV-NEXT: cir.trap // CIR-DEV-NEXT: } -// CIR-DEV: cir.func weak {{.*}}@__cxa_pure_virtual() requires_call_conv_lowering attributes {runtime_func} { +// CIR-DEV: cir.func weak {{.*}}@__cxa_pure_virtual() attributes {runtime_func} { // CIR-DEV-NEXT: cir.trap // CIR-DEV-NEXT: } diff --git a/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip b/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip new file mode 100644 index 00000000000000..d713a3bd4cf7e2 --- /dev/null +++ b/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip @@ -0,0 +1,137 @@ +// REQUIRES: amdgpu-registered-target +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -x hip -fcuda-is-device -fclangir -fclangir-call-conv-lowering -emit-cir %s -o %t.cir +// RUN: FileCheck --check-prefix=CIR --input-file=%t.cir %s +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -x hip -fcuda-is-device -fclangir -fclangir-call-conv-lowering -emit-llvm %s -o %t-cir.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t-cir.ll %s +// RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -x hip -fcuda-is-device -emit-llvm %s -o %t.ll +// RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s + +// Checks that CallConvLowering classifies AMDGPU kernel arguments under the +// kernel convention. + +// TODO(cir): Add multi-field aggregates, passed byref in addrspace(4), once +// CallConvLowering supports IndirectAliased. + +#define __global__ __attribute__((global)) +#define __device__ __attribute__((device)) + +typedef struct { int i; } i32_wrapper_t; +typedef struct { i32_wrapper_t w; } nested_t; +typedef struct { float f[1]; } f32_array1_t; +typedef struct { float *x; } ptr_wrapper_t; +typedef float float3 __attribute__((ext_vector_type(3))); +typedef float float4 __attribute__((ext_vector_type(4))); +typedef short short4 __attribute__((ext_vector_type(4))); + +// Word-sized scalars pass through unchanged. +__global__ void kern_scalar(int i, float f) {} + +// CIR: cir.func {{.*}}@_Z11kern_scalarif(%arg0: !s32i {{.*}}, %arg1: !cir.float {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_scalarif(i32 noundef %{{.*}}, float noundef %{{.*}}) + +__global__ void kern_wide(long l, double d) {} + +// CIR: cir.func {{.*}}@_Z9kern_wideld(%arg0: !s64i {{.*}}, %arg1: !cir.double {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_wideld(i64 noundef %{{.*}}, double noundef %{{.*}}) + +__global__ void kern_half(_Float16 h) {} + +// CIR: cir.func {{.*}}@_Z9kern_halfDF16_(%arg0: !cir.f16 {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_halfDF16_(half noundef %{{.*}}) + +__global__ void kern_bitint(_BitInt(65) b) {} + +// CIR: cir.func {{.*}}@_Z11kern_bitintDB65_(%arg0: !cir.int<s, 65, bitint> {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_bitintDB65_(i65 noundef %{{.*}}) + +// Vectors, including 16-bit and 3-element ones, pass whole. +__global__ void kern_float4(float4 v) {} + +// CIR: cir.func {{.*}}@_Z11kern_float4Dv4_f(%arg0: !cir.vector<4 x !cir.float> {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_float4Dv4_f(<4 x float> noundef %{{.*}}) + +__global__ void kern_short4(short4 v) {} + +// CIR: cir.func {{.*}}@_Z11kern_short4Dv4_s(%arg0: !cir.vector<4 x !s16i> {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_short4Dv4_s(<4 x i16> noundef %{{.*}}) + +__global__ void kern_float3(float3 v) {} + +// CIR: cir.func {{.*}}@_Z11kern_float3Dv3_f(%arg0: !cir.vector<3 x !cir.float> {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_float3Dv3_f(<3 x float> noundef %{{.*}}) + +// A pointer already in a non-generic address space keeps it. +__global__ void kern_global_ptr(__attribute__((address_space(1))) int *p) {} + +// CIR: cir.func {{.*}}@_Z15kern_global_ptrPU3AS1i(%arg0: !cir.ptr<!s32i, target_address_space(1)> {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z15kern_global_ptrPU3AS1i(ptr addrspace(1) noundef %{{.*}}) + +// HIP passes a generic pointer or reference as a global pointer and casts it +// back to generic in the kernel. +__global__ void kern_ptr(int *p) {} + +// CIR: cir.func {{.*}}@_Z8kern_ptrPi(%[[P:[^:]+]]: !cir.ptr<!s32i, target_address_space(1)> {{.*}}){{.*}}cc(amdgpu_kernel) +// CIR: cir.cast address_space %[[P]] : !cir.ptr<!s32i, target_address_space(1)> -> !cir.ptr<!s32i> +// LLVM: define {{.*}}amdgpu_kernel void @_Z8kern_ptrPi(ptr addrspace(1) noundef %{{.*}}) + +__global__ void kern_ref(int &r) {} + +// CIR: cir.func {{.*}}@_Z8kern_refRi(%{{.*}}: !cir.ptr<!s32i, target_address_space(1)> {{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z8kern_refRi(ptr addrspace(1) noundef nonnull align 4 dereferenceable(4) %{{.*}}) + +// A struct holding only a pointer is unwrapped first, so it is coerced too. +__global__ void kern_ptr_wrap(ptr_wrapper_t w) {} + +// CIR: cir.func {{.*}}@_Z13kern_ptr_wrap13ptr_wrapper_t(%{{.*}}: !cir.ptr<{{.*}}, target_address_space(1)>{{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z13kern_ptr_wrap13ptr_wrapper_t(ptr addrspace(1) %{{.*}}) + +// A device function keeps the generic pointer. +__device__ void dev_ptr(int *p) {} + +// CIR: cir.func {{.*}}@_Z7dev_ptrPi(%{{.*}}: !cir.ptr<!s32i> {{.*}}) +// LLVM: define {{.*}}void @_Z7dev_ptrPi(ptr noundef %{{.*}}) + +// A single-element struct kernel argument is passed as its element. +__global__ void kern_wrapper(i32_wrapper_t w) {} + +// CIR: cir.func {{.*}}@_Z12kern_wrapper13i32_wrapper_t(%arg0: !s32i{{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z12kern_wrapper13i32_wrapper_t(i32 %{{.*}}) + +// The element is found through nested structs and one-element arrays. +__global__ void kern_nested(nested_t n) {} + +// CIR: cir.func {{.*}}@_Z11kern_nested8nested_t(%arg0: !s32i{{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_nested8nested_t(i32 %{{.*}}) + +__global__ void kern_array1(f32_array1_t a) {} + +// CIR: cir.func {{.*}}@_Z11kern_array112f32_array1_t(%arg0: !cir.float{{.*}}){{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_array112f32_array1_t(float %{{.*}}) + +// Kernel arguments are read from the kernarg segment, so sub-word integers +// and bool are not extended, unlike in a device function. +__global__ void kern_char(char c) {} + +// CIR: cir.func {{.*}}@_Z9kern_charc(%arg0: !s8i {llvm.noundef}{{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_charc(i8 noundef %{{.*}}) + +__global__ void kern_uchar(unsigned char c) {} + +// CIR: cir.func {{.*}}@_Z10kern_ucharh(%arg0: !u8i {llvm.noundef}{{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z10kern_ucharh(i8 noundef %{{.*}}) + +__global__ void kern_short(short s) {} + +// CIR: cir.func {{.*}}@_Z10kern_shorts(%arg0: !s16i {llvm.noundef}{{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z10kern_shorts(i16 noundef %{{.*}}) + +__global__ void kern_bool(bool b) {} + +// CIR: cir.func {{.*}}@_Z9kern_boolb(%arg0: !cir.bool {llvm.noundef}{{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_boolb(i1 noundef %{{.*}}) + +// The same argument to a device function is extended. +__device__ void dev_char(char c) {} + +// CIR: cir.func {{.*}}@_Z8dev_charc(%arg0: !s8i {{.*}}llvm.signext +// LLVM: define {{.*}}void @_Z8dev_charc(i8 noundef signext %{{.*}}) diff --git a/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip b/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip index c55cd55a98d675..7cbb8ff8301ec0 100644 --- a/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip +++ b/clang/test/CIR/CodeGenHIP/cleanup-alloca-addrspace.hip @@ -7,9 +7,10 @@ // correct address space after flatten cfg. This is important for address-space // aware targets like amdgpu. -// CIR-FLAT-LABEL: cir.func {{.*}} @_Z1fv +// S is not trivially destructible, so f returns it through a generic sret +// pointer rather than a __retval alloca. +// CIR-FLAT-LABEL: cir.func {{.*}} @_Z1fv(%arg0: !cir.ptr<!rec_S> {{.*}}llvm.sret = !rec_S // CIR-FLAT: cir.alloca "__cleanup_dest_slot" {{.*}} : !cir.ptr<!s32i, target_address_space(5)> -// CIR-FLAT: cir.alloca "__retval" {{.*}} : !cir.ptr<!rec_S, target_address_space(5)> // CIR-FLAT: cir.alloca "nrvo" {{.*}} : !cir.ptr<!cir.bool, target_address_space(5)> // CIR-FLAT-LABEL: cir.func {{.*}} @_ZN1SD1Ev @@ -18,9 +19,8 @@ // CIR-FLAT-LABEL: cir.func {{.*}} @_ZN1SD2Ev // CIR-FLAT: cir.alloca "this" {{.*}} : !cir.ptr<!cir.ptr<!rec_S>, target_address_space(5)> -// LLVM-LABEL: define {{.*}} @_Z1fv +// LLVM-LABEL: define {{.*}} void @_Z1fv(ptr {{.*}}sret(%struct.S) // LLVM: alloca i32, align 4, addrspace(5) -// LLVM: alloca %struct.S, align 1, addrspace(5) // LLVM: alloca i8, align 1, addrspace(5) // LLVM-LABEL: define {{.*}} @_ZN1SD1Ev >From 3ceb1b3f5ee3f71a124dbeebb49ae25fc3eaaac7 Mon Sep 17 00:00:00 2001 From: skc7 <[email protected]> Date: Tue, 10 Nov 2026 16:31:41 +0530 Subject: [PATCH 2/3] add working test cases after rebase --- .../call-conv-lowering-amdgpu-arg-types.c | 421 +++++++++++++++++- .../call-conv-lowering-amdgpu-cxx-records.cpp | 101 ++++- .../call-conv-lowering-amdgpu-return-types.c | 159 ++++++- .../call-conv-lowering-amdgpu-kernel-args.hip | 42 +- 4 files changed, 677 insertions(+), 46 deletions(-) diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-arg-types.c b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-arg-types.c index ef65e90a528166..52eeb8c8bbe7f1 100644 --- a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-arg-types.c +++ b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-arg-types.c @@ -6,18 +6,8 @@ // RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -emit-llvm %s -o %t.ll // RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s -// Checks that CallConvLowering classifies arguments of ordinary AMDGPU -// functions, which use the default convention rather than the kernel one. - -// TODO(cir): Add the argument cases still NYI in CallConvLowering: -// - Aggregates of 33 to 64 bits, which classic coerces to [2 x i32]. -// abiTypeToCIR has no array case yet. -// - Aggregates over 64 bits that fit the 16-register budget. The classifier -// returns Direct with no coerce type, which the bridge rejects. -// - Aggregates past the register budget, passed byref in addrspace(5). -// - Structs with a flexible array member and transparent unions. The bridge -// does not set those record flags yet. -// - Arguments passed through an ellipsis. +// Checks that CallConvLowering passes arguments of non-kernel AMDGPU functions +// the same way as the existing LLVM codegen. typedef struct {} empty_t; typedef struct { char c; } i8_wrapper_t; @@ -27,6 +17,22 @@ typedef struct { int *p; } ptr_wrapper_t; typedef struct { char a, b; } char2_t; typedef struct { char a, b, c; } small_t; typedef union { int i; float f; } int_float_t; +typedef union { + int *ip; + float *fp; +} __attribute__((transparent_union)) transparent_t; +typedef struct { int a, b; } pair_t; +typedef struct { int a, b, c; } triple_t; +typedef struct { int data[17]; } big_t; +typedef struct { int data[16]; } big16_t; +typedef struct { int a : 3; int b : 5; } bits_t; +typedef struct { int n; int data[]; } fam_t; +typedef struct { _Complex double c; } cdwrap_t; +typedef struct { int v[3]; } arrwrap_t; +typedef struct { struct { int a, b, c; } t; } nestwrap_t; +typedef union { int a[3]; double d; } u_pad_t; +typedef union { double d[2]; long l[2]; } u_dl_t; +typedef union __attribute__((packed)) { char c[13]; double d; } u_packed_t; typedef short short4 __attribute__((ext_vector_type(4))); typedef float float3 __attribute__((ext_vector_type(3))); @@ -56,6 +62,12 @@ void arg_short(short s) {} // CIR: cir.func {{.*}}@arg_short(%arg0: !s16i {{.*}}llvm.signext // LLVM: define {{.*}}void @arg_short(i16 noundef signext %{{.*}}) +// The call site carries the same extension as the callee. +void call_char(char c) { arg_char(c); } + +// LLVM: define {{.*}}void @call_char(i8 noundef signext %{{.*}}) +// LLVM: call void @arg_char(i8 noundef signext %{{.*}}) + // Word-sized and wider scalars pass through unchanged. void arg_int(int i) {} @@ -67,6 +79,21 @@ void arg_long(long l) {} // CIR: cir.func {{.*}}@arg_long(%arg0: !s64i {{.*}}) // LLVM: define {{.*}}void @arg_long(i64 noundef %{{.*}}) +// 128-bit integers pass whole. +void arg_int128(__int128 i) {} + +// CIR: cir.func {{.*}}@arg_int128(%[[I:[^:]+]]: !s128i {{.*}}) +// CIR: cir.store %[[I]], %{{.*}} : !s128i, !cir.ptr<!s128i> +// LLVM: define {{.*}}void @arg_int128(i128 noundef %[[I:[^)]+]]) +// LLVM: store i128 %[[I]], ptr %{{.*}} + +void arg_bitint128(_BitInt(128) i) {} + +// CIR: cir.func {{.*}}@arg_bitint128(%[[I:[^:]+]]: !s128i_bitint {{.*}}) +// CIR: cir.store %[[I]], %{{.*}} : !s128i_bitint, !cir.ptr<!s128i_bitint> +// LLVM: define {{.*}}void @arg_bitint128(i128 noundef %[[I:[^)]+]]) +// LLVM: store i128 %[[I]], ptr %{{.*}}, align 8 + void arg_float(float f) {} // CIR: cir.func {{.*}}@arg_float(%arg0: !cir.float {{.*}}) @@ -134,6 +161,33 @@ void arg_ptr_wrapper(ptr_wrapper_t w) {} // CIR: cir.func {{.*}}@arg_ptr_wrapper(%arg0: !cir.ptr<{{[^,]*}}>{{.*}}) // LLVM: define {{.*}}void @arg_ptr_wrapper(ptr %{{.*}}) +// A struct of one aggregate field is passed as that field. +void arg_cdwrap(cdwrap_t w) {} + +// CIR: cir.func {{.*}}@arg_cdwrap(%[[W:[^:]+]]: !cir.complex<!cir.double>{{.*}}) +// CIR-NEXT: %[[W_COERCE:.*]] = cir.alloca "coerce" align(8) : !cir.ptr<!rec_cdwrap_t, target_address_space(5)> +// CIR-NEXT: %[[W_C:.*]] = cir.get_member %[[W_COERCE]][0] {name = ""} : !cir.ptr<!rec_cdwrap_t, target_address_space(5)> -> !cir.ptr<!cir.complex<!cir.double>, target_address_space(5)> +// CIR-NEXT: cir.store %[[W]], %[[W_C]] : !cir.complex<!cir.double>, !cir.ptr<!cir.complex<!cir.double>, target_address_space(5)> +// LLVM: define {{.*}}void @arg_cdwrap({ double, double } %{{.*}}) + +void arg_arrwrap(arrwrap_t w) {} + +// CIR: cir.func {{.*}}@arg_arrwrap(%[[W:[^:]+]]: !cir.array<!s32i x 3>{{.*}}) +// CIR-NEXT: %[[W_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!rec_arrwrap_t, target_address_space(5)> +// CIR-NEXT: %[[W_V:.*]] = cir.get_member %[[W_COERCE]][0] {name = ""} : !cir.ptr<!rec_arrwrap_t, target_address_space(5)> -> !cir.ptr<!cir.array<!s32i x 3>, target_address_space(5)> +// CIR-NEXT: cir.store %[[W]], %[[W_V]] : !cir.array<!s32i x 3>, !cir.ptr<!cir.array<!s32i x 3>, target_address_space(5)> +// LLVM: define {{.*}}void @arg_arrwrap([3 x i32] %[[W:[^)]+]]) +// LLVM: %[[W_V:.*]] = getelementptr inbounds nuw %struct.arrwrap_t, ptr addrspace(5) %{{.*}}, i32 0, i32 0 +// LLVM-NEXT: store [3 x i32] %[[W]], ptr addrspace(5) %[[W_V]], align 4 + +void arg_nestwrap(nestwrap_t w) {} + +// CIR: cir.func {{.*}}@arg_nestwrap(%[[W:[^:]+]]: ![[INNER:rec_anon[0-9A-Za-z]*]] +// CIR-NEXT: %[[W_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!rec_nestwrap_t, target_address_space(5)> +// CIR-NEXT: %[[W_T:.*]] = cir.get_member %[[W_COERCE]][0] {name = ""} : !cir.ptr<!rec_nestwrap_t, target_address_space(5)> -> !cir.ptr<![[INNER]], target_address_space(5)> +// CIR-NEXT: cir.store %[[W]], %[[W_T]] : ![[INNER]], !cir.ptr<![[INNER]], target_address_space(5)> +// LLVM: define {{.*}}void @arg_nestwrap(%struct.anon{{.*}} %{{.*}}) + // Aggregates up to 32 bits are packed into one integer register. void arg_char2(char2_t s) {} @@ -150,6 +204,34 @@ void arg_union(int_float_t u) {} // CIR: cir.func {{.*}}@arg_union(%arg0: !u32i{{.*}}) // LLVM: define {{.*}}void @arg_union(i32 %{{.*}}) +// Bit-fields are packed like any other small aggregate. +void arg_bits(bits_t b) {} + +// CIR: cir.func {{.*}}@arg_bits(%[[B:[^:]+]]: !u32i{{.*}}) +// CIR-NEXT: %[[B_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!u32i, target_address_space(5)> +// CIR-NEXT: cir.store %[[B]], %[[B_COERCE]] : !u32i, !cir.ptr<!u32i, target_address_space(5)> +// LLVM: define {{.*}}void @arg_bits(i32 %[[B:[^)]+]]) +// LLVM: store i32 %[[B]], ptr addrspace(5) %{{.*}}, align 4 + +// Aggregates of 33 to 64 bits are packed into a pair of i32 registers. +void arg_pair(pair_t p) {} + +// CIR: cir.func {{.*}}@arg_pair(%[[P:[^:]+]]: !cir.array<!u32i x 2>{{.*}}) +// CIR-NEXT: %[[P_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR-NEXT: cir.store %[[P]], %[[P_COERCE]] : !cir.array<!u32i x 2>, !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// LLVM: define {{.*}}void @arg_pair([2 x i32] %[[P:[^)]+]]) +// LLVM: store [2 x i32] %[[P]], ptr addrspace(5) %{{.*}}, align 4 + +// A _Complex up to 64 bits is packed like a small aggregate. +void arg_cfloat(_Complex float c) {} + +// CIR: cir.func {{.*}}@arg_cfloat(%[[C:[^:]+]]: !cir.array<!u32i x 2> {{.*}}) +// CIR-NEXT: %[[C_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR-NEXT: cir.store %[[C]], %[[C_COERCE]] : !cir.array<!u32i x 2>, !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR-NEXT: cir.cast bitcast %[[C_COERCE]] : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> -> !cir.ptr<!cir.complex<!cir.float>, target_address_space(5)> +// LLVM: define {{.*}}void @arg_cfloat([2 x i32] noundef %[[C:[^)]+]]) +// LLVM: store [2 x i32] %[[C]], ptr addrspace(5) %{{.*}}, align 4 + // The call site is coerced the same way as the callee. void call_small(small_t s) { arg_small(s); } @@ -158,8 +240,313 @@ void call_small(small_t s) { arg_small(s); } // LLVM: define {{.*}}void @call_small(i32 %{{.*}}) // LLVM: call void @arg_small(i32 %{{.*}}) -// The call site carries the same extension as the callee. -void call_char(char c) { arg_char(c); } - -// LLVM: define {{.*}}void @call_char(i8 noundef signext %{{.*}}) -// LLVM: call void @arg_char(i8 noundef signext %{{.*}}) +void call_pair(pair_t p) { arg_pair(p); } + +// CIR: cir.func {{.*}}@call_pair( +// CIR: %[[ARG:.*]] = cir.load align(4) %{{.*}} : !cir.ptr<!cir.array<!u32i x 2>>, !cir.array<!u32i x 2> +// CIR: cir.call @arg_pair(%[[ARG]]) {{.*}}: (!cir.array<!u32i x 2>) -> () +// LLVM: define {{.*}}void @call_pair( +// LLVM: %[[ARG:.*]] = load [2 x i32], ptr %{{.*}}, align 4 +// LLVM: call void @arg_pair([2 x i32] %[[ARG]]) + +// A transparent union is passed as its first member. +void arg_transparent(transparent_t u) {} + +// CIR: cir.func {{.*}}@arg_transparent(%[[U:[^:]+]]: !cir.ptr<!s32i>{{.*}}) +// CIR-NEXT: %[[U_COERCE:.*]] = cir.alloca "coerce" align(8) : !cir.ptr<!cir.ptr<!s32i>, target_address_space(5)> +// CIR-NEXT: cir.store %[[U]], %[[U_COERCE]] : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>, target_address_space(5)> +// LLVM: define {{.*}}void @arg_transparent(ptr %[[U:[^)]+]]) +// LLVM: store ptr %[[U]], ptr addrspace(5) %{{.*}}, align 8 + +// A caller can pass any member type directly. +void call_transparent(int *p) { arg_transparent(p); } + +// CIR: cir.func {{.*}}@call_transparent( +// CIR: %[[U_PTR:.*]] = cir.cast bitcast %{{.*}} : !cir.ptr<!rec_transparent_t> -> !cir.ptr<!cir.ptr<!s32i>> +// CIR-NEXT: %[[ARG:.*]] = cir.load align(8) %[[U_PTR]] : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i> +// CIR-NEXT: cir.call @arg_transparent(%[[ARG]]) {{.*}}: (!cir.ptr<!s32i>) -> () +// LLVM: define {{.*}}void @call_transparent(ptr noundef %{{.*}}) +// LLVM: call void @arg_transparent(ptr %{{.*}}) + +// Larger aggregates that fit the register budget pass one field per register. +void arg_triple(triple_t t) {} + +// CIR: cir.func {{.*}}@arg_triple(%[[T0:[^:]+]]: !s32i{{.*}}, %[[T1:[^:]+]]: !s32i{{.*}}, %[[T2:[^:]+]]: !s32i{{.*}}) +// CIR-NEXT: %[[T_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!rec_triple_t, target_address_space(5)> +// CIR-NEXT: %[[F0:.*]] = cir.get_member %[[T_COERCE]][0] +// CIR-NEXT: cir.store %[[T0]], %[[F0]] +// CIR-NEXT: %[[F1:.*]] = cir.get_member %[[T_COERCE]][1] +// CIR-NEXT: cir.store %[[T1]], %[[F1]] +// CIR-NEXT: %[[F2:.*]] = cir.get_member %[[T_COERCE]][2] +// CIR-NEXT: cir.store %[[T2]], %[[F2]] +// LLVM: define {{.*}}void @arg_triple(i32 %[[T0:[^,]+]], i32 %[[T1:[^,]+]], i32 %[[T2:[^)]+]]) +// LLVM: store i32 %[[T0]], ptr addrspace(5) %{{.*}}, align 4 +// LLVM: store i32 %[[T1]], ptr addrspace(5) %{{.*}}, align 4 +// LLVM: store i32 %[[T2]], ptr addrspace(5) %{{.*}}, align 4 + +void call_triple(triple_t t) { arg_triple(t); } + +// CIR: cir.func {{.*}}@call_triple( +// CIR: cir.call @arg_triple(%{{.*}}, %{{.*}}, %{{.*}}) {{.*}}: (!s32i, !s32i, !s32i) -> () +// LLVM: define {{.*}}void @call_triple( +// LLVM: call void @arg_triple(i32 %{{.*}}, i32 %{{.*}}, i32 %{{.*}}) + +// A call through a function pointer is coerced like a direct call. +void call_fnptr(void (*fp)(triple_t), triple_t t) { fp(t); } + +// CIR: cir.func {{.*}}@call_fnptr(%{{.*}}: !cir.ptr<!cir.func<(!rec_triple_t)>> {{.*}}, %{{.*}}: !s32i{{.*}}, %{{.*}}: !s32i{{.*}}, %{{.*}}: !s32i{{.*}}) +// CIR: %[[FN:.*]] = cir.cast bitcast %{{.*}} : !cir.ptr<!cir.func<(!rec_triple_t)>> -> !cir.ptr<!cir.func<(!s32i, !s32i, !s32i)>> +// CIR-NEXT: cir.call %[[FN]](%{{.*}}, %{{.*}}, %{{.*}}) {{.*}}: (!cir.ptr<!cir.func<(!s32i, !s32i, !s32i)>>, !s32i, !s32i, !s32i) -> () +// LLVM: define {{.*}}void @call_fnptr(ptr noundef %{{.*}}, i32 %{{.*}}, i32 %{{.*}}, i32 %{{.*}}) +// LLVM: %[[FN:.*]] = load ptr, ptr %{{.*}}, align 8 +// LLVM: call void %[[FN]](i32 %{{.*}}, i32 %{{.*}}, i32 %{{.*}}) + +// A _Complex wider than 64 bits is split into its two parts. +void arg_cdouble(_Complex double c) {} + +// CIR: cir.func {{.*}}@arg_cdouble(%[[RE:[^:]+]]: !cir.double {llvm.noundef}{{.*}}, %[[IM:[^:]+]]: !cir.double {llvm.noundef}{{.*}}) +// CIR-NEXT: %[[C_COERCE:.*]] = cir.alloca "coerce" align(8) : !cir.ptr<![[PAIR:rec_anon_struct[0-9]*]], target_address_space(5)> +// CIR-NEXT: %[[C_RE:.*]] = cir.get_member %[[C_COERCE]][0] {name = ""} : !cir.ptr<![[PAIR]], target_address_space(5)> -> !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: cir.store %[[RE]], %[[C_RE]] : !cir.double, !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: %[[C_IM:.*]] = cir.get_member %[[C_COERCE]][1] {name = ""} : !cir.ptr<![[PAIR]], target_address_space(5)> -> !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: cir.store %[[IM]], %[[C_IM]] : !cir.double, !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: %[[C_PTR:.*]] = cir.cast bitcast %[[C_COERCE]] : !cir.ptr<![[PAIR]], target_address_space(5)> -> !cir.ptr<!cir.complex<!cir.double>, target_address_space(5)> +// CIR-NEXT: %{{.*}} = cir.load %[[C_PTR]] : !cir.ptr<!cir.complex<!cir.double>, target_address_space(5)>, !cir.complex<!cir.double> +// LLVM: define {{.*}}void @arg_cdouble(double noundef %[[RE:[^,]+]], double noundef %[[IM:[^)]+]]) +// LLVM: %[[C_RE:.*]] = getelementptr inbounds nuw { double, double }, ptr addrspace(5) %[[C:.*]], i32 0, i32 0 +// LLVM-NEXT: store double %[[RE]], ptr addrspace(5) %[[C_RE]], align 8 +// LLVM-NEXT: %[[C_IM:.*]] = getelementptr inbounds nuw { double, double }, ptr addrspace(5) %[[C]], i32 0, i32 1 +// LLVM-NEXT: store double %[[IM]], ptr addrspace(5) %[[C_IM]], align 8 + +void arg_clong(_Complex long c) {} + +// CIR: cir.func {{.*}}@arg_clong(%{{.*}}: !s64i {llvm.noundef}{{.*}}, %{{.*}}: !s64i {llvm.noundef}{{.*}}) +// CIR: cir.cast bitcast %{{.*}} : !cir.ptr<!rec_anon_struct{{[0-9]*}}, target_address_space(5)> -> !cir.ptr<!cir.complex<!s64i>, target_address_space(5)> +// LLVM: define {{.*}}void @arg_clong(i64 noundef %[[RE:[^,]+]], i64 noundef %[[IM:[^)]+]]) +// LLVM: store i64 %[[RE]], ptr addrspace(5) %{{.*}}, align 8 +// LLVM-NEXT: %{{.*}} = getelementptr inbounds nuw { i64, i64 }, ptr addrspace(5) %{{.*}}, i32 0, i32 1 +// LLVM-NEXT: store i64 %[[IM]], ptr addrspace(5) %{{.*}}, align 8 + +// The caller splits it the same way. +void call_cdouble(_Complex double c) { arg_cdouble(c); } + +// CIR: cir.func {{.*}}@call_cdouble( +// CIR: %[[C_PAIR:.*]] = cir.cast bitcast %{{.*}} : !cir.ptr<!cir.complex<!cir.double>, target_address_space(5)> -> !cir.ptr<![[PAIR:rec_anon_struct[0-9]*]], target_address_space(5)> +// CIR-NEXT: %[[C_RE:.*]] = cir.get_member %[[C_PAIR]][0] {name = ""} : !cir.ptr<![[PAIR]], target_address_space(5)> -> !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: %[[RE:.*]] = cir.load %[[C_RE]] : !cir.ptr<!cir.double, target_address_space(5)>, !cir.double +// CIR-NEXT: %[[C_IM:.*]] = cir.get_member %[[C_PAIR]][1] {name = ""} : !cir.ptr<![[PAIR]], target_address_space(5)> -> !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: %[[IM:.*]] = cir.load %[[C_IM]] : !cir.ptr<!cir.double, target_address_space(5)>, !cir.double +// CIR-NEXT: cir.call @arg_cdouble(%[[RE]], %[[IM]]) {{.*}}: (!cir.double {llvm.noundef}, !cir.double {llvm.noundef}) -> () +// LLVM: define {{.*}}void @call_cdouble(double noundef %{{.*}}, double noundef %{{.*}}) +// LLVM: %[[RE:.*]] = load double, ptr addrspace(5) %{{.*}}, align 8 +// LLVM-NEXT: %{{.*}} = getelementptr inbounds nuw { double, double }, ptr addrspace(5) %{{.*}}, i32 0, i32 1 +// LLVM-NEXT: %[[IM:.*]] = load double, ptr addrspace(5) %{{.*}}, align 8 +// LLVM-NEXT: call void @arg_cdouble(double noundef %[[RE]], double noundef %[[IM]]) + +// A union is passed as its storage member, then its tail padding. +void arg_u_pad(u_pad_t u) {} + +// CIR: cir.func {{.*}}@arg_u_pad(%[[D:[^:]+]]: !cir.double{{.*}}, %[[PAD:[^:]+]]: !cir.array<!u8i x 8>{{.*}}) +// CIR-NEXT: %[[U_COERCE:.*]] = cir.alloca "coerce" align(8) : !cir.ptr<![[FLAT:rec_anon_struct[0-9]*]], target_address_space(5)> +// CIR-NEXT: %[[U_D:.*]] = cir.get_member %[[U_COERCE]][0] {name = ""} : !cir.ptr<![[FLAT]], target_address_space(5)> -> !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: cir.store %[[D]], %[[U_D]] : !cir.double, !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: %[[U_PAD:.*]] = cir.get_member %[[U_COERCE]][1] {name = ""} : !cir.ptr<![[FLAT]], target_address_space(5)> -> !cir.ptr<!cir.array<!u8i x 8>, target_address_space(5)> +// CIR-NEXT: cir.store %[[PAD]], %[[U_PAD]] : !cir.array<!u8i x 8>, !cir.ptr<!cir.array<!u8i x 8>, target_address_space(5)> +// CIR-NEXT: cir.cast bitcast %[[U_COERCE]] : !cir.ptr<![[FLAT]], target_address_space(5)> -> !cir.ptr<!rec_u_pad_t, target_address_space(5)> +// LLVM: define {{.*}}void @arg_u_pad(double %[[D:[^,]+]], [8 x i8] %[[PAD:[^)]+]]) +// LLVM: store double %[[D]], ptr addrspace(5) %{{.*}}, align 8 +// LLVM-NEXT: %{{.*}} = getelementptr inbounds nuw {{.*}}, ptr addrspace(5) %{{.*}}, i32 0, i32 1 +// LLVM-NEXT: store [8 x i8] %[[PAD]], ptr addrspace(5) %{{.*}} + +void call_u_pad(u_pad_t u) { arg_u_pad(u); } + +// CIR: cir.func {{.*}}@call_u_pad( +// CIR: %[[U_FLAT:.*]] = cir.cast bitcast %{{.*}} : !cir.ptr<!rec_u_pad_t> -> !cir.ptr<![[FLAT:rec_anon_struct[0-9]*]]> +// CIR-NEXT: %[[U_D:.*]] = cir.get_member %[[U_FLAT]][0] {name = ""} : !cir.ptr<![[FLAT]]> -> !cir.ptr<!cir.double> +// CIR-NEXT: %[[D:.*]] = cir.load align(8) %[[U_D]] : !cir.ptr<!cir.double>, !cir.double +// CIR-NEXT: %[[U_PAD:.*]] = cir.get_member %[[U_FLAT]][1] {name = ""} : !cir.ptr<![[FLAT]]> -> !cir.ptr<!cir.array<!u8i x 8>> +// CIR-NEXT: %[[PAD:.*]] = cir.load align(8) %[[U_PAD]] : !cir.ptr<!cir.array<!u8i x 8>>, !cir.array<!u8i x 8> +// CIR-NEXT: cir.call @arg_u_pad(%[[D]], %[[PAD]]) {{.*}}: (!cir.double, !cir.array<!u8i x 8>) -> () +// LLVM: define {{.*}}void @call_u_pad(double %{{.*}}, [8 x i8] %{{.*}}) +// LLVM: %[[D:.*]] = load double, ptr %{{.*}}, align 8 +// LLVM-NEXT: %{{.*}} = getelementptr inbounds nuw {{.*}}, ptr %{{.*}}, i32 0, i32 1 +// LLVM-NEXT: %[[PAD:.*]] = load [8 x i8], ptr %{{.*}}, align 8 +// LLVM-NEXT: call void @arg_u_pad(double %[[D]], [8 x i8] %[[PAD]]) + +// With no tail padding a union is passed as its storage member alone. +void arg_u_dl(u_dl_t u) {} + +// CIR: cir.func {{.*}}@arg_u_dl(%[[U:[^:]+]]: !cir.array<!cir.double x 2>{{.*}}) +// CIR-NEXT: %[[U_COERCE:.*]] = cir.alloca "coerce" align(8) : !cir.ptr<![[FLAT:rec_anon_struct[0-9]*]], target_address_space(5)> +// CIR-NEXT: %[[U_D:.*]] = cir.get_member %[[U_COERCE]][0] {name = ""} : !cir.ptr<![[FLAT]], target_address_space(5)> -> !cir.ptr<!cir.array<!cir.double x 2>, target_address_space(5)> +// CIR-NEXT: cir.store %[[U]], %[[U_D]] : !cir.array<!cir.double x 2>, !cir.ptr<!cir.array<!cir.double x 2>, target_address_space(5)> +// CIR-NEXT: cir.cast bitcast %[[U_COERCE]] : !cir.ptr<![[FLAT]], target_address_space(5)> -> !cir.ptr<!rec_u_dl_t, target_address_space(5)> +// LLVM: define {{.*}}void @arg_u_dl([2 x double] %[[U:[^)]+]]) +// LLVM: store [2 x double] %[[U]], ptr addrspace(5) %{{.*}}, align 8 + +// A packed union keeps its padding, and its double is stored at align 1. +void arg_u_packed(u_packed_t u) {} + +// CIR: cir.func {{.*}}@arg_u_packed(%[[D:[^:]+]]: !cir.double{{.*}}, %[[PAD:[^:]+]]: !cir.array<!u8i x 5>{{.*}}) +// CIR-NEXT: %[[U_COERCE:.*]] = cir.alloca "coerce" align(1) : !cir.ptr<![[FLAT:rec_anon_struct[0-9]*]], target_address_space(5)> +// CIR-NEXT: %[[U_D:.*]] = cir.get_member %[[U_COERCE]][0] {name = ""} : !cir.ptr<![[FLAT]], target_address_space(5)> -> !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: cir.store align(1) %[[D]], %[[U_D]] : !cir.double, !cir.ptr<!cir.double, target_address_space(5)> +// CIR-NEXT: %[[U_PAD:.*]] = cir.get_member %[[U_COERCE]][1] {name = ""} : !cir.ptr<![[FLAT]], target_address_space(5)> -> !cir.ptr<!cir.array<!u8i x 5>, target_address_space(5)> +// CIR-NEXT: cir.store %[[PAD]], %[[U_PAD]] : !cir.array<!u8i x 5>, !cir.ptr<!cir.array<!u8i x 5>, target_address_space(5)> +// LLVM: define {{.*}}void @arg_u_packed(double %[[D:[^,]+]], [5 x i8] %[[PAD:[^)]+]]) +// LLVM: store double %[[D]], ptr addrspace(5) %{{.*}}, align 1 +// LLVM-NEXT: %{{.*}} = getelementptr inbounds nuw {{.*}}, ptr addrspace(5) %{{.*}}, i32 0, i32 1 +// LLVM-NEXT: store [5 x i8] %[[PAD]], ptr addrspace(5) %{{.*}}, align 1 + +// An aggregate of exactly 16 registers is passed direct. +void arg_big16(big16_t b) {} + +// CIR: cir.func {{.*}}@arg_big16(%[[B:[^:]+]]: !cir.array<!s32i x 16>{{.*}}) +// CIR-NEXT: %[[B_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!rec_big16_t, target_address_space(5)> +// CIR-NEXT: %[[B_DATA:.*]] = cir.get_member %[[B_COERCE]][0] {name = ""} : !cir.ptr<!rec_big16_t, target_address_space(5)> -> !cir.ptr<!cir.array<!s32i x 16>, target_address_space(5)> +// CIR-NEXT: cir.store %[[B]], %[[B_DATA]] : !cir.array<!s32i x 16>, !cir.ptr<!cir.array<!s32i x 16>, target_address_space(5)> +// LLVM: define {{.*}}void @arg_big16([16 x i32] %[[B:[^)]+]]) +// LLVM: %[[B_DATA:.*]] = getelementptr inbounds nuw %struct.big16_t, ptr addrspace(5) %{{.*}}, i32 0, i32 0 +// LLVM-NEXT: store [16 x i32] %[[B]], ptr addrspace(5) %[[B_DATA]], align 4 + +// An aggregate past the register budget is passed byref in addrspace(5). +void arg_big(big_t b) {} + +// CIR: cir.func {{.*}}@arg_big(%[[B_ARG:[^:]+]]: !cir.ptr<!rec_big_t, target_address_space(5)> {{.*}}llvm.byref = !rec_big_t +// CIR: cir.copy %[[B_ARG]] align(4) to %{{.*}} align(4) : !cir.ptr<!rec_big_t, target_address_space(5)>, !cir.ptr<!rec_big_t> +// LLVM: define {{.*}}void @arg_big(ptr addrspace(5) noundef byref(%struct.big_t) align 4 %[[B_ARG:.*]]) +// LLVM: call void @llvm.memcpy.{{.*}}(ptr{{.*}} align 4 %{{.*}}, ptr addrspace(5) align 4 %[[B_ARG]], i64 68, i1 false) + +// The caller passes its own local, not a copy of it. +void call_big(void) { + big_t b = {0}; + arg_big(b); +} + +// CIR: cir.func {{.*}}@call_big() +// CIR: %[[B:.*]] = cir.alloca "b" {{.*}} : !cir.ptr<!rec_big_t, target_address_space(5)> +// CIR: cir.call @arg_big(%[[B]]) {{.*}}: (!cir.ptr<!rec_big_t, target_address_space(5)> {{.*}}llvm.byref = !rec_big_t +// LLVM: define {{.*}}void @call_big() +// LLVM: call void @arg_big(ptr addrspace(5) noundef byref(%struct.big_t) align 4 %{{.*}}) + +// Scalars use the same register budget, so a struct that would fit on its +// own is passed byref once they have used it up. +void arg_budget(int a0, int a1, int a2, int a3, int a4, int a5, int a6, + int a7, int a8, int a9, int a10, int a11, int a12, int a13, + triple_t t) {} + +// CIR: cir.func {{.*}}@arg_budget(%arg0: !s32i {{.*}}%arg13: !s32i {{.*}}, %[[T_ARG:[^:]+]]: !cir.ptr<!rec_triple_t, target_address_space(5)> {{.*}}llvm.byref = !rec_triple_t +// CIR: cir.copy %[[T_ARG]] align(4) to %{{.*}} align(4) : !cir.ptr<!rec_triple_t, target_address_space(5)>, !cir.ptr<!rec_triple_t> +// LLVM: define {{.*}}void @arg_budget(i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, ptr addrspace(5) noundef byref(%struct.triple_t) align 4 %[[T_ARG:.*]]) +// LLVM: call void @llvm.memcpy.{{.*}}(ptr{{.*}} align 4 %{{.*}}, ptr addrspace(5) align 4 %[[T_ARG]], i64 12, i1 false) + +// A struct that takes exactly the registers left is still passed direct. +void arg_budget_exact(int a0, int a1, int a2, int a3, int a4, int a5, int a6, + int a7, int a8, int a9, int a10, int a11, int a12, + triple_t t) {} + +// CIR: cir.func {{.*}}@arg_budget_exact(%arg0: !s32i {{.*}}%arg12: !s32i {{.*}}, %[[T0:[^:]+]]: !s32i{{.*}}, %[[T1:[^:]+]]: !s32i{{.*}}, %[[T2:[^:]+]]: !s32i{{.*}}) +// CIR-NEXT: %[[T_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!rec_triple_t, target_address_space(5)> +// CIR-NEXT: %[[F0:.*]] = cir.get_member %[[T_COERCE]][0] {name = ""} : !cir.ptr<!rec_triple_t, target_address_space(5)> -> !cir.ptr<!s32i, target_address_space(5)> +// CIR-NEXT: cir.store %[[T0]], %[[F0]] : !s32i, !cir.ptr<!s32i, target_address_space(5)> +// CIR-NEXT: %[[F1:.*]] = cir.get_member %[[T_COERCE]][1] {name = ""} : !cir.ptr<!rec_triple_t, target_address_space(5)> -> !cir.ptr<!s32i, target_address_space(5)> +// CIR-NEXT: cir.store %[[T1]], %[[F1]] : !s32i, !cir.ptr<!s32i, target_address_space(5)> +// CIR-NEXT: %[[F2:.*]] = cir.get_member %[[T_COERCE]][2] {name = ""} : !cir.ptr<!rec_triple_t, target_address_space(5)> -> !cir.ptr<!s32i, target_address_space(5)> +// CIR-NEXT: cir.store %[[T2]], %[[F2]] : !s32i, !cir.ptr<!s32i, target_address_space(5)> +// LLVM: define {{.*}}void @arg_budget_exact(i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 %[[T0:[^,]+]], i32 %[[T1:[^,]+]], i32 %[[T2:[^)]+]]) +// LLVM: store i32 %[[T0]], ptr addrspace(5) %{{.*}}, align 4 +// LLVM-NEXT: %{{.*}} = getelementptr inbounds nuw %struct.triple_t, ptr addrspace(5) %{{.*}}, i32 0, i32 1 +// LLVM-NEXT: store i32 %[[T1]], ptr addrspace(5) %{{.*}}, align 4 +// LLVM-NEXT: %{{.*}} = getelementptr inbounds nuw %struct.triple_t, ptr addrspace(5) %{{.*}}, i32 0, i32 2 +// LLVM-NEXT: store i32 %[[T2]], ptr addrspace(5) %{{.*}}, align 4 + +// An aggregate of up to 64 bits is packed even once the budget is used up. +void arg_budget_pair(int a0, int a1, int a2, int a3, int a4, int a5, int a6, + int a7, int a8, int a9, int a10, int a11, int a12, + int a13, int a14, int a15, pair_t p) {} + +// CIR: cir.func {{.*}}@arg_budget_pair(%arg0: !s32i {{.*}}%arg15: !s32i {{.*}}, %[[P:[^:]+]]: !cir.array<!u32i x 2>{{.*}}) +// CIR-NEXT: %[[P_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR-NEXT: cir.store %[[P]], %[[P_COERCE]] : !cir.array<!u32i x 2>, !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR-NEXT: cir.cast bitcast %[[P_COERCE]] : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> -> !cir.ptr<!rec_pair_t, target_address_space(5)> +// LLVM: define {{.*}}void @arg_budget_pair(i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, [2 x i32] %[[P:[^)]+]]) +// LLVM: store [2 x i32] %[[P]], ptr addrspace(5) %{{.*}}, align 4 + +// A packed aggregate still uses budget, so the next struct no longer fits. +void arg_budget_pair_triple(int a0, int a1, int a2, int a3, int a4, int a5, + int a6, int a7, int a8, int a9, int a10, int a11, + int a12, int a13, pair_t p, triple_t t) {} + +// CIR: cir.func {{.*}}@arg_budget_pair_triple(%arg0: !s32i {{.*}}%arg13: !s32i {{.*}}, %[[P:[^:]+]]: !cir.array<!u32i x 2>{{.*}}, %[[T_ARG:[^:]+]]: !cir.ptr<!rec_triple_t, target_address_space(5)> {llvm.align = 4 : i64, llvm.byref = !rec_triple_t, llvm.noundef} +// CIR-NEXT: %[[P_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR-NEXT: cir.store %[[P]], %[[P_COERCE]] : !cir.array<!u32i x 2>, !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR: cir.copy %[[T_ARG]] align(4) to %{{.*}} align(4) : !cir.ptr<!rec_triple_t, target_address_space(5)>, !cir.ptr<!rec_triple_t> +// LLVM: define {{.*}}void @arg_budget_pair_triple(i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, [2 x i32] %[[P:[^,]+]], ptr addrspace(5) noundef byref(%struct.triple_t) align 4 %[[T_ARG:[^)]+]]) +// LLVM: store [2 x i32] %[[P]], ptr addrspace(5) %{{.*}}, align 4 +// LLVM: call void @llvm.memcpy.{{.*}}(ptr{{.*}} align 4 %{{.*}}, ptr addrspace(5) align 4 %[[T_ARG]], i64 12, i1 false) + +// Past the register budget a _Complex is passed byref in addrspace(5). +void arg_cdouble_budget(int a0, int a1, int a2, int a3, int a4, int a5, int a6, + int a7, int a8, int a9, int a10, int a11, int a12, + int a13, _Complex double c) {} + +// CIR: cir.func {{.*}}@arg_cdouble_budget(%arg0: !s32i {{.*}}%arg13: !s32i {{.*}}, %[[C_ARG:[^:]+]]: !cir.ptr<!cir.complex<!cir.double>, target_address_space(5)> {llvm.align = 8 : i64, llvm.byref = !cir.complex<!cir.double>, llvm.noundef} +// CIR-NEXT: %{{.*}} = cir.load %[[C_ARG]] : !cir.ptr<!cir.complex<!cir.double>, target_address_space(5)>, !cir.complex<!cir.double> +// LLVM: define {{.*}}void @arg_cdouble_budget(i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, i32 noundef %{{.*}}, ptr addrspace(5) noundef byref({ double, double }) align 8 %{{.*}}) + +// A struct with a flexible array member is passed byval in addrspace(5). +void arg_fam(fam_t s) {} + +// CIR: cir.func {{.*}}@arg_fam(%[[S_ARG:[^:]+]]: !cir.ptr<!rec_fam_t, target_address_space(5)> {llvm.align = 4 : i64, llvm.byval = !rec_fam_t, llvm.noundef} +// CIR-NEXT: %[[S:.*]] = cir.alloca "s" align(4) init : !cir.ptr<!rec_fam_t, target_address_space(5)> +// CIR-NEXT: %[[S_VIEW:.*]] = cir.cast address_space %[[S]] : !cir.ptr<!rec_fam_t, target_address_space(5)> -> !cir.ptr<!rec_fam_t> +// CIR-NEXT: cir.copy %[[S_ARG]] align(4) to %[[S_VIEW]] align(4) : !cir.ptr<!rec_fam_t, target_address_space(5)>, !cir.ptr<!rec_fam_t> +// LLVM: define {{.*}}void @arg_fam(ptr addrspace(5) noundef byval(%struct.fam_t) align 4 %{{.*}}) + +// The caller passes its own local as the byval pointer. +void call_fam(fam_t s) { arg_fam(s); } + +// CIR: cir.func {{.*}}@call_fam( +// CIR-NEXT: %[[S:.*]] = cir.alloca "s" align(4) init : !cir.ptr<!rec_fam_t, target_address_space(5)> +// CIR: cir.call @arg_fam(%[[S]]) {{.*}}: (!cir.ptr<!rec_fam_t, target_address_space(5)> {llvm.align = 4 : i64, llvm.byval = !rec_fam_t, llvm.noundef}) -> () +// LLVM: define {{.*}}void @call_fam(ptr addrspace(5) noundef byval(%struct.fam_t) align 4 %{{.*}}) +// LLVM: call void @arg_fam(ptr addrspace(5) noundef byval(%struct.fam_t) align 4 %{{.*}}) + +void take_var(int n, ...); + +// Promoted scalars pass through an ellipsis unchanged. +void call_var_scalars(char c, double d) { take_var(2, c, d); } + +// CIR: cir.func {{.*}}@call_var_scalars( +// CIR: %[[C:.*]] = cir.load align(1) %{{.*}} : !cir.ptr<!s8i>, !s8i +// CIR-NEXT: %[[C_INT:.*]] = cir.cast integral %[[C]] : !s8i -> !s32i +// CIR-NEXT: %[[D:.*]] = cir.load align(8) %{{.*}} : !cir.ptr<!cir.double>, !cir.double +// CIR-NEXT: cir.call @take_var(%{{.*}}, %[[C_INT]], %[[D]]) {{.*}}: (!s32i {llvm.noundef}, !s32i {llvm.noundef}, !cir.double {llvm.noundef}) -> () +// CIR: cir.func private @take_var(!s32i {llvm.noundef}, ...) +// LLVM: define {{.*}}void @call_var_scalars( +// LLVM: %[[C_INT:.*]] = sext i8 %{{.*}} to i32 +// LLVM-NEXT: %[[D:.*]] = load double, ptr %{{.*}}, align 8 +// LLVM-NEXT: call void (i32, ...) @take_var(i32 noundef 2, i32 noundef %[[C_INT]], double noundef %[[D]]) +// LLVM: declare void @take_var(i32 noundef, ...) + +// Aggregates pass through an ellipsis whole, not packed into registers. +void call_var_aggs(pair_t p, small_t s) { take_var(2, p, s); } + +// CIR: cir.func {{.*}}@call_var_aggs( +// CIR: %[[P:.*]] = cir.load align(4) %{{.*}} : !cir.ptr<!rec_pair_t>, !rec_pair_t +// CIR-NEXT: %[[S:.*]] = cir.load align(1) %{{.*}} : !cir.ptr<!rec_small_t>, !rec_small_t +// CIR-NEXT: cir.call @take_var(%{{.*}}, %[[P]], %[[S]]) {{.*}}: (!s32i {llvm.noundef}, !rec_pair_t, !rec_small_t) -> () +// LLVM: define {{.*}}void @call_var_aggs([2 x i32] %{{.*}}, i32 %{{.*}}) +// LLVM: %[[P:.*]] = load %struct.pair_t, ptr %{{.*}}, align 4 +// LLVM-NEXT: %[[S:.*]] = load %struct.small_t, ptr %{{.*}}, align 1 +// LLVM-NEXT: call void (i32, ...) @take_var(i32 noundef 2, %struct.pair_t %[[P]], %struct.small_t %[[S]]) + +// Through an ellipsis a _Complex is passed whole. +void call_var_cdouble(_Complex double c) { take_var(1, c); } + +// CIR: cir.func {{.*}}@call_var_cdouble(%{{.*}}: !cir.double {llvm.noundef}{{.*}}, %{{.*}}: !cir.double {llvm.noundef}{{.*}}) +// CIR: cir.store align(8) %{{.*}}, %[[TMP:.*]] : !cir.complex<!cir.double>, !cir.ptr<!cir.complex<!cir.double>> +// CIR-NEXT: %[[C:.*]] = cir.load align(8) %[[TMP]] : !cir.ptr<!cir.complex<!cir.double>>, !cir.complex<!cir.double> +// CIR-NEXT: cir.call @take_var(%{{.*}}, %[[C]]) {{.*}}: (!s32i {llvm.noundef}, !cir.complex<!cir.double> {llvm.noundef}) -> () +// LLVM: define {{.*}}void @call_var_cdouble(double noundef %{{.*}}, double noundef %{{.*}}) +// LLVM: call void (i32, ...) @take_var(i32 noundef 1, { double, double } noundef %{{.*}}) diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-cxx-records.cpp b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-cxx-records.cpp index e434f9d37bcfc1..471ca1684b28b0 100644 --- a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-cxx-records.cpp +++ b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-cxx-records.cpp @@ -6,16 +6,8 @@ // RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -emit-llvm %s -o %t.ll // RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s -// Checks that CallConvLowering classifies C++ records on ordinary AMDGPU -// functions, where C++ rules on triviality and layout change the result. - -// TODO(cir): Add the C++ record cases still NYI in CallConvLowering: -// - Non-trivial records as arguments. Classic passes them indirectly as -// ptr addrspace(5), which the bridge cannot express yet. -// - Member function pointers, lowered to a 128-bit pair that the bridge -// cannot coerce yet. -// - Records of 33 to 64 bits, such as a base plus a field, which classic -// coerces to [2 x i32]. +// Checks that CallConvLowering passes and returns C++ records the same way as +// the existing LLVM codegen, where triviality and layout change the result. struct Empty {}; @@ -32,13 +24,33 @@ struct NonCopyable { struct TrivialBase { int i; }; struct DerivedSingle : TrivialBase {}; +struct DerivedPair : TrivialBase { int j; }; struct MemberPtrHolder { int TrivialBase::*p; }; +typedef void (TrivialBase::*mfp_t)(); -// A record that cannot be passed in registers returns through a generic sret. +// A record that cannot be passed in registers is built directly in a generic +// sret. NonTrivialDtor ret_non_trivial_dtor() { return {1}; } -// CIR: cir.func {{.*}}@_Z20ret_non_trivial_dtorv(%arg0: !cir.ptr<!rec_NonTrivialDtor> {{.*}}llvm.sret = !rec_NonTrivialDtor -// LLVM: define {{.*}}void @_Z20ret_non_trivial_dtorv(ptr dead_on_unwind noalias writable sret(%struct.NonTrivialDtor) align 4 %{{.*}}) +// CIR: cir.func {{.*}}@_Z20ret_non_trivial_dtorv(%[[RET:[^:]+]]: !cir.ptr<!rec_NonTrivialDtor> {{.*}}llvm.sret = !rec_NonTrivialDtor +// CIR-NEXT: %[[I:.*]] = cir.get_member %[[RET]][0] {name = "i"} : !cir.ptr<!rec_NonTrivialDtor> -> !cir.ptr<!s32i> +// CIR-NEXT: %[[ONE:.*]] = cir.const #cir.int<1> : !s32i +// CIR-NEXT: cir.store align(4) %[[ONE]], %[[I]] : !s32i, !cir.ptr<!s32i> +// CIR-NEXT: cir.return +// LLVM: define {{.*}}void @_Z20ret_non_trivial_dtorv(ptr dead_on_unwind noalias writable sret(%struct.NonTrivialDtor) align 4 %[[RET:.*]]) +// LLVM: %[[I:.*]] = getelementptr inbounds nuw %struct.NonTrivialDtor, ptr %[[RET]], i32 0, i32 0 +// LLVM: store i32 1, ptr %[[I]], align 4 + +// The caller's local is built in place by the callee. +void use_non_trivial_dtor() { NonTrivialDtor x = ret_non_trivial_dtor(); } + +// CIR: cir.func {{.*}}@_Z20use_non_trivial_dtorv() +// CIR: %[[X:.*]] = cir.alloca "x" {{.*}} : !cir.ptr<!rec_NonTrivialDtor, target_address_space(5)> +// CIR: %[[X_VIEW:.*]] = cir.cast address_space %[[X]] : !cir.ptr<!rec_NonTrivialDtor, target_address_space(5)> -> !cir.ptr<!rec_NonTrivialDtor> +// CIR: cir.call @_Z20ret_non_trivial_dtorv(%[[X_VIEW]]) {{.*}}: (!cir.ptr<!rec_NonTrivialDtor> {{.*}}llvm.sret = !rec_NonTrivialDtor +// CIR: cir.call @_ZN14NonTrivialDtorD1Ev(%[[X_VIEW]]) +// LLVM: define {{.*}}void @_Z20use_non_trivial_dtorv() +// LLVM: call void @_Z20ret_non_trivial_dtorv(ptr dead_on_unwind writable sret(%struct.NonTrivialDtor) align 4 %{{.*}}) NonCopyable ret_non_copyable() { return NonCopyable(); } @@ -56,6 +68,24 @@ void arg_derived_single(DerivedSingle d) {} // CIR: cir.func {{.*}}@_Z18arg_derived_single13DerivedSingle(%arg0: !s32i{{.*}}) // LLVM: define {{.*}}void @_Z18arg_derived_single13DerivedSingle(i32 %{{.*}}) +// A base plus a field is packed into a pair of i32 registers. +DerivedPair ret_derived_pair() { return {}; } + +// CIR: cir.func {{.*}}@_Z16ret_derived_pairv() -> !cir.array<!u32i x 2> +// CIR: %[[RET:.*]] = cir.load align(4) %{{.*}} : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)>, !cir.array<!u32i x 2> +// CIR: cir.return %[[RET]] : !cir.array<!u32i x 2> +// LLVM: define {{.*}}[2 x i32] @_Z16ret_derived_pairv() +// LLVM: %[[RET:.*]] = load [2 x i32], ptr addrspace(5) %{{.*}}, align 4 +// LLVM: ret [2 x i32] %[[RET]] + +void arg_derived_pair(DerivedPair d) {} + +// CIR: cir.func {{.*}}@_Z16arg_derived_pair11DerivedPair(%[[D:[^:]+]]: !cir.array<!u32i x 2>{{.*}}) +// CIR-NEXT: %[[D_COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR-NEXT: cir.store %[[D]], %[[D_COERCE]] : !cir.array<!u32i x 2>, !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// LLVM: define {{.*}}void @_Z16arg_derived_pair11DerivedPair([2 x i32] %[[D:[^)]+]]) +// LLVM: store [2 x i32] %[[D]], ptr addrspace(5) %{{.*}}, align 4 + // A data member pointer is lowered to an i64 offset before classification. MemberPtrHolder ret_member_ptr() { return {nullptr}; } @@ -67,8 +97,53 @@ void arg_member_ptr(MemberPtrHolder h) {} // CIR: cir.func {{.*}}@_Z14arg_member_ptr15MemberPtrHolder(%arg0: !s64i{{.*}}) // LLVM: define {{.*}}void @_Z14arg_member_ptr15MemberPtrHolder(i64 %{{.*}}) +// A member function pointer is lowered to a pair of i64 before +// classification, and the pair passes one field per register. +void arg_mfp(mfp_t f) {} + +// CIR: cir.func {{.*}}@_Z7arg_mfpM11TrivialBaseFvvE(%[[PTR:[^:]+]]: !s64i{{.*}}, %[[ADJ:[^:]+]]: !s64i{{.*}}) +// CIR-NEXT: %[[F_COERCE:.*]] = cir.alloca "coerce" align(8) : !cir.ptr<!rec_anon_struct, target_address_space(5)> +// CIR-NEXT: %[[F0:.*]] = cir.get_member %[[F_COERCE]][0] +// CIR-NEXT: cir.store %[[PTR]], %[[F0]] +// CIR-NEXT: %[[F1:.*]] = cir.get_member %[[F_COERCE]][1] +// CIR-NEXT: cir.store %[[ADJ]], %[[F1]] +// LLVM: define {{.*}}void @_Z7arg_mfpM11TrivialBaseFvvE(i64 %[[PTR:[^,]+]], i64 %[[ADJ:[^)]+]]) +// LLVM: store i64 %[[PTR]], ptr addrspace(5) %{{.*}}, align 8 +// LLVM: store i64 %[[ADJ]], ptr addrspace(5) %{{.*}}, align 8 + +mfp_t ret_mfp() { return nullptr; } + +// CIR: cir.func {{.*}}@_Z7ret_mfpv() -> !rec_anon_struct +// CIR: %[[RET:.*]] = cir.load %{{.*}} : !cir.ptr<!rec_anon_struct, target_address_space(5)>, !rec_anon_struct +// CIR-NEXT: cir.return %[[RET]] : !rec_anon_struct +// LLVM: define {{.*}}{ i64, i64 } @_Z7ret_mfpv() + // An empty C++ record is one byte in size but is still dropped. void arg_empty(Empty e) {} // CIR: cir.func {{.*}}@_Z9arg_empty5Empty() // LLVM: define {{.*}}void @_Z9arg_empty5Empty() + +// A non-trivial record argument is passed through a pointer in addrspace(5). +void arg_non_trivial_dtor(NonTrivialDtor n) {} + +// CIR: cir.func {{.*}}@_Z20arg_non_trivial_dtor14NonTrivialDtor(%[[N:[^:]+]]: !cir.ptr<!rec_NonTrivialDtor, target_address_space(5)> {llvm.align = 4 : i64, llvm.dereferenceable = 4 : i64, llvm.nofreeobj, llvm.noundef} +// CIR-NEXT: %{{.*}} = cir.cast address_space %[[N]] : !cir.ptr<!rec_NonTrivialDtor, target_address_space(5)> -> !cir.ptr<!rec_NonTrivialDtor> +// CIR-NEXT: cir.return +// LLVM: define {{.*}}void @_Z20arg_non_trivial_dtor14NonTrivialDtor(ptr addrspace(5) nofreeobj noundef align 4 dereferenceable(4) %{{.*}}) + +// The caller passes its copied temporary, not a further copy. +void call_non_trivial_dtor(NonTrivialDtor &n) { arg_non_trivial_dtor(n); } + +// CIR: cir.func {{.*}}@_Z21call_non_trivial_dtorR14NonTrivialDtor( +// CIR: %[[TMP:.*]] = cir.alloca "agg.tmp0" align(4) : !cir.ptr<!rec_NonTrivialDtor, target_address_space(5)> +// CIR-NEXT: %[[TMP_VIEW:.*]] = cir.cast address_space %[[TMP]] : !cir.ptr<!rec_NonTrivialDtor, target_address_space(5)> -> !cir.ptr<!rec_NonTrivialDtor> +// CIR: cir.copy %{{.*}} align(4) to %[[TMP_VIEW]] align(4) : !cir.ptr<!rec_NonTrivialDtor> +// CIR: cir.call @_Z20arg_non_trivial_dtor14NonTrivialDtor(%[[TMP]]) {{.*}}: (!cir.ptr<!rec_NonTrivialDtor, target_address_space(5)> {{.*}}) -> () +// CIR: cir.call @_ZN14NonTrivialDtorD1Ev(%[[TMP_VIEW]]) +// LLVM: define {{.*}}void @_Z21call_non_trivial_dtorR14NonTrivialDtor( +// LLVM: %[[TMP:.*]] = alloca %struct.NonTrivialDtor, align 4, addrspace(5) +// LLVM: %[[TMP_VIEW:.*]] = addrspacecast ptr addrspace(5) %[[TMP]] to ptr +// LLVM: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %[[TMP_VIEW]], ptr align 4 %{{.*}}, i64 4, i1 false) +// LLVM: call void @_Z20arg_non_trivial_dtor14NonTrivialDtor(ptr addrspace(5) nofreeobj noundef align 4 dereferenceable(4) %{{.*}}) +// LLVM: call void @_ZN14NonTrivialDtorD1Ev(ptr {{.*}}%[[TMP_VIEW]]) diff --git a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-return-types.c b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-return-types.c index e62674bd5a1702..4db471a01a7b64 100644 --- a/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-return-types.c +++ b/clang/test/CIR/CodeGen/call-conv-lowering-amdgpu-return-types.c @@ -6,19 +6,8 @@ // RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -emit-llvm %s -o %t.ll // RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s -// Checks that CallConvLowering classifies return values of ordinary AMDGPU -// functions, which use the default convention rather than the kernel one. - -// TODO(cir): Add the return cases still NYI in CallConvLowering: -// - Aggregates of 33 to 64 bits, which classic coerces to [2 x i32]. -// abiTypeToCIR has no array case yet. -// - Aggregates over 64 bits that fit the 16-register budget. The classifier -// returns Direct with no coerce type, which the bridge rejects. -// - Larger aggregates, returned through sret in addrspace(5). The bridge -// cannot express an indirect address space yet. -// - Structs with a flexible array member. The bridge does not set that -// record flag yet. -// - 3-element vector returns, blocked on CIRGen's vec3 load and store NYI. +// Checks that CallConvLowering returns values from non-kernel AMDGPU functions +// the same way as the existing LLVM codegen. typedef struct {} empty_t; typedef struct { char c; } i8_wrapper_t; @@ -28,6 +17,13 @@ typedef struct { int *p; } ptr_wrapper_t; typedef struct { char a, b; } char2_t; typedef struct { char a, b, c; } small_t; typedef union { int i; float f; } int_float_t; +typedef struct { int a, b; } pair_t; +typedef struct { int a, b, c; } triple_t; +typedef struct { int data[17]; } big_t; +typedef struct { int data[16]; } big16_t; +typedef struct { int n; int data[]; } fam_t; +typedef struct { _Complex double c; } cdwrap_t; +typedef union { int a[3]; double d; } u_pad_t; void ret_void(void) {} @@ -85,6 +81,7 @@ int *ret_ptr(void) { return 0; } // CIR: cir.func {{.*}}@ret_ptr() -> {{\(?}}!cir.ptr<!s32i> // LLVM: define {{.*}}ptr @ret_ptr() + _BitInt(65) ret_bitint65(void) { return 0; } // CIR: cir.func {{.*}}@ret_bitint65() -> {{\(?}}!cir.int<s, 65, bitint> @@ -132,6 +129,19 @@ ptr_wrapper_t ret_ptr_wrapper(void) { // CIR: cir.func {{.*}}@ret_ptr_wrapper() -> !cir.ptr<{{[^>]*}}> // LLVM: define {{.*}}ptr @ret_ptr_wrapper() +// A one-field struct return is not unwrapped. +cdwrap_t ret_cdwrap(void) { + cdwrap_t w = {0}; + return w; +} + +// CIR: cir.func {{.*}}@ret_cdwrap() -> !rec_cdwrap_t +// CIR: %[[RET:.*]] = cir.load %{{.*}} : !cir.ptr<!rec_cdwrap_t, target_address_space(5)>, !rec_cdwrap_t +// CIR-NEXT: cir.return %[[RET]] : !rec_cdwrap_t +// LLVM: define {{.*}}%struct.cdwrap_t @ret_cdwrap() +// LLVM: %[[RET:.*]] = load %struct.cdwrap_t, ptr addrspace(5) %{{.*}}, align 8 +// LLVM-NEXT: ret %struct.cdwrap_t %[[RET]] + // Aggregates up to 32 bits are packed into one integer register. char2_t ret_char2(void) { char2_t s = {1, 2}; @@ -156,3 +166,126 @@ int_float_t ret_union(void) { // CIR: cir.func {{.*}}@ret_union() -> !u32i // LLVM: define {{.*}}i32 @ret_union() + +// Aggregates of 33 to 64 bits are packed into a pair of i32 registers. +pair_t ret_pair(void) { + pair_t p = {1, 2}; + return p; +} + +// CIR: cir.func {{.*}}@ret_pair() -> !cir.array<!u32i x 2> +// CIR: %[[RETVAL:.*]] = cir.alloca "__retval" {{.*}} : !cir.ptr<!rec_pair_t, target_address_space(5)> +// CIR: %[[RET_ARR:.*]] = cir.cast bitcast %[[RETVAL]] : !cir.ptr<!rec_pair_t, target_address_space(5)> -> !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR: %[[RET:.*]] = cir.load align(4) %[[RET_ARR]] : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)>, !cir.array<!u32i x 2> +// CIR: cir.return %[[RET]] : !cir.array<!u32i x 2> +// LLVM: define {{.*}}[2 x i32] @ret_pair() +// LLVM: %[[RET:.*]] = load [2 x i32], ptr addrspace(5) %{{.*}}, align 4 +// LLVM: ret [2 x i32] %[[RET]] + +// The call site unpacks the result the same way. +void call_ret_pair(void) { pair_t p = ret_pair(); } + +// CIR: cir.func {{.*}}@call_ret_pair() +// CIR: %[[COERCE:.*]] = cir.alloca "coerce" align(4) : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR: %[[P:.*]] = cir.alloca "p" {{.*}} : !cir.ptr<!rec_pair_t, target_address_space(5)> +// CIR: %[[P_VIEW:.*]] = cir.cast address_space %[[P]] : !cir.ptr<!rec_pair_t, target_address_space(5)> -> !cir.ptr<!rec_pair_t> +// CIR: %[[RES:.*]] = cir.call @ret_pair() {{.*}}: () -> !cir.array<!u32i x 2> +// CIR: cir.store %[[RES]], %[[COERCE]] : !cir.array<!u32i x 2>, !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR: %[[COERCE_REC:.*]] = cir.cast bitcast %[[COERCE]] : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> -> !cir.ptr<!rec_pair_t, target_address_space(5)> +// CIR: cir.copy %[[COERCE_REC]] align(4) to %[[P_VIEW]] align(4) +// LLVM: define {{.*}}void @call_ret_pair() +// LLVM: %[[RES:.*]] = call [2 x i32] @ret_pair() +// LLVM: store [2 x i32] %[[RES]], ptr addrspace(5) %{{.*}}, align 4 + +// A _Complex up to 64 bits is packed like a small aggregate. +_Complex float ret_cfloat(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_cfloat() -> !cir.array<!u32i x 2> +// CIR: %[[RET_PTR:.*]] = cir.cast bitcast %{{.*}} : !cir.ptr<!cir.complex<!cir.float>, target_address_space(5)> -> !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)> +// CIR-NEXT: %[[RET:.*]] = cir.load %[[RET_PTR]] : !cir.ptr<!cir.array<!u32i x 2>, target_address_space(5)>, !cir.array<!u32i x 2> +// CIR-NEXT: cir.return %[[RET]] : !cir.array<!u32i x 2> +// LLVM: define {{.*}}[2 x i32] @ret_cfloat() +// LLVM: %[[RET:.*]] = load [2 x i32], ptr addrspace(5) %{{.*}}, align 4 +// LLVM-NEXT: ret [2 x i32] %[[RET]] + +// Larger aggregates that fit the register budget return whole. +triple_t ret_triple(void) { + triple_t t = {1, 2, 3}; + return t; +} + +// CIR: cir.func {{.*}}@ret_triple() -> !rec_triple_t +// CIR: %[[RETVAL:.*]] = cir.alloca "__retval" {{.*}} : !cir.ptr<!rec_triple_t, target_address_space(5)> +// CIR: %[[RET:.*]] = cir.load %[[RETVAL]] : !cir.ptr<!rec_triple_t, target_address_space(5)>, !rec_triple_t +// CIR: cir.return %[[RET]] : !rec_triple_t +// LLVM: define {{.*}}%struct.triple_t @ret_triple() +// LLVM: %[[RET:.*]] = load %struct.triple_t, ptr addrspace(5) %{{.*}}, align 4 +// LLVM: ret %struct.triple_t %[[RET]] + +// A wider _Complex that fits the register budget returns whole. +_Complex double ret_cdouble(void) { return 0; } + +// CIR: cir.func {{.*}}@ret_cdouble() -> ![[PAIR:rec_anon_struct[0-9]*]] +// CIR: %[[RET_PTR:.*]] = cir.cast bitcast %{{.*}} : !cir.ptr<!cir.complex<!cir.double>, target_address_space(5)> -> !cir.ptr<![[PAIR]], target_address_space(5)> +// CIR-NEXT: %[[RET:.*]] = cir.load %[[RET_PTR]] : !cir.ptr<![[PAIR]], target_address_space(5)>, ![[PAIR]] +// CIR-NEXT: cir.return %[[RET]] : ![[PAIR]] +// LLVM: define {{.*}}{ double, double } @ret_cdouble() +// LLVM: ret { double, double } %{{.*}} + +// A union return is not flattened. +u_pad_t ret_u_pad(u_pad_t *p) { return *p; } + +// CIR: cir.func {{.*}}@ret_u_pad(%{{.*}}: !cir.ptr<!rec_u_pad_t> {llvm.noundef}{{.*}}) -> !rec_u_pad_t +// CIR: %[[RET:.*]] = cir.load %{{.*}} : !cir.ptr<!rec_u_pad_t, target_address_space(5)>, !rec_u_pad_t +// CIR-NEXT: cir.return %[[RET]] : !rec_u_pad_t +// LLVM: define {{.*}}%union.u_pad_t @ret_u_pad(ptr noundef %{{.*}}) +// LLVM: %[[RET:.*]] = load %union.u_pad_t, ptr addrspace(5) %{{.*}}, align 8 +// LLVM-NEXT: ret %union.u_pad_t %[[RET]] + +// An aggregate of exactly 16 registers is returned direct. +big16_t ret_big16(big16_t *p) { return *p; } + +// CIR: cir.func {{.*}}@ret_big16(%{{.*}}: !cir.ptr<!rec_big16_t> {llvm.noundef}{{.*}}) -> !rec_big16_t +// CIR: %[[RET:.*]] = cir.load %{{.*}} : !cir.ptr<!rec_big16_t, target_address_space(5)>, !rec_big16_t +// CIR-NEXT: cir.return %[[RET]] : !rec_big16_t +// LLVM: define {{.*}}%struct.big16_t @ret_big16(ptr noundef %{{.*}}) +// LLVM: %[[RET:.*]] = load %struct.big16_t, ptr addrspace(5) %{{.*}}, align 4 +// LLVM-NEXT: ret %struct.big16_t %[[RET]] + +// An aggregate past the register budget returns through sret in addrspace(5). +big_t ret_big(void) { + big_t b = {0}; + return b; +} + +// CIR: cir.func {{.*}}@ret_big(%[[RET:[^:]+]]: !cir.ptr<!rec_big_t, target_address_space(5)> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.noalias, llvm.sret = !rec_big_t, llvm.writable} +// CIR-NEXT: %[[RET_VIEW:.*]] = cir.cast address_space %[[RET]] : !cir.ptr<!rec_big_t, target_address_space(5)> -> !cir.ptr<!rec_big_t> +// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.zero : !rec_big_t +// CIR-NEXT: cir.store align(4) %[[ZERO]], %[[RET_VIEW]] : !rec_big_t, !cir.ptr<!rec_big_t> +// CIR-NEXT: cir.return +// LLVM: define {{.*}}void @ret_big(ptr addrspace(5) dead_on_unwind noalias writable sret(%struct.big_t) align 4 %[[RET:[^)]+]]) +// LLVM: %{{.*}} = addrspacecast ptr addrspace(5) %[[RET]] to ptr + +// The caller's local is passed as the sret. +void call_ret_big(void) { big_t b = ret_big(); } + +// CIR: cir.func {{.*}}@call_ret_big() +// CIR-NEXT: %[[B:.*]] = cir.alloca "b" align(4) init : !cir.ptr<!rec_big_t, target_address_space(5)> +// CIR: cir.call @ret_big(%[[B]]) {{.*}}: (!cir.ptr<!rec_big_t, target_address_space(5)> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.sret = !rec_big_t, llvm.writable}) -> () +// LLVM: define {{.*}}void @call_ret_big() +// LLVM: alloca %struct.big_t, align 4, addrspace(5) +// LLVM: call void @ret_big(ptr addrspace(5) dead_on_unwind writable sret(%struct.big_t) align 4 %{{.*}}) + +// A struct with a flexible array member returns through sret in addrspace(5). +fam_t ret_fam(void) { + fam_t s = {0}; + return s; +} + +// CIR: cir.func {{.*}}@ret_fam(%[[RET:[^:]+]]: !cir.ptr<!rec_fam_t, target_address_space(5)> {llvm.align = 4 : i64, llvm.dead_on_unwind, llvm.noalias, llvm.sret = !rec_fam_t, llvm.writable} +// CIR-NEXT: %[[RET_VIEW:.*]] = cir.cast address_space %[[RET]] : !cir.ptr<!rec_fam_t, target_address_space(5)> -> !cir.ptr<!rec_fam_t> +// CIR-NEXT: %[[ZERO:.*]] = cir.const #cir.zero : !rec_fam_t +// CIR-NEXT: cir.store align(4) %[[ZERO]], %[[RET_VIEW]] : !rec_fam_t, !cir.ptr<!rec_fam_t> +// CIR-NEXT: cir.return +// LLVM: define {{.*}}void @ret_fam(ptr addrspace(5) dead_on_unwind noalias writable sret(%struct.fam_t) align 4 %[[RET:[^)]+]]) +// LLVM: %{{.*}} = addrspacecast ptr addrspace(5) %[[RET]] to ptr diff --git a/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip b/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip index d713a3bd4cf7e2..05afba022c7921 100644 --- a/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip +++ b/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip @@ -9,9 +9,6 @@ // Checks that CallConvLowering classifies AMDGPU kernel arguments under the // kernel convention. -// TODO(cir): Add multi-field aggregates, passed byref in addrspace(4), once -// CallConvLowering supports IndirectAliased. - #define __global__ __attribute__((global)) #define __device__ __attribute__((device)) @@ -19,6 +16,14 @@ typedef struct { int i; } i32_wrapper_t; typedef struct { i32_wrapper_t w; } nested_t; typedef struct { float f[1]; } f32_array1_t; typedef struct { float *x; } ptr_wrapper_t; +typedef struct { int a; int b; } pair_t; +typedef struct {} empty_t; +typedef struct { float *x; int n; } ptr_pair_t; + +// Unlike ptr_wrapper_t, the pointer field is not coerced to addrspace(1). +// CIR: !rec_ptr_pair_t = !cir.struct<"ptr_pair_t" {data !cir.ptr<!cir.float>, data !s32i}> +// LLVM: %struct.ptr_pair_t = type { ptr, i32 } + typedef float float3 __attribute__((ext_vector_type(3))); typedef float float4 __attribute__((ext_vector_type(4))); typedef short short4 __attribute__((ext_vector_type(4))); @@ -108,6 +113,37 @@ __global__ void kern_array1(f32_array1_t a) {} // CIR: cir.func {{.*}}@_Z11kern_array112f32_array1_t(%arg0: !cir.float{{.*}}){{.*}}cc(amdgpu_kernel) // LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_array112f32_array1_t(float %{{.*}}) +// A multi-field struct is read in place from the kernarg segment, addrspace(4). +__global__ void kern_pair(pair_t p) {} + +// CIR: cir.func {{.*}}@_Z9kern_pair6pair_t(%[[PAIR:[^:]+]]: !cir.ptr<!rec_pair_t, target_address_space(4)> {{.*}}llvm.byref = !rec_pair_t{{.*}}cc(amdgpu_kernel) +// CIR: cir.copy %[[PAIR]] align(4) to %{{.*}} align(4) : !cir.ptr<!rec_pair_t, target_address_space(4)>, !cir.ptr<!rec_pair_t> +// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_pair6pair_t(ptr addrspace(4) noundef byref(%struct.pair_t) align 4 %[[PAIR:.*]]) +// LLVM: call void @llvm.memcpy.{{.*}}(ptr{{.*}} align 4 %{{.*}}, ptr addrspace(4) align 4 %[[PAIR]], i64 8, i1 false) + +// Kernels do not drop an empty struct. It is read from the kernarg segment. +__global__ void kern_empty(empty_t e) {} + +// CIR: cir.func {{.*}}@_Z10kern_empty7empty_t(%[[E:[^:]+]]: !cir.ptr<!rec_empty_t, target_address_space(4)> {{.*}}llvm.byref = !rec_empty_t{{.*}}cc(amdgpu_kernel) +// CIR: cir.copy %[[E]] align(1) to %{{.*}} align(1) : !cir.ptr<!rec_empty_t, target_address_space(4)>, !cir.ptr<!rec_empty_t> +// LLVM: define {{.*}}amdgpu_kernel void @_Z10kern_empty7empty_t(ptr addrspace(4) noundef byref(%struct.empty_t) align 1 %[[E:.*]]) +// LLVM: call void @llvm.memcpy.{{.*}}(ptr{{.*}} align 1 %{{.*}}, ptr addrspace(4) align 1 %[[E]], i64 1, i1 false) + +// A _Complex is an aggregate, so it is read in place too. +__global__ void kern_cfloat(_Complex float c) {} + +// CIR: cir.func {{.*}}@_Z11kern_cfloatCf(%[[C:[^:]+]]: !cir.ptr<!cir.complex<!cir.float>, target_address_space(4)> {{.*}}llvm.byref = !cir.complex<!cir.float>{{.*}}cc(amdgpu_kernel) +// CIR-NEXT: cir.load %[[C]] : !cir.ptr<!cir.complex<!cir.float>, target_address_space(4)>, !cir.complex<!cir.float> +// LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_cfloatCf(ptr addrspace(4) noundef byref({ float, float }) align 4 %{{.*}}) + +// A pointer inside a multi-field struct stays generic. +__global__ void kern_ptr_pair(ptr_pair_t p) {} + +// CIR: cir.func {{.*}}@_Z13kern_ptr_pair10ptr_pair_t(%[[P:[^:]+]]: !cir.ptr<!rec_ptr_pair_t, target_address_space(4)> {{.*}}llvm.byref = !rec_ptr_pair_t{{.*}}cc(amdgpu_kernel) +// CIR: cir.copy %[[P]] align(8) to %{{.*}} align(8) : !cir.ptr<!rec_ptr_pair_t, target_address_space(4)>, !cir.ptr<!rec_ptr_pair_t> +// LLVM: define {{.*}}amdgpu_kernel void @_Z13kern_ptr_pair10ptr_pair_t(ptr addrspace(4) noundef byref(%struct.ptr_pair_t) align 8 %[[P:.*]]) +// LLVM: call void @llvm.memcpy.{{.*}}(ptr{{.*}} align 8 %{{.*}}, ptr addrspace(4) align 8 %[[P]], i64 16, i1 false) + // Kernel arguments are read from the kernarg segment, so sub-word integers // and bool are not extended, unlike in a device function. __global__ void kern_char(char c) {} >From ff34c08b6f815b988c799794772e61b2291a5477 Mon Sep 17 00:00:00 2001 From: skc7 <[email protected]> Date: Mon, 16 Nov 2026 10:54:52 +0530 Subject: [PATCH 3/3] update hip test --- .../call-conv-lowering-amdgpu-kernel-args.hip | 60 +++++++++---------- 1 file changed, 30 insertions(+), 30 deletions(-) diff --git a/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip b/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip index 05afba022c7921..0edc1f063ace13 100644 --- a/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip +++ b/clang/test/CIR/CodeGenHIP/call-conv-lowering-amdgpu-kernel-args.hip @@ -6,8 +6,8 @@ // RUN: %clang_cc1 -triple amdgpu-amd-amdhsa -x hip -fcuda-is-device -emit-llvm %s -o %t.ll // RUN: FileCheck --check-prefix=LLVM --input-file=%t.ll %s -// Checks that CallConvLowering classifies AMDGPU kernel arguments under the -// kernel convention. +// Checks that CallConvLowering passes HIP kernel arguments the same way as +// the existing LLVM codegen, through the kernarg segment. #define __global__ __attribute__((global)) #define __device__ __attribute__((device)) @@ -49,6 +49,34 @@ __global__ void kern_bitint(_BitInt(65) b) {} // CIR: cir.func {{.*}}@_Z11kern_bitintDB65_(%arg0: !cir.int<s, 65, bitint> {{.*}}){{.*}}cc(amdgpu_kernel) // LLVM: define {{.*}}amdgpu_kernel void @_Z11kern_bitintDB65_(i65 noundef %{{.*}}) +// Kernel arguments are read from the kernarg segment, so sub-word integers +// and bool are not extended, unlike in a device function. +__global__ void kern_char(char c) {} + +// CIR: cir.func {{.*}}@_Z9kern_charc(%arg0: !s8i {llvm.noundef}{{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_charc(i8 noundef %{{.*}}) + +__global__ void kern_uchar(unsigned char c) {} + +// CIR: cir.func {{.*}}@_Z10kern_ucharh(%arg0: !u8i {llvm.noundef}{{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z10kern_ucharh(i8 noundef %{{.*}}) + +__global__ void kern_short(short s) {} + +// CIR: cir.func {{.*}}@_Z10kern_shorts(%arg0: !s16i {llvm.noundef}{{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z10kern_shorts(i16 noundef %{{.*}}) + +__global__ void kern_bool(bool b) {} + +// CIR: cir.func {{.*}}@_Z9kern_boolb(%arg0: !cir.bool {llvm.noundef}{{.*}}cc(amdgpu_kernel) +// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_boolb(i1 noundef %{{.*}}) + +// The same argument to a device function is extended. +__device__ void dev_char(char c) {} + +// CIR: cir.func {{.*}}@_Z8dev_charc(%arg0: !s8i {{.*}}llvm.signext +// LLVM: define {{.*}}void @_Z8dev_charc(i8 noundef signext %{{.*}}) + // Vectors, including 16-bit and 3-element ones, pass whole. __global__ void kern_float4(float4 v) {} @@ -143,31 +171,3 @@ __global__ void kern_ptr_pair(ptr_pair_t p) {} // CIR: cir.copy %[[P]] align(8) to %{{.*}} align(8) : !cir.ptr<!rec_ptr_pair_t, target_address_space(4)>, !cir.ptr<!rec_ptr_pair_t> // LLVM: define {{.*}}amdgpu_kernel void @_Z13kern_ptr_pair10ptr_pair_t(ptr addrspace(4) noundef byref(%struct.ptr_pair_t) align 8 %[[P:.*]]) // LLVM: call void @llvm.memcpy.{{.*}}(ptr{{.*}} align 8 %{{.*}}, ptr addrspace(4) align 8 %[[P]], i64 16, i1 false) - -// Kernel arguments are read from the kernarg segment, so sub-word integers -// and bool are not extended, unlike in a device function. -__global__ void kern_char(char c) {} - -// CIR: cir.func {{.*}}@_Z9kern_charc(%arg0: !s8i {llvm.noundef}{{.*}}cc(amdgpu_kernel) -// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_charc(i8 noundef %{{.*}}) - -__global__ void kern_uchar(unsigned char c) {} - -// CIR: cir.func {{.*}}@_Z10kern_ucharh(%arg0: !u8i {llvm.noundef}{{.*}}cc(amdgpu_kernel) -// LLVM: define {{.*}}amdgpu_kernel void @_Z10kern_ucharh(i8 noundef %{{.*}}) - -__global__ void kern_short(short s) {} - -// CIR: cir.func {{.*}}@_Z10kern_shorts(%arg0: !s16i {llvm.noundef}{{.*}}cc(amdgpu_kernel) -// LLVM: define {{.*}}amdgpu_kernel void @_Z10kern_shorts(i16 noundef %{{.*}}) - -__global__ void kern_bool(bool b) {} - -// CIR: cir.func {{.*}}@_Z9kern_boolb(%arg0: !cir.bool {llvm.noundef}{{.*}}cc(amdgpu_kernel) -// LLVM: define {{.*}}amdgpu_kernel void @_Z9kern_boolb(i1 noundef %{{.*}}) - -// The same argument to a device function is extended. -__device__ void dev_char(char c) {} - -// CIR: cir.func {{.*}}@_Z8dev_charc(%arg0: !s8i {{.*}}llvm.signext -// LLVM: define {{.*}}void @_Z8dev_charc(i8 noundef signext %{{.*}}) _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
