[MemProf] Don't instrument stack accesses unless requested
authorTeresa Johnson <tejohnson@google.com>
Thu, 16 Sep 2021 05:06:07 +0000 (22:06 -0700)
committerTeresa Johnson <tejohnson@google.com>
Thu, 16 Sep 2021 19:21:51 +0000 (12:21 -0700)
Skip stack accesses unless requested, as the memory profiler runtime
does not currently look at or report accesses for these addresses.

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

llvm/lib/Transforms/Instrumentation/MemProfiler.cpp
llvm/test/Instrumentation/HeapProfiler/stack.ll [new file with mode: 0644]

index 610a76d..727672f 100644 (file)
@@ -19,6 +19,7 @@
 #include "llvm/ADT/Statistic.h"
 #include "llvm/ADT/StringRef.h"
 #include "llvm/ADT/Triple.h"
+#include "llvm/Analysis/ValueTracking.h"
 #include "llvm/IR/Constant.h"
 #include "llvm/IR/DataLayout.h"
 #include "llvm/IR/Function.h"
@@ -107,6 +108,10 @@ static cl::opt<int>
                          cl::desc("granularity of memprof shadow mapping"),
                          cl::Hidden, cl::init(DefaultShadowGranularity));
 
+static cl::opt<bool> ClStack("memprof-instrument-stack",
+                             cl::desc("Instrument scalar stack variables"),
+                             cl::Hidden, cl::init(false));
+
 // Debug flags.
 
 static cl::opt<int> ClDebug("memprof-debug", cl::desc("debug"), cl::Hidden,
@@ -123,6 +128,8 @@ static cl::opt<int> ClDebugMax("memprof-debug-max", cl::desc("Debug max inst"),
 
 STATISTIC(NumInstrumentedReads, "Number of instrumented reads");
 STATISTIC(NumInstrumentedWrites, "Number of instrumented writes");
+STATISTIC(NumSkippedStackReads, "Number of non-instrumented stack reads");
+STATISTIC(NumSkippedStackWrites, "Number of non-instrumented stack writes");
 
 namespace {
 
@@ -446,6 +453,15 @@ void MemProfiler::instrumentMaskedLoadOrStore(const DataLayout &DL, Value *Mask,
 
 void MemProfiler::instrumentMop(Instruction *I, const DataLayout &DL,
                                 InterestingMemoryAccess &Access) {
+  // Skip instrumentation of stack accesses unless requested.
+  if (!ClStack && isa<AllocaInst>(getUnderlyingObject(Access.Addr))) {
+    if (Access.IsWrite)
+      ++NumSkippedStackWrites;
+    else
+      ++NumSkippedStackReads;
+    return;
+  }
+
   if (Access.IsWrite)
     NumInstrumentedWrites++;
   else
diff --git a/llvm/test/Instrumentation/HeapProfiler/stack.ll b/llvm/test/Instrumentation/HeapProfiler/stack.ll
new file mode 100644 (file)
index 0000000..28be598
--- /dev/null
@@ -0,0 +1,49 @@
+; Test optional annotation of stack addresses.
+;
+; RUN: opt < %s -passes='function(memprof),module(memprof-module)' -S | FileCheck --check-prefixes=CHECK %s
+; RUN: opt < %s -passes='function(memprof),module(memprof-module)' -memprof-instrument-stack -S | FileCheck --check-prefixes=CHECK,STACK %s
+
+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 i32 @test_stack_load() {
+entry:
+  %x = alloca i32, align 4
+  %tmp1 = load i32, i32* %x, align 4
+  ret i32 %tmp1
+}
+; CHECK-LABEL: @test_stack_load
+; CHECK:         %[[SHADOW_OFFSET:[^ ]*]] = load i64, i64* @__memprof_shadow_memory_dynamic_address
+; CHECK-NEXT:   %x = alloca i32
+; STACK-NEXT:    %[[LOAD_ADDR:[^ ]*]] = ptrtoint i32* %x to i64
+; STACK-NEXT:    %[[MASKED_ADDR:[^ ]*]] = and i64 %[[LOAD_ADDR]], -64
+; STACK-NEXT:    %[[SHIFTED_ADDR:[^ ]*]] = lshr i64 %[[MASKED_ADDR]], 3
+; STACK-NEXT:    add i64 %[[SHIFTED_ADDR]], %[[SHADOW_OFFSET]]
+; STACK-NEXT:    %[[LOAD_SHADOW_PTR:[^ ]*]] = inttoptr
+; STACK-NEXT:    %[[LOAD_SHADOW:[^ ]*]] = load i64, i64* %[[LOAD_SHADOW_PTR]]
+; STACK-NEXT:    %[[NEW_SHADOW:[^ ]*]] = add i64 %[[LOAD_SHADOW]], 1
+; STACK-NEXT:    store i64 %[[NEW_SHADOW]], i64* %[[LOAD_SHADOW_PTR]]
+; The actual load.
+; CHECK-NEXT:    %tmp1 = load i32, i32* %x
+; CHECK-NEXT:    ret i32 %tmp1
+
+define void @test_stack_store() {
+entry:
+  %x = alloca i32, align 4
+  store i32 1, i32* %x, align 4
+  ret void
+}
+; CHECK-LABEL: @test_stack_store
+; CHECK:         %[[SHADOW_OFFSET:[^ ]*]] = load i64, i64* @__memprof_shadow_memory_dynamic_address
+; CHECK-NEXT:   %x = alloca i32
+; STACK-NEXT:    %[[STORE_ADDR:[^ ]*]] = ptrtoint i32* %x to i64
+; STACK-NEXT:    %[[MASKED_ADDR:[^ ]*]] = and i64 %[[STORE_ADDR]], -64
+; STACK-NEXT:    %[[SHIFTED_ADDR:[^ ]*]] = lshr i64 %[[MASKED_ADDR]], 3
+; STACK-NEXT:    add i64 %[[SHIFTED_ADDR]], %[[SHADOW_OFFSET]]
+; STACK-NEXT:    %[[STORE_SHADOW_PTR:[^ ]*]] = inttoptr
+; STACK-NEXT:    %[[STORE_SHADOW:[^ ]*]] = load i64, i64* %[[STORE_SHADOW_PTR]]
+; STACK-NEXT:    %[[NEW_SHADOW:[^ ]*]] = add i64 %[[STORE_SHADOW]], 1
+; STACK-NEXT:    store i64 %[[NEW_SHADOW]], i64* %[[STORE_SHADOW_PTR]]
+; The actual store.
+; CHECK-NEXT:    store i32 1, i32* %x
+; CHECK-NEXT:    ret void