Skip to content

Commit a619d05

Browse files
authored
[SYCLomatic][intercept-build] Refine libear's implementation to fix fail cases. (#2730)
The related cases are as follows: 1. dpct/cmp-cmds-linker-entry-src-files/one.cu 2. dpct/dpct-intercept-build/main.cpp 3. dpct/gen_build_script2/hello.cu 4. dpct/gen_build_script3/hello.cu Signed-off-by: chenwei.sun <[email protected]>
1 parent 0e3bc4e commit a619d05

File tree

1 file changed

+34
-72
lines changed
  • clang/tools/scan-build-py/lib/libear

1 file changed

+34
-72
lines changed

clang/tools/scan-build-py/lib/libear/ear.c

Lines changed: 34 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -487,28 +487,40 @@ static int call_eaccess(const char *pathname, int mode) {
487487
return result;
488488
}
489489

490-
int eaccess(const char *pathname, int mode) {
491-
int ret = call_eaccess(pathname, mode);
492-
if (ret == 0) {
493-
return 0;
494-
}
490+
int is_nvcc_available(void) {
491+
char *compiler = getenv("INTERCEPT_COMPILE_PATH");
492+
char *is_compiler_exported =
493+
getenv("IS_INTERCEPT_COMPILE_PATH_FROM_ENV_PATH");
495494

496-
int nvcc_available = 0;
497-
char *value = getenv("INTERCEPT_COMPILE_PATH");
498-
if (value) {
499-
nvcc_available = 1;
495+
// Consider tool chain is avaialbe only when it is available from env path.
496+
if (is_compiler_exported && *is_compiler_exported == '1' && compiler) {
497+
return 1;
500498
}
499+
return 0;
500+
}
501501

502+
int is_nvcc_cmd(const char *pathname) {
502503
int len = strlen(pathname);
503-
if (!nvcc_available && len == 4 && pathname[3] == 'c' && pathname[2] == 'c' &&
504+
if (len == 4 && pathname[3] == 'c' && pathname[2] == 'c' &&
504505
pathname[1] == 'v' && pathname[0] == 'n') {
505506
// To handle case like "nvcc foo.cu ..."
506-
return 0;
507+
return 1;
507508
}
508-
if (!nvcc_available && len > 4 && pathname[len - 1] == 'c' &&
509-
pathname[len - 2] == 'c' && pathname[len - 3] == 'v' &&
510-
pathname[len - 4] == 'n' && pathname[len - 5] == '/') {
509+
if (len > 4 && pathname[len - 1] == 'c' && pathname[len - 2] == 'c' &&
510+
pathname[len - 3] == 'v' && pathname[len - 4] == 'n' &&
511+
pathname[len - 5] == '/') {
511512
// To handle case like "/path/to/nvcc foo.cu ..."
513+
return 1;
514+
}
515+
return 0;
516+
}
517+
518+
int eaccess(const char *pathname, int mode) {
519+
int ret = call_eaccess(pathname, mode);
520+
if (ret == 0) {
521+
return 0;
522+
}
523+
if (!is_nvcc_available() && is_nvcc_cmd(pathname)) {
512524
return 0;
513525
}
514526
return ret;
@@ -538,35 +550,8 @@ int stat(const char *pathname, struct stat *statbuf) {
538550
if (ret == 0) {
539551
return 0;
540552
}
541-
int len = strlen(pathname);
542-
if (len == 4 && pathname[3] == 'c' && pathname[2] == 'c' &&
543-
pathname[1] == 'v' && pathname[0] == 'n') {
544-
// To handle case like "nvcc foo.cu ..."
545-
546-
const char *nvcc_path = getenv("INTERCEPT_COMPILE_PATH");
547-
if (nvcc_path) {
548-
call_stat(nvcc_path, statbuf);
549-
return 0;
550-
}
551-
552-
pathname = get_intercept_stub_path();
553-
call_stat(pathname, statbuf);
554-
return 0;
555-
}
556553

557-
if (len > 4 && pathname[len - 1] == 'c' && pathname[len - 2] == 'c' &&
558-
pathname[len - 3] == 'v' && pathname[len - 4] == 'n' &&
559-
pathname[len - 5] == '/') {
560-
// To handle case like "/path/to/nvcc foo.cu ..."
561-
562-
const char *nvcc_path = getenv("INTERCEPT_COMPILE_PATH");
563-
if (nvcc_path) {
564-
call_stat(nvcc_path, statbuf);
565-
return 0;
566-
}
567-
568-
pathname = get_intercept_stub_path();
569-
call_stat(pathname, statbuf);
554+
if (!is_nvcc_available() && is_nvcc_cmd(pathname)) {
570555
return 0;
571556
}
572557
return ret;
@@ -1724,33 +1709,10 @@ char *replace_binary_name(const char *src, const char *pos, int compiler_idx,
17241709
int is_tool_available(char const *argv[], size_t const argc) {
17251710
const char *pathname = argv[0];
17261711
int len = strlen(pathname);
1727-
int is_nvcc = 0;
1728-
int is_nvcc_available = 0;
1729-
1730-
char *value = getenv("INTERCEPT_COMPILE_PATH");
1731-
if (value) {
1732-
is_nvcc_available = 1;
1733-
}
17341712

1735-
if (len == 4 && pathname[3] == 'c' && pathname[2] == 'c' &&
1736-
pathname[1] == 'v' && pathname[0] == 'n') {
1737-
// To handle case like "nvcc"
1738-
is_nvcc = 1;
1739-
value = getenv("IS_INTERCEPT_COMPILE_PATH_FROM_ENV_PATH");
1740-
if (value && *value == '0') {
1741-
return 0;
1742-
}
1743-
}
1744-
if (len > 4 && pathname[len - 1] == 'c' && pathname[len - 2] == 'c' &&
1745-
pathname[len - 3] == 'v' && pathname[len - 4] == 'n' &&
1746-
pathname[len - 5] == '/') {
1747-
// To handle case like "/path/to/nvcc"
1748-
is_nvcc = 1;
1749-
}
1750-
if (is_nvcc) {
1751-
if (is_nvcc_available) {
1713+
if (is_nvcc_cmd(pathname)) {
1714+
if (is_nvcc_available())
17521715
return 1;
1753-
}
17541716
return 0;
17551717
}
17561718

@@ -1765,7 +1727,7 @@ int is_tool_available(char const *argv[], size_t const argc) {
17651727
is_ld = 1;
17661728
}
17671729
if (is_ld) {
1768-
if (!is_nvcc_available) {
1730+
if (!is_nvcc_available()) {
17691731
for (size_t idx = 0; idx < argc; idx++) {
17701732
// if ld linker command uses cuda libarary like libcuda.so or
17711733
// libcudart.so, then the ld command should be intercepted.
@@ -1776,12 +1738,12 @@ int is_tool_available(char const *argv[], size_t const argc) {
17761738
}
17771739
}
17781740

1779-
if (!is_nvcc_available && argc == 3) {
1741+
if (!is_nvcc_available() && argc == 3) {
17801742
// To handle case like "/bin/[sh/bash] -c '[echo or something]
17811743
// [/path/to/]nvcc -c foo.cu -o foo.o'" on the environment where tool chain
17821744
// is not available.
17831745
int is_bash = 0;
1784-
is_nvcc = 0;
1746+
int is_nvcc_cmd = 0;
17851747
const char *pathname = argv[0];
17861748
len = strlen(pathname);
17871749

@@ -1805,7 +1767,7 @@ int is_tool_available(char const *argv[], size_t const argc) {
18051767
}
18061768

18071769
if (pos) {
1808-
is_nvcc =
1770+
is_nvcc_cmd =
18091771
pos > argv[2]
18101772
? strlen(pos) >= 4 && isspace(*(pos + 4)) &&
18111773
(*(pos - 1) == '/' || *(pos - 1) == ';' ||
@@ -1816,7 +1778,7 @@ int is_tool_available(char const *argv[], size_t const argc) {
18161778
// sure it is a compiler command.
18171779
}
18181780

1819-
if (is_bash && strcmp(argv[1], "-c") == 0 && is_nvcc) {
1781+
if (is_bash && strcmp(argv[1], "-c") == 0 && is_nvcc_cmd) {
18201782
return 0;
18211783
}
18221784
}

0 commit comments

Comments
 (0)