@@ -630,8 +630,16 @@ LogicalResult LowerFunction::rewriteCallOp(CallOp op,
630
630
// NOTE(cir): There is no direct way to fetch the function type from the
631
631
// CallOp, so we fetch it from the source function. This assumes the
632
632
// function definition has not yet been lowered.
633
- cir_cconv_assert (SrcFn && " No source function" );
634
- auto fnType = SrcFn.getFunctionType ();
633
+
634
+ FuncType fnType;
635
+ if (SrcFn) {
636
+ fnType = SrcFn.getFunctionType ();
637
+ } else if (op.isIndirect ()) {
638
+ if (auto ptrTy = dyn_cast<PointerType>(op.getIndirectCall ().getType ()))
639
+ fnType = dyn_cast<FuncType>(ptrTy.getPointee ());
640
+ }
641
+
642
+ cir_cconv_assert (fnType && " No callee function type" );
635
643
636
644
// Rewrite the call operation to abide to the ABI calling convention.
637
645
auto Ret = rewriteCallOp (fnType, SrcFn, op, retValSlot);
@@ -687,7 +695,7 @@ Value LowerFunction::rewriteCallOp(FuncType calleeTy, FuncOp origCallee,
687
695
//
688
696
// Chain calls use this same code path to add the invisible chain parameter
689
697
// to the function type.
690
- if (origCallee.getNoProto () || Chain) {
698
+ if (( origCallee && origCallee .getNoProto () ) || Chain) {
691
699
cir_cconv_assert_or_abort (::cir::MissingFeatures::ABINoProtoFunctions (),
692
700
" NYI" );
693
701
}
@@ -870,8 +878,21 @@ Value LowerFunction::rewriteCallOp(const LowerFunctionInfo &CallInfo,
870
878
// NOTE(cir): We don't know if the callee was already lowered, so we only
871
879
// fetch the name from the callee, while the return type is fetch from the
872
880
// lowering types manager.
873
- CallOp newCallOp = rewriter.create <CallOp>(
874
- loc, Caller.getCalleeAttr (), IRFuncTy.getReturnType (), IRCallArgs);
881
+
882
+ CallOp newCallOp;
883
+
884
+ if (Caller.isIndirect ()) {
885
+ rewriter.setInsertionPoint (Caller);
886
+ auto val = Caller.getIndirectCall ();
887
+ auto ptrTy = PointerType::get (val.getContext (), IRFuncTy);
888
+ auto callee =
889
+ rewriter.create <CastOp>(val.getLoc (), ptrTy, CastKind::bitcast, val);
890
+ newCallOp = rewriter.create <CallOp>(loc, callee, IRFuncTy, IRCallArgs);
891
+ } else {
892
+ newCallOp = rewriter.create <CallOp>(loc, Caller.getCalleeAttr (),
893
+ IRFuncTy.getReturnType (), IRCallArgs);
894
+ }
895
+
875
896
auto extraAttrs =
876
897
rewriter.getAttr <ExtraFuncAttributesAttr>(rewriter.getDictionaryAttr ({}));
877
898
newCallOp->setAttr (" extra_attrs" , extraAttrs);
0 commit comments