Skip to content

Commit

Permalink
[memprof] Only insert dynamic shadow load when needed
Browse files Browse the repository at this point in the history
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
  • Loading branch information
teresajohnson committed May 2, 2022
1 parent b26e44e commit 084b65f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 2 deletions.
11 changes: 9 additions & 2 deletions llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
Expand Up @@ -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.
Expand All @@ -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 ||
Expand Down
23 changes: 23 additions & 0 deletions 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

0 comments on commit 084b65f

Please sign in to comment.