From a44582356ac2bfe2a9d1b9c8b5b6d8ac7d2e68d9 Mon Sep 17 00:00:00 2001
From: Akash Kothari <akashk4@tyler.cs.illinois.edu>
Date: Mon, 21 Dec 2020 11:52:57 -0600
Subject: [PATCH] LLVM-9-port ExtractHPVMLeafNodes pass

---
 .../ExtractHPVMLeafNodes/CMakeLists.txt       |  3 ++-
 .../ExtractHPVMLeafNodes.cpp                  | 26 ++++++++++---------
 .../ExtractHPVMLeafNodes.exports              |  2 ++
 .../ExtractHPVMLeafNodes/LLVMBuild.txt        |  3 +--
 4 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/hpvm/lib/Transforms/ExtractHPVMLeafNodes/CMakeLists.txt b/hpvm/lib/Transforms/ExtractHPVMLeafNodes/CMakeLists.txt
index 6421b528d7..bb943f9100 100644
--- a/hpvm/lib/Transforms/ExtractHPVMLeafNodes/CMakeLists.txt
+++ b/hpvm/lib/Transforms/ExtractHPVMLeafNodes/CMakeLists.txt
@@ -2,7 +2,8 @@ if(WIN32 OR CYGWIN)
   set(LLVM_LINK_COMPONENTS Core Support)
 endif()
 
-add_llvm_loadable_module( ExtractHPVMLeafNodes
+add_llvm_library( ExtractHPVMLeafNodes
+  MODULE
   ExtractHPVMLeafNodes.cpp
 
   DEPENDS
diff --git a/hpvm/lib/Transforms/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.cpp b/hpvm/lib/Transforms/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.cpp
index cd7ead9f6c..031503aded 100644
--- a/hpvm/lib/Transforms/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.cpp
+++ b/hpvm/lib/Transforms/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.cpp
@@ -12,10 +12,11 @@
 
 #include "llvm/Support/SourceMgr.h"
 #include "llvm/Pass.h"
-#include "llvm/SupportVISC/DFGTreeTraversal.h"
-#include "llvm/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.h"
-#include "llvm/Transforms/Utils/BasicBlockUtils.h"
 
+#include "SupportHPVM/DFGTreeTraversal.h"
+#include "ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.h"
+
+#include "llvm/Transforms/Utils/BasicBlockUtils.h"
 #include "llvm/IR/IRPrintingPasses.h"
 #include "llvm/IR/LegacyPassManager.h"
 #include "llvm/Support/ToolOutputFile.h"
@@ -53,10 +54,10 @@ void PrintLeafNodes::process(DFLeafNode* N) {
   }
 
   // Find function generated for node
-  Function *F = N->getGenFuncForTarget(visc::CPU_TARGET);
+  Function *F = N->getGenFuncForTarget(hpvm::CPU_TARGET);
   assert(F != NULL
          && "This pass is invoked after code generation for x86 is completed.\nFound leaf node for which code generation has not happened!\n");
-  assert(N->hasX86GenFuncForTarget(visc::CPU_TARGET) &&
+  assert(N->hasCPUGenFuncForTarget(hpvm::CPU_TARGET) &&
          "The generated function from x86 pass is not an x86 function\n");
 
   std::string module_name = std::string("./build/") + std::string(F->getName().str().c_str()) + std::string("_module.ll");
@@ -85,8 +86,8 @@ void PrintLeafNodes::process(DFLeafNode* N) {
       StringRef CallName = CalledF->getName();
       errs() << "CallName: " << CallName << "\n";
 
-//      if (CallName.startswith("llvm_visc")) { //TODO
-      if ((CallName.startswith("llvm_visc")) || (CallName.startswith("tensor"))) { //TODO
+//      if (CallName.startswith("llvm_hpvm")) { //TODO
+      if ((CallName.startswith("llvm_hpvm")) || (CallName.startswith("tensor"))) { //TODO
 //        errs() << "This is an HPVM runtime call. Include its declaration.\n";
         errs() << "This is an HPVM runtime call or tensor. Include its declaration.\n";
 
@@ -96,7 +97,7 @@ void PrintLeafNodes::process(DFLeafNode* N) {
         for (unsigned argno = 0; argno < CI->getNumArgOperands(); argno++) {
           Fargs.push_back(CI->getArgOperand(argno));
         }
-        Function *FDecl = cast<Function>(m->getOrInsertFunction(CallName, CalledFType));
+        Function *FDecl = dyn_cast<Function>((m->getOrInsertFunction(CallName, CalledFType)).getCallee());
         CallInst *NewCI = CallInst::Create(CalledFType, FDecl, Fargs, CallName, CI);
         errs() << "NewCI: " << *NewCI << "\n";
         CI->replaceAllUsesWith(NewCI);
@@ -116,7 +117,7 @@ void PrintLeafNodes::process(DFLeafNode* N) {
 
   errs() << "Writing to File --- " << tw.str() << "\n";
   std::error_code EC;
-  tool_output_file Out(tw.str(), EC, sys::fs::F_None);
+  ToolOutputFile Out(tw.str(), EC, sys::fs::F_None);
   if (EC) {
     errs() << EC.message() << '\n';
   }
@@ -145,10 +146,10 @@ void PrintLeafNodes::process(DFLeafNode* N) {
 
   // Get the parent node's generated x86 function
   DFInternalNode *ParentNode = N->getParent();
-  Function *PGenF = ParentNode->getGenFuncForTarget(visc::CPU_TARGET);
+  Function *PGenF = ParentNode->getGenFuncForTarget(hpvm::CPU_TARGET);
   assert(PGenF != NULL
          && "This pass is invoked after code generation for x86 is completed.\nFound node for which code generation has not happened!\n");
-  assert(ParentNode->hasX86GenFuncForTarget(visc::CPU_TARGET) &&
+  assert(ParentNode->hasCPUGenFuncForTarget(hpvm::CPU_TARGET) &&
          "The generated function from x86 pass is not an x86 function\n");
 
   for (inst_iterator i = inst_begin(PGenF), e = inst_end(PGenF); i != e; ++i) {
@@ -169,7 +170,7 @@ void PrintLeafNodes::process(DFLeafNode* N) {
         for (unsigned argno = 0; argno < CI->getNumArgOperands(); argno++) {
           Fargs.push_back(CI->getArgOperand(argno));
         }
-        Function *FDecl = cast<Function>(M.getOrInsertFunction(FName, FType));
+        Function *FDecl = dyn_cast<Function>(M.getOrInsertFunction(FName, FType).getCallee());
         CallInst *NewCI = CallInst::Create(FType, FDecl, Fargs, FName, CI);
         errs() << "NewCI: " << *NewCI << "\n";
         CI->replaceAllUsesWith(NewCI);
@@ -244,3 +245,4 @@ static RegisterPass<ExtractHPVMLeafNodeGenFunctionsWrapper> X(
          "Pass to extract leaf nodes to modules in HPVM",
          false /* does not modify the CFG */,
 true /* transformation, not just analysis */);
+
diff --git a/hpvm/lib/Transforms/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.exports b/hpvm/lib/Transforms/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.exports
index e69de29bb2..139597f9cb 100644
--- a/hpvm/lib/Transforms/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.exports
+++ b/hpvm/lib/Transforms/ExtractHPVMLeafNodes/ExtractHPVMLeafNodes.exports
@@ -0,0 +1,2 @@
+
+
diff --git a/hpvm/lib/Transforms/ExtractHPVMLeafNodes/LLVMBuild.txt b/hpvm/lib/Transforms/ExtractHPVMLeafNodes/LLVMBuild.txt
index 9862f559e5..73ac540f06 100644
--- a/hpvm/lib/Transforms/ExtractHPVMLeafNodes/LLVMBuild.txt
+++ b/hpvm/lib/Transforms/ExtractHPVMLeafNodes/LLVMBuild.txt
@@ -1,4 +1,4 @@
-;===- ./lib/Transforms/DFG2LLVM_NVPTX/LLVMBuild.txt ------------*- Conf -*--===;
+;===- ./lib/Transforms/DFG2LLVM_WrapperAPI/LLVMBuild.txt -------*- Conf -*--===;
 ;
 ;                     The LLVM Compiler Infrastructure
 ;
@@ -19,4 +19,3 @@
 type = Library
 name = ExtractHPVMLeafNodes
 parent = Transforms
-
-- 
GitLab