[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-08 Thread Jian Cai via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rG4db2b7024868: Add a flag to debug automatic variable 
initialization (authored by jcai19).

Changed prior to commit:
  https://reviews.llvm.org/D77168?vs=269301=269324#toc

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/DiagnosticDriverKinds.td
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -555,5 +555,18 @@
 // CHECK-TRIVIAL-ZERO-GOOD-NOT: hasn't been enabled
 // CHECK-TRIVIAL-ZERO-BAD: hasn't been enabled
 
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN-STOP-AFTER %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-STOP-AFTER %s
+// RUN: %clang -### -S -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-STOP-AFTER-MISSING-DEPENDENCY %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN-STOP-AFTER-INVALID-VALUE %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-STOP-AFTER-INVALID-VALUE %s
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-NOT: is used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-NOT: only accepts positive integers
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-NOT: is used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-NOT: only accepts positive integers
+// CHECK-TRIVIAL-STOP-AFTER-MISSING-DEPENDENCY: used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-INVALID-VALUE: only accepts positive integers
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-INVALID-VALUE: only accepts positive integers
+
 // RUN: %clang -### -S -fno-temp-file %s 2>&1 | FileCheck -check-prefix=CHECK-NO-TEMP-FILE %s
 // CHECK-NO-TEMP-FILE: "-fno-temp-file"
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-1-SCALAR
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2-ARRAY
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3-VLA
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-4-POINTER
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=5 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-5-BUILTIN
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=1 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-1-SCALAR
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2-ARRAY
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3-VLA
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-4-POINTER
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=5 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-5-BUILTIN
+
+#define ARRLEN 10
+
+typedef struct {
+  int 

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-08 Thread Jian Cai via Phabricator via cfe-commits
jcai19 marked 2 inline comments as done.
jcai19 added a comment.

In D77168#2080296 , @jfb wrote:

> Two minor corrections, looks good otherwise.


Thank you for all the comments and help. I have fixed the issues.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-08 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 269301.
jcai19 added a comment.

Fix typos.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/DiagnosticDriverKinds.td
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -579,5 +579,18 @@
 // CHECK-TRIVIAL-ZERO-GOOD-NOT: hasn't been enabled
 // CHECK-TRIVIAL-ZERO-BAD: hasn't been enabled
 
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN-STOP-AFTER %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-STOP-AFTER %s
+// RUN: %clang -### -S -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-STOP-AFTER-MISSING-DEPENDENCY %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN-STOP-AFTER-INVALID-VALUE %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-STOP-AFTER-INVALID-VALUE %s
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-NOT: is used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-NOT: only accepts positive integers
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-NOT: is used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-NOT: only accepts positive integers
+// CHECK-TRIVIAL-STOP-AFTER-MISSING-DEPENDENCY: used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-INVALID-VALUE: only accepts positive integers
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-INVALID-VALUE: only accepts positive integers
+
 // RUN: %clang -### -S -fno-temp-file %s 2>&1 | FileCheck -check-prefix=CHECK-NO-TEMP-FILE %s
 // CHECK-NO-TEMP-FILE: "-fno-temp-file"
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-1-SCALAR
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2-ARRAY
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3-VLA
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-4-POINTER
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=5 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-5-BUILTIN
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=1 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-1-SCALAR
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2-ARRAY
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3-VLA
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-4-POINTER
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=5 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-5-BUILTIN
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo(unsigned n) {
+  // scalar variable
+  long a = 888;
+  // array
+  S arr[ARRLEN];
+  // VLA
+  S vla[n];
+  // pointer
+  void *p;
+  // builtin
+  p 

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-08 Thread JF Bastien via Phabricator via cfe-commits
jfb accepted this revision.
jfb added a comment.
This revision is now accepted and ready to land.

Two minor corrections, looks good otherwise.




Comment at: clang/include/clang/Basic/DiagnosticDriverKinds.td:491
+def err_drv_trivial_auto_var_init_stop_after_invalid_value : Error<
+  "-ftrivial-auto-var-init-stop-after=* only accpets positive integers.">;
+

Typo "accepts".



Comment at: clang/lib/CodeGen/CodeGenModule.cpp:5973
+  DiagnosticsEngine::Warning,
+  "-ftrivial-auto-var-init-stop-after=* has been enabled to limit the "
+  "number of times ftrivial-auto-var-init=%0 gets applied.");

Could you print out the count here, instead of `*`?



Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-07 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

> If we have a mechanism bisecting pragmas, this option will not be needed.

Yes we would have not had to start to work on this patch if such mechanism had 
been implemented. We can always migrate to that approach if said mechanism is 
implemented in the future. I think it should not be too difficult.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-07 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 269103.
jcai19 marked 4 inline comments as done.
jcai19 added a comment.

Cover VLAs.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/DiagnosticDriverKinds.td
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -579,5 +579,18 @@
 // CHECK-TRIVIAL-ZERO-GOOD-NOT: hasn't been enabled
 // CHECK-TRIVIAL-ZERO-BAD: hasn't been enabled
 
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN-STOP-AFTER %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-STOP-AFTER %s
+// RUN: %clang -### -S -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-STOP-AFTER-MISSING-DEPENDENCY %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN-STOP-AFTER-INVALID-VALUE %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=0 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-STOP-AFTER-INVALID-VALUE %s
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-NOT: is used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-NOT: only accpets positive integers
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-NOT: is used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-NOT: only accpets positive integers
+// CHECK-TRIVIAL-STOP-AFTER-MISSING-DEPENDENCY: used without -ftrivial-auto-var-init
+// CHECK-TRIVIAL-PATTERN-STOP-AFTER-INVALID-VALUE: only accpets positive integers
+// CHECK-TRIVIAL-ZERO-STOP-AFTER-INVALID-VALUE: only accpets positive integers
+
 // RUN: %clang -### -S -fno-temp-file %s 2>&1 | FileCheck -check-prefix=CHECK-NO-TEMP-FILE %s
 // CHECK-NO-TEMP-FILE: "-fno-temp-file"
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,58 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-1-SCALAR
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2-ARRAY
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3-VLA
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-4-POINTER
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=5 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-5-BUILTIN
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=1 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-1-SCALAR
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2-ARRAY
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3-VLA
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-4-POINTER
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=5 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-5-BUILTIN
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo(unsigned n) {
+  // scalar variable
+  long a = 888;
+  // array
+  S arr[ARRLEN];
+  // VLA
+  S vla[n];
+  

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-07 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

In D77168#2073736 , @jfb wrote:

> Can you add a test for the diagnostic firing after the correct number of 
> initializations? This should include a few types of auto-init, including VLAs.


Thank you for the comments! It turned out the patch did not cover VLAs, which I 
have addressed in the latest version along other issues you brought up. Thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-04 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

In D77168#2070334 , @llozano wrote:

> In D77168#2070083 , @MaskRay wrote:
>
> > In D77168#2070049 , @jcai19 wrote:
> >
> > > In D77168#2069783 , @MaskRay 
> > > wrote:
> > >
> > > > Do we have a mechanism bisecting pragmas? If yes, we can let that tool 
> > > > add `#pragma clang attribute push([[clang::uninitialized]], apply_to = 
> > > > variable)`
> > >
> > >
> > > Not that I am aware of unfortunately. The whole point of having this 
> > > patch is to add the ability to bisect programmatically, along the 
> > > approach of using pragma as introduced in https://reviews.llvm.org/D78693.
> >
> >
> > If we have a mechanism bisecting pragmas, this option will not be needed.
>
>
> I think you are coming late into the discussion. If you read before, We had 
> already agreed that having mechanisms for automatic triaging is "very 
> compelling". The change suggested here just makes automatic triaging much 
> easier at a very low cost.


I expressed my opinion very early in https://reviews.llvm.org/D77168#1955507  
If other reviewers find this useful even if we have a generic framework, I will 
not object.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-04 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

Can you add a test for the diagnostic firing after the correct number of 
initializations? This should include a few types of auto-init, including VLAs.




Comment at: clang/include/clang/Basic/DiagnosticDriverKinds.td:489
+  "-ftrivial-auto-var-init-stop-after=* has been enabled to limit the number 
of times ftrivial-auto-var-init=zero/pattern gets applied.">,
+  InGroup>;
+

I don't think this is sufficiently clear: automatic variable initialization is 
now disabled because ftrivial-auto-var-init-stop-after has reached its limit.



Comment at: clang/lib/CodeGen/CGDecl.cpp:1684
+  if (CGM.stopAutoInit())
+return;
+  if (trivialAutoVarInit == LangOptions::TrivialAutoVarInitKind::Zero)

I'd rather repeat this than fallthrought and repeat the condition in if/else.



Comment at: clang/lib/CodeGen/CodeGenModule.h:1390
+  if (NumAutoVarInit >= StopAfter)
+return true;
+  ++NumAutoVarInit;

The first time this returns true is when the diagnostic should be emitted.



Comment at: clang/lib/Driver/ToolChains/Clang.cpp:3092
+  Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) {
+D.Diag(diag::warn_drv_trivial_auto_var_init_stop_after_enabled);
+A->claim();

Not here.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-03 Thread Luis Lozano via Phabricator via cfe-commits
llozano added a comment.

In D77168#2070083 , @MaskRay wrote:

> In D77168#2070049 , @jcai19 wrote:
>
> > In D77168#2069783 , @MaskRay wrote:
> >
> > > Do we have a mechanism bisecting pragmas? If yes, we can let that tool 
> > > add `#pragma clang attribute push([[clang::uninitialized]], apply_to = 
> > > variable)`
> >
> >
> > Not that I am aware of unfortunately. The whole point of having this patch 
> > is to add the ability to bisect programmatically, along the approach of 
> > using pragma as introduced in https://reviews.llvm.org/D78693.
>
>
> If we have a mechanism bisecting pragmas, this option will not be needed.


I think you are coming late into the discussion. If you read before, We had 
already agreed that having mechanisms for automatic triaging is "very 
compelling". The change suggested here just makes automatic triaging much 
easier at a very low cost.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-02 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

In D77168#2070049 , @jcai19 wrote:

> In D77168#2069783 , @MaskRay wrote:
>
> > Do we have a mechanism bisecting pragmas? If yes, we can let that tool add 
> > `#pragma clang attribute push([[clang::uninitialized]], apply_to = 
> > variable)`
>
>
> Not that I am aware of unfortunately. The whole point of having this patch is 
> to add the ability to bisect programmatically, along the approach of using 
> pragma as introduced in https://reviews.llvm.org/D78693.


If we have a mechanism bisecting pragmas, this option will not be needed.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-02 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

In D77168#2069783 , @MaskRay wrote:

> Do we have a mechanism bisecting pragmas? If yes, we can let that tool add 
> `#pragma clang attribute push([[clang::uninitialized]], apply_to = variable)`


Not that I am aware of unfortunately. The whole point of having this patch is 
to add the ability to bisect programmatically, along the approach of using 
pragma as introduced in https://reviews.llvm.org/D78693.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-02 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

Do we have a mechanism bisecting pragmas? If yes, we can let that tool add 
`#pragma clang attribute push([[clang::uninitialized]], apply_to = variable)`


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-06-02 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

@jfb Does the current implementation look good? Thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-05-20 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

Just want to follow up and see if anything is missing from the latest patch :)


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-05-04 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 261740.
jcai19 added a comment.

Issue a warning when the proposed flag is used.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/DiagnosticDriverKinds.td
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -571,8 +571,8 @@
 // CHECK-RECORD-GCC-SWITCHES-ESCAPED: "-record-command-line" "{{.+}}with\\ spaces{{.+}}"
 
 // RUN: %clang -### -S -ftrivial-auto-var-init=uninitialized %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-UNINIT %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=pattern %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
 // RUN: %clang -### -S -ftrivial-auto-var-init=zero %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-BAD %s
 // CHECK-TRIVIAL-UNINIT-NOT: hasn't been enabled
 // CHECK-TRIVIAL-PATTERN-NOT: hasn't been enabled
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-4
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-4
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo(unsigned n) {
+  long a = 888;
+  S arr1[ARRLEN];
+  void *p;
+  p = __builtin_alloca(sizeof(unsigned long long) * n);
+  // PATTERN-STOP-AFTER-2-NOT: store i8* inttoptr (i64 -6148914691236517206 to i8*), i8** %p, align 8
+  // PATTERN-STOP-AFTER-3: store i8* inttoptr (i64 -6148914691236517206 to i8*), i8** %p, align 8
+  // PATTERN-STOP-AFTER-3-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 -86, i64 %mul, i1 false)
+  // PATTERN-STOP-AFTER-4: call void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 -86, i64 %mul, i1 false)
+  // ZERO-STOP-AFTER-2-NOT: store i8* null, i8** %p, align 8
+  // ZERO-STOP-AFTER-3: store i8* null, i8** %p, align 8
+  // ZERO-STOP-AFTER-3-NOT: void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 0, i64 %mul, i1 false)
+  // ZERO-STOP-AFTER-4: void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 0, i64 %mul, i1 false)
+  return 0;
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3256,6 +3256,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-05-01 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 261597.
jcai19 added a comment.

Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -571,8 +571,8 @@
 // CHECK-RECORD-GCC-SWITCHES-ESCAPED: "-record-command-line" "{{.+}}with\\ spaces{{.+}}"
 
 // RUN: %clang -### -S -ftrivial-auto-var-init=uninitialized %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-UNINIT %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=pattern %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
 // RUN: %clang -### -S -ftrivial-auto-var-init=zero %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-BAD %s
 // CHECK-TRIVIAL-UNINIT-NOT: hasn't been enabled
 // CHECK-TRIVIAL-PATTERN-NOT: hasn't been enabled
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-4
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-4
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo(unsigned n) {
+  long a = 888;
+  S arr1[ARRLEN];
+  void *p;
+  p = __builtin_alloca(sizeof(unsigned long long) * n);
+  // PATTERN-STOP-AFTER-2-NOT: store i8* inttoptr (i64 -6148914691236517206 to i8*), i8** %p, align 8
+  // PATTERN-STOP-AFTER-3: store i8* inttoptr (i64 -6148914691236517206 to i8*), i8** %p, align 8
+  // PATTERN-STOP-AFTER-3-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 -86, i64 %mul, i1 false)
+  // PATTERN-STOP-AFTER-4: call void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 -86, i64 %mul, i1 false)
+  // ZERO-STOP-AFTER-2-NOT: store i8* null, i8** %p, align 8
+  // ZERO-STOP-AFTER-3: store i8* null, i8** %p, align 8
+  // ZERO-STOP-AFTER-3-NOT: void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 0, i64 %mul, i1 false)
+  // ZERO-STOP-AFTER-4: void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 0, i64 %mul, i1 false)
+  return 0;
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3256,6 +3256,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ 

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-05-01 Thread Jian Cai via Phabricator via cfe-commits
jcai19 marked an inline comment as done.
jcai19 added inline comments.



Comment at: clang/lib/CodeGen/CGDecl.cpp:1817
+  CGM.countAutoVarInit();
+}
+

jfb wrote:
> This isn't the right place to stop auto-init: we can get past this point even 
> without auto-init. As-is you're introducing a change in the regular language.
> 
> I hope that some tests for whatever happens below would break if you hack 
> your flag to always stop auto-init for any value. i.e. just on your machine, 
> run the entire test suite with this forcibly on. Only auto-init tests should 
> fail, the others should all still pass.
I tried to always return and it seemed only auto-init tests failed while 
running check-clang. But I agree this is not the right place. I have moved this 
piece of code. Thanks!



Comment at: clang/lib/CodeGen/CodeGenModule.h:305
   const CodeGenOptions 
+  mutable unsigned NumAutoVarInit = 0;
   llvm::Module 

jfb wrote:
> This is reachable from a context where `CodeGenModule` is `const` ? :( 
I copied it from my previous iteration and forgot to remove the keyword. Thanks 
for catching that.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-05-01 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 261595.
jcai19 marked 3 inline comments as done.
jcai19 added a comment.

Update based on comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -571,8 +571,8 @@
 // CHECK-RECORD-GCC-SWITCHES-ESCAPED: "-record-command-line" "{{.+}}with\\ spaces{{.+}}"
 
 // RUN: %clang -### -S -ftrivial-auto-var-init=uninitialized %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-UNINIT %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=pattern %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
 // RUN: %clang -### -S -ftrivial-auto-var-init=zero %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-BAD %s
 // CHECK-TRIVIAL-UNINIT-NOT: hasn't been enabled
 // CHECK-TRIVIAL-PATTERN-NOT: hasn't been enabled
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-4
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-4
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo(unsigned n) {
+  long a = 888;
+  S arr1[ARRLEN];
+  void *p;
+  p = __builtin_alloca(sizeof(unsigned long long) * n);
+  // PATTERN-STOP-AFTER-2-NOT: store i8* inttoptr (i64 -6148914691236517206 to i8*), i8** %p, align 8
+  // PATTERN-STOP-AFTER-3: store i8* inttoptr (i64 -6148914691236517206 to i8*), i8** %p, align 8
+  // PATTERN-STOP-AFTER-3-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 -86, i64 %mul, i1 false)
+  // PATTERN-STOP-AFTER-4: call void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 -86, i64 %mul, i1 false)
+  // ZERO-STOP-AFTER-2-NOT: store i8* null, i8** %p, align 8
+  // ZERO-STOP-AFTER-3: store i8* null, i8** %p, align 8
+  // ZERO-STOP-AFTER-3-NOT: void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 0, i64 %mul, i1 false)
+  // ZERO-STOP-AFTER-4: void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 0, i64 %mul, i1 false)
+  return 0;
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3256,6 +3256,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- 

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-29 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

I think you want to emit a diagnostic any time auto-init doesn't happen because 
of this new flag. With the code simplification I propose, it would be pretty 
hard to introduce a regression... and it someone does it wouldn't be a silent 
one :)




Comment at: clang/lib/CodeGen/CGBuiltin.cpp:84
+CGF.CGM.countAutoVarInit();
+  }
   CGF.Builder.CreateMemSet(AI, Byte, Size, AlignmentInBytes);

I think this logic is about right, but it seems like something you'd want to 
use as a two-liner instead. Maybe something like:

```
if (autoInitStopCounter())
  return;
```

This would check the value is set, check its value against the counter, and 
increment the counter.



Comment at: clang/lib/CodeGen/CGDecl.cpp:1817
+  CGM.countAutoVarInit();
+}
+

This isn't the right place to stop auto-init: we can get past this point even 
without auto-init. As-is you're introducing a change in the regular language.

I hope that some tests for whatever happens below would break if you hack your 
flag to always stop auto-init for any value. i.e. just on your machine, run the 
entire test suite with this forcibly on. Only auto-init tests should fail, the 
others should all still pass.



Comment at: clang/lib/CodeGen/CodeGenModule.h:305
   const CodeGenOptions 
+  mutable unsigned NumAutoVarInit = 0;
   llvm::Module 

This is reachable from a context where `CodeGenModule` is `const` ? :( 


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-28 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 260796.
jcai19 added a comment.

Update tests.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -571,8 +571,8 @@
 // CHECK-RECORD-GCC-SWITCHES-ESCAPED: "-record-command-line" "{{.+}}with\\ spaces{{.+}}"
 
 // RUN: %clang -### -S -ftrivial-auto-var-init=uninitialized %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-UNINIT %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=pattern %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
 // RUN: %clang -### -S -ftrivial-auto-var-init=zero %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-BAD %s
 // CHECK-TRIVIAL-UNINIT-NOT: hasn't been enabled
 // CHECK-TRIVIAL-PATTERN-NOT: hasn't been enabled
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,29 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-4
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=4 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-4
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo(unsigned n) {
+  long a = 888;
+  S arr1[ARRLEN];
+  void *p;
+  p = __builtin_alloca (sizeof(unsigned long long) * n);
+  // PATTERN-STOP-AFTER-2-NOT: store i8* inttoptr (i64 -6148914691236517206 to i8*), i8** %p, align 8
+  // PATTERN-STOP-AFTER-3: store i8* inttoptr (i64 -6148914691236517206 to i8*), i8** %p, align 8
+  // PATTERN-STOP-AFTER-3-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 -86, i64 %mul, i1 false)
+  // PATTERN-STOP-AFTER-4: call void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 -86, i64 %mul, i1 false)
+  // ZERO-STOP-AFTER-2-NOT: store i8* null, i8** %p, align 8
+  // ZERO-STOP-AFTER-3: store i8* null, i8** %p, align 8
+  // ZERO-STOP-AFTER-3-NOT: void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 0, i64 %mul, i1 false)
+  // ZERO-STOP-AFTER-4: void @llvm.memset.p0i8.i64(i8* align 16 %2, i8 0, i64 %mul, i1 false)
+  return 0;
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3256,6 +3256,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ 

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-28 Thread Jian Cai via Phabricator via cfe-commits
jcai19 marked an inline comment as done.
jcai19 added a comment.



> - Allows bracketing as John suggested (lower / upper bounds where to stop / 
> start).

I have made some updates to this patch based on the comments here. One question 
I have though is whether we should have another option for starting point. Now 
that we implement the pragma support at https://reviews.llvm.org/D78693, 
bracketing can be done with pragma of uninitialized attribute. For bisection, 
we do not really need to specify a starting point. Not having it also means 
less change to the code path of automatic variable initialization, and 
therefore a less chance of introducing bugs.




Comment at: clang/lib/CodeGen/CGDecl.cpp:1814
+if (StopAfter) {
+  static unsigned Counter = 0;
+  if (Counter >= StopAfter)

rjmccall wrote:
> jcai19 wrote:
> > rjmccall wrote:
> > > srhines wrote:
> > > > MaskRay wrote:
> > > > > I am a bit worried about the static variable. This makes CodeGen not 
> > > > > reusable.
> > > > The counter could exist in ASTContext instead.
> > > IRGenModule would be the more appropriate place.
> > I can't seem to find IRGenModule. Do you mean CodeGenModule by any chance? 
> > Thanks.
> Yes, sorry.  The Clang and Swift frontends use slightly different names for 
> the same concept.
Thank you for the confirmation! Sorry I missed your reply earlier.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-28 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 260788.
jcai19 added a comment.

Update baesd on comments.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGBuiltin.cpp
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/CodeGen/CodeGenModule.h
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -571,8 +571,8 @@
 // CHECK-RECORD-GCC-SWITCHES-ESCAPED: "-record-command-line" "{{.+}}with\\ spaces{{.+}}"
 
 // RUN: %clang -### -S -ftrivial-auto-var-init=uninitialized %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-UNINIT %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=pattern %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
 // RUN: %clang -### -S -ftrivial-auto-var-init=zero %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-BAD %s
 // CHECK-TRIVIAL-UNINIT-NOT: hasn't been enabled
 // CHECK-TRIVIAL-PATTERN-NOT: hasn't been enabled
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo() {
+  long a = 888;
+  S arr1[ARRLEN], arr2[ARRLEN];
+  // PATTERN-STOP-AFTER-2-NOT: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+  // PATTERN-STOP-AFTER-2-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 -86, i64 80, i1 false)
+  // PATTERN-STOP-AFTER-3: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+  // PATTERN-STOP-AFTER-3: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 -86, i64 80, i1 false)
+
+  // ZERO-STOP-AFTER-2-NOT: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+  // ZERO-STOP-AFTER-2-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 0, i64 80, i1 false)
+  // ZERO-STOP-AFTER-3: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+  // ZERO-STOP-AFTER-3: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 0, i64 80, i1 false)
+
+  return 0;
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3256,6 +3256,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3086,6 +3086,14 @@
 CmdArgs.push_back(
 Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
   }
+
+  if (Arg *A =
+  Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) {
+A->claim();
+StringRef Val = A->getValue();
+CmdArgs.push_back(
+Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val));
+  }
 }
 
 static void 

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-21 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

> I'd also like to see the pragma attribute approach, as well as byte-pattern 
> variability as I described. I don't think auto-narrowing is the only approach 
> we should push people towards.

Thank you for all the feedback. I agree,  the pragma attribute and the 
byte-pattern approach are versatile, and great to have along the proposed 
approach. Will work on that and iterate on this patch based on the feedback.

> Do you have an existing experience of using this flag in your build? What's 
> your strategy for using it?

Thank you for sharing your experience. It's great to know other people are 
facing similar challenges. I used this flag to debug an issue 
 while enabling 
-ftrivial-auto-var-init=pattern on ChromeOS kernel 4.14. I used a bisection 
tool that traced the issue into the file of interest, but I did not know which 
part of the code caused the issue. That file had many 

 small functions and local variables so it would be time consuming to manually 
fiddle with each variable to find out the root cause. On Linux kernel, it's 
easy to add build flags to each individual files, and ChromeOS provided a tool 
to update the OS on the test device,  so each time I increased the counter with 
the proposed option and rebuilt the kernel, I updated the test device with the 
new kernel to verify if the issue still reproduce.  Eventually it turned out 
some variable was used before [initialized](crrev.com/c/2020401). It seems your 
scenario is slightly different, in the sense you already know which portion of 
the code is problematic, in that case a pragma-driven approach should work 
perfectly.

> In our build, we always apply flags to the entire target, not individual 
> files, so to use the proposed flag, I'd have to modify the build to extract a 
> single file into a separate target in order to apply the flag only to that 
> file which seems comparatively intrusive to inserting the pragma. Is that 
> something you could comment on?

I am not familiar with Fuchsia build system, it does sound more complicated if 
you have to create a separate build target to just pass the flag. However, you 
will need this flag most likely when you run into an issue and you do not have 
much idea what might have been the cause, so I assume this will not happen that 
often :). And IMO the time saved from automating the bisect process should more 
than enough cover the extra effort for creating the build targets in those 
cases.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-20 Thread Petr Hosek via Phabricator via cfe-commits
phosek added a comment.

We've rolled out `-ftrivial-auto-var-init=pattern` to all of Fuchsia several 
months ago. In my experience, having the `pragma` would have been really 
valuable during the rollout stage when we've often commented out portion of 
code while narrowing down the issue, which is basically a strawman version of 
the `pragma`. Similarly, the variable byte pattern would have also been a great 
timesaver.

I don't think we ever had a use case for the proposed flag, which doesn't mean 
that it's not useful, but I don't know how to use it effectively. Specifically, 
every time I was debugging an uninitialized variable issue, I'd know which file 
and often even which portion of the file the bug is in. In our build, we always 
apply flags to the entire target, not individual files, so to use the proposed 
flag, I'd have to modify the build to extract a single file into a separate 
target in order to apply the flag only to that file which seems comparatively 
intrusive to inserting the `pragma`. Is that something you could comment on? Do 
you have an existing experience of using this flag in your build? What's your 
strategy for using it?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-17 Thread Hubert Tong via Phabricator via cfe-commits
hubert.reinterpretcast added a comment.

In D77168#1989973 , @jfb wrote:

> I'd also like to see the pragma attribute approach, as well as byte-pattern 
> variability as I described. I don't think auto-narrowing is the only approach 
> we should push people towards.


I also would like to see the pragma attribute in addition to the command-line 
option. The attribute is closer to the source code even for debugging purposes. 
If it is not too finicky, it might be usable even with relatively naïve text 
bisection tooling.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-17 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

Automatic narrowing of bugs is indeed compelling, so I'd support that as long 
as it:

- Allows bracketing as John suggested (lower / upper bounds where to stop / 
start).
- Is implemented in a way which makes it really hard to regress the security 
mitigation. Maybe this requires emitting a diagnostic when auto-init isn't 
applied because of the flag.
- Is thorough (i.e. it covers all auto-init, for example `initializeAlloca` was 
missing).

I'd also like to see the pragma attribute approach, as well as byte-pattern 
variability as I described. I don't think auto-narrowing is the only approach 
we should push people towards.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-17 Thread Luis Lozano via Phabricator via cfe-commits
llozano added a comment.

In D77168#1988138 , @srhines wrote:

> In D77168#1988122 , @jfb wrote:
>
> > In D77168#1988049 , @srhines wrote:
> >
> > > `pragma clang attribute` is interesting, but how do you apply that in a 
> > > selective fashion to local variables (especially in a way that can be 
> > > automated)? At first, I didn't think the goal for this should be to 
> > > create a frequently used option for **most** end users, but I do think 
> > > that it could be quite useful for more folks when debugging, especially 
> > > if it is easy to automate (which optimization-fuel approaches are, while 
> > > pragmas are not).
> >
> >
> > `__attribute__((uninitialized))` for more selectiveness :)
> >
> > Of course, not automated. In general we don't really automate compiler 
> > things of this sort, say UBSan. OptRemarks is the best we really have to 
> > dig into these things. One other option would be to truly randomize the 
> > init pattern: emit a handful of different byte patterns, and then see the 
> > crash caused by a different pattern, and track it back to which local 
> > variable got that byte pattern.
>
>
> That's just it though. This technique is tried and true in other 
> compilers/tools. Why should we let past history dictate whether to facilitate 
> automation in the present/future? Rather than having to "track it back", you 
> can let the tooling do it for you. It gets you at least a breakage related to 
> this transformation, and the best part is you can fix that one, and then run 
> it again to chase down further issues. All without modifying source code. I 
> feel like I can't be the only one who has used these kinds of tools for 
> debugging transformations before, hence why I feel so invested in making this 
> easy for everyone (compiler devs and regular users). UBSan isn't as relevant 
> as it can provide diagnostics for the particular instance in which it was 
> tripped. If we wanted to do something similar for initialization, we would 
> have to mark the uninitialized value and track down any potential use of it, 
> which seems like a lot more complexity and decreased performance too.


I totally agree with the value of automatic triaging of compiler 
transformations. My team uses techniques like that quite often. It may be 
relatively easy to triage a failure caused/exposed by a compiler transformation 
when you are applying to a single package that you have some familiarity with. 
It is totally different when you are trying to apply a new compiler 
transformation to a platform made up of hundreds (or thousands) of packages you 
are not familiar with. If this feature becomes more popular, a lot of compiler 
users will benefit from having mechanisms to automatic triage initializations 
that change program behavior. This is useful not only for compiler developers 
but also for compiler users.
The pragma attribute push functionality is useful but I see that as 
complimentary to what this change is proposing. It allows developers more 
flexibility on what gets the transformation applied and a larger granularity 
but it is not as convenient for bisection since it needs source code changes.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-16 Thread Stephen Hines via Phabricator via cfe-commits
srhines added a comment.

In D77168#1988122 , @jfb wrote:

> In D77168#1988049 , @srhines wrote:
>
> > `pragma clang attribute` is interesting, but how do you apply that in a 
> > selective fashion to local variables (especially in a way that can be 
> > automated)? At first, I didn't think the goal for this should be to create 
> > a frequently used option for **most** end users, but I do think that it 
> > could be quite useful for more folks when debugging, especially if it is 
> > easy to automate (which optimization-fuel approaches are, while pragmas are 
> > not).
>
>
> `__attribute__((uninitialized))` for more selectiveness :)
>
> Of course, not automated. In general we don't really automate compiler things 
> of this sort, say UBSan. OptRemarks is the best we really have to dig into 
> these things. One other option would be to truly randomize the init pattern: 
> emit a handful of different byte patterns, and then see the crash caused by a 
> different pattern, and track it back to which local variable got that byte 
> pattern.


That's just it though. This technique is tried and true in other 
compilers/tools. Why should we let past history dictate whether to facilitate 
automation in the present/future? Rather than having to "track it back", you 
can let the tooling do it for you. It gets you at least a breakage related to 
this transformation, and the best part is you can fix that one, and then run it 
again to chase down further issues. All without modifying source code. I feel 
like I can't be the only one who has used these kinds of tools for debugging 
transformations before, hence why I feel so invested in making this easy for 
everyone (compiler devs and regular users). UBSan isn't as relevant as it can 
provide diagnostics for the particular instance in which it was tripped. If we 
wanted to do something similar for initialization, we would have to mark the 
uninitialized value and track down any potential use of it, which seems like a 
lot more complexity and decreased performance too.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-16 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

In D77168#1988049 , @srhines wrote:

> `pragma clang attribute` is interesting, but how do you apply that in a 
> selective fashion to local variables (especially in a way that can be 
> automated)? At first, I didn't think the goal for this should be to create a 
> frequently used option for **most** end users, but I do think that it could 
> be quite useful for more folks when debugging, especially if it is easy to 
> automate (which optimization-fuel approaches are, while pragmas are not).


`__attribute__((uninitialized))` for more selectiveness :)

Of course, not automated. In general we don't really automate compiler things 
of this sort, say UBSan. OptRemarks is the best we really have to dig into 
these things. One other option would be to truly randomize the init pattern: 
emit a handful of different byte patterns, and then see the crash caused by a 
different pattern, and track it back to which local variable got that byte 
pattern.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-16 Thread Stephen Hines via Phabricator via cfe-commits
srhines added a comment.

`pragma clang attribute` is interesting, but how do you apply that in a 
selective fashion to local variables (especially in a way that can be 
automated)? At first, I didn't think the goal for this should be to create a 
frequently used option for **most** end users, but I do think that it could be 
quite useful for more folks when debugging, especially if it is easy to 
automate (which optimization-fuel approaches are, while pragmas are not).


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-16 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

In D77168#1984109 , @jcai19 wrote:

> > Right, support needs to be added, and I was wondering if you'd want to do 
> > this because it seems like a tool that would help you out.
>
> Sorry for the delay. Yes the biggest advantage of this option would be to 
> make automating bisection much easier. Or are you suggesting we should do it 
> differently, like a tool that can automatically inserts macros in the source 
> code?


I suggest implementing support for this:

  void use(void*);
  
  #pragma clang attribute push ([[clang::uninitialized]], apply_to = variable)
  void buggy() {
  int arr[256];
  int boom;
  float bam;
  struct { int oops; } oops;
  union { int oof; float a; } oof;
  
  use();
  use();
  use();
  use();
  use();
  }
  #pragma clang attribute pop

`pragma clang attribute` is already a feature, we just need to teach it about 
this one attribute.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-15 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

> Right, support needs to be added, and I was wondering if you'd want to do 
> this because it seems like a tool that would help you out.

Sorry for the delay. Yes the biggest advantage of this option would be to make 
automating bisection much easier. Or are you suggesting we should do it 
differently, like a tool that can automatically inserts macros in the source 
code?


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added a comment.

In D77168#1955500 , @jfb wrote:

> In D77168#1955450 , @jcai19 wrote:
>
> > In D77168#1955312 , @jfb wrote:
> >
> > > Do you not think `pragma` is a more general approach? That's what's used 
> > > in a bunch of other cases, and I'd like to see it attempted here.
> >
> >
> > Yes I absolutely agree pragma is a great way to bisect and will work in 
> > many scenarios, and the result is clear once the bisection is done. But 
> > like @srhines said, doing this one function at a time is preferable but not 
> > always possible (or maybe we can wrap consecutive functions within the same 
> > pragma push/pop pair? I tried to verify but ran into error: attribute 
> > 'uninitialized' is not supported by '#pragma clang attribute), and 
> > automating it is more difficult. From this perspective, I think the 
> > proposed patch is complementary to pragma and not meant to replace it.
>
>
> Right, support needs to be added, and I was wondering if you'd want to do 
> this because it seems like a tool that would help you out.


My understanding is that we can invent a compiler option to add `#pragma clang 
attribute push` for declarations satisfying some criteria (e.g. first N)? No 
source is modified. This option can potentially be useful for other attributes 
which are orthogonal to their functionality (concrete examples: sanitizer/XRay 
attributes). If we go that route, I agree that this proposed 
-ftrivial-auto-var-init-stop-after= does not seem that useful.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

In D77168#1955450 , @jcai19 wrote:

> In D77168#1955312 , @jfb wrote:
>
> > Do you not think `pragma` is a more general approach? That's what's used in 
> > a bunch of other cases, and I'd like to see it attempted here.
>
>
> Yes I absolutely agree pragma is a great way to bisect and will work in many 
> scenarios, and the result is clear once the bisection is done. But like 
> @srhines said, doing this one function at a time is preferable but not always 
> possible (or maybe we can wrap consecutive functions within the same pragma 
> push/pop pair? I tried to verify but ran into error: attribute 
> 'uninitialized' is not supported by '#pragma clang attribute), and automating 
> it is more difficult. From this perspective, I think the proposed patch is 
> complementary to pragma and not meant to replace it.


Right, support needs to be added, and I was wondering if you'd want to do this 
because it seems like a tool that would help you out.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

In D77168#1955312 , @jfb wrote:

> Do you not think `pragma` is a more general approach? That's what's used in a 
> bunch of other cases, and I'd like to see it attempted here.


Yes I absolutely agree pragma is a great way to bisect and will work in many 
scenarios, and the result is clear once the bisection is done. But like 
@srhines said, doing this one function at a time is preferable but not always 
possible (or maybe we can wrap consecutive functions within the same pragma 
push/pop pair? I tried to verify but ran into error: attribute 'uninitialized' 
is not supported by '#pragma clang attribute), and automating it is more 
difficult. From this perspective, I think the proposed patch is complementary 
to pragma and not meant to replace it.

> If not, I agree with John that just counting up isn't a good bisection 
> experience. I'd rather see a begin / end bound.

Thank you for the feedback! I will start looking into it.

> You're also missing the auto-init in `initializeAlloca`.

Thanks I will address this in next patch.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread John McCall via Phabricator via cfe-commits
rjmccall added inline comments.



Comment at: clang/lib/CodeGen/CGDecl.cpp:1814
+if (StopAfter) {
+  static unsigned Counter = 0;
+  if (Counter >= StopAfter)

jcai19 wrote:
> rjmccall wrote:
> > srhines wrote:
> > > MaskRay wrote:
> > > > I am a bit worried about the static variable. This makes CodeGen not 
> > > > reusable.
> > > The counter could exist in ASTContext instead.
> > IRGenModule would be the more appropriate place.
> I can't seem to find IRGenModule. Do you mean CodeGenModule by any chance? 
> Thanks.
Yes, sorry.  The Clang and Swift frontends use slightly different names for the 
same concept.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread Stephen Hines via Phabricator via cfe-commits
srhines added a comment.

In D77168#1955312 , @jfb wrote:

> Do you not think `pragma` is a more general approach? That's what's used in a 
> bunch of other cases, and I'd like to see it attempted here.
>  If not, I agree with John that just counting up isn't a good bisection 
> experience. I'd rather see a begin / end bound.


I agree that begin/end is actually better.

My experience has been that you really don't want to be modifying source files 
if you can avoid it, as that is harder to automate in a script. And you do want 
to script this kind of debugging because it isn't a compiler crash, it's often 
a runtime issue that might take longer to reproduce/etc., so I think that it is 
preferable to remove the human (error prone) element of adding/tweaking 
pragmas. I did say that the pragmas are useful in other ways, but I don't think 
that it is effective for everything.

> You're also missing the auto-init in `initializeAlloca`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

Do you not think `pragma` is a more general approach? That's what's used in a 
bunch of other cases, and I'd like to see it attempted here.
If not, I agree with John that just counting up isn't a good bisection 
experience. I'd rather see a begin / end bound.

You're also missing the auto-init in `initializeAlloca`.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread Jian Cai via Phabricator via cfe-commits
jcai19 marked 4 inline comments as done.
jcai19 added inline comments.



Comment at: clang/lib/CodeGen/CGDecl.cpp:1814
+if (StopAfter) {
+  static unsigned Counter = 0;
+  if (Counter >= StopAfter)

rjmccall wrote:
> srhines wrote:
> > MaskRay wrote:
> > > I am a bit worried about the static variable. This makes CodeGen not 
> > > reusable.
> > The counter could exist in ASTContext instead.
> IRGenModule would be the more appropriate place.
I can't seem to find IRGenModule. Do you mean CodeGenModule by any chance? 
Thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

> For example, won't you need some way of figuring out what the 1,134th 
> variable in a file was after you've successfully bisected?

Once we know the Xth variable is causing issues, we can compare the IR or 
assembly of -fvar-auto-init-stop-after=X-1 and -fvar-auto-init-stop-after=X to 
figure out which variable it is, or I can print out the variable name and the 
enclosing function if it is preferred.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 254242.
jcai19 added a comment.

Remove an unnecessary line.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -571,8 +571,8 @@
 // CHECK-RECORD-GCC-SWITCHES-ESCAPED: "-record-command-line" "{{.+}}with\\ spaces{{.+}}"
 
 // RUN: %clang -### -S -ftrivial-auto-var-init=uninitialized %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-UNINIT %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=pattern %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
 // RUN: %clang -### -S -ftrivial-auto-var-init=zero %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-BAD %s
 // CHECK-TRIVIAL-UNINIT-NOT: hasn't been enabled
 // CHECK-TRIVIAL-PATTERN-NOT: hasn't been enabled
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo () {
+  long a = 888;
+  S arr1[ARRLEN], arr2[ARRLEN];
+// PATTERN-STOP-AFTER-2-NOT: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// PATTERN-STOP-AFTER-2-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 -86, i64 80, i1 false)
+// PATTERN-STOP-AFTER-3: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// PATTERN-STOP-AFTER-3: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 -86, i64 80, i1 false)
+
+// ZERO-STOP-AFTER-2-NOT: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// ZERO-STOP-AFTER-2-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 0, i64 80, i1 false)
+// ZERO-STOP-AFTER-3: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// ZERO-STOP-AFTER-3: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 0, i64 80, i1 false)
+
+  return 0;
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3273,6 +3273,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3088,6 +3088,13 @@
 CmdArgs.push_back(
 Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
   }
+
+  if (Arg *A = Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) {
+A->claim();
+StringRef Val = A->getValue();
+CmdArgs.push_back(
+Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val));
+  }
 }
 
 static void RenderOpenCLOptions(const ArgList , ArgStringList ) {
Index: 

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-04-01 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 254240.
jcai19 added a comment.

Address some of the concerns.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/AST/ASTContext.h
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -571,8 +571,8 @@
 // CHECK-RECORD-GCC-SWITCHES-ESCAPED: "-record-command-line" "{{.+}}with\\ spaces{{.+}}"
 
 // RUN: %clang -### -S -ftrivial-auto-var-init=uninitialized %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-UNINIT %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=pattern %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
 // RUN: %clang -### -S -ftrivial-auto-var-init=zero %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-BAD %s
 // CHECK-TRIVIAL-UNINIT-NOT: hasn't been enabled
 // CHECK-TRIVIAL-PATTERN-NOT: hasn't been enabled
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo () {
+  long a = 888;
+  S arr1[ARRLEN], arr2[ARRLEN];
+// PATTERN-STOP-AFTER-2-NOT: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// PATTERN-STOP-AFTER-2-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 -86, i64 80, i1 false)
+// PATTERN-STOP-AFTER-3: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// PATTERN-STOP-AFTER-3: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 -86, i64 80, i1 false)
+
+// ZERO-STOP-AFTER-2-NOT: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// ZERO-STOP-AFTER-2-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 0, i64 80, i1 false)
+// ZERO-STOP-AFTER-3: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// ZERO-STOP-AFTER-3: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 0, i64 80, i1 false)
+
+  return 0;
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3273,6 +3273,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3088,6 +3088,13 @@
 CmdArgs.push_back(
 Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
   }
+
+  if (Arg *A = Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) {
+A->claim();
+StringRef Val = A->getValue();
+CmdArgs.push_back(
+Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val));
+  }
 }
 
 static void RenderOpenCLOptions(const ArgList , ArgStringList ) {
Index: 

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-03-31 Thread John McCall via Phabricator via cfe-commits
rjmccall added a comment.

I can understand the automation benefits of just counting the number of 
variables initialized so far in the translation unit without having to modify 
source, but if you can possibly do this with the pragma, that seems like both a 
more flexible tool (you can isolate multiple variables) and one that will 
produce more easily-interpretable results.  For example, won't you need some 
way of figuring out what the 1,134th variable in a file was after you've 
successfully bisected?




Comment at: clang/lib/CodeGen/CGDecl.cpp:1814
+if (StopAfter) {
+  static unsigned Counter = 0;
+  if (Counter >= StopAfter)

srhines wrote:
> MaskRay wrote:
> > I am a bit worried about the static variable. This makes CodeGen not 
> > reusable.
> The counter could exist in ASTContext instead.
IRGenModule would be the more appropriate place.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-03-31 Thread Stephen Hines via Phabricator via cfe-commits
srhines added a comment.

In D77168#1953635 , @jfb wrote:

> I'm not sure this is a good idea at all. We want to keep the codepath as 
> simple as possible to avoid introducing bugs. If a codebase sees a crash then 
> it's easier to bisect one function at a time than doing something like this. 
> I'd much rather see bisection using pragma to apply the `uninitialized` 
> attribute to multiple declarations.


Certainly one function at a time is preferable, but that's not always possible. 
I do think that this would be a useful feature for making bisection easier, 
since automating the injection of `pragma`s into code seems a lot more 
challenging. Of course, the `pragma` approach is nice for other reasons 
(letting people carve out entire functions for initialized/uninitialized, but I 
think that is orthogonal to the debugging aspect.




Comment at: clang/lib/CodeGen/CGDecl.cpp:1814
+if (StopAfter) {
+  static unsigned Counter = 0;
+  if (Counter >= StopAfter)

MaskRay wrote:
> I am a bit worried about the static variable. This makes CodeGen not reusable.
The counter could exist in ASTContext instead.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-03-31 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

I'd much rather see folks bisect using something like:

  void use(void*);
  
  #pragma clang attribute push ([[clang::uninitialized]], apply_to = variable)
  void buggy() {
  int arr[256];
  int boom;
  float bam;
  struct { int oops; } oops;
  union { int oof; float a; } oof;
  
  use();
  use();
  use();
  use();
  use();
  }
  #pragma clang attribute pop

This should be easy to add support for.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-03-31 Thread Jian Cai via Phabricator via cfe-commits
jcai19 added a comment.

Agreed, but having this option may save quite some time when bisecting files 
with many functions and stack variables, like this one 
.
 It will be much easier to write a script that automates the bisection process. 
What do you think? Thanks.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-03-31 Thread JF Bastien via Phabricator via cfe-commits
jfb added a comment.

I'm not sure this is a good idea at all. We want to keep the codepath as simple 
as possible to avoid introducing bugs. If a codebase sees a crash then it's 
easier to bisect one function at a time than doing something like this. I'd 
much rather see bisection using pragma to apply the `uninitialized` attribute 
to multiple declarations.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-03-31 Thread Fangrui Song via Phabricator via cfe-commits
MaskRay added inline comments.



Comment at: clang/lib/CodeGen/CGDecl.cpp:1814
+if (StopAfter) {
+  static unsigned Counter = 0;
+  if (Counter >= StopAfter)

I am a bit worried about the static variable. This makes CodeGen not reusable.



Comment at: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp:1
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fblocks 
-ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s 
-emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fblocks 
-ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s 
-emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3

You can drop `-fblocks`, because the test has nothing to do with the Blocks 
extension.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168



___
cfe-commits mailing list
cfe-commits@lists.llvm.org
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-03-31 Thread Jian Cai via Phabricator via cfe-commits
jcai19 updated this revision to Diff 254019.
jcai19 added a comment.

Add test cases.


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D77168/new/

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp
  clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
  clang/test/Driver/clang_f_opts.c

Index: clang/test/Driver/clang_f_opts.c
===
--- clang/test/Driver/clang_f_opts.c
+++ clang/test/Driver/clang_f_opts.c
@@ -571,8 +571,8 @@
 // CHECK-RECORD-GCC-SWITCHES-ESCAPED: "-record-command-line" "{{.+}}with\\ spaces{{.+}}"
 
 // RUN: %clang -### -S -ftrivial-auto-var-init=uninitialized %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-UNINIT %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=pattern %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
-// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-PATTERN %s
+// RUN: %clang -### -S -ftrivial-auto-var-init=zero -enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang -ftrivial-auto-var-init-stop-after=1 %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-GOOD %s
 // RUN: %clang -### -S -ftrivial-auto-var-init=zero %s 2>&1 | FileCheck -check-prefix=CHECK-TRIVIAL-ZERO-BAD %s
 // CHECK-TRIVIAL-UNINIT-NOT: hasn't been enabled
 // CHECK-TRIVIAL-PATTERN-NOT: hasn't been enabled
Index: clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
===
--- /dev/null
+++ clang/test/CodeGenCXX/auto-var-init-stop-after.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fblocks -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fblocks -ftrivial-auto-var-init=pattern -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=PATTERN-STOP-AFTER-3
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fblocks -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=2 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-2
+// RUN: %clang_cc1 -triple x86_64-unknown-unknown -fblocks -ftrivial-auto-var-init=zero -ftrivial-auto-var-init-stop-after=3 %s -emit-llvm -o - | FileCheck %s -check-prefix=ZERO-STOP-AFTER-3
+
+#define ARRLEN 10
+
+typedef struct {
+  int i;
+  char c;
+} S;
+
+int foo () {
+  long a = 888;
+  S arr1[ARRLEN], arr2[ARRLEN];
+// PATTERN-STOP-AFTER-2-NOT: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// PATTERN-STOP-AFTER-2-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 -86, i64 80, i1 false)
+// PATTERN-STOP-AFTER-3: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// PATTERN-STOP-AFTER-3: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 -86, i64 80, i1 false)
+
+// ZERO-STOP-AFTER-2-NOT: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// ZERO-STOP-AFTER-2-NOT: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 0, i64 80, i1 false)
+// ZERO-STOP-AFTER-3: %1 = bitcast [10 x %struct.S]* %arr2 to i8*
+// ZERO-STOP-AFTER-3: call void @llvm.memset.p0i8.i64(i8* align 16 %1, i8 0, i64 80, i1 false)
+
+  return 0;
+}
Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3273,6 +3273,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3088,6 +3088,13 @@
 CmdArgs.push_back(
 Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
   }
+
+  if (Arg *A = Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) {
+A->claim();
+StringRef Val = A->getValue();
+CmdArgs.push_back(
+Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val));
+  }
 }
 
 static void RenderOpenCLOptions(const ArgList , ArgStringList ) {
Index: clang/lib/CodeGen/CGDecl.cpp

[PATCH] D77168: Add a flag to debug automatic variable initialization

2020-03-31 Thread Jian Cai via Phabricator via cfe-commits
jcai19 created this revision.
Herald added a project: clang.
Herald added a subscriber: cfe-commits.

Add -ftrivial-auto-var-init-stop-after= to limit the number of times
stack variables are initialized when -ftrivial-auto-var-init= is used to
initialize stack variables to zero or a pattern. This flag can be used
to bisect uninitialized uses of a stack variable exposed by automatic
variable initialization, such as http://crrev.com/c/2020401.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D77168

Files:
  clang/include/clang/Basic/LangOptions.def
  clang/include/clang/Driver/Options.td
  clang/lib/CodeGen/CGDecl.cpp
  clang/lib/Driver/ToolChains/Clang.cpp
  clang/lib/Frontend/CompilerInvocation.cpp


Index: clang/lib/Frontend/CompilerInvocation.cpp
===
--- clang/lib/Frontend/CompilerInvocation.cpp
+++ clang/lib/Frontend/CompilerInvocation.cpp
@@ -3273,6 +3273,11 @@
   Diags.Report(diag::err_drv_invalid_value) << A->getAsString(Args) << Val;
   }
 
+  if (Arg *A = Args.getLastArg(OPT_ftrivial_auto_var_init_stop_after)) {
+unsigned Val = (unsigned)std::stoi(A->getValue());
+Opts.TrivialAutoVarInitStopAfter = Val;
+  }
+
   // Parse -fsanitize= arguments.
   parseSanitizerKinds("-fsanitize=", Args.getAllArgValues(OPT_fsanitize_EQ),
   Diags, Opts.Sanitize);
Index: clang/lib/Driver/ToolChains/Clang.cpp
===
--- clang/lib/Driver/ToolChains/Clang.cpp
+++ clang/lib/Driver/ToolChains/Clang.cpp
@@ -3088,6 +3088,13 @@
 CmdArgs.push_back(
 Args.MakeArgString("-ftrivial-auto-var-init=" + TrivialAutoVarInit));
   }
+
+  if (Arg *A = 
Args.getLastArg(options::OPT_ftrivial_auto_var_init_stop_after)) {
+A->claim();
+StringRef Val = A->getValue();
+CmdArgs.push_back(
+Args.MakeArgString("-ftrivial-auto-var-init-stop-after=" + Val));
+  }
 }
 
 static void RenderOpenCLOptions(const ArgList , ArgStringList ) {
Index: clang/lib/CodeGen/CGDecl.cpp
===
--- clang/lib/CodeGen/CGDecl.cpp
+++ clang/lib/CodeGen/CGDecl.cpp
@@ -1809,6 +1809,19 @@
 LangOptions::TrivialAutoVarInitKind::Uninitialized)
   return;
 
+unsigned StopAfter = 
getContext().getLangOpts().TrivialAutoVarInitStopAfter;
+if (StopAfter) {
+  static unsigned Counter = 0;
+  if (Counter >= StopAfter)
+return;
+  if (Counter++ == StopAfter - 1) {
+const DeclContext * CD = D.getParentFunctionOrMethod ();
+std::string FunctionName = 
(cast(CD))->getNameInfo().getAsString();
+StringRef SourceFileName = CGM.getModule().getSourceFileName();
+llvm::errs() << "The last variable initialized is " << D.getName() << 
" of function " << FunctionName << " in " << SourceFileName << "\n";
+  }
+}
+
 // Only initialize a __block's storage: we always initialize the header.
 if (emission.IsEscapingByRef && !locIsByrefHeader)
   Loc = emitBlockByrefAddress(Loc, , /*follow=*/false);
Index: clang/include/clang/Driver/Options.td
===
--- clang/include/clang/Driver/Options.td
+++ clang/include/clang/Driver/Options.td
@@ -1799,6 +1799,8 @@
 def enable_trivial_var_init_zero : Flag<["-"], 
"enable-trivial-auto-var-init-zero-knowing-it-will-be-removed-from-clang">,
   Flags<[CC1Option, CoreOption]>,
   HelpText<"Trivial automatic variable initialization to zero is only here for 
benchmarks, it'll eventually be removed, and I'm OK with that because I'm only 
using it to benchmark">;
+def ftrivial_auto_var_init_stop_after : Joined<["-"], 
"ftrivial-auto-var-init-stop-after=">, Group,
+  Flags<[CC1Option, CoreOption]>, HelpText<"Stop initializing trivial 
automatic stack variables after the specified number of instances">;
 def fstandalone_debug : Flag<["-"], "fstandalone-debug">, Group, 
Flags<[CoreOption]>,
   HelpText<"Emit full debug info for all types used by the program">;
 def fno_standalone_debug : Flag<["-"], "fno-standalone-debug">, 
Group, Flags<[CoreOption]>,
Index: clang/include/clang/Basic/LangOptions.def
===
--- clang/include/clang/Basic/LangOptions.def
+++ clang/include/clang/Basic/LangOptions.def
@@ -293,6 +293,8 @@
  "stack protector mode")
 ENUM_LANGOPT(TrivialAutoVarInit, TrivialAutoVarInitKind, 2, 
TrivialAutoVarInitKind::Uninitialized,
  "trivial automatic variable initialization")
+VALUE_LANGOPT(TrivialAutoVarInitStopAfter, 32, 0,
+ "stop trivial automatic variable initialization after the 
specified number of instances")
 ENUM_LANGOPT(SignedOverflowBehavior, SignedOverflowBehaviorTy, 2, 
SOB_Undefined,
  "signed integer overflow handling")
 


Index: clang/lib/Frontend/CompilerInvocation.cpp