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 &os, const StatementSeq &x);
 
 // 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 &x) {
@@ -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 &os, const Rvalue &x) {
@@ -122,25 +133,50 @@
   }
   return os;
 }
-std::ostream &operator<<(std::ostream &os, const LoopFunction &x) {
-  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 &operator<<(std::ostream &os, const Loop &x) {
+  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 &operator<<(std::ostream &os, const LoopFunction &x) {
+  os << "target triple = \"x86_64-unknown-linux-gnu\"\n"
+     << "define void @foo(i32* %a, i32* %b, i32* %c, i64 %s) {\n";
+  for (auto &lp : 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 &operator<<(std::ostream &os, const Loop &x){
+  return os << "for (int i=0; i<s; i++){\n"
+            << x.statements() << "}\n";
+}
 std::ostream &operator<<(std::ostream &os, const LoopFunction &x) {
-  return os << "void foo(int *a, int *b, int *__restrict__ c, size_t s) {\n"
-            << "for (int i=0; i<s; i++){\n"
-            << x.statements() << "}\n}\n";
+  os << "void foo(int *a, int *b, int *__restrict__ c, size_t s) {\n";
+  for (auto &lp : x.loops())
+    os << lp;
+  return os << "}\n";
 }
 
 // ---------------------------------
Index: clang/tools/clang-fuzzer/cxx_loop_proto.proto
===================================================================
--- clang/tools/clang-fuzzer/cxx_loop_proto.proto
+++ clang/tools/clang-fuzzer/cxx_loop_proto.proto
@@ -73,8 +73,12 @@
   repeated Statement statements = 1;
 }
 
-message LoopFunction {
+message Loop {
   required StatementSeq statements = 1;
 }
 
+message LoopFunction {
+  repeated Loop loops = 1;
+}
+
 package clang_fuzzer;
_______________________________________________
cfe-commits mailing list
cfe-commits@lists.llvm.org
http://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to