Skip to content

Commit 723b80d

Browse files
authored
[SYCL] Fix ICE when -g and -ffile-prefix-map flags are set (#8459)
Set the source file name when footer is present and option fmacro-prefix-map is used when generating debug information. This patch is to fix a crash in the compiler when generating debug information with the option -ffile-prefix-map. Example: $ pwd /tmp/tmp.jocQk9WVa0 $ $ cat builtin.cpp void a(__builtin_va_list); using ::a; $ $ clang -c -g -fsycl /tmp/tmp.jocQk9WVa0/builtin.cpp $ $ clang -g -fsycl -ffile-prefix-map=/tmp/= /tmp/tmp.jocQk9WVa0/builtin.cpp Expected<T> must be checked before access or destruction. Unchecked Expected<T> contained error: No such file or directoryPLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace, preprocessed source, and associated run script. Stack dump: 0. Program arguments: …………………/tmp/tmp.jocQk9WVa0/builtin.cpp 1. <eof> parser at end of file 2. /tmp/tmp.jocQk9WVa0/builtin.cpp:2:9: LLVM IR generation of declaration 'a https://github.com/intel/llvm/blob/sycl/clang/lib/CodeGen/CGDebugInfo.cpp#L397 is returning a error because the FileName is wrong.
1 parent 6a47be0 commit 723b80d

File tree

4 files changed

+52
-13
lines changed

4 files changed

+52
-13
lines changed

clang/lib/CodeGen/CGDebugInfo.cpp

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -392,8 +392,8 @@ std::optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
392392
FileID ComputeValidFileID(SourceManager &SM, StringRef FileName) {
393393
FileID MainFileID = SM.getMainFileID();
394394
// Find the filename FileName and load it.
395-
llvm::Expected<FileEntryRef> ExpectedFileRef =
396-
SM.getFileManager().getFileRef(FileName);
395+
llvm::Expected<FileEntryRef> ExpectedFileRef =
396+
SM.getFileManager().getFileRef(FileName);
397397
if (ExpectedFileRef) {
398398
MainFileID = SM.getOrCreateFileID(ExpectedFileRef.get(),
399399
SrcMgr::CharacteristicKind::C_User);
@@ -407,18 +407,25 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
407407
FileID FID;
408408

409409
if (Loc.isInvalid()) {
410-
// The DIFile used by the CU is distinct from the main source file. Call
411-
// createFile() below for canonicalization if the source file was specified
412-
// with an absolute path.
413-
FileName = TheCU->getFile()->getFilename();
410+
if (CGM.getCodeGenOpts().SYCLUseMainFileName &&
411+
CGM.getLangOpts().MacroPrefixMap.size() > 0) {
412+
// When fmacro-prefix-map is used, the original source file
413+
// file name is indicated by FullMainFileName instead of the CU.
414+
auto &CGO = CGM.getCodeGenOpts();
415+
FileName = CGO.FullMainFileName;
416+
FID = ComputeValidFileID(SM, CGO.FullMainFileName);
417+
} else {
418+
// The DIFile used by the CU is distinct from the main source file. Call
419+
// createFile() below for canonicalization if the source file was
420+
// specified with an absolute path.
421+
FileName = TheCU->getFile()->getFilename();
422+
}
414423
} else {
415424
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
416425
FileName = PLoc.getFilename();
417426

418427
if (FileName.empty()) {
419428
FileName = TheCU->getFile()->getFilename();
420-
} else {
421-
FileName = PLoc.getFilename();
422429
}
423430
FID = PLoc.getFileID();
424431
}
@@ -654,11 +661,18 @@ void CGDebugInfo::CreateCompileUnit() {
654661
// file. Its directory part specifies what becomes the
655662
// DW_AT_comp_dir (the compilation directory), even if the source
656663
// file was specified with an absolute path.
664+
// Unless an integration footer is involved, and the directory part is
665+
// specified by the FileEntryRef provided by the FileID of the main source
666+
// file.
657667
if (CSKind)
658668
CSInfo.emplace(*CSKind, Checksum);
659-
llvm::DIFile *CUFile = DBuilder.createFile(
660-
remapDIPath(MainFileName), remapDIPath(getCurrentDirname()), CSInfo,
661-
getSource(SM, SM.getMainFileID()));
669+
670+
if (!CGM.getCodeGenOpts().SYCLUseMainFileName)
671+
MainFileDir = getCurrentDirname();
672+
673+
llvm::DIFile *CUFile =
674+
DBuilder.createFile(remapDIPath(MainFileName), remapDIPath(MainFileDir),
675+
CSInfo, getSource(SM, SM.getMainFileID()));
662676

663677
StringRef Sysroot, SDK;
664678
if (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB) {

clang/test/CodeGenSYCL/debug-info-checksum-temp-name.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
//
2525
// CHECK: !DICompileUnit({{.*}} file: ![[#FILE:]]
2626
// CHECK: ![[#FILE]] = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs{{.+}}debug-info-checksum.cpp"
27+
// CHECK-SAME: directory: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs"
2728
// CHECK-SAME: checksumkind: CSK_MD5, checksum: "f1fb5d68350b47d90a53968ac8c40529"
2829

2930
#include "Inputs/debug-info-checksum.cpp"

clang/test/CodeGenSYCL/debug-info-file-checksum.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,11 +25,13 @@
2525
// RUN: | FileCheck %s -check-prefix=COMP2
2626

2727
// COMP1: !DICompileUnit({{.*}} file: ![[#FILE1:]]
28-
// COMP1: ![[#FILE1]] = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}checksum.cpp"
28+
// COMP1: ![[#FILE1]] = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs{{.+}}checksum.cpp"
29+
// COMP1-SAME: directory: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL/Inputs"
2930
// COMP1-SAME: checksumkind: CSK_MD5, checksum: "259269f735d83ec32c46a11352458493")
3031

3132
// COMP2: !DICompileUnit({{.*}} file: ![[#FILE2:]]
32-
// COMP2: ![[#FILE2]] = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}checksum.cpp"
33+
// COMP2: ![[#FILE2]] = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs{{.+}}checksum.cpp"
34+
// COMP2: directory: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL/Output"
3335
// COMP2-SAME: checksumkind: CSK_MD5, checksum: "259269f735d83ec32c46a11352458493")
3436

3537
// TODO: Fails on windows because of the use of append-file command that returns
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// This test checks that in the presence of the option -fdebug-prefix-map
2+
// the DICompileUnit information is correct, i.e test filename and directory
3+
// path are correct.
4+
5+
// RUN: %clang_cc1 -triple spir64-unknown-unknown -fsycl-is-device \
6+
// RUN: -fsycl-int-header=%t.header.h -fsycl-int-footer=%t.footer.h \
7+
// RUN: -main-file-name %s -fsycl-use-main-file-name \
8+
// RUN: -full-main-file-name %s \
9+
// RUN: -fmacro-prefix-map=%S/= -fcoverage-prefix-map=%S/= \
10+
// RUN: -fdebug-prefix-map=%S/= \
11+
// RUN: -debug-info-kind=constructor -emit-llvm -O0 -o - %s \
12+
// RUN: | FileCheck %s
13+
14+
15+
// CHECK: distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: ![[#FILE1:]]
16+
// CHECK-NEXT: ![[#FILE1]] = !DIFile(filename: "debug-info-file-prefix-map.cpp"
17+
// CHECK-SAME: directory: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL"
18+
// CHECK: ![[#FILE2:]] = !DIFile(filename: "debug-info-file-prefix-map.cpp", directory: "")
19+
// CHECK: !DIDerivedType(tag: DW_TAG_typedef, name: "__builtin_va_list", file: ![[#FILE2]]
20+
21+
void a(__builtin_va_list);
22+
using ::a;

0 commit comments

Comments
 (0)