Skip to content

[SYCL] Fix ICE when -g and -ffile-prefix-map flags are set #8459

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

Merged
merged 10 commits into from
Mar 8, 2023
36 changes: 25 additions & 11 deletions clang/lib/CodeGen/CGDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -392,8 +392,8 @@ std::optional<StringRef> CGDebugInfo::getSource(const SourceManager &SM,
FileID ComputeValidFileID(SourceManager &SM, StringRef FileName) {
FileID MainFileID = SM.getMainFileID();
// Find the filename FileName and load it.
llvm::Expected<FileEntryRef> ExpectedFileRef =
SM.getFileManager().getFileRef(FileName);
llvm::Expected<FileEntryRef> ExpectedFileRef =
SM.getFileManager().getFileRef(FileName);
if (ExpectedFileRef) {
MainFileID = SM.getOrCreateFileID(ExpectedFileRef.get(),
SrcMgr::CharacteristicKind::C_User);
Expand All @@ -407,18 +407,25 @@ llvm::DIFile *CGDebugInfo::getOrCreateFile(SourceLocation Loc) {
FileID FID;

if (Loc.isInvalid()) {
// The DIFile used by the CU is distinct from the main source file. Call
// createFile() below for canonicalization if the source file was specified
// with an absolute path.
FileName = TheCU->getFile()->getFilename();
if (CGM.getCodeGenOpts().SYCLUseMainFileName &&
CGM.getLangOpts().MacroPrefixMap.size() > 0) {
// When fmacro-prefix-map is used, the original source file
// file name is indicated by FullMainFileName instead of the CU.
auto &CGO = CGM.getCodeGenOpts();
FileName = CGO.FullMainFileName;
FID = ComputeValidFileID(SM, CGO.FullMainFileName);
} else {
// The DIFile used by the CU is distinct from the main source file. Call
// createFile() below for canonicalization if the source file was
// specified with an absolute path.
FileName = TheCU->getFile()->getFilename();
}
} else {
PresumedLoc PLoc = SM.getPresumedLoc(Loc);
FileName = PLoc.getFilename();

if (FileName.empty()) {
FileName = TheCU->getFile()->getFilename();
} else {
FileName = PLoc.getFilename();
}
FID = PLoc.getFileID();
}
Expand Down Expand Up @@ -654,11 +661,18 @@ void CGDebugInfo::CreateCompileUnit() {
// file. Its directory part specifies what becomes the
// DW_AT_comp_dir (the compilation directory), even if the source
// file was specified with an absolute path.
// Unless an integration footer is involved, and the directory part is
// specified by the FileEntryRef provided by the FileID of the main source
// file.
if (CSKind)
CSInfo.emplace(*CSKind, Checksum);
llvm::DIFile *CUFile = DBuilder.createFile(
remapDIPath(MainFileName), remapDIPath(getCurrentDirname()), CSInfo,
getSource(SM, SM.getMainFileID()));

if (!CGM.getCodeGenOpts().SYCLUseMainFileName)
MainFileDir = getCurrentDirname();

llvm::DIFile *CUFile =
DBuilder.createFile(remapDIPath(MainFileName), remapDIPath(MainFileDir),
CSInfo, getSource(SM, SM.getMainFileID()));

StringRef Sysroot, SDK;
if (CGM.getCodeGenOpts().getDebuggerTuning() == llvm::DebuggerKind::LLDB) {
Expand Down
1 change: 1 addition & 0 deletions clang/test/CodeGenSYCL/debug-info-checksum-temp-name.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
//
// CHECK: !DICompileUnit({{.*}} file: ![[#FILE:]]
// CHECK: ![[#FILE]] = !DIFile(filename: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs{{.+}}debug-info-checksum.cpp"
// CHECK-SAME: directory: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL{{.+}}Inputs"
// CHECK-SAME: checksumkind: CSK_MD5, checksum: "f1fb5d68350b47d90a53968ac8c40529"

#include "Inputs/debug-info-checksum.cpp"
6 changes: 4 additions & 2 deletions clang/test/CodeGenSYCL/debug-info-file-checksum.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,13 @@
// RUN: | FileCheck %s -check-prefix=COMP2

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

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

// TODO: Fails on windows because of the use of append-file command that returns
Expand Down
22 changes: 22 additions & 0 deletions clang/test/CodeGenSYCL/debug-info-file-prefix-map.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// This test checks that in the presence of the option -fdebug-prefix-map
// the DICompileUnit information is correct, i.e test filename and directory
// path are correct.

// RUN: %clang_cc1 -triple spir64-unknown-unknown -fsycl-is-device \
// RUN: -fsycl-int-header=%t.header.h -fsycl-int-footer=%t.footer.h \
// RUN: -main-file-name %s -fsycl-use-main-file-name \
// RUN: -full-main-file-name %s \
// RUN: -fmacro-prefix-map=%S/= -fcoverage-prefix-map=%S/= \
// RUN: -fdebug-prefix-map=%S/= \
// RUN: -debug-info-kind=constructor -emit-llvm -O0 -o - %s \
// RUN: | FileCheck %s


// CHECK: distinct !DICompileUnit(language: DW_LANG_C_plus_plus_14, file: ![[#FILE1:]]
// CHECK-NEXT: ![[#FILE1]] = !DIFile(filename: "debug-info-file-prefix-map.cpp"
// CHECK-SAME: directory: "{{.*}}clang{{.+}}test{{.+}}CodeGenSYCL"
// CHECK: ![[#FILE2:]] = !DIFile(filename: "debug-info-file-prefix-map.cpp", directory: "")
// CHECK: !DIDerivedType(tag: DW_TAG_typedef, name: "__builtin_va_list", file: ![[#FILE2]]

void a(__builtin_va_list);
using ::a;