Skip to content

Commit 8560756

Browse files
authored
Keep instructions used by the DebugBuildIdentifier (KhronosGroup#6189)
Update DCE to keep instructions referred to by the DebugBuildIdentifier and update the test to verify these are present in the output SPIR-V.
1 parent c837be3 commit 8560756

File tree

2 files changed

+38
-4
lines changed

2 files changed

+38
-4
lines changed

source/opt/aggressive_dead_code_elim_pass.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,8 +916,17 @@ bool AggressiveDCEPass::ProcessGlobalValues() {
916916
}
917917
// Save debug build identifier even if no other instructions refer to it.
918918
if (dbg.GetShader100DebugOpcode() ==
919-
NonSemanticShaderDebugInfo100DebugBuildIdentifier)
919+
NonSemanticShaderDebugInfo100DebugBuildIdentifier) {
920+
// The debug build identifier refers to other instructions that
921+
// can potentially be removed, they also need to be kept alive.
922+
dbg.ForEachInId([this](const uint32_t* id) {
923+
Instruction* ref_inst = get_def_use_mgr()->GetDef(*id);
924+
if (ref_inst) {
925+
live_insts_.Set(ref_inst->unique_id());
926+
}
927+
});
920928
continue;
929+
}
921930
to_kill_.push_back(&dbg);
922931
modified = true;
923932
}

test/opt/optimizer_test.cpp

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,14 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
#include "spirv-tools/optimizer.hpp"
16+
17+
#include <sstream>
1518
#include <string>
1619
#include <vector>
1720

1821
#include "gmock/gmock.h"
1922
#include "spirv-tools/libspirv.hpp"
20-
#include "spirv-tools/optimizer.hpp"
2123
#include "test/opt/pass_fixture.h"
2224

2325
namespace spvtools {
@@ -567,9 +569,32 @@ OpFunctionEnd
567569
SPV_BINARY_TO_TEXT_OPTION_NO_HEADER);
568570

569571
// Test that the DebugBuildIdentifier is not removed after DCE.
570-
bool found = after.find("DebugBuildIdentifier") != std::string::npos;
571-
EXPECT_TRUE(found)
572+
size_t dbi_pos = after.find("DebugBuildIdentifier");
573+
EXPECT_NE(dbi_pos, std::string::npos)
572574
<< "Was expecting the DebugBuildIdentifier to have been kept.";
575+
std::string string_id;
576+
std::string flags_id;
577+
if (dbi_pos != std::string::npos) {
578+
std::stringstream ss(after.substr(dbi_pos));
579+
std::string temp;
580+
char percent;
581+
ss >> temp; // Consume "DebugBuildIdentifier"
582+
ss >> percent >> string_id;
583+
ss >> percent >> flags_id;
584+
}
585+
586+
EXPECT_FALSE(string_id.empty())
587+
<< "Could not find string id for DebugBuildIdentifier.";
588+
EXPECT_FALSE(flags_id.empty())
589+
<< "Could not find flags id for DebugBuildIdentifier.";
590+
591+
bool found =
592+
(after.find("%" + string_id + " = OpString") != std::string::npos);
593+
EXPECT_TRUE(found)
594+
<< "Was expecting the DebugBuildIdentifier string to have been kept.";
595+
found = (after.find("%" + flags_id + " = OpConstant") != std::string::npos);
596+
EXPECT_TRUE(found)
597+
<< "Was expecting the DebugBuildIdentifier constant to have been kept.";
573598
}
574599

575600
} // namespace

0 commit comments

Comments
 (0)