[PATCH] D128999: [CGSCC] Don't count calls to intrinsic functions in finding potential deviritualizations.

Rong Xu via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 1 09:48:03 PDT 2022


xur created this revision.
xur added a reviewer: tejohnson.
Herald added subscribers: wenlei, hiraditya.
Herald added a project: All.
xur requested review of this revision.
Herald added a project: LLVM.

This patch exclude the direct calls to intrinsic functions that cannot
be an indirect-call target in the heuristics. The debug intrinsics make
the algorithm unstable for builds with and without "-g" option.

We found this issue in the CSPGO compilation where we only use -g
in the CSPGO optimized build (but not in CSPGO instrumentation build).
This issue leads to some profile hash-mismatch.

Here is an example to show the problem.

code snippet
------------

extern int (*goo)();
static int bar(int n, int n2, int n3) { return n*n + n2 + n3; }
int foo(int sum) {

  int n = bar(2, 0, 0);
  if (n != 4) sum += goo();
  sum += bar(sum, sum, sum);
  return sum;

}

end of snippet
--------------

// Compile with: clang -O2.
// Without -g:
//   before CGSCC indirect_call=1 direct_call=2
//   after CGSCC round1:  indirect_call=0 direct_call=0 ==> stopped
// With -g:
//   before CGSCC indirect_call=1 direct_call=7
//   after CGSCC round1:  indirect_call=0 direct_call=9 ==> run round2
//   after CGSCC round2:  indirect_call=0 direct_call=9 ==> stopped

The above simple example generates the same IR with extra round of CGSCC
pass. But it's not hard to notice that they can easily leads to
different CodeGen in the real world program, not to mentioning the extra
compile time.

With this patch, -g will have the same behavior as without -g.


https://reviews.llvm.org/D128999

Files:
  llvm/lib/Analysis/CGSCCPassManager.cpp


Index: llvm/lib/Analysis/CGSCCPassManager.cpp
===================================================================
--- llvm/lib/Analysis/CGSCCPassManager.cpp
+++ llvm/lib/Analysis/CGSCCPassManager.cpp
@@ -19,6 +19,7 @@
 #include "llvm/IR/Constant.h"
 #include "llvm/IR/InstIterator.h"
 #include "llvm/IR/Instruction.h"
+#include "llvm/IR/IntrinsicInst.h"
 #include "llvm/IR/PassManager.h"
 #include "llvm/IR/PassManagerImpl.h"
 #include "llvm/IR/ValueHandle.h"
@@ -392,6 +393,19 @@
               .first->second;
       for (Instruction &I : instructions(N.getFunction()))
         if (auto *CB = dyn_cast<CallBase>(&I)) {
+          // Filter out the intrinsic functions that cannot be the
+          // indirect-call target, especially those DbgInfoInstrinsic.
+          // This avoids different compilation behavior for the same source
+          // with and without -g option.
+          if (auto *II = dyn_cast<IntrinsicInst>(&I)) {
+            if (II->isAssumeLikeIntrinsic()) {
+              continue;
+            }
+            Intrinsic::ID ID = II->getIntrinsicID();
+            if (isDbgInfoIntrinsic(ID)) {
+              continue;
+            }
+          }
           if (CB->getCalledFunction()) {
             ++Count.Direct;
           } else {


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D128999.441712.patch
Type: text/x-patch
Size: 1267 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220701/0901fee6/attachment.bin>


More information about the llvm-commits mailing list