From 41f65c0aeaa66155886372dc8652bf9bed0f62d4 Mon Sep 17 00:00:00 2001
From: Enzo De Carvalho Bittencourt <enzo.decarvalhobittencourt@ensiie.eu>
Date: Tue, 17 Oct 2023 15:11:49 +0200
Subject: [PATCH] added postdom by sets

---
 include/pass_mpi_collective.hpp |  5 ++
 src/pass_mpi_collective.cpp     | 81 ++++++++++++++++++++++++++++++++-
 2 files changed, 85 insertions(+), 1 deletion(-)

diff --git a/include/pass_mpi_collective.hpp b/include/pass_mpi_collective.hpp
index c95fcca..b620a5d 100644
--- a/include/pass_mpi_collective.hpp
+++ b/include/pass_mpi_collective.hpp
@@ -88,6 +88,11 @@ public:
 	// rank definition
 	void rank_collective(function *fun);
 	void __rank_collective(basic_block bb);
+	
+	//post-dom set of same-rank collectives
+	bool __is_pd_set_mpi_coll_rank(function* fun, basic_block bb, int rank
+																 , int mpi_code, bitmap pd);
+	bitmap_head __get_set_mpi_coll_rank(function* fun, int rank, int mpi_code);
 
 	void better_rank_collective(function *fun);
 	int __better_rank_collective(basic_block bb,
diff --git a/src/pass_mpi_collective.cpp b/src/pass_mpi_collective.cpp
index c65d0cf..e18c4ad 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>
+//bitmaps
+#include <bitmap.h>
 // basename
 #include <filesystem>
 // set
@@ -68,6 +70,9 @@ unsigned int pass_mpi_collective::execute(function *fun)
 	mark_edge(fun);
 	rank_collective(fun);
 	//better_rank_collective(fun);
+	
+	bitmap_head pdbitmap = __get_set_mpi_coll_rank(fun, 2, MPI_BARRIER);
+	debug_bitmap(&pdbitmap);
 
 	cfgviz_dump(fun, "_split");
 
@@ -628,11 +633,85 @@ int pass_mpi_collective::__better_rank_collective(basic_block bb,
 	{
 		next_rank++;
 	}
+	
 	((bb_data *) bb->aux)->collective_rank[mpi_code] = next_rank;
-
 	return next_rank;
 }
 
+bool pass_mpi_collective::__is_pd_set_mpi_coll_rank(function* fun
+																										, basic_block bb
+																										, int rank
+																										, int mpi_code
+																										, bitmap pds)
+{
+	edge e;
+	edge_iterator ei;
+	
+	//printf("entered bb->index : %i\n",bb->index);
+	((bb_data *) bb->aux)->mark1 = 1;
+	
+	if (((bb_data *) bb->aux)->collective_rank[mpi_code]  
+			== rank) //if we are in the pd set
+	{ 	
+		bitmap_set_bit(pds, bb->index);
+		return true;
+	}
+	
+	if (bb == EXIT_BLOCK_PTR_FOR_FN(fun))
+	{
+		return false;
+	} 	
+	
+	bool is_pd_by_set = true;//is postdominated by set
+	int count_edges  = 0;
+
+	FOR_EACH_EDGE(e, ei, bb->succs)
+	{
+		if (((edge_data *) e->aux)->loop)
+		{
+			//printf("loop detected... Jumping out...\n");
+			continue; //if marked as a loop or already visited, ignore
+		}
+		
+		count_edges++;
+		//printf("%i: %i\n",bb->index, count_edges);
+		if (((bb_data *) e->dest->aux)->mark1)
+		{
+			//printf("node %i already visited.. jumping out...\n", e->dest->index);
+			is_pd_by_set = is_pd_by_set && bitmap_bit_p(pds, e->dest->index);
+			continue; //if already visited, ignore
+		} 
+
+		is_pd_by_set = 
+			 __is_pd_set_mpi_coll_rank(fun, e->dest, rank, mpi_code, pds) 
+			 && is_pd_by_set;
+	}
+	
+	if (count_edges == 0)
+	{
+		return false; //we are in a leaf
+	} 
+
+	if (is_pd_by_set) // add the bb to the pdset if it is pd by set.
+	{
+		bitmap_set_bit(pds, bb->index);
+	}
+	//printf("I now know that %i is %b\n", bb->index, is_pd_by_set);
+	return is_pd_by_set;
+}
+
+bitmap_head pass_mpi_collective::__get_set_mpi_coll_rank(function* fun
+																													, int rank
+																													, int mpi_code)
+{
+	bitmap_head set_post_dommed;
+	bitmap_initialize(&set_post_dommed, &bitmap_default_obstack);
+	
+	__is_pd_set_mpi_coll_rank(fun, ENTRY_BLOCK_PTR_FOR_FN(fun)
+														, rank, mpi_code, &set_post_dommed);
+
+	return set_post_dommed;
+} 
 //============================	GRAPHVIZ  =====================================
 
 /* Build a filename (as a string) based on function name */
-- 
GitLab