[llvm] 16663d8 - IndirectCallPromotion: Fix illegal promotion with opaque pointers

Matt Arsenault via llvm-commits llvm-commits at lists.llvm.org
Sun Nov 27 06:39:47 PST 2022


Author: Matt Arsenault
Date: 2022-11-27T09:39:42-05:00
New Revision: 16663d8ab36b749b302afdc931244bb1951e0e1c

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

LOG: IndirectCallPromotion: Fix illegal promotion with opaque pointers

This was doing a type check on the argument types and skipping all
other safety checks if they matched.

Added: 
    llvm/test/Transforms/PGOProfile/mismatched-byval-opaque.ll
    llvm/test/Transforms/PGOProfile/mismatched-inalloca-opaque.ll

Modified: 
    llvm/lib/Transforms/Utils/CallPromotionUtils.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
index e530afc277db3..c352003b6564b 100644
--- a/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
+++ b/llvm/lib/Transforms/Utils/CallPromotionUtils.cpp
@@ -415,18 +415,8 @@ bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee,
   // site.
   unsigned I = 0;
   for (; I < NumParams; ++I) {
-    Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(I);
-    Type *ActualTy = CB.getArgOperand(I)->getType();
-    if (FormalTy == ActualTy)
-      continue;
-    if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) {
-      if (FailureReason)
-        *FailureReason = "Argument type mismatch";
-      return false;
-    }
     // Make sure that the callee and call agree on byval/inalloca. The types do
     // not have to match.
-
     if (Callee->hasParamAttribute(I, Attribute::ByVal) !=
         CB.getAttributes().hasParamAttr(I, Attribute::ByVal)) {
       if (FailureReason)
@@ -439,6 +429,16 @@ bool llvm::isLegalToPromote(const CallBase &CB, Function *Callee,
         *FailureReason = "inalloca mismatch";
       return false;
     }
+
+    Type *FormalTy = Callee->getFunctionType()->getFunctionParamType(I);
+    Type *ActualTy = CB.getArgOperand(I)->getType();
+    if (FormalTy == ActualTy)
+      continue;
+    if (!CastInst::isBitOrNoopPointerCastable(ActualTy, FormalTy, DL)) {
+      if (FailureReason)
+        *FailureReason = "Argument type mismatch";
+      return false;
+    }
   }
   for (; I < NumArgs; I++) {
     // Vararg functions can have more arguments than parameters.

diff  --git a/llvm/test/Transforms/PGOProfile/mismatched-byval-opaque.ll b/llvm/test/Transforms/PGOProfile/mismatched-byval-opaque.ll
new file mode 100644
index 0000000000000..fa3ffe3b24cfb
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/mismatched-byval-opaque.ll
@@ -0,0 +1,22 @@
+; RUN: opt -passes=pgo-icall-prom -profile-summary-hot-count=10 -S < %s -pass-remarks-output=- | FileCheck %s
+
+; CHECK: byval mismatch
+
+define void @a(ptr %0) !prof !0 {
+  ret void
+}
+
+define void @b(ptr %v, ptr %p) !prof !1 {
+; CHECK-LABEL: @b
+; CHECK-NEXT: load
+; CHECK-NEXT: call void {{.*}}(ptr byval(i64)
+; CHECK-NEXT: ret void
+;
+  %a = load ptr, ptr %v
+  call void %a(ptr byval(i64) %p), !prof !2
+  ret void
+}
+
+!0 = !{!"function_entry_count", i64 36}
+!1 = !{!"function_entry_count", i64 1}
+!2 = !{!"VP", i32 0, i64 18, i64 12157170054180749580, i64 18}

diff  --git a/llvm/test/Transforms/PGOProfile/mismatched-inalloca-opaque.ll b/llvm/test/Transforms/PGOProfile/mismatched-inalloca-opaque.ll
new file mode 100644
index 0000000000000..a403835bbe1f9
--- /dev/null
+++ b/llvm/test/Transforms/PGOProfile/mismatched-inalloca-opaque.ll
@@ -0,0 +1,21 @@
+; RUN: opt -passes=pgo-icall-prom -profile-summary-hot-count=10 -S < %s -pass-remarks-output=- | FileCheck %s
+
+; CHECK: inalloca mismatch
+
+define void @a(ptr %0) !prof !0 {
+  ret void
+}
+
+define void @b(ptr %v, ptr %p) !prof !1 {
+; CHECK-LABEL: @b
+; CHECK-NEXT: load
+; CHECK-NEXT: call void {{.*}}(ptr inalloca(i64)
+; CHECK-NEXT: ret void
+  %a = load ptr, ptr %v
+  call void %a(ptr inalloca(i64) %p), !prof !2
+  ret void
+}
+
+!0 = !{!"function_entry_count", i64 36}
+!1 = !{!"function_entry_count", i64 1}
+!2 = !{!"VP", i32 0, i64 18, i64 12157170054180749580, i64 18}


        


More information about the llvm-commits mailing list