Skip to content

Commit 97b5edb

Browse files
[SPIRV] Fix DebugSource for files which are not found (#7662)
#4218 and #4362 identified and partially fixed problems with DebugSource generation. #6085 recognized that exe and API users were having different outputs in DebugSource, and attempted to fix that. However, as the conversation in #7569 shows, the solution was not entirely correct. This PR causes dxc to connect the input source string, whether by exe or API, to one and only one DebugSource, like other compilers. Files which are not found produce a DebugSource without the second operand. This PR fixes #7569
1 parent d64d34c commit 97b5edb

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

tools/clang/lib/SPIRV/EmitVisitor.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,8 @@ uint32_t getHeaderVersion(spv_target_env env) {
136136
std::string
137137
ReadSourceCode(llvm::StringRef filePath,
138138
const clang::spirv::SpirvCodeGenOptions &spvOptions) {
139+
140+
std::string localFilePath(filePath.begin(), filePath.end());
139141
try {
140142
dxc::DxcDllSupport dllSupport;
141143
IFT(dllSupport.Initialize());
@@ -154,7 +156,10 @@ ReadSourceCode(llvm::StringRef filePath,
154156
} catch (...) {
155157
// An exception has occurred while reading the file
156158
// return the original source (which may have been supplied directly)
157-
if (!spvOptions.origSource.empty()) {
159+
// only for the main input file
160+
if ((!strcmp(localFilePath.c_str(), "hlsl.hlsl") &&
161+
spvOptions.inputFile.empty()) ||
162+
!strcmp(localFilePath.c_str(), spvOptions.inputFile.c_str())) {
158163
return spvOptions.origSource.c_str();
159164
}
160165
return "";

tools/clang/unittests/SPIRV/CodeGenSpirvTest.cpp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,4 +41,26 @@ float4 PSMain(float4 color : COLOR) : SV_TARGET { return color; }
4141
"OpSource HLSL 600 %4 \"// RUN: %dxc -T ps_6_0 -E PSMain -Zi"));
4242
}
4343

44+
// This test demonstrates that in-memory source is transmitted
45+
// to OpDebugSource only once when there are multiple files referenced
46+
TEST_F(LibTest, InlinedCodeWithDebugMultipleFilesTest) {
47+
const std::string command(
48+
R"(// RUN: %dxc -T vs_6_0 -E main -fspv-debug=vulkan-with-source)");
49+
const std::string code = command + R"(
50+
#line 1 "otherfile.hlsl"
51+
struct VertexOutput {
52+
[[vk::location(0)]] float3 Color : COLOR0;
53+
};
54+
55+
float4 main(VertexOutput v) : SV_Position
56+
{
57+
return float4(v.Color, 1.0);
58+
}
59+
)";
60+
std::string spirv = compileCodeAndGetSpirvAsm(code);
61+
EXPECT_THAT(spirv, ContainsRegex("%50 = OpString \"// RUN: %dxc -T vs_6_0 -E "
62+
"main -fspv-debug=vulkan-with-source"));
63+
EXPECT_THAT(spirv, ContainsRegex("DebugSource %23\n"));
64+
EXPECT_THAT(spirv, ContainsRegex("DebugSource %5 %50\n"));
65+
}
4466
} // namespace

0 commit comments

Comments
 (0)