Skip to content
This repository was archived by the owner on Apr 28, 2023. It is now read-only.

Commit 7b0bd2c

Browse files
Extract checkedSystemCall function
It will be reused in another location in the next commit.
1 parent 9bd04d4 commit 7b0bd2c

File tree

2 files changed

+42
-16
lines changed

2 files changed

+42
-16
lines changed

tc/core/cuda/cuda_rtc.cc

Lines changed: 6 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#include "tc/core/cuda/cuda_rtc.h"
3030
#include "tc/core/flags.h"
3131
#include "tc/core/scope_guard.h"
32+
#include "tc/core/utils/system.h"
3233

3334
namespace tc {
3435
std::mutex nvrtc_mutex;
@@ -65,17 +66,6 @@ void checkOrCreateContext() {
6566
}
6667

6768
namespace {
68-
static void checkedSystemCall(
69-
const std::string& cmd,
70-
const std::vector<std::string>& args) {
71-
std::stringstream command;
72-
command << cmd << " ";
73-
for (const auto& s : args) {
74-
command << s << " ";
75-
}
76-
TC_CHECK_EQ(std::system(command.str().c_str()), 0) << command.str();
77-
}
78-
7969
static std::tuple<int, int, int> getCudaArchitecture() {
8070
int device, major, minor;
8171
CUdevice deviceHandle;
@@ -119,7 +109,7 @@ static std::string llvmCompile(
119109
});
120110

121111
// Compile
122-
checkedSystemCall(
112+
utils::checkedSystemCall(
123113
std::string(TC_STRINGIFY(TC_LLVM_BIN_DIR)) + "/clang++",
124114
{"-x cuda " + inputFileName,
125115
"--cuda-device-only",
@@ -134,7 +124,7 @@ static std::string llvmCompile(
134124
"-o " + outputClangFile});
135125

136126
// Link libdevice before opt
137-
checkedSystemCall(
127+
utils::checkedSystemCall(
138128
std::string(TC_STRINGIFY(TC_LLVM_BIN_DIR)) + "/llvm-link ",
139129
{outputClangFile,
140130
std::string(TC_STRINGIFY(TC_CUDA_TOOLKIT_ROOT_DIR)) +
@@ -143,7 +133,7 @@ static std::string llvmCompile(
143133
"-o " + outputLinkFile});
144134

145135
// Opt
146-
checkedSystemCall(
136+
utils::checkedSystemCall(
147137
std::string(TC_STRINGIFY(TC_LLVM_BIN_DIR)) + "/opt",
148138
{"-internalize",
149139
std::string("-internalize-public-api-list=") + name,
@@ -154,7 +144,7 @@ static std::string llvmCompile(
154144
std::string("-o ") + outputOptFile});
155145

156146
// Ptx
157-
checkedSystemCall(
147+
utils::checkedSystemCall(
158148
std::string(TC_STRINGIFY(TC_LLVM_BIN_DIR)) + "/llc",
159149
{std::string("-mcpu=") + arch,
160150
outputOptFile,
@@ -188,7 +178,7 @@ static std::string nvccCompile(
188178
// cstdio's std::remove to delete files
189179
tc::ScopeGuard sgo([&]() { std::remove(outputPtxFile.c_str()); });
190180

191-
checkedSystemCall(
181+
utils::checkedSystemCall(
192182
std::string(TC_STRINGIFY(TC_CUDA_TOOLKIT_ROOT_DIR)) + "/bin/nvcc",
193183
{"-x cu",
194184
inputFileName,

tc/core/utils/system.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
/**
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
#pragma once
17+
18+
#include <string>
19+
#include <vector>
20+
21+
#include "tc/core/check.h"
22+
23+
namespace tc {
24+
namespace utils {
25+
inline void checkedSystemCall(
26+
const std::string& cmd,
27+
const std::vector<std::string>& args) {
28+
std::stringstream command;
29+
command << cmd << " ";
30+
for (const auto& s : args) {
31+
command << s << " ";
32+
}
33+
TC_CHECK_EQ(std::system(command.str().c_str()), 0) << command.str();
34+
}
35+
} // namespace utils
36+
} // namespace tc

0 commit comments

Comments
 (0)