[clang] cbdedfb - [CIR] Handle Indirect call in callee setter (#244751)

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 24 16:48:15 PST 2026


Author: ShengYi Hung
Date: 2026-11-25T08:48:09+08:00
New Revision: cbdedfbcb0ff793fca395430115a768948af75ce

URL: https://github.com/llvm/llvm-project/commit/cbdedfbcb0ff793fca395430115a768948af75ce
DIFF: https://github.com/llvm/llvm-project/commit/cbdedfbcb0ff793fca395430115a768948af75ce.diff

LOG: [CIR] Handle Indirect call in callee setter (#244751)

Indirect call from function pointer has no SymbolRefAttr, which cause
assetion after dyn_cast.

According to FIROps.cpp, four cases ({Direct, Indirect} x {Direct,
Indirect}) are handled separately. As we don't have arg_attr in indirect
call, we just prepend and discard attr when call type is chaged.

Assisted-by: Claude # Test

Added: 
    clang/test/CIR/Transforms/inline-indirect-call.cir

Modified: 
    clang/include/clang/CIR/Dialect/IR/CIROps.td
    clang/unittests/CIR/CallOpTest.cpp

Removed: 
    


################################################################################
diff  --git a/clang/include/clang/CIR/Dialect/IR/CIROps.td b/clang/include/clang/CIR/Dialect/IR/CIROps.td
index e15176740bde9d..f87a9c60e1ed2f 100644
--- a/clang/include/clang/CIR/Dialect/IR/CIROps.td
+++ b/clang/include/clang/CIR/Dialect/IR/CIROps.td
@@ -5104,11 +5104,7 @@ class CIR_CallOpBase<string mnemonic, list<Trait> extra_traits = []>
       return getOperand(0);
     }
 
-    /// Set the callee for this operation.
-    void setCalleeFromCallable(::mlir::CallInterfaceCallable callee) {
-      (*this)->setAttr(getCalleeAttrName(),
-                       mlir::cast<mlir::SymbolRefAttr>(callee));
-    }
+    void setCalleeFromCallable(::mlir::CallInterfaceCallable callee);
 
     /// Returns the `cir.func` being called or null if the call is indirect or
     /// cannot be resolved.
@@ -5128,6 +5124,25 @@ class CIR_CallOpBase<string mnemonic, list<Trait> extra_traits = []>
   let extraClassDefinition = [{
     bool $cppClass::isIndirect() { return !getCallee(); }
 
+    void $cppClass::setCalleeFromCallable(
+        ::mlir::CallInterfaceCallable callee) {
+      bool wasIndirect = isIndirect();
+      if (auto symbol = llvm::dyn_cast<mlir::SymbolRefAttr>(callee)) {
+        if (wasIndirect)
+          (*this)->eraseOperand(0);
+        (*this)->setAttr(getCalleeAttrName(), symbol);
+        return;
+      }
+
+      auto calleeValue = llvm::cast<mlir::Value>(callee);
+      if (wasIndirect) {
+        setOperand(0, calleeValue);
+        return;
+      }
+      (*this)->removeAttr(getCalleeAttrName());
+      (*this)->insertOperands(0, calleeValue);
+    }
+
     cir::FuncOp $cppClass::resolveCalleeInTable(
         mlir::SymbolTableCollection &symbolTable) {
       mlir::FlatSymbolRefAttr callee = getCalleeAttr();

diff  --git a/clang/test/CIR/Transforms/inline-indirect-call.cir b/clang/test/CIR/Transforms/inline-indirect-call.cir
new file mode 100644
index 00000000000000..8acd91c73b5145
--- /dev/null
+++ b/clang/test/CIR/Transforms/inline-indirect-call.cir
@@ -0,0 +1,32 @@
+// RUN: cir-opt %s -inline -o - | FileCheck %s
+
+// The inliner builds a call graph, which asks every call for its callee. An
+// indirect call has to answer with its function pointer operand, not with an
+// empty callable.
+
+!fty = !cir.func<(!cir.double) -> !cir.double>
+
+module {
+  cir.func @sq(%x: !cir.double) -> !cir.double {
+    %y = cir.fmul %x, %x : !cir.double
+    cir.return %y : !cir.double
+  }
+
+  cir.func @apply(%fp: !cir.ptr<!fty>, %x: !cir.double) -> !cir.double {
+    %r = cir.call %fp(%x) : (!cir.ptr<!fty>, !cir.double) -> !cir.double
+    cir.return %r : !cir.double
+  }
+
+  cir.func @g(%x: !cir.double) -> !cir.double {
+    %a = cir.call @sq(%x) : (!cir.double) -> !cir.double
+    cir.return %a : !cir.double
+  }
+}
+
+// CHECK-LABEL: cir.func @apply(
+// CHECK-SAME:    %[[FP:.*]]: !cir.ptr<!cir.func<(!cir.double) -> !cir.double>>, %[[X:.*]]: !cir.double)
+// CHECK:         %[[R:.*]] = cir.call %[[FP]](%[[X]])
+// CHECK:         cir.return %[[R]]
+
+// CHECK-LABEL: cir.func @g(
+// CHECK:         cir.call @sq(

diff  --git a/clang/unittests/CIR/CallOpTest.cpp b/clang/unittests/CIR/CallOpTest.cpp
index 96e2e000f09ff2..787ab60032eee1 100644
--- a/clang/unittests/CIR/CallOpTest.cpp
+++ b/clang/unittests/CIR/CallOpTest.cpp
@@ -10,6 +10,8 @@
 #include "mlir/IR/MLIRContext.h"
 #include "mlir/IR/OwningOpRef.h"
 #include "mlir/IR/SymbolTable.h"
+#include "mlir/IR/Verifier.h"
+#include "mlir/Interfaces/CallInterfaces.h"
 #include "mlir/Parser/Parser.h"
 #include "clang/CIR/Dialect/IR/CIRDialect.h"
 
@@ -94,4 +96,148 @@ TEST_F(CIRCallOpTest, ResolveCallee) {
   EXPECT_EQ(tryCalls[0].resolveCalleeInTable(symbolTable), marked);
 }
 
+// Functions with the same signature that calls can be retargeted between, and
+// one direct and one indirect call that each pass a single argument carrying
+// arg_attrs.
+constexpr const char *retargetModuleText = R"CIR(
+!s32i = !cir.int<s, 32>
+!fty = !cir.func<(!s32i)>
+
+module {
+  cir.func @target(%a: !s32i) {
+    cir.return
+  }
+  cir.func @other(%a: !s32i) {
+    cir.return
+  }
+  cir.func @caller(%fp0: !cir.ptr<!fty>, %fp1: !cir.ptr<!fty>, %x: !s32i) {
+    cir.call @target(%x) : (!s32i {cir.test = "arg0"}) -> ()
+    cir.call %fp0(%x) : (!cir.ptr<!fty>, !s32i {cir.test = "arg0"}) -> ()
+    cir.return
+  }
+  cir.func @try_caller(%fp0: !cir.ptr<!fty>, %x: !s32i) {
+    cir.try_call @target(%x) ^bb1, ^bb2 : (!s32i) -> ()
+  ^bb1:
+    cir.return
+  ^bb2:
+    cir.return
+  }
+}
+)CIR";
+
+class CIRCallOpRetargetTest : public CIRCallOpTest {
+protected:
+  void SetUp() override {
+    module = parse(retargetModuleText);
+    ASSERT_TRUE(module);
+    module->walk([&](cir::CallOp op) { calls.push_back(op); });
+    ASSERT_EQ(calls.size(), 2u);
+    auto caller = module->lookupSymbol<cir::FuncOp>("caller");
+    ASSERT_TRUE(caller);
+    fp0 = caller.getArgument(0);
+    fp1 = caller.getArgument(1);
+    x = caller.getArgument(2);
+  }
+
+  cir::CallOp directCall() { return calls[0]; }
+  cir::CallOp indirectCall() { return calls[1]; }
+
+  // The call still passes %x as its only argument, with its arg_attrs intact.
+  static void expectArgPreserved(cir::CallOp call, Value x) {
+    ASSERT_EQ(call.getNumArgOperands(), 1u);
+    EXPECT_EQ(call.getArgOperand(0), x);
+    ArrayAttr argAttrs = call.getArgAttrsAttr();
+    ASSERT_TRUE(argAttrs);
+    ASSERT_EQ(argAttrs.size(), 1u);
+    EXPECT_EQ(cast<DictionaryAttr>(argAttrs[0]).get("cir.test"),
+              StringAttr::get(call.getContext(), "arg0"));
+  }
+
+  static StringRef calleeName(CallOpInterface call) {
+    return cast<SymbolRefAttr>(call.getCallableForCallee())
+        .getRootReference()
+        .getValue();
+  }
+
+  OwningOpRef<ModuleOp> module;
+  SmallVector<cir::CallOp> calls;
+  Value fp0, fp1, x;
+};
+
+TEST_F(CIRCallOpRetargetTest, DirectToDirect) {
+  cir::CallOp call = directCall();
+  call.setCalleeFromCallable(FlatSymbolRefAttr::get(&context, "other"));
+
+  EXPECT_FALSE(call.isIndirect());
+  EXPECT_EQ(calleeName(call), "other");
+  EXPECT_EQ(call->getNumOperands(), 1u);
+  expectArgPreserved(call, x);
+  EXPECT_TRUE(succeeded(verify(*module)));
+}
+
+TEST_F(CIRCallOpRetargetTest, DirectToIndirect) {
+  cir::CallOp call = directCall();
+  call.setCalleeFromCallable(fp0);
+
+  EXPECT_TRUE(call.isIndirect());
+  EXPECT_FALSE(call.getCalleeAttr());
+  EXPECT_EQ(cast<Value>(call.getCallableForCallee()), fp0);
+  ASSERT_EQ(call->getNumOperands(), 2u);
+  EXPECT_EQ(call->getOperand(0), fp0);
+  expectArgPreserved(call, x);
+  EXPECT_TRUE(succeeded(verify(*module)));
+}
+
+TEST_F(CIRCallOpRetargetTest, IndirectToDirect) {
+  cir::CallOp call = indirectCall();
+  call.setCalleeFromCallable(FlatSymbolRefAttr::get(&context, "target"));
+
+  EXPECT_FALSE(call.isIndirect());
+  EXPECT_EQ(calleeName(call), "target");
+  ASSERT_EQ(call->getNumOperands(), 1u);
+  EXPECT_EQ(call->getOperand(0), x);
+  expectArgPreserved(call, x);
+  EXPECT_TRUE(succeeded(verify(*module)));
+}
+
+TEST_F(CIRCallOpRetargetTest, IndirectToIndirect) {
+  cir::CallOp call = indirectCall();
+  call.setCalleeFromCallable(fp1);
+
+  EXPECT_TRUE(call.isIndirect());
+  EXPECT_EQ(cast<Value>(call.getCallableForCallee()), fp1);
+  ASSERT_EQ(call->getNumOperands(), 2u);
+  EXPECT_EQ(call->getOperand(0), fp1);
+  expectArgPreserved(call, x);
+  EXPECT_TRUE(succeeded(verify(*module)));
+}
+
+// cir.try_call shares the implementation; its successors must be untouched.
+TEST_F(CIRCallOpRetargetTest, TryCallRoundTrip) {
+  SmallVector<cir::TryCallOp> tryCalls;
+  module->walk([&](cir::TryCallOp op) { tryCalls.push_back(op); });
+  ASSERT_EQ(tryCalls.size(), 1u);
+  cir::TryCallOp call = tryCalls[0];
+  Value tryFp = call->getParentOfType<cir::FuncOp>().getArgument(0);
+  Value tryX = call->getParentOfType<cir::FuncOp>().getArgument(1);
+  Block *normal = call.getNormalDest();
+  Block *unwind = call.getUnwindDest();
+
+  call.setCalleeFromCallable(tryFp);
+  EXPECT_TRUE(call.isIndirect());
+  ASSERT_EQ(call->getNumOperands(), 2u);
+  EXPECT_EQ(call->getOperand(0), tryFp);
+  EXPECT_EQ(call->getOperand(1), tryX);
+  EXPECT_TRUE(succeeded(verify(*module)));
+
+  call.setCalleeFromCallable(FlatSymbolRefAttr::get(&context, "other"));
+  EXPECT_FALSE(call.isIndirect());
+  EXPECT_EQ(calleeName(call), "other");
+  ASSERT_EQ(call->getNumOperands(), 1u);
+  EXPECT_EQ(call->getOperand(0), tryX);
+  EXPECT_EQ(call.getNormalDest(), normal);
+  EXPECT_EQ(call.getUnwindDest(), unwind);
+  EXPECT_TRUE(succeeded(verify(*module)));
+}
+
 } // namespace


        


More information about the cfe-commits mailing list