[llvm] [Instrumentation] Support verifying machine function (PR #90931)
via llvm-commits
llvm-commits at lists.llvm.org
Fri May 3 17:36:46 PDT 2024
https://github.com/paperchalice updated https://github.com/llvm/llvm-project/pull/90931
>From 069bd6ee12e89e8b367aa95ade62a3534ff86f5c Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Fri, 3 May 2024 10:04:28 +0800
Subject: [PATCH 1/4] [Instrumentation] Support verifying machine function
---
llvm/lib/Passes/StandardInstrumentations.cpp | 6 ++++++
llvm/test/tools/llc/new-pm/verify.mir | 10 ++++++++++
llvm/tools/llc/NewPMDriver.cpp | 2 +-
3 files changed, 17 insertions(+), 1 deletion(-)
create mode 100644 llvm/test/tools/llc/new-pm/verify.mir
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 63490c83e85f05..1d6c464c80b1e4 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -1486,6 +1486,12 @@ void VerifyInstrumentation::registerCallbacks(
"\"{0}\", compilation aborted!",
P));
}
+
+ // TODO: Use complete MachineVerifierPass.
+ if (auto *MF = unwrapIR<MachineFunction>(IR)) {
+ dbgs() << "Verifying machine function " << MF->getName() << '\n';
+ verifyMachineFunction("", *MF);
+ }
}
});
}
diff --git a/llvm/test/tools/llc/new-pm/verify.mir b/llvm/test/tools/llc/new-pm/verify.mir
new file mode 100644
index 00000000000000..71c25b14d7c858
--- /dev/null
+++ b/llvm/test/tools/llc/new-pm/verify.mir
@@ -0,0 +1,10 @@
+# RUN: llc -mtriple=x86_64-pc-linux-gnu -x mir -passes=no-op-machine-function -filetype=null < %s 2>&1 | FileCheck %s
+
+# CHECK: Verifying machine function f
+
+---
+name: f
+body: |
+ bb.0:
+ RET 0
+...
diff --git a/llvm/tools/llc/NewPMDriver.cpp b/llvm/tools/llc/NewPMDriver.cpp
index 6d9956ea07d356..fb1959c6457f4a 100644
--- a/llvm/tools/llc/NewPMDriver.cpp
+++ b/llvm/tools/llc/NewPMDriver.cpp
@@ -115,7 +115,7 @@ int llvm::compileModuleWithNewPM(
MachineModuleInfo MMI(&LLVMTM);
PassInstrumentationCallbacks PIC;
- StandardInstrumentations SI(Context, Opt.DebugPM);
+ StandardInstrumentations SI(Context, Opt.DebugPM, !NoVerify);
SI.registerCallbacks(PIC);
registerCodeGenCallback(PIC, LLVMTM);
>From a1b8be3808f64ac6b1d0cd4b67c12ac06c555b56 Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Fri, 3 May 2024 12:44:41 +0800
Subject: [PATCH 2/4] print "Verifying machine function" in when debug logging
is true
---
llvm/lib/Passes/StandardInstrumentations.cpp | 8 ++++++--
1 file changed, 6 insertions(+), 2 deletions(-)
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 1d6c464c80b1e4..3fadad3ac90c2a 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -1489,8 +1489,12 @@ void VerifyInstrumentation::registerCallbacks(
// TODO: Use complete MachineVerifierPass.
if (auto *MF = unwrapIR<MachineFunction>(IR)) {
- dbgs() << "Verifying machine function " << MF->getName() << '\n';
- verifyMachineFunction("", *MF);
+ if (DebugLogging)
+ dbgs() << "Verifying machine function " << MF->getName() << '\n';
+ verifyMachineFunction(formatv("Broken module found after pass "
+ "\"{0}\", compilation aborted!",
+ P),
+ *MF);
}
}
});
>From 237b57f4dfb3fa306692fc9fc8820026b32506bb Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Fri, 3 May 2024 14:33:30 +0800
Subject: [PATCH 3/4] extend TriggerVerifierErrorPass
---
llvm/include/llvm/Passes/MachinePassRegistry.def | 1 +
llvm/lib/Passes/PassBuilder.cpp | 9 +++++++++
llvm/lib/Passes/StandardInstrumentations.cpp | 9 +++++----
llvm/test/CodeGen/MIR/X86/machine-verifier.mir | 2 +-
llvm/test/tools/llc/new-pm/verify.mir | 4 ++--
5 files changed, 18 insertions(+), 7 deletions(-)
diff --git a/llvm/include/llvm/Passes/MachinePassRegistry.def b/llvm/include/llvm/Passes/MachinePassRegistry.def
index 5a14d619ea076f..380bffe75b3099 100644
--- a/llvm/include/llvm/Passes/MachinePassRegistry.def
+++ b/llvm/include/llvm/Passes/MachinePassRegistry.def
@@ -128,6 +128,7 @@ MACHINE_FUNCTION_PASS("no-op-machine-function", NoOpMachineFunctionPass())
MACHINE_FUNCTION_PASS("print", PrintMIRPass())
MACHINE_FUNCTION_PASS("require-all-machine-function-properties",
RequireAllMachineFunctionPropertiesPass())
+MACHINE_FUNCTION_PASS("trigger-verifier-error", TriggerVerifierErrorPass())
#undef MACHINE_FUNCTION_PASS
// After a pass is converted to new pass manager, its entry should be moved from
diff --git a/llvm/lib/Passes/PassBuilder.cpp b/llvm/lib/Passes/PassBuilder.cpp
index 6f69dfce91b336..51ddb73943b10d 100644
--- a/llvm/lib/Passes/PassBuilder.cpp
+++ b/llvm/lib/Passes/PassBuilder.cpp
@@ -93,6 +93,7 @@
#include "llvm/CodeGen/MIRPrinter.h"
#include "llvm/CodeGen/MachineFunctionAnalysis.h"
#include "llvm/CodeGen/MachinePassManager.h"
+#include "llvm/CodeGen/MachineRegisterInfo.h"
#include "llvm/CodeGen/PreISelIntrinsicLowering.h"
#include "llvm/CodeGen/SafeStack.h"
#include "llvm/CodeGen/SelectOptimize.h"
@@ -363,6 +364,14 @@ class TriggerVerifierErrorPass
return PreservedAnalyses::none();
}
+ PreservedAnalyses run(MachineFunction &MF, MachineFunctionAnalysisManager &) {
+ // Intentionally create a virtual register and set NoVRegs property.
+ auto &MRI = MF.getRegInfo();
+ MRI.createGenericVirtualRegister(LLT::scalar(8));
+ MF.getProperties().set(MachineFunctionProperties::Property::NoVRegs);
+ return PreservedAnalyses::all();
+ }
+
static StringRef name() { return "TriggerVerifierErrorPass"; }
};
diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 3fadad3ac90c2a..d57e06b3501197 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -1491,10 +1491,11 @@ void VerifyInstrumentation::registerCallbacks(
if (auto *MF = unwrapIR<MachineFunction>(IR)) {
if (DebugLogging)
dbgs() << "Verifying machine function " << MF->getName() << '\n';
- verifyMachineFunction(formatv("Broken module found after pass "
- "\"{0}\", compilation aborted!",
- P),
- *MF);
+ verifyMachineFunction(
+ formatv("Broken machine function found after pass "
+ "\"{0}\", compilation aborted!",
+ P),
+ *MF);
}
}
});
diff --git a/llvm/test/CodeGen/MIR/X86/machine-verifier.mir b/llvm/test/CodeGen/MIR/X86/machine-verifier.mir
index 5cf5e8f0adc92d..6966b3e6778e25 100644
--- a/llvm/test/CodeGen/MIR/X86/machine-verifier.mir
+++ b/llvm/test/CodeGen/MIR/X86/machine-verifier.mir
@@ -1,5 +1,5 @@
# RUN: not --crash llc -march=x86-64 -run-pass none -o /dev/null %s 2>&1 | FileCheck %s
-# This test ensures that the MIR parser runs the machine verifier after parsing.
+# This test ensures that the VerifyInstrumentation works for machine function.
--- |
diff --git a/llvm/test/tools/llc/new-pm/verify.mir b/llvm/test/tools/llc/new-pm/verify.mir
index 71c25b14d7c858..5c16664e988c01 100644
--- a/llvm/test/tools/llc/new-pm/verify.mir
+++ b/llvm/test/tools/llc/new-pm/verify.mir
@@ -1,7 +1,7 @@
-# RUN: llc -mtriple=x86_64-pc-linux-gnu -x mir -passes=no-op-machine-function -filetype=null < %s 2>&1 | FileCheck %s
+# RUN: not --crash llc -mtriple=x86_64-pc-linux-gnu -debug-pass-manager -passes='module(function(machine-function(trigger-verifier-error)))' -filetype=null %s 2>&1 | FileCheck %s
# CHECK: Verifying machine function f
-
+# CHECK: Broken machine function found after pass
---
name: f
body: |
>From be1e55df2aab0c5f75201f1f20a3afc342632a4b Mon Sep 17 00:00:00 2001
From: PaperChalice <liujunchang97 at outlook.com>
Date: Sat, 4 May 2024 08:36:30 +0800
Subject: [PATCH 4/4] add pass name
---
llvm/test/tools/llc/new-pm/verify.mir | 2 +-
1 file changed, 1 insertion(+), 1 deletion(-)
diff --git a/llvm/test/tools/llc/new-pm/verify.mir b/llvm/test/tools/llc/new-pm/verify.mir
index 5c16664e988c01..0cc7fc837e5bed 100644
--- a/llvm/test/tools/llc/new-pm/verify.mir
+++ b/llvm/test/tools/llc/new-pm/verify.mir
@@ -1,7 +1,7 @@
# RUN: not --crash llc -mtriple=x86_64-pc-linux-gnu -debug-pass-manager -passes='module(function(machine-function(trigger-verifier-error)))' -filetype=null %s 2>&1 | FileCheck %s
# CHECK: Verifying machine function f
-# CHECK: Broken machine function found after pass
+# CHECK: Broken machine function found after pass "TriggerVerifierErrorPass"
---
name: f
body: |
More information about the llvm-commits
mailing list