Skip to content

Commit 491de11

Browse files
authored
[SYCLomatic] Fix the FirstIncludeOffset (#2456)
Signed-off-by: Jiang, Zhiwei <[email protected]>
1 parent 4b26735 commit 491de11

File tree

8 files changed

+143
-20
lines changed

8 files changed

+143
-20
lines changed

clang/lib/DPCT/ASTTraversal.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9784,6 +9784,10 @@ void MemoryMigrationRule::mallocMigration(
97849784
DpctGlobalInfo::addPriorityReplInfo(
97859785
LocInfo.first.getCanonicalPath().str() + std::to_string(LocInfo.second), Info);
97869786
}
9787+
// Insert header here since PriorityReplInfo delay the replacement addation
9788+
// to the post process. At that time, the MainFile is invalid.
9789+
DpctGlobalInfo::getInstance().insertHeader(C->getBeginLoc(),
9790+
HeaderType::HT_SYCL);
97879791
instrumentAddressToSizeRecordForCodePin(C,0,1);
97889792
} else if (Name == "cudaHostAlloc" || Name == "cudaMallocHost" ||
97899793
Name == "cuMemHostAlloc" || Name == "cuMemAllocHost_v2" ||
@@ -9804,6 +9808,10 @@ void MemoryMigrationRule::mallocMigration(
98049808
EA.getSubExprRepl().end());
98059809
DpctGlobalInfo::addPriorityReplInfo(
98069810
LocInfo.first.getCanonicalPath().str() + std::to_string(LocInfo.second), Info);
9811+
// Insert header here since PriorityReplInfo delay the replacement
9812+
// addation to the post process. At that time, the MainFile is invalid.
9813+
DpctGlobalInfo::getInstance().insertHeader(C->getBeginLoc(),
9814+
HeaderType::HT_SYCL);
98079815
} else {
98089816
ManagedPointerAnalysis MPA(C, IsAssigned);
98099817
MPA.RecursiveAnalyze();
@@ -10636,6 +10644,10 @@ void MemoryMigrationRule::miscMigration(const MatchFinder::MatchResult &Result,
1063610644
EA.getSubExprRepl().end());
1063710645
DpctGlobalInfo::addPriorityReplInfo(
1063810646
LocInfo.first.getCanonicalPath().str() + std::to_string(LocInfo.second), Info);
10647+
// Insert header here since PriorityReplInfo delay the replacement
10648+
// addation to the post process. At that time, the MainFile is invalid.
10649+
DpctGlobalInfo::getInstance().insertHeader(C->getBeginLoc(),
10650+
HeaderType::HT_SYCL);
1063910651
} else {
1064010652
report(C->getBeginLoc(), Diagnostics::API_NOT_MIGRATED, false,
1064110653
MapNames::ITFName.at(Name));

clang/lib/DPCT/AnalysisInfo.cpp

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -813,16 +813,18 @@ bool DpctFileInfo::isInAnalysisScope() {
813813
return DpctGlobalInfo::isInAnalysisScope(FilePath);
814814
}
815815
void DpctFileInfo::setFileEnterOffset(unsigned Offset) {
816-
if (!HasInclusionDirective) {
817-
FirstIncludeOffset = Offset;
816+
auto MF = DpctGlobalInfo::getInstance().getMainFile();
817+
if (!HasInclusionDirectiveSet.count(MF)) {
818+
FirstIncludeOffset[MF] = Offset;
818819
LastIncludeOffset = Offset;
819820
}
820821
}
821822
void DpctFileInfo::setFirstIncludeOffset(unsigned Offset) {
822-
if (!HasInclusionDirective) {
823-
FirstIncludeOffset = Offset;
823+
auto MF = DpctGlobalInfo::getInstance().getMainFile();
824+
if (!HasInclusionDirectiveSet.count(MF)) {
825+
FirstIncludeOffset[MF] = Offset;
824826
LastIncludeOffset = Offset;
825-
HasInclusionDirective = true;
827+
HasInclusionDirectiveSet.insert(MF);
826828
}
827829
}
828830
void DpctFileInfo::concatHeader(llvm::raw_string_ostream &OS) {}
@@ -853,9 +855,10 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
853855
ReplacementType IsForCodePin) {
854856
if (Type == HT_DPL_Algorithm || Type == HT_DPL_Execution || Type == HT_SYCL) {
855857
if (auto MF = DpctGlobalInfo::getInstance().getMainFile())
856-
if (this != MF.get())
858+
if (this != MF.get() && FirstIncludeOffset.count(MF)) {
857859
DpctGlobalInfo::getInstance().getMainFile()->insertHeader(
858-
Type, FirstIncludeOffset);
860+
Type, FirstIncludeOffset.at(MF));
861+
}
859862
}
860863
if (HeaderInsertedBitMap[Type])
861864
return;
@@ -877,8 +880,11 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
877880
case HT_DPL_Algorithm:
878881
case HT_DPL_Execution:
879882
concatHeader(OS, getHeaderSpelling(Type));
880-
return insertHeader(OS.str(), FirstIncludeOffset,
881-
InsertPosition::IP_AlwaysLeft);
883+
if (auto Iter = FirstIncludeOffset.find(
884+
DpctGlobalInfo::getInstance().getMainFile());
885+
Iter != FirstIncludeOffset.end())
886+
insertHeader(OS.str(), Iter->second, InsertPosition::IP_AlwaysLeft);
887+
return;
882888
case HT_SYCL:
883889
// Add the label for profiling macro "DPCT_PROFILING_ENABLED", which will be
884890
// replaced by "#define DPCT_PROFILING_ENABLED" or not in the post
@@ -924,7 +930,10 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
924930
<< DpctGlobalInfo::getGlobalQueueName() << ";" << getNL();
925931
}
926932
}
927-
insertHeader(OS.str(), FirstIncludeOffset, InsertPosition::IP_Left);
933+
if (auto Iter = FirstIncludeOffset.find(
934+
DpctGlobalInfo::getInstance().getMainFile());
935+
Iter != FirstIncludeOffset.end())
936+
insertHeader(OS.str(), Iter->second, InsertPosition::IP_Left);
928937
if (!RTVersionValue.empty())
929938
MigratedMacroDefinitionOS << "#define DPCT_COMPAT_RT_VERSION "
930939
<< RTVersionValue << getNL();
@@ -941,8 +950,11 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
941950
InsertPosition::IP_AlwaysLeft);
942951
for (const auto &File :
943952
DpctGlobalInfo::getCustomHelperFunctionAddtionalIncludes()) {
944-
insertHeader("#include \"" + File + +"\"" + getNL(), FirstIncludeOffset,
945-
InsertPosition::IP_Right);
953+
if (auto Iter = FirstIncludeOffset.find(
954+
DpctGlobalInfo::getInstance().getMainFile());
955+
Iter != FirstIncludeOffset.end())
956+
insertHeader("#include \"" + File + +"\"" + getNL(), Iter->second,
957+
InsertPosition::IP_Right);
946958
}
947959
return;
948960

@@ -994,14 +1006,20 @@ void DpctFileInfo::insertHeader(HeaderType Type, unsigned Offset,
9941006
}
9951007
SchemaRelativePath += "codepin_autogen_util.hpp\"";
9961008
concatHeader(OS, SchemaRelativePath);
997-
return insertHeader(OS.str(), FirstIncludeOffset, InsertPosition::IP_Right,
998-
IsForCodePin);
1009+
if (auto Iter = FirstIncludeOffset.find(
1010+
DpctGlobalInfo::getInstance().getMainFile());
1011+
Iter != FirstIncludeOffset.end())
1012+
insertHeader(OS.str(), Iter->second, InsertPosition::IP_Right,
1013+
IsForCodePin);
1014+
return;
9991015
} break;
10001016
default:
10011017
break;
10021018
}
10031019

1004-
if (Offset != FirstIncludeOffset)
1020+
if (FirstIncludeOffset.count(DpctGlobalInfo::getInstance().getMainFile()) &&
1021+
Offset !=
1022+
FirstIncludeOffset.at(DpctGlobalInfo::getInstance().getMainFile()))
10051023
OS << getNL();
10061024
concatHeader(OS, getHeaderSpelling(Type));
10071025
return insertHeader(OS.str(), LastIncludeOffset, InsertPosition::IP_Right);

clang/lib/DPCT/AnalysisInfo.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,10 +586,14 @@ class DpctFileInfo {
586586
clang::tooling::UnifiedPath FilePath;
587587
std::string FileContentCache;
588588

589-
unsigned FirstIncludeOffset = 0;
589+
// Save the FirstIncludeOffset in each MainFile
590+
std::map<std::shared_ptr<DpctFileInfo> /*MainFile*/, unsigned>
591+
FirstIncludeOffset;
590592
unsigned LastIncludeOffset = 0;
591593
const unsigned FileBeginOffset = 0;
592-
bool HasInclusionDirective = false;
594+
// Save the status whether FirstIncludeOffset is set by setFirstIncludeOffset
595+
// for each MainFile
596+
std::set<std::shared_ptr<DpctFileInfo> /*MainFile*/> HasInclusionDirectiveSet;
593597
std::vector<std::string> InsertedHeaders;
594598
std::vector<std::string> InsertedHeadersCUDA;
595599
std::bitset<32> HeaderInsertedBitMap;

clang/lib/DPCT/RulesLangLib/CUBAPIMigration.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -603,12 +603,22 @@ void removeVarDecl(const VarDecl *VD) {
603603
(new ReplaceStmt(DS, ""))->getReplacement(Context));
604604
DeclPRInfo->Priority = 1;
605605
DpctGlobalInfo::addPriorityReplInfo(Key, DeclPRInfo);
606+
// Insert header here since PriorityReplInfo delay the replacement
607+
// addation to the post process. At that time, the MainFile is
608+
// invalid.
609+
DpctGlobalInfo::getInstance().insertHeader(DS->getBeginLoc(),
610+
HeaderType::HT_SYCL);
606611
} else {
607612
auto SubDeclPRInfo = std::make_shared<PriorityReplInfo>();
608613
SubDeclPRInfo->Repls.emplace_back(
609614
replaceText(Beg, End.getLocWithOffset(1), "", SM)
610615
->getReplacement(Context));
611616
DpctGlobalInfo::addPriorityReplInfo(Key, SubDeclPRInfo);
617+
// Insert header here since PriorityReplInfo delay the replacement
618+
// addation to the post process. At that time, the MainFile is
619+
// invalid.
620+
DpctGlobalInfo::getInstance().insertHeader(Beg,
621+
HeaderType::HT_SYCL);
612622
}
613623
break;
614624
}
@@ -666,6 +676,11 @@ void CubDeviceLevelRule::removeRedundantTempVar(const CallExpr *CE) {
666676
auto LocInfo = DpctGlobalInfo::getLocInfo((*Itr)->getBeginLoc());
667677
auto Info = std::make_shared<PriorityReplInfo>();
668678
Info->Priority = 1;
679+
// Insert header here since PriorityReplInfo delay the replacement
680+
// addation to the post process. At that time, the MainFile is
681+
// invalid.
682+
DpctGlobalInfo::getInstance().insertHeader((*Itr)->getBeginLoc(),
683+
HeaderType::HT_SYCL);
669684
if (IsUsed) {
670685
Info->Repls.emplace_back(ReplaceStmt(*Itr, "0").getReplacement(
671686
DpctGlobalInfo::getContext()));
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// RUN: echo ""
2+
3+
// CHECK: #include "b.h"
4+
#include "b.h"

clang/test/dpct/header_insert/b.cu

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
// UNSUPPORTED: system-windows
2+
// RUN: cd %T
3+
// RUN: cp %S/b.cu .
4+
// RUN: cp %S/b.cpp .
5+
// RUN: cp %S/b.h .
6+
// RUN: echo "[" > compile_commands.json
7+
// RUN: echo " {" >> compile_commands.json
8+
// RUN: echo " \"command\": \"c++ b.cpp\"," >> compile_commands.json
9+
// RUN: echo " \"directory\": \"%/T\"," >> compile_commands.json
10+
// RUN: echo " \"file\": \"%/T/b.cpp\"" >> compile_commands.json
11+
// RUN: echo " }," >> compile_commands.json
12+
// RUN: echo " {" >> compile_commands.json
13+
// RUN: echo " \"command\": \"nvcc b.cu\"," >> compile_commands.json
14+
// RUN: echo " \"directory\": \"%/T\"," >> compile_commands.json
15+
// RUN: echo " \"file\": \"%/T/b.cu\"" >> compile_commands.json
16+
// RUN: echo " }" >> compile_commands.json
17+
// RUN: echo "]" >> compile_commands.json
18+
// RUN: dpct -p=. --out-root=./out --cuda-include-path="%cuda-path/include"
19+
// RUN: FileCheck %S/b.cu --match-full-lines --input-file %T/out/b.dp.cpp
20+
// RUN: FileCheck %S/b.cpp --match-full-lines --input-file %T/out/b.cpp
21+
// RUN: FileCheck %S/b.h --match-full-lines --input-file %T/out/b.h
22+
// RUN: rm -rf ./out
23+
24+
// CHECK: #define CU_FILE
25+
// CHECK-NEXT: #include <sycl/sycl.hpp>
26+
// CHECK-NEXT: #include <dpct/dpct.hpp>
27+
// CHECK-NEXT: #include "b.h"
28+
// CHECK-EMPTY:
29+
// CHECK-NEXT: void foo() {
30+
// CHECK-NEXT: sycl::float2 f2;
31+
// CHECK-NEXT: }
32+
33+
#define CU_FILE
34+
#include "b.h"
35+
36+
void foo() {
37+
float2 f2;
38+
}

clang/test/dpct/header_insert/b.h

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
2+
// CHECK: #ifndef CU_FILE
3+
// CHECK-NEXT: #include <cstdio>
4+
// CHECK-NEXT: #else
5+
// CHECK-NEXT: #include <sycl/sycl.hpp>
6+
// CHECK-NEXT: #include <dpct/dpct.hpp>
7+
// CHECK-NEXT: #include <iostream>
8+
// CHECK-NEXT: #endif
9+
// CHECK-EMPTY:
10+
// CHECK-NEXT: typedef struct dpct_type_{{[0-9]+}} {
11+
// CHECK-NEXT: unsigned i;
12+
// CHECK-NEXT: } T1;
13+
// CHECK-EMPTY:
14+
// CHECK-NEXT: #ifdef CU_FILE
15+
// CHECK-NEXT: sycl::float2 ff;
16+
// CHECK-NEXT: #endif
17+
18+
// This test targets to make sure SYCL header files are inserted into
19+
// right location (in the code section: CU_FILE is defined)
20+
// to avoid build fail for code path without CU_FILE.
21+
22+
#ifndef CU_FILE
23+
#include <cstdio>
24+
#else
25+
#include <iostream>
26+
#endif
27+
28+
typedef struct {
29+
unsigned i;
30+
} T1;
31+
32+
#ifdef CU_FILE
33+
float2 ff;
34+
#endif

clang/test/dpct/unused_atomic_macro.cu

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,7 @@
44
// RUN: FileCheck --input-file %T/unused_atomic_macro/unused_atomic_macro.dp.cpp --match-full-lines %s
55
// RUN: %if build_lit %{icpx -c -fsycl %T/unused_atomic_macro/unused_atomic_macro.dp.cpp -o %T/unused_atomic_macro/unused_atomic_macro.dp.o %}
66

7-
// CHECK: #include <sycl/sycl.hpp>
8-
// CHECK-NEXT: #include <dpct/dpct.hpp>
9-
// CHECK-NEXT: /*
7+
// CHECK: /*
108
// CHECK-NEXT: DPCT1058:{{[0-9]+}}: "atomicAdd" is not migrated because it is not called in the code.
119
// CHECK-NEXT: */
1210
// CHECK-NEXT: #define ATOMIC_ADD(x, v) atomicAdd(&x, v);

0 commit comments

Comments
 (0)