[PATCH] D134769: [ASan] Workaround for a performance problem in StackLifetime::calculateLocalLiveness. The stack-safety-max-allocas flag prevents the algorithm from being invoked for functions with a lot of allocas.

Kirill Stoimenov via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 27 15:14:44 PDT 2022


kstoimenov created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
kstoimenov requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D134769

Files:
  llvm/lib/Analysis/StackSafetyAnalysis.cpp
  llvm/test/Instrumentation/AddressSanitizer/asan-stack-safety.ll


Index: llvm/test/Instrumentation/AddressSanitizer/asan-stack-safety.ll
===================================================================
--- llvm/test/Instrumentation/AddressSanitizer/asan-stack-safety.ll
+++ llvm/test/Instrumentation/AddressSanitizer/asan-stack-safety.ll
@@ -1,11 +1,17 @@
 ; REQUIRES: x86-registered-target
 
-; RUN: opt < %s -S -asan-instrumentation-with-call-threshold=0 -passes='asan-pipeline' -asan-use-stack-safety=0 -o - | FileCheck %s --implicit-check-not="call {{.*}} @__asan_{{load|store|stack}}" --check-prefixes=CHECK,NOSAFETY
-; RUN: opt < %s -S -asan-instrumentation-with-call-threshold=0 -passes='asan-pipeline' -asan-use-stack-safety=1 -o - | FileCheck %s --implicit-check-not="call {{.*}} @__asan_{{load|store|stack}}"
+; RUN: opt < %s -S -asan-instrumentation-with-call-threshold=0 -passes='asan-pipeline' -asan-use-stack-safety=0 -o - | \
+; RUN:   FileCheck %s --implicit-check-not="call {{.*}} @__asan_{{load|store|stack}}" --check-prefixes=CHECK,NOSAFETY
+; RUN: opt < %s -S -asan-instrumentation-with-call-threshold=0 -passes='asan-pipeline' -asan-use-stack-safety=1 -o - | \
+; RUN:   FileCheck %s --implicit-check-not="call {{.*}} @__asan_{{load|store|stack}}"
+; RUN: opt < %s -S -asan-instrumentation-with-call-threshold=0 -passes='asan-pipeline' -asan-use-stack-safety=1 \
+; RUN:   -stack-safety-max-allocas=0 -o - | FileCheck %s --check-prefixes=ALLOCAS
 
+; ALLOCAS-LABEL: define i32 @load
 ; CHECK-LABEL: define i32 @load
 define i32 @load() sanitize_address {
   %buf = alloca [10 x i8], align 1
+  ; ALLOCAS: call i64 @__asan_stack_malloc
   ; NOSAFETY: call i64 @__asan_stack_malloc
   %arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0
   %1 = load i8, i8* %arrayidx, align 1
@@ -13,9 +19,11 @@
   ret i32 0
 }
 
+; ALLOCAS-LABEL: define i32 @store
 ; CHECK-LABEL: define i32 @store
 define i32 @store() sanitize_address {
   %buf = alloca [10 x i8], align 1
+  ; ALLOCAS: call i64 @__asan_stack_malloc
   ; NOSAFETY: call i64 @__asan_stack_malloc
   %arrayidx = getelementptr inbounds [10 x i8], [10 x i8]* %buf, i64 0, i64 0
   store i8 0, i8* %arrayidx
Index: llvm/lib/Analysis/StackSafetyAnalysis.cpp
===================================================================
--- llvm/lib/Analysis/StackSafetyAnalysis.cpp
+++ llvm/lib/Analysis/StackSafetyAnalysis.cpp
@@ -59,6 +59,9 @@
 STATISTIC(NumIndexCalleeMultipleExternal, "Number of index callee non-unique external.");
 
 
+static cl::opt<size_t> StackSafetyMaxAllocas("stack-safety-max-allocas",
+                                             cl::init(250), cl::Hidden);
+
 static cl::opt<int> StackSafetyMaxIterations("stack-safety-max-iterations",
                                              cl::init(20), cl::Hidden);
 
@@ -527,8 +530,15 @@
 
   SmallVector<AllocaInst *, 64> Allocas;
   for (auto &I : instructions(F))
-    if (auto *AI = dyn_cast<AllocaInst>(&I))
+    if (auto *AI = dyn_cast<AllocaInst>(&I)) {
       Allocas.push_back(AI);
+      // Skip stack safety analysis if there are too many allocas.
+      if (Allocas.size() > StackSafetyMaxAllocas) {
+        LLVM_DEBUG(dbgs() << "\n[StackSafety] skipped\n");
+        return Info;
+      }
+    }
+
   StackLifetime SL(F, Allocas, StackLifetime::LivenessType::Must);
   SL.run();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D134769.463348.patch
Type: text/x-patch
Size: 3289 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20220927/8cca8fab/attachment.bin>


More information about the llvm-commits mailing list