[llvm] 8d5c8d5 - [InlineCost] Check that function types match

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Apr 12 02:05:42 PDT 2022


Author: Nikita Popov
Date: 2022-04-12T11:05:33+02:00
New Revision: 8d5c8d57c637d898094af323d1888ea5a3364f8c

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

LOG: [InlineCost] Check that function types match

Retain the behavior we get without opaque pointers: A call to a
known function with different function type is considered an
indirect call.

This fixes the crash reported in https://reviews.llvm.org/D123300#3444772.

Added: 
    llvm/test/Transforms/Inline/opaque-ptr.ll

Modified: 
    llvm/lib/Analysis/InlineCost.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Analysis/InlineCost.cpp b/llvm/lib/Analysis/InlineCost.cpp
index dd404fcacaaf1..033ca9a8ecdec 100644
--- a/llvm/lib/Analysis/InlineCost.cpp
+++ b/llvm/lib/Analysis/InlineCost.cpp
@@ -2136,14 +2136,14 @@ bool CallAnalyzer::visitCallBase(CallBase &Call) {
   if (isa<CallInst>(Call) && cast<CallInst>(Call).cannotDuplicate())
     ContainsNoDuplicateCall = true;
 
-  Value *Callee = Call.getCalledOperand();
-  Function *F = dyn_cast_or_null<Function>(Callee);
+  Function *F = Call.getCalledFunction();
   bool IsIndirectCall = !F;
   if (IsIndirectCall) {
     // Check if this happens to be an indirect function call to a known function
     // in this inline context. If not, we've done all we can.
+    Value *Callee = Call.getCalledOperand();
     F = dyn_cast_or_null<Function>(SimplifiedValues.lookup(Callee));
-    if (!F) {
+    if (!F || F->getFunctionType() != Call.getFunctionType()) {
       onCallArgumentSetup(Call);
 
       if (!Call.onlyReadsMemory())

diff  --git a/llvm/test/Transforms/Inline/opaque-ptr.ll b/llvm/test/Transforms/Inline/opaque-ptr.ll
new file mode 100644
index 0000000000000..2d22cacb18020
--- /dev/null
+++ b/llvm/test/Transforms/Inline/opaque-ptr.ll
@@ -0,0 +1,27 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
+; RUN: opt -S -inline < %s | FileCheck %s
+
+define void @test(ptr %p1, ptr %p2) {
+; CHECK-LABEL: @test(
+; CHECK-NEXT:    ret void
+;
+  ret void
+}
+
+define void @test1() {
+; CHECK-LABEL: @test1(
+; CHECK-NEXT:    [[CALL_I:%.*]] = call i32 @test(ptr null)
+; CHECK-NEXT:    ret void
+;
+  call void @test2(ptr @test)
+  ret void
+}
+
+define void @test2(ptr %i) {
+; CHECK-LABEL: @test2(
+; CHECK-NEXT:    [[CALL:%.*]] = call i32 [[I:%.*]](ptr null)
+; CHECK-NEXT:    ret void
+;
+  %call = call i32 %i(ptr null)
+  ret void
+}


        


More information about the llvm-commits mailing list