Skip to content

[CIR][ABI][Lowering] Supports call by function pointer in the calling convention lowering pass #1034

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

Merged
merged 3 commits into from
Oct 31, 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
2 changes: 1 addition & 1 deletion clang/lib/CIR/Dialect/Transforms/CallConvLowering.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ struct CallConvLowering {
rewriter.setInsertionPoint(op);
auto typ = op.getIndirectCall().getType();
if (isFuncPointerTy(typ)) {
cir_cconv_unreachable("Indirect calls NYI");
lowerModule->rewriteFunctionCall(op);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,16 @@ LogicalResult LowerFunction::rewriteCallOp(CallOp op,
// NOTE(cir): There is no direct way to fetch the function type from the
// CallOp, so we fetch it from the source function. This assumes the
// function definition has not yet been lowered.
cir_cconv_assert(SrcFn && "No source function");
auto fnType = SrcFn.getFunctionType();

FuncType fnType;
if (SrcFn) {
fnType = SrcFn.getFunctionType();
} else if (op.isIndirect()) {
if (auto ptrTy = dyn_cast<PointerType>(op.getIndirectCall().getType()))
fnType = dyn_cast<FuncType>(ptrTy.getPointee());
}

cir_cconv_assert(fnType && "No callee function type");

// Rewrite the call operation to abide to the ABI calling convention.
auto Ret = rewriteCallOp(fnType, SrcFn, op, retValSlot);
Expand Down Expand Up @@ -641,7 +649,7 @@ Value LowerFunction::rewriteCallOp(FuncType calleeTy, FuncOp origCallee,
//
// Chain calls use this same code path to add the invisible chain parameter
// to the function type.
if (origCallee.getNoProto() || Chain) {
if ((origCallee && origCallee.getNoProto()) || Chain) {
cir_cconv_assert_or_abort(::cir::MissingFeatures::ABINoProtoFunctions(),
"NYI");
}
Expand Down Expand Up @@ -824,8 +832,21 @@ Value LowerFunction::rewriteCallOp(const LowerFunctionInfo &CallInfo,
// NOTE(cir): We don't know if the callee was already lowered, so we only
// fetch the name from the callee, while the return type is fetch from the
// lowering types manager.
CallOp newCallOp = rewriter.create<CallOp>(
loc, Caller.getCalleeAttr(), IRFuncTy.getReturnType(), IRCallArgs);

CallOp newCallOp;

if (Caller.isIndirect()) {
rewriter.setInsertionPoint(Caller);
auto val = Caller.getIndirectCall();
auto ptrTy = PointerType::get(val.getContext(), IRFuncTy);
auto callee =
rewriter.create<CastOp>(val.getLoc(), ptrTy, CastKind::bitcast, val);
newCallOp = rewriter.create<CallOp>(loc, callee, IRFuncTy, IRCallArgs);
} else {
newCallOp = rewriter.create<CallOp>(loc, Caller.getCalleeAttr(),
IRFuncTy.getReturnType(), IRCallArgs);
}

auto extraAttrs =
rewriter.getAttr<ExtraFuncAttributesAttr>(rewriter.getDictionaryAttr({}));
newCallOp->setAttr("extra_attrs", extraAttrs);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class LowerModule {
LogicalResult rewriteFunctionDefinition(FuncOp op);

// Rewrite CIR CallOp to match the target ABI.
LogicalResult rewriteFunctionCall(CallOp callOp, FuncOp funcOp);
LogicalResult rewriteFunctionCall(CallOp callOp, FuncOp funcOp = {});
};

std::unique_ptr<LowerModule> createLowerModule(ModuleOp module,
Expand Down
29 changes: 29 additions & 0 deletions clang/test/CIR/CallConvLowering/x86_64/fptrs.c
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir-flat -fclangir-call-conv-lowering %s -o - | FileCheck %s
// RUN: %clang_cc1 -triple x86_64-unknown-linux-gnu -fclangir -emit-llvm -fclangir-call-conv-lowering %s -o -| FileCheck %s -check-prefix=LLVM

typedef struct {
int a;
Expand All @@ -16,3 +17,31 @@ int foo(S s) { return 42 + s.a; }
void bar() {
myfptr a = foo;
}

// CHECK: cir.func {{.*@baz}}(%arg0: !s32i
// CHECK: %[[#V0:]] = cir.alloca !ty_S, !cir.ptr<!ty_S>, [""] {alignment = 4 : i64}
// CHECK: %[[#V1:]] = cir.cast(bitcast, %[[#V0]] : !cir.ptr<!ty_S>), !cir.ptr<!s32i>
// CHECK: cir.store %arg0, %[[#V1]] : !s32i, !cir.ptr<!s32i>
// CHECK: %[[#V2:]] = cir.alloca !cir.ptr<!cir.func<!s32i (!ty_S)>>, !cir.ptr<!cir.ptr<!cir.func<!s32i (!ty_S)>>>, ["a", init]
// CHECK: %[[#V3:]] = cir.get_global @foo : !cir.ptr<!cir.func<!s32i (!s32i)>>
// CHECK: %[[#V4:]] = cir.cast(bitcast, %[[#V3]] : !cir.ptr<!cir.func<!s32i (!s32i)>>), !cir.ptr<!cir.func<!s32i (!ty_S)>>
// CHECK: cir.store %[[#V4]], %[[#V2]] : !cir.ptr<!cir.func<!s32i (!ty_S)>>, !cir.ptr<!cir.ptr<!cir.func<!s32i (!ty_S)>>>
// CHECK: %[[#V5:]] = cir.load %[[#V2]] : !cir.ptr<!cir.ptr<!cir.func<!s32i (!ty_S)>>>, !cir.ptr<!cir.func<!s32i (!ty_S)>>
// CHECK: %[[#V6:]] = cir.cast(bitcast, %[[#V0]] : !cir.ptr<!ty_S>), !cir.ptr<!s32i>
// CHECK: %[[#V7:]] = cir.load %[[#V6]] : !cir.ptr<!s32i>, !s32i
// CHECK: %[[#V8:]] = cir.cast(bitcast, %[[#V5]] : !cir.ptr<!cir.func<!s32i (!ty_S)>>), !cir.ptr<!cir.func<!s32i (!s32i)>>
// CHECK: %[[#V9:]] = cir.call %[[#V8]](%[[#V7]]) : (!cir.ptr<!cir.func<!s32i (!s32i)>>, !s32i) -> !s32i

// LLVM: define dso_local void @baz(i32 %0)
// LLVM: %[[#V1:]] = alloca %struct.S, i64 1
// LLVM: store i32 %0, ptr %[[#V1]]
// LLVM: %[[#V2:]] = alloca ptr, i64 1
// LLVM: store ptr @foo, ptr %[[#V2]]
// LLVM: %[[#V3:]] = load ptr, ptr %[[#V2]]
// LLVM: %[[#V4:]] = load i32, ptr %[[#V1]]
// LLVM: %[[#V5:]] = call i32 %[[#V3]](i32 %[[#V4]])

void baz(S s) {
myfptr a = foo;
a(s);
}