Skip to content

src/main.cpp

This file implements sotoc, a clang to to enable outlining of OpenMP target region, which can be used by different compiler.

Namespaces

Name
clang::tooling
llvm

Classes

Name
class TargetRegionTransformer
class SourceTransformAction

Functions

Name
llvm::cl::OptionCategory SotocCategory("sotoc options" )
llvm::cl::extrahelp MoreHelp("\nExtracts code in OpenMP target regions from source file and ""generates target function code" )
int main(int argc, const char ** argv)

Attributes

Name
int SotocDebugLevel

Functions Documentation

function SotocCategory

1
2
3
static llvm::cl::OptionCategory SotocCategory(
    "sotoc options" 
)

function MoreHelp

1
2
3
static llvm::cl::extrahelp MoreHelp(
    "\nExtracts code in OpenMP target regions from source file and ""generates target function code" 
)

function main

1
2
3
4
int main(
    int argc,
    const char ** argv
)

Attributes Documentation

variable SotocDebugLevel

1
int SotocDebugLevel = 0;

Source code

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
//===-- sotoc/src/main.cpp ------------------------------------------------===//
//
//                     The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//

#include <cstdlib>
#include <memory>
#include <sstream>
#include <string>

#include "clang/Frontend/FrontendActions.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "clang/Tooling/Tooling.h"
// Declares llvm::cl::extrahelp.
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTContext.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/Process.h"
#include "llvm/Support/raw_ostream.h"

#include "Debug.h"
#include "DeclResolver.h"
#include "TargetCode.h"
#include "TargetCodeFragment.h"
#include "Visitors.h"

using namespace clang::tooling;
using namespace llvm;

class TargetRegionTransformer : public clang::ASTConsumer {
  TargetCode &Code;
  clang::Rewriter &TargetCodeRewriter;

public:
  TargetRegionTransformer(TargetCode &Code, clang::Rewriter &TargetCodeRewriter)
      : Code(Code), TargetCodeRewriter(TargetCodeRewriter) {}

  void HandleTranslationUnit(clang::ASTContext &Context) final {
    // create space to store information types and functions
    TypeDeclResolver Types;
    // Functinos are... special because they can reference new types
    FunctionDeclResolver Functions(Types);

    // read target code information from AST into TargetCode
    FindTargetCodeVisitor FindCodeVisitor(Code, Types, Functions, Context);
    FindCodeVisitor.TraverseDecl(Context.getTranslationUnitDecl());
    Functions.orderAndAddFragments(Code);
    Types.orderAndAddFragments(Code);
  }
};

class SourceTransformAction : public clang::ASTFrontendAction {
  clang::Rewriter TargetCodeRewriter;
  TargetCode *Code;

public:
  void EndSourceFileAction() override {
    // std::error_code error_code;
    // llvm::raw_fd_ostream outFile("output.txt", error_code,
    // llvm::sys::fs::F_Append);
    DEBUGP("Generating Code");
    Code->generateCode(llvm::outs());
    // outFile.close();
    delete Code;
  }

  std::unique_ptr<clang::ASTConsumer>
  CreateASTConsumer(clang::CompilerInstance &CI, clang::StringRef) final {
    TargetCodeRewriter.setSourceMgr(CI.getSourceManager(), CI.getLangOpts());
    // TargetCode holds all necessary information about source locations of
    // target regions to extract that code
    Code = new TargetCode(TargetCodeRewriter);
    return std::unique_ptr<clang::ASTConsumer>(
        new TargetRegionTransformer(*Code, TargetCodeRewriter));
  }
};

int SotocDebugLevel = 0;

static llvm::cl::OptionCategory SotocCategory("sotoc options");
static llvm::cl::extrahelp
    MoreHelp("\nExtracts code in OpenMP target regions from source file and "
             "generates target function code");

int main(int argc, const char **argv) {
  auto option = clang::tooling::CommonOptionsParser::create(argc, argv, SotocCategory);
  if (!option) {
    return -1;
  }
  clang::tooling::ClangTool tool(option->getCompilations(),
                                 option->getSourcePathList());

#ifdef SOTOC_DEBUG
  SotocDebugLevel =
      std::atoi(sys::Process::GetEnv("SOTOC_DEBUG").getValueOr("0").c_str());
#endif

  DEBUGP("Starting source transformation tool");

  return tool.run(
      clang::tooling::newFrontendActionFactory<SourceTransformAction>().get());
}

Last update: 2021-11-24
Back to top