From 3cc9dfa1305f5f726874dd6b0e96c2294c0cddd9 Mon Sep 17 00:00:00 2001
From: Enzo De Carvalho Bittencourt <enzo.decarvalhobittencourt@ensiie.eu>
Date: Tue, 10 Oct 2023 12:14:42 +0200
Subject: [PATCH] fixed arcane dotfiles naming

---
 Makefile                        | 16 +++++++--
 include/pass_mpi_collective.hpp |  3 ++
 src/pass_mpi_collective.cpp     | 64 ++++++++++++++++++++++++++++-----
 src/test/test3.c                | 54 ++++++++++++++++++++++++++++
 test2.c                         | 16 +++++++++
 test3.c                         | 33 +++++++++++++++++
 6 files changed, 174 insertions(+), 12 deletions(-)
 create mode 100644 src/test/test3.c
 create mode 100644 test2.c
 create mode 100644 test3.c

diff --git a/Makefile b/Makefile
index 6bdaaf9..260b15f 100644
--- a/Makefile
+++ b/Makefile
@@ -18,7 +18,7 @@ DEBUG_TEST_FLAGS=-fdump-tree-all -fdump-tree-all-graph
 
 # Comment to disable debug
 FLAGS+=$(DEBUG_FLAGS)
-TEST_FLAGS+=$(DEBUG_TEST_FLAGS)
+#TEST_FLAGS+=$(DEBUG_TEST_FLAGS)
 
 
 
@@ -29,13 +29,18 @@ LIBD=lib
 TARGETD=bin
 TESTD=test
 TESTSRCD=$(SRCD)/test
+DOTD=dot
 
 # Files
 SRCS=$(notdir $(wildcard ($(SRCD)/*.cpp))
 OBJS=$(addprefix $(OBJD)/, $(SRCS:cpp=o))
 
 
-
+.PHONY: all
+all:
+	$(MAKE) plugin
+	$(MAKE) test
+	
 .PHONY: plugin
 plugin:
 	mkdir -p $(OBJD)
@@ -45,12 +50,14 @@ plugin:
 .PHONY: test
 test:
 	mkdir -p $(TESTD)
+	mkdir -p $(DOTD)
 	$(MAKE) $(TESTD)/test2
+	$(MAKE) $(TESTD)/test3
 
 $(TESTD)/%: $(TESTSRCD)/%.c
 	$(CC) $(TEST_FLAGS) $(PLUGIN_FLAGS) $^ -o $@
 
-$(OBJD)/%.o: $(SRCD)/%.cpp
+$(OBJD)/%.o: $(SRCD)/%.cpp 
 	$(CXX) $(FLAGS) $(PLUGIN_INCLUDE) -c $^ -o $@
 
 $(LIBD)/libplugin.so: $(OBJD)/plugin.o $(OBJD)/pass_mpi_collective.o
@@ -58,6 +65,8 @@ $(LIBD)/libplugin.so: $(OBJD)/plugin.o $(OBJD)/pass_mpi_collective.o
 	# removing test scince plugin was rebuild
 	rm -rf $(TESTD)
 
+%.png: %.dot
+	  dot -Tpng $< -o $@
 
 .PHONY: clean
 clean:
@@ -65,6 +74,7 @@ clean:
 	rm -rf $(TARGETD)
 	rm -rf $(LIBD)
 	rm -rf $(TESTD)
+	rm -rf $(DOTD)
 
 .PHONY: style
 style:
diff --git a/include/pass_mpi_collective.hpp b/include/pass_mpi_collective.hpp
index 32a24bc..3da54fb 100644
--- a/include/pass_mpi_collective.hpp
+++ b/include/pass_mpi_collective.hpp
@@ -37,6 +37,9 @@ public:
 	mpi_collective_code is_mpi_collec(gimple *stmt);
 	void split_blocks(function *fun);
 
+	void label_bb(function *fun);
+	void clean_aux(function *fun);
+
 private:
 	bool __is_mpi_func(gimple *stmt);
 	mpi_collective_code __is_mpi_collec(gimple *stmt);
diff --git a/src/pass_mpi_collective.cpp b/src/pass_mpi_collective.cpp
index ed2a349..93349c0 100644
--- a/src/pass_mpi_collective.cpp
+++ b/src/pass_mpi_collective.cpp
@@ -8,6 +8,8 @@
 #include <gimple.h>
 // gsi
 #include <gimple-iterator.h>
+// basename
+#include <filesystem>
 
 // our pass
 #include "pass_mpi_collective.hpp"
@@ -47,7 +49,11 @@ unsigned int pass_mpi_collective::execute(function *fun)
 
 	print_tree(fun);
 	split_blocks(fun);
+	printf("\t------------------------------[split]-------------------------------\n");
 	print_tree(fun);
+	label_bb(fun);
+	cfgviz_dump(fun, "_split");
+	clean_aux(fun);
 
 	return 0;
 }
@@ -86,6 +92,8 @@ void pass_mpi_collective::print_tree(function *fun)
 	}
 }
 
+
+
 bool pass_mpi_collective::is_func(gimple *stmt)
 {return is_gimple_call(stmt);}
 
@@ -148,11 +156,12 @@ void pass_mpi_collective::split_blocks(function *fun)
 			prev_stmt = stmt;
 			stmt = gsi_stmt(gsi);
 			if (is_mpi_collec(stmt) != LAST_AND_UNUSED_MPI_COLLECTIVE_CODE)
-				++nb_collective;
-			if (nb_collective >= 2){
+			{ ++nb_collective; }
+			if (nb_collective >= 2)
+			{
 				split_block(bb, prev_stmt);
 				split = true;
-				//The inner for doesn't stop naturally, whereas the FOR_EACH_BB_FN 
+				//The inner for doesn't stop naturally, whereas the FOR_EACH_BB_FN
 				//*will* iterate on the new bb we just split. hence the split=true
 			}
 		}
@@ -166,16 +175,52 @@ char *pass_mpi_collective::cfgviz_generate_filename(function *fun,
 	char *target_filename;
 
 	target_filename = (char *)xmalloc(1024 * sizeof(char));
+	int line = LOCATION_LINE(fun->function_start_locus);
+	const char * mfilename = 
+		std::filesystem::path(LOCATION_FILE(fun->function_start_locus))
+		.filename().c_str();
+
+	snprintf(target_filename, 1024, "%s%s_%s_%d_%s.dot",
+		"dot/",
+	  mfilename,
+	  function_name(fun),
+	  line,
+		suffix);
+	
+	return target_filename;
+}
 
-	snprintf(target_filename, 1024, "%s_%s_%d_%s.dot",
-	         current_function_name(),
-	         LOCATION_FILE(fun->function_start_locus),
-	         LOCATION_LINE(fun->function_start_locus),
-	         suffix);
+void pass_mpi_collective::label_bb(function *fun)
+{
+	basic_block bb;
+	gimple_stmt_iterator gsi;
+	gimple *stmt;
+	//size_t line;
 
-	return target_filename;
+	FOR_EACH_BB_FN(bb, fun)
+	{
+		printf("\tBasic block: %d\n", bb->index);
+		for (gsi = gsi_start_bb(bb); !gsi_end_p(gsi); gsi_next(&gsi))
+		{
+			stmt = gsi_stmt(gsi);
+			int mpi_code = is_mpi_collec(stmt);
+			if (mpi_code != LAST_AND_UNUSED_MPI_COLLECTIVE_CODE)
+			{bb->aux = (void *) (new int(mpi_code));}
+		}
+	}
+}
+
+void pass_mpi_collective::clean_aux(function *fun)
+{
+	basic_block bb;
+	FOR_ALL_BB_FN(bb, fun)
+	{
+		if (bb->aux != NULL)
+		{delete (int *) bb->aux; bb->aux = NULL;}
+	}
 }
 
+//============================  GRAPHVIZ  =====================================
 /* Dump the graphviz representation of function 'fun' in file 'out' */
 void pass_mpi_collective::cfgviz_internal_dump(
   function *fun, FILE *out)
@@ -232,3 +277,4 @@ void pass_mpi_collective::cfgviz_dump(function *fun,
 	fclose(out);
 	free(target_filename);
 }
+
diff --git a/src/test/test3.c b/src/test/test3.c
new file mode 100644
index 0000000..cf8758f
--- /dev/null
+++ b/src/test/test3.c
@@ -0,0 +1,54 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+#include <mpi.h>
+
+
+int main(int argc, char * argv[])
+{
+	MPI_Init(&argc, &argv);
+
+	int i;
+	int a = 2;
+	int b = 3;
+	int c=0;
+
+	for(i=0; i<10; i++)
+	{
+
+		MPI_Barrier(MPI_COMM_WORLD);
+
+		if(c<10)
+		{
+			printf("je suis dans le if (c=%d)\n", c);
+			MPI_Barrier(MPI_COMM_WORLD);
+			MPI_Barrier(MPI_COMM_WORLD);
+			if(c <5)
+			{
+				a = a*a +1;
+			}
+			else
+			{
+				c = c*3;
+				if(c <20)
+				{
+					return c;
+				}
+			}
+
+		}
+		else
+		{
+			printf("je suis dans le else (c=%d)\n", c);
+			MPI_Barrier(MPI_COMM_WORLD);
+			return 1;
+		}
+		c+= (a+b);	
+		printf("fin du for\n");
+	}
+
+	printf("c=%d\n", c);
+
+	MPI_Finalize();
+	return 1;
+}
diff --git a/test2.c b/test2.c
new file mode 100644
index 0000000..aebdc6d
--- /dev/null
+++ b/test2.c
@@ -0,0 +1,16 @@
+Digraph G{
+0 [label="BB 0 " shape=ellipse]
+0 -> 2 [color=red label=""]
+2 [label="BB 2 MPI_Init" shape=ellipse]
+2 -> 4 [color=red label=""]
+3 [label="BB 3 " shape=ellipse]
+3 -> 4 [color=red label=""]
+4 [label="BB 4 " shape=ellipse]
+4 -> 3 [color=red label="true"]
+4 -> 5 [color=red label="false"]
+5 [label="BB 5 MPI_Finalize" shape=ellipse]
+5 -> 6 [color=red label=""]
+6 [label="BB 6 " shape=ellipse]
+6 -> 1 [color=red label=""]
+1 [label="BB 1 " shape=ellipse]
+}
diff --git a/test3.c b/test3.c
new file mode 100644
index 0000000..0be728d
--- /dev/null
+++ b/test3.c
@@ -0,0 +1,33 @@
+Digraph G{
+0 [label="BB 0 " shape=ellipse]
+0 -> 2 [color=red label=""]
+2 [label="BB 2 MPI_Init" shape=ellipse]
+2 -> 10 [color=red label=""]
+3 [label="BB 3 MPI_Barrier" shape=ellipse]
+3 -> 4 [color=red label="true"]
+3 -> 8 [color=red label="false"]
+4 [label="BB 4 MPI_Barrier" shape=ellipse]
+4 -> 13 [color=red label=""]
+13 [label="BB 13 MPI_Barrier" shape=ellipse]
+13 -> 5 [color=red label="true"]
+13 -> 6 [color=red label="false"]
+5 [label="BB 5 " shape=ellipse]
+5 -> 9 [color=red label=""]
+6 [label="BB 6 " shape=ellipse]
+6 -> 7 [color=red label="true"]
+6 -> 9 [color=red label="false"]
+7 [label="BB 7 " shape=ellipse]
+7 -> 12 [color=red label=""]
+8 [label="BB 8 MPI_Barrier" shape=ellipse]
+8 -> 12 [color=red label=""]
+9 [label="BB 9 " shape=ellipse]
+9 -> 10 [color=red label=""]
+10 [label="BB 10 " shape=ellipse]
+10 -> 3 [color=red label="true"]
+10 -> 11 [color=red label="false"]
+11 [label="BB 11 MPI_Finalize" shape=ellipse]
+11 -> 12 [color=red label=""]
+12 [label="BB 12 " shape=ellipse]
+12 -> 1 [color=red label=""]
+1 [label="BB 1 " shape=ellipse]
+}
-- 
GitLab