23#ifndef SRC_HIPBIN_NVIDIA_H_
24#define SRC_HIPBIN_NVIDIA_H_
26#include "hipBin_base.h"
27#include "hipBin_util.h"
35 string cudaPath_ =
"";
37 string hipCFlags_, hipCXXFlags_, hipLdFlags_;
42 virtual bool detectPlatform();
43 virtual void constructCompilerPath();
44 virtual const string& getCompilerPath()
const;
46 virtual string getCppConfig();
47 virtual void printFull();
48 virtual void printCompilerInfo()
const;
49 virtual string getCompilerVersion();
50 virtual void checkHipconfig();
51 virtual string getDeviceLibPath()
const;
52 virtual string getHipLibPath()
const;
53 virtual string getHipCC()
const;
54 virtual string getCompilerIncludePath();
55 virtual string getHipInclude()
const;
56 virtual void initializeHipCXXFlags();
57 virtual void initializeHipCFlags();
58 virtual void initializeHipLdFlags();
59 virtual const string& getHipCXXFlags()
const;
60 virtual const string& getHipCFlags()
const;
61 virtual const string& getHipLdFlags()
const;
62 virtual void executeHipCCCmd(vector<string> argv);
65HipBinNvidia::HipBinNvidia() {
67 platformInfo.os = getOSInfo();
68 platformInfo.platform = nvidia;
69 platformInfo.runtime = cuda;
70 platformInfo.compiler = nvcc;
71 platformInfoNV_ = platformInfo;
72 constructCompilerPath();
76bool HipBinNvidia::detectPlatform() {
78 const string& nvccPath = getCompilerPath();
79 fs::path cmdNv = nvccPath;
81 const OsType& os = getOSInfo();
83 bool detected =
false;
84 if (var.hipPlatformEnv_.empty()) {
85 if (canRunCompiler(cmdNv.string(), out) || (canRunCompiler(
"nvcc", out))) {
89 if (var.hipPlatformEnv_ ==
"nvidia" || var.hipPlatformEnv_ ==
"nvcc") {
91 if (var.hipPlatformEnv_ ==
"nvcc")
92 std::cerr <<
"Warning: HIP_PLATFORM=nvcc is deprecated."
93 <<
"Please use HIP_PLATFORM=nvidia." << endl;
102string HipBinNvidia::getDeviceLibPath()
const {
103 cout <<
"TODO Not required for now" << endl;
108string HipBinNvidia::getHipCC()
const {
110 const string& cudaPath = getCompilerPath();
112 hipCCPath = cudaPath;
113 hipCCPath /=
"bin/nvcc";
114 hipCC = hipCCPath.string();
119string HipBinNvidia::getCompilerIncludePath() {
120 cout <<
"TODO Not required for now" << endl;
126void HipBinNvidia::checkHipconfig() {
127 cout << endl <<
"Check system installation: " << endl;
128 cout <<
"check hipconfig in PATH..." << endl;
129 if (system(
"which hipconfig > /dev/null 2>&1") != 0) {
130 std::cerr <<
"FAIL " << endl;
132 cout <<
"good" << endl;
137void HipBinNvidia::printFull() {
138 const string& hipVersion = getHipVersion();
139 const string& hipPath = getHipPath();
140 const string& roccmPath = getRoccmPath();
142 const string& ccpConfig = getCppConfig();
143 const string& cudaPath = getCompilerPath();
144 cout <<
"HIP version: " << hipVersion << endl;
145 cout << endl <<
"==hipconfig" << endl;
146 cout <<
"HIP_PATH :" << hipPath << endl;
147 cout <<
"ROCM_PATH :" << roccmPath << endl;
148 cout <<
"HIP_COMPILER :" << CompilerTypeStr(
149 platformInfo.compiler) << endl;
150 cout <<
"HIP_PLATFORM :" << PlatformTypeStr(
151 platformInfo.platform) << endl;
152 cout <<
"HIP_RUNTIME :" << RuntimeTypeStr(
153 platformInfo.runtime) << endl;
154 cout <<
"CPP_CONFIG :" << ccpConfig << endl;
155 cout << endl <<
"== nvcc" << endl;
156 cout <<
"CUDA_PATH :" << cudaPath <<endl;
158 cout << endl <<
"== Envirnoment Variables" << endl;
159 printEnvironmentVariables();
161 if (fs::exists(
"/usr/bin/lsb_release"))
162 system(
"/usr/bin/lsb_release -a");
166string HipBinNvidia::getHipInclude()
const {
167 string hipPath, hipInclude;
168 hipPath = getHipPath();
169 fs::path hipIncludefs = hipPath;
170 hipIncludefs /=
"include";
171 hipInclude = hipIncludefs.string();
176void HipBinNvidia::initializeHipLdFlags() {
178 const string& cudaPath = getCompilerPath();
179 hipLdFlags =
" -Wno-deprecated-gpu-targets -lcuda -lcudart -L" +
181 hipLdFlags_ = hipLdFlags;
186const string& HipBinNvidia::getHipCFlags()
const {
191const string& HipBinNvidia::getHipLdFlags()
const {
196void HipBinNvidia::initializeHipCFlags() {
198 const string& cudaPath = getCompilerPath();
199 hipCFlags +=
" -isystem " + cudaPath +
"/include";
200 string hipIncludePath;
201 hipIncludePath = getHipInclude();
202 hipCFlags +=
" -isystem \"" + hipIncludePath +
"\"";
203 hipCFlags_ = hipCFlags;
207const string& HipBinNvidia::getHipCXXFlags()
const {
212void HipBinNvidia::initializeHipCXXFlags() {
213 string hipCXXFlags =
" -Wno-deprecated-gpu-targets ";
214 const string& cudaPath = getCompilerPath();
215 hipCXXFlags +=
" -isystem " + cudaPath +
"/include";
216 string hipIncludePath;
217 hipIncludePath = getHipInclude();
218 hipCXXFlags +=
" -isystem \"" + hipIncludePath +
"\"";
219 hipCXXFlags_ = hipCXXFlags;
223string HipBinNvidia::getHipLibPath()
const {
226 hipLibPath = env.hipLibPathEnv_;
231void HipBinNvidia::constructCompilerPath() {
234 if (envVariables.cudaPathEnv_.empty()) {
236 cudaPathfs =
"/usr/local/cuda";
237 complierPath = cudaPathfs.string();
239 complierPath = envVariables.cudaPathEnv_;
241 cudaPath_ = complierPath;
246const string& HipBinNvidia::getCompilerPath()
const {
251void HipBinNvidia::printCompilerInfo()
const {
254 nvcc = getCompilerPath();
256 cmd = nvcc.string() +
" --version";
261string HipBinNvidia::getCompilerVersion() {
262 string complierVersion, cmd;
264 nvcc = getCompilerPath();
266 cmd = nvcc.string() +
" --version";
268 return complierVersion;
272const PlatformInfo& HipBinNvidia::getPlatformInfo()
const {
273 return platformInfoNV_;
277string HipBinNvidia::getCppConfig() {
279 " - D__HIP_PLATFORM_NVCC__ = -D__HIP_PLATFORM_NVIDIA__ = -I";
281 hipPath = getHipPath();
282 cppConfig += hipPath;
283 cppConfig +=
"/include -I";
284 cppConfig += cudaPath_;
285 cppConfig +=
"/include";
290void HipBinNvidia::executeHipCCCmd(vector<string> argv) {
291 if (argv.size() < 2) {
292 cout<<
"No Arguments passed, exiting ...\n";
297 if (!var.verboseEnv_.empty())
298 verbose = stoi(var.verboseEnv_);
303 bool default_amdgpu_target = 1;
304 bool compileOnly = 0;
305 bool needCXXFLAGS = 0;
307 bool needLDFLAGS = 1;
308 bool fileTypeFlag = 0;
309 bool hasOMPTargets = 0;
317 bool printHipVersion = 0;
318 bool printCXXFlags = 0;
319 bool printLDFlags = 0;
323 bool setLinkType = 0;
331 vector<string> options, inputs;
334 vector<string> targetOpts = {
"--offload-arch=",
"--amdgpu-target="};
336 bool skipOutputFile =
false;
337 const OsType& os = getOSInfo();
338 string hip_compile_cxx_as_hip;
339 if (var.hipCompileCxxAsHipEnv_.empty()) {
340 hip_compile_cxx_as_hip =
"1";
342 hip_compile_cxx_as_hip = var.hipCompileCxxAsHipEnv_;
344 string HIPLDARCHFLAGS;
345 initializeHipCXXFlags();
346 initializeHipCFlags();
347 initializeHipLdFlags();
348 string HIPCXXFLAGS, HIPCFLAGS, HIPLDFLAGS;
349 HIPCFLAGS = getHipCFlags();
350 HIPCXXFLAGS = getHipCXXFlags();
351 HIPLDFLAGS = getHipLdFlags();
353 hipPath = getHipPath();
355 const string& nvccPath = getCompilerPath();
356 const string& hipVersion = getHipVersion();
358 cout <<
"HIP_PATH=" << hipPath << endl;
359 cout <<
"HIP_PLATFORM=" << PlatformTypeStr(platformInfo.platform) <<endl;
360 cout <<
"HIP_COMPILER=" << CompilerTypeStr(platformInfo.compiler) <<endl;
361 cout <<
"HIP_RUNTIME=" << RuntimeTypeStr(platformInfo.runtime) <<endl;
362 cout <<
"CUDA_PATH=" << nvccPath <<endl;
365 cout <<
"hipcc-args: ";
366 for (
unsigned int i = 1; i< argv.size(); i++) {
367 cout << argv.at(i) <<
" ";
373 ISACMD += hipPath +
"/bin/hipcc -ptx ";
374 if (argv.at(1) ==
"--genco") {
375 for (
unsigned int i = 2; i < argv.size(); i++) {
376 string isaarg = argv.at(i);
381 cout<<
"hipcc-cmd: " << ISACMD <<
"\n";
383 system(ISACMD.c_str());
386 for (
unsigned int argcount = 1; argcount < argv.size(); argcount++) {
388 string arg = argv.at(argcount);
389 regex toRemove(
"\\s+");
394 string trimarg = hipBinUtilPtr_->replaceRegex(arg, toRemove,
"");
395 bool swallowArg =
false;
396 bool escapeArg =
true;
397 if (arg ==
"-c" || arg ==
"--genco" || arg ==
"-E") {
401 if (skipOutputFile) {
403 toolArgs +=
" \"" + arg +
"\"";
412 if ((trimarg ==
"-stdlib=libc++") && (setStdLib == 0)) {
413 HIPCXXFLAGS +=
" -stdlib=libc++";
417 for (
unsigned int i = 0; i <targetOpts.size(); i++) {
418 string targetOpt = targetOpts.at(i);
419 string pattern =
"^" + targetOpt +
".*";
420 if (hipBinUtilPtr_->stringRegexMatch(arg, pattern)) {
423 targetsStr.size() >0 ? targetsStr +=
",": targetsStr +=
"";
424 targetsStr += arg.substr(targetOpt.size());
425 default_amdgpu_target = 0;
428 if (trimarg ==
"--version") {
431 if (trimarg ==
"--short-version") {
435 if (trimarg ==
"--cxxflags") {
439 if (trimarg ==
"--ldflags") {
443 if (trimarg ==
"-M") {
447 if (trimarg ==
"-use_fast_math") {
448 HIPCXXFLAGS +=
" -DHIP_FAST_MATH ";
449 HIPCFLAGS +=
" -DHIP_FAST_MATH ";
451 if ((trimarg ==
"-use-staticlib") && (setLinkType == 0)) {
456 if ((trimarg ==
"-use-sharedlib") && (setLinkType == 0)) {
460 if (hipBinUtilPtr_->stringRegexMatch(arg,
"^-O.*")) {
463 if (hipBinUtilPtr_->substringPresent(
464 arg,
"--amdhsa-code-object-version=")) {
465 arg = hipBinUtilPtr_->replaceStr(
466 arg,
"--amdhsa-code-object-version=",
"");
473 if (arg ==
"-fPIC" || hipBinUtilPtr_->substringPresent(arg,
"-Wl,")) {
474 HIPCXXFLAGS +=
" -Xcompiler "+ arg;
479 }
else if ((arg ==
"c" && prevArg ==
"-x") || (arg ==
"-xc")) {
484 }
else if ((arg ==
"c++" && prevArg ==
"-x") || (arg ==
"-xc++")) {
489 }
else if ((arg ==
"hip" && prevArg ==
"-x") || (arg ==
"-xhip")) {
494 }
else if (hipBinUtilPtr_->substringPresent(arg,
"-fopenmp-targets=")) {
496 }
else if (hipBinUtilPtr_->stringRegexMatch(arg,
"^-.*")) {
497 if (arg ==
"-fgpu-rdc") {
499 }
else if (arg ==
"-fno-gpu-rdc") {
502 if (hipBinUtilPtr_->stringRegexMatch(arg,
"^--hipcc.*")) {
504 if (arg ==
"--hipcc-func-supp") {
506 }
else if (arg ==
"--hipcc-no-func-supp") {
510 options.push_back(arg);
512 }
else if (prevArg !=
"-o") {
513 if (fileTypeFlag == 0) {
514 if (hipBinUtilPtr_->stringRegexMatch(arg,
".*\\.c$")) {
518 }
else if ((hipBinUtilPtr_->stringRegexMatch(arg,
".*\\.cpp$")) ||
519 (hipBinUtilPtr_->stringRegexMatch(arg,
".*\\.cxx$")) ||
520 (hipBinUtilPtr_->stringRegexMatch(arg,
".*\\.cc$")) ||
521 (hipBinUtilPtr_->stringRegexMatch(arg,
".*\\.C$"))) {
524 }
else if (((hipBinUtilPtr_->stringRegexMatch(arg,
".*\\.cu$") ||
525 hipBinUtilPtr_->stringRegexMatch(arg,
".*\\.cuh$")) &&
526 hip_compile_cxx_as_hip !=
"0") ||
527 (hipBinUtilPtr_->stringRegexMatch(arg,
".*\\.hip$"))) {
534 }
else if (hasCXX || hasHIP) {
537 inputs.push_back(arg);
540 if (os != windows && escapeArg) {
541 regex reg(
"[^-a-zA-Z0-9_=+,.\\/]");
542 arg = regex_replace(arg, reg,
"\\$&");
545 toolArgs +=
" " + arg;
549 HIPCXXFLAGS +=
" -x cu";
552 HIPCXXFLAGS +=
" -M -D__CUDACC__";
553 HIPCFLAGS +=
" -M -D__CUDACC__";
555 if (!var.hipccCompileFlagsAppendEnv_.empty()) {
556 HIPCXXFLAGS +=
"\" " + var.hipccCompileFlagsAppendEnv_ +
"\"";
557 HIPCFLAGS +=
"\" " + var.hipccCompileFlagsAppendEnv_ +
"\"";
559 if (!var.hipccLinkFlagsAppendEnv_.empty()) {
560 HIPLDFLAGS +=
"\" " + var.hipccLinkFlagsAppendEnv_ +
"\"";
563 compiler = getHipCC();
564 string CMD = compiler;
566 CMD +=
" " + HIPCFLAGS;
569 CMD +=
" " + HIPCXXFLAGS;
571 if (needLDFLAGS && !compileOnly) {
572 CMD +=
" " + HIPLDFLAGS;
574 CMD +=
" " + toolArgs;
576 cout <<
"hipcc-cmd: " << CMD <<
"\n";
578 if (printHipVersion) {
580 cout <<
"HIP version: ";
582 cout << hipVersion << endl;
592 sysOut = hipBinUtilPtr_->exec(CMD.c_str(),
true);
593 string cmdOut = sysOut.out;
594 int CMD_EXIT_CODE = sysOut.exitCode;
595 if (CMD_EXIT_CODE !=0) {
596 cout <<
"failed to execute:" << CMD << std::endl;
Definition hipBin_base.h:207
Definition hipBin_nvidia.h:32
Definition hipBin_util.h:144
Definition hipBin_base.h:141
Definition hipBin_util.h:138