Skip to content

Commit b293e4f

Browse files
authored
[SYCLomatic] In a translation unit, if the header file <bits/stdc++.h> is included before SYCL header file, then insert the SYCL header file at the beginning of the main source file of the translation unit. (#2790)
Signed-off-by: Jiang, Zhiwei <zhiwei.jiang@intel.com>
1 parent 99fcbd9 commit b293e4f

31 files changed

+90
-26
lines changed

clang/lib/DPCT/AnalysisInfo.cpp

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -878,8 +878,14 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
878878
if (Type == HT_DPL_Algorithm || Type == HT_DPL_Execution || Type == HT_SYCL) {
879879
if (auto MF = DpctGlobalInfo::getInstance().getMainFile())
880880
if (this != MF.get() && FirstIncludeOffset.count(MF)) {
881-
DpctGlobalInfo::getInstance().getMainFile()->insertHeader(
882-
Type, FirstIncludeOffset.at(MF));
881+
// If <bits/stdc++.h> is included before <sycl/sycl.hpp>, the
882+
// compilation will fail.
883+
auto Iter =
884+
DpctGlobalInfo::getAfterBitsStdcxxFilesMap().find(MF->FilePath);
885+
if (Iter != DpctGlobalInfo::getAfterBitsStdcxxFilesMap().end()) {
886+
if (Iter->second.count(this->FilePath))
887+
MF->insertHeader(Type, FirstIncludeOffset.at(MF));
888+
}
883889
}
884890
}
885891
if (DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath][Type])
@@ -2547,6 +2553,10 @@ std::unordered_set<std::string>
25472553
std::unordered_map<clang::tooling::UnifiedPath,
25482554
std::bitset<HeaderType::NUM_HEADERS>>
25492555
DpctGlobalInfo::HeaderInsertedBitMap = {};
2556+
bool DpctGlobalInfo::IsAfterBitsStdcxx = false;
2557+
std::map<clang::tooling::UnifiedPath /*MainFile*/,
2558+
std::set<clang::tooling::UnifiedPath>>
2559+
DpctGlobalInfo::AfterBitsStdcxxFiles;
25502560
///// class DpctNameGenerator /////
25512561
void DpctNameGenerator::printName(const FunctionDecl *FD,
25522562
llvm::raw_ostream &OS) {

clang/lib/DPCT/AnalysisInfo.h

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1398,6 +1398,12 @@ class DpctGlobalInfo {
13981398
getHeaderInsertedBitMap() {
13991399
return HeaderInsertedBitMap;
14001400
}
1401+
static bool &getIsAfterBitsStdcxxStatus() { return IsAfterBitsStdcxx; }
1402+
static std::map<clang::tooling::UnifiedPath,
1403+
std::set<clang::tooling::UnifiedPath>> &
1404+
getAfterBitsStdcxxFilesMap() {
1405+
return AfterBitsStdcxxFiles;
1406+
}
14011407
std::shared_ptr<DpctFileInfo>
14021408
insertFile(const clang::tooling::UnifiedPath &FilePath) {
14031409
return insertObject(FileMap, FilePath);
@@ -1407,7 +1413,10 @@ class DpctGlobalInfo {
14071413
return findObject(FileMap, FilePath);
14081414
}
14091415
std::shared_ptr<DpctFileInfo> getMainFile() const { return MainFile; }
1410-
void setMainFile(std::shared_ptr<DpctFileInfo> Main) { MainFile = Main; }
1416+
void setMainFile(std::shared_ptr<DpctFileInfo> Main) {
1417+
getIsAfterBitsStdcxxStatus() = false;
1418+
MainFile = Main;
1419+
}
14111420
void recordIncludingRelationship(
14121421
const clang::tooling::UnifiedPath &CurrentFileName,
14131422
const clang::tooling::UnifiedPath &IncludedFileName);
@@ -1726,6 +1735,17 @@ class DpctGlobalInfo {
17261735
static std::unordered_map<clang::tooling::UnifiedPath,
17271736
std::bitset<HeaderType::NUM_HEADERS>>
17281737
HeaderInsertedBitMap;
1738+
// `IsAfterBitsStdcxx` is used as a flag. It is set to true when PP meet
1739+
// <bits/stdc++.h>. It is reset to false when starting to process a new
1740+
// translation unit.
1741+
static bool IsAfterBitsStdcxx;
1742+
// If `IsAfterBitsStdcxx` is true, it means <bits/stdc++.h> is already
1743+
// included, so the tool will record all files included since this time point.
1744+
// When inserting `sycl.hpp`, the tool can check if the insert location is in
1745+
// the map, if it is, the tool will also insert `sycl.hpp` at the main file.
1746+
static std::map<clang::tooling::UnifiedPath /*MainFile*/,
1747+
std::set<clang::tooling::UnifiedPath>>
1748+
AfterBitsStdcxxFiles;
17291749
};
17301750

17311751
/// Generate mangle name of FunctionDecl as key of DeviceFunctionInfo.

clang/lib/DPCT/PreProcessor.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -825,6 +825,11 @@ void IncludesCallbacks::FileChanged(SourceLocation Loc, FileChangeReason Reason,
825825
}
826826

827827
clang::tooling::UnifiedPath InFile = SM.getFilename(Loc).str();
828+
if (DpctGlobalInfo::getIsAfterBitsStdcxxStatus()) {
829+
DpctGlobalInfo::getAfterBitsStdcxxFilesMap()
830+
[DpctGlobalInfo::getInstance().getMainFile()->getFilePath()]
831+
.insert(InFile);
832+
}
828833
if (IsFileInCmd || ProcessAll ||
829834
GetSourceFileType(InFile.getCanonicalPath()) & SPT_CudaSource) {
830835
IncludeFileMap[DpctGlobalInfo::removeSymlinks(

clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ void IncludesCallbacks::InclusionDirective(
132132
FileInfo->setFirstIncludeOffset(LocInfo.second);
133133
LastInclusionLocationUpdater Updater(FileInfo, FilenameRange.getEnd());
134134

135+
if (FileName == "bits/stdc++.h") {
136+
DpctGlobalInfo::getInstance().getIsAfterBitsStdcxxStatus() = true;
137+
}
138+
135139
clang::tooling::UnifiedPath IncludedFile;
136140
if (auto OptionalAbs = Global.getAbsolutePath(*File))
137141
IncludedFile = OptionalAbs.value();

clang/test/dpct/d_dh_constant_db.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// RUN: echo " \"file\": \"%/T/d_dh_constant_db/d_dh_constant_db.cpp\"" >> compile_commands.json
1818
// RUN: echo " }" >> compile_commands.json
1919
// RUN: echo "]" >> compile_commands.json
20-
// RUN: rm %T/d_dh_constant_db/out/MainSourceFiles.yaml
2120
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2221
// RUN: FileCheck %s --match-full-lines --input-file %T/d_dh_constant_db/out/constant_header.h
2322
// RUN: cd ..

clang/test/dpct/d_dh_constant_db_win.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// RUN: echo " \"file\": \"%/T/d_dh_constant_db_win/d_dh_constant_db_win.cpp\"" >> compile_commands.json
1818
// RUN: echo " }" >> compile_commands.json
1919
// RUN: echo "]" >> compile_commands.json
20-
// RUN: rm %T/d_dh_constant_db_win/out/MainSourceFiles.yaml
2120
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2221
// RUN: FileCheck %s --match-full-lines --input-file %T/d_dh_constant_db_win/out/constant_header.h
2322
// RUN: cd ..

clang/test/dpct/d_hd_constant_db.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// RUN: echo " \"file\": \"%/T/d_hd_constant_db/d_hd_constant_db.cpp\"" >> compile_commands.json
1818
// RUN: echo " }" >> compile_commands.json
1919
// RUN: echo "]" >> compile_commands.json
20-
// RUN: rm %T/d_hd_constant_db/out/MainSourceFiles.yaml
2120
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2221
// RUN: FileCheck %s --match-full-lines --input-file %T/d_hd_constant_db/out/constant_header.h
2322
// RUN: cd ..

clang/test/dpct/d_hd_constant_db_win.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// RUN: echo " \"file\": \"%/T/d_hd_constant_db_win/d_hd_constant_db_win.cpp\"" >> compile_commands.json
1818
// RUN: echo " }" >> compile_commands.json
1919
// RUN: echo "]" >> compile_commands.json
20-
// RUN: rm %T/d_hd_constant_db_win/out/MainSourceFiles.yaml
2120
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2221
// RUN: FileCheck %s --match-full-lines --input-file %T/d_hd_constant_db_win/out/constant_header.h
2322
// RUN: cd ..

clang/test/dpct/dh_d_constant_db.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// RUN: echo "]" >> compile_commands.json
1919
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2020
// RUN: rm compile_commands.json
21-
// RUN: rm %T/dh_d_constant_db/out/MainSourceFiles.yaml
2221
// RUN: dpct dh_d_constant_db.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only
2322
// RUN: FileCheck %s --match-full-lines --input-file %T/dh_d_constant_db/out/constant_header.h
2423
// RUN: cd ..

clang/test/dpct/dh_d_constant_db_win.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// RUN: echo "]" >> compile_commands.json
1919
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2020
// RUN: rm compile_commands.json
21-
// RUN: rm %T/dh_d_constant_db_win/out/MainSourceFiles.yaml
2221
// RUN: dpct dh_d_constant_db_win.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only
2322
// RUN: FileCheck %s --match-full-lines --input-file %T/dh_d_constant_db_win/out/constant_header.h
2423
// RUN: cd ..

clang/test/dpct/dh_h_constant_db.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// RUN: echo "]" >> compile_commands.json
1919
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2020
// RUN: rm compile_commands.json
21-
// RUN: rm %T/dh_h_constant_db/out/MainSourceFiles.yaml
2221
// RUN: dpct dh_h_constant_db.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x c --cuda-host-only
2322
// RUN: FileCheck %s --match-full-lines --input-file %T/dh_h_constant_db/out/constant_header.h
2423
// RUN: cd ..

clang/test/dpct/dh_h_constant_db_win.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// RUN: echo "]" >> compile_commands.json
1919
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2020
// RUN: rm compile_commands.json
21-
// RUN: rm %T/dh_h_constant_db_win/out/MainSourceFiles.yaml
2221
// RUN: dpct dh_h_constant_db_win.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x c --cuda-host-only
2322
// RUN: FileCheck %s --match-full-lines --input-file %T/dh_h_constant_db_win/out/constant_header.h
2423
// RUN: cd ..

clang/test/dpct/h_dh_constant_db.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// RUN: echo " \"file\": \"%/T/h_dh_constant_db/h_dh_constant_db.cpp\"" >> compile_commands.json
1818
// RUN: echo " }" >> compile_commands.json
1919
// RUN: echo "]" >> compile_commands.json
20-
// RUN: rm %T/h_dh_constant_db/out/MainSourceFiles.yaml
2120
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2221
// RUN: FileCheck %s --match-full-lines --input-file %T/h_dh_constant_db/out/constant_header.h
2322
// RUN: cd ..

clang/test/dpct/h_dh_constant_db_win.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// RUN: echo " \"file\": \"%/T/h_dh_constant_db_win/h_dh_constant_db_win.cpp\"" >> compile_commands.json
1818
// RUN: echo " }" >> compile_commands.json
1919
// RUN: echo "]" >> compile_commands.json
20-
// RUN: rm %T/h_dh_constant_db_win/out/MainSourceFiles.yaml
2120
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2221
// RUN: FileCheck %s --match-full-lines --input-file %T/h_dh_constant_db_win/out/constant_header.h
2322
// RUN: cd ..

clang/test/dpct/h_hd_constant_db.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// RUN: echo " \"file\": \"%/T/h_hd_constant_db/h_hd_constant_db.cpp\"" >> compile_commands.json
1818
// RUN: echo " }" >> compile_commands.json
1919
// RUN: echo "]" >> compile_commands.json
20-
// RUN: rm %T/h_hd_constant_db/out/MainSourceFiles.yaml
2120
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2221
// RUN: FileCheck %s --match-full-lines --input-file %T/h_hd_constant_db/out/constant_header.h
2322
// RUN: cd ..

clang/test/dpct/h_hd_constant_db_win.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@
1717
// RUN: echo " \"file\": \"%/T/h_hd_constant_db_win/h_hd_constant_db_win.cpp\"" >> compile_commands.json
1818
// RUN: echo " }" >> compile_commands.json
1919
// RUN: echo "]" >> compile_commands.json
20-
// RUN: rm %T/h_hd_constant_db_win/out/MainSourceFiles.yaml
2120
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2221
// RUN: FileCheck %s --match-full-lines --input-file %T/h_hd_constant_db_win/out/constant_header.h
2322
// RUN: cd ..

clang/test/dpct/hd_d_constant_db.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// RUN: echo "]" >> compile_commands.json
1919
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2020
// RUN: rm compile_commands.json
21-
// RUN: rm %T/hd_d_constant_db/out/MainSourceFiles.yaml
2221
// RUN: dpct hd_d_constant_db.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only
2322
// RUN: FileCheck %s --match-full-lines --input-file %T/hd_d_constant_db/out/constant_header.h
2423
// RUN: cd ..

clang/test/dpct/hd_d_constant_db_win.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// RUN: echo "]" >> compile_commands.json
1919
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2020
// RUN: rm compile_commands.json
21-
// RUN: rm %T/hd_d_constant_db_win/out/MainSourceFiles.yaml
2221
// RUN: dpct hd_d_constant_db_win.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x cuda --cuda-host-only
2322
// RUN: FileCheck %s --match-full-lines --input-file %T/hd_d_constant_db_win/out/constant_header.h
2423
// RUN: cd ..

clang/test/dpct/hd_h_constant_db.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// RUN: echo "]" >> compile_commands.json
1919
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2020
// RUN: rm compile_commands.json
21-
// RUN: rm %T/hd_h_constant_db/out/MainSourceFiles.yaml
2221
// RUN: dpct hd_h_constant_db.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x c --cuda-host-only
2322
// RUN: FileCheck %s --match-full-lines --input-file %T/hd_h_constant_db/out/constant_header.h
2423
// RUN: cd ..

clang/test/dpct/hd_h_constant_db_win.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
// RUN: echo "]" >> compile_commands.json
1919
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
2020
// RUN: rm compile_commands.json
21-
// RUN: rm %T/hd_h_constant_db_win/out/MainSourceFiles.yaml
2221
// RUN: dpct hd_h_constant_db_win.cpp --out-root=./out --cuda-include-path="%cuda-path/include" -- -x c --cuda-host-only
2322
// RUN: FileCheck %s --match-full-lines --input-file %T/hd_h_constant_db_win/out/constant_header.h
2423
// RUN: cd ..

clang/test/dpct/header_insert/a.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
// UNSUPPORTED: system-windows
12
// RUN: dpct --out-root %T/ %s --cuda-include-path="%cuda-path/include"
23
// RUN: FileCheck --input-file %T/a.cpp.dp.cpp --match-full-lines %s
34

clang/test/dpct/header_insert_2/a.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: dpct --out-root %T/ %s --cuda-include-path="%cuda-path/include"
2+
// RUN: FileCheck --input-file %T/a.cpp --match-full-lines %s
3+
4+
// CHECK: #include "h1.h"
5+
#include "h1.h"
6+
7+
int main() {
8+
return 0;
9+
}

clang/test/dpct/header_insert_2/h1.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
#include <vector>
3+
#include <numeric>
4+
#include "h2.h"
5+
#include "h3.h"

clang/test/dpct/header_insert_2/h2.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
// <bits/stdc++.h> is not included
3+
//#include <bits/stdc++.h>

clang/test/dpct/header_insert_2/h3.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
#include <iostream>
3+
#include <string>
4+
#include <cudnn.h>

clang/test/dpct/header_insert_3/a.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// RUN: dpct --out-root %T/ %s --cuda-include-path="%cuda-path/include"
2+
// RUN: FileCheck --input-file %T/a.cpp --match-full-lines %s
3+
4+
// CHECK: #include "h1.h"
5+
#include "h1.h"
6+
7+
int main() {
8+
return 0;
9+
}

clang/test/dpct/header_insert_3/h1.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
#include <vector>
3+
#include <numeric>
4+
#include "h2.h"
5+
#include "h3.h"

clang/test/dpct/header_insert_3/h2.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
2+
#include <iostream>
3+
#include <string>
4+
#include <cudnn.h>

clang/test/dpct/header_insert_3/h3.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
2+
// <bits/stdc++.h> is included after sycl.hpp
3+
#include <bits/stdc++.h>

clang/test/dpct/uncanonical_path/test.cpp

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
// RUN: dpct --out-root %T/uncanonical_path %s --cuda-include-path="%cuda-path/include"
2-
// RUN: FileCheck --input-file %T/uncanonical_path/test.cpp.dp.cpp --match-full-lines %s
3-
// RUN: %if build_lit %{icpx -c -fsycl %T/uncanonical_path/test.cpp.dp.cpp -o %T/uncanonical_path/test.o %}
2+
// RUN: FileCheck --input-file %T/uncanonical_path/test.cpp --match-full-lines %s
3+
// RUN: %if build_lit %{icpx -c -fsycl %T/uncanonical_path/test.cpp -o %T/uncanonical_path/test.o %}
44

5-
// CHECK: #include <sycl/sycl.hpp>
6-
// CHECK-NEXT: #include <dpct/dpct.hpp>
7-
// CHECK-NEXT: #include "../uncanonical_path//test.h"
5+
// CHECK: #include "../uncanonical_path//test.h"
86
#include "../uncanonical_path//test.h"
97

108
int main() {

clang/test/dpct/user_define_rule_header_order1.cu

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
// RUN: FileCheck --input-file %T/user_define_rule_header_order1_output/user_define_rule_header_order1.dp.cpp --match-full-lines user_define_rule_header_order1.cu
1111
// RUN: %if build_lit %{icpx -c -fsycl -DNO_BUILD_TEST %T/user_define_rule_header_order1_output/user_define_rule_header_order1.dp.cpp -o %T/user_define_rule_header_order1_output/user_define_rule_header_orde1r.dp.o %}
1212

13-
// CHECK: #include <oneapi/dpl/execution>
14-
// CHECK: #include <oneapi/dpl/algorithm>
13+
// CHECK: #include <stddef.h>
14+
// CHECK-NEXT: #include "user_define_rule_header_order1.h"
1515
#include <stddef.h>
1616
#include "user_define_rule_header_order1.h"

0 commit comments

Comments
 (0)