Skip to content

Commit 4de1376

Browse files
authored
[CIR][CodeGen] Set CIR function calling conventions (#780)
This PR adds the counterparts of methods `SetFunctionAttributes` `SetLLVMFunctionAttributes` from OG CodeGen, in order to set proper calling conv for `cir.func` ops. `spir-calling-conv.cl` is the dedicated test, while other OpenCL-related tests are also updated. It removes previous workaround of incorrect calling conv and better synchronizes with the original tests. (These changes are not musts).
1 parent eda60d8 commit 4de1376

File tree

7 files changed

+71
-13
lines changed

7 files changed

+71
-13
lines changed

clang/lib/CIR/CodeGen/CIRGenFunctionInfo.h

+10-2
Original file line numberDiff line numberDiff line change
@@ -250,9 +250,17 @@ class CIRGenFunctionInfo final
250250
return getExtParameterInfos()[argIndex];
251251
}
252252

253-
/// getCallingConvention - REturn the user specified calling convention, which
253+
/// getCallingConvention - Return the user specified calling convention, which
254254
/// has been translated into a CIR CC.
255-
mlir::cir::CallingConv getCallingConvention() const { return CallingConvention; }
255+
mlir::cir::CallingConv getCallingConvention() const {
256+
return CallingConvention;
257+
}
258+
259+
/// getEffectiveCallingConvention - Return the actual calling convention to
260+
/// use, which may depend on the ABI.
261+
mlir::cir::CallingConv getEffectiveCallingConvention() const {
262+
return EffectiveCallingConvention;
263+
}
256264

257265
clang::CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
258266

clang/lib/CIR/CodeGen/CIRGenModule.cpp

+29-3
Original file line numberDiff line numberDiff line change
@@ -2360,9 +2360,35 @@ void CIRGenModule::setExtraAttributesForFunc(FuncOp f,
23602360
builder.getContext(), attrs.getDictionary(builder.getContext())));
23612361
}
23622362

2363-
void CIRGenModule::setFunctionAttributes(GlobalDecl GD, mlir::cir::FuncOp F,
2364-
bool IsIncompleteFunction,
2365-
bool IsThunk) {
2363+
void CIRGenModule::setCIRFunctionAttributes(GlobalDecl GD,
2364+
const CIRGenFunctionInfo &info,
2365+
mlir::cir::FuncOp func,
2366+
bool isThunk) {
2367+
// TODO(cir): More logic of constructAttributeList is needed.
2368+
// NOTE(cir): Here we only need CallConv, so a call to constructAttributeList
2369+
// is omitted for simplicity.
2370+
mlir::cir::CallingConv callingConv = info.getEffectiveCallingConvention();
2371+
2372+
// TODO(cir): Check X86_VectorCall incompatibility with WinARM64EC
2373+
2374+
func.setCallingConv(callingConv);
2375+
}
2376+
2377+
void CIRGenModule::setFunctionAttributes(GlobalDecl globalDecl,
2378+
mlir::cir::FuncOp func,
2379+
bool isIncompleteFunction,
2380+
bool isThunk) {
2381+
// NOTE(cir): Original CodeGen checks if this is an intrinsic. In CIR we
2382+
// represent them in dedicated ops. The correct attributes are ensured during
2383+
// translation to LLVM. Thus, we don't need to check for them here.
2384+
2385+
if (!isIncompleteFunction) {
2386+
setCIRFunctionAttributes(globalDecl,
2387+
getTypes().arrangeGlobalDeclaration(globalDecl),
2388+
func, isThunk);
2389+
}
2390+
2391+
// TODO(cir): Complete the remaining part of the function.
23662392
assert(!MissingFeatures::setFunctionAttributes());
23672393
}
23682394

clang/lib/CIR/CodeGen/CIRGenModule.h

+4
Original file line numberDiff line numberDiff line change
@@ -551,6 +551,10 @@ class CIRGenModule : public CIRGenTypeCache {
551551
void setFunctionAttributes(GlobalDecl GD, mlir::cir::FuncOp F,
552552
bool IsIncompleteFunction, bool IsThunk);
553553

554+
/// Set the CIR function attributes (sext, zext, etc).
555+
void setCIRFunctionAttributes(GlobalDecl GD, const CIRGenFunctionInfo &info,
556+
mlir::cir::FuncOp func, bool isThunk);
557+
554558
void buildGlobalDefinition(clang::GlobalDecl D,
555559
mlir::Operation *Op = nullptr);
556560
void buildGlobalFunctionDefinition(clang::GlobalDecl D, mlir::Operation *Op);

clang/test/CIR/CodeGen/OpenCL/kernel-arg-metadata.cl

+2-2
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
__kernel void kernel_function() {}
77

88
// CIR: #fn_attr[[ATTR:[0-9]*]] = {{.+}}cl.kernel_arg_metadata = #cir.cl.kernel_arg_metadata<addr_space = [], access_qual = [], type = [], base_type = [], type_qual = []>{{.+}}
9-
// CIR: cir.func @kernel_function() extra(#fn_attr[[ATTR]])
9+
// CIR: cir.func @kernel_function() cc(spir_kernel) extra(#fn_attr[[ATTR]])
1010

11-
// LLVM: define {{.*}}void @kernel_function() {{[^{]+}} !kernel_arg_addr_space ![[MD:[0-9]+]] !kernel_arg_access_qual ![[MD]] !kernel_arg_type ![[MD]] !kernel_arg_base_type ![[MD]] !kernel_arg_type_qual ![[MD]] {
11+
// LLVM: define {{.*}}spir_kernel void @kernel_function() {{[^{]+}} !kernel_arg_addr_space ![[MD:[0-9]+]] !kernel_arg_access_qual ![[MD]] !kernel_arg_type ![[MD]] !kernel_arg_base_type ![[MD]] !kernel_arg_type_qual ![[MD]] {
1212
// LLVM: ![[MD]] = !{}

clang/test/CIR/CodeGen/OpenCL/kernel-attributes.cl

+5-5
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// RUN: %clang_cc1 -fclangir -emit-cir -triple x86_64-unknown-linux-gnu %s -o %t.cir
1+
// RUN: %clang_cc1 -fclangir -emit-cir -triple spirv64-unknown-unknown %s -o %t.cir
22
// RUN: FileCheck %s --input-file=%t.cir --check-prefix=CIR
3-
// RUN: %clang_cc1 -fclangir -emit-llvm -triple x86_64-unknown-linux-gnu %s -o %t.ll
3+
// RUN: %clang_cc1 -fclangir -emit-llvm -triple spirv64-unknown-unknown %s -o %t.ll
44
// RUN: FileCheck %s --input-file=%t.ll --check-prefix=LLVM
55

66
typedef unsigned int uint4 __attribute__((ext_vector_type(4)));
@@ -11,7 +11,7 @@ kernel __attribute__((vec_type_hint(int))) __attribute__((reqd_work_group_size(
1111
// CIR-DAG: #fn_attr[[KERNEL1:[0-9]*]] = {{.+}}cl.kernel_metadata = #cir.cl.kernel_metadata<reqd_work_group_size = [1 : i32, 2 : i32, 4 : i32], vec_type_hint = !s32i, vec_type_hint_signedness = 1>{{.+}}
1212
// CIR-DAG: cir.func @kernel1{{.+}} extra(#fn_attr[[KERNEL1]])
1313

14-
// LLVM-DAG: define{{.*}}@kernel1(i32 {{[^%]*}}%0) {{[^{]+}} !reqd_work_group_size ![[MD1_REQD_WG:[0-9]+]] !vec_type_hint ![[MD1_VEC_TYPE:[0-9]+]]
14+
// LLVM-DAG: define {{(dso_local )?}}spir_kernel void @kernel1(i32 {{[^%]*}}%0) {{[^{]+}} !reqd_work_group_size ![[MD1_REQD_WG:[0-9]+]] !vec_type_hint ![[MD1_VEC_TYPE:[0-9]+]]
1515
// LLVM-DAG: [[MD1_VEC_TYPE]] = !{i32 undef, i32 1}
1616
// LLVM-DAG: [[MD1_REQD_WG]] = !{i32 1, i32 2, i32 4}
1717

@@ -21,7 +21,7 @@ kernel __attribute__((vec_type_hint(uint4))) __attribute__((work_group_size_hint
2121
// CIR-DAG: #fn_attr[[KERNEL2:[0-9]*]] = {{.+}}cl.kernel_metadata = #cir.cl.kernel_metadata<work_group_size_hint = [8 : i32, 16 : i32, 32 : i32], vec_type_hint = !cir.vector<!u32i x 4>, vec_type_hint_signedness = 0>{{.+}}
2222
// CIR-DAG: cir.func @kernel2{{.+}} extra(#fn_attr[[KERNEL2]])
2323

24-
// LLVM-DAG: define{{.*}}@kernel2(i32 {{[^%]*}}%0) {{[^{]+}} !vec_type_hint ![[MD2_VEC_TYPE:[0-9]+]] !work_group_size_hint ![[MD2_WG_SIZE:[0-9]+]]
24+
// LLVM-DAG: define {{(dso_local )?}}spir_kernel void @kernel2(i32 {{[^%]*}}%0) {{[^{]+}} !vec_type_hint ![[MD2_VEC_TYPE:[0-9]+]] !work_group_size_hint ![[MD2_WG_SIZE:[0-9]+]]
2525
// LLVM-DAG: [[MD2_VEC_TYPE]] = !{<4 x i32> undef, i32 0}
2626
// LLVM-DAG: [[MD2_WG_SIZE]] = !{i32 8, i32 16, i32 32}
2727

@@ -31,5 +31,5 @@ kernel __attribute__((intel_reqd_sub_group_size(8))) void kernel3(int a) {}
3131
// CIR-DAG: #fn_attr[[KERNEL3:[0-9]*]] = {{.+}}cl.kernel_metadata = #cir.cl.kernel_metadata<intel_reqd_sub_group_size = 8 : i32>{{.+}}
3232
// CIR-DAG: cir.func @kernel3{{.+}} extra(#fn_attr[[KERNEL3]])
3333

34-
// LLVM-DAG: define{{.*}}@kernel3(i32 {{[^%]*}}%0) {{[^{]+}} !intel_reqd_sub_group_size ![[MD3_INTEL:[0-9]+]]
34+
// LLVM-DAG: define {{(dso_local )?}}spir_kernel void @kernel3(i32 {{[^%]*}}%0) {{[^{]+}} !intel_reqd_sub_group_size ![[MD3_INTEL:[0-9]+]]
3535
// LLVM-DAG: [[MD3_INTEL]] = !{i32 8}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// RUN: %clang_cc1 -fclangir %s -O0 -triple "spirv64-unknown-unknown" -emit-cir -o %t.cir
2+
// RUN: FileCheck %s --input-file=%t.cir --check-prefix=CIR
3+
// RUN: %clang_cc1 -fclangir %s -O0 -triple "spirv64-unknown-unknown" -emit-llvm -o %t.ll
4+
// RUN: FileCheck %s --input-file=%t.ll --check-prefix=LLVM
5+
6+
// CIR: cir.func {{.*}}@get_dummy_id{{.*}} cc(spir_function)
7+
// LLVM-DAG: declare{{.*}} spir_func i32 @get_dummy_id(
8+
int get_dummy_id(int D);
9+
10+
// CIR: cir.func {{.*}}@bar{{.*}} cc(spir_kernel)
11+
// LLVM-DAG: declare{{.*}} spir_kernel void @bar(
12+
kernel void bar(global int *A);
13+
14+
// CIR: cir.func {{.*}}@foo{{.*}} cc(spir_kernel)
15+
// LLVM-DAG: define{{.*}} spir_kernel void @foo(
16+
kernel void foo(global int *A) {
17+
int id = get_dummy_id(0);
18+
A[id] = id;
19+
bar(A);
20+
}

clang/test/CIR/CodeGen/OpenCL/spirv-target.cl

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ typedef struct {
1515
} my_st;
1616

1717
// CIR-SPIRV64: cir.func @func(
18-
// LLVM-SPIRV64: @func(
18+
// LLVM-SPIRV64: define spir_kernel void @func(
1919
kernel void func(global long *arg) {
2020
int res1[sizeof(my_st) == 24 ? 1 : -1]; // expected-no-diagnostics
2121
int res2[sizeof(void *) == 8 ? 1 : -1]; // expected-no-diagnostics

0 commit comments

Comments
 (0)