[llvm] 3e48c8d - [Assignment Tracking] Only set module flag if pass modifies a function

via llvm-commits llvm-commits at lists.llvm.org
Wed Feb 22 08:58:13 PST 2023


Author: OCHyams
Date: 2023-02-22T16:57:07Z
New Revision: 3e48c8d75d1ca937ab3595909fb4198226c4ad1d

URL: https://github.com/llvm/llvm-project/commit/3e48c8d75d1ca937ab3595909fb4198226c4ad1d
DIFF: https://github.com/llvm/llvm-project/commit/3e48c8d75d1ca937ab3595909fb4198226c4ad1d.diff

LOG: [Assignment Tracking] Only set module flag if pass modifies a function

The module flag "debug-info-assignment-tracking" is set by the
declare-to-assign pass to indicate that assignment tracking is enabled. This
patch changes declare-to-assign to only set the flag if it makes a
modification. This lets the compiler avoid doing extra work for no benefit,
such as is currently the case if assignment tracking is requested for a build
with line tables only (-gmlt) or a build without debug info.

Reviewed By: scott.linder

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

Added: 
    llvm/test/DebugInfo/Generic/assignment-tracking/declare-to-assign/set-flag-only-if-modified.ll

Modified: 
    llvm/include/llvm/IR/DebugInfo.h
    llvm/lib/IR/DebugInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/IR/DebugInfo.h b/llvm/include/llvm/IR/DebugInfo.h
index 5e62d8723a550..4b3c3a62441a8 100644
--- a/llvm/include/llvm/IR/DebugInfo.h
+++ b/llvm/include/llvm/IR/DebugInfo.h
@@ -296,7 +296,7 @@ std::optional<AssignmentInfo> getAssignmentInfo(const DataLayout &DL,
 class AssignmentTrackingPass : public PassInfoMixin<AssignmentTrackingPass> {
   /// Note: this method does not set the debug-info-assignment-tracking module
   /// flag.
-  void runOnFunction(Function &F);
+  bool runOnFunction(Function &F);
 
 public:
   PreservedAnalyses run(Function &F, FunctionAnalysisManager &AM);

diff  --git a/llvm/lib/IR/DebugInfo.cpp b/llvm/lib/IR/DebugInfo.cpp
index c601dbf0d0b24..d3e7c257394ad 100644
--- a/llvm/lib/IR/DebugInfo.cpp
+++ b/llvm/lib/IR/DebugInfo.cpp
@@ -1963,7 +1963,8 @@ void at::trackAssignments(Function::iterator Start, Function::iterator End,
   }
 }
 
-void AssignmentTrackingPass::runOnFunction(Function &F) {
+bool AssignmentTrackingPass::runOnFunction(Function &F) {
+  bool Changed = false;
   // Collect a map of {backing storage : dbg.declares} (currently "backing
   // storage" is limited to Allocas). We'll use this to find dbg.declares to
   // delete after running `trackAssignments`.
@@ -2016,8 +2017,10 @@ void AssignmentTrackingPass::runOnFunction(Function &F) {
       // Delete DDI because the variable location is now tracked using
       // assignment tracking.
       DDI->eraseFromParent();
+      Changed = true;
     }
   }
+  return Changed;
 }
 
 static const char *AssignmentTrackingModuleFlag =
@@ -2040,7 +2043,8 @@ bool llvm::isAssignmentTrackingEnabled(const Module &M) {
 
 PreservedAnalyses AssignmentTrackingPass::run(Function &F,
                                               FunctionAnalysisManager &AM) {
-  runOnFunction(F);
+  if (!runOnFunction(F))
+    return PreservedAnalyses::all();
 
   // Record that this module uses assignment tracking. It doesn't matter that
   // some functons in the module may not use it - the debug info in those
@@ -2056,8 +2060,12 @@ PreservedAnalyses AssignmentTrackingPass::run(Function &F,
 
 PreservedAnalyses AssignmentTrackingPass::run(Module &M,
                                               ModuleAnalysisManager &AM) {
+  bool Changed = false;
   for (auto &F : M)
-    runOnFunction(F);
+    Changed |= runOnFunction(F);
+
+  if (!Changed)
+    return PreservedAnalyses::all();
 
   // Record that this module uses assignment tracking.
   setAssignmentTrackingModuleFlag(M);

diff  --git a/llvm/test/DebugInfo/Generic/assignment-tracking/declare-to-assign/set-flag-only-if-modified.ll b/llvm/test/DebugInfo/Generic/assignment-tracking/declare-to-assign/set-flag-only-if-modified.ll
new file mode 100644
index 0000000000000..3481bfe019914
--- /dev/null
+++ b/llvm/test/DebugInfo/Generic/assignment-tracking/declare-to-assign/set-flag-only-if-modified.ll
@@ -0,0 +1,47 @@
+; RUN: opt -passes=declare-to-assign -S %s -o - \
+; RUN: | FileCheck %s --check-prefix=WITHOUT-INTRINSIC
+
+; RUN: sed 's/;Uncomment-with-sed//g' < %s \
+; RUN: | opt -passes=declare-to-assign -S - -o - \
+; RUN: | FileCheck %s --check-prefix=WITH-INTRINSIC
+
+; WITHOUT-INTRINSIC-NOT: "debug-info-assignment-tracking"
+; WITH-INTRINSIC: "debug-info-assignment-tracking"
+
+;; Check that the module flag "debug-info-assignment-tracking" is only added if
+;; declare-to-assign actually modifies the function. In the first run the
+;; dbg.declare is commented out and the pass leaves the function unmodified.
+;; In the second run sed is used to uncomment the dbg.declare and
+;; declare-to-assign replaces it with a dbg.assign.
+
+define dso_local void @f() !dbg !9 {
+entry:
+  %a = alloca i32, align 4
+;Uncomment-with-sed  call void @llvm.dbg.declare(metadata ptr %a, metadata !13, metadata !DIExpression()), !dbg !16
+  ret void, !dbg !17
+}
+
+declare void @llvm.dbg.declare(metadata, metadata, metadata)
+
+!llvm.dbg.cu = !{!0}
+!llvm.module.flags = !{!2, !3, !4, !5, !6, !7}
+!llvm.ident = !{!8}
+
+!0 = distinct !DICompileUnit(language: DW_LANG_C11, file: !1, producer: "clang version 17.0.0", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, splitDebugInlining: false, nameTableKind: None)
+!1 = !DIFile(filename: "test.c", directory: "/")
+!2 = !{i32 7, !"Dwarf Version", i32 5}
+!3 = !{i32 2, !"Debug Info Version", i32 3}
+!4 = !{i32 1, !"wchar_size", i32 4}
+!5 = !{i32 8, !"PIC Level", i32 2}
+!6 = !{i32 7, !"PIE Level", i32 2}
+!7 = !{i32 7, !"uwtable", i32 2}
+!8 = !{!"clang version 17.0.0"}
+!9 = distinct !DISubprogram(name: "f", scope: !1, file: !1, line: 1, type: !10, scopeLine: 1, flags: DIFlagAllCallsDescribed, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: !0, retainedNodes: !12)
+!10 = !DISubroutineType(types: !11)
+!11 = !{null}
+!12 = !{!13}
+!13 = !DILocalVariable(name: "a", scope: !9, file: !1, line: 1, type: !14)
+!14 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
+!15 = !DILocation(line: 1, column: 12, scope: !9)
+!16 = !DILocation(line: 1, column: 16, scope: !9)
+!17 = !DILocation(line: 1, column: 19, scope: !9)


        


More information about the llvm-commits mailing list