Skip to content

Commit 6a79d2b

Browse files
authored
[CIR][Lowering] Supports varargs in the CallingConvention pass (#1005)
This PR adds several copy-pasted lines and a small test and now var args seems to work in the calling convention pass
1 parent 5873d88 commit 6a79d2b

File tree

3 files changed

+32
-12
lines changed

3 files changed

+32
-12
lines changed

clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerCall.cpp

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,13 @@ arrangeFreeFunctionLikeCall(LowerTypes &LT, LowerModule &LM,
4747

4848
cir_cconv_assert(!::cir::MissingFeatures::chainCall() && !chainCall && "NYI");
4949
FnInfoOpts opts = chainCall ? FnInfoOpts::IsChainCall : FnInfoOpts::None;
50-
return LT.arrangeLLVMFunctionInfo(fnType.getReturnType(), opts,
51-
fnType.getInputs(), required);
50+
51+
SmallVector<Type> argTypes;
52+
for (const auto &a : args)
53+
argTypes.push_back(a.getType());
54+
55+
return LT.arrangeLLVMFunctionInfo(fnType.getReturnType(), opts, argTypes,
56+
required);
5257
}
5358

5459
/// Adds the formal parameters in FPT to the given prefix. If any parameter in

clang/lib/CIR/Dialect/Transforms/TargetLowering/LowerFunctionInfo.h

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,15 @@ class RequiredArgs {
4949
if (!prototype.isVarArg())
5050
return All;
5151

52-
cir_cconv_assert_or_abort(!::cir::MissingFeatures::variadicFunctions(),
53-
"NYI");
54-
return All; // FIXME(cir): Temporary workaround for the assertion above.
52+
return RequiredArgs(prototype.getNumInputs() + additional);
5553
}
5654

5755
bool allowsOptionalArgs() const { return NumRequired != ~0U; }
56+
57+
unsigned getNumRequiredArgs() const {
58+
assert(allowsOptionalArgs());
59+
return NumRequired;
60+
}
5861
};
5962

6063
// Implementation detail of LowerFunctionInfo, factored out so it can be
@@ -149,14 +152,9 @@ class LowerFunctionInfo final
149152

150153
unsigned arg_size() const { return NumArgs; }
151154

152-
bool isVariadic() const {
153-
cir_cconv_assert(!::cir::MissingFeatures::variadicFunctions());
154-
return false;
155-
}
155+
bool isVariadic() const { return Required.allowsOptionalArgs(); }
156156
unsigned getNumRequiredArgs() const {
157-
if (isVariadic())
158-
cir_cconv_unreachable("NYI");
159-
return arg_size();
157+
return isVariadic() ? Required.getNumRequiredArgs() : arg_size();
160158
}
161159

162160
Type getReturnType() const { return getArgsBuffer()[0].type; }
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -fclangir-call-conv-lowering -emit-cir-flat -mmlir --mlir-print-ir-after=cir-call-conv-lowering %s -o %t.cir
2+
3+
int printf(const char *str, ...);
4+
5+
// CHECK: cir.func {{.*@bar}}
6+
// CHECK: %[[#V1:]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
7+
// CHECK: %[[#V2:]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
8+
// CHECK: cir.store %arg0, %[[#V0]] : !s32i, !cir.ptr<!s32i>
9+
// CHECK: cir.store %arg1, %[[#V1]] : !s32i, !cir.ptr<!s32i>
10+
// CHECK: %[[#V2:]] = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 7>>
11+
// CHECK: %[[#V3:]] = cir.cast(array_to_ptrdecay, %[[#V2]] : !cir.ptr<!cir.array<!s8i x 7>>), !cir.ptr<!s8i>
12+
// CHECK: %[[#V4:]] = cir.load %[[#V1]] : !cir.ptr<!s32i>, !s32i
13+
// CHECK: %[[#V5:]] = cir.load %[[#V2]] : !cir.ptr<!s32i>, !s32i
14+
// CHECK: %[[#V6:]] = cir.call @printf(%[[#V3]], %[[#V4]], %[[#V5]]) : (!cir.ptr<!s8i>, !s32i, !s32i) -> !s32i
15+
void bar(int a, int b) {
16+
printf("%d %d\n", a, b);
17+
}

0 commit comments

Comments
 (0)