Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][Lowering] Supports varargs in the CallingConvention pass #1005

Merged
merged 5 commits into from
Oct 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -47,8 +47,13 @@ arrangeFreeFunctionLikeCall(LowerTypes &LT, LowerModule &LM,

cir_cconv_assert(!::cir::MissingFeatures::chainCall() && !chainCall && "NYI");
FnInfoOpts opts = chainCall ? FnInfoOpts::IsChainCall : FnInfoOpts::None;
return LT.arrangeLLVMFunctionInfo(fnType.getReturnType(), opts,
fnType.getInputs(), required);

SmallVector<Type> argTypes;
for (const auto &a : args)
argTypes.push_back(a.getType());

return LT.arrangeLLVMFunctionInfo(fnType.getReturnType(), opts, argTypes,
required);
}

/// Adds the formal parameters in FPT to the given prefix. If any parameter in
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,15 @@ class RequiredArgs {
if (!prototype.isVarArg())
return All;

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

bool allowsOptionalArgs() const { return NumRequired != ~0U; }

unsigned getNumRequiredArgs() const {
assert(allowsOptionalArgs());
return NumRequired;
}
};

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

unsigned arg_size() const { return NumArgs; }

bool isVariadic() const {
cir_cconv_assert(!::cir::MissingFeatures::variadicFunctions());
return false;
}
bool isVariadic() const { return Required.allowsOptionalArgs(); }
unsigned getNumRequiredArgs() const {
if (isVariadic())
cir_cconv_unreachable("NYI");
return arg_size();
return isVariadic() ? Required.getNumRequiredArgs() : arg_size();
}

Type getReturnType() const { return getArgsBuffer()[0].type; }
Expand Down
17 changes: 17 additions & 0 deletions clang/test/CIR/CallConvLowering/x86_64/varargs.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// 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

int printf(const char *str, ...);

// CHECK: cir.func {{.*@bar}}
// CHECK: %[[#V1:]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a", init]
// CHECK: %[[#V2:]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b", init]
// CHECK: cir.store %arg0, %[[#V0]] : !s32i, !cir.ptr<!s32i>
// CHECK: cir.store %arg1, %[[#V1]] : !s32i, !cir.ptr<!s32i>
// CHECK: %[[#V2:]] = cir.get_global @".str" : !cir.ptr<!cir.array<!s8i x 7>>
// CHECK: %[[#V3:]] = cir.cast(array_to_ptrdecay, %[[#V2]] : !cir.ptr<!cir.array<!s8i x 7>>), !cir.ptr<!s8i>
// CHECK: %[[#V4:]] = cir.load %[[#V1]] : !cir.ptr<!s32i>, !s32i
// CHECK: %[[#V5:]] = cir.load %[[#V2]] : !cir.ptr<!s32i>, !s32i
// CHECK: %[[#V6:]] = cir.call @printf(%[[#V3]], %[[#V4]], %[[#V5]]) : (!cir.ptr<!s8i>, !s32i, !s32i) -> !s32i
void bar(int a, int b) {
printf("%d %d\n", a, b);
}