Skip to content

Commit 674c14e

Browse files
authored
[SYCLomatic] Add “-I SYCL-include” in generated Makefile for source files which use SYCL header file and build by non-sycl compiler (#811)
Signed-off-by: chenwei.sun <[email protected]>
1 parent d847c7b commit 674c14e

File tree

3 files changed

+58
-37
lines changed

3 files changed

+58
-37
lines changed

clang/lib/DPCT/GenMakefile.cpp

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -41,12 +41,7 @@ std::vector<std::pair<std::string /*original file name or linker entry*/,
4141
static void fillCompileCmds(
4242
std::map<std::string, std::vector<clang::tooling::CompilationInfo>>
4343
&CompileCmds,
44-
std::string MigratedFileName, std::string CompileOptions,
45-
std::string Compiler, std::string TargetName) {
46-
clang::tooling::CompilationInfo CmpInfo;
47-
CmpInfo.MigratedFileName = MigratedFileName;
48-
CmpInfo.CompileOptions = CompileOptions;
49-
CmpInfo.Compiler = Compiler;
44+
clang::tooling::CompilationInfo &CmpInfo, std::string TargetName) {
5045
CompileCmds[TargetName].push_back(CmpInfo);
5146
}
5247

@@ -102,7 +97,7 @@ static void getCompileInfo(
10297
// Set the target name
10398
TargetName = Obj;
10499
IsTargetName = false;
105-
Tool = "$(CC) -o"; // use 'icpx -fsycl' to link the target file in the
100+
Tool = "$(CC) -fsycl -o"; // use 'icpx -fsycl' to link the target file in the
106101
// generated Makefile.
107102
} else if (llvm::StringRef(Obj).endswith(".o")) {
108103
llvm::SmallString<512> FilePathAbs(Obj);
@@ -163,6 +158,18 @@ static void getCompileInfo(
163158
bool IsIncludeWithWhitespace = false;
164159

165160
const std::string Directory = Entry.second[0];
161+
162+
llvm::SmallString<512> RealPath;
163+
llvm::sys::fs::real_path(FileName, RealPath, true);
164+
if (!llvm::sys::path::is_absolute(FileName))
165+
RealPath = Directory + "/" + FileName;
166+
makeCanonical(RealPath);
167+
bool HasCudaSemantics = false;
168+
if (IncludeFileMap.count(std::string(RealPath.str())) &&
169+
IncludeFileMap.at(std::string(RealPath.str()))) {
170+
HasCudaSemantics = true;
171+
}
172+
166173
std::unordered_set<std::string> DuplicateDuplicateFilter;
167174
for (const auto &Option : Entry.second) {
168175

@@ -250,19 +257,6 @@ static void getCompileInfo(
250257
auto Version = Option.substr(Idx, Option.length() - Idx);
251258
int Val = std::atoi(Version.c_str());
252259

253-
llvm::SmallString<512> RealPath;
254-
llvm::sys::fs::real_path(FileName, RealPath, true);
255-
if (!llvm::sys::path::is_absolute(FileName))
256-
RealPath = Directory + "/" + FileName;
257-
makeCanonical(RealPath);
258-
259-
bool HasCudaSemantics = false;
260-
261-
if (IncludeFileMap.count(std::string(RealPath.str())) &&
262-
IncludeFileMap.at(std::string(RealPath.str()))) {
263-
HasCudaSemantics = true;
264-
}
265-
266260
// SYCL support c++17 as default.
267261
if (HasCudaSemantics && Val <= 17) {
268262
NewOptions += "-std=c++17 ";
@@ -308,7 +302,16 @@ static void getCompileInfo(
308302
// header files for migrated code, the path of the helper header files
309303
// should be included.
310304
if (llvm::sys::fs::exists(OutRoot.str() + "/include")) {
311-
NewOptions += " -I ./include ";
305+
NewOptions += "-I ./include ";
306+
}
307+
308+
// Add SYCL head file path to the including path in the generated Makefile
309+
// for source files which originally has CUDA Semantics and compiled by
310+
// non-nvcc compiler
311+
if (HasCudaSemantics &&
312+
!llvm::StringRef(Entry.second[1]).endswith("nvcc")) {
313+
NewOptions += "-I $(INCLUDE_SYCL) ";
314+
NewOptions += "-I $(INCLUDE_CL) ";
312315
}
313316

314317
SmallString<512> OutDirectory = llvm::StringRef(FileName);
@@ -340,17 +343,15 @@ static void getCompileInfo(
340343
auto Iter = CmdsMap.find(Obj);
341344
if (Iter != CmdsMap.end()) {
342345
auto CmpInfo = Iter->second;
343-
fillCompileCmds(CompileCmds, CmpInfo.MigratedFileName,
344-
CmpInfo.CompileOptions, CmpInfo.Compiler, Entry.first);
346+
fillCompileCmds(CompileCmds, CmpInfo, Entry.first);
345347
}
346348
}
347349
}
348350

349351
if (ObjsInLinkerCmdPerTarget.empty()) {
350352
for (const auto &Cmd : CmdsMap) {
351-
fillCompileCmds(CompileCmds, Cmd.second.MigratedFileName,
352-
Cmd.second.CompileOptions, Cmd.second.Compiler,
353-
EmptyTarget);
353+
auto CmpInfo = Cmd.second;
354+
fillCompileCmds(CompileCmds, CmpInfo, EmptyTarget);
354355
}
355356
}
356357
}
@@ -365,14 +366,22 @@ genMakefile(clang::tooling::RefactoringTool &Tool, StringRef OutRoot,
365366
llvm::raw_string_ostream OS(Buf);
366367
std::string TargetName;
367368

368-
OS << "CC := icpx -fsycl\n\n";
369+
OS << "CC := icpx\n\n";
369370
OS << "LD := $(CC)\n\n";
370371
OS << buildString(
371372
"#", DiagnosticsUtils::getMsgText(MakefileMsgs::GEN_MAKEFILE_LIB), "\n");
372373
OS << "LIB := \n\n";
373374

374375
OS << buildString("FLAGS := \n\n");
375376

377+
OS << buildString("ifeq ($(shell which $(CC)),)\n");
378+
OS << buildString(" $(error ERROR - $(CC) compiler not found)\n");
379+
OS << buildString("endif\n\n");
380+
381+
OS << buildString("ROOT_DIR := $(shell dirname $(shell which $(CC)))\n");
382+
OS << buildString("INCLUDE_SYCL := $(ROOT_DIR)/../include\n");
383+
OS << buildString("INCLUDE_CL := $(ROOT_DIR)/../include/sycl\n\n");
384+
376385
std::map<std::string, std::string> ObjsPerTarget;
377386

378387
int TargetIdx = 0;
@@ -473,7 +482,7 @@ genMakefile(clang::tooling::RefactoringTool &Tool, StringRef OutRoot,
473482
// Use 'icpx -fsycl' to compile the migrated SYCL file.
474483
std::string Compiler =
475484
llvm::StringRef((Entry.second)[Idx].Compiler).endswith("nvcc")
476-
? "$(CC)"
485+
? "$(CC) -fsycl"
477486
: (Entry.second)[Idx].Compiler;
478487

479488
OS << buildString("\t", Compiler, " -c ${", SrcStrName, "} -o ${",
@@ -507,7 +516,7 @@ genMakefile(clang::tooling::RefactoringTool &Tool, StringRef OutRoot,
507516

508517
std::string Compiler =
509518
llvm::StringRef((Entry.second)[Idx].Compiler).endswith("nvcc")
510-
? "$(CC)"
519+
? "$(CC) -fsycl"
511520
: (Entry.second)[Idx].Compiler;
512521

513522
OS << buildString("\t", Compiler, " -c ${", SrcStrName, "} -o ${",

clang/test/dpct/ar_target/Makefile.dpct.ref

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
1-
// CHECK:CC := icpx -fsycl
1+
// CHECK:CC := icpx
22
// CHECK:LD := $(CC)
33
// CHECK:#DPCT2001:0: You can link with more library by add them here.
44
// CHECK:LIB :=
55
// CHECK:FLAGS :=
6+
// CHECK:ifeq ($(shell which $(CC)),)
7+
// CHECK: $(error ERROR - $(CC) compiler not found)
8+
// CHECK:endif
9+
// CHECK:ROOT_DIR := $(shell dirname $(shell which $(CC)))
10+
// CHECK:INCLUDE_SYCL := $(ROOT_DIR)/../include
11+
// CHECK:INCLUDE_CL := $(ROOT_DIR)/../include/sycl
612
// CHECK:TARGET_0_SRC_0 = ./one.dp.cpp
713
// CHECK:TARGET_0_OBJ_0 = ./one.dp.o
814
// CHECK:TARGET_0_FLAG_0 = ${FLAGS}
@@ -26,15 +32,15 @@
2632
// CHECK:OBJS_1 := ${TARGET_1_OBJ_0} ${TARGET_1_OBJ_1} ${TARGET_1_OBJ_2}
2733
// CHECK:all: $(TARGET)
2834
// CHECK:$(TARGET_0): $(OBJS_0)
29-
// CHECK: $(CC) -o $@ $^ $(LIB)
35+
// CHECK: $(CC) -fsycl -o $@ $^ $(LIB)
3036
// CHECK:$(TARGET_0_OBJ_0):$(TARGET_0_SRC_0)
31-
// CHECK: $(CC) -c ${TARGET_0_SRC_0} -o ${TARGET_0_OBJ_0} $(TARGET_0_FLAG_0)
37+
// CHECK: $(CC) -fsycl -c ${TARGET_0_SRC_0} -o ${TARGET_0_OBJ_0} $(TARGET_0_FLAG_0)
3238
// CHECK:$(TARGET_0_OBJ_1):$(TARGET_0_SRC_1)
3339
// CHECK: c++ -c ${TARGET_0_SRC_1} -o ${TARGET_0_OBJ_1} $(TARGET_0_FLAG_1)
3440
// CHECK:$(TARGET_1): $(OBJS_1)
3541
// CHECK: ar -r $@ $^ $(LIB)
3642
// CHECK:$(TARGET_1_OBJ_0):$(TARGET_1_SRC_0)
37-
// CHECK: $(CC) -c ${TARGET_1_SRC_0} -o ${TARGET_1_OBJ_0} $(TARGET_1_FLAG_0)
43+
// CHECK: $(CC) -fsycl -c ${TARGET_1_SRC_0} -o ${TARGET_1_OBJ_0} $(TARGET_1_FLAG_0)
3844
// CHECK:$(TARGET_1_OBJ_1):$(TARGET_1_SRC_1)
3945
// CHECK: c++ -c ${TARGET_1_SRC_1} -o ${TARGET_1_OBJ_1} $(TARGET_1_FLAG_1)
4046
// CHECK:$(TARGET_1_OBJ_2):$(TARGET_1_SRC_2)

clang/test/dpct/gen_build_script/Makefile.dpct.ref

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,29 @@
1-
// CHECK:CC := icpx -fsycl
1+
// CHECK:CC := icpx
22
// CHECK:LD := $(CC)
33
// CHECK:#DPCT2001:0: You can link with more library by add them here.
44
// CHECK:LIB :=
55
// CHECK:FLAGS :=
6+
// CHECK:ifeq ($(shell which $(CC)),)
7+
// CHECK: $(error ERROR - $(CC) compiler not found)
8+
// CHECK:endif
9+
// CHECK:ROOT_DIR := $(shell dirname $(shell which $(CC)))
10+
// CHECK:INCLUDE_SYCL := $(ROOT_DIR)/../include
11+
// CHECK:INCLUDE_CL := $(ROOT_DIR)/../include/sycl
612
// CHECK:TARGET_0_SRC_0 = ./source/foo.dp.cpp
713
// CHECK:TARGET_0_OBJ_0 = ./source/foo.dp.o
814
// CHECK:TARGET_0_FLAG_0 = -std=c++20 ${FLAGS}
915
// CHECK:TARGET_0_SRC_1 = ./source/bar.cpp.dp.cpp
1016
// CHECK:TARGET_0_OBJ_1 = ./source/bar.cpp.dp.o
11-
// CHECK:TARGET_0_FLAG_1 = -std=c++17 ${FLAGS}
17+
// CHECK:TARGET_0_FLAG_1 = -std=c++17 -I $(INCLUDE_SYCL) -I $(INCLUDE_CL) ${FLAGS}
1218
// CHECK:TARGET_0 := app
1319
// CHECK:TARGET := ${TARGET_0}
1420
// CHECK:.PHONY:all clean
1521
// CHECK:OBJS_0 := ${TARGET_0_OBJ_0} ${TARGET_0_OBJ_1}
1622
// CHECK:all: $(TARGET)
1723
// CHECK:$(TARGET_0): $(OBJS_0)
18-
// CHECK: $(CC) -o $@ $^ $(LIB)
24+
// CHECK: $(CC) -fsycl -o $@ $^ $(LIB)
1925
// CHECK:$(TARGET_0_OBJ_0):$(TARGET_0_SRC_0)
20-
// CHECK: $(CC) -c ${TARGET_0_SRC_0} -o ${TARGET_0_OBJ_0} $(TARGET_0_FLAG_0)
26+
// CHECK: $(CC) -fsycl -c ${TARGET_0_SRC_0} -o ${TARGET_0_OBJ_0} $(TARGET_0_FLAG_0)
2127
// CHECK:$(TARGET_0_OBJ_1):$(TARGET_0_SRC_1)
2228
// CHECK: g++ -c ${TARGET_0_SRC_1} -o ${TARGET_0_OBJ_1} $(TARGET_0_FLAG_1)
2329
// CHECK:clean:

0 commit comments

Comments
 (0)