[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 211988.
emmettneyman marked 6 inline comments as done.
emmettneyman added a comment.

Addressed comments and feedback


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-requires-designator.cpp
  clang/test/SemaCXX/attr-requires-init.cpp

Index: clang/test/SemaCXX/attr-requires-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-requires-init.cpp
@@ -0,0 +1,86 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+[[clang::requires_init]] int x;// expected-warning{{'requires_init' attribute only applies to non-static data members}}
+[[clang::requires_init]] void fun(int x) { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  return;
+}
+struct [[clang::requires_init]] Foo { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  int x;
+};
+
+// Struct with one required field
+struct Bar {
+  [[clang::requires_init]] int y; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;// expected-warning{{initializer for variable b1 must explicitly initialize field y}}
+Bar b2{};  // expected-warning{{initializer for variable b2 must explicitly initialize field y}}
+Bar b3{1}; // expected-warning{{initializer for variable b3 must explicitly initialize field y}}
+
+// The following are valid ways of initializing instances of this struct.
+Bar b6{.y = 1};
+
+// Struct with multiple required fields
+struct Baz {
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+  [[clang::requires_init]] int z; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Baz z1; // expected-warning{{initializer for variable z1 must explicitly initialize field x}} expected-warning{{initializer for variable z1 must explicitly initialize field z}}
+Baz z2{};   // expected-warning{{initializer for variable z2 must explicitly initialize field x}} expected-warning{{initializer for variable z2 must explicitly initialize field z}}
+Baz z3{1, 2};   // expected-warning{{initializer for variable z3 must explicitly initialize field x}} expected-warning{{initializer for variable z3 must explicitly initialize field z}}
+Baz z4{1, 2, 3};// expected-warning{{initializer for variable z4 must explicitly initialize field x}} expected-warning{{initializer for variable z4 must explicitly initialize field z}}
+Baz z5{.x = 1, 2};  // expected-warning{{initializer for variable z5 must explicitly initialize field z}}
+Baz z6{.x = 1, .y = 2}; // expected-warning{{initializer for variable z6 must explicitly initialize field z}}
+
+// The following are valid ways of initializing instances of this struct.
+Baz z7{.x = 1, .y = 2, .z = 3};
+Baz z8{.x = 1, .z = 3};
+Baz z9{.x = 1, 2, .z = 3};
+
+// The requires_init attribute can also be applied to public fields of classes.
+class Cla {
+public:
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;// expected-warning{{initializer for variable c1 must explicitly initialize field x}}
+Cla c2{};  // expected-warning{{initializer for variable c2 must explicitly initialize field x}}
+Cla c3{1}; // expected-warning{{initializer for variable c3 must explicitly initialize field x}}
+Cla c4{1, 2};  // expected-warning{{initializer for variable c4 must explicitly initialize field x}}
+Cla c5{1, .y = 2}; // expected-warning{{initializer for variable c5 must explicitly initialize field x}}
+
+// The following are valid ways of initializing instances of this class.
+Cla c6{.x = 1};
+Cla c7{.x = 1, .y = 2};
+Cla c8{.x = 1, 2};
+
+// This attribute cannot be applied to fields of a union.
+union Uni {
+  [[clang::requires_init]] int x; // expected-warning{{'requires_init' attribute ignored}}
+  int y;
+};
+
+// If any of the fields in the record are non-public all requires_init
+// attributes in the record are ignored.
+struct PriMems {
+  [[clang::requires_init]] int x; // expected-warning{{'requires_init' attribute ignored}}
+private:
+  int y;
+};
+PriMems pm1;
+PriMems pm2();
+
+class PriClass {
+  int x;
+public:
+  

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman marked 16 inline comments as done.
emmettneyman added inline comments.



Comment at: clang/include/clang/Basic/Attr.td:1948
+def RequiresDesignator : InheritableAttr {
+  let Spellings = [Clang<"requires_designator">, GCC<"designated_init">];
+  let Subjects = SubjectList<[Record]>;

aaron.ballman wrote:
> Why does this have a `Clang` spelling in addition to the `GCC` spelling? I 
> think it only needs the `GCC` spelling.
I kept the `Clang` spelling so that there's naming consistency between the two 
attributes (`requires_designator` and `requires_init`). I can remove it if it's 
redundant/unnecessary, let me know.



Comment at: clang/include/clang/Basic/AttrDocs.td:1484-1485
+requires designated initializers be used rather than positional initializers.
+This attribute additionally has a stronger restriction that declarations must 
be
+brace-initialized (which is why ``Foo foo;`` is considered invalid above. The
+final way this attribute differs from GCC's ``designated_init`` attribute is

aaron.ballman wrote:
> Why is it valuable to differ from GCC's behavior in this regard?
The motivation behind GCC's attribute seems to be to avoid using positional 
arguments:
> The intent of this attribute is to allow the programmer to indicate that a 
> structure’s layout may change, and that therefore relying on positional 
> initialization will result in future breakage.

The motivation behind this attribute is a little bit different. Instead of 
avoiding the use of positional arguments, the goal of this attribute is to 
bring ObjC-style named parameters to C/C++ and to be able to enforce their use 
in certain situations. In line with this goal, we wanted to forbid the 
following pattern:
```
Foo foo;
foo.x = 42;
foo.y = 36;
```
That's why this attribute enforces that all declarations be brace-initialized.



Comment at: clang/include/clang/Basic/AttrDocs.td:1487
+final way this attribute differs from GCC's ``designated_init`` attribute is
+that it can be applied to union and class types, as well as struct types.
+  }];

aaron.ballman wrote:
> Given that it can be added to class types, I wonder what the behavior should 
> be for code like:
> ```
> struct Foo {
>   int a = 1;
>   int b, c;
>   int d = 4;
> };
> 
> Foo foo = {...};
> ```
> Does the initializer for `foo` have to specify `.a` and `.d`?
The `requires_designator` attribute doesn't require that any of the fields have 
to be specified, only that designated initializer syntax has to be used. So, 
for the struct definition above, any of the following are valid:
```
Foo foo {};
Foo foo {.a = 1, .b = 2, .c = 3, .d = 4};
Foo foo {.b = 2, .c = 3};
```
Any of the fields can be left out and default-initialized. I've added this 
example to the lit tests for the attribute.



Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:3537
+def note_declared_requires_designator_here : Note<
+  "required by 'requires_designator' attribute here">;
+def warn_requires_init_failed : Warning<

aaron.ballman wrote:
> This attribute spelling seems wrong, it should be `designated_init`, no?
Does it make more sense to use `designated_init` or `requires_designator` as 
the primary spelling for this attribute? I chose to use the 
`requires_designator` spelling so that the two attributes have consistent and 
similar spellings. Let me know if you think it should be the other way around 
(or if you want the `Clang` spelling removed entirely).



Comment at: clang/lib/Sema/SemaDecl.cpp:11216
+// If the type of the declaration is a struct/class and that type has the
+// require_designated_init attribute, check that the initializer has
+// the proper designated initializer syntax.

aaron.ballman wrote:
> Attribute spelling is stale in this comment.
See comments above about keeping this spelling.



Comment at: clang/lib/Sema/SemaDecl.cpp:15314
 
+  // If this TagDecl has any non-public fields, all requires_designator and
+  // requires_init attributes should be ignored.

aaron.ballman wrote:
> Attribute name is stale in this comment.
> 
> This is the wrong place to do this work -- it should be done from 
> SemaDeclAttr.cpp when processing the attribute. We should avoid adding the 
> attribute in the first place rather than adding the attribute and then later 
> removing it.
See question above regarding attribute spelling.

I thought about this a lot because I agree, it feels wrong to remove the 
attribute once it is already added to a field, struct, etc. However I could not 
think of any other way or any other place to do this check. Since all fields of 
the struct need to have been processed already in order to check whether there 
are any non-public members, it seems like the only place to do this check is at 
the end of handling the entire `TagDecl`. When the 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-18 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 210649.
emmettneyman added a comment.

Created diagnostic group, added behavior and documentation for private fields, 
added documentation regarding GCC attribute


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticGroups.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-requires-designator.cpp
  clang/test/SemaCXX/attr-requires-init.cpp

Index: clang/test/SemaCXX/attr-requires-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-requires-init.cpp
@@ -0,0 +1,86 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+[[clang::requires_init]] int x;// expected-warning{{'requires_init' attribute only applies to non-static data members}}
+[[clang::requires_init]] void fun(int x) { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  return;
+}
+struct [[clang::requires_init]] Foo { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  int x;
+};
+
+// Struct with one required field
+struct Bar {
+  [[clang::requires_init]] int y; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;// expected-warning{{initializer for variable b1 must explicitly initialize field y}}
+Bar b2{};  // expected-warning{{initializer for variable b2 must explicitly initialize field y}}
+Bar b3{1}; // expected-warning{{initializer for variable b3 must explicitly initialize field y}}
+
+// The following are valid ways of initializing instances of this struct.
+Bar b6{.y = 1};
+
+// Struct with multiple required fields
+struct Baz {
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+  [[clang::requires_init]] int z; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Baz z1; // expected-warning{{initializer for variable z1 must explicitly initialize field x}} expected-warning{{initializer for variable z1 must explicitly initialize field z}}
+Baz z2{};   // expected-warning{{initializer for variable z2 must explicitly initialize field x}} expected-warning{{initializer for variable z2 must explicitly initialize field z}}
+Baz z3{1, 2};   // expected-warning{{initializer for variable z3 must explicitly initialize field x}} expected-warning{{initializer for variable z3 must explicitly initialize field z}}
+Baz z4{1, 2, 3};// expected-warning{{initializer for variable z4 must explicitly initialize field x}} expected-warning{{initializer for variable z4 must explicitly initialize field z}}
+Baz z5{.x = 1, 2};  // expected-warning{{initializer for variable z5 must explicitly initialize field z}}
+Baz z6{.x = 1, .y = 2}; // expected-warning{{initializer for variable z6 must explicitly initialize field z}}
+
+// The following are valid ways of initializing instances of this struct.
+Baz z7{.x = 1, .y = 2, .z = 3};
+Baz z8{.x = 1, .z = 3};
+Baz z9{.x = 1, 2, .z = 3};
+
+// The requires_init attribute can also be applied to public fields of classes.
+class Cla {
+public:
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;// expected-warning{{initializer for variable c1 must explicitly initialize field x}}
+Cla c2{};  // expected-warning{{initializer for variable c2 must explicitly initialize field x}}
+Cla c3{1}; // expected-warning{{initializer for variable c3 must explicitly initialize field x}}
+Cla c4{1, 2};  // expected-warning{{initializer for variable c4 must explicitly initialize field x}}
+Cla c5{1, .y = 2}; // expected-warning{{initializer for variable c5 must explicitly initialize field x}}
+
+// The following are valid ways of initializing instances of this class.
+Cla c6{.x = 1};
+Cla c7{.x = 1, .y = 2};
+Cla c8{.x = 1, 2};
+
+// This attribute cannot be applied to fields of a union.
+union Uni {
+  [[clang::requires_init]] int x; // expected-warning{{'requires_init' attribute ignored}}
+  int y;
+};
+
+// If any of the fields in the record are non-public all requires_init
+// attributes in the record are ignored.
+struct PriMems {
+  [[clang::requires_init]] int x; // expected-warning{{'requires_init' attribute ignored}}
+private:
+  int y;
+};
+PriMems pm1;
+PriMems pm2();
+
+class 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-16 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman planned changes to this revision.
emmettneyman added a comment.

Changes planned:

- Move diagnostics into a diagnostic group.
- Add behavior and test cases for private members of structs/classes.
- Investigate behavior of GCC `designated_init` attribute


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380



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


[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-11 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/include/clang/Basic/AttrDocs.td:1448
 
+def RequireDesignatedInitDocs : Documentation {
+  let Category = DocCatType;

aaron.ballman wrote:
> The behavior should be documented as to what happens when you apply these 
> attributes to unions.
For now, adding a `requires_init` attribute to a field of a union will result 
in a warning that the attribute is ignored. In the future, I might submit 
another patch in the future adding behavior for this attribute for fields of a 
union.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380



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


[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-11 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 209362.
emmettneyman marked 2 inline comments as done.
emmettneyman added a comment.

Added documentation about using the attributes with unions.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-requires-designator.cpp
  clang/test/SemaCXX/attr-requires-init.cpp

Index: clang/test/SemaCXX/attr-requires-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-requires-init.cpp
@@ -0,0 +1,67 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+[[clang::requires_init]] int x;// expected-warning{{'requires_init' attribute only applies to non-static data members}}
+[[clang::requires_init]] void fun(int x) { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  return;
+}
+struct [[clang::requires_init]] Foo { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  int x;
+};
+
+// Struct with one required field
+struct Bar {
+  [[clang::requires_init]] int y; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;// expected-warning{{initializer for variable b1 must explicitly initialize field y}}
+Bar b2{};  // expected-warning{{initializer for variable b2 must explicitly initialize field y}}
+Bar b3{1}; // expected-warning{{initializer for variable b3 must explicitly initialize field y}}
+
+// The following are valid ways of initializing instances of this struct.
+Bar b6{.y = 1};
+
+// Struct with multiple required fields
+struct Baz {
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+  [[clang::requires_init]] int z; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Baz z1; // expected-warning{{initializer for variable z1 must explicitly initialize field x}} expected-warning{{initializer for variable z1 must explicitly initialize field z}}
+Baz z2{};   // expected-warning{{initializer for variable z2 must explicitly initialize field x}} expected-warning{{initializer for variable z2 must explicitly initialize field z}}
+Baz z3{1, 2};   // expected-warning{{initializer for variable z3 must explicitly initialize field x}} expected-warning{{initializer for variable z3 must explicitly initialize field z}}
+Baz z4{1, 2, 3};// expected-warning{{initializer for variable z4 must explicitly initialize field x}} expected-warning{{initializer for variable z4 must explicitly initialize field z}}
+Baz z5{.x = 1, 2};  // expected-warning{{initializer for variable z5 must explicitly initialize field z}}
+Baz z6{.x = 1, .y = 2}; // expected-warning{{initializer for variable z6 must explicitly initialize field z}}
+
+// The following are valid ways of initializing instances of this struct.
+Baz z7{.x = 1, .y = 2, .z = 3};
+Baz z8{.x = 1, .z = 3};
+Baz z9{.x = 1, 2, .z = 3};
+
+// The required attribute can also be applied to public fields of classes.
+class Cla {
+public:
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;// expected-warning{{initializer for variable c1 must explicitly initialize field x}}
+Cla c2{};  // expected-warning{{initializer for variable c2 must explicitly initialize field x}}
+Cla c3{1}; // expected-warning{{initializer for variable c3 must explicitly initialize field x}}
+Cla c4{1, 2};  // expected-warning{{initializer for variable c4 must explicitly initialize field x}}
+Cla c5{1, .y = 2}; // expected-warning{{initializer for variable c5 must explicitly initialize field x}}
+
+// The following are valid ways of initializing instances of this class.
+Cla c6{.x = 1};
+Cla c7{.x = 1, .y = 2};
+Cla c8{.x = 1, 2};
+
+// This attribute cannot be applied to fields of a union.
+union Uni {
+  [[clang::requires_init]] int x; // expected-warning{{'requires_init' attribute ignored}}
+  int y;
+};
Index: clang/test/SemaCXX/attr-requires-designator.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-requires-designator.cpp
@@ -0,0 +1,102 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// The requires_designator attribute only applies to types. It will
+// generate a warning when attached to 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-11 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman marked 3 inline comments as done.
emmettneyman added inline comments.



Comment at: clang/include/clang/Basic/Attr.td:1948
+def RequiresDesignator : InheritableAttr {
+  let Spellings = [Clang<"requires_designator">];
+  let Subjects = SubjectList<[Record]>;

compnerd wrote:
> aaron.ballman wrote:
> > Hmm, after making this suggestion, I noticed that GCC seems to support a 
> > similar attribute named `designated_init` 
> > (https://gcc.gnu.org/onlinedocs/gcc/Common-Type-Attributes.html#Common-Type-Attributes).
> >  Was your goal to support the same thing GCC supported?
> `designated_init` is a suggestion, the original patch seemed to be a stronger 
> version.  I think that we should be supporting the GNU spelling for 
> `designated_init` and can support a Clang spelling of 
> `requires_designated_init` if the goal is to have the stronger guarantee that 
> this *must* happen.
I hadn't known about the GCC attribute until now. Yes, the 
`requires_designator` (originally `require_designated_init`) attribute wants to 
enforce the same thing I believe. I couldn't find more documentation for the 
GCC `designated_init` attribute so it's a little tough to tell whether the 
behavior is the exact same. The attribute in this patch allows a field to be 
default constructed (unless the other attribute is applied to that specific 
field) but enforces that a brace initializer must be used. So `Foo foo {};` 
would be valid (every field is default constructed) but `Foo foo;` would not be 
valid. I'm not sure if that's the same behavior the GCC attribute is trying to 
enforce. But on a high level, both are trying to prohibit using positional args 
when declaring a struct.

@compnerd I don't mind this attribute generating warnings rather than errors. 
It's ok for this attribute to be a "suggestion" as well. Like @aaron.ballman 
mentioned, "users can always use -Werror to strengthen their own requirements."


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380



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


[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-11 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 209256.
emmettneyman added a comment.

Small changes: adding `auto`


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-requires-designator.cpp
  clang/test/SemaCXX/attr-requires-init.cpp

Index: clang/test/SemaCXX/attr-requires-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-requires-init.cpp
@@ -0,0 +1,61 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+[[clang::requires_init]] int x;// expected-warning{{'requires_init' attribute only applies to non-static data members}}
+[[clang::requires_init]] void fun(int x) { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  return;
+}
+struct [[clang::requires_init]] Foo { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  int x;
+};
+
+// Struct with one required field
+struct Bar {
+  [[clang::requires_init]] int y; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;// expected-warning{{initializer for variable b1 must explicitly initialize field y}}
+Bar b2{};  // expected-warning{{initializer for variable b2 must explicitly initialize field y}}
+Bar b3{1}; // expected-warning{{initializer for variable b3 must explicitly initialize field y}}
+
+// The following are valid ways of initializing instances of this struct.
+Bar b6{.y = 1};
+
+// Struct with multiple required fields
+struct Baz {
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+  [[clang::requires_init]] int z; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Baz z1; // expected-warning{{initializer for variable z1 must explicitly initialize field x}} expected-warning{{initializer for variable z1 must explicitly initialize field z}}
+Baz z2{};   // expected-warning{{initializer for variable z2 must explicitly initialize field x}} expected-warning{{initializer for variable z2 must explicitly initialize field z}}
+Baz z3{1, 2};   // expected-warning{{initializer for variable z3 must explicitly initialize field x}} expected-warning{{initializer for variable z3 must explicitly initialize field z}}
+Baz z4{1, 2, 3};// expected-warning{{initializer for variable z4 must explicitly initialize field x}} expected-warning{{initializer for variable z4 must explicitly initialize field z}}
+Baz z5{.x = 1, 2};  // expected-warning{{initializer for variable z5 must explicitly initialize field z}}
+Baz z6{.x = 1, .y = 2}; // expected-warning{{initializer for variable z6 must explicitly initialize field z}}
+
+// The following are valid ways of initializing instances of this struct.
+Baz z7{.x = 1, .y = 2, .z = 3};
+Baz z8{.x = 1, .z = 3};
+Baz z9{.x = 1, 2, .z = 3};
+
+// The required attribute can also be applied to public fields of classes.
+class Cla {
+public:
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;// expected-warning{{initializer for variable c1 must explicitly initialize field x}}
+Cla c2{};  // expected-warning{{initializer for variable c2 must explicitly initialize field x}}
+Cla c3{1}; // expected-warning{{initializer for variable c3 must explicitly initialize field x}}
+Cla c4{1, 2};  // expected-warning{{initializer for variable c4 must explicitly initialize field x}}
+Cla c5{1, .y = 2}; // expected-warning{{initializer for variable c5 must explicitly initialize field x}}
+
+// The following are valid ways of initializing instances of this class.
+Cla c6{.x = 1};
+Cla c7{.x = 1, .y = 2};
+Cla c8{.x = 1, 2};
Index: clang/test/SemaCXX/attr-requires-designator.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-requires-designator.cpp
@@ -0,0 +1,102 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// The requires_designator attribute only applies to types. It will
+// generate a warning when attached to variables, functions, arrays, etc.
+[[clang::requires_designator]] int x;// expected-warning{{'requires_designator' attribute only applies to structs, unions, and classes}}
+[[clang::requires_designator]] void fun(int x) { // 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-10 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman planned changes to this revision.
emmettneyman added a comment.

Working on adding more information to the documentation and adding more 
in-depth unit tests.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380



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


[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-10 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/include/clang/Basic/DiagnosticSemaKinds.td:3533-3541
+def err_require_designated_init_failed : Error<
+  "variable declaration does not use designated initializer syntax">;
+def note_declared_required_designated_init_here : Note<
+  "required by 'require_designated_init' attribute here">;
+
+def err_required_failed : Error<
+  "initializer for variable %0 must explicitly initialize field %1">;

aaron.ballman wrote:
> I'm a little uncomfortable with these being errors rather than warnings. I 
> think of these attributes as being preferences rather than requirements; the 
> code isn't *wrong* if it fails to use the designated initializer (it's 
> conforming to C or C++, does not have UB, etc) and other implementations are 
> free to ignore those attributes and the end result will be identical to 
> what's produced by Clang.
> 
> What do you think about turning these into warnings? Users can always use 
> `-Werror` to strengthen their own requirements.
That makes sense, I'll make the change.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380



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


[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-10 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 209097.
emmettneyman marked 32 inline comments as done.
emmettneyman added a comment.

Addressed several of the comments regarding naming and style.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-requires-designator.cpp
  clang/test/SemaCXX/attr-requires-init.cpp

Index: clang/test/SemaCXX/attr-requires-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-requires-init.cpp
@@ -0,0 +1,61 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+[[clang::requires_init]] int x;// expected-warning{{'requires_init' attribute only applies to non-static data members}}
+[[clang::requires_init]] void fun(int x) { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  return;
+}
+struct [[clang::requires_init]] Foo { // expected-warning{{'requires_init' attribute only applies to non-static data members}}
+  int x;
+};
+
+// Struct with one required field
+struct Bar {
+  [[clang::requires_init]] int y; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;// expected-warning{{initializer for variable b1 must explicitly initialize field y}}
+Bar b2{};  // expected-warning{{initializer for variable b2 must explicitly initialize field y}}
+Bar b3{1}; // expected-warning{{initializer for variable b3 must explicitly initialize field y}}
+
+// The following are valid ways of initializing instances of this struct.
+Bar b6{.y = 1};
+
+// Struct with multiple required fields
+struct Baz {
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+  [[clang::requires_init]] int z; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Baz z1; // expected-warning{{initializer for variable z1 must explicitly initialize field x}} expected-warning{{initializer for variable z1 must explicitly initialize field z}}
+Baz z2{};   // expected-warning{{initializer for variable z2 must explicitly initialize field x}} expected-warning{{initializer for variable z2 must explicitly initialize field z}}
+Baz z3{1, 2};   // expected-warning{{initializer for variable z3 must explicitly initialize field x}} expected-warning{{initializer for variable z3 must explicitly initialize field z}}
+Baz z4{1, 2, 3};// expected-warning{{initializer for variable z4 must explicitly initialize field x}} expected-warning{{initializer for variable z4 must explicitly initialize field z}}
+Baz z5{.x = 1, 2};  // expected-warning{{initializer for variable z5 must explicitly initialize field z}}
+Baz z6{.x = 1, .y = 2}; // expected-warning{{initializer for variable z6 must explicitly initialize field z}}
+
+// The following are valid ways of initializing instances of this struct.
+Baz z7{.x = 1, .y = 2, .z = 3};
+Baz z8{.x = 1, .z = 3};
+Baz z9{.x = 1, 2, .z = 3};
+
+// The required attribute can also be applied to public fields of classes.
+class Cla {
+public:
+  [[clang::requires_init]] int x; // expected-note 0+ {{enforced by 'requires_init' attribute here}}
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;// expected-warning{{initializer for variable c1 must explicitly initialize field x}}
+Cla c2{};  // expected-warning{{initializer for variable c2 must explicitly initialize field x}}
+Cla c3{1}; // expected-warning{{initializer for variable c3 must explicitly initialize field x}}
+Cla c4{1, 2};  // expected-warning{{initializer for variable c4 must explicitly initialize field x}}
+Cla c5{1, .y = 2}; // expected-warning{{initializer for variable c5 must explicitly initialize field x}}
+
+// The following are valid ways of initializing instances of this class.
+Cla c6{.x = 1};
+Cla c7{.x = 1, .y = 2};
+Cla c8{.x = 1, 2};
Index: clang/test/SemaCXX/attr-requires-designator.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-requires-designator.cpp
@@ -0,0 +1,102 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+// The requires_designator attribute only applies to types. It will
+// generate a warning when attached to variables, functions, arrays, etc.
+[[clang::requires_designator]] int x;// expected-warning{{'requires_designator' attribute only applies to structs, unions, and 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman marked 2 inline comments as done.
emmettneyman added inline comments.



Comment at: clang/test/SemaCXX/attr-designated-init-required.cpp:3
+
+#define ATTR [[clang::designated_init_required]]
+

compnerd wrote:
> Why the macro?
I modeled this file after 
`test/SemaCXX/attr-require-constant-initialization.cpp` where a macro is 
defined at the top. I assume just to keep things neat.



Comment at: clang/test/SemaCXX/attr-require-designated-init.cpp:3
+
+#define ATTR [[clang::require_designated_init]]
+

compnerd wrote:
> Why the macro?
See above.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380



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


[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added a comment.

In D64380#1577350 , @compnerd wrote:

> I don't see any cases where `[[clang::required]]` is tested, am I missing 
> something?


I renamed the attribute at your suggestion. It's now called 
`[[clang::designated_init_required]`.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380



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


[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 208842.
emmettneyman added a comment.

slight change to specification reference format


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-designated-init-required.cpp
  clang/test/SemaCXX/attr-require-designated-init.cpp

Index: clang/test/SemaCXX/attr-require-designated-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-require-designated-init.cpp
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+#define ATTR [[clang::require_designated_init]]
+
+// The require_designated_init attribute only applies to types. It will
+// generate a warning when attached to variables, functions, arrays, etc.
+int ATTR x;// expected-error{{'require_designated_init' attribute cannot be applied to types}}
+void ATTR fun(int x) { // expected-error{{'require_designated_init' attribute cannot be applied to types}}
+  return;
+}
+int ATTR arr[10]; // expected-error{{'require_designated_init' attribute cannot be applied to types}}
+
+// Struct with one field with require_designated_init attribute
+struct ATTR Foo { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int a;
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Foo f1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Foo f2{1};// expected-error{{variable declaration does not use designated initializer syntax}}
+Foo f3 = {1}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this struct.
+Foo f4{};
+Foo f5 = {};
+Foo f6{.a = 1};
+Foo f7 = {.a = 1};
+
+// Struct with multiple fields wth require_designated_init attribute
+struct ATTR Bar { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int b;
+  int c;
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b2{1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b3 = {1, 2};  // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b4{.b = 1, 2};// expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b5 = {.b = 1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this struct.
+Bar b6{};
+Bar b7 = {};
+Bar b8{.b = 1};
+Bar b9 = {.b = 1};
+Bar b10{.b = 1, .c = 2};
+Bar b11 = {.b = 1, .c = 2};
+Bar b12 = {.c = 2, .b = 1};
+
+// Struct without require_designated_init attribute
+struct Baz {
+  int d;
+  int e;
+};
+
+// The following are all valid ways of initializing instances of this struct.
+Baz z1;
+Baz z2{};
+Baz z3 = {};
+Baz z4{1, 2};
+Baz z5 = {1, 2};
+Baz z6{.d = 1, .e = 2};
+Baz z7 = {.d = 1, .e = 2};
+Baz z8{1};
+Baz z9 = {1};
+Baz z10{.d = 1, 2};
+Baz z11 = {.d = 1, 2};
+
+// The require_designated_init attribute can also be attached to unions.
+union ATTR Uni { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int x;
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this union.
+Uni u1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Uni u2{1};// expected-error{{variable declaration does not use designated initializer syntax}}
+Uni u3 = {1}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this union.
+Uni u4{};
+Uni u5 = {};
+Uni u6{.x = 1};
+Uni u7 = {.x = 1};
+
+// The require_designated_init attribute can also be attached to classes.
+class ATTR Cla { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+public:
+  int x;
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c2{1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c3 = {1, 2};  // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c4{.x = 1, 2};// expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c5 = {.x = 1, 2}; // 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 208840.
emmettneyman marked an inline comment as done.
emmettneyman added a comment.

Remove cppreference link, add reference to C++ spec.


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-designated-init-required.cpp
  clang/test/SemaCXX/attr-require-designated-init.cpp

Index: clang/test/SemaCXX/attr-require-designated-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-require-designated-init.cpp
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+#define ATTR [[clang::require_designated_init]]
+
+// The require_designated_init attribute only applies to types. It will
+// generate a warning when attached to variables, functions, arrays, etc.
+int ATTR x;// expected-error{{'require_designated_init' attribute cannot be applied to types}}
+void ATTR fun(int x) { // expected-error{{'require_designated_init' attribute cannot be applied to types}}
+  return;
+}
+int ATTR arr[10]; // expected-error{{'require_designated_init' attribute cannot be applied to types}}
+
+// Struct with one field with require_designated_init attribute
+struct ATTR Foo { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int a;
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Foo f1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Foo f2{1};// expected-error{{variable declaration does not use designated initializer syntax}}
+Foo f3 = {1}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this struct.
+Foo f4{};
+Foo f5 = {};
+Foo f6{.a = 1};
+Foo f7 = {.a = 1};
+
+// Struct with multiple fields wth require_designated_init attribute
+struct ATTR Bar { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int b;
+  int c;
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b2{1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b3 = {1, 2};  // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b4{.b = 1, 2};// expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b5 = {.b = 1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this struct.
+Bar b6{};
+Bar b7 = {};
+Bar b8{.b = 1};
+Bar b9 = {.b = 1};
+Bar b10{.b = 1, .c = 2};
+Bar b11 = {.b = 1, .c = 2};
+Bar b12 = {.c = 2, .b = 1};
+
+// Struct without require_designated_init attribute
+struct Baz {
+  int d;
+  int e;
+};
+
+// The following are all valid ways of initializing instances of this struct.
+Baz z1;
+Baz z2{};
+Baz z3 = {};
+Baz z4{1, 2};
+Baz z5 = {1, 2};
+Baz z6{.d = 1, .e = 2};
+Baz z7 = {.d = 1, .e = 2};
+Baz z8{1};
+Baz z9 = {1};
+Baz z10{.d = 1, 2};
+Baz z11 = {.d = 1, 2};
+
+// The require_designated_init attribute can also be attached to unions.
+union ATTR Uni { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int x;
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this union.
+Uni u1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Uni u2{1};// expected-error{{variable declaration does not use designated initializer syntax}}
+Uni u3 = {1}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this union.
+Uni u4{};
+Uni u5 = {};
+Uni u6{.x = 1};
+Uni u7 = {.x = 1};
+
+// The require_designated_init attribute can also be attached to classes.
+class ATTR Cla { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+public:
+  int x;
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c2{1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c3 = {1, 2};  // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c4{.x = 1, 2};// expected-error{{variable declaration does not use designated initializer 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 208829.
emmettneyman added a comment.

Updated tests and diagnostic messages with new attribute spelling


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-designated-init-required.cpp
  clang/test/SemaCXX/attr-require-designated-init.cpp

Index: clang/test/SemaCXX/attr-require-designated-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-require-designated-init.cpp
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+#define ATTR [[clang::require_designated_init]]
+
+// The require_designated_init attribute only applies to types. It will
+// generate a warning when attached to variables, functions, arrays, etc.
+int ATTR x;// expected-error{{'require_designated_init' attribute cannot be applied to types}}
+void ATTR fun(int x) { // expected-error{{'require_designated_init' attribute cannot be applied to types}}
+  return;
+}
+int ATTR arr[10]; // expected-error{{'require_designated_init' attribute cannot be applied to types}}
+
+// Struct with one field with require_designated_init attribute
+struct ATTR Foo { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int a;
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Foo f1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Foo f2{1};// expected-error{{variable declaration does not use designated initializer syntax}}
+Foo f3 = {1}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this struct.
+Foo f4{};
+Foo f5 = {};
+Foo f6{.a = 1};
+Foo f7 = {.a = 1};
+
+// Struct with multiple fields wth require_designated_init attribute
+struct ATTR Bar { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int b;
+  int c;
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b2{1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b3 = {1, 2};  // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b4{.b = 1, 2};// expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b5 = {.b = 1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this struct.
+Bar b6{};
+Bar b7 = {};
+Bar b8{.b = 1};
+Bar b9 = {.b = 1};
+Bar b10{.b = 1, .c = 2};
+Bar b11 = {.b = 1, .c = 2};
+Bar b12 = {.c = 2, .b = 1};
+
+// Struct without require_designated_init attribute
+struct Baz {
+  int d;
+  int e;
+};
+
+// The following are all valid ways of initializing instances of this struct.
+Baz z1;
+Baz z2{};
+Baz z3 = {};
+Baz z4{1, 2};
+Baz z5 = {1, 2};
+Baz z6{.d = 1, .e = 2};
+Baz z7 = {.d = 1, .e = 2};
+Baz z8{1};
+Baz z9 = {1};
+Baz z10{.d = 1, 2};
+Baz z11 = {.d = 1, 2};
+
+// The require_designated_init attribute can also be attached to unions.
+union ATTR Uni { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int x;
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this union.
+Uni u1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Uni u2{1};// expected-error{{variable declaration does not use designated initializer syntax}}
+Uni u3 = {1}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this union.
+Uni u4{};
+Uni u5 = {};
+Uni u6{.x = 1};
+Uni u7 = {.x = 1};
+
+// The require_designated_init attribute can also be attached to classes.
+class ATTR Cla { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+public:
+  int x;
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c2{1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c3 = {1, 2};  // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c4{.x = 1, 2};// expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c5 = {.x = 1, 2}; // 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 208814.
emmettneyman marked 3 inline comments as done.
emmettneyman added a comment.

Changed attribute spelling from GNU to CXX11 and renamed the 'required' 
attribute to 'designated_init_required'


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-designated-init-required.cpp
  clang/test/SemaCXX/attr-require-designated-init.cpp

Index: clang/test/SemaCXX/attr-require-designated-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-require-designated-init.cpp
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+#define ATTR [[clang::require_designated_init]]
+
+// The require_designated_init attribute only applies to types. It will
+// generate a warning when attached to variables, functions, arrays, etc.
+int ATTR x;// expected-error{{'require_designated_init' attribute cannot be applied to types}}
+void ATTR fun(int x) { // expected-error{{'require_designated_init' attribute cannot be applied to types}}
+  return;
+}
+int ATTR arr[10]; // expected-error{{'require_designated_init' attribute cannot be applied to types}}
+
+// Struct with one field with require_designated_init attribute
+struct ATTR Foo { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int a;
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Foo f1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Foo f2{1};// expected-error{{variable declaration does not use designated initializer syntax}}
+Foo f3 = {1}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this struct.
+Foo f4{};
+Foo f5 = {};
+Foo f6{.a = 1};
+Foo f7 = {.a = 1};
+
+// Struct with multiple fields wth require_designated_init attribute
+struct ATTR Bar { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int b;
+  int c;
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b2{1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b3 = {1, 2};  // expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b4{.b = 1, 2};// expected-error{{variable declaration does not use designated initializer syntax}}
+Bar b5 = {.b = 1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this struct.
+Bar b6{};
+Bar b7 = {};
+Bar b8{.b = 1};
+Bar b9 = {.b = 1};
+Bar b10{.b = 1, .c = 2};
+Bar b11 = {.b = 1, .c = 2};
+Bar b12 = {.c = 2, .b = 1};
+
+// Struct without require_designated_init attribute
+struct Baz {
+  int d;
+  int e;
+};
+
+// The following are all valid ways of initializing instances of this struct.
+Baz z1;
+Baz z2{};
+Baz z3 = {};
+Baz z4{1, 2};
+Baz z5 = {1, 2};
+Baz z6{.d = 1, .e = 2};
+Baz z7 = {.d = 1, .e = 2};
+Baz z8{1};
+Baz z9 = {1};
+Baz z10{.d = 1, 2};
+Baz z11 = {.d = 1, 2};
+
+// The require_designated_init attribute can also be attached to unions.
+union ATTR Uni { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+  int x;
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this union.
+Uni u1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Uni u2{1};// expected-error{{variable declaration does not use designated initializer syntax}}
+Uni u3 = {1}; // expected-error{{variable declaration does not use designated initializer syntax}}
+// The following are valid ways of initializing instances of this union.
+Uni u4{};
+Uni u5 = {};
+Uni u6{.x = 1};
+Uni u7 = {.x = 1};
+
+// The require_designated_init attribute can also be attached to classes.
+class ATTR Cla { // expected-note 0+ {{required by 'require_designated_init' attribute here}}
+public:
+  int x;
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;   // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c2{1, 2}; // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c3 = {1, 2};  // expected-error{{variable declaration does not use designated initializer syntax}}
+Cla c4{.x = 1, 2};// 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 208741.
emmettneyman marked 6 inline comments as done.
emmettneyman added a comment.

Small changes in response to feedback


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-require-designated-init.cpp
  clang/test/SemaCXX/attr-required.cpp

Index: clang/test/SemaCXX/attr-required.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-required.cpp
@@ -0,0 +1,63 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+#define ATTR __attribute__((required))
+
+int ATTR x;// expected-warning{{'required' attribute only applies to non-static data members}}
+void ATTR fun(int x) { // expected-warning{{'required' attribute only applies to non-static data members}}
+  return;
+}
+struct ATTR Foo { // expected-warning{{'required' attribute only applies to non-static data members}}
+  int x;
+};
+
+// Struct with one required field
+struct Bar {
+  int ATTR y; // expected-note 0+ {{enforced by 'required' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;// expected-error{{initializer for variable b1 must explicitly initialize field y}}
+Bar b2{};  // expected-error{{initializer for variable b2 must explicitly initialize field y}}
+Bar b3{1}; // expected-error{{initializer for variable b3 must explicitly initialize field y}}
+
+// The following are valid ways of initializing instances of this struct.
+Bar b6{.y = 1};
+
+// Struct with multiple required fields
+struct Baz {
+  int ATTR x; // expected-note 0+ {{enforced by 'required' attribute here}}
+  int y;
+  int ATTR z; // expected-note 0+ {{enforced by 'required' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Baz z1; // expected-error{{initializer for variable z1 must explicitly initialize field x}} expected-error{{initializer for variable z1 must explicitly initialize field z}}
+Baz z2{};   // expected-error{{initializer for variable z2 must explicitly initialize field x}} expected-error{{initializer for variable z2 must explicitly initialize field z}}
+Baz z3{1, 2};   // expected-error{{initializer for variable z3 must explicitly initialize field x}} expected-error{{initializer for variable z3 must explicitly initialize field z}}
+Baz z4{1, 2, 3};// expected-error{{initializer for variable z4 must explicitly initialize field x}} expected-error{{initializer for variable z4 must explicitly initialize field z}}
+Baz z5{.x = 1, 2};  // expected-error{{initializer for variable z5 must explicitly initialize field z}}
+Baz z6{.x = 1, .y = 2}; // expected-error{{initializer for variable z6 must explicitly initialize field z}}
+
+// The following are valid ways of initializing instances of this struct.
+Baz z7{.x = 1, .y = 2, .z = 3};
+Baz z8{.x = 1, .z = 3};
+Baz z9{.x = 1, 2, .z = 3};
+
+// The required attribute can also be applied to public fields of classes.
+class Cla {
+public:
+  int ATTR x; // expected-note 0+ {{enforced by 'required' attribute here}}
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;// expected-error{{initializer for variable c1 must explicitly initialize field x}}
+Cla c2{};  // expected-error{{initializer for variable c2 must explicitly initialize field x}}
+Cla c3{1}; // expected-error{{initializer for variable c3 must explicitly initialize field x}}
+Cla c4{1, 2};  // expected-error{{initializer for variable c4 must explicitly initialize field x}}
+Cla c5{1, .y = 2}; // expected-error{{initializer for variable c5 must explicitly initialize field x}}
+
+// The following are valid ways of initializing instances of this class.
+Cla c6{.x = 1};
+Cla c7{.x = 1, .y = 2};
+Cla c8{.x = 1, 2};
Index: clang/test/SemaCXX/attr-require-designated-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-require-designated-init.cpp
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+#define ATTR __attribute__((require_designated_init))
+
+// The require_designated_init attribute only applies to types. It will
+// generate a warning when attached to variables, functions, arrays, etc.
+int ATTR x;// expected-warning{{attribute only applies to types}}
+void ATTR fun(int x) { // expected-warning{{attribute only applies to types}}
+  return;
+}
+int ATTR arr[10]; // expected-warning{{attribute only applies to types}}
+
+// Struct with one field with 

[PATCH] D64380: Add 'require_designated_init' and 'required' attribute to clang

2019-07-08 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 208544.
emmettneyman added a comment.

Undo clang-format changes to SemaDecl.cpp and SemaDeclAttr.cpp


Repository:
  rG LLVM Github Monorepo

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

https://reviews.llvm.org/D64380

Files:
  clang/include/clang/Basic/Attr.td
  clang/include/clang/Basic/AttrDocs.td
  clang/include/clang/Basic/DiagnosticSemaKinds.td
  clang/lib/Sema/SemaDecl.cpp
  clang/lib/Sema/SemaDeclAttr.cpp
  clang/test/Misc/pragma-attribute-supported-attributes-list.test
  clang/test/SemaCXX/attr-require-designated-init.cpp
  clang/test/SemaCXX/attr-required.cpp

Index: clang/test/SemaCXX/attr-required.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-required.cpp
@@ -0,0 +1,63 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+#define ATTR __attribute__((required))
+
+int ATTR x;// expected-warning{{'required' attribute only applies to non-static data members}}
+void ATTR fun(int x) { // expected-warning{{'required' attribute only applies to non-static data members}}
+  return;
+}
+struct ATTR Foo { // expected-warning{{'required' attribute only applies to non-static data members}}
+  int x;
+};
+
+// Struct with one required field
+struct Bar {
+  int ATTR y; // expected-note 0+ {{enforced by 'required' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Bar b1;// expected-error{{initializer for variable b1 must explicitly initialize field y}}
+Bar b2{};  // expected-error{{initializer for variable b2 must explicitly initialize field y}}
+Bar b3{1}; // expected-error{{initializer for variable b3 must explicitly initialize field y}}
+
+// The following are valid ways of initializing instances of this struct.
+Bar b6{.y = 1};
+
+// Struct with multiple required fields
+struct Baz {
+  int ATTR x; // expected-note 0+ {{enforced by 'required' attribute here}}
+  int y;
+  int ATTR z; // expected-note 0+ {{enforced by 'required' attribute here}}
+};
+
+// The following are invalid ways of initializing instances of this struct.
+Baz z1; // expected-error{{initializer for variable z1 must explicitly initialize field x}} expected-error{{initializer for variable z1 must explicitly initialize field z}}
+Baz z2{};   // expected-error{{initializer for variable z2 must explicitly initialize field x}} expected-error{{initializer for variable z2 must explicitly initialize field z}}
+Baz z3{1, 2};   // expected-error{{initializer for variable z3 must explicitly initialize field x}} expected-error{{initializer for variable z3 must explicitly initialize field z}}
+Baz z4{1, 2, 3};// expected-error{{initializer for variable z4 must explicitly initialize field x}} expected-error{{initializer for variable z4 must explicitly initialize field z}}
+Baz z5{.x = 1, 2};  // expected-error{{initializer for variable z5 must explicitly initialize field z}}
+Baz z6{.x = 1, .y = 2}; // expected-error{{initializer for variable z6 must explicitly initialize field z}}
+
+// The following are valid ways of initializing instances of this struct.
+Baz z7{.x = 1, .y = 2, .z = 3};
+Baz z8{.x = 1, .z = 3};
+Baz z9{.x = 1, 2, .z = 3};
+
+// The required attribute can also be applied to public fields of classes.
+class Cla {
+public:
+  int ATTR x; // expected-note 0+ {{enforced by 'required' attribute here}}
+  int y;
+};
+
+// The following are invalid ways of initializing instances of this class.
+Cla c1;// expected-error{{initializer for variable c1 must explicitly initialize field x}}
+Cla c2{};  // expected-error{{initializer for variable c2 must explicitly initialize field x}}
+Cla c3{1}; // expected-error{{initializer for variable c3 must explicitly initialize field x}}
+Cla c4{1, 2};  // expected-error{{initializer for variable c4 must explicitly initialize field x}}
+Cla c5{1, .y = 2}; // expected-error{{initializer for variable c5 must explicitly initialize field x}}
+
+// The following are valid ways of initializing instances of this class.
+Cla c6{.x = 1};
+Cla c7{.x = 1, .y = 2};
+Cla c8{.x = 1, 2};
Index: clang/test/SemaCXX/attr-require-designated-init.cpp
===
--- /dev/null
+++ clang/test/SemaCXX/attr-require-designated-init.cpp
@@ -0,0 +1,104 @@
+// RUN: %clang_cc1 -fsyntax-only -verify %s
+
+#define ATTR __attribute__((require_designated_init))
+
+// The require_designated_init attribute only applies to types. It will
+// generate a warning when attached to variables, functions, arrays, etc.
+int ATTR x;// expected-warning{{attribute only applies to types}}
+void ATTR fun(int x) { // expected-warning{{attribute only applies to types}}
+  return;
+}
+int ATTR arr[10]; // expected-warning{{attribute only applies to types}}
+
+// Struct with one field with require_designated_init 

[PATCH] D50829: Update README and Dockerfile to include llvm-proto-fuzzer

2018-08-16 Thread Emmett Neyman via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL339933: Update README and Dockerfile to include 
llvm-proto-fuzzer (authored by emmettneyman, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D50829?vs=161093=161095#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D50829

Files:
  cfe/trunk/tools/clang-fuzzer/Dockerfile
  cfe/trunk/tools/clang-fuzzer/README.txt


Index: cfe/trunk/tools/clang-fuzzer/README.txt
===
--- cfe/trunk/tools/clang-fuzzer/README.txt
+++ cfe/trunk/tools/clang-fuzzer/README.txt
@@ -80,3 +80,37 @@
 
 To translate a clang-proto-fuzzer corpus output to C++:
   bin/clang-proto-to-cxx CORPUS_OUTPUT_FILE
+
+===
+ llvm-proto-fuzzer
+===
+Like, clang-proto-fuzzer, llvm-proto-fuzzer is also a protobuf-mutator based
+fuzzer. It receives as input a cxx_loop_proto which it then converts into a
+string of valid LLVM IR: a function with either a single loop or two nested
+loops. It then creates a new string of IR by running optimization passes over
+the original IR. Currently, it only runs a loop-vectorize pass but more passes
+can easily be added to the fuzzer. Once there are two versions of the input
+function (optimized and not), llvm-proto-fuzzer uses LLVM's JIT Engine to
+compile both functions. Lastly, it runs both functions on a suite of inputs and
+checks that both functions behave the same on all inputs. In this way,
+llvm-proto-fuzzer can find not only compiler crashes, but also miscompiles
+originating from LLVM's optimization passes.
+
+llvm-proto-fuzzer is built very similarly to clang-proto-fuzzer. You can run 
the
+fuzzer with the following command:
+  bin/clang-llvm-proto-fuzzer CORPUS_DIR
+
+To translate a cxx_loop_proto file into LLVM IR do:
+  bin/clang-loop-proto-to-llvm CORPUS_OUTPUT_FILE
+To translate a cxx_loop_proto file into C++ do:
+  bin/clang-loop-proto-to-cxx CORPUS_OUTPUT_FILE
+
+Note: To get a higher number of executions per second with llvm-proto-fuzzer it
+helps to build it without ASan instrumentation and with the -O2 flag. Because
+the fuzzer is not only compiling code, but also running it, as the inputs get
+large, the time necessary to fuzz one input can get very high.
+Example:
+  cmake .. -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
+-DCLANG_ENABLE_PROTO_FUZZER=ON -DLLVM_USE_SANITIZE_COVERAGE=YES \
+-DCMAKE_CXX_FLAGS="-O2"
+  ninja clang-llvm-proto-fuzzer clang-loop-proto-to-llvm
Index: cfe/trunk/tools/clang-fuzzer/Dockerfile
===
--- cfe/trunk/tools/clang-fuzzer/Dockerfile
+++ cfe/trunk/tools/clang-fuzzer/Dockerfile
@@ -35,3 +35,7 @@
 RUN cd build1 && ninja clang-fuzzer
 RUN cd build1 && ninja clang-proto-fuzzer
 RUN cd build1 && ninja clang-proto-to-cxx
+RUN cd build1 && ninja clang-loop-proto-to-cxx
+RUN cd build1 && ninja clang-loop-proto-to-llvm
+RUN cd build1 && ninja clang-loop-proto-fuzzer
+RUN cd build1 && ninja clang-llvm-proto-fuzzer


Index: cfe/trunk/tools/clang-fuzzer/README.txt
===
--- cfe/trunk/tools/clang-fuzzer/README.txt
+++ cfe/trunk/tools/clang-fuzzer/README.txt
@@ -80,3 +80,37 @@
 
 To translate a clang-proto-fuzzer corpus output to C++:
   bin/clang-proto-to-cxx CORPUS_OUTPUT_FILE
+
+===
+ llvm-proto-fuzzer
+===
+Like, clang-proto-fuzzer, llvm-proto-fuzzer is also a protobuf-mutator based
+fuzzer. It receives as input a cxx_loop_proto which it then converts into a
+string of valid LLVM IR: a function with either a single loop or two nested
+loops. It then creates a new string of IR by running optimization passes over
+the original IR. Currently, it only runs a loop-vectorize pass but more passes
+can easily be added to the fuzzer. Once there are two versions of the input
+function (optimized and not), llvm-proto-fuzzer uses LLVM's JIT Engine to
+compile both functions. Lastly, it runs both functions on a suite of inputs and
+checks that both functions behave the same on all inputs. In this way,
+llvm-proto-fuzzer can find not only compiler crashes, but also miscompiles
+originating from LLVM's optimization passes.
+
+llvm-proto-fuzzer is built very similarly to clang-proto-fuzzer. You can run the
+fuzzer with the following command:
+  bin/clang-llvm-proto-fuzzer CORPUS_DIR
+
+To translate a cxx_loop_proto file into LLVM IR do:
+  bin/clang-loop-proto-to-llvm CORPUS_OUTPUT_FILE
+To translate a cxx_loop_proto file into C++ do:
+  bin/clang-loop-proto-to-cxx CORPUS_OUTPUT_FILE
+
+Note: To get a higher number of executions per second with llvm-proto-fuzzer it
+helps to build it without ASan instrumentation and with the -O2 flag. Because
+the fuzzer is not only compiling code, but also running it, as the inputs get
+large, the time necessary to fuzz one input can get very high.
+Example:
+  cmake 

[PATCH] D50829: Update README and Dockerfile to include llvm-proto-fuzzer

2018-08-16 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 161093.
emmettneyman added a comment.

Rebased and ready to land


Repository:
  rC Clang

https://reviews.llvm.org/D50829

Files:
  clang/tools/clang-fuzzer/Dockerfile
  clang/tools/clang-fuzzer/README.txt


Index: clang/tools/clang-fuzzer/README.txt
===
--- clang/tools/clang-fuzzer/README.txt
+++ clang/tools/clang-fuzzer/README.txt
@@ -80,3 +80,37 @@
 
 To translate a clang-proto-fuzzer corpus output to C++:
   bin/clang-proto-to-cxx CORPUS_OUTPUT_FILE
+
+===
+ llvm-proto-fuzzer
+===
+Like, clang-proto-fuzzer, llvm-proto-fuzzer is also a protobuf-mutator based
+fuzzer. It receives as input a cxx_loop_proto which it then converts into a
+string of valid LLVM IR: a function with either a single loop or two nested
+loops. It then creates a new string of IR by running optimization passes over
+the original IR. Currently, it only runs a loop-vectorize pass but more passes
+can easily be added to the fuzzer. Once there are two versions of the input
+function (optimized and not), llvm-proto-fuzzer uses LLVM's JIT Engine to
+compile both functions. Lastly, it runs both functions on a suite of inputs and
+checks that both functions behave the same on all inputs. In this way,
+llvm-proto-fuzzer can find not only compiler crashes, but also miscompiles
+originating from LLVM's optimization passes.
+
+llvm-proto-fuzzer is built very similarly to clang-proto-fuzzer. You can run 
the
+fuzzer with the following command:
+  bin/clang-llvm-proto-fuzzer CORPUS_DIR
+
+To translate a cxx_loop_proto file into LLVM IR do:
+  bin/clang-loop-proto-to-llvm CORPUS_OUTPUT_FILE
+To translate a cxx_loop_proto file into C++ do:
+  bin/clang-loop-proto-to-cxx CORPUS_OUTPUT_FILE
+
+Note: To get a higher number of executions per second with llvm-proto-fuzzer it
+helps to build it without ASan instrumentation and with the -O2 flag. Because
+the fuzzer is not only compiling code, but also running it, as the inputs get
+large, the time necessary to fuzz one input can get very high.
+Example:
+  cmake .. -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
+-DCLANG_ENABLE_PROTO_FUZZER=ON -DLLVM_USE_SANITIZE_COVERAGE=YES \
+-DCMAKE_CXX_FLAGS="-O2"
+  ninja clang-llvm-proto-fuzzer clang-loop-proto-to-llvm
Index: clang/tools/clang-fuzzer/Dockerfile
===
--- clang/tools/clang-fuzzer/Dockerfile
+++ clang/tools/clang-fuzzer/Dockerfile
@@ -35,3 +35,7 @@
 RUN cd build1 && ninja clang-fuzzer
 RUN cd build1 && ninja clang-proto-fuzzer
 RUN cd build1 && ninja clang-proto-to-cxx
+RUN cd build1 && ninja clang-loop-proto-to-cxx
+RUN cd build1 && ninja clang-loop-proto-to-llvm
+RUN cd build1 && ninja clang-loop-proto-fuzzer
+RUN cd build1 && ninja clang-llvm-proto-fuzzer


Index: clang/tools/clang-fuzzer/README.txt
===
--- clang/tools/clang-fuzzer/README.txt
+++ clang/tools/clang-fuzzer/README.txt
@@ -80,3 +80,37 @@
 
 To translate a clang-proto-fuzzer corpus output to C++:
   bin/clang-proto-to-cxx CORPUS_OUTPUT_FILE
+
+===
+ llvm-proto-fuzzer
+===
+Like, clang-proto-fuzzer, llvm-proto-fuzzer is also a protobuf-mutator based
+fuzzer. It receives as input a cxx_loop_proto which it then converts into a
+string of valid LLVM IR: a function with either a single loop or two nested
+loops. It then creates a new string of IR by running optimization passes over
+the original IR. Currently, it only runs a loop-vectorize pass but more passes
+can easily be added to the fuzzer. Once there are two versions of the input
+function (optimized and not), llvm-proto-fuzzer uses LLVM's JIT Engine to
+compile both functions. Lastly, it runs both functions on a suite of inputs and
+checks that both functions behave the same on all inputs. In this way,
+llvm-proto-fuzzer can find not only compiler crashes, but also miscompiles
+originating from LLVM's optimization passes.
+
+llvm-proto-fuzzer is built very similarly to clang-proto-fuzzer. You can run the
+fuzzer with the following command:
+  bin/clang-llvm-proto-fuzzer CORPUS_DIR
+
+To translate a cxx_loop_proto file into LLVM IR do:
+  bin/clang-loop-proto-to-llvm CORPUS_OUTPUT_FILE
+To translate a cxx_loop_proto file into C++ do:
+  bin/clang-loop-proto-to-cxx CORPUS_OUTPUT_FILE
+
+Note: To get a higher number of executions per second with llvm-proto-fuzzer it
+helps to build it without ASan instrumentation and with the -O2 flag. Because
+the fuzzer is not only compiling code, but also running it, as the inputs get
+large, the time necessary to fuzz one input can get very high.
+Example:
+  cmake .. -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
+-DCLANG_ENABLE_PROTO_FUZZER=ON -DLLVM_USE_SANITIZE_COVERAGE=YES \
+-DCMAKE_CXX_FLAGS="-O2"
+  ninja clang-llvm-proto-fuzzer 

[PATCH] D50829: Update README and Dockerfile to include llvm-proto-fuzzer

2018-08-16 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 161080.
emmettneyman added a comment.

Added to README


Repository:
  rC Clang

https://reviews.llvm.org/D50829

Files:
  clang/tools/clang-fuzzer/Dockerfile
  clang/tools/clang-fuzzer/README.txt


Index: clang/tools/clang-fuzzer/README.txt
===
--- clang/tools/clang-fuzzer/README.txt
+++ clang/tools/clang-fuzzer/README.txt
@@ -80,3 +80,37 @@
 
 To translate a clang-proto-fuzzer corpus output to C++:
   bin/clang-proto-to-cxx CORPUS_OUTPUT_FILE
+
+===
+ llvm-proto-fuzzer
+===
+Like, clang-proto-fuzzer, llvm-proto-fuzzer is also a protobuf-mutator based
+fuzzer. It receives as input a cxx_loop_proto which it then converts into a
+string of valid LLVM IR: a function with either a single loop or two nested
+loops. It then creates a new string of IR by running optimization passes over
+the original IR. Currently, it only runs a loop-vectorize pass but more passes
+can easily be added to the fuzzer. Once there are two versions of the input
+function (optimized and not), llvm-proto-fuzzer uses LLVM's JIT Engine to
+compile both functions. Lastly, it runs both functions on a suite of inputs and
+checks that both functions behave the same on all inputs. In this way,
+llvm-proto-fuzzer can find not only compiler crashes, but also miscompiles
+originating from LLVM's optimization passes.
+
+llvm-proto-fuzzer is built very similarly to clang-proto-fuzzer. You can run 
the
+fuzzer with the following command:
+  bin/clang-llvm-proto-fuzzer CORPUS_DIR
+
+To translate a cxx_loop_proto file into LLVM IR do:
+  bin/clang-loop-proto-to-llvm CORPUS_OUTPUT_FILE
+To translate a cxx_loop_proto file into C++ do:
+  bin/clang-loop-proto-to-cxx CORPUS_OUTPUT_FILE
+
+Note: To get a higher number of executions per second with llvm-proto-fuzzer it
+helps to build it without ASan instrumentation and with the -O2 flag. Because
+the fuzzer is not only compiling code, but also running it, as the inputs get
+large, the time necessary to fuzz one input can get very high.
+Example:
+  cmake .. -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
+-DCLANG_ENABLE_PROTO_FUZZER=ON -DLLVM_USE_SANITIZE_COVERAGE=YES \
+-DCMAKE_CXX_FLAGS="-O2"
+  ninja clang-llvm-proto-fuzzer clang-loop-proto-to-llvm
Index: clang/tools/clang-fuzzer/Dockerfile
===
--- clang/tools/clang-fuzzer/Dockerfile
+++ clang/tools/clang-fuzzer/Dockerfile
@@ -35,3 +35,7 @@
 RUN cd build1 && ninja clang-fuzzer
 RUN cd build1 && ninja clang-proto-fuzzer
 RUN cd build1 && ninja clang-proto-to-cxx
+RUN cd build1 && ninja clang-loop-proto-to-cxx
+RUN cd build1 && ninja clang-loop-proto-to-llvm
+RUN cd build1 && ninja clang-loop-proto-fuzzer
+RUN cd build1 && ninja clang-llvm-proto-fuzzer


Index: clang/tools/clang-fuzzer/README.txt
===
--- clang/tools/clang-fuzzer/README.txt
+++ clang/tools/clang-fuzzer/README.txt
@@ -80,3 +80,37 @@
 
 To translate a clang-proto-fuzzer corpus output to C++:
   bin/clang-proto-to-cxx CORPUS_OUTPUT_FILE
+
+===
+ llvm-proto-fuzzer
+===
+Like, clang-proto-fuzzer, llvm-proto-fuzzer is also a protobuf-mutator based
+fuzzer. It receives as input a cxx_loop_proto which it then converts into a
+string of valid LLVM IR: a function with either a single loop or two nested
+loops. It then creates a new string of IR by running optimization passes over
+the original IR. Currently, it only runs a loop-vectorize pass but more passes
+can easily be added to the fuzzer. Once there are two versions of the input
+function (optimized and not), llvm-proto-fuzzer uses LLVM's JIT Engine to
+compile both functions. Lastly, it runs both functions on a suite of inputs and
+checks that both functions behave the same on all inputs. In this way,
+llvm-proto-fuzzer can find not only compiler crashes, but also miscompiles
+originating from LLVM's optimization passes.
+
+llvm-proto-fuzzer is built very similarly to clang-proto-fuzzer. You can run the
+fuzzer with the following command:
+  bin/clang-llvm-proto-fuzzer CORPUS_DIR
+
+To translate a cxx_loop_proto file into LLVM IR do:
+  bin/clang-loop-proto-to-llvm CORPUS_OUTPUT_FILE
+To translate a cxx_loop_proto file into C++ do:
+  bin/clang-loop-proto-to-cxx CORPUS_OUTPUT_FILE
+
+Note: To get a higher number of executions per second with llvm-proto-fuzzer it
+helps to build it without ASan instrumentation and with the -O2 flag. Because
+the fuzzer is not only compiling code, but also running it, as the inputs get
+large, the time necessary to fuzz one input can get very high.
+Example:
+  cmake .. -GNinja -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++ \
+-DCLANG_ENABLE_PROTO_FUZZER=ON -DLLVM_USE_SANITIZE_COVERAGE=YES \
+-DCMAKE_CXX_FLAGS="-O2"
+  ninja clang-llvm-proto-fuzzer clang-loop-proto-to-llvm

[PATCH] D50829: Update README and Dockerfile to include llvm-proto-fuzzer

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added a comment.

Is there anything I should add to the documentation? Anything I should remove?


Repository:
  rC Clang

https://reviews.llvm.org/D50829



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


[PATCH] D50829: Update README and Dockerfile to include llvm-proto-fuzzer

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
emmettneyman added reviewers: morehouse, kcc.
Herald added a subscriber: cfe-commits.

Added commands to Dockerfile to build llvm-proto-fuzzer and the other related 
tools. Also added a section to the bottom of the README describing what 
llvm-proto-fuzzer does and how to run it.


Repository:
  rC Clang

https://reviews.llvm.org/D50829

Files:
  clang/tools/clang-fuzzer/Dockerfile
  clang/tools/clang-fuzzer/README.txt


Index: clang/tools/clang-fuzzer/README.txt
===
--- clang/tools/clang-fuzzer/README.txt
+++ clang/tools/clang-fuzzer/README.txt
@@ -80,3 +80,32 @@
 
 To translate a clang-proto-fuzzer corpus output to C++:
   bin/clang-proto-to-cxx CORPUS_OUTPUT_FILE
+
+===
+ llvm-proto-fuzzer
+===
+Like, clang-proto-fuzzer, llvm-proto-fuzzer is also a protobuf-mutator based
+fuzzer. It receives as input a cxx_loop_proto which it then converts into a
+string of valid LLVM IR: a function with either a single loop or two nested
+loops. It then creates a new string of IR by running optimization passes over
+the original IR. Currently, it only runs a loop-vectorize pass but more passes
+can easily be added to the fuzzer. Once there are two versions of the input
+function (optimized and not), llvm-proto-fuzzer uses LLVM's JIT Engine to
+compile both functions. Lastly, it runs both functions on a suite of inputs and
+checks that both functions behave the same on all inputs. In this way,
+llvm-proto-fuzzer can find not only compiler crashes, but also miscompiles
+originating from LLVM's optimization passes.
+
+llvm-proto-fuzzer is built very similarly to clang-proto-fuzzer. You can run 
the
+fuzzer with the following command:
+  bin/clang-llvm-proto-fuzzer CORPUS_DIR
+
+To translate a cxx_loop_proto file into LLVM IR do:
+  bin/clang-loop-proto-to-llvm
+To translate a cxx_loop_proto file into C++ do:
+  bin/clang-loop-proto-to-cxx
+
+Note: To get a higher number of executions per second with llvm-proto-fuzzer it
+helps to build it without ASan instrumentation and with the -O2 flag. Because
+the fuzzer is not only compiling code, but also running it, as the inputs get
+large, the time necessary to fuzz one input can get very high.
Index: clang/tools/clang-fuzzer/Dockerfile
===
--- clang/tools/clang-fuzzer/Dockerfile
+++ clang/tools/clang-fuzzer/Dockerfile
@@ -35,3 +35,7 @@
 RUN cd build1 && ninja clang-fuzzer
 RUN cd build1 && ninja clang-proto-fuzzer
 RUN cd build1 && ninja clang-proto-to-cxx
+RUN cd build1 && ninja clang-loop-proto-to-cxx
+RUN cd build1 && ninja clang-loop-proto-to-llvm
+RUN cd build1 && ninja clang-loop-proto-fuzzer
+RUN cd build1 && ninja clang-llvm-proto-fuzzer


Index: clang/tools/clang-fuzzer/README.txt
===
--- clang/tools/clang-fuzzer/README.txt
+++ clang/tools/clang-fuzzer/README.txt
@@ -80,3 +80,32 @@
 
 To translate a clang-proto-fuzzer corpus output to C++:
   bin/clang-proto-to-cxx CORPUS_OUTPUT_FILE
+
+===
+ llvm-proto-fuzzer
+===
+Like, clang-proto-fuzzer, llvm-proto-fuzzer is also a protobuf-mutator based
+fuzzer. It receives as input a cxx_loop_proto which it then converts into a
+string of valid LLVM IR: a function with either a single loop or two nested
+loops. It then creates a new string of IR by running optimization passes over
+the original IR. Currently, it only runs a loop-vectorize pass but more passes
+can easily be added to the fuzzer. Once there are two versions of the input
+function (optimized and not), llvm-proto-fuzzer uses LLVM's JIT Engine to
+compile both functions. Lastly, it runs both functions on a suite of inputs and
+checks that both functions behave the same on all inputs. In this way,
+llvm-proto-fuzzer can find not only compiler crashes, but also miscompiles
+originating from LLVM's optimization passes.
+
+llvm-proto-fuzzer is built very similarly to clang-proto-fuzzer. You can run the
+fuzzer with the following command:
+  bin/clang-llvm-proto-fuzzer CORPUS_DIR
+
+To translate a cxx_loop_proto file into LLVM IR do:
+  bin/clang-loop-proto-to-llvm
+To translate a cxx_loop_proto file into C++ do:
+  bin/clang-loop-proto-to-cxx
+
+Note: To get a higher number of executions per second with llvm-proto-fuzzer it
+helps to build it without ASan instrumentation and with the -O2 flag. Because
+the fuzzer is not only compiling code, but also running it, as the inputs get
+large, the time necessary to fuzz one input can get very high.
Index: clang/tools/clang-fuzzer/Dockerfile
===
--- clang/tools/clang-fuzzer/Dockerfile
+++ clang/tools/clang-fuzzer/Dockerfile
@@ -35,3 +35,7 @@
 RUN cd build1 && ninja clang-fuzzer
 RUN cd build1 && ninja clang-proto-fuzzer
 RUN cd build1 && ninja clang-proto-to-cxx
+RUN cd build1 && 

[PATCH] D50670: Implementation of nested loops in cxx_loop_proto

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rC339832: Implementation of nested loops in cxx_loop_proto 
(authored by emmettneyman, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D50670?vs=160936=160937#toc

Repository:
  rC Clang

https://reviews.llvm.org/D50670

Files:
  tools/clang-fuzzer/cxx_loop_proto.proto
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp

Index: tools/clang-fuzzer/cxx_loop_proto.proto
===
--- tools/clang-fuzzer/cxx_loop_proto.proto
+++ tools/clang-fuzzer/cxx_loop_proto.proto
@@ -9,10 +9,11 @@
 ///
 /// \file
 ///  This file describes a subset of C++ as a protobuf. It is used to
-///  more easily find interesting inputs for fuzzing Clang. This subset
-///  differs from the one defined in cxx_proto.proto by eliminating while
-///  loops and conditionals. The goal is that the C++ code generated will be
-///  more likely to stress the LLVM loop vectorizer.
+///  more easily find interesting inputs for fuzzing LLVM's vectorizer.
+///  This subset differs from the one defined in cxx_proto.proto by eliminating
+///  while loops and conditionals. The goal is that the C++ code generated will
+///  be more likely to stress the LLVM loop vectorizer. The code generated will
+///  contain either a single loop or two nested loops.
 ///
 //===--===//
 
@@ -74,7 +75,8 @@
 }
 
 message LoopFunction {
-  required StatementSeq statements = 1;
+  optional StatementSeq inner_statements = 1;
+  required StatementSeq outer_statements = 2;
 }
 
 package clang_fuzzer;
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -30,17 +30,30 @@
 std::string StateSeqToString(std::ostream , const StatementSeq );
 
 // Counter variable to generate new LLVM IR variable names and wrapper function
-std::string get_var() {
+static std::string get_var() {
   static int ctr = 0;
   return "%var" + std::to_string(ctr++);
 }
 
+static bool inner_loop = false;
+class InnerLoop {
+  public:
+  InnerLoop() {
+inner_loop = true;
+  }
+  ~InnerLoop() {
+inner_loop = false;
+  }
+};
+
+
 // Proto to LLVM.
 
 std::string ConstToString(const Const ) {
   return std::to_string(x.val());
 }
 std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string which_loop = inner_loop ? "inner" : "outer";
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
@@ -54,7 +67,8 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr
+ << ", i64 %" << which_loop << "_ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,25 +136,61 @@
   }
   return os;
 }
+void NestedLoopToString(std::ostream , const LoopFunction ) {
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "outer_loop_start:\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %inner_loop_start, label %end\n"
+ << "outer_loop:\n"
+ << x.outer_statements()
+ << "%o_ct_new = add i64 %outer_ct, 1\n"
+ << "%jmp_outer = icmp eq i64 %o_ct_new, %s\n"
+ << "br i1 %jmp_outer, label %end, label %inner_loop_start\n"
+ << "inner_loop_start:\n"
+ << "%outer_ct = phi i64 [%o_ct_new, %outer_loop], [0, %outer_loop_start]\n"
+ << "br label %inner_loop\n"
+ << "inner_loop:\n"
+ << "%inner_ct = phi i64 [0, %inner_loop_start], [%i_ct_new, %inner_loop]\n";
+  {
+InnerLoop IL;
+os << x.inner_statements();
+  }
+  os << "%i_ct_new = add i64 %inner_ct, 1\n"
+ << "%jmp_inner = icmp eq i64 %i_ct_new, %s\n"
+ << "br i1 %jmp_inner, label %outer_loop, label %inner_loop, !llvm.loop !0\n"
+ << "end:\n"
+ << "ret void\n"
+ << "}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+}
+void SingleLoopToString(std::ostream , const LoopFunction ) {
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %start, label %end\n"
+ << "start:\n"
+ << "br label %loop\n"
+ << "end:\n"
+ << "ret void\n"
+ << "loop:\n"
+ << "%outer_ct = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
+ << x.outer_statements()
+ << "%ctnew = add i64 %outer_ct, 1\n"
+ << "%j = icmp eq i64 %ctnew, 

[PATCH] D50670: Implementation of nested loops in cxx_loop_proto

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 160936.
emmettneyman added a comment.

Updated comments, rebased, and ready to land


Repository:
  rC Clang

https://reviews.llvm.org/D50670

Files:
  clang/tools/clang-fuzzer/cxx_loop_proto.proto
  clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp

Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -30,17 +30,30 @@
 std::string StateSeqToString(std::ostream , const StatementSeq );
 
 // Counter variable to generate new LLVM IR variable names and wrapper function
-std::string get_var() {
+static std::string get_var() {
   static int ctr = 0;
   return "%var" + std::to_string(ctr++);
 }
 
+static bool inner_loop = false;
+class InnerLoop {
+  public:
+  InnerLoop() {
+inner_loop = true;
+  }
+  ~InnerLoop() {
+inner_loop = false;
+  }
+};
+
+
 // Proto to LLVM.
 
 std::string ConstToString(const Const ) {
   return std::to_string(x.val());
 }
 std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string which_loop = inner_loop ? "inner" : "outer";
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
@@ -54,7 +67,8 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr
+ << ", i64 %" << which_loop << "_ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,25 +136,61 @@
   }
   return os;
 }
+void NestedLoopToString(std::ostream , const LoopFunction ) {
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "outer_loop_start:\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %inner_loop_start, label %end\n"
+ << "outer_loop:\n"
+ << x.outer_statements()
+ << "%o_ct_new = add i64 %outer_ct, 1\n"
+ << "%jmp_outer = icmp eq i64 %o_ct_new, %s\n"
+ << "br i1 %jmp_outer, label %end, label %inner_loop_start\n"
+ << "inner_loop_start:\n"
+ << "%outer_ct = phi i64 [%o_ct_new, %outer_loop], [0, %outer_loop_start]\n"
+ << "br label %inner_loop\n"
+ << "inner_loop:\n"
+ << "%inner_ct = phi i64 [0, %inner_loop_start], [%i_ct_new, %inner_loop]\n";
+  {
+InnerLoop IL;
+os << x.inner_statements();
+  }
+  os << "%i_ct_new = add i64 %inner_ct, 1\n"
+ << "%jmp_inner = icmp eq i64 %i_ct_new, %s\n"
+ << "br i1 %jmp_inner, label %outer_loop, label %inner_loop, !llvm.loop !0\n"
+ << "end:\n"
+ << "ret void\n"
+ << "}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+}
+void SingleLoopToString(std::ostream , const LoopFunction ) {
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %start, label %end\n"
+ << "start:\n"
+ << "br label %loop\n"
+ << "end:\n"
+ << "ret void\n"
+ << "loop:\n"
+ << "%outer_ct = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
+ << x.outer_statements()
+ << "%ctnew = add i64 %outer_ct, 1\n"
+ << "%j = icmp eq i64 %ctnew, %s\n"
+ << "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+}
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
-<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
-<< "%1 = icmp sgt i64 %s, 0\n"
-<< "br i1 %1, label %start, label %end\n"
-<< "start:\n"
-<< "br label %loop\n"
-<< "end:\n"
-<< "ret void\n"
-<< "loop:\n"
-<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
-<< x.statements()
-<< "%ctnew = add i64 %ct, 1\n"
-<< "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
-<< "!0 = distinct !{!0, !1, !2}\n"
-<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
-<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
-<< "}\n";
+  if (x.has_inner_statements())
+NestedLoopToString(os, x);
+  else
+SingleLoopToString(os, x);
+  return os;
 }
 
 // -
Index: 

[PATCH] D50670: Implementation of nested loops in cxx_loop_proto

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 160932.
emmettneyman added a comment.

Changed modifying global var to scoped class


Repository:
  rC Clang

https://reviews.llvm.org/D50670

Files:
  clang/tools/clang-fuzzer/cxx_loop_proto.proto
  clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp

Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -30,17 +30,30 @@
 std::string StateSeqToString(std::ostream , const StatementSeq );
 
 // Counter variable to generate new LLVM IR variable names and wrapper function
-std::string get_var() {
+static std::string get_var() {
   static int ctr = 0;
   return "%var" + std::to_string(ctr++);
 }
 
+static bool inner_loop = false;
+class InnerLoop {
+  public:
+  InnerLoop() {
+inner_loop = true;
+  }
+  ~InnerLoop() {
+inner_loop = false;
+  }
+};
+
+
 // Proto to LLVM.
 
 std::string ConstToString(const Const ) {
   return std::to_string(x.val());
 }
 std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string which_loop = inner_loop ? "inner" : "outer";
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
@@ -54,7 +67,8 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr
+ << ", i64 %" << which_loop << "_ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,25 +136,61 @@
   }
   return os;
 }
+void NestedLoopToString(std::ostream , const LoopFunction ) {
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "outer_loop_start:\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %inner_loop_start, label %end\n"
+ << "outer_loop:\n"
+ << x.outer_statements()
+ << "%o_ct_new = add i64 %outer_ct, 1\n"
+ << "%jmp_outer = icmp eq i64 %o_ct_new, %s\n"
+ << "br i1 %jmp_outer, label %end, label %inner_loop_start\n"
+ << "inner_loop_start:\n"
+ << "%outer_ct = phi i64 [%o_ct_new, %outer_loop], [0, %outer_loop_start]\n"
+ << "br label %inner_loop\n"
+ << "inner_loop:\n"
+ << "%inner_ct = phi i64 [0, %inner_loop_start], [%i_ct_new, %inner_loop]\n";
+  {
+InnerLoop IL;
+os << x.inner_statements();
+  }
+  os << "%i_ct_new = add i64 %inner_ct, 1\n"
+ << "%jmp_inner = icmp eq i64 %i_ct_new, %s\n"
+ << "br i1 %jmp_inner, label %outer_loop, label %inner_loop, !llvm.loop !0\n"
+ << "end:\n"
+ << "ret void\n"
+ << "}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+}
+void SingleLoopToString(std::ostream , const LoopFunction ) {
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %start, label %end\n"
+ << "start:\n"
+ << "br label %loop\n"
+ << "end:\n"
+ << "ret void\n"
+ << "loop:\n"
+ << "%outer_ct = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
+ << x.outer_statements()
+ << "%ctnew = add i64 %outer_ct, 1\n"
+ << "%j = icmp eq i64 %ctnew, %s\n"
+ << "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+}
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
-<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
-<< "%1 = icmp sgt i64 %s, 0\n"
-<< "br i1 %1, label %start, label %end\n"
-<< "start:\n"
-<< "br label %loop\n"
-<< "end:\n"
-<< "ret void\n"
-<< "loop:\n"
-<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
-<< x.statements()
-<< "%ctnew = add i64 %ct, 1\n"
-<< "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
-<< "!0 = distinct !{!0, !1, !2}\n"
-<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
-<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
-<< "}\n";
+  if (x.has_inner_statements())
+NestedLoopToString(os, x);
+  else
+SingleLoopToString(os, x);
+  return os;
 }
 
 // -
Index: 

[PATCH] D50670: Implementation of nested loops in cxx_loop_proto

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 160920.
emmettneyman added a comment.

Made the inner loop optional


Repository:
  rC Clang

https://reviews.llvm.org/D50670

Files:
  clang/tools/clang-fuzzer/cxx_loop_proto.proto
  clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp

Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -30,17 +30,19 @@
 std::string StateSeqToString(std::ostream , const StatementSeq );
 
 // Counter variable to generate new LLVM IR variable names and wrapper function
-std::string get_var() {
+static std::string get_var() {
   static int ctr = 0;
   return "%var" + std::to_string(ctr++);
 }
+static bool inner_loop;
 
 // Proto to LLVM.
 
 std::string ConstToString(const Const ) {
   return std::to_string(x.val());
 }
 std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string which_loop = inner_loop ? "inner" : "outer";
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
@@ -54,7 +56,8 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr
+ << ", i64 %" << which_loop << "_ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,25 +125,61 @@
   }
   return os;
 }
+void NestedLoopToString(std::ostream , const LoopFunction ) {
+  inner_loop = false;
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "outer_loop_start:\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %inner_loop_start, label %end\n"
+ << "outer_loop:\n"
+ << x.outer_statements();
+  inner_loop = true;
+  os << "%o_ct_new = add i64 %outer_ct, 1\n"
+ << "%jmp_outer = icmp eq i64 %o_ct_new, %s\n"
+ << "br i1 %jmp_outer, label %end, label %inner_loop_start\n"
+ << "inner_loop_start:\n"
+ << "%outer_ct = phi i64 [%o_ct_new, %outer_loop], [0, %outer_loop_start]\n"
+ << "br label %inner_loop\n"
+ << "inner_loop:\n"
+ << "%inner_ct = phi i64 [0, %inner_loop_start], [%i_ct_new, %inner_loop]\n"
+ << x.inner_statements()
+ << "%i_ct_new = add i64 %inner_ct, 1\n"
+ << "%jmp_inner = icmp eq i64 %i_ct_new, %s\n"
+ << "br i1 %jmp_inner, label %outer_loop, label %inner_loop, !llvm.loop !0\n"
+ << "end:\n"
+ << "ret void\n"
+ << "}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+}
+void SingleLoopToString(std::ostream , const LoopFunction ) {
+  inner_loop = false;
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %start, label %end\n"
+ << "start:\n"
+ << "br label %loop\n"
+ << "end:\n"
+ << "ret void\n"
+ << "loop:\n"
+ << "%outer_ct = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
+ << x.outer_statements()
+ << "%ctnew = add i64 %outer_ct, 1\n"
+ << "%j = icmp eq i64 %ctnew, %s\n"
+ << "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+}
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
-<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
-<< "%1 = icmp sgt i64 %s, 0\n"
-<< "br i1 %1, label %start, label %end\n"
-<< "start:\n"
-<< "br label %loop\n"
-<< "end:\n"
-<< "ret void\n"
-<< "loop:\n"
-<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
-<< x.statements()
-<< "%ctnew = add i64 %ct, 1\n"
-<< "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
-<< "!0 = distinct !{!0, !1, !2}\n"
-<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
-<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
-<< "}\n";
+  if (x.has_inner_statements())
+NestedLoopToString(os, x);
+  else
+SingleLoopToString(os, x);
+  return os;
 }
 
 // -
Index: clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
===
--- 

[PATCH] D50670: Implementation of nested loops in cxx_loop_proto

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:132
+  os << "target triple = \"x86_64-pc-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"

morehouse wrote:
> I'm curious how this change affects coverage independent of the rest of this 
> change.  Also what would happen if we set `%a` and `%b` to noalias as well?
`%c` was always supposed to be `noalias`. It got changed accidentally in the 
last patch. I'm not sure what would change in the coverage if `%a` and `%b` 
also got set to be `noalias`.

@kcc and I had originally planned for `foo (int *a, int *b, int *__restrict__ 
c, size_t s)` to be just the first function signature we tried for this fuzz 
target and to change it up and try different signatures.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:127
   }
+  inner_loop = true;
   return os;

morehouse wrote:
> Maybe this fixes the bug, but modifying `inner_loop` from different functions 
> is still error-prone.
> 
> Please either make this a scoped variable (with a wrapper class that sets it 
> to true in the constructor and sets it to false in the destructor), or make 
> it a parameter.
Would it be better if `inner_loop` was only modified in the `LoopFunction` 
operator override? For example:

```
std::ostream <<(std::ostream , const LoopFunction ) {
  inner_loop = false;
  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
 
 << << "outer_loop:\n"
 << x.outer_statements();
  inner_loop = true;
  os << "%o_ct_new = add i64 %outer_ct, 1\n"
 
 << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
  return os;

```
And removed `inner_loop = true;` from the above function?



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:140
+ << "br label %inner_loop\n"
+ << "end:\n"
+ << "ret void\n"

morehouse wrote:
> I don't see any jumps to `end`.  I think this will be an infinite loop.
There are two jumps to `end`, one before entering the `outer_loop` and one at 
the end of `outer_loop`.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:143
+ << "outer_loop:\n"
+ << x.outer_statements()
+ << "%o_ct_new = add i64 %outer_ct, 1\n"

morehouse wrote:
> morehouse wrote:
> > IIUC this creates loop structure always like this:
> > 
> > ```
> > for (int i = 0; i < s; i++) {
> >   for (int j = 0; j < s; j++) {
> > // statements
> >   }
> >   // statements
> > }
> > ```
> > 
> > Maybe not necessary for this patch, but I'm curious if adding statements 
> > before the inner loop would exercise different coverage in the vectorizer.
> Will all loops be double-nested now?
Yes, that's correct. It's also worth noting that all the statements in the 
inner loop will only use `j` to index into the arrays, never `i`. It shouldn't 
be hard to add outer loop statements before the inner loop. A third 
`StatementSeq` could be added to the `LoopFunction` proto: one for outer loop 
statements before the inner loop, one for inner loop statements, and one for 
outer loop statements after the inner loop.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:143
+ << "outer_loop:\n"
+ << x.outer_statements()
+ << "%o_ct_new = add i64 %outer_ct, 1\n"

emmettneyman wrote:
> morehouse wrote:
> > morehouse wrote:
> > > IIUC this creates loop structure always like this:
> > > 
> > > ```
> > > for (int i = 0; i < s; i++) {
> > >   for (int j = 0; j < s; j++) {
> > > // statements
> > >   }
> > >   // statements
> > > }
> > > ```
> > > 
> > > Maybe not necessary for this patch, but I'm curious if adding statements 
> > > before the inner loop would exercise different coverage in the vectorizer.
> > Will all loops be double-nested now?
> Yes, that's correct. It's also worth noting that all the statements in the 
> inner loop will only use `j` to index into the arrays, never `i`. It 
> shouldn't be hard to add outer loop statements before the inner loop. A third 
> `StatementSeq` could be added to the `LoopFunction` proto: one for outer loop 
> statements before the inner loop, one for inner loop statements, and one for 
> outer loop statements after the inner loop.
Yes, all loops will be double-nested. It shouldn't be difficult to make the 
inner loop optional though. In `cxx_loop_proto`, the first `Statement_Seq`, 
`inner_statements`, can be made `optional` and then a different IR structures 
can be generated depending on whether or not `inner_statements` exists.


Repository:
  rC Clang

https://reviews.llvm.org/D50670



___
cfe-commits mailing list
cfe-commits@lists.llvm.org

[PATCH] D50670: Implementation of nested loops in cxx_loop_proto

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 160870.
emmettneyman added a comment.

Small changes to generated IR, my last change hadn't saved


Repository:
  rC Clang

https://reviews.llvm.org/D50670

Files:
  clang/tools/clang-fuzzer/cxx_loop_proto.proto
  clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp

Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -30,17 +30,20 @@
 std::string StateSeqToString(std::ostream , const StatementSeq );
 
 // Counter variable to generate new LLVM IR variable names and wrapper function
-std::string get_var() {
+static std::string get_var() {
   static int ctr = 0;
   return "%var" + std::to_string(ctr++);
 }
 
+static bool inner_loop = false;
+
 // Proto to LLVM.
 
 std::string ConstToString(const Const ) {
   return std::to_string(x.val());
 }
 std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string which_loop = inner_loop ? "inner" : "outer";
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
@@ -54,7 +57,8 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr
+ << ", i64 %" << which_loop << "_ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -120,27 +124,37 @@
   for (auto  : x.statements()) {
 os << st;
   }
+  inner_loop = true;
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
-<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
-<< "%1 = icmp sgt i64 %s, 0\n"
-<< "br i1 %1, label %start, label %end\n"
-<< "start:\n"
-<< "br label %loop\n"
-<< "end:\n"
-<< "ret void\n"
-<< "loop:\n"
-<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
-<< x.statements()
-<< "%ctnew = add i64 %ct, 1\n"
-<< "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
-<< "!0 = distinct !{!0, !1, !2}\n"
-<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
-<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
-<< "}\n";
+  inner_loop = false;
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "outer_loop_start:\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %inner_loop_start, label %end\n"
+ << "outer_loop:\n"
+ << x.outer_statements()
+ << "%o_ct_new = add i64 %outer_ct, 1\n"
+ << "%jmp_outer = icmp eq i64 %o_ct_new, %s\n"
+ << "br i1 %jmp_outer, label %end, label %inner_loop_start\n"
+ << "inner_loop_start:\n"
+ << "%outer_ct = phi i64 [%o_ct_new, %outer_loop], [0, %outer_loop_start]\n"
+ << "br label %inner_loop\n"
+ << "inner_loop:\n"
+ << "%inner_ct = phi i64 [0, %inner_loop_start], [%i_ct_new, %inner_loop]\n"
+ << x.inner_statements()
+ << "%i_ct_new = add i64 %inner_ct, 1\n"
+ << "%jmp_inner = icmp eq i64 %i_ct_new, %s\n"
+ << "br i1 %jmp_inner, label %outer_loop, label %inner_loop, !llvm.loop !0\n"
+ << "end:\n"
+ << "ret void\n"
+ << "}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+  return os;
 }
 
 // -
Index: clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
===
--- clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
+++ clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
@@ -28,6 +28,8 @@
 
 namespace clang_fuzzer {
 
+static bool inner_loop;
+
 // Forward decls.
 std::ostream <<(std::ostream , const BinaryOp );
 std::ostream <<(std::ostream , const StatementSeq );
@@ -37,13 +39,14 @@
   return os << "(" << x.val() << ")";
 }
 std::ostream <<(std::ostream , const VarRef ) {
+  std::string var = inner_loop ? "i" : "j";
   switch (x.arr()) {
 case VarRef::ARR_A:
-  return os << "a[i]";
+  return os << "a[" << var << "]";
 case VarRef::ARR_B:
-  return os << "b[i]";
+  return os << "b[" << var << "]";
 case VarRef::ARR_C:
-  return os << "c[i]";
+  return os << "c[" << var << "]";
   }
 }
 std::ostream <<(std::ostream , const Rvalue ) {
@@ -109,9 +112,13 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << 

[PATCH] D50670: Implementation of nested loops in cxx_loop_proto

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 160866.
emmettneyman edited the summary of this revision.
emmettneyman added a comment.

Small changes to generated IR


Repository:
  rC Clang

https://reviews.llvm.org/D50670

Files:
  clang/tools/clang-fuzzer/cxx_loop_proto.proto
  clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp

Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -30,17 +30,20 @@
 std::string StateSeqToString(std::ostream , const StatementSeq );
 
 // Counter variable to generate new LLVM IR variable names and wrapper function
-std::string get_var() {
+static std::string get_var() {
   static int ctr = 0;
   return "%var" + std::to_string(ctr++);
 }
 
+static bool inner_loop = false;
+
 // Proto to LLVM.
 
 std::string ConstToString(const Const ) {
   return std::to_string(x.val());
 }
 std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string which_loop = inner_loop ? "inner" : "outer";
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
@@ -54,7 +57,8 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr
+ << ", i64 %" << which_loop << "_ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -120,27 +124,37 @@
   for (auto  : x.statements()) {
 os << st;
   }
+  inner_loop = true;
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
-<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
-<< "%1 = icmp sgt i64 %s, 0\n"
-<< "br i1 %1, label %start, label %end\n"
-<< "start:\n"
-<< "br label %loop\n"
-<< "end:\n"
-<< "ret void\n"
-<< "loop:\n"
-<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
-<< x.statements()
-<< "%ctnew = add i64 %ct, 1\n"
-<< "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
-<< "!0 = distinct !{!0, !1, !2}\n"
-<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
-<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
-<< "}\n";
+  inner_loop = false;
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "outer_loop_start:\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %inner_loop_start, label %end\n"
+ << "inner_loop_start:\n"
+ << "%outer_ct = phi i64 [%o_ct_new, %outer_loop], [0, %outer_loop_start]\n"
+ << "br label %inner_loop\n"
+ << "end:\n"
+ << "ret void\n"
+ << "outer_loop:\n"
+ << x.outer_statements()
+ << "%o_ct_new = add i64 %outer_ct, 1\n"
+ << "%jmp_outer = icmp eq i64 %o_ct_new, %s\n"
+ << "br i1 %jmp_outer, label %end, label %inner_loop_start\n"
+ << "inner_loop:\n"
+ << "%inner_ct = phi i64 [0, %inner_loop_start], [%i_ct_new, %inner_loop]\n"
+ << x.inner_statements()
+ << "%i_ct_new = add i64 %inner_ct, 1\n"
+ << "%jmp_inner = icmp eq i64 %i_ct_new, %s\n"
+ << "br i1 %jmp_inner, label %outer_loop, label %inner_loop, !llvm.loop !0\n"
+ << "}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+  return os;
 }
 
 // -
Index: clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
===
--- clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
+++ clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
@@ -28,6 +28,8 @@
 
 namespace clang_fuzzer {
 
+static bool inner_loop;
+
 // Forward decls.
 std::ostream <<(std::ostream , const BinaryOp );
 std::ostream <<(std::ostream , const StatementSeq );
@@ -37,13 +39,14 @@
   return os << "(" << x.val() << ")";
 }
 std::ostream <<(std::ostream , const VarRef ) {
+  std::string var = inner_loop ? "i" : "j";
   switch (x.arr()) {
 case VarRef::ARR_A:
-  return os << "a[i]";
+  return os << "a[" << var << "]";
 case VarRef::ARR_B:
-  return os << "b[i]";
+  return os << "b[" << var << "]";
 case VarRef::ARR_C:
-  return os << "c[i]";
+  return os << "c[" << var << "]";
   }
 }
 std::ostream <<(std::ostream , const Rvalue ) {
@@ -109,9 +112,13 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction 

[PATCH] D50670: Implementation of nested loops in cxx_loop_proto

2018-08-15 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added a comment.

In https://reviews.llvm.org/D50670#1200784, @morehouse wrote:

> Does this hit new coverage in the vectorizer?


Yes, this does hit new coverage in the loop vectorizer code.




Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:131
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
-<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
-<< "%1 = icmp sgt i64 %s, 0\n"
-<< "br i1 %1, label %start, label %end\n"
-<< "start:\n"
-<< "br label %loop\n"
-<< "end:\n"
-<< "ret void\n"
-<< "loop:\n"
-<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
-<< x.statements()
-<< "%ctnew = add i64 %ct, 1\n"
-<< "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
-<< "!0 = distinct !{!0, !1, !2}\n"
-<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
-<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
-<< "}\n";
+  os << "target triple = \"x86_64-pc-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"

morehouse wrote:
> Why do you change this to `pc` again?
Sorry, it got changed back when I copy/pasted the IR.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:136
+ << "outer_loop_start:\n"
+ << "br label %inner_loop_start\n"
+ << "inner_loop_start:\n"

morehouse wrote:
> Looks like a pointless branch.
I realize it's just a label with a branch instruction but I think I need it for 
the phi instruction. I will move the `outer_loop_start` label above the `icmp` 
instruction and change the branch from `outer_loop_start` to `inner_loop_start`.




Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:144
+ << x.outer_statements()
+ << "%o_ct_new = add nuw nsw i64 %outer_ct, 1\n"
+ << "%jmp_outer = icmp eq i64 %o_ct_new, %s\n"

morehouse wrote:
> Why `nuw`, `nsw` here?
Sorry, it got changed back when I copy/pasted the IR.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:154
+ << "}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"

morehouse wrote:
> Can we simplify the order of blocks here?  It is confusing to follow all 
> these jumps forward and backward.
Yes, I think it might be cleaner to have the blocks like this: 

```
define @foo... {

outer_loop_start

outer_loop

inner_loop_start

inner_loop

end
}
```


Repository:
  rC Clang

https://reviews.llvm.org/D50670



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


[PATCH] D50670: Implementation of multiple loops in cxx_loop_proto

2018-08-14 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 160740.
emmettneyman added a comment.

Changed the multiloop protos to nested loop protos. All the protos have an 
inner loop and an outer loop.


Repository:
  rC Clang

https://reviews.llvm.org/D50670

Files:
  clang/tools/clang-fuzzer/cxx_loop_proto.proto
  clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp

Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -30,17 +30,20 @@
 std::string StateSeqToString(std::ostream , const StatementSeq );
 
 // Counter variable to generate new LLVM IR variable names and wrapper function
-std::string get_var() {
+static std::string get_var() {
   static int ctr = 0;
   return "%var" + std::to_string(ctr++);
 }
 
+static bool inner_loop = false;
+
 // Proto to LLVM.
 
 std::string ConstToString(const Const ) {
   return std::to_string(x.val());
 }
 std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string var = inner_loop ? "inner" : "outer";
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
@@ -54,7 +57,8 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr
+ << ", i64 %" << var << "_ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -120,27 +124,37 @@
   for (auto  : x.statements()) {
 os << st;
   }
+  inner_loop = true;
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
-<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
-<< "%1 = icmp sgt i64 %s, 0\n"
-<< "br i1 %1, label %start, label %end\n"
-<< "start:\n"
-<< "br label %loop\n"
-<< "end:\n"
-<< "ret void\n"
-<< "loop:\n"
-<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
-<< x.statements()
-<< "%ctnew = add i64 %ct, 1\n"
-<< "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
-<< "!0 = distinct !{!0, !1, !2}\n"
-<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
-<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
-<< "}\n";
+  os << "target triple = \"x86_64-pc-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
+ << "%cmp = icmp sgt i64 %s, 0\n"
+ << "br i1 %cmp, label %outer_loop_start, label %end\n"
+ << "outer_loop_start:\n"
+ << "br label %inner_loop_start\n"
+ << "inner_loop_start:\n"
+ << "%outer_ct = phi i64 [%o_ct_new, %outer_loop], [0, %outer_loop_start]\n"
+ << "br label %inner_loop\n"
+ << "end:\n"
+ << "ret void\n"
+ << "outer_loop:\n"
+ << x.outer_statements()
+ << "%o_ct_new = add nuw nsw i64 %outer_ct, 1\n"
+ << "%jmp_outer = icmp eq i64 %o_ct_new, %s\n"
+ << "br i1 %jmp_outer, label %end, label %inner_loop_start\n"
+ << "inner_loop:\n"
+ << "%inner_ct = phi i64 [0, %inner_loop_start], [%i_ct_new, %inner_loop]\n"
+ << x.inner_statements()
+ << "%i_ct_new = add nuw nsw i64 %inner_ct, 1\n"
+ << "%jmp_inner = icmp eq i64 %i_ct_new, %s\n"
+ << "br i1 %jmp_inner, label %outer_loop, label %inner_loop, !llvm.loop !0\n"
+ << "}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+  return os;
 }
 
 // -
Index: clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
===
--- clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
+++ clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
@@ -28,6 +28,8 @@
 
 namespace clang_fuzzer {
 
+static bool inner_loop;
+
 // Forward decls.
 std::ostream <<(std::ostream , const BinaryOp );
 std::ostream <<(std::ostream , const StatementSeq );
@@ -37,13 +39,14 @@
   return os << "(" << x.val() << ")";
 }
 std::ostream <<(std::ostream , const VarRef ) {
+  std::string var = inner_loop ? "i" : "j";
   switch (x.arr()) {
 case VarRef::ARR_A:
-  return os << "a[i]";
+  return os << "a[" << var << "]";
 case VarRef::ARR_B:
-  return os << "b[i]";
+  return os << "b[" << var << "]";
 case VarRef::ARR_C:
-  return os << "c[i]";
+  return os << "c[" << var << "]";
   }
 }
 std::ostream <<(std::ostream , const Rvalue ) {
@@ -109,9 +112,13 @@
   return os;
 }
 

[PATCH] D50670: Implementation of multiple loops in cxx_loop_proto

2018-08-14 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added a comment.

No, it doesn't actually. I thought it would try to combine the separate loops 
into one block of vectorized instructions but after looking at the coverage of 
multiple loops vs single loop, they cover exactly the same parts of the Loop 
Vectorizer. Should I switch my focus to nested loops instead? I think nested 
loops will increase coverage.


Repository:
  rC Clang

https://reviews.llvm.org/D50670



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


[PATCH] D50670: Implementation of multiple loops in cxx_loop_proto

2018-08-13 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
emmettneyman added reviewers: morehouse, kcc.
Herald added a subscriber: cfe-commits.

Extended `cxx_loop_proto` to have multiple for loops. Modified 
`loop_proto_to_llvm` and `loop_proto_to_cxx` to handle the new protos. In 
`loop_proto_to_llvm`, I only translate the first ten loops from the protobuf 
into IR. This will keep down the runtime of each fuzzer run.


Repository:
  rC Clang

https://reviews.llvm.org/D50670

Files:
  clang/tools/clang-fuzzer/cxx_loop_proto.proto
  clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp

Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -30,11 +30,21 @@
 std::string StateSeqToString(std::ostream , const StatementSeq );
 
 // Counter variable to generate new LLVM IR variable names and wrapper function
-std::string get_var() {
+static std::string get_var() {
   static int ctr = 0;
   return "%var" + std::to_string(ctr++);
 }
 
+static int loop_ctr = 0;
+
+static std::string get_loop_num() {
+  return std::to_string(loop_ctr);
+}
+
+static void new_loop() {
+  loop_ctr++;
+}
+
 // Proto to LLVM.
 
 std::string ConstToString(const Const ) {
@@ -54,7 +64,8 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr
+ << ", i64 %ct_" << get_loop_num() << "\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,25 +133,50 @@
   }
   return os;
 }
-std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
-<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
-<< "%1 = icmp sgt i64 %s, 0\n"
-<< "br i1 %1, label %start, label %end\n"
-<< "start:\n"
-<< "br label %loop\n"
-<< "end:\n"
-<< "ret void\n"
-<< "loop:\n"
-<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
+std::ostream <<(std::ostream , const Loop ) {
+  new_loop();
+  std::string lnum = get_loop_num();
+  std::string next_lnum = std::to_string(stoi(lnum) + 1);
+  if (!lnum.compare("1")) {
+os << "%cmp_" << lnum << " = icmp sgt i64 %s, 0\n"
+   << "br i1 %cmp_" << lnum << ", label %loop_start_" << lnum 
+   << ", label %end\n";
+  }
+  return os << "loop_start_" << lnum << ":\n"
+<< "br label %loop_" << lnum << "\n"
+<< "loop_end_" << lnum << ":\n"
+<< "%cmp_" << next_lnum << " = icmp sgt i64 %s, 0\n"
+<< "br i1 %cmp_" << next_lnum << ", label %loop_start_" << next_lnum
+<< ", label %end\n"
+<< "loop_" << lnum << ":\n"
+<< " %ct_" << lnum << "   = phi i64 [ %ctnew_" << lnum
+<< ", %loop_" << lnum << " ], [ 0, %loop_start_" << lnum << " ]\n"
 << x.statements()
-<< "%ctnew = add i64 %ct, 1\n"
-<< "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
-<< "!0 = distinct !{!0, !1, !2}\n"
-<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
-<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
-<< "}\n";
+<< "%ctnew_" << lnum << " = add i64 %ct_" << lnum << ", 1\n"
+<< "%j_" << lnum << " = icmp eq i64 %ctnew_" << lnum << ", %s\n"
+<< "br i1 %j_" << lnum << ", label %loop_end_" << lnum
+<< ", label %loop_" << lnum << ", !llvm.loop !0\n";
+
+}
+std::ostream <<(std::ostream , const LoopFunction ) {
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+ << "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n";
+  for (auto  : x.loops()) {
+os << lp;
+// No matter how many loops the protobuf has, only translate 10 of them
+if (!get_loop_num().compare("10"))
+  break;
+  }
+  new_loop();
+  std::string lnum = get_loop_num();
+  os << "loop_start_" << lnum << ":\n"
+ << "br label %end\n"
+ << "end:\n"
+ << "ret void\n}\n"
+ << "!0 = distinct !{!0, !1, !2}\n"
+ << "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+ << "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize << "}\n";
+  return os;
 }
 
 // -
Index: clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
===
--- clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
+++ clang/tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
@@ -108,10 +108,15 @@
 os << st;
   return os;
 }
+std::ostream <<(std::ostream , 

[PATCH] D50530: Added LLVM metadata to generated IR to increase vectorization width

2018-08-09 Thread Emmett Neyman via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL339392: Added LLVM metadata to generated IR to increase 
vectorization width (authored by emmettneyman, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D50530?vs=160016=160017#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D50530

Files:
  cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -15,6 +15,7 @@
 
 #include "loop_proto_to_llvm.h"
 #include "cxx_loop_proto.pb.h"
+#include "../handle-llvm/input_arrays.h"
 
 // The following is needed to convert protos in human-readable form
 #include 
@@ -135,7 +136,11 @@
 << x.statements()
 << "%ctnew = add i64 %ct, 1\n"
 << "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop\n}\n";
+<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+<< "!0 = distinct !{!0, !1, !2}\n"
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }
 
 // -


Index: cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -15,6 +15,7 @@
 
 #include "loop_proto_to_llvm.h"
 #include "cxx_loop_proto.pb.h"
+#include "../handle-llvm/input_arrays.h"
 
 // The following is needed to convert protos in human-readable form
 #include 
@@ -135,7 +136,11 @@
 << x.statements()
 << "%ctnew = add i64 %ct, 1\n"
 << "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop\n}\n";
+<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+<< "!0 = distinct !{!0, !1, !2}\n"
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50530: Added LLVM metadata to generated IR to increase vectorization width

2018-08-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 160016.
emmettneyman added a comment.

Rebased and ready to land


Repository:
  rC Clang

https://reviews.llvm.org/D50530

Files:
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -15,6 +15,7 @@
 
 #include "loop_proto_to_llvm.h"
 #include "cxx_loop_proto.pb.h"
+#include "../handle-llvm/input_arrays.h"
 
 // The following is needed to convert protos in human-readable form
 #include 
@@ -135,7 +136,11 @@
 << x.statements()
 << "%ctnew = add i64 %ct, 1\n"
 << "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop\n}\n";
+<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+<< "!0 = distinct !{!0, !1, !2}\n"
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }
 
 // -


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -15,6 +15,7 @@
 
 #include "loop_proto_to_llvm.h"
 #include "cxx_loop_proto.pb.h"
+#include "../handle-llvm/input_arrays.h"
 
 // The following is needed to convert protos in human-readable form
 #include 
@@ -135,7 +136,11 @@
 << x.statements()
 << "%ctnew = add i64 %ct, 1\n"
 << "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop\n}\n";
+<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+<< "!0 = distinct !{!0, !1, !2}\n"
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50530: Added LLVM metadata to generated IR to increase vectorization width

2018-08-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159993.
emmettneyman added a comment.

Switched the include statement to the cpp file


Repository:
  rC Clang

https://reviews.llvm.org/D50530

Files:
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -15,6 +15,7 @@
 
 #include "loop_proto_to_llvm.h"
 #include "cxx_loop_proto.pb.h"
+#include "../handle-llvm/input_arrays.h"
 
 // The following is needed to convert protos in human-readable form
 #include 
@@ -135,7 +136,11 @@
 << x.statements()
 << "%ctnew = add i64 %ct, 1\n"
 << "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop\n}\n";
+<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+<< "!0 = distinct !{!0, !1, !2}\n"
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }
 
 // -


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -15,6 +15,7 @@
 
 #include "loop_proto_to_llvm.h"
 #include "cxx_loop_proto.pb.h"
+#include "../handle-llvm/input_arrays.h"
 
 // The following is needed to convert protos in human-readable form
 #include 
@@ -135,7 +136,11 @@
 << x.statements()
 << "%ctnew = add i64 %ct, 1\n"
 << "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop\n}\n";
+<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+<< "!0 = distinct !{!0, !1, !2}\n"
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50530: Added LLVM metadata to generated IR to increase vectorization width

2018-08-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:141
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";

morehouse wrote:
> I'm not sure `kArraySize` is what you want here.  Does "width" refer to array 
> size or the SIMD width?
> 
> Any problem with letting the vectorizer determine this automatically?
The "width" refers to how wide the vector types should be in the outputted IR. 
So for example, a width of 64 would produce IR instructions with the type `<64 
x i32>`. 

The problem with letting the vectorizer determine the width automatically is 
that since the length of the arrays (and so, the number of loop iterations) is 
passed to the function as a parameter, the vectorizer always chooses the 
default width of 4 when running the loop vectorize optimization pass.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:142
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }

morehouse wrote:
> Does this metadata change coverage in the vectorizer?
I'm running the coverage now, but my guess is yes. Because this metadata 
overrides the default behavior of the loop vectorizer, I think it will increase 
the coverage.


Repository:
  rC Clang

https://reviews.llvm.org/D50530



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


[PATCH] D50530: Added LLVM metadata to generated IR to increase vectorization width

2018-08-09 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
emmettneyman added reviewers: morehouse, kcc.
Herald added a subscriber: cfe-commits.

Edited `loop_proto_to_llvm` to emit metadata at the end of the generated IR. 
This metadata will increase the vector width when the IR is optimized.


Repository:
  rC Clang

https://reviews.llvm.org/D50530

Files:
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
@@ -15,6 +15,8 @@
 #include 
 #include 
 
+#include "../handle-llvm/input_arrays.h"
+
 namespace clang_fuzzer {
 class LoopFunction;
 
Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -135,7 +135,11 @@
 << x.statements()
 << "%ctnew = add i64 %ct, 1\n"
 << "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop\n}\n";
+<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+<< "!0 = distinct !{!0, !1, !2}\n"
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }
 
 // -


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
@@ -15,6 +15,8 @@
 #include 
 #include 
 
+#include "../handle-llvm/input_arrays.h"
+
 namespace clang_fuzzer {
 class LoopFunction;
 
Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -135,7 +135,11 @@
 << x.statements()
 << "%ctnew = add i64 %ct, 1\n"
 << "%j = icmp eq i64 %ctnew, %s\n"
-<< "br i1 %j, label %end, label %loop\n}\n";
+<< "br i1 %j, label %end, label %loop, !llvm.loop !0\n}\n"
+<< "!0 = distinct !{!0, !1, !2}\n"
+<< "!1 = !{!\"llvm.loop.vectorize.enable\", i1 true}\n"
+<< "!2 = !{!\"llvm.loop.vectorize.width\", i32 " << kArraySize
+<< "}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50482: Added another optimization pass to make vectorizing possible

2018-08-08 Thread Emmett Neyman via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL339305: Added another optimization pass to make vectorizing 
possible (authored by emmettneyman, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D50482?vs=159838=159839#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D50482

Files:
  cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp


Index: cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -100,17 +100,29 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, ModuleTriple, 
E);
+  TargetMachine *Machine =
+  TheTarget->createTargetMachine(M->getTargetTriple(), getCPUStr(),
+ getFeaturesStr(), Options, 
getRelocModel(),
+ getCodeModel(), OLvl);
+  std::unique_ptr TM(Machine);
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  LLVMTargetMachine  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);


Index: cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -100,17 +100,29 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, ModuleTriple, E);
+  TargetMachine *Machine =
+  TheTarget->createTargetMachine(M->getTargetTriple(), getCPUStr(),
+ getFeaturesStr(), Options, getRelocModel(),
+ getCodeModel(), OLvl);
+  std::unique_ptr TM(Machine);
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  LLVMTargetMachine  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50482: Added another optimization pass to make vectorizing possible

2018-08-08 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159838.
emmettneyman added a comment.

  Rebase


Repository:
  rC Clang

https://reviews.llvm.org/D50482

Files:
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp


Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -100,17 +100,29 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, ModuleTriple, 
E);
+  TargetMachine *Machine =
+  TheTarget->createTargetMachine(M->getTargetTriple(), getCPUStr(),
+ getFeaturesStr(), Options, 
getRelocModel(),
+ getCodeModel(), OLvl);
+  std::unique_ptr TM(Machine);
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  LLVMTargetMachine  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);


Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -100,17 +100,29 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, ModuleTriple, E);
+  TargetMachine *Machine =
+  TheTarget->createTargetMachine(M->getTargetTriple(), getCPUStr(),
+ getFeaturesStr(), Options, getRelocModel(),
+ getCodeModel(), OLvl);
+  std::unique_ptr TM(Machine);
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  LLVMTargetMachine  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50482: Added another optimization pass to make vectorizing possible

2018-08-08 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:103
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();

morehouse wrote:
> I think you can avoid creating a Triple and instead just call 
> `M->getTargetTriple()` in `lookupTarget` below.
`M->getTargetTriple()` returns a `std::string` and both `lookupTarget()` and 
`TargetLibraryInfoWrapperPass()` take a `Triple` object.


Repository:
  rC Clang

https://reviews.llvm.org/D50482



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


[PATCH] D50482: Added another optimization pass to make vectorizing possible

2018-08-08 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159834.
emmettneyman added a comment.

- Inlined function


Repository:
  rC Clang

https://reviews.llvm.org/D50482

Files:
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp


Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -100,17 +100,29 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, ModuleTriple, 
E);
+  TargetMachine *Machine =
+  TheTarget->createTargetMachine(M->getTargetTriple(), getCPUStr(),
+ getFeaturesStr(), Options, 
getRelocModel(),
+ getCodeModel(), OLvl);
+  std::unique_ptr TM(Machine);
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  LLVMTargetMachine  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);


Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -100,17 +100,29 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, ModuleTriple, E);
+  TargetMachine *Machine =
+  TheTarget->createTargetMachine(M->getTargetTriple(), getCPUStr(),
+ getFeaturesStr(), Options, getRelocModel(),
+ getCodeModel(), OLvl);
+  std::unique_ptr TM(Machine);
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  LLVMTargetMachine  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50482: Added another optimization pass to make vectorizing possible

2018-08-08 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:126
+
+  auto  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));

morehouse wrote:
> Is TM guaranteed to be an LLVMTargetMachine?
Yes, since the target triple will always be X86.


Repository:
  rC Clang

https://reviews.llvm.org/D50482



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


[PATCH] D50482: Added another optimization pass to make vectorizing possible

2018-08-08 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159831.
emmettneyman added a comment.

- minor style fix


Repository:
  rC Clang

https://reviews.llvm.org/D50482

Files:
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp


Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -77,6 +77,18 @@
   std::exit(1);
 }
 
+// Helper function that returns a *TargetMachine, used by OptLLVM
+static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
+   StringRef FeaturesStr,
+   const TargetOptions ,
+   CodeGenOpt::Level OLvl) {
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, E);
+  return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
+FeaturesStr, Options, getRelocModel(),
+getCodeModel(), OLvl);
+}
+
 // Helper function to add optimization passes to the TargetMachine at the 
 // specified optimization level, OptLevel
 static void AddOptimizationPasses(legacy::PassManagerBase ,
@@ -100,17 +112,24 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::unique_ptr TM(GetTargetMachine(ModuleTriple, getCPUStr(),
+getFeaturesStr(), Options, OLvl));
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  LLVMTargetMachine  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);


Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -77,6 +77,18 @@
   std::exit(1);
 }
 
+// Helper function that returns a *TargetMachine, used by OptLLVM
+static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
+   StringRef FeaturesStr,
+   const TargetOptions ,
+   CodeGenOpt::Level OLvl) {
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, E);
+  return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
+FeaturesStr, Options, getRelocModel(),
+getCodeModel(), OLvl);
+}
+
 // Helper function to add optimization passes to the TargetMachine at the 
 // specified optimization level, OptLevel
 static void AddOptimizationPasses(legacy::PassManagerBase ,
@@ -100,17 +112,24 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::unique_ptr TM(GetTargetMachine(ModuleTriple, getCPUStr(),
+getFeaturesStr(), Options, OLvl));
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  LLVMTargetMachine  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50482: Added another optimization pass to make vectorizing possible

2018-08-08 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
emmettneyman added reviewers: morehouse, kcc.
Herald added a subscriber: cfe-commits.

I noticed that my code wasn't going deep into the loop vectorizer code so added 
another pass that makes it go further.


Repository:
  rC Clang

https://reviews.llvm.org/D50482

Files:
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp


Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -77,6 +77,18 @@
   std::exit(1);
 }
 
+// Helper function that returns a *TargetMachine, used by OptLLVM
+static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
+   StringRef FeaturesStr,
+   const TargetOptions ,
+   CodeGenOpt::Level OLvl) {
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, E);
+  return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
+FeaturesStr, Options, getRelocModel(),
+getCodeModel(), OLvl);
+}
+
 // Helper function to add optimization passes to the TargetMachine at the 
 // specified optimization level, OptLevel
 static void AddOptimizationPasses(legacy::PassManagerBase ,
@@ -100,17 +112,24 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::unique_ptr TM(GetTargetMachine(ModuleTriple, getCPUStr(),
+getFeaturesStr(), Options, OLvl));
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  auto  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);


Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -77,6 +77,18 @@
   std::exit(1);
 }
 
+// Helper function that returns a *TargetMachine, used by OptLLVM
+static TargetMachine* GetTargetMachine(Triple TheTriple, StringRef CPUStr,
+   StringRef FeaturesStr,
+   const TargetOptions ,
+   CodeGenOpt::Level OLvl) {
+  std::string E;
+  const Target *TheTarget = TargetRegistry::lookupTarget(MArch, TheTriple, E);
+  return TheTarget->createTargetMachine(TheTriple.getTriple(), CPUStr,
+FeaturesStr, Options, getRelocModel(),
+getCodeModel(), OLvl);
+}
+
 // Helper function to add optimization passes to the TargetMachine at the 
 // specified optimization level, OptLevel
 static void AddOptimizationPasses(legacy::PassManagerBase ,
@@ -100,17 +112,24 @@
   if (!M || verifyModule(*M, ()))
 ErrorAndExit("Could not parse IR");
 
+  Triple ModuleTriple(M->getTargetTriple());
+  const TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
+  std::unique_ptr TM(GetTargetMachine(ModuleTriple, getCPUStr(),
+getFeaturesStr(), Options, OLvl));
   setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
-  
+
   legacy::PassManager Passes;
-  Triple ModuleTriple(M->getTargetTriple());
   
   Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
-  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createTargetTransformInfoWrapperPass(TM->getTargetIRAnalysis()));
+
+  auto  = static_cast(*TM);
+  Passes.add(LTM.createPassConfig(Passes));
+
   Passes.add(createVerifierPass());
 
   AddOptimizationPasses(Passes, OLvl, 0);
-  
+
   // Add a pass that writes the optimized IR to an output stream
   std::string outString;
   raw_string_ostream OS(outString);
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50342: Changed how LLVM IR was generated to increase vectorization

2018-08-06 Thread Emmett Neyman via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL339080: Changed how LLVM IR was generated to increase 
vectorization (authored by emmettneyman, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D50342?vs=159425=159427#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D50342

Files:
  cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -53,7 +53,7 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 
%ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,20 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) 
{\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"
+<< "start:\n"
+<< "br label %loop\n"
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"
+<< "br i1 %j, label %end, label %loop\n}\n";
 }
 
 // -


Index: cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ cfe/trunk/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -53,7 +53,7 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,20 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"
+<< "start:\n"
+<< "br label %loop\n"
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"
+<< "br i1 %j, label %end, label %loop\n}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50342: Changed how LLVM IR was generated to increase vectorization

2018-08-06 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159425.
emmettneyman added a comment.

  Rebased and ready to land


Repository:
  rC Clang

https://reviews.llvm.org/D50342

Files:
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -53,7 +53,7 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 
%ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,20 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) 
{\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"
+<< "start:\n"
+<< "br label %loop\n"
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"
+<< "br i1 %j, label %end, label %loop\n}\n";
 }
 
 // -


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -53,7 +53,7 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,20 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"
+<< "start:\n"
+<< "br label %loop\n"
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"
+<< "br i1 %j, label %end, label %loop\n}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50342: Changed how LLVM IR was generated to increase vectorization

2018-08-06 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159421.
emmettneyman added a comment.

Some small fixes to improve simplicity of generated IR


Repository:
  rC Clang

https://reviews.llvm.org/D50342

Files:
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -53,7 +53,7 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 
%ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,20 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) 
{\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"
+<< "start:\n"
+<< "br label %loop\n"
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"
+<< "br i1 %j, label %end, label %loop\n}\n";
 }
 
 // -


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -53,7 +53,7 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,20 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"
+<< "start:\n"
+<< "br label %loop\n"
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"
+<< "br i1 %j, label %end, label %loop\n}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50342: Changed how LLVM IR was generated to increase vectorization

2018-08-06 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:127
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"

morehouse wrote:
> Should `%s` be signed?  Do we want unsigned compare here?
I don't think it matters since `%s` will always be positive (it will always be 
equal to 
`kArraySize` from `input_arrays.h`). 



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:134
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"

morehouse wrote:
> Seems like the `endloop` label is unnecessary.  Does this help vectorize?  If 
> not, lets get rid of unconditional jumps to the next line.
I initially had it this way since that's how `-emit-llvm` generated the code. 
But it doesn't seem like it's necessary for loops to be vectorized upon further 
inspection.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:138
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add nuw nsw i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"

morehouse wrote:
> This will make overflow undefined... Isn't that the opposite of what we want? 
>  That will permit LLVM to assume overflow never happens and modify the code 
> in new ways based on that assumption.
That's a good point. It probably won't matter since this addition will never 
overflow (we're just starting at zero and incrementing by one). This is what 
was generated by `-emit-llvm`. I'll remove it to reduce confusion.


Repository:
  rC Clang

https://reviews.llvm.org/D50342



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


[PATCH] D50342: Changed how LLVM IR was generated to increase vectorization

2018-08-06 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159389.
emmettneyman added a comment.

Added back more descriptive variable and loop names


Repository:
  rC Clang

https://reviews.llvm.org/D50342

Files:
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -53,7 +53,7 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 
%ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,22 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) 
{\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"
+<< "start:\n"
+<< "br label %loop\n"
+<< "endloop:\n"
+<< "br label %end\n"
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add nuw nsw i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"
+<< "br i1 %j, label %endloop, label %loop\n}\n";
 }
 
 // -


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -53,7 +53,7 @@
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,22 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n"
+<< "%1 = icmp sgt i64 %s, 0\n"
+<< "br i1 %1, label %start, label %end\n"
+<< "start:\n"
+<< "br label %loop\n"
+<< "endloop:\n"
+<< "br label %end\n"
+<< "end:\n"
+<< "ret void\n"
 << "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+<< " %ct   = phi i64 [ %ctnew, %loop ], [ 0, %start ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%ctnew = add nuw nsw i64 %ct, 1\n"
+<< "%j = icmp eq i64 %ctnew, %s\n"
+<< "br i1 %j, label %endloop, label %loop\n}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50342: Changed how LLVM IR was generated to increase vectorization

2018-08-06 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159373.
emmettneyman added a comment.

Changed pc to unknown


Repository:
  rC Clang

https://reviews.llvm.org/D50342

Files:
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -43,17 +43,17 @@
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
-arr = "%a";
+arr = "%0";
 break;
   case VarRef::ARR_B:
-arr = "%b";
+arr = "%1";
 break;
   case VarRef::ARR_C:
-arr = "%c";
+arr = "%2";
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 
%ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,22 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) 
{\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
-<< "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32*, i32*, i32*, i64) {\n"
+<< "%5 = icmp sgt i64 %3, 0\n"
+<< "br i1 %5, label %6, label %8\n"
+<< "; :6:\n"
+<< "br label %9\n"
+<< "; :7:\n"
+<< "br label %8\n"
+<< "; :8:\n"
+<< "ret void\n"
+<< "; :9:\n"
+<< " %ct   = phi i64 [ %10, %9 ], [ 0, %6 ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%10 = add nuw nsw i64 %ct, 1\n"
+<< "%11 = icmp eq i64 %10, %3\n"
+<< "br i1 %11, label %7, label %9\n}\n";
 }
 
 // -


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -43,17 +43,17 @@
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
-arr = "%a";
+arr = "%0";
 break;
   case VarRef::ARR_B:
-arr = "%b";
+arr = "%1";
 break;
   case VarRef::ARR_C:
-arr = "%c";
+arr = "%2";
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,22 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
-<< "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+  return os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+<< "define void @foo(i32*, i32*, i32*, i64) {\n"
+<< "%5 = icmp sgt i64 %3, 0\n"
+<< "br i1 %5, label %6, label %8\n"
+<< "; :6:\n"
+<< "br label %9\n"
+<< "; :7:\n"
+<< "br label %8\n"
+<< "; :8:\n"
+<< "ret void\n"
+<< "; :9:\n"
+<< " %ct   = phi i64 [ %10, %9 ], [ 0, %6 ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%10 = add nuw nsw i64 %ct, 1\n"
+<< "%11 = icmp eq i64 %10, %3\n"
+<< "br i1 %11, label %7, label %9\n}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50342: Changed how LLVM IR was generated to increase vectorization

2018-08-06 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:125
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) 
{\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
-<< "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+  return os << "target triple = \"x86_64-pc-linux-gnu\"\n"
+<< "define void @foo(i32*, i32*, i32*, i64) {\n"

morehouse wrote:
> What does `pc` mean in this triple?  I'm used to seeing 
> `x86_64-unknown-linux-gnu`.
It's probably generating `pc` since I'm using my desktop. I'll change it to 
`unknown` so it's not platform-specific.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:126
+  return os << "target triple = \"x86_64-pc-linux-gnu\"\n"
+<< "define void @foo(i32*, i32*, i32*, i64) {\n"
+<< "%5 = icmp sgt i64 %3, 0\n"

morehouse wrote:
> Does removing the variable names really make this easier to vectorize?
It doesn't, I just thought it was cleaner and produced slightly smaller IR. It 
also more closely mimics the behavior of the `-emit-llvm` flag.



Comment at: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:129
+<< "br i1 %5, label %6, label %8\n"
+<< "; :6:\n"
+<< "br label %9\n"

morehouse wrote:
> Does removing branch names really make this easier to vectorize?
Same answer as above. Should I change these back?


Repository:
  rC Clang

https://reviews.llvm.org/D50342



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


[PATCH] D50342: Changed how LLVM IR was generated to increase vectorization

2018-08-06 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
emmettneyman added reviewers: morehouse, kcc.
Herald added a subscriber: cfe-commits.

Changed the structure of the generated IR to make it easier to vectorize


Repository:
  rC Clang

https://reviews.llvm.org/D50342

Files:
  clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -43,17 +43,17 @@
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
-arr = "%a";
+arr = "%0";
 break;
   case VarRef::ARR_B:
-arr = "%b";
+arr = "%1";
 break;
   case VarRef::ARR_C:
-arr = "%c";
+arr = "%2";
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 
%ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,22 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) 
{\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
-<< "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+  return os << "target triple = \"x86_64-pc-linux-gnu\"\n"
+<< "define void @foo(i32*, i32*, i32*, i64) {\n"
+<< "%5 = icmp sgt i64 %3, 0\n"
+<< "br i1 %5, label %6, label %8\n"
+<< "; :6:\n"
+<< "br label %9\n"
+<< "; :7:\n"
+<< "br label %8\n"
+<< "; :8:\n"
+<< "ret void\n"
+<< "; :9:\n"
+<< " %ct   = phi i64 [ %10, %9 ], [ 0, %6 ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%10 = add nuw nsw i64 %ct, 1\n"
+<< "%11 = icmp eq i64 %10, %3\n"
+<< "br i1 %11, label %7, label %9\n}\n";
 }
 
 // -


Index: clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
+++ clang/tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -43,17 +43,17 @@
   std::string arr;
   switch(x.arr()) {
   case VarRef::ARR_A:
-arr = "%a";
+arr = "%0";
 break;
   case VarRef::ARR_B:
-arr = "%b";
+arr = "%1";
 break;
   case VarRef::ARR_C:
-arr = "%c";
+arr = "%2";
 break;
   }
   std::string ptr_var = get_var();
-  os << ptr_var << " = getelementptr i32, i32* " << arr << ", i64 %ct\n";
+  os << ptr_var << " = getelementptr inbounds i32, i32* " << arr << ", i64 %ct\n";
   return ptr_var;
 }
 std::string RvalueToString(std::ostream , const Rvalue ) {
@@ -122,21 +122,22 @@
   return os;
 }
 std::ostream <<(std::ostream , const LoopFunction ) {
-  return os << "define void @foo(i32* %a, i32* %b, i32* noalias %c, i64 %s) {\n"
-<< "%i = alloca i64\n"
-<< "store i64 0, i64* %i\n"
-<< "br label %loop\n\n"
-<< "loop:\n"
-<< "%ct = load i64, i64* %i\n"
-<< "%comp = icmp eq i64 %ct, %s\n"
-<< "br i1 %comp, label %endloop, label %body\n\n"
-<< "body:\n"
+  return os << "target triple = \"x86_64-pc-linux-gnu\"\n"
+<< "define void @foo(i32*, i32*, i32*, i64) {\n"
+<< "%5 = icmp sgt i64 %3, 0\n"
+<< "br i1 %5, label %6, label %8\n"
+<< "; :6:\n"
+<< "br label %9\n"
+<< "; :7:\n"
+<< "br label %8\n"
+<< "; :8:\n"
+<< "ret void\n"
+<< "; :9:\n"
+<< " %ct   = phi i64 [ %10, %9 ], [ 0, %6 ]\n"
 << x.statements()
-<< "%z = add i64 1, %ct\n"
-<< "store i64 %z, i64* %i\n"
-<< "br label %loop\n\n"
-<< "endloop:\n"
-<< "ret void\n}\n";
+<< "%10 = add nuw nsw i64 %ct, 1\n"
+<< "%11 = icmp eq i64 %10, %3\n"
+<< "br i1 %11, label %7, label %9\n}\n";
 }
 
 // -
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-03 Thread Emmett Neyman via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rC338943: LLVM Proto Fuzzer - Run Functions on Suite of Inputs 
(authored by emmettneyman, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D50194?vs=159151=159152#toc

Repository:
  rC Clang

https://reviews.llvm.org/D50194

Files:
  tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  tools/clang-fuzzer/handle-llvm/handle_llvm.h
  tools/clang-fuzzer/handle-llvm/input_arrays.h

Index: tools/clang-fuzzer/handle-llvm/input_arrays.h
===
--- tools/clang-fuzzer/handle-llvm/input_arrays.h
+++ tools/clang-fuzzer/handle-llvm/input_arrays.h
@@ -0,0 +1,118 @@
+//==-- input_arrays.h - Helper function for LLVM fuzzer inputs -==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Define a few static variables used by the LLVM Proto Fuzzer
+//
+//===--===//
+
+#include 
+
+static const int kArraySize = 64;
+static const int kNumArrays = 93;
+static const int kTotalSize = sizeof(int) * kArraySize * kNumArrays;
+
+// Define two arrays that will hold the input and output for the two functions
+static int OptArrays[kNumArrays][kArraySize];
+static int UnoptArrays[kNumArrays][kArraySize];
+
+// Define a corpus of possible inputs
+static int InputArrays[kNumArrays][kArraySize] =
+{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX},
+  {INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN},
+  {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+  {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
+  {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
+  {1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 

[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-03 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159151.
emmettneyman added a comment.

ready to land


Repository:
  rC Clang

https://reviews.llvm.org/D50194

Files:
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
  clang/tools/clang-fuzzer/handle-llvm/input_arrays.h

Index: clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
===
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
@@ -0,0 +1,118 @@
+//==-- input_arrays.h - Helper function for LLVM fuzzer inputs -==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Define a few static variables used by the LLVM Proto Fuzzer
+//
+//===--===//
+
+#include 
+
+static const int kArraySize = 64;
+static const int kNumArrays = 93;
+static const int kTotalSize = sizeof(int) * kArraySize * kNumArrays;
+
+// Define two arrays that will hold the input and output for the two functions
+static int OptArrays[kNumArrays][kArraySize];
+static int UnoptArrays[kNumArrays][kArraySize];
+
+// Define a corpus of possible inputs
+static int InputArrays[kNumArrays][kArraySize] =
+{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX},
+  {INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN},
+  {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+  {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
+  {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
+  {1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024},
+  {65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 

[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-03 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159146.
emmettneyman added a comment.

New input arrays and minor fixes


Repository:
  rC Clang

https://reviews.llvm.org/D50194

Files:
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
  clang/tools/clang-fuzzer/handle-llvm/input_arrays.h

Index: clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
===
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
@@ -0,0 +1,118 @@
+//==-- input_arrays.h - Helper function for LLVM fuzzer inputs -==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Define a few static variables used by the LLVM Proto Fuzzer
+//
+//===--===//
+
+#include 
+
+static const int kArraySize = 64;
+static const int kNumArrays = 93;
+static const int kTotalSize = sizeof(int) * kArraySize * kNumArrays;
+
+// Define two arrays that will hold the input and output for the two functions
+static int OptArrays[kNumArrays][kArraySize];
+static int UnoptArrays[kNumArrays][kArraySize];
+
+// Define a corpus of possible inputs
+static int InputArrays[kNumArrays][kArraySize] =
+{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, -1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX, INT_MAX},
+  {INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN, INT_MIN},
+  {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+  {16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
+  {255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255},
+  {1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024, 1024},
+  {65535, 65535, 65535, 65535, 65535, 65535, 65535, 65535, 

[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-03 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159100.
emmettneyman added a comment.

Added static to some functions, made small fixes


Repository:
  rC Clang

https://reviews.llvm.org/D50194

Files:
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
  clang/tools/clang-fuzzer/handle-llvm/input_arrays.h

Index: clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
===
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
@@ -0,0 +1,126 @@
+//==-- input_arrays.h - Helper function for LLVM fuzzer inputs -==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Define a few static variables used by the LLVM Proto Fuzzer
+//
+//===--===//
+
+#include 
+#include 
+
+static const int ArraySize = 64;
+static const int NumArrays = 100;
+static const int TotalSize = sizeof(int) * ArraySize * NumArrays;
+
+// Define two arrays that will hold the input and output for the two functions
+static int OptArrays[NumArrays][ArraySize];
+static int UnoptArrays[NumArrays][ArraySize];
+
+// Define a corpus of possible inputs
+static int InputArrays[NumArrays][ArraySize] =
+{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+  {0, 0, 0, 5, 0, 2, 0, 8, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
+  {1, 1, 2, 1, 6, 0, 3, 1, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
+  {0, 0, 2, 0, 0, 7, 0, 4, 2, 0, 12, 12, 0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
+  {1, 1, 0, 2, 2, 9, 8, 0, 5, 3, 1, 14, 14, 14, 0, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
+  {0, 0, 0, 4, 0, 1, 10, 9, 0, 6, 4, 2, 0, 16, 16, 16, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
+  {1, 1, 2, 3, 2, 3, 0, 11, 10, 0, 7, 5, 3, 1, 18, 18, 18, 18, 0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18},
+  {0, 0, 2, 5, 4, 0, 2, 13, 12, 11, 0, 8, 6, 4, 2, 0, 20, 20, 20, 20, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20},
+  {1, 1, 0, 1, 6, 2, 4, 1, 14, 13, 12, 0, 9, 7, 5, 3, 1, 22, 22, 22, 22, 22, 0, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22},
+  {0, 0, 0, 0, 4, 4, 0, 3, 0, 15, 14, 13, 0, 10, 8, 6, 4, 2, 0, 24, 24, 24, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24},
+  {1, 1, 2, 2, 6, 6, 2, 5, 2, 17, 16, 15, 14, 0, 11, 9, 7, 5, 3, 1, 26, 26, 26, 26, 26, 26, 0, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26},
+  {0, 0, 2, 4, 0, 8, 4, 0, 4, 1, 18, 17, 16, 15, 0, 12, 10, 8, 6, 4, 2, 0, 28, 28, 28, 28, 28, 28, 0, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 

[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-03 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/input_arrays.h:36
+  {1, 1, 2, 3, 2, 3, 0, 11, 10, 0, 7, 5, 3, 1, 18, 18, 18, 18, 0, 18, 18, 18, 
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 
18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 
18, 18},
+  {0, 0, 2, 5, 4, 0, 2, 13, 12, 11, 0, 8, 6, 4, 2, 0, 20, 20, 20, 20, 0, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 
20, 20},
+  {1, 1, 0, 1, 6, 2, 4, 1, 14, 13, 12, 0, 9, 7, 5, 3, 1, 22, 22, 22, 22, 22, 
0, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 
22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 
22, 22},

kcc wrote:
> constants are not diverse enough. 
Yeah, I will completely redo the input arrays.


Repository:
  rC Clang

https://reviews.llvm.org/D50194



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


[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-03 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 159083.
emmettneyman added a comment.

Refactored code to avoid memcpy-ing function


Repository:
  rC Clang

https://reviews.llvm.org/D50194

Files:
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
  clang/tools/clang-fuzzer/handle-llvm/input_arrays.h

Index: clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
===
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
@@ -0,0 +1,125 @@
+//==-- input_arrays.h - Helper function for LLVM fuzzer inputs -==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Define a few static variables used by the LLVM Proto Fuzzer
+//
+//===--===//
+
+#include 
+#include 
+
+static const int ArraySize = 64;
+static const int NumArrays = 100;
+
+// Define two arrays that will hold the input and output for the two functions
+static int OptArrays[NumArrays][ArraySize];
+static int UnoptArrays[NumArrays][ArraySize];
+
+// Define a corpus of possible inputs
+static int InputArrays[NumArrays][ArraySize] =
+{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
+  {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1},
+  {0, 0, 0, 5, 0, 2, 0, 8, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
+  {1, 1, 2, 1, 6, 0, 3, 1, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
+  {0, 0, 2, 0, 0, 7, 0, 4, 2, 0, 12, 12, 0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
+  {1, 1, 0, 2, 2, 9, 8, 0, 5, 3, 1, 14, 14, 14, 0, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
+  {0, 0, 0, 4, 0, 1, 10, 9, 0, 6, 4, 2, 0, 16, 16, 16, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
+  {1, 1, 2, 3, 2, 3, 0, 11, 10, 0, 7, 5, 3, 1, 18, 18, 18, 18, 0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18},
+  {0, 0, 2, 5, 4, 0, 2, 13, 12, 11, 0, 8, 6, 4, 2, 0, 20, 20, 20, 20, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20},
+  {1, 1, 0, 1, 6, 2, 4, 1, 14, 13, 12, 0, 9, 7, 5, 3, 1, 22, 22, 22, 22, 22, 0, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22},
+  {0, 0, 0, 0, 4, 4, 0, 3, 0, 15, 14, 13, 0, 10, 8, 6, 4, 2, 0, 24, 24, 24, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24},
+  {1, 1, 2, 2, 6, 6, 2, 5, 2, 17, 16, 15, 14, 0, 11, 9, 7, 5, 3, 1, 26, 26, 26, 26, 26, 26, 0, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26, 26},
+  {0, 0, 2, 4, 0, 8, 4, 0, 4, 1, 18, 17, 16, 15, 0, 12, 10, 8, 6, 4, 2, 0, 28, 28, 28, 28, 28, 28, 0, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28, 28},
+  {1, 1, 0, 3, 2, 5, 6, 2, 6, 3, 0, 19, 

[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-03 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added a comment.

An unrelated question:
Right now I have a mix of `static` and non-`static` functions in 
`handle_llvm.cpp`. Should they all be `static`?


Repository:
  rC Clang

https://reviews.llvm.org/D50194



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


[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-03 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:173
+  int s = getSize((char *) func_ptr);
+  memcpy(mem, func_ptr, s);
+}

morehouse wrote:
> emmettneyman wrote:
> > morehouse wrote:
> > > Why do we need to copy the function somewhere else?  Looks very 
> > > error-prone and unnecessary.  Also makes this patch larger than it needs 
> > > to be.
> > I'm copying the functions because otherwise, the generated machine code 
> > gets lost as soon as we exit that function's scope. So I'd have to run the 
> > functions inside `CreateJITFunction` if I don't copy it.
> > 
> > I thought about doing it this way: moving the code from `RunFuncsOnInputs` 
> > to the bottom of `CreateJITFunction` and then comparing the arrays after 
> > both calls to `CreateJITFunction` inside `HandleLLVM`. Do you think that 
> > would be cleaner?
> Or just increase the scope of `EE`.
When I tried to increase the scope of `EE` (and also `M` since `EntryFunc` 
lives inside the module), the program segfaulted immediately after exiting 
`CreateJITFunc`, I think while trying to deconstruct an object from the 
function. I'm not sure if there's a way around this since there are so many 
objects being created inside `CreateJITFunc`. But it's definitely possible I'm 
missing something with how `unique_ptr` work.


Repository:
  rC Clang

https://reviews.llvm.org/D50194



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


[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-02 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 158800.
emmettneyman added a comment.

Replaced hardcoded numbers with variables


Repository:
  rC Clang

https://reviews.llvm.org/D50194

Files:
  clang/tools/clang-fuzzer/CMakeLists.txt
  clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
  clang/tools/clang-fuzzer/handle-llvm/input_arrays.cpp
  clang/tools/clang-fuzzer/handle-llvm/input_arrays.h

Index: clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
===
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
@@ -0,0 +1,134 @@
+//==-- input_arrays.h - Helper function for LLVM fuzzer inputs -==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Declares helper functions for getting the inputs on which to call the
+// compiled functions.
+//
+//===--===//
+
+#include 
+#include 
+
+static const int ArraySize = 64;
+static const int NumArrays = 100;
+
+// Declare the arrays that will be fed to the compiled functions
+static int a1[ArraySize];
+static int b1[ArraySize];
+static int c1[ArraySize];
+static int a2[ArraySize];
+static int b2[ArraySize];
+static int c2[ArraySize];
+
+void UpdateArrays();
+
+std::string ArrayToString(int *arr, int s);
+
+// Define a corpus of possible inputs
+static int InputArrays[NumArrays][ArraySize] =
+{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
+  {1, 1, 0, 0, 1, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6},
+  {0, 0, 0, 5, 0, 2, 0, 8, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
+  {1, 1, 2, 1, 6, 0, 3, 1, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
+  {0, 0, 2, 0, 0, 7, 0, 4, 2, 0, 12, 12, 0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
+  {1, 1, 0, 2, 2, 9, 8, 0, 5, 3, 1, 14, 14, 14, 0, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
+  {0, 0, 0, 4, 0, 1, 10, 9, 0, 6, 4, 2, 0, 16, 16, 16, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
+  {1, 1, 2, 3, 2, 3, 0, 11, 10, 0, 7, 5, 3, 1, 18, 18, 18, 18, 0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18},
+  {0, 0, 2, 5, 4, 0, 2, 13, 12, 11, 0, 8, 6, 4, 2, 0, 20, 20, 20, 20, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20},
+  {1, 1, 0, 1, 6, 2, 4, 1, 14, 13, 12, 0, 9, 7, 5, 3, 1, 22, 22, 22, 22, 22, 0, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22},
+  {0, 0, 0, 0, 4, 4, 0, 3, 0, 15, 14, 13, 0, 10, 8, 6, 4, 2, 0, 24, 24, 24, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24},
+  {1, 1, 2, 2, 6, 6, 2, 5, 2, 17, 16, 15, 14, 0, 11, 9, 7, 5, 3, 1, 26, 26, 26, 26, 26, 26, 0, 26, 26, 26, 

[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-02 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:173
+  int s = getSize((char *) func_ptr);
+  memcpy(mem, func_ptr, s);
+}

morehouse wrote:
> Why do we need to copy the function somewhere else?  Looks very error-prone 
> and unnecessary.  Also makes this patch larger than it needs to be.
I'm copying the functions because otherwise, the generated machine code gets 
lost as soon as we exit that function's scope. So I'd have to run the functions 
inside `CreateJITFunction` if I don't copy it.

I thought about doing it this way: moving the code from `RunFuncsOnInputs` to 
the bottom of `CreateJITFunction` and then comparing the arrays after both 
calls to `CreateJITFunction` inside `HandleLLVM`. Do you think that would be 
cleaner?



Comment at: clang/tools/clang-fuzzer/handle-llvm/input_arrays.cpp:30
+  memcpy(b2, InputArrays[b_index], ArraySize * sizeof(int));
+  memcpy(c2, InputArrays[c_index], ArraySize * sizeof(int));
+}

morehouse wrote:
> Do the generated functions ever modify arrays a and b, or just c?  If just c, 
> we can avoid lots of memcpys here.
Right now the generated functions can modify any of the arrays.


Repository:
  rC Clang

https://reviews.llvm.org/D50194



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


[PATCH] D50194: LLVM Proto Fuzzer - Run Functions on Suite of Inputs

2018-08-02 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
emmettneyman added reviewers: kcc, morehouse.
Herald added subscribers: cfe-commits, mgorny.

Added corpus of arrays to use as inputs for the functions. Check that the two
functions modify the inputted arrays in the same way.


Repository:
  rC Clang

https://reviews.llvm.org/D50194

Files:
  clang/tools/clang-fuzzer/CMakeLists.txt
  clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
  clang/tools/clang-fuzzer/handle-llvm/input_arrays.cpp
  clang/tools/clang-fuzzer/handle-llvm/input_arrays.h

Index: clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
===
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/input_arrays.h
@@ -0,0 +1,134 @@
+//==-- input_arrays.h - Helper function for LLVM fuzzer inputs -==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Declares helper functions for getting the inputs on which to call the
+// compiled functions.
+//
+//===--===//
+
+#include 
+#include 
+
+static const int ArraySize = 64;
+static const int NumArrays = 100;
+
+// Declare the arrays that will be fed to the compiled functions
+static int a1[ArraySize];
+static int b1[ArraySize];
+static int c1[ArraySize];
+static int a2[ArraySize];
+static int b2[ArraySize];
+static int c2[ArraySize];
+
+void UpdateArrays();
+
+std::string ArrayToString(int *arr, int s);
+
+// Define a corpus of possible inputs
+static int InputArrays[100][64] =
+{ {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
+  {0, 0, 0, 0, 0, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4},
+  {1, 1, 0, 0, 1, 6, 0, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6, 6},
+  {0, 0, 0, 5, 0, 2, 0, 8, 0, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8, 8},
+  {1, 1, 2, 1, 6, 0, 3, 1, 10, 10, 0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10},
+  {0, 0, 2, 0, 0, 7, 0, 4, 2, 0, 12, 12, 0, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12, 12},
+  {1, 1, 0, 2, 2, 9, 8, 0, 5, 3, 1, 14, 14, 14, 0, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14, 14},
+  {0, 0, 0, 4, 0, 1, 10, 9, 0, 6, 4, 2, 0, 16, 16, 16, 0, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16, 16},
+  {1, 1, 2, 3, 2, 3, 0, 11, 10, 0, 7, 5, 3, 1, 18, 18, 18, 18, 0, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18, 18},
+  {0, 0, 2, 5, 4, 0, 2, 13, 12, 11, 0, 8, 6, 4, 2, 0, 20, 20, 20, 20, 0, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20, 20},
+  {1, 1, 0, 1, 6, 2, 4, 1, 14, 13, 12, 0, 9, 7, 5, 3, 1, 22, 22, 22, 22, 22, 0, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22, 22},
+  {0, 0, 0, 0, 4, 4, 0, 3, 0, 15, 14, 13, 0, 10, 8, 6, 4, 2, 0, 24, 24, 24, 24, 24, 0, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 24, 

[PATCH] D49895: added shared library to fix buildbot

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
This revision was not accepted when it landed; it landed in state "Needs 
Review".
This revision was automatically updated to reflect the committed changes.
Closed by commit rC338091: added shared library to fix buildbot (authored by 
emmettneyman, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D49895?vs=157618=157620#toc

Repository:
  rC Clang

https://reviews.llvm.org/D49895

Files:
  tools/clang-fuzzer/handle-llvm/CMakeLists.txt


Index: tools/clang-fuzzer/handle-llvm/CMakeLists.txt
===
--- tools/clang-fuzzer/handle-llvm/CMakeLists.txt
+++ tools/clang-fuzzer/handle-llvm/CMakeLists.txt
@@ -1,7 +1,9 @@
 set(LLVM_LINK_COMPONENTS
+  Analysis
   CodeGen
   Core
   ExecutionEngine
+  IPO
   IRReader
   MC
   MCJIT


Index: tools/clang-fuzzer/handle-llvm/CMakeLists.txt
===
--- tools/clang-fuzzer/handle-llvm/CMakeLists.txt
+++ tools/clang-fuzzer/handle-llvm/CMakeLists.txt
@@ -1,7 +1,9 @@
 set(LLVM_LINK_COMPONENTS
+  Analysis
   CodeGen
   Core
   ExecutionEngine
+  IPO
   IRReader
   MC
   MCJIT
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49895: added shared library to fix buildbot

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
Herald added subscribers: cfe-commits, mgorny.

added shared library to fix buildbot


Repository:
  rC Clang

https://reviews.llvm.org/D49895

Files:
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt


Index: clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
===
--- clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
+++ clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
@@ -1,7 +1,9 @@
 set(LLVM_LINK_COMPONENTS
+  Analysis
   CodeGen
   Core
   ExecutionEngine
+  IPO
   IRReader
   MC
   MCJIT


Index: clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
===
--- clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
+++ clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
@@ -1,7 +1,9 @@
 set(LLVM_LINK_COMPONENTS
+  Analysis
   CodeGen
   Core
   ExecutionEngine
+  IPO
   IRReader
   MC
   MCJIT
___
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits


[PATCH] D49892: Added shared library to fix build bot

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
emmettneyman added a reviewer: morehouse.
Herald added subscribers: cfe-commits, mgorny.

Repository:
  rC Clang

https://reviews.llvm.org/D49892

Files:
  clang/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,48 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs a loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Vectorize.h"
 
 using namespace llvm;
 
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +68,111 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+void ErrorAndExit(std::string message) {
+  errs()<< "ERROR: " << message << "\n";
+  std::exit(1);
+}
 
-  // Set the Module to include the the IR code to be compiled
-  SMDiagnostic Err;
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  CodeGenOpt::Level OptLevel,
+  unsigned SizeLevel) {
+  // Create and initialize a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateModulePassManager(MPM);
+}
 
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string , CodeGenOpt::Level OLvl) {
+  // Create a module that will run the optimization passes
+  SMDiagnostic Err;
   LLVMContext Context;
-  std::unique_ptr M = parseIR(MemoryBufferRef(S, "IR"), Err, Context);
-  if (!M) {
-errs() << "error: could not parse IR!\n";
-std::exit(1);
-  }
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M || verifyModule(*M, ()))
+ErrorAndExit("Could not parse IR");
 
-  // Create a new Target
-  std::string Error;
-  const Target *TheTarget = TargetRegistry::lookupTarget(
-  sys::getDefaultTargetTriple(), Error);
-  if (!TheTarget) {
-errs() << Error;
-std::exit(1);
-  }
+  setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
+  
+  legacy::PassManager Passes;
+  Triple ModuleTriple(M->getTargetTriple());
+  
+  Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createVerifierPass());
 
-  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
-
-  // Create a new Machine
-  std::string CPUStr = getCPUStr();
-  

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
This revision was automatically updated to reflect the committed changes.
Closed by commit rL338077: Updated llvm-proto-fuzzer to execute the compiled 
code (authored by emmettneyman, committed by ).

Changed prior to commit:
  https://reviews.llvm.org/D49526?vs=157577=157591#toc

Repository:
  rL LLVM

https://reviews.llvm.org/D49526

Files:
  cfe/trunk/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  cfe/trunk/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: cfe/trunk/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
===
--- cfe/trunk/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
+++ cfe/trunk/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
@@ -16,10 +16,13 @@
 
 #include "fuzzer_initialize.h"
 
+#include "llvm/InitializePasses.h"
+#include "llvm/PassRegistry.h"
 #include "llvm/Support/TargetSelect.h"
 #include 
 
 using namespace clang_fuzzer;
+using namespace llvm;
 
 
 namespace clang_fuzzer {
@@ -33,10 +36,22 @@
 }
 
 extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {
-  llvm::InitializeAllTargets();
-  llvm::InitializeAllTargetMCs();
-  llvm::InitializeAllAsmPrinters();
-  llvm::InitializeAllAsmParsers();
+  InitializeAllTargets();
+  InitializeAllTargetMCs();
+  InitializeAllAsmPrinters();
+  InitializeAllAsmParsers();
+  
+  PassRegistry  = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeVectorization(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeAggressiveInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
 
   CLArgs.push_back("-O2");
   for (int I = 1; I < *argc; I++) {
Index: cfe/trunk/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
===
--- cfe/trunk/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
+++ cfe/trunk/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
@@ -1,10 +1,18 @@
 set(LLVM_LINK_COMPONENTS
+  CodeGen
   Core
+  ExecutionEngine
   IRReader
   MC
+  MCJIT
+  Object
+  RuntimeDyld
+  SelectionDAG
   Support
-  Analysis
-  )
+  Target
+  TransformUtils
+  native
+)
 
 # Depend on LLVM IR intrinsic generation.
 set(handle_llvm_deps intrinsics_gen)
Index: cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ cfe/trunk/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,48 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs a loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Vectorize.h"
 
 using namespace llvm;
 
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +68,111 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 157577.
emmettneyman added a comment.

Made some minor fixes


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,48 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs a loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Vectorize.h"
 
 using namespace llvm;
 
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +68,111 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+void ErrorAndExit(std::string message) {
+  errs()<< "ERROR: " << message << "\n";
+  std::exit(1);
+}
 
-  // Set the Module to include the the IR code to be compiled
-  SMDiagnostic Err;
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  CodeGenOpt::Level OptLevel,
+  unsigned SizeLevel) {
+  // Create and initialize a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateModulePassManager(MPM);
+}
 
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string , CodeGenOpt::Level OLvl) {
+  // Create a module that will run the optimization passes
+  SMDiagnostic Err;
   LLVMContext Context;
-  std::unique_ptr M = parseIR(MemoryBufferRef(S, "IR"), Err, Context);
-  if (!M) {
-errs() << "error: could not parse IR!\n";
-std::exit(1);
-  }
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M || verifyModule(*M, ()))
+ErrorAndExit("Could not parse IR");
 
-  // Create a new Target
-  std::string Error;
-  const Target *TheTarget = TargetRegistry::lookupTarget(
-  sys::getDefaultTargetTriple(), Error);
-  if (!TheTarget) {
-errs() << Error;
-std::exit(1);
-  }
+  setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
+  
+  legacy::PassManager Passes;
+  Triple ModuleTriple(M->getTargetTriple());
+  
+  Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createVerifierPass());
 
-  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
-
-  // Create a new Machine
-  std::string CPUStr = getCPUStr();
-  std::string FeaturesStr = 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 157553.
emmettneyman added a comment.

Changed int to CodeGenOpt::Level and fixed unique_ptr issue


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,48 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs a loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Vectorize.h"
 
 using namespace llvm;
 
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +68,112 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+void ErrorAndExit(std::string message) {
+  errs()<< "ERROR: " << message << "\n";
+  std::exit(1);
+}
 
-  // Set the Module to include the the IR code to be compiled
-  SMDiagnostic Err;
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  CodeGenOpt::Level OptLevel,
+  unsigned SizeLevel) {
+  // Create and initialize a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateModulePassManager(MPM);
+}
 
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string , CodeGenOpt::Level OLvl) {
+  // Create a module that will run the optimization passes
+  SMDiagnostic Err;
   LLVMContext Context;
-  std::unique_ptr M = parseIR(MemoryBufferRef(S, "IR"), Err, Context);
-  if (!M) {
-errs() << "error: could not parse IR!\n";
-std::exit(1);
-  }
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M || verifyModule(*M, ()))
+ErrorAndExit("Could not parse IR");
 
-  // Create a new Target
-  std::string Error;
-  const Target *TheTarget = TargetRegistry::lookupTarget(
-  sys::getDefaultTargetTriple(), Error);
-  if (!TheTarget) {
-errs() << Error;
-std::exit(1);
-  }
+  setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
+  
+  legacy::PassManager Passes;
+  Triple ModuleTriple(M->getTargetTriple());
+  
+  Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createVerifierPass());
 
-  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
-
-  // Create a new Machine
-  std::string CPUStr = 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added a comment.

In https://reviews.llvm.org/D49526#1177208, @morehouse wrote:

> Do we need to parse the arguments for opt-level, or can we just hardcode 
> `-O2` and remove the argument parsing code?


I have the argument parsing code since the original `clang-proto-fuzzer` code 
had it and @kcc mentioned we might want to change the command line arguments in 
the future.


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 157545.
emmettneyman added a comment.

Small change to fix line length


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,48 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs a loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Vectorize.h"
 
 using namespace llvm;
 
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +68,111 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+void ErrorAndExit(std::string message) {
+  errs()<< "ERROR: " << message << "\n";
+  std::exit(1);
+}
 
-  // Set the Module to include the the IR code to be compiled
-  SMDiagnostic Err;
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Create and initialize a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateModulePassManager(MPM);
+}
 
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string , int OLvl) {
+  // Create a module that will run the optimization passes
+  SMDiagnostic Err;
   LLVMContext Context;
-  std::unique_ptr M = parseIR(MemoryBufferRef(S, "IR"), Err, Context);
-  if (!M) {
-errs() << "error: could not parse IR!\n";
-std::exit(1);
-  }
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M || verifyModule(*M, ()))
+ErrorAndExit("Could not parse IR");
 
-  // Create a new Target
-  std::string Error;
-  const Target *TheTarget = TargetRegistry::lookupTarget(
-  sys::getDefaultTargetTriple(), Error);
-  if (!TheTarget) {
-errs() << Error;
-std::exit(1);
-  }
+  setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
+  
+  legacy::PassManager Passes;
+  Triple ModuleTriple(M->getTargetTriple());
+  
+  Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createVerifierPass());
 
-  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
-
-  // Create a new Machine
-  std::string CPUStr = getCPUStr();
-  std::string FeaturesStr = getFeaturesStr();
-  std::unique_ptr 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:144
+   Context);
+  Module *M = Owner.get();
+  if (!M)

morehouse wrote:
> Why not just rename `Owner` to `M` and remove this line?
Reverted it back since the change caused the fuzzer to crash.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:147
+  builder.setUseOrcMCJITReplacement(false);
+  builder.setMCJITMemoryManager(make_unique());
+  builder.setOptLevel(OLvl);

morehouse wrote:
> This uses `llvm:make_unique`, which was written when LLVM used C++11.  But 
> since LLVM now uses C++14, I think we should use `std::make_unique` instead.
We talked offline, but just to put it in writing: My current LLVM build uses 
C++11 so I'm keeping it `llvm::make_unique`.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:184
+  CodeGenOpt::Level OLvl;
+  getOptLevel(ExtraArgs, OLvl);
+  

morehouse wrote:
> We're allowing the JIT opt-level to be specified on the command line, but 
> we're hard-coding OptLevel=3 for the optimization passes.
> 
> Do we need to allow the opt-level to be specified on the command line?
Good point. Will change to make `OptLLVM()` use the same optimization level as 
the JIT Engine.


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-26 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 157544.
emmettneyman added a comment.

- Code style fixes
- Removed `FPasses`
- Allowed CL Args to specify opt level for `OptLLVM()`


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,48 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs a loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Vectorize.h"
 
 using namespace llvm;
 
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +68,110 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+void ErrorAndExit(std::string message) {
+  errs()<< "ERROR: " << message << "\n";
+  std::exit(1);
+}
 
-  // Set the Module to include the the IR code to be compiled
-  SMDiagnostic Err;
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Create and initialize a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateModulePassManager(MPM);
+}
 
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string , int OLvl) {
+  // Create a module that will run the optimization passes
+  SMDiagnostic Err;
   LLVMContext Context;
-  std::unique_ptr M = parseIR(MemoryBufferRef(S, "IR"), Err, Context);
-  if (!M) {
-errs() << "error: could not parse IR!\n";
-std::exit(1);
-  }
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M || verifyModule(*M, ()))
+ErrorAndExit("Could not parse IR");
 
-  // Create a new Target
-  std::string Error;
-  const Target *TheTarget = TargetRegistry::lookupTarget(
-  sys::getDefaultTargetTriple(), Error);
-  if (!TheTarget) {
-errs() << Error;
-std::exit(1);
-  }
+  setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
+  
+  legacy::PassManager Passes;
+  Triple ModuleTriple(M->getTargetTriple());
+  
+  Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createVerifierPass());
 
-  TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
-
-  // Create a new Machine
-  std::string CPUStr = getCPUStr();
-  std::string 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-25 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:159
+  std::unique_ptr(RTDyldMM));
+  builder.setOptLevel(OLvl);
+  builder.setTargetOptions(InitTargetOptionsFromCodeGenFlags());

emmettneyman wrote:
> morehouse wrote:
> > If the JIT does optimization already, do we need to call `OptLLVM` at all?  
> > Will the vectorizer kick in without `OptLLVM`?
> I'll look into this more. I couldn't find an answer quickly.
No, the JIT doesn't run any optimization passes. The optimization level given 
just controls CodeGen.

Source: http://lists.llvm.org/pipermail/llvm-dev/2016-May/099631.html


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-25 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 157388.
emmettneyman added a comment.

Fixed some things, made code cleaner


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,48 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs a loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Vectorize.h"
 
 using namespace llvm;
 
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +68,123 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+void ErrorAndExit(std::string message) {
+  errs()<< "ERROR: " << message << "\n";
+  std::exit(1);
+}
+
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  legacy::FunctionPassManager ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Create and initialize a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
+}
 
-  // Set the Module to include the the IR code to be compiled
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string ) {
+  // Create a module that will run the optimization passes
   SMDiagnostic Err;
+  LLVMContext Context;
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M || verifyModule(*M, ()))
+ErrorAndExit("Could not parse IR");
+
+  setFunctionAttributes(getCPUStr(), getFeaturesStr(), *M);
+  
+  legacy::PassManager Passes;
+  Triple ModuleTriple(M->getTargetTriple());
+  
+  Passes.add(new TargetLibraryInfoWrapperPass(ModuleTriple));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  Passes.add(createVerifierPass());
+  
+  std::unique_ptr FPasses =
+  make_unique(M.get());
+  FPasses->add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  FPasses->add(createVerifierPass());
+
+  AddOptimizationPasses(Passes, *FPasses, 3, 0);
+  
+  FPasses->doInitialization();
+  for (Function  : *M)
+FPasses->run(F);
+  FPasses->doFinalization();
+  
+  // Add a pass that writes the optimized IR to an output stream
+  std::string outString;
+  raw_string_ostream OS(outString);
+  

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-25 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:89
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);

morehouse wrote:
> If we do this, do we need to explicitly add the vectorizer pass below?
No we don't. I've deleted the line below.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:110
+  TargetLibraryInfoImpl TLII(ModuleTriple);
+  Passes.add(new TargetLibraryInfoWrapperPass(TLII));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));

morehouse wrote:
> Can simplify to `Passes.add(new 
> TargetLibraryInfoWrapperPass(M->getTargetTriple))`.
Contrary to the function's name, `getTargetTriple()` actually returns a 
`string`, not a `Triple`. But I changed it to `new 
TargetLibraryInfoWrapperPass(ModuleTriple)` and deleted line 109.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:115
+  make_unique(M.get());
+  FPasses->add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+

morehouse wrote:
> morehouse wrote:
> > Why do we add a `TargetTransformInfoWrapperPass` to both `Passes` and 
> > `FPasses`?
> Do we need both `Passes` and `FPasses`?
I think because we're just adding a loop vectorize pass, we don't need 
`FPasses`. The loop vectorize pass lives within the regular 
`ModulePassManager`, not the `FunctionPassManager`. I decided to put it in the 
code so, in the future, it would be easier to add different kinds of passes to 
the fuzz target. Should I remove it?



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:159
+  std::unique_ptr(RTDyldMM));
+  builder.setOptLevel(OLvl);
+  builder.setTargetOptions(InitTargetOptionsFromCodeGenFlags());

morehouse wrote:
> If the JIT does optimization already, do we need to call `OptLLVM` at all?  
> Will the vectorizer kick in without `OptLLVM`?
I'll look into this more. I couldn't find an answer quickly.


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-25 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 157335.
emmettneyman added a comment.

- cleaned up code and moved initialization code
- removed fake command line parsing


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,48 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs a loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Vectorize.h"
 
 using namespace llvm;
 
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +68,132 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+void ErrorAndExit(std::string message) {
+  errs()<< "ERROR: " << message << "\n";
+  std::exit(1);
+}
+
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  legacy::FunctionPassManager ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Verify that input is correct by adding a verifier pass
+  FPM.add(createVerifierPass());
 
-  // Set the Module to include the the IR code to be compiled
+  // Create and initialize a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
+}
+
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string , CodeGenOpt::Level ) {
+  // Create a module that will run the optimization passes
   SMDiagnostic Err;
+  LLVMContext Context;
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, Context);
+  if (!M || verifyModule(*M, ()))
+ErrorAndExit("Could not parse IR");
+
+  std::string CPUStr = getCPUStr();
+  std::string FeaturesStr = getFeaturesStr();
+  setFunctionAttributes(CPUStr, FeaturesStr, *M);
+  
+  legacy::PassManager Passes;
+  Triple ModuleTriple(M->getTargetTriple());
+  TargetLibraryInfoImpl TLII(ModuleTriple);
+  Passes.add(new TargetLibraryInfoWrapperPass(TLII));
+  Passes.add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+  
+  std::unique_ptr FPasses =
+  make_unique(M.get());
+  FPasses->add(createTargetTransformInfoWrapperPass(TargetIRAnalysis()));
+
+  AddOptimizationPasses(Passes, *FPasses, 3, 0);
+  Pass *P = createLoopVectorizePass();
+  if (!P)
+ErrorAndExit("Cannot 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-25 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:208
+
+  static_cast(RTDyldMM)->invalidateInstructionCache();
+

morehouse wrote:
> This cast shouldn't be necessary.
Turns out this line is redundant anyways. `EE->finalizeObject()`calls 
`invalidateInstructionCache()`.


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-25 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:190
+  builder.setMCJITMemoryManager(
+  std::unique_ptr(RTDyldMM));
+  builder.setOptLevel(OLvl);

morehouse wrote:
> emmettneyman wrote:
> > morehouse wrote:
> > > emmettneyman wrote:
> > > > morehouse wrote:
> > > > > These 3 lines can be combined to `builder.setMCJITMemoryManager(new 
> > > > > SectionMemoryManager())`
> > > > I use RTDyldMM on line 208. Should I just keep these lines as they are?
> > > Ah, missed that.  In that case, you can probably simplify this line to
> > > `builder.setMCJITMemoryManager(RTDyldMM)`.
> > It looks like set `MCJITMemoryManager()` needs to take a `unique_ptr`. I'm 
> > not sure how to clean it up any more than it already is.
> Does it not implicitly cast?
No, I think it only works in the other direction.


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-25 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:190
+  builder.setMCJITMemoryManager(
+  std::unique_ptr(RTDyldMM));
+  builder.setOptLevel(OLvl);

morehouse wrote:
> emmettneyman wrote:
> > morehouse wrote:
> > > These 3 lines can be combined to `builder.setMCJITMemoryManager(new 
> > > SectionMemoryManager())`
> > I use RTDyldMM on line 208. Should I just keep these lines as they are?
> Ah, missed that.  In that case, you can probably simplify this line to
> `builder.setMCJITMemoryManager(RTDyldMM)`.
It looks like set `MCJITMemoryManager()` needs to take a `unique_ptr`. I'm not 
sure how to clean it up any more than it already is.


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-24 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp:190
+  builder.setMCJITMemoryManager(
+  std::unique_ptr(RTDyldMM));
+  builder.setOptLevel(OLvl);

morehouse wrote:
> These 3 lines can be combined to `builder.setMCJITMemoryManager(new 
> SectionMemoryManager())`
I use RTDyldMM on line 208. Should I just keep these lines as they are?


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-24 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 157138.
emmettneyman added a comment.

Cleaned up code

Tried to get rid of ParseCommandLineOptions() call but could not figure out
how to initialize a PassInfo object without it.


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,50 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs an loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
 
 using namespace llvm;
 
+static cl::list
+PassList(cl::desc("Optimizations available:"));
+
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +70,164 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+// Helper function to call pass initialization functions
+void InitEverything() {
+  PassRegistry  = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeVectorization(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeAggressiveInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
+}
+
+void ErrorAndExit(std::string message) {
+  errs()<< "ERROR: " << message << "\n";
+  std::exit(1);
+}
+
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  legacy::FunctionPassManager ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Verify that input is correct by adding a verifier pass
+  FPM.add(createVerifierPass());
+
+  // Create and initializa a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
+}
+
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string IR, CodeGenOpt::Level ) {
+  InitEverything();
+ 
+  // Mimic argc and argv and pass them to ParseCommandLineOptions to initilize
+  // PassList, ie which optimizations we want to run on the IR
+  // TODO: Find a better way of doing this
+  const char *args[2];
+  std::string arg0 = "llvm-proto-fuzzer";
+  std::string arg1 = "-loop-vectorize";
+  args[0] = arg0.c_str();
+  args[1] = arg1.c_str();
+  cl::ParseCommandLineOptions(2, args);
 
-  // Set the Module to include the the 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-23 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 156862.
emmettneyman added a comment.

Made fixes to patch, rebased CMake file


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,51 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs an loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
 
 using namespace llvm;
 
+static cl::list
+PassList(cl::desc("Optimizations available:"));
+
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +71,182 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
-  // Parse ExtraArgs to set the optimization level
-  CodeGenOpt::Level OLvl;
-  getOptLevel(ExtraArgs, OLvl);
+// Helper function to call pass initialization functions
+void InitEverything() {
+  PassRegistry  = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeVectorization(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeAggressiveInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
+}
 
-  // Set the Module to include the the IR code to be compiled
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  legacy::FunctionPassManager ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Verify that input is correct by adding a verifier pass
+  FPM.add(createVerifierPass());
+
+  // Create and initializa a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
+}
+
+// Mimics the opt tool to run an optimization pass over the provided IR
+std::string OptLLVM(const std::string IR, CodeGenOpt::Level ) {
+  InitEverything();
+ 
+  // Mimic argc and argv and pass them to ParseCommandLineOptions to initilize
+  // PassList, ie which optimizations we want to run on the IR
+  // TODO: Find a better way of doing this
+  char *args[2];
+  char t[18] = "llvm-proto-fuzzer";
+  char s[16] = "-loop-vectorize";
+  args[0] = t;
+  args[1] = s;
+  cl::ParseCommandLineOptions(2, args, "");
+
+  // Create a module that will run the optimization passes
   SMDiagnostic Err;
+  LLVMContext Context;
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 156370.
emmettneyman added a comment.

- Fixed typo that broke build


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/CMakeLists.txt
  clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,53 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs an loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
 
 using namespace llvm;
 
+static cl::list
+PassList(cl::desc("Optimizations available:"));
+
+static std::string OptIR;
+
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +73,220 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
+// Helper function to call pass initialization functions
+void InitEverything() {
+  PassRegistry  = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeVectorization(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeAggressiveInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
+}
+
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  legacy::FunctionPassManager ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Verify that input is correct by adding a verifier pass
+  FPM.add(createVerifierPass());
+
+  // Create and initializa a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
+}
+
+// Mimics the opt tool to run an optimization pass over the provided IR
+void OptLLVM(const std::string IR, CodeGenOpt::Level ) {
+  InitEverything();
+ 
+  // Mimic argc and argv and pass them to ParseCommandLineOptions to initilize
+  // PassList, ie which optimizations we want to run on the IR
+  // TODO: Find a better way of doing this
+  char *args[2];
+  char t[18] = "llvm-proto-fuzzer";
+  char s[16] = "-loop-vectorize";
+  args[0] = t;
+  args[1] = s;
+  cl::ParseCommandLineOptions(2, args, "");
+
+  // Create a module that will run the optimization passes
+  SMDiagnostic Err;
+  LLVMContext Context;
+  std::unique_ptr M = parseIR(MemoryBufferRef(IR, "IR"), Err, 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 156364.
emmettneyman added a comment.

- Cleaned up leftover code from mmap memcpy


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/CMakeLists.txt
  clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,53 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs an loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
 
 using namespace llvm;
 
+static cl::list
+PassList(cl::desc("Optimizations available:"));
+
+static std::string OptIR;
+
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +73,220 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
+// Helper function to call pass initialization functions
+void InitEverything() {
+  PassRegistry  = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeVectorization(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeAggressiveInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
+}
+
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  legacy::FunctionPassManager ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Verify that input is correct by adding a verifier pass
+  FPM.add(createVerifierPass());
+
+  // Create and initializa a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
+}
+
+// Mimics the opt tool to run an optimization pass over the provided IR
+void OptLLVM(const std::string IR, CodeGenOpt::Level ) {
+  InitEverything();
+ 
+  // Mimic argc and argv and pass them to ParseCommandLineOptions to initilize
+  // PassList, ie which optimizations we want to run on the IR
+  // TODO: Find a better way of doing this
+  char *args[2];
+  char t[18] = "llvm-proto-fuzzer";
+  char s[16] = "-loop-vectorize";
+  args[0] = t;
+  args[1] = s;
+  cl::ParseCommandLineOptions(2, args, "");
+
+  // Create a module that will run the 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 156362.
emmettneyman added a comment.

- Switched to JIT for compilation and execution


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/CMakeLists.txt
  clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h

Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,53 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs an loop
+// vectorizer optimization pass over the given IR code. Then mimics lli on both
+// versions to JIT the generated code and execute it. Currently, functions are 
+// executed on dummy inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/ExecutionEngine/GenericValue.h"
+#include "llvm/ExecutionEngine/JITEventListener.h"
+#include "llvm/ExecutionEngine/JITSymbol.h"
+#include "llvm/ExecutionEngine/MCJIT.h"
+#include "llvm/ExecutionEngine/ObjectCache.h"
+#include "llvm/ExecutionEngine/RTDyldMemoryManager.h"
+#include "llvm/ExecutionEngine/SectionMemoryManager.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
-
-#include 
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
 
 using namespace llvm;
 
+static cl::list
+PassList(cl::desc("Optimizations available:"));
+
+static std::string OptIR;
+
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,59 +73,221 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-  const std::vector ) {
+// Helper function to call pass initialization functions
+void InitEverything() {
+  PassRegistry  = *PassRegistry::getPassRegistry();
+  initializeCore(Registry);
+  initializeScalarOpts(Registry);
+  initializeVectorization(Registry);
+  initializeIPO(Registry);
+  initializeAnalysis(Registry);
+  initializeTransformUtils(Registry);
+  initializeInstCombine(Registry);
+  initializeAggressiveInstCombine(Registry);
+  initializeInstrumentation(Registry);
+  initializeTarget(Registry);
+}
+
+// Helper function to add optimization passes to the TargetMachine at the 
+// specified optimization level, OptLevel
+static void AddOptimizationPasses(legacy::PassManagerBase ,
+  legacy::FunctionPassManager ,
+  unsigned OptLevel, unsigned SizeLevel) {
+  // Verify that input is correct by adding a verifier pass
+  FPM.add(createVerifierPass());
+
+  // Create and initializa a PassManagerBuilder
+  PassManagerBuilder Builder;
+  Builder.OptLevel = OptLevel;
+  Builder.SizeLevel = SizeLevel;
+  Builder.Inliner = createFunctionInliningPass(OptLevel, SizeLevel, false);
+  Builder.LoopVectorize = true;
+  Builder.populateFunctionPassManager(FPM);
+  Builder.populateModulePassManager(MPM);
+}
+
+// Mimics the opt tool to run an optimization pass over the provided IR
+void OptLLVM(const std::string IR, CodeGenOpt::Level ) {
+  InitEverything();
+ 
+  // Mimic argc and argv and pass them to ParseCommandLineOptions to initilize
+  // PassList, ie which optimizations we want to run on the IR
+  // TODO: Find a better way of doing this
+  char *args[2];
+  char t[18] = "llvm-proto-fuzzer";
+  char s[16] = "-loop-vectorize";
+  args[0] = t;
+  args[1] = s;
+  cl::ParseCommandLineOptions(2, args, "");
+
+  // Create a module that will run the 

[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-18 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added a comment.

The files

  Object.h
  Object.cpp
  llvm-objcopy.h

are from llvm/tools/llvm-obj-copy with only slight modifications, mostly 
deleting irrelevant parts.


Repository:
  rC Clang

https://reviews.llvm.org/D49526



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


[PATCH] D49526: Updated llvm-proto-fuzzer to execute the compiled code

2018-07-18 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman created this revision.
emmettneyman added reviewers: morehouse, kcc.
Herald added subscribers: cfe-commits, mgorny.
Herald added a reviewer: alexshap.

Made changes to the llvm-proto-fuzzer

- Added loop vectorizer optimization pass in order to have two IR versions
- Updated old fuzz target to handle two different IR versions
- Wrote code to execute both versions in memory


Repository:
  rC Clang

https://reviews.llvm.org/D49526

Files:
  clang/tools/clang-fuzzer/CMakeLists.txt
  clang/tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  clang/tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  clang/tools/clang-fuzzer/handle-llvm/Object.cpp
  clang/tools/clang-fuzzer/handle-llvm/Object.h
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.cpp
  clang/tools/clang-fuzzer/handle-llvm/fuzzer_initialize.h
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  clang/tools/clang-fuzzer/handle-llvm/handle_llvm.h
  clang/tools/clang-fuzzer/handle-llvm/llvm-objcopy.h

Index: clang/tools/clang-fuzzer/handle-llvm/llvm-objcopy.h
===
--- /dev/null
+++ clang/tools/clang-fuzzer/handle-llvm/llvm-objcopy.h
@@ -0,0 +1,42 @@
+//===- llvm-objcopy.h ---*- C++ -*-===//
+//
+//  The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//  Modified version of llvm/tools/llvm-objcopy/llvm-objcopy.h
+//
+//===--===//
+
+#ifndef LLVM_TOOLS_OBJCOPY_OBJCOPY_H
+#define LLVM_TOOLS_OBJCOPY_OBJCOPY_H
+
+#include "llvm/ADT/Twine.h"
+#include "llvm/Support/Compiler.h"
+#include "llvm/Support/Error.h"
+#include "llvm/Support/raw_ostream.h"
+#include 
+
+namespace llvm {
+
+LLVM_ATTRIBUTE_NORETURN extern void error(Twine Message);
+LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File, Error E);
+LLVM_ATTRIBUTE_NORETURN extern void reportError(StringRef File,
+std::error_code EC);
+
+// This is taken from llvm-readobj.
+// [see here](llvm/tools/llvm-readobj/llvm-readobj.h:38)
+template  T unwrapOrError(Expected EO) {
+  if (EO)
+return *EO;
+  std::string Buf;
+  raw_string_ostream OS(Buf);
+  logAllUnhandledErrors(EO.takeError(), OS, "");
+  OS.flush();
+  error(Buf);
+}
+
+} // end namespace llvm
+
+#endif // LLVM_TOOLS_OBJCOPY_OBJCOPY_H
Index: clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
===
--- clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
+++ clang/tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
@@ -7,33 +7,56 @@
 //
 //===--===//
 //
-// Implements HandleLLVM for use by the Clang fuzzers. Mimics the llc tool to
-// compile an LLVM IR file to X86_64 assembly.
+// Implements HandleLLVM for use by the Clang fuzzers. First runs an loop
+// vectorizer optimization pass over the given IR code. Then mimics llc on both
+// versions of the IR code to genereate machine code for each version. Inserts
+// both versions of the code into memory and executes both functions on dummy
+// inputs.
 //
 //===--===//
 
 #include "handle_llvm.h"
+#include "Object.h"
 
 #include "llvm/ADT/Triple.h"
 #include "llvm/Analysis/TargetLibraryInfo.h"
+#include "llvm/Analysis/TargetTransformInfo.h"
+#include "llvm/BinaryFormat/ELF.h"
 #include "llvm/CodeGen/CommandFlags.inc"
 #include "llvm/CodeGen/MachineModuleInfo.h"
+#include "llvm/CodeGen/TargetPassConfig.h"
+#include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
+#include "llvm/IR/LegacyPassNameParser.h"
 #include "llvm/IR/LLVMContext.h"
 #include "llvm/IR/Module.h"
 #include "llvm/IR/Verifier.h"
 #include "llvm/IRReader/IRReader.h"
+#include "llvm/Object/Binary.h"
+#include "llvm/Object/ELFObjectFile.h"
+#include "llvm/Object/ELFTypes.h"
+#include "llvm/Pass.h"
 #include "llvm/PassRegistry.h"
-#include "llvm/Support/InitLLVM.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Support/TargetRegistry.h"
+#include "llvm/Support/TargetSelect.h"
 #include "llvm/Target/TargetMachine.h"
+#include "llvm/Transforms/IPO/PassManagerBuilder.h"
+#include "llvm/Transforms/IPO.h"
 
 #include 
 
 using namespace llvm;
+using namespace object;
+using namespace ELF;
 
+static cl::list
+PassList(cl::desc("Optimizations available:"));
+
+static std::string OptIR;
+
+// Helper function to parse command line args and find the optimization level
 static void getOptLevel(const std::vector ,
   CodeGenOpt::Level ) {
   // Find the optimization level from the command line args
@@ -53,23 +76,243 @@
   }
 }
 
-void clang_fuzzer::HandleLLVM(const std::string ,
-   

[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 152006.
emmettneyman added a comment.

- actually fixed error statement


Repository:
  rC Clang

https://reviews.llvm.org/D48106

Files:
  tools/clang-fuzzer/CMakeLists.txt
  tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  tools/clang-fuzzer/cxx_loop_proto.proto
  tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  tools/clang-fuzzer/handle-cxx/handle_cxx.cpp
  tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  tools/clang-fuzzer/handle-llvm/handle_llvm.h
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
  tools/clang-fuzzer/proto-to-llvm/CMakeLists.txt
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp

Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
===
--- tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
@@ -1,32 +1,31 @@
-//==-- loop_proto_to_cxx_main.cpp - Driver for protobuf-C++ conversion -==//
+//==-- loop_proto_to_llvm_main.cpp - Driver for protobuf-LLVM conversion==//
 //
 // The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
 //===--===//
 //
-// Implements a simple driver to print a C++ program from a protobuf with loops.
+// Implements a simple driver to print a LLVM program from a protobuf with loops
 //
 //===--===//
 
-// This is a copy and will be updated later to introduce changes
 
 #include 
 #include 
 #include 
 #include 
 
-#include "proto_to_cxx.h"
+#include "loop_proto_to_llvm.h"
 
 int main(int argc, char **argv) {
   for (int i = 1; i < argc; i++) {
 std::fstream in(argv[i]);
 std::string str((std::istreambuf_iterator(in)),
 std::istreambuf_iterator());
-std::cout << "// " << argv[i] << std::endl;
-std::cout << clang_fuzzer::LoopProtoToCxx(
+std::cout << ";; " << argv[i] << std::endl;
+std::cout << clang_fuzzer::LoopProtoToLLVM(
 reinterpret_cast(str.data()), str.size());
   }
 }
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
===
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
@@ -0,0 +1,23 @@
+//==-- loop_proto_to_llvm.h - Protobuf-C++ conversion ==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Defines functions for converting between protobufs and LLVM IR.
+//
+//===--===//
+
+#include 
+#include 
+#include 
+
+namespace clang_fuzzer {
+class LoopFunction;
+
+std::string LoopFunctionToLLVMString(const LoopFunction );
+std::string LoopProtoToLLVM(const uint8_t *data, size_t size);
+}
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -0,0 +1,156 @@
+//==-- loop_proto_to_llvm.cpp - Protobuf-C++ conversion
+//-==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Implements functions for converting between protobufs and LLVM IR.
+//
+//
+//===--===//
+
+#include "loop_proto_to_llvm.h"
+#include "cxx_loop_proto.pb.h"
+
+// The following is needed to convert protos in human-readable form
+#include 
+
+#include 
+#include 
+
+namespace clang_fuzzer {
+
+// Forward decls
+std::string BinopToString(std::ostream , const BinaryOp );
+std::string StateSeqToString(std::ostream , const StatementSeq );
+
+// Counter variable to generate new LLVM IR variable names and wrapper function
+std::string get_var() {
+  static int ctr = 0;
+  return "%var" + std::to_string(ctr++);
+}
+
+// Proto to LLVM.
+
+std::string ConstToString(const Const ) {
+  return std::to_string(x.val());
+}
+std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string arr;
+  switch(x.arr()) {
+  case VarRef::ARR_A:
+arr = "%a";
+break;
+  case 

[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added a comment.

In https://reviews.llvm.org/D48106#1137224, @morehouse wrote:

> Looks like `exit(0)` is still there.


oops, forgot to `:w`


Repository:
  rC Clang

https://reviews.llvm.org/D48106



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


[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 152005.
emmettneyman added a comment.

- removed unnecessary comment and fixed exit statement


Repository:
  rC Clang

https://reviews.llvm.org/D48106

Files:
  tools/clang-fuzzer/CMakeLists.txt
  tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  tools/clang-fuzzer/cxx_loop_proto.proto
  tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  tools/clang-fuzzer/handle-cxx/handle_cxx.cpp
  tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  tools/clang-fuzzer/handle-llvm/handle_llvm.h
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
  tools/clang-fuzzer/proto-to-llvm/CMakeLists.txt
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp

Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
===
--- tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
@@ -1,32 +1,31 @@
-//==-- loop_proto_to_cxx_main.cpp - Driver for protobuf-C++ conversion -==//
+//==-- loop_proto_to_llvm_main.cpp - Driver for protobuf-LLVM conversion==//
 //
 // The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
 //===--===//
 //
-// Implements a simple driver to print a C++ program from a protobuf with loops.
+// Implements a simple driver to print a LLVM program from a protobuf with loops
 //
 //===--===//
 
-// This is a copy and will be updated later to introduce changes
 
 #include 
 #include 
 #include 
 #include 
 
-#include "proto_to_cxx.h"
+#include "loop_proto_to_llvm.h"
 
 int main(int argc, char **argv) {
   for (int i = 1; i < argc; i++) {
 std::fstream in(argv[i]);
 std::string str((std::istreambuf_iterator(in)),
 std::istreambuf_iterator());
-std::cout << "// " << argv[i] << std::endl;
-std::cout << clang_fuzzer::LoopProtoToCxx(
+std::cout << ";; " << argv[i] << std::endl;
+std::cout << clang_fuzzer::LoopProtoToLLVM(
 reinterpret_cast(str.data()), str.size());
   }
 }
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
===
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
@@ -0,0 +1,23 @@
+//==-- loop_proto_to_llvm.h - Protobuf-C++ conversion ==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Defines functions for converting between protobufs and LLVM IR.
+//
+//===--===//
+
+#include 
+#include 
+#include 
+
+namespace clang_fuzzer {
+class LoopFunction;
+
+std::string LoopFunctionToLLVMString(const LoopFunction );
+std::string LoopProtoToLLVM(const uint8_t *data, size_t size);
+}
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -0,0 +1,156 @@
+//==-- loop_proto_to_llvm.cpp - Protobuf-C++ conversion
+//-==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Implements functions for converting between protobufs and LLVM IR.
+//
+//
+//===--===//
+
+#include "loop_proto_to_llvm.h"
+#include "cxx_loop_proto.pb.h"
+
+// The following is needed to convert protos in human-readable form
+#include 
+
+#include 
+#include 
+
+namespace clang_fuzzer {
+
+// Forward decls
+std::string BinopToString(std::ostream , const BinaryOp );
+std::string StateSeqToString(std::ostream , const StatementSeq );
+
+// Counter variable to generate new LLVM IR variable names and wrapper function
+std::string get_var() {
+  static int ctr = 0;
+  return "%var" + std::to_string(ctr++);
+}
+
+// Proto to LLVM.
+
+std::string ConstToString(const Const ) {
+  return std::to_string(x.val());
+}
+std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string arr;
+  switch(x.arr()) {
+  case VarRef::ARR_A:
+arr = "%a";
+

[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 152003.
emmettneyman added a comment.

- minor changes to improve readability and style


Repository:
  rC Clang

https://reviews.llvm.org/D48106

Files:
  tools/clang-fuzzer/CMakeLists.txt
  tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  tools/clang-fuzzer/cxx_loop_proto.proto
  tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  tools/clang-fuzzer/handle-cxx/handle_cxx.cpp
  tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  tools/clang-fuzzer/handle-llvm/handle_llvm.h
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
  tools/clang-fuzzer/proto-to-llvm/CMakeLists.txt
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp

Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
===
--- tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
@@ -1,32 +1,31 @@
-//==-- loop_proto_to_cxx_main.cpp - Driver for protobuf-C++ conversion -==//
+//==-- loop_proto_to_llvm_main.cpp - Driver for protobuf-LLVM conversion==//
 //
 // The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
 //===--===//
 //
-// Implements a simple driver to print a C++ program from a protobuf with loops.
+// Implements a simple driver to print a LLVM program from a protobuf with loops
 //
 //===--===//
 
-// This is a copy and will be updated later to introduce changes
 
 #include 
 #include 
 #include 
 #include 
 
-#include "proto_to_cxx.h"
+#include "loop_proto_to_llvm.h"
 
 int main(int argc, char **argv) {
   for (int i = 1; i < argc; i++) {
 std::fstream in(argv[i]);
 std::string str((std::istreambuf_iterator(in)),
 std::istreambuf_iterator());
-std::cout << "// " << argv[i] << std::endl;
-std::cout << clang_fuzzer::LoopProtoToCxx(
+std::cout << ";; " << argv[i] << std::endl;
+std::cout << clang_fuzzer::LoopProtoToLLVM(
 reinterpret_cast(str.data()), str.size());
   }
 }
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
===
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
@@ -0,0 +1,23 @@
+//==-- loop_proto_to_llvm.h - Protobuf-C++ conversion ==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Defines functions for converting between protobufs and LLVM IR.
+//
+//===--===//
+
+#include 
+#include 
+#include 
+
+namespace clang_fuzzer {
+class LoopFunction;
+
+std::string LoopFunctionToLLVMString(const LoopFunction );
+std::string LoopProtoToLLVM(const uint8_t *data, size_t size);
+}
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -0,0 +1,156 @@
+//==-- loop_proto_to_llvm.cpp - Protobuf-C++ conversion
+//-==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Implements functions for converting between protobufs and LLVM IR.
+//
+//
+//===--===//
+
+#include "loop_proto_to_llvm.h"
+#include "cxx_loop_proto.pb.h"
+
+// The following is needed to convert protos in human-readable form
+#include 
+
+#include 
+#include 
+
+namespace clang_fuzzer {
+
+// Forward decls
+std::string BinopToString(std::ostream , const BinaryOp );
+std::string StateSeqToString(std::ostream , const StatementSeq );
+
+// Counter variable to generate new LLVM IR variable names and wrapper function
+std::string get_var() {
+  static int ctr = 0;
+  return "%var" + std::to_string(ctr++);
+}
+
+// Proto to LLVM.
+
+std::string ConstToString(const Const ) {
+  return std::to_string(x.val());
+}
+std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string arr;
+  switch(x.arr()) {
+  case VarRef::ARR_A:
+arr = "%a";
+break;

[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman updated this revision to Diff 151986.
emmettneyman added a comment.

- made changes to handle_llvm.cpp in response to reviewer comments


Repository:
  rC Clang

https://reviews.llvm.org/D48106

Files:
  tools/clang-fuzzer/CMakeLists.txt
  tools/clang-fuzzer/ExampleClangLLVMProtoFuzzer.cpp
  tools/clang-fuzzer/cxx_loop_proto.proto
  tools/clang-fuzzer/fuzzer-initialize/fuzzer_initialize.cpp
  tools/clang-fuzzer/handle-cxx/handle_cxx.cpp
  tools/clang-fuzzer/handle-llvm/CMakeLists.txt
  tools/clang-fuzzer/handle-llvm/handle_llvm.cpp
  tools/clang-fuzzer/handle-llvm/handle_llvm.h
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx.cpp
  tools/clang-fuzzer/proto-to-cxx/loop_proto_to_cxx_main.cpp
  tools/clang-fuzzer/proto-to-llvm/CMakeLists.txt
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
  tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp

Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
===
--- tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm_main.cpp
@@ -1,32 +1,31 @@
-//==-- loop_proto_to_cxx_main.cpp - Driver for protobuf-C++ conversion -==//
+//==-- loop_proto_to_llvm_main.cpp - Driver for protobuf-LLVM conversion==//
 //
 // The LLVM Compiler Infrastructure
 //
 // This file is distributed under the University of Illinois Open Source
 // License. See LICENSE.TXT for details.
 //
 //===--===//
 //
-// Implements a simple driver to print a C++ program from a protobuf with loops.
+// Implements a simple driver to print a LLVM program from a protobuf with loops
 //
 //===--===//
 
-// This is a copy and will be updated later to introduce changes
 
 #include 
 #include 
 #include 
 #include 
 
-#include "proto_to_cxx.h"
+#include "loop_proto_to_llvm.h"
 
 int main(int argc, char **argv) {
   for (int i = 1; i < argc; i++) {
 std::fstream in(argv[i]);
 std::string str((std::istreambuf_iterator(in)),
 std::istreambuf_iterator());
-std::cout << "// " << argv[i] << std::endl;
-std::cout << clang_fuzzer::LoopProtoToCxx(
+std::cout << ";; " << argv[i] << std::endl;
+std::cout << clang_fuzzer::LoopProtoToLLVM(
 reinterpret_cast(str.data()), str.size());
   }
 }
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
===
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.h
@@ -0,0 +1,23 @@
+//==-- loop_proto_to_llvm.h - Protobuf-C++ conversion ==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Defines functions for converting between protobufs and LLVM IR.
+//
+//===--===//
+
+#include 
+#include 
+#include 
+
+namespace clang_fuzzer {
+class LoopFunction;
+
+std::string LoopFunctionToLLVMString(const LoopFunction );
+std::string LoopProtoToLLVM(const uint8_t *data, size_t size);
+}
Index: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
===
--- /dev/null
+++ tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp
@@ -0,0 +1,156 @@
+//==-- loop_proto_to_llvm.cpp - Protobuf-C++ conversion
+//-==//
+//
+// The LLVM Compiler Infrastructure
+//
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
+//
+//===--===//
+//
+// Implements functions for converting between protobufs and LLVM IR.
+//
+//
+//===--===//
+
+#include "loop_proto_to_llvm.h"
+#include "cxx_loop_proto.pb.h"
+
+// The following is needed to convert protos in human-readable form
+#include 
+
+#include 
+#include 
+
+namespace clang_fuzzer {
+
+// Forward decls
+std::string BinopToString(std::ostream , const BinaryOp );
+std::string StateSeqToString(std::ostream , const StatementSeq );
+
+// Counter variable to generate new LLVM IR variable names and wrapper function
+std::string get_var() {
+  static int ctr = 0;
+  return "%var" + std::to_string(ctr++);
+}
+
+// Proto to LLVM.
+
+std::string ConstToString(const Const ) {
+  return std::to_string(x.val());
+}
+std::string VarRefToString(std::ostream , const VarRef ) {
+  std::string arr;
+  switch(x.arr()) {
+  case VarRef::ARR_A:
+arr = 

[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: tools/clang-fuzzer/proto-to-llvm/loop_proto_to_llvm.cpp:71
+return val_var;
+  }
+}

morehouse wrote:
> Is it still possible for the protobuf to not have a constant, a binOp, or a 
> varRef?
Right now, since the protobufs use `oneof`, it is possible for the protobuf to 
not have a constant binOp, or varRef. For now, I'll generate "dummy" code for 
this case. 

Then, I'll submit a new patch that eliminates the use of `oneof` for both 
`cxx_proto.proto` and `cxx_loop_proto` so that "dummy" code isn't necessary.


Repository:
  rC Clang

https://reviews.llvm.org/D48106



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


[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: tools/clang-fuzzer/handle-llvm/\:113
+
+  legacy::PassManager PM;
+  TargetLibraryInfoImpl TLII(Triple(M->getTargetTriple()));

morehouse wrote:
> Any reason not to use the newer PassManager?
Clang (`llc`) and the `llvm-isel-fuzzer` both still use the legacy PassManager. 
The newer PassManager has a new interface that doesn't use `Module`s.


Repository:
  rC Clang

https://reviews.llvm.org/D48106



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


[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: tools/clang-fuzzer/handle-llvm/\:62
+  initializeScavengerTestPass(*Registry);
+
+}

morehouse wrote:
> Does this initialization need to happen every time the fuzzer generates a new 
> input, or can we call this from `LLVMFuzzerInitialize()` instead?
I was following the pattern from `handle_cxx.cpp` in which the initialization 
calls were made every time a new input was generated. However looking at the 
documentation and at `llvm-isel-fuzzer.cpp`, it seems like putting it in 
`LLVMFuzzerInitialize()` makes more sense. I will make this change in the next 
commit.


Repository:
  rC Clang

https://reviews.llvm.org/D48106



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


[PATCH] D48106: implemented proto to llvm

2018-06-19 Thread Emmett Neyman via Phabricator via cfe-commits
emmettneyman added inline comments.



Comment at: tools/clang-fuzzer/handle-llvm/CMakeLists.txt:5
+  handle_llvm.cpp
+  )

morehouse wrote:
> There's fewer libraries linked here than in `handle-cxx/` (not saying this is 
> wrong, but it could be).  Do you get link errors if you build 
> `clang-llvm-proto-fuzzer` with shared libraries?
It builds without any errors both with all the libraries from `handle-cxx/` 
linked in and without any of the libraries linked in (as I have here). Which is 
preferred?


Repository:
  rC Clang

https://reviews.llvm.org/D48106



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


  1   2   >