Skip to content

[SYCLomatic] If viewed from the translation unit, 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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: SYCLomatic
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions clang/lib/DPCT/AnalysisInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -872,8 +872,14 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
if (Type == HT_DPL_Algorithm || Type == HT_DPL_Execution || Type == HT_SYCL) {
if (auto MF = DpctGlobalInfo::getInstance().getMainFile())
if (this != MF.get() && FirstIncludeOffset.count(MF)) {
DpctGlobalInfo::getInstance().getMainFile()->insertHeader(
Type, FirstIncludeOffset.at(MF));
// If <bits/stdc++.h> is included before <sycl/sycl.hpp>, the
// compilation will fail.
auto Iter =
DpctGlobalInfo::getAfterBitsStdcxxFilesMap().find(MF->FilePath);
if (Iter != DpctGlobalInfo::getAfterBitsStdcxxFilesMap().end()) {
if (Iter->second.count(this->FilePath))
MF->insertHeader(Type, FirstIncludeOffset.at(MF));
}
}
}
if (DpctGlobalInfo::getHeaderInsertedBitMap()[FilePath][Type])
Expand Down Expand Up @@ -2537,6 +2543,10 @@ std::unordered_set<std::string>
DpctGlobalInfo::CustomHelperFunctionAddtionalIncludes = {};
std::unordered_map<clang::tooling::UnifiedPath, std::bitset<32>>
DpctGlobalInfo::HeaderInsertedBitMap = {};
bool DpctGlobalInfo::IsAfterBitsStdcxx = false;
std::map<clang::tooling::UnifiedPath /*MainFile*/,
std::set<clang::tooling::UnifiedPath>>
DpctGlobalInfo::AfterBitsStdcxxFiles;
///// class DpctNameGenerator /////
void DpctNameGenerator::printName(const FunctionDecl *FD,
llvm::raw_ostream &OS) {
Expand Down
22 changes: 21 additions & 1 deletion clang/lib/DPCT/AnalysisInfo.h
Original file line number Diff line number Diff line change
Expand Up @@ -1394,6 +1394,12 @@ class DpctGlobalInfo {
getHeaderInsertedBitMap() {
return HeaderInsertedBitMap;
}
static bool &getIsAfterBitsStdcxxStatus() { return IsAfterBitsStdcxx; }
static std::map<clang::tooling::UnifiedPath,
std::set<clang::tooling::UnifiedPath>> &
getAfterBitsStdcxxFilesMap() {
return AfterBitsStdcxxFiles;
}
std::shared_ptr<DpctFileInfo>
insertFile(const clang::tooling::UnifiedPath &FilePath) {
return insertObject(FileMap, FilePath);
Expand All @@ -1403,7 +1409,10 @@ class DpctGlobalInfo {
return findObject(FileMap, FilePath);
}
std::shared_ptr<DpctFileInfo> getMainFile() const { return MainFile; }
void setMainFile(std::shared_ptr<DpctFileInfo> Main) { MainFile = Main; }
void setMainFile(std::shared_ptr<DpctFileInfo> Main) {
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] It would be helpful to add a brief comment in setMainFile explaining why the IsAfterBitsStdcxx flag is reset when a new main file is set, to clarify the logic for future maintainers.

Copilot uses AI. Check for mistakes.

getIsAfterBitsStdcxxStatus() = false;
MainFile = Main;
}
void recordIncludingRelationship(
const clang::tooling::UnifiedPath &CurrentFileName,
const clang::tooling::UnifiedPath &IncludedFileName);
Expand Down Expand Up @@ -1721,6 +1730,17 @@ class DpctGlobalInfo {
static std::unordered_set<std::string> CustomHelperFunctionAddtionalIncludes;
static std::unordered_map<clang::tooling::UnifiedPath, std::bitset<32>>
HeaderInsertedBitMap;
// `IsAfterBitsStdcxx` is used as a flag. It is set to true when PP meet
// <bits/stdc++.h>. It is reset to false when starting to process a new
// translation unit.
static bool IsAfterBitsStdcxx;
// If `IsAfterBitsStdcxx` is true, it means <bits/stdc++.h> is already
// included, so the tool will record all files included since this time point.
// When inserting `sycl.hpp`, the tool can check if the insert location is in
// the map, if it is, the tool will also insert `sycl.hpp` at the main file.
static std::map<clang::tooling::UnifiedPath /*MainFile*/,
std::set<clang::tooling::UnifiedPath>>
AfterBitsStdcxxFiles;
};

/// Generate mangle name of FunctionDecl as key of DeviceFunctionInfo.
Expand Down
5 changes: 5 additions & 0 deletions clang/lib/DPCT/PreProcessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,11 @@ void IncludesCallbacks::FileChanged(SourceLocation Loc, FileChangeReason Reason,
}

clang::tooling::UnifiedPath InFile = SM.getFilename(Loc).str();
if (DpctGlobalInfo::getIsAfterBitsStdcxxStatus()) {
DpctGlobalInfo::getAfterBitsStdcxxFilesMap()
[DpctGlobalInfo::getInstance().getMainFile()->getFilePath()]
.insert(InFile);
}
if (IsFileInCmd || ProcessAll ||
GetSourceFileType(InFile.getCanonicalPath()) & SPT_CudaSource) {
IncludeFileMap[DpctGlobalInfo::removeSymlinks(
Expand Down
4 changes: 4 additions & 0 deletions clang/lib/DPCT/RulesInclude/InclusionHeaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,10 @@ void IncludesCallbacks::InclusionDirective(
FileInfo->setFirstIncludeOffset(LocInfo.second);
LastInclusionLocationUpdater Updater(FileInfo, FilenameRange.getEnd());

if (FileName == "bits/stdc++.h") {
Copy link
Preview

Copilot AI May 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] Consider extracting the literal "bits/stdc++.h" into a named constant to improve maintainability and reduce the chances of typos in future modifications.

Suggested change
if (FileName == "bits/stdc++.h") {
if (FileName == BITS_STDCXX_HEADER) {

Copilot uses AI. Check for mistakes.

DpctGlobalInfo::getInstance().getIsAfterBitsStdcxxStatus() = true;
}

clang::tooling::UnifiedPath IncludedFile;
if (auto OptionalAbs = Global.getAbsolutePath(*File))
IncludedFile = OptionalAbs.value();
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/d_dh_constant_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/d_dh_constant_db_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/d_hd_constant_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/d_hd_constant_db_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/dh_d_constant_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/dh_d_constant_db_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/dh_h_constant_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/dh_h_constant_db_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/h_dh_constant_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/h_dh_constant_db_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/h_hd_constant_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/h_hd_constant_db_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/hd_d_constant_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/hd_d_constant_db_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/hd_h_constant_db.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// 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 ..
Expand Down
1 change: 0 additions & 1 deletion clang/test/dpct/hd_h_constant_db_win.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
// 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 ..
Expand Down
1 change: 1 addition & 0 deletions clang/test/dpct/header_insert/a.cpp
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
// UNSUPPORTED: system-windows
// 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

Expand Down
9 changes: 9 additions & 0 deletions clang/test/dpct/header_insert_2/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: dpct --out-root %T/ %s --cuda-include-path="%cuda-path/include"
// RUN: FileCheck --input-file %T/a.cpp --match-full-lines %s

// CHECK: #include "h1.h"
#include "h1.h"

int main() {
return 0;
}
5 changes: 5 additions & 0 deletions clang/test/dpct/header_insert_2/h1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#include <vector>
#include <numeric>
#include "h2.h"
#include "h3.h"
3 changes: 3 additions & 0 deletions clang/test/dpct/header_insert_2/h2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

// <bits/stdc++.h> is not included
//#include <bits/stdc++.h>
4 changes: 4 additions & 0 deletions clang/test/dpct/header_insert_2/h3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#include <iostream>
#include <string>
#include <cudnn.h>
9 changes: 9 additions & 0 deletions clang/test/dpct/header_insert_3/a.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
// RUN: dpct --out-root %T/ %s --cuda-include-path="%cuda-path/include"
// RUN: FileCheck --input-file %T/a.cpp --match-full-lines %s

// CHECK: #include "h1.h"
#include "h1.h"

int main() {
return 0;
}
5 changes: 5 additions & 0 deletions clang/test/dpct/header_insert_3/h1.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

#include <vector>
#include <numeric>
#include "h2.h"
#include "h3.h"
4 changes: 4 additions & 0 deletions clang/test/dpct/header_insert_3/h2.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

#include <iostream>
#include <string>
#include <cudnn.h>
3 changes: 3 additions & 0 deletions clang/test/dpct/header_insert_3/h3.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

// <bits/stdc++.h> is included after sycl.hpp
#include <bits/stdc++.h>
8 changes: 3 additions & 5 deletions clang/test/dpct/uncanonical_path/test.cpp
Original file line number Diff line number Diff line change
@@ -1,10 +1,8 @@
// RUN: dpct --out-root %T/uncanonical_path %s --cuda-include-path="%cuda-path/include"
// 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 %}
// 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 %}

// CHECK: #include <sycl/sycl.hpp>
// CHECK-NEXT: #include <dpct/dpct.hpp>
// CHECK-NEXT: #include "../uncanonical_path//test.h"
// CHECK: #include "../uncanonical_path//test.h"
#include "../uncanonical_path//test.h"

int main() {
Expand Down
4 changes: 2 additions & 2 deletions clang/test/dpct/user_define_rule_header_order1.cu
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
// 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
// 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 %}

// CHECK: #include <oneapi/dpl/execution>
// CHECK: #include <oneapi/dpl/algorithm>
// CHECK: #include <stddef.h>
// CHECK-NEXT: #include "user_define_rule_header_order1.h"
#include <stddef.h>
#include "user_define_rule_header_order1.h"