Skip to content

Commit 513302a

Browse files
committed
Merge branch 'new-wrapper-tool' into 'aurora_offloading_prototype'
A new wrapper tool. See merge request NEC-RWTH-Projects/clang!19
2 parents 5421001 + a5fbaef commit 513302a

13 files changed

+569
-545
lines changed

clang/lib/Driver/ToolChains/NECAuroraOffload.cpp

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,13 @@ using namespace clang::driver;
1212
using namespace clang::driver::tools;
1313
using namespace llvm::opt;
1414

15-
1615
void necauroratools::Common::ConstructJob(Compilation &C, const JobAction &JA,
1716
const InputInfo &Output,
1817
const InputInfoList &Inputs,
1918
const llvm::opt::ArgList &Args,
2019
const char *LinkingOutput) const {
2120
ArgStringList CmdArgs;
22-
std::vector<llvm::opt::Arg*> PPargs;
21+
std::vector<llvm::opt::Arg *> PPargs;
2322

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

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

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

67-
if(A->getOption().matches(options::OPT_Preprocessor_Group)){
68-
PPargs.push_back(A);
67+
if (A->getOption().matches(options::OPT_Preprocessor_Group)) {
68+
PPargs.push_back(A);
6969
continue;
7070
}
7171

7272
A->render(Args, CmdArgs);
7373
}
7474
}
7575

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

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

92-
const char *Exec = Args.MakeArgString(getToolChain().GetProgramPath(ToolName));
94+
const char *Exec =
95+
Args.MakeArgString(getToolChain().GetProgramPath(ToolName));
9396
C.addCommand(llvm::make_unique<Command>(JA, *this, Exec, CmdArgs, Inputs));
9497
}
9598

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

98-
void necauroratools::Linker::RenderExtraToolArgs(const JobAction &JA,
99-
llvm::opt::ArgStringList &CmdArgs) const {
101+
void necauroratools::Linker::RenderExtraToolArgs(
102+
const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const {
100103
// no extra args, just hope for the best
101104
}
102105

103-
void necauroratools::OffloadCompilerWrapper::RenderExtraToolArgs(const JobAction &JA,
104-
llvm::opt::ArgStringList &CmdArgs) const {
106+
void necauroratools::OffloadCompilerWrapper::RenderExtraToolArgs(
107+
const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const {
105108
// the same as for Gnu
106109
const Driver &D = getToolChain().getDriver();
107110

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

134-
void necauroratools::Assembler::RenderExtraToolArgs(const JobAction &JA,
135-
llvm::opt::ArgStringList &CmdArgs) const {
136+
void necauroratools::Assembler::RenderExtraToolArgs(
137+
const JobAction &JA, llvm::opt::ArgStringList &CmdArgs) const {
136138
CmdArgs.push_back("-S");
137139
}
138140

139-
140-
Tool *toolchains::NECAuroraOffloadToolChain::SelectTool(const JobAction &JA) const {
141+
Tool *
142+
toolchains::NECAuroraOffloadToolChain::SelectTool(const JobAction &JA) const {
141143
switch (JA.getKind()) {
142144
case Action::PreprocessJobClass:
143145
case Action::CompileJobClass:

clang/lib/Driver/ToolChains/NECAuroraOffload.h

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,13 @@ namespace necauroratools {
1616
// so, like gcc, we can create a common base class
1717
class LLVM_LIBRARY_VISIBILITY Common : public Tool {
1818
virtual void anchor();
19+
1920
public:
20-
Common(const char *Name, const char *ShortName, const char *BinaryName,
21-
const ToolChain &TC)
22-
: Tool(Name, ShortName, TC), ToolName(BinaryName) {}
21+
Common(const char *Name, const char *ShortName, const ToolChain &TC)
22+
: Tool(Name, ShortName, TC), ToolName("necaurora-ofld-wrapper") {}
2323

2424
bool hasIntegratedAssembler() const override { return true; }
2525

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

45-
4644
class LLVM_LIBRARY_VISIBILITY Linker : public Common {
4745
public:
4846
Linker(const ToolChain &TC)
49-
: Common("NECAurora::Linker", "Linker (via ncc)",
50-
"necaurora-linker-wrapper", TC) {}
47+
: Common("NECAurora::Linker", "Linker (via ncc)", TC) {}
5148

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

59-
6056
class LLVM_LIBRARY_VISIBILITY OffloadCompilerWrapper : public Common {
6157
public:
6258
OffloadCompilerWrapper(const ToolChain &TC)
63-
: Common("NECAurora::OffloadCompiler", "Offload Compiler (via ncc)",
64-
"necaurora-ofld-cc1-wrapper", TC) {}
59+
: Common("NECAurora::OffloadCompiler", "Offload Compiler (via ncc)", TC) {
60+
}
6561

6662
void RenderExtraToolArgs(const JobAction &JA,
6763
llvm::opt::ArgStringList &CmdArgs) const override;
@@ -74,27 +70,27 @@ class LLVM_LIBRARY_VISIBILITY OffloadCompilerWrapper : public Common {
7470
class LLVM_LIBRARY_VISIBILITY Assembler : public Common {
7571
public:
7672
Assembler(const ToolChain &TC)
77-
: Common("NECAurora::Assembler", "Assembler (via ncc)",
78-
"necaurora-asm-wrapper", TC) {}
73+
: Common("NECAurora::Assembler", "Assembler (via ncc)", TC) {}
7974

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

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

86-
} // necauroratools
87-
} // tools
81+
} // namespace necauroratools
82+
} // namespace tools
8883

8984
namespace toolchains {
9085

9186
class LLVM_LIBRARY_VISIBILITY NECAuroraOffloadToolChain : public Generic_ELF {
9287
virtual void anchor();
88+
9389
public:
9490
NECAuroraOffloadToolChain(const Driver &D, const llvm::Triple &Triple,
9591
const ToolChain &HostTC,
9692
const llvm::opt::ArgList &Args)
97-
: Generic_ELF(D, Triple, Args), HostTC(HostTC) {}
93+
: Generic_ELF(D, Triple, Args), HostTC(HostTC) {}
9894

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

124-
} // toolchains
125-
} // driver
126-
} // clang
120+
} // namespace toolchains
121+
} // namespace driver
122+
} // namespace clang
127123

128124
#endif // LLVM_CLANG_LIB_DRIVER_TOOLCHAINS_NECAURORAOFFLOAD_H

clang/tools/nec-aurora-build/CMakeLists.txt

Lines changed: 29 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,34 +8,50 @@ find_program(NECAURORA_TARGET_COMPILER ncc
88
DOC "NEC target compiler."
99
PATHS
1010
"/opt/nec/ve/bin/")
11+
1112
if(NOT NECAURORA_TARGET_COMPILER)
1213
message(STATUS "NEC target compiler could not be found, defaulting to GCC.")
1314
# Use a separate variable to print this message every time.
1415
find_program(NECAURORA_TARGET_COMPILER_GCC gcc)
1516
set(NECAURORA_TARGET_COMPILER "${NECAURORA_TARGET_COMPILER_GCC}")
1617
endif()
1718

19+
find_library(
20+
NECAURORA_LIBVEORUN_STATIC
21+
NAMES
22+
libveorun.a
23+
PATHS
24+
/usr/lib
25+
/usr/local/lib
26+
/opt/local/lib
27+
/sw/lib
28+
/opt/nec/ve/lib
29+
ENV LIBRARY_PATH
30+
ENV LD_LIBRARY_PATH)
31+
32+
set(NECAURORA_STATIC_LINKING_AVAILABLE 1)
33+
34+
if (NECAURORA_LIBVEORUN_STATIC)
35+
message(STATUS "Found libveorun.a")
36+
set(LIBVEORUN_STATIC_PATH "${NECAURORA_LIBVEORUN_STATIC}")
37+
endif()
38+
1839
find_package(NECAURORA_LIBELF)
1940
if(NOT NECAURORA_LIBELF_FOUND)
2041
# TODO: Really fatal error? Can we just exclude static linking?
2142
message(FATAL_ERROR "LIBELF not found.")
2243
endif()
44+
2345
configure_file(config.h.in config.h @ONLY)
2446
include_directories(${CMAKE_CURRENT_BINARY_DIR})
2547

26-
add_executable(necaurora-ofld-cc1-wrapper
27-
necaurora-ofld-cc1-wrapper.cpp
28-
necaurora-utils.cpp)
48+
add_executable(necaurora-ofld-wrapper
49+
necaurora-ofld-wrapper.cpp
50+
compiler.cpp
51+
linker.cpp
52+
utils.cpp)
2953

30-
add_executable(necaurora-ofld-tool-wrapper
31-
necaurora-ofld-tool-wrapper.cpp
32-
necaurora-utils.cpp)
33-
target_link_libraries(necaurora-ofld-tool-wrapper
54+
target_link_libraries(necaurora-ofld-wrapper
3455
${NECAURORA_LIBELF_LIBRARIES})
3556

36-
install(TARGETS necaurora-ofld-cc1-wrapper DESTINATION bin)
37-
38-
39-
install(TARGETS necaurora-ofld-tool-wrapper DESTINATION bin)
40-
add_clang_symlink(necaurora-linker-wrapper necaurora-ofld-tool-wrapper)
41-
add_clang_symlink(necaurora-asm-wrapper necaurora-ofld-tool-wrapper)
57+
install(TARGETS necaurora-ofld-wrapper DESTINATION bin)
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
#include <iostream>
2+
#include <sstream>
3+
#include <unistd.h>
4+
5+
#include "necaurora-ofld-wrapper.h"
6+
7+
int runSourceTransformation(const std::string &InputPath,
8+
const std::string &SotocPath,
9+
std::string &OutputPath,
10+
const std::string &ArgsString) {
11+
std::stringstream CmdLine;
12+
13+
// find last '/' in string so we can get just the filename
14+
size_t PosLastSlashInPath = InputPath.rfind("/");
15+
16+
if (PosLastSlashInPath == InputPath.length()) {
17+
PosLastSlashInPath = 0;
18+
}
19+
20+
// find last '.' in string so we can get the filename extension
21+
size_t PosLastDotInPath = InputPath.rfind(".");
22+
if (PosLastDotInPath == std::string::npos) {
23+
std::cerr
24+
<< "necaurora-ofld-cc1-wrapper: Input file has no file extension (1)"
25+
<< " (neeeds to be .c or .cpp or .i)" << std::endl;
26+
return -1;
27+
}
28+
29+
// Input file name without extension
30+
std::string InputFileNameWE(InputPath, PosLastSlashInPath + 1,
31+
(PosLastDotInPath - (PosLastSlashInPath + 1)));
32+
33+
std::string InputFileExt(InputPath, PosLastDotInPath);
34+
35+
if (InputFileExt != ".c" && InputFileExt != ".cpp" && InputFileExt != ".i") {
36+
std::cerr << "necaurora-ofld-cc1-wrapper: Input file has no file extension"
37+
<< std::endl;
38+
return -1;
39+
}
40+
41+
// We create an empty temp file
42+
std::string Content = "";
43+
std::string TmpFile = writeTmpFile(Content, InputFileNameWE + ".sotoc-transformed", InputFileExt);
44+
if (TmpFile == "")
45+
return -1;
46+
47+
CmdLine << SotocPath << " " << InputPath << " -- " << ArgsString << " > "
48+
<< TmpFile;
49+
50+
if (Verbose) {
51+
std::cout << " \"" << CmdLine.str() << "\"\n";
52+
}
53+
54+
OutputPath = TmpFile;
55+
return system(CmdLine.str().c_str());
56+
}
57+
58+
int runTargetCompiler(const std::string &InputPath, const std::string &Args) {
59+
60+
std::stringstream CmdLine;
61+
62+
CmdLine << getTargetCompiler() << " -c " << InputPath << " " << Args;
63+
64+
if (Verbose) {
65+
std::cout << " \"" << CmdLine.str() << "\"" << std::endl;
66+
}
67+
68+
int ret = system(CmdLine.str().c_str());
69+
70+
if (!KeepTransformedFilesDir) {
71+
std::cout << "Removing tmpfil" << std::endl;
72+
std::remove(InputPath.c_str());
73+
}
74+
75+
return ret;
76+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
#define DEFAULT_TARGET_COMPILER "@NECAURORA_TARGET_COMPILER@"
2+
#cmakedefine LIBVEORUN_STATIC_PATH "@LIBVEORUN_STATIC_PATH@"

0 commit comments

Comments
 (0)