[llvm] [GISel] Add debug counter to force sdag fallback (PR #78257)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Jan 16 05:27:00 PST 2024


https://github.com/nikic updated https://github.com/llvm/llvm-project/pull/78257

>From 676c7ac579e0a6659f90711c50c6313b725cafb2 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 16 Jan 2024 12:14:24 +0100
Subject: [PATCH 1/2] [GISel] Add debug counter to force sdag fallback

Add a debug counter that allows forcing an sdag fallback after
a certain number of functions.

The intended use-case is to bisect which function gets miscompiled
by global isel using `-debug-counter=globalisel-count=N` (in cases
where sdag doesn't also miscompile it, of course).

The "falling back" debug line is printed unconditionally, because
irtranslator is extremely spammy, so using LLVM_DEBUG is not
helpful.
---
 llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp  | 10 ++++++++
 .../AArch64/GlobalISel/counter-fallback.ll    | 25 +++++++++++++++++++
 2 files changed, 35 insertions(+)
 create mode 100644 llvm/test/CodeGen/AArch64/GlobalISel/counter-fallback.ll

diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
index 662de0f3fe0e5e8..632bb9eb7360a0c 100644
--- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -76,6 +76,7 @@
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/DebugCounter.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
@@ -96,6 +97,9 @@
 
 using namespace llvm;
 
+DEBUG_COUNTER(GlobalISelCounter, "globalisel",
+              "Controls whether to select function with GlobalISel");
+
 static cl::opt<bool>
     EnableCSEInIRTranslator("enable-cse-in-irtranslator",
                             cl::desc("Should enable CSE in irtranslator"),
@@ -3701,6 +3705,12 @@ bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
   // Make our arguments/constants entry block fallthrough to the IR entry block.
   EntryBB->addSuccessor(&getMBB(F.front()));
 
+  if (!DebugCounter::shouldExecute(GlobalISelCounter)) {
+    dbgs() << "Falling back for function " << CurMF.getName() << "\n";
+    CurMF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
+    return false;
+  }
+
   if (CLI->fallBackToDAGISel(*MF)) {
     OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
                                F.getSubprogram(), &F.getEntryBlock());
diff --git a/llvm/test/CodeGen/AArch64/GlobalISel/counter-fallback.ll b/llvm/test/CodeGen/AArch64/GlobalISel/counter-fallback.ll
new file mode 100644
index 000000000000000..9a39b5a2ad7a927
--- /dev/null
+++ b/llvm/test/CodeGen/AArch64/GlobalISel/counter-fallback.ll
@@ -0,0 +1,25 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py UTC_ARGS: --version 4
+; RUN: llc -mtriple=aarch64-- -global-isel -global-isel-abort=0 -debug-counter=globalisel-count=1 %s -o - 2>/dev/null | FileCheck %s
+; RUN: llc -mtriple=aarch64-- -global-isel -global-isel-abort=0 -debug-counter=globalisel-count=1 %s -o /dev/null 2>&1 | FileCheck %s --check-prefix=DEBUG
+
+; REQUIRES: asserts
+
+; DEBUG-NOT: Falling back for function test1
+; DEBUG: Falling back for function test2
+
+define i32 @test1(i32 %x) {
+; CHECK-LABEL: test1:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    ret
+  ret i32 %x
+}
+
+define i32 @test2(i32 %x) {
+; CHECK-LABEL: test2:
+; CHECK:       // %bb.0:
+; CHECK-NEXT:    ret
+  ret i32 %x
+}
+
+;; NOTE: These prefixes are unused and the list is autogenerated. Do not add tests below this line:
+; DEBUG: {{.*}}

>From 917e6902fce83179fd7d4902240715a6fb52c202 Mon Sep 17 00:00:00 2001
From: Nikita Popov <npopov at redhat.com>
Date: Tue, 16 Jan 2024 14:26:36 +0100
Subject: [PATCH 2/2] Move counter from IRTranslator to InstructionSelect

---
 llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp      | 10 ----------
 llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp | 11 +++++++++++
 2 files changed, 11 insertions(+), 10 deletions(-)

diff --git a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
index 632bb9eb7360a0c..662de0f3fe0e5e8 100644
--- a/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/IRTranslator.cpp
@@ -76,7 +76,6 @@
 #include "llvm/Support/Casting.h"
 #include "llvm/Support/CodeGen.h"
 #include "llvm/Support/Debug.h"
-#include "llvm/Support/DebugCounter.h"
 #include "llvm/Support/ErrorHandling.h"
 #include "llvm/Support/MathExtras.h"
 #include "llvm/Support/raw_ostream.h"
@@ -97,9 +96,6 @@
 
 using namespace llvm;
 
-DEBUG_COUNTER(GlobalISelCounter, "globalisel",
-              "Controls whether to select function with GlobalISel");
-
 static cl::opt<bool>
     EnableCSEInIRTranslator("enable-cse-in-irtranslator",
                             cl::desc("Should enable CSE in irtranslator"),
@@ -3705,12 +3701,6 @@ bool IRTranslator::runOnMachineFunction(MachineFunction &CurMF) {
   // Make our arguments/constants entry block fallthrough to the IR entry block.
   EntryBB->addSuccessor(&getMBB(F.front()));
 
-  if (!DebugCounter::shouldExecute(GlobalISelCounter)) {
-    dbgs() << "Falling back for function " << CurMF.getName() << "\n";
-    CurMF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
-    return false;
-  }
-
   if (CLI->fallBackToDAGISel(*MF)) {
     OptimizationRemarkMissed R("gisel-irtranslator", "GISelFailure",
                                F.getSubprogram(), &F.getEntryBlock());
diff --git a/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp b/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp
index baea773cf528e92..30b2430249d2330 100644
--- a/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp
+++ b/llvm/lib/CodeGen/GlobalISel/InstructionSelect.cpp
@@ -31,12 +31,16 @@
 #include "llvm/Support/CodeGenCoverage.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
+#include "llvm/Support/DebugCounter.h"
 #include "llvm/Target/TargetMachine.h"
 
 #define DEBUG_TYPE "instruction-select"
 
 using namespace llvm;
 
+DEBUG_COUNTER(GlobalISelCounter, "globalisel",
+              "Controls whether to select function with GlobalISel");
+
 #ifdef LLVM_GISEL_COV_PREFIX
 static cl::opt<std::string>
     CoveragePrefix("gisel-coverage-prefix", cl::init(LLVM_GISEL_COV_PREFIX),
@@ -294,6 +298,13 @@ bool InstructionSelect::runOnMachineFunction(MachineFunction &MF) {
     return false;
   }
 #endif
+
+  if (!DebugCounter::shouldExecute(GlobalISelCounter)) {
+    dbgs() << "Falling back for function " << MF.getName() << "\n";
+    MF.getProperties().set(MachineFunctionProperties::Property::FailedISel);
+    return false;
+  }
+
   // Determine if there are any calls in this machine function. Ported from
   // SelectionDAG.
   MachineFrameInfo &MFI = MF.getFrameInfo();



More information about the llvm-commits mailing list