[llvm] 084b65f - [memprof] Only insert dynamic shadow load when needed

Teresa Johnson via llvm-commits llvm-commits at lists.llvm.org
Mon May 2 13:36:13 PDT 2022


Author: Teresa Johnson
Date: 2022-05-02T13:36:00-07:00
New Revision: 084b65f7dc3986f0ee584c1b449cfe0c5e751fb3

URL: https://github.com/llvm/llvm-project/commit/084b65f7dc3986f0ee584c1b449cfe0c5e751fb3
DIFF: https://github.com/llvm/llvm-project/commit/084b65f7dc3986f0ee584c1b449cfe0c5e751fb3.diff

LOG: [memprof] Only insert dynamic shadow load when needed

We don't need to insert a load of the dynamic shadow address unless there
are interesting memory accesses to profile.

Split out of D124703.

Differential Revision: https://reviews.llvm.org/D124797

Added: 
    llvm/test/Instrumentation/HeapProfiler/no-instrumentation.ll

Modified: 
    llvm/lib/Transforms/Instrumentation/MemProfiler.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp b/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
index 6d02ef808449d..780a446c9c2e8 100644
--- a/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
+++ b/llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
@@ -633,8 +633,6 @@ bool MemProfiler::instrumentFunction(Function &F) {
 
   initializeCallbacks(*F.getParent());
 
-  FunctionModified |= insertDynamicShadowAtFunctionEntry(F);
-
   SmallVector<Instruction *, 16> ToInstrument;
 
   // Fill the set of memory operations to instrument.
@@ -645,6 +643,15 @@ bool MemProfiler::instrumentFunction(Function &F) {
     }
   }
 
+  if (ToInstrument.empty()) {
+    LLVM_DEBUG(dbgs() << "MEMPROF done instrumenting: " << FunctionModified
+                      << " " << F << "\n");
+
+    return FunctionModified;
+  }
+
+  FunctionModified |= insertDynamicShadowAtFunctionEntry(F);
+
   int NumInstrumented = 0;
   for (auto *Inst : ToInstrument) {
     if (ClDebugMin < 0 || ClDebugMax < 0 ||

diff  --git a/llvm/test/Instrumentation/HeapProfiler/no-instrumentation.ll b/llvm/test/Instrumentation/HeapProfiler/no-instrumentation.ll
new file mode 100644
index 0000000000000..c05c013814361
--- /dev/null
+++ b/llvm/test/Instrumentation/HeapProfiler/no-instrumentation.ll
@@ -0,0 +1,23 @@
+;; Test that we don't add any instrumentation code to functions without
+;; interesting memory accesses.
+;
+; RUN: opt < %s -passes='function(memprof),module(memprof-module)' -S -debug 2>&1 | FileCheck %s
+
+;; Require asserts for -debug
+; REQUIRES: asserts
+
+target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128-n8:16:32:64"
+target triple = "x86_64-unknown-linux-gnu"
+
+define void @_Z3foov() {
+entry:
+  ret void
+}
+
+;; Confirm we ran memprof and decided not to instrument
+; CHECK: MEMPROF done instrumenting: 0 define void @_Z3foov
+
+;; We should not add any instrumentation related code
+; CHECK: define void @_Z3foov
+; CHECK-NEXT: entry:
+; CHECK-NEXT:  ret void


        


More information about the llvm-commits mailing list