Skip to content

Commit

Permalink
Merge branch 'new-wrapper-tool' into 'aurora_offloading_prototype'
Browse files Browse the repository at this point in the history
A new wrapper tool.

See merge request NEC-RWTH-Projects/clang!19
  • Loading branch information
manorom committed Mar 27, 2019
2 parents 5421001 + a5fbaef commit 513302a
Show file tree
Hide file tree
Showing 13 changed files with 569 additions and 545 deletions.
40 changes: 21 additions & 19 deletions clang/lib/Driver/ToolChains/NECAuroraOffload.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,13 @@ using namespace clang::driver;
using namespace clang::driver::tools;
using namespace llvm::opt;


void necauroratools::Common::ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output,
const InputInfoList &Inputs,
const llvm::opt::ArgList &Args,
const char *LinkingOutput) const {
ArgStringList CmdArgs;
std::vector<llvm::opt::Arg*> PPargs;
std::vector<llvm::opt::Arg *> PPargs;

// We need to pass the input source, one file at a time, as first argument to
// the compiler wrapper.
Expand Down Expand Up @@ -51,8 +50,9 @@ void necauroratools::Common::ConstructJob(Compilation &C, const JobAction &JA,
!A->getOption().hasFlag(options::DriverOption) &&
!A->getOption().hasFlag(options::LinkerInput)) {

// MR_MARKER: because we are the offloading compiler, we dont have to claim(?)
//A->claim();
// MR_MARKER: because we are the offloading compiler, we dont have to
// claim(?)
// A->claim();

// Don't forward any -g arguments to assembly steps.
if (isa<AssembleJobAction>(JA) &&
Expand All @@ -64,18 +64,20 @@ void necauroratools::Common::ConstructJob(Compilation &C, const JobAction &JA,
A->getOption().matches(options::OPT_W_Group))
continue;

if(A->getOption().matches(options::OPT_Preprocessor_Group)){
PPargs.push_back(A);
if (A->getOption().matches(options::OPT_Preprocessor_Group)) {
PPargs.push_back(A);
continue;
}

A->render(Args, CmdArgs);
}
}

for(auto &A : PPargs) {
for(uint i = 0; i<A->getNumValues();++i){
CmdArgs.push_back(Args.MakeArgString(("-" + std::string(A->getOption().getName()) + A->getValue(i)).c_str()));
for (auto &A : PPargs) {
for (uint i = 0; i < A->getNumValues(); ++i) {
CmdArgs.push_back(Args.MakeArgString(
("-" + std::string(A->getOption().getName()) + A->getValue(i))
.c_str()));
}
}

Expand All @@ -89,19 +91,20 @@ void necauroratools::Common::ConstructJob(Compilation &C, const JobAction &JA,
CmdArgs.push_back("-fsyntax-only");
}

const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(ToolName));
const char *Exec =
Args.MakeArgString(getToolChain().GetProgramPath(ToolName));
C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
}

void necauroratools::Common::anchor() {}

void necauroratools::Linker::RenderExtraToolArgs(const JobAction &JA,
llvm::opt::ArgStringList &CmdArgs) const {
void necauroratools::Linker::RenderExtraToolArgs(
const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const {
// no extra args, just hope for the best
}

void necauroratools::OffloadCompilerWrapper::RenderExtraToolArgs(const JobAction &JA,
llvm::opt::ArgStringList &CmdArgs) const {
void necauroratools::OffloadCompilerWrapper::RenderExtraToolArgs(
const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const {
// the same as for Gnu
const Driver &D = getToolChain().getDriver();

Expand All @@ -128,16 +131,15 @@ void necauroratools::OffloadCompilerWrapper::RenderExtraToolArgs(const JobAction
default:
D.Diag(diag::err_drv_invalid_gcc_output_type) << getTypeName(JA.getType());
}

}

void necauroratools::Assembler::RenderExtraToolArgs(const JobAction &JA,
llvm::opt::ArgStringList &CmdArgs) const {
void necauroratools::Assembler::RenderExtraToolArgs(
const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const {
CmdArgs.push_back("-S");
}


Tool *toolchains::NECAuroraOffloadToolChain::SelectTool(const JobAction &JA) const {
Tool *
toolchains::NECAuroraOffloadToolChain::SelectTool(const JobAction &JA) const {
switch (JA.getKind()) {
case Action::PreprocessJobClass:
case Action::CompileJobClass:
Expand Down
32 changes: 14 additions & 18 deletions clang/lib/Driver/ToolChains/NECAuroraOffload.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,13 @@ namespace necauroratools {
// so, like gcc, we can create a common base class
class LLVM_LIBRARY_VISIBILITY Common : public Tool {
virtual void anchor();

public:
Common(const char *Name, const char *ShortName, const char *BinaryName,
const ToolChain &TC)
: Tool(Name, ShortName, TC), ToolName(BinaryName) {}
Common(const char *Name, const char *ShortName, const ToolChain &TC)
: Tool(Name, ShortName, TC), ToolName("necaurora-ofld-wrapper") {}

bool hasIntegratedAssembler() const override { return true; }


void ConstructJob(Compilation &C, const JobAction &JA,
const InputInfo &Output, const InputInfoList &Inputs,
const llvm::opt::ArgList &Args,
Expand All @@ -42,12 +41,10 @@ class LLVM_LIBRARY_VISIBILITY Common : public Tool {
const char *ToolName;
};


class LLVM_LIBRARY_VISIBILITY Linker : public Common {
public:
Linker(const ToolChain &TC)
: Common("NECAurora::Linker", "Linker (via ncc)",
"necaurora-linker-wrapper", TC) {}
: Common("NECAurora::Linker", "Linker (via ncc)", TC) {}

bool hasIntegratedCPP() const override { return false; }
bool isLinkJob() const override { return true; }
Expand All @@ -56,12 +53,11 @@ class LLVM_LIBRARY_VISIBILITY Linker : public Common {
llvm::opt::ArgStringList &CmdArgs) const override;
};


class LLVM_LIBRARY_VISIBILITY OffloadCompilerWrapper : public Common {
public:
OffloadCompilerWrapper(const ToolChain &TC)
: Common("NECAurora::OffloadCompiler", "Offload Compiler (via ncc)",
"necaurora-ofld-cc1-wrapper", TC) {}
: Common("NECAurora::OffloadCompiler", "Offload Compiler (via ncc)", TC) {
}

void RenderExtraToolArgs(const JobAction &JA,
llvm::opt::ArgStringList &CmdArgs) const override;
Expand All @@ -74,27 +70,27 @@ class LLVM_LIBRARY_VISIBILITY OffloadCompilerWrapper : public Common {
class LLVM_LIBRARY_VISIBILITY Assembler : public Common {
public:
Assembler(const ToolChain &TC)
: Common("NECAurora::Assembler", "Assembler (via ncc)",
"necaurora-asm-wrapper", TC) {}
: Common("NECAurora::Assembler", "Assembler (via ncc)", TC) {}

bool hasIntegratedCPP() const override { return false; } //?

void RenderExtraToolArgs(const JobAction &JA,
llvm::opt::ArgStringList &CmdArgs) const override;
};

} // necauroratools
} // tools
} // namespace necauroratools
} // namespace tools

namespace toolchains {

class LLVM_LIBRARY_VISIBILITY NECAuroraOffloadToolChain : public Generic_ELF {
virtual void anchor();

public:
NECAuroraOffloadToolChain(const Driver &D, const llvm::Triple &Triple,
const ToolChain &HostTC,
const llvm::opt::ArgList &Args)
: Generic_ELF(D, Triple, Args), HostTC(HostTC) {}
: Generic_ELF(D, Triple, Args), HostTC(HostTC) {}

const llvm::Triple *getAuxTriple() const override {
return &HostTC.getTriple();
Expand All @@ -121,8 +117,8 @@ class LLVM_LIBRARY_VISIBILITY NECAuroraOffloadToolChain : public Generic_ELF {
Tool *buildAssembler() const override;
};

} // toolchains
} // driver
} // clang
} // namespace toolchains
} // namespace driver
} // namespace clang

#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_NECAURORAOFFLOAD_H
42 changes: 29 additions & 13 deletions clang/tools/nec-aurora-build/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,34 +8,50 @@ find_program(NECAURORA_TARGET_COMPILER ncc
DOC "NEC target compiler."
PATHS
"/opt/nec/ve/bin/")

if(NOT NECAURORA_TARGET_COMPILER)
message(STATUS "NEC target compiler could not be found, defaulting to GCC.")
# Use a separate variable to print this message every time.
find_program(NECAURORA_TARGET_COMPILER_GCC gcc)
set(NECAURORA_TARGET_COMPILER "${NECAURORA_TARGET_COMPILER_GCC}")
endif()

find_library(
NECAURORA_LIBVEORUN_STATIC
NAMES
libveorun.a
PATHS
/usr/lib
/usr/local/lib
/opt/local/lib
/sw/lib
/opt/nec/ve/lib
ENV LIBRARY_PATH
ENV LD_LIBRARY_PATH)

set(NECAURORA_STATIC_LINKING_AVAILABLE 1)

if (NECAURORA_LIBVEORUN_STATIC)
message(STATUS "Found libveorun.a")
set(LIBVEORUN_STATIC_PATH "${NECAURORA_LIBVEORUN_STATIC}")
endif()

find_package(NECAURORA_LIBELF)
if(NOT NECAURORA_LIBELF_FOUND)
# TODO: Really fatal error? Can we just exclude static linking?
message(FATAL_ERROR "LIBELF not found.")
endif()

configure_file(config.h.in config.h @ONLY)
include_directories(${CMAKE_CURRENT_BINARY_DIR})

add_executable(necaurora-ofld-cc1-wrapper
necaurora-ofld-cc1-wrapper.cpp
necaurora-utils.cpp)
add_executable(necaurora-ofld-wrapper
necaurora-ofld-wrapper.cpp
compiler.cpp
linker.cpp
utils.cpp)

add_executable(necaurora-ofld-tool-wrapper
necaurora-ofld-tool-wrapper.cpp
necaurora-utils.cpp)
target_link_libraries(necaurora-ofld-tool-wrapper
target_link_libraries(necaurora-ofld-wrapper
${NECAURORA_LIBELF_LIBRARIES})

install(TARGETS necaurora-ofld-cc1-wrapper DESTINATION bin)


install(TARGETS necaurora-ofld-tool-wrapper DESTINATION bin)
add_clang_symlink(necaurora-linker-wrapper necaurora-ofld-tool-wrapper)
add_clang_symlink(necaurora-asm-wrapper necaurora-ofld-tool-wrapper)
install(TARGETS necaurora-ofld-wrapper DESTINATION bin)
76 changes: 76 additions & 0 deletions clang/tools/nec-aurora-build/compiler.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
#include <iostream>
#include <sstream>
#include <unistd.h>

#include "necaurora-ofld-wrapper.h"

int runSourceTransformation(const std::string &InputPath,
const std::string &SotocPath,
std::string &OutputPath,
const std::string &ArgsString) {
std::stringstream CmdLine;

// find last '/' in string so we can get just the filename
size_t PosLastSlashInPath = InputPath.rfind("/");

if (PosLastSlashInPath == InputPath.length()) {
PosLastSlashInPath = 0;
}

// find last '.' in string so we can get the filename extension
size_t PosLastDotInPath = InputPath.rfind(".");
if (PosLastDotInPath == std::string::npos) {
std::cerr
<< "necaurora-ofld-cc1-wrapper: Input file has no file extension (1)"
<< " (neeeds to be .c or .cpp or .i)" << std::endl;
return -1;
}

// Input file name without extension
std::string InputFileNameWE(InputPath, PosLastSlashInPath + 1,
(PosLastDotInPath - (PosLastSlashInPath + 1)));

std::string InputFileExt(InputPath, PosLastDotInPath);

if (InputFileExt != ".c" && InputFileExt != ".cpp" && InputFileExt != ".i") {
std::cerr << "necaurora-ofld-cc1-wrapper: Input file has no file extension"
<< std::endl;
return -1;
}

// We create an empty temp file
std::string Content = "";
std::string TmpFile = writeTmpFile(Content, InputFileNameWE + ".sotoc-transformed", InputFileExt);
if (TmpFile == "")
return -1;

CmdLine << SotocPath << " " << InputPath << " -- " << ArgsString << " > "
<< TmpFile;

if (Verbose) {
std::cout << " \"" << CmdLine.str() << "\"\n";
}

OutputPath = TmpFile;
return system(CmdLine.str().c_str());
}

int runTargetCompiler(const std::string &InputPath, const std::string &Args) {

std::stringstream CmdLine;

CmdLine << getTargetCompiler() << " -c " << InputPath << " " << Args;

if (Verbose) {
std::cout << " \"" << CmdLine.str() << "\"" << std::endl;
}

int ret = system(CmdLine.str().c_str());

if (!KeepTransformedFilesDir) {
std::cout << "Removing tmpfil" << std::endl;
std::remove(InputPath.c_str());
}

return ret;
}
1 change: 1 addition & 0 deletions clang/tools/nec-aurora-build/config.h.in
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
#define DEFAULT_TARGET_COMPILER "@NECAURORA_TARGET_COMPILER@"
#cmakedefine LIBVEORUN_STATIC_PATH "@LIBVEORUN_STATIC_PATH@"
Loading

0 comments on commit 513302a

Please sign in to comment.