[llvm] [StandardInstrumentations] Ensure non-null module pointer when getting display name for IR file (PR #110779)

via llvm-commits llvm-commits at lists.llvm.org
Tue Oct 1 18:45:15 PDT 2024


https://github.com/duk-37 created https://github.com/llvm/llvm-project/pull/110779

Fixes a crash when using `-filter-print-funcs` with `-ir-dump-directory`. A quick reproducer on trunk (also included as a test):

```ll
; opt -passes=no-op-function -print-after=no-op-function -filter-print-funcs=nope -ir-dump-directory=somewhere test.ll

define void @test() {
    ret void
}
```

[Compiler Explorer](https://godbolt.org/z/sPErz44h4)

>From 2e3fe7cfc0a509e4036e7a16d638a48276053a59 Mon Sep 17 00:00:00 2001
From: duk <37 at cmail.nu>
Date: Tue, 1 Oct 2024 21:31:28 -0400
Subject: [PATCH] [StandardInstrumentations] Ensure non-null module pointer
 when getting display name for IR file

Fixes a crash when using -filter-print-funcs with -ir-dump-directory.
---
 llvm/lib/Passes/StandardInstrumentations.cpp |  3 ++-
 llvm/test/Other/dump-with-filter.ll          | 13 +++++++++++++
 2 files changed, 15 insertions(+), 1 deletion(-)
 create mode 100644 llvm/test/Other/dump-with-filter.ll

diff --git a/llvm/lib/Passes/StandardInstrumentations.cpp b/llvm/lib/Passes/StandardInstrumentations.cpp
index 036484c9c1c0c4..c960afcf6e9736 100644
--- a/llvm/lib/Passes/StandardInstrumentations.cpp
+++ b/llvm/lib/Passes/StandardInstrumentations.cpp
@@ -751,7 +751,8 @@ PrintIRInstrumentation::~PrintIRInstrumentation() {
 static SmallString<32> getIRFileDisplayName(Any IR) {
   SmallString<32> Result;
   raw_svector_ostream ResultStream(Result);
-  const Module *M = unwrapModule(IR);
+  const Module *M = unwrapModule(IR, /*Force=*/true);
+  assert(M && "should have unwrapped module");
   uint64_t NameHash = xxh3_64bits(M->getName());
   unsigned MaxHashWidth = sizeof(uint64_t) * 2;
   write_hex(ResultStream, NameHash, HexPrintStyle::Lower, MaxHashWidth);
diff --git a/llvm/test/Other/dump-with-filter.ll b/llvm/test/Other/dump-with-filter.ll
new file mode 100644
index 00000000000000..67b91ae4231675
--- /dev/null
+++ b/llvm/test/Other/dump-with-filter.ll
@@ -0,0 +1,13 @@
+;; Make sure we can run -filter-print-funcs with -ir-dump-directory.
+; RUN: opt %s -disable-output -passes='no-op-function' -print-before=no-op-function -print-after=no-op-function \
+; RUN:   -ir-dump-directory %t/logs -filter-print-funcs=test2
+; RUN: ls %t/logs | count 2
+; RUN: rm -rf %t/logs
+
+define void @test() {
+    ret void
+}
+
+define void @test2() {
+    ret void
+}



More information about the llvm-commits mailing list