Skip to content

Commit aa090b2

Browse files
committed
[SYCL] Fix debug info generation when integration footer is present
Signed-off-by: Zahira Ammarguellat <[email protected]>
1 parent 7ecf4f3 commit aa090b2

File tree

12 files changed

+130
-6
lines changed

12 files changed

+130
-6
lines changed

clang/include/clang/Basic/CodeGenOptions.def

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -485,6 +485,10 @@ CODEGENOPT(SkipRaxSetup, 1, 0)
485485
ENUM_CODEGENOPT(ZeroCallUsedRegs, llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind,
486486
5, llvm::ZeroCallUsedRegs::ZeroCallUsedRegsKind::Skip)
487487

488+
/// Whether to expect -main-file-name to be an absolute path to use it for
489+
/// checksum calculations or not.
490+
CODEGENOPT(SYCLUseMainFileName, 1, 0)
491+
488492
/// Whether to use opaque pointers.
489493
CODEGENOPT(OpaquePointers, 1, 0)
490494

clang/include/clang/Basic/CodeGenOptions.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,9 @@ class CodeGenOptions : public CodeGenOptionsBase {
220220
/// file, for example with -save-temps.
221221
std::string MainFileName;
222222

223+
/// The user provided name for the "main file", with its full path.
224+
std::string FullMainFileName;
225+
223226
/// The name for the split debug info file used for the DW_AT_[GNU_]dwo_name
224227
/// attribute in the skeleton CU.
225228
std::string SplitDwarfFile;

clang/include/clang/Driver/Options.td

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6151,6 +6151,10 @@ def main_file_name : Separate<["-"], "main-file-name">,
61516151
HelpText<"Main file name to use for debug info and source if missing">,
61526152
Flags<[CC1Option, CC1AsOption, NoDriverOption]>,
61536153
MarshallingInfoString<CodeGenOpts<"MainFileName">>;
6154+
def full_main_file_name : Separate<["-"], "full-main-file-name">,
6155+
HelpText<"Name file with full path to use for debug info during host and device compile">,
6156+
Flags<[CC1Option, CC1AsOption, NoDriverOption]>,
6157+
MarshallingInfoString<CodeGenOpts<"FullMainFileName">>;
61546158
def split_dwarf_output : Separate<["-"], "split-dwarf-output">,
61556159
HelpText<"File name to use for split dwarf debug info output">,
61566160
Flags<[CC1Option, CC1AsOption, NoDriverOption]>,
@@ -6510,6 +6514,10 @@ def fsycl_disable_range_rounding : Flag<["-"], "fsycl-disable-range-rounding">,
65106514
def fsycl_enable_int_header_diags: Flag<["-"], "fsycl-enable-int-header-diags">,
65116515
HelpText<"Enable diagnostics that require the SYCL integration header.">,
65126516
MarshallingInfoFlag<LangOpts<"SYCLEnableIntHeaderDiags">>;
6517+
def fsycl_use_main_file_name : Flag<["-"], "fsycl-use-main-file-name">,
6518+
HelpText<"Tells compiler that -main-file-name contains an absolute path and "
6519+
"file specified there should be used for checksum calculation.">,
6520+
MarshallingInfoFlag<CodeGenOpts<"SYCLUseMainFileName">>;
65136521

65146522
} // let Flags = [CC1Option, NoDriverOption]
65156523

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -513,22 +513,39 @@ void CGDebugInfo::CreateCompileUnit() {
513513
// Get absolute path name.
514514
SourceManager &SM = CGM.getContext().getSourceManager();
515515
std::string MainFileName = CGM.getCodeGenOpts().MainFileName;
516+
std::string FullMainFileName = CGM.getCodeGenOpts().FullMainFileName;
516517
if (MainFileName.empty())
517518
MainFileName = "<stdin>";
518519

519520
// The main file name provided via the "-main-file-name" option contains just
520521
// the file name itself with no path information. This file name may have had
521522
// a relative path, so we look into the actual file entry for the main
522523
// file to determine the real absolute path for the file.
524+
// An exception here is workflow when integration footer is involved: in that
525+
// case driver passes an absolute path to the original user-provided source
526+
// file, whilst main file corresponds to a temporary file generated by the
527+
// compiler.
523528
std::string MainFileDir;
524529
if (Optional<FileEntryRef> MainFile =
525530
SM.getFileEntryRefForID(SM.getMainFileID())) {
526531
MainFileDir = std::string(MainFile->getDir().getName());
527-
if (!llvm::sys::path::is_absolute(MainFileName)) {
532+
FileID MainFileID = SM.getMainFileID();
533+
if (!llvm::sys::path::is_absolute(MainFileName) &&
534+
!CGM.getCodeGenOpts().SYCLUseMainFileName) {
528535
llvm::SmallString<1024> MainFileDirSS(MainFileDir);
529536
llvm::sys::path::append(MainFileDirSS, MainFileName);
530537
MainFileName =
531538
std::string(llvm::sys::path::remove_leading_dotslash(MainFileDirSS));
539+
} else if (CGM.getCodeGenOpts().SYCLUseMainFileName) {
540+
// When integration footer is involved, main file is a temporary file
541+
// generated by the compiler, but -main-file-name is expected to contain
542+
// an absolute path to the original user-provided source file. We use it
543+
// here to properly calculate its checksum.
544+
auto ExpectedFileRef = SM.getFileManager().getFileRef(FullMainFileName);
545+
if (ExpectedFileRef)
546+
MainFileID = SM.getOrCreateFileID(ExpectedFileRef.get(),
547+
SrcMgr::CharacteristicKind::C_User);
548+
MainFileName = FullMainFileName;
532549
}
533550
// If the main file name provided is identical to the input file name, and
534551
// if the input file is a preprocessed source, use the module name for
@@ -540,7 +557,7 @@ void CGDebugInfo::CreateCompileUnit() {
540557
.isPreprocessed())
541558
MainFileName = CGM.getModule().getName().str();
542559

543-
CSKind = computeChecksum(SM.getMainFileID(), Checksum);
560+
CSKind = computeChecksum(MainFileID, Checksum);
544561
}
545562

546563
llvm::dwarf::SourceLanguage LangTag;

clang/lib/Driver/ToolChains/Clang.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5438,7 +5438,17 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
54385438
// Set the main file name, so that debug info works even with
54395439
// -save-temps.
54405440
CmdArgs.push_back("-main-file-name");
5441-
CmdArgs.push_back(getBaseInputName(Args, Input));
5441+
if (!IsSYCL || Args.hasArg(options::OPT_fno_sycl_use_footer)) {
5442+
CmdArgs.push_back(getBaseInputName(Args, Input));
5443+
} else {
5444+
SmallString<256> AbsPath = llvm::StringRef(Input.getBaseInput());
5445+
D.getVFS().makeAbsolute(AbsPath);
5446+
CmdArgs.push_back(
5447+
Args.MakeArgString(llvm::sys::path::filename(Input.getBaseInput())));
5448+
CmdArgs.push_back("-fsycl-use-main-file-name");
5449+
}
5450+
CmdArgs.push_back("-full-main-file-name");
5451+
CmdArgs.push_back(Input.getBaseInput());
54425452

54435453
// Some flags which affect the language (via preprocessor
54445454
// defines).
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
#line 1 "checksum.cpp"
2+
int main() {
3+
int x = 0;
4+
return x + 1;
5+
}
6+
7+
#include "footer.h"
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
int main() {
2+
int x = 0;
3+
return x + 1;
4+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
#include "Inputs/sycl.hpp"
2+
3+
int main() {
4+
sycl::sampler Sampler;
5+
sycl::kernel_single_task<class use_kernel_for_test>([=]() {
6+
Sampler.use();
7+
});
8+
return 0;
9+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
// This file attempts to emulate content of a file, which is passed to host
2+
// compiler when integration footer is used: this file is considered to be a
3+
// main file and Inputs/debug-info-checksum.cpp serves as integration footer.
4+
//
5+
// The file is specifically named debug-info-checksum-temp-name.cpp to emulate
6+
// the temporary file name, which is generated by the compiler driver for the
7+
// host .cpp file after appending footer to it.
8+
//
9+
// The command line executed is based on what is actually invoked by the
10+
// compiler driver and we explicitly pass 'Inputs/debug-info-checksum.cpp' as
11+
// a main file name to ensure that we can instruct the compiler to emit the
12+
// correct debug info (paths and checksums), even though the input file is not
13+
// exactly what user specified on the command line.
14+
//
15+
// RUN: %clang_cc1 -fsycl-is-host -I %S %S/Inputs/debug-info-checksum.cpp \
16+
// RUN: -triple x86_64-unknown-linux-gpu \
17+
// RUN: -main-file-name "%S/Inputs/debug-info-checksum.cpp" \
18+
// RUN: -full-main-file-name "%S/Inputs/debug-info-checksum.cpp" \
19+
// RUN: -fsycl-use-main-file-name -dwarf-version=5 -S -emit-llvm \
20+
// RUN: -O0 -debug-info-kind=constructor -o - | FileCheck %s
21+
//
22+
// Verify that DICompileUnit points to a correct file and that checksum is also
23+
// correct.
24+
//
25+
// CHECK: !DICompileUnit({{.*}} file: ![[#FILE:]]
26+
// CHECK: ![[#FILE]] = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs{{.+}}debug-info-checksum.cpp"
27+
// CHECK-SAME: checksumkind: CSK_MD5, checksum: "f1fb5d68350b47d90a53968ac8c40529"
28+
29+
#include "Inputs/debug-info-checksum.cpp
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// RUN: %clang_cc1 -fsycl-is-device %S/Inputs/checksum.cpp \
2+
// RUN: -triple spir64-unknown-unknown \
3+
// RUN: -main-file-name "%S/Inputs/checksum.cpp" \
4+
// RUN: -full-main-file-name "%S/Inputs/checksum.cpp" \
5+
// RUN: -fsycl-use-main-file-name -gcodeview -debug-info-kind=constructor \
6+
// RUN: -emit-llvm -O0 -o - | FileCheck %s
7+
8+
// Check that "checksum" is created correctly for the compiled file and
9+
// that the same checksum is generated for the input file appended with
10+
// the footer.
11+
12+
// CHECK: !DICompileUnit({{.*}} file: ![[#FILE:]]
13+
// CHECK: ![[#FILE:]] = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs{{.+}}checksum.cpp"
14+
// CHECK-SAME: checksumkind: CSK_MD5, checksum: "259269f735d83ec32c46a11352458493")
15+
16+
// RUN: %clang_cc1 -fsycl-is-host %S/Inputs/checksum-with-footer.cpp \
17+
// RUN: -triple x86_64-pc-windows-msvc \
18+
// RUN: -main-file-name %S/Inputs/checksum.cpp \
19+
// RUN: -full-main-file-name %S/Inputs/checksum.cpp \
20+
// RUN: -fsycl-use-main-file-name -gcodeview -debug-info-kind=constructor \
21+
// RUN: -S -emit-llvm -O0 -o - | FileCheck %s
22+
23+
24+
// CHECKSUM: distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: !1
25+
// CHECKSUM: !1 = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs{{.+}}checksum.cpp"
26+
// CHECKSUM-SAME: checksumkind: CSK_MD5, checksum: "259269f735d83ec32c46a11352458493")

clang/test/Driver/sycl-int-footer.cpp

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
// RUN: | FileCheck -check-prefix FOOTER %s -DSRCDIR=%/S -DCMDDIR=cmdline/dir
44
// FOOTER: clang{{.*}} "-fsycl-is-device"{{.*}} "-fsycl-int-header=[[INTHEADER:.+\.h]]" "-fsycl-int-footer=[[INTFOOTER:.+\h]]" "-sycl-std={{.*}}"{{.*}} "-include" "dummy.h"
55
// FOOTER: append-file{{.*}} "[[INPUTFILE:.+\.cpp]]" "--append=[[INTFOOTER]]" "--orig-filename=[[INPUTFILE]]" "--output=[[APPENDEDSRC:.+\.cpp]]"
6-
// FOOTER: clang{{.*}} "-include" "[[INTHEADER]]"{{.*}} "-fsycl-is-host"{{.*}} "-include" "dummy.h"{{.*}} "-iquote" "[[SRCDIR]]" "-I" "cmdline/dir"
6+
// FOOTER: clang{{.*}} "-include" "[[INTHEADER]]"{{.*}} "-fsycl-is-host"{{.*}} "-main-file-name" "[[SRCFILE:.+\cpp]]" "-fsycl-use-main-file-name{{.*}} "-include" "dummy.h"{{.*}} "-iquote" "[[SRCDIR]]" "-I" "cmdline/dir"
77
// FOOTER-NOT: "-include" "[[INTHEADER]]"
88

99
/// Preprocessed file creation with integration footer
@@ -24,9 +24,15 @@
2424

2525
/// Check that integration footer can be disabled
2626
// RUN: %clangxx -fsycl -fno-sycl-use-footer %s -### 2>&1 \
27-
// RUN: | FileCheck -check-prefix NO-FOOTER --implicit-check-not "-fsycl-int-footer" %s
27+
// RUN: | FileCheck -check-prefix NO-FOOTER --implicit-check-not "-fsycl-int-footer" --implicit-check-not "-fsycl-use-main-file-name" %s
2828
// NO-FOOTER: clang{{.*}} "-fsycl-is-device"{{.*}} "-fsycl-int-header=[[INTHEADER:.+\.h]]" "-sycl-std={{.*}}"
29-
// NO-FOOTER: clang{{.*}} "-include" "[[INTHEADER]]"{{.*}} "-fsycl-is-host"{{.*}} "-o"
29+
// NO-FOOTER: clang{{.*}} "-include" "[[INTHEADER]]"{{.*}} "-fsycl-is-host"{{.*}} "-main-file-name" "sycl-int-footer.cpp"{{.*}} "-o"
30+
31+
// Test that -fsycl-use-main-file-name is not passed if -fsycl is not passed.
32+
// This is test is located here, because -fsycl-use-main-file-name is tightly
33+
// connected to the integration footer.
34+
// RUN: %clangxx %s -### 2>&1 | FileCheck %s --check-prefix NO-FSYCL --implicit-check-not "-fsycl-use-main-file-name"
35+
// NO-FSYCL: clang{{.*}} "-main-file-name" "sycl-int-footer.cpp"
3036

3137
/// Check phases without integration footer
3238
// RUN: %clangxx -fsycl -fno-sycl-instrument-device-code -fno-sycl-device-lib=all -fno-sycl-use-footer -target x86_64-unknown-linux-gnu %s -ccc-print-phases 2>&1 \

0 commit comments

Comments
 (0)