From 469f6690ea7d8450723db15f269493792425c3c3 Mon Sep 17 00:00:00 2001 From: "Jiang, Zhiwei" Date: Tue, 10 Sep 2024 13:30:37 +0800 Subject: [PATCH 1/2] [SYCLomatic] If need insert #include , add it at the beginning of a translation unit Signed-off-by: Jiang, Zhiwei --- clang/lib/DPCT/AnalysisInfo.cpp | 10 +++++----- clang/lib/DPCT/MigrationAction.cpp | 1 + clang/test/dpct/d_dh_constant_db.cpp | 1 + clang/test/dpct/d_dh_constant_db_win.cpp | 1 + clang/test/dpct/d_hd_constant_db.cpp | 1 + clang/test/dpct/d_hd_constant_db_win.cpp | 1 + clang/test/dpct/dh_d_constant_db.cpp | 1 + clang/test/dpct/dh_d_constant_db_win.cpp | 1 + clang/test/dpct/dh_h_constant_db.cpp | 1 + clang/test/dpct/dh_h_constant_db_win.cpp | 1 + clang/test/dpct/h_dh_constant_db.cpp | 1 + clang/test/dpct/h_dh_constant_db_win.cpp | 1 + clang/test/dpct/h_hd_constant_db.cpp | 1 + clang/test/dpct/h_hd_constant_db_win.cpp | 1 + clang/test/dpct/hd_d_constant_db.cpp | 1 + clang/test/dpct/hd_d_constant_db_win.cpp | 1 + clang/test/dpct/hd_h_constant_db.cpp | 1 + clang/test/dpct/hd_h_constant_db_win.cpp | 1 + clang/test/dpct/header_insert/a.cpp | 11 +++++++++++ clang/test/dpct/header_insert/h1.h | 5 +++++ clang/test/dpct/header_insert/h2.h | 2 ++ clang/test/dpct/header_insert/h3.h | 4 ++++ clang/test/dpct/header_order/dnn.cu | 1 - clang/test/dpct/header_order/test.cu | 1 - clang/test/dpct/uncanonical_path/test.cpp | 8 +++++--- 25 files changed, 49 insertions(+), 10 deletions(-) create mode 100644 clang/test/dpct/header_insert/a.cpp create mode 100644 clang/test/dpct/header_insert/h1.h create mode 100644 clang/test/dpct/header_insert/h2.h create mode 100644 clang/test/dpct/header_insert/h3.h diff --git a/clang/lib/DPCT/AnalysisInfo.cpp b/clang/lib/DPCT/AnalysisInfo.cpp index f70284a4bff7..0cb578aae6fa 100644 --- a/clang/lib/DPCT/AnalysisInfo.cpp +++ b/clang/lib/DPCT/AnalysisInfo.cpp @@ -842,11 +842,11 @@ StringRef DpctFileInfo::getHeaderSpelling(HeaderType Value) { } void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset, ReplacementType IsForCodePin) { - if (Type == HT_DPL_Algorithm || Type == HT_DPL_Execution || - Type == HT_DPCT_DNNL_Utils) { - if (this != DpctGlobalInfo::getInstance().getMainFile().get()) - DpctGlobalInfo::getInstance().getMainFile()->insertHeader( - Type, FirstIncludeOffset); + if (Type == HT_DPL_Algorithm || Type == HT_DPL_Execution || Type == HT_SYCL) { + if (auto MF = DpctGlobalInfo::getInstance().getMainFile()) + if (this != MF.get()) + DpctGlobalInfo::getInstance().getMainFile()->insertHeader( + Type, FirstIncludeOffset); } if (HeaderInsertedBitMap[Type]) return; diff --git a/clang/lib/DPCT/MigrationAction.cpp b/clang/lib/DPCT/MigrationAction.cpp index 90ac12e02de4..a4bf17168ef0 100644 --- a/clang/lib/DPCT/MigrationAction.cpp +++ b/clang/lib/DPCT/MigrationAction.cpp @@ -269,6 +269,7 @@ void DpctToolAction::runPasses() { for (auto Pass : Passes) { runPass(Pass); } + DpctGlobalInfo::getInstance().setMainFile(nullptr); runWithCrashGuard( [&]() { Global.buildReplacements(); diff --git a/clang/test/dpct/d_dh_constant_db.cpp b/clang/test/dpct/d_dh_constant_db.cpp index 647de13fa3f7..b4dc82903b86 100644 --- a/clang/test/dpct/d_dh_constant_db.cpp +++ b/clang/test/dpct/d_dh_constant_db.cpp @@ -17,6 +17,7 @@ // RUN: echo " \"file\": \"%/T/d_dh_constant_db/d_dh_constant_db.cpp\"" >> compile_commands.json // RUN: echo " }" >> compile_commands.json // RUN: echo "]" >> compile_commands.json +// RUN: rm %T/d_dh_constant_db/out/MainSourceFiles.yaml // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/d_dh_constant_db/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/d_dh_constant_db_win.cpp b/clang/test/dpct/d_dh_constant_db_win.cpp index 486977b777c3..a2e3a5978aa0 100644 --- a/clang/test/dpct/d_dh_constant_db_win.cpp +++ b/clang/test/dpct/d_dh_constant_db_win.cpp @@ -17,6 +17,7 @@ // RUN: echo " \"file\": \"%/T/d_dh_constant_db_win/d_dh_constant_db_win.cpp\"" >> compile_commands.json // RUN: echo " }" >> compile_commands.json // RUN: echo "]" >> compile_commands.json +// RUN: rm %T/d_dh_constant_db_win/out/MainSourceFiles.yaml // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/d_dh_constant_db_win/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/d_hd_constant_db.cpp b/clang/test/dpct/d_hd_constant_db.cpp index 1f64901cb520..aacc5117c8d0 100644 --- a/clang/test/dpct/d_hd_constant_db.cpp +++ b/clang/test/dpct/d_hd_constant_db.cpp @@ -17,6 +17,7 @@ // RUN: echo " \"file\": \"%/T/d_hd_constant_db/d_hd_constant_db.cpp\"" >> compile_commands.json // RUN: echo " }" >> compile_commands.json // RUN: echo "]" >> compile_commands.json +// RUN: rm %T/d_hd_constant_db/out/MainSourceFiles.yaml // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/d_hd_constant_db/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/d_hd_constant_db_win.cpp b/clang/test/dpct/d_hd_constant_db_win.cpp index 7919fc465261..be2a1b8be8be 100644 --- a/clang/test/dpct/d_hd_constant_db_win.cpp +++ b/clang/test/dpct/d_hd_constant_db_win.cpp @@ -17,6 +17,7 @@ // RUN: echo " \"file\": \"%/T/d_hd_constant_db_win/d_hd_constant_db_win.cpp\"" >> compile_commands.json // RUN: echo " }" >> compile_commands.json // RUN: echo "]" >> compile_commands.json +// RUN: rm %T/d_hd_constant_db_win/out/MainSourceFiles.yaml // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/d_hd_constant_db_win/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/dh_d_constant_db.cpp b/clang/test/dpct/dh_d_constant_db.cpp index b8e9bde433ff..935035a019e4 100644 --- a/clang/test/dpct/dh_d_constant_db.cpp +++ b/clang/test/dpct/dh_d_constant_db.cpp @@ -18,6 +18,7 @@ // RUN: echo "]" >> compile_commands.json // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: rm compile_commands.json +// RUN: rm %T/dh_d_constant_db/out/MainSourceFiles.yaml // RUN: dpct dh_d_constant_db.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only // RUN: FileCheck %s --match-full-lines --input-file %T/dh_d_constant_db/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/dh_d_constant_db_win.cpp b/clang/test/dpct/dh_d_constant_db_win.cpp index e4f68f10746d..3a3f94dc2b8e 100644 --- a/clang/test/dpct/dh_d_constant_db_win.cpp +++ b/clang/test/dpct/dh_d_constant_db_win.cpp @@ -18,6 +18,7 @@ // RUN: echo "]" >> compile_commands.json // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: rm compile_commands.json +// RUN: rm %T/dh_d_constant_db_win/out/MainSourceFiles.yaml // RUN: dpct dh_d_constant_db_win.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only // RUN: FileCheck %s --match-full-lines --input-file %T/dh_d_constant_db_win/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/dh_h_constant_db.cpp b/clang/test/dpct/dh_h_constant_db.cpp index 73f35a1a7d2d..3ba726ad57ce 100644 --- a/clang/test/dpct/dh_h_constant_db.cpp +++ b/clang/test/dpct/dh_h_constant_db.cpp @@ -18,6 +18,7 @@ // RUN: echo "]" >> compile_commands.json // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: rm compile_commands.json +// RUN: rm %T/dh_h_constant_db/out/MainSourceFiles.yaml // RUN: dpct dh_h_constant_db.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x c --cuda-host-only // RUN: FileCheck %s --match-full-lines --input-file %T/dh_h_constant_db/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/dh_h_constant_db_win.cpp b/clang/test/dpct/dh_h_constant_db_win.cpp index 640df41e6d1c..318373d01362 100644 --- a/clang/test/dpct/dh_h_constant_db_win.cpp +++ b/clang/test/dpct/dh_h_constant_db_win.cpp @@ -18,6 +18,7 @@ // RUN: echo "]" >> compile_commands.json // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: rm compile_commands.json +// RUN: rm %T/dh_h_constant_db_win/out/MainSourceFiles.yaml // RUN: dpct dh_h_constant_db_win.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x c --cuda-host-only // RUN: FileCheck %s --match-full-lines --input-file %T/dh_h_constant_db_win/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/h_dh_constant_db.cpp b/clang/test/dpct/h_dh_constant_db.cpp index 2130f1d84a8a..b486c8f12423 100644 --- a/clang/test/dpct/h_dh_constant_db.cpp +++ b/clang/test/dpct/h_dh_constant_db.cpp @@ -17,6 +17,7 @@ // RUN: echo " \"file\": \"%/T/h_dh_constant_db/h_dh_constant_db.cpp\"" >> compile_commands.json // RUN: echo " }" >> compile_commands.json // RUN: echo "]" >> compile_commands.json +// RUN: rm %T/h_dh_constant_db/out/MainSourceFiles.yaml // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/h_dh_constant_db/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/h_dh_constant_db_win.cpp b/clang/test/dpct/h_dh_constant_db_win.cpp index 748e42ea7402..52a350b7a696 100644 --- a/clang/test/dpct/h_dh_constant_db_win.cpp +++ b/clang/test/dpct/h_dh_constant_db_win.cpp @@ -17,6 +17,7 @@ // RUN: echo " \"file\": \"%/T/h_dh_constant_db_win/h_dh_constant_db_win.cpp\"" >> compile_commands.json // RUN: echo " }" >> compile_commands.json // RUN: echo "]" >> compile_commands.json +// RUN: rm %T/h_dh_constant_db_win/out/MainSourceFiles.yaml // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/h_dh_constant_db_win/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/h_hd_constant_db.cpp b/clang/test/dpct/h_hd_constant_db.cpp index b8c6e6595adb..23085564fdd3 100644 --- a/clang/test/dpct/h_hd_constant_db.cpp +++ b/clang/test/dpct/h_hd_constant_db.cpp @@ -17,6 +17,7 @@ // RUN: echo " \"file\": \"%/T/h_hd_constant_db/h_hd_constant_db.cpp\"" >> compile_commands.json // RUN: echo " }" >> compile_commands.json // RUN: echo "]" >> compile_commands.json +// RUN: rm %T/h_hd_constant_db/out/MainSourceFiles.yaml // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/h_hd_constant_db/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/h_hd_constant_db_win.cpp b/clang/test/dpct/h_hd_constant_db_win.cpp index f56ebd3eb174..b172565902e4 100644 --- a/clang/test/dpct/h_hd_constant_db_win.cpp +++ b/clang/test/dpct/h_hd_constant_db_win.cpp @@ -17,6 +17,7 @@ // RUN: echo " \"file\": \"%/T/h_hd_constant_db_win/h_hd_constant_db_win.cpp\"" >> compile_commands.json // RUN: echo " }" >> compile_commands.json // RUN: echo "]" >> compile_commands.json +// RUN: rm %T/h_hd_constant_db_win/out/MainSourceFiles.yaml // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: FileCheck %s --match-full-lines --input-file %T/h_hd_constant_db_win/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/hd_d_constant_db.cpp b/clang/test/dpct/hd_d_constant_db.cpp index 2da7b4da50d7..fd65ccb7df1e 100644 --- a/clang/test/dpct/hd_d_constant_db.cpp +++ b/clang/test/dpct/hd_d_constant_db.cpp @@ -18,6 +18,7 @@ // RUN: echo "]" >> compile_commands.json // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: rm compile_commands.json +// RUN: rm %T/hd_d_constant_db/out/MainSourceFiles.yaml // RUN: dpct hd_d_constant_db.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only // RUN: FileCheck %s --match-full-lines --input-file %T/hd_d_constant_db/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/hd_d_constant_db_win.cpp b/clang/test/dpct/hd_d_constant_db_win.cpp index 92e59c71aeeb..4da746d1beaf 100644 --- a/clang/test/dpct/hd_d_constant_db_win.cpp +++ b/clang/test/dpct/hd_d_constant_db_win.cpp @@ -18,6 +18,7 @@ // RUN: echo "]" >> compile_commands.json // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: rm compile_commands.json +// RUN: rm %T/hd_d_constant_db_win/out/MainSourceFiles.yaml // RUN: dpct hd_d_constant_db_win.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only // RUN: FileCheck %s --match-full-lines --input-file %T/hd_d_constant_db_win/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/hd_h_constant_db.cpp b/clang/test/dpct/hd_h_constant_db.cpp index 3f110a90e88e..e6b917db43df 100644 --- a/clang/test/dpct/hd_h_constant_db.cpp +++ b/clang/test/dpct/hd_h_constant_db.cpp @@ -18,6 +18,7 @@ // RUN: echo "]" >> compile_commands.json // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: rm compile_commands.json +// RUN: rm %T/hd_h_constant_db/out/MainSourceFiles.yaml // RUN: dpct hd_h_constant_db.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x c --cuda-host-only // RUN: FileCheck %s --match-full-lines --input-file %T/hd_h_constant_db/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/hd_h_constant_db_win.cpp b/clang/test/dpct/hd_h_constant_db_win.cpp index e6b4c16eed9f..b647fd8aba0d 100644 --- a/clang/test/dpct/hd_h_constant_db_win.cpp +++ b/clang/test/dpct/hd_h_constant_db_win.cpp @@ -18,6 +18,7 @@ // RUN: echo "]" >> compile_commands.json // RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include" // RUN: rm compile_commands.json +// RUN: rm %T/hd_h_constant_db_win/out/MainSourceFiles.yaml // RUN: dpct hd_h_constant_db_win.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x c --cuda-host-only // RUN: FileCheck %s --match-full-lines --input-file %T/hd_h_constant_db_win/out/constant_header.h // RUN: cd .. diff --git a/clang/test/dpct/header_insert/a.cpp b/clang/test/dpct/header_insert/a.cpp new file mode 100644 index 000000000000..0902b01e6b1c --- /dev/null +++ b/clang/test/dpct/header_insert/a.cpp @@ -0,0 +1,11 @@ +// RUN: dpct --out-root %T/ %s --cuda-include-path="%cuda-path/include" +// RUN: FileCheck --input-file %T/a.cpp.dp.cpp --match-full-lines %s + +// CHECK: #include +// CHECK-NEXT: #include +// CHECK-NEXT: #include "h1.h" +#include "h1.h" + +int main() { + return 0; +} diff --git a/clang/test/dpct/header_insert/h1.h b/clang/test/dpct/header_insert/h1.h new file mode 100644 index 000000000000..2771cad12725 --- /dev/null +++ b/clang/test/dpct/header_insert/h1.h @@ -0,0 +1,5 @@ + +#include +#include +#include "h2.h" +#include "h3.h" diff --git a/clang/test/dpct/header_insert/h2.h b/clang/test/dpct/header_insert/h2.h new file mode 100644 index 000000000000..a9c139727ded --- /dev/null +++ b/clang/test/dpct/header_insert/h2.h @@ -0,0 +1,2 @@ + +#include diff --git a/clang/test/dpct/header_insert/h3.h b/clang/test/dpct/header_insert/h3.h new file mode 100644 index 000000000000..41fe537aac16 --- /dev/null +++ b/clang/test/dpct/header_insert/h3.h @@ -0,0 +1,4 @@ + +#include +#include +#include diff --git a/clang/test/dpct/header_order/dnn.cu b/clang/test/dpct/header_order/dnn.cu index fb5913a99db0..7cf0db9ebf17 100644 --- a/clang/test/dpct/header_order/dnn.cu +++ b/clang/test/dpct/header_order/dnn.cu @@ -2,7 +2,6 @@ // CHECK: #include // CHECK-NEXT: #include // CHECK-NEXT: #include "dnn.h" -// CHECK-NEXT: #include #include "dnn.h" int test(cudnnHandle_t handle) { diff --git a/clang/test/dpct/header_order/test.cu b/clang/test/dpct/header_order/test.cu index c782680b9c7a..a8f4db89d55a 100644 --- a/clang/test/dpct/header_order/test.cu +++ b/clang/test/dpct/header_order/test.cu @@ -9,7 +9,6 @@ // CHECK-NEXT: #include // CHECK-NEXT: #include // CHECK-NEXT: #include "dnn.h" -// CHECK-NEXT: #include #include #include #include diff --git a/clang/test/dpct/uncanonical_path/test.cpp b/clang/test/dpct/uncanonical_path/test.cpp index 04256d8ab25b..a4414e407a2f 100644 --- a/clang/test/dpct/uncanonical_path/test.cpp +++ b/clang/test/dpct/uncanonical_path/test.cpp @@ -1,8 +1,10 @@ // RUN: dpct --out-root %T/uncanonical_path %s --cuda-include-path="%cuda-path/include" -// RUN: FileCheck --input-file %T/uncanonical_path/test.cpp --match-full-lines %s -// RUN: %if build_lit %{icpx -c -fsycl %T/uncanonical_path/test.cpp -o %T/uncanonical_path/test.o %} +// RUN: FileCheck --input-file %T/uncanonical_path/test.cpp.dp.cpp --match-full-lines %s +// RUN: %if build_lit %{icpx -c -fsycl %T/uncanonical_path/test.cpp.dp.cpp -o %T/uncanonical_path/test.o %} -// CHECK: #include "../uncanonical_path//test.h" +// CHECK: #include +// CHECK-NEXT: #include +// CHECK-NEXT: #include "../uncanonical_path//test.h" #include "../uncanonical_path//test.h" int main() { From 98b926e3e90637b6575b0d6f2ca59bb8f5333f5e Mon Sep 17 00:00:00 2001 From: "Jiang, Zhiwei" Date: Wed, 11 Sep 2024 15:04:11 +0800 Subject: [PATCH 2/2] Add a comment Signed-off-by: Jiang, Zhiwei --- clang/lib/DPCT/MigrationAction.cpp | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/clang/lib/DPCT/MigrationAction.cpp b/clang/lib/DPCT/MigrationAction.cpp index a4bf17168ef0..5bba2a0fa8c5 100644 --- a/clang/lib/DPCT/MigrationAction.cpp +++ b/clang/lib/DPCT/MigrationAction.cpp @@ -269,6 +269,13 @@ void DpctToolAction::runPasses() { for (auto Pass : Passes) { runPass(Pass); } + // Before post-processing, we need to set the MainFile to empty since it is no + // longer valid. Currently, DpctFileInfo::insertHeader() is executed both + // before and during the post-processing. In that function, the MainFile value + // is accessed for inserting two kinds of header files: oneDPL header and SYCL + // header. We assume those two headers have already been inserted before the + // post-processing. So, clearing the value of MainFile can avoid the MainFile + // value used in the post-processing. DpctGlobalInfo::getInstance().setMainFile(nullptr); runWithCrashGuard( [&]() {